linux/fs/nfsd/nfs4state.c
<<
>>
Prefs
   1/*
   2*  Copyright (c) 2001 The Regents of the University of Michigan.
   3*  All rights reserved.
   4*
   5*  Kendrick Smith <kmsmith@umich.edu>
   6*  Andy Adamson <kandros@umich.edu>
   7*
   8*  Redistribution and use in source and binary forms, with or without
   9*  modification, are permitted provided that the following conditions
  10*  are met:
  11*
  12*  1. Redistributions of source code must retain the above copyright
  13*     notice, this list of conditions and the following disclaimer.
  14*  2. Redistributions in binary form must reproduce the above copyright
  15*     notice, this list of conditions and the following disclaimer in the
  16*     documentation and/or other materials provided with the distribution.
  17*  3. Neither the name of the University nor the names of its
  18*     contributors may be used to endorse or promote products derived
  19*     from this software without specific prior written permission.
  20*
  21*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23*  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24*  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25*  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28*  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29*  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32*
  33*/
  34
  35#include <linux/file.h>
  36#include <linux/fs.h>
  37#include <linux/slab.h>
  38#include <linux/namei.h>
  39#include <linux/swap.h>
  40#include <linux/pagemap.h>
  41#include <linux/ratelimit.h>
  42#include <linux/sunrpc/svcauth_gss.h>
  43#include <linux/sunrpc/addr.h>
  44#include <linux/jhash.h>
  45#include "xdr4.h"
  46#include "xdr4cb.h"
  47#include "vfs.h"
  48#include "current_stateid.h"
  49
  50#include "netns.h"
  51#include "pnfs.h"
  52
  53#define NFSDDBG_FACILITY                NFSDDBG_PROC
  54
  55#define all_ones {{~0,~0},~0}
  56static const stateid_t one_stateid = {
  57        .si_generation = ~0,
  58        .si_opaque = all_ones,
  59};
  60static const stateid_t zero_stateid = {
  61        /* all fields zero */
  62};
  63static const stateid_t currentstateid = {
  64        .si_generation = 1,
  65};
  66
  67static u64 current_sessionid = 1;
  68
  69#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  70#define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  71#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  72
  73/* forward declarations */
  74static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
  75static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
  76
  77/* Locking: */
  78
  79/*
  80 * Currently used for the del_recall_lru and file hash table.  In an
  81 * effort to decrease the scope of the client_mutex, this spinlock may
  82 * eventually cover more:
  83 */
  84static DEFINE_SPINLOCK(state_lock);
  85
  86/*
  87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
  88 * the refcount on the open stateid to drop.
  89 */
  90static DECLARE_WAIT_QUEUE_HEAD(close_wq);
  91
  92static struct kmem_cache *openowner_slab;
  93static struct kmem_cache *lockowner_slab;
  94static struct kmem_cache *file_slab;
  95static struct kmem_cache *stateid_slab;
  96static struct kmem_cache *deleg_slab;
  97static struct kmem_cache *odstate_slab;
  98
  99static void free_session(struct nfsd4_session *);
 100
 101static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
 102static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
 103
 104static bool is_session_dead(struct nfsd4_session *ses)
 105{
 106        return ses->se_flags & NFS4_SESSION_DEAD;
 107}
 108
 109static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
 110{
 111        if (atomic_read(&ses->se_ref) > ref_held_by_me)
 112                return nfserr_jukebox;
 113        ses->se_flags |= NFS4_SESSION_DEAD;
 114        return nfs_ok;
 115}
 116
 117static bool is_client_expired(struct nfs4_client *clp)
 118{
 119        return clp->cl_time == 0;
 120}
 121
 122static __be32 get_client_locked(struct nfs4_client *clp)
 123{
 124        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 125
 126        lockdep_assert_held(&nn->client_lock);
 127
 128        if (is_client_expired(clp))
 129                return nfserr_expired;
 130        atomic_inc(&clp->cl_refcount);
 131        return nfs_ok;
 132}
 133
 134/* must be called under the client_lock */
 135static inline void
 136renew_client_locked(struct nfs4_client *clp)
 137{
 138        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 139
 140        if (is_client_expired(clp)) {
 141                WARN_ON(1);
 142                printk("%s: client (clientid %08x/%08x) already expired\n",
 143                        __func__,
 144                        clp->cl_clientid.cl_boot,
 145                        clp->cl_clientid.cl_id);
 146                return;
 147        }
 148
 149        dprintk("renewing client (clientid %08x/%08x)\n",
 150                        clp->cl_clientid.cl_boot,
 151                        clp->cl_clientid.cl_id);
 152        list_move_tail(&clp->cl_lru, &nn->client_lru);
 153        clp->cl_time = get_seconds();
 154}
 155
 156static void put_client_renew_locked(struct nfs4_client *clp)
 157{
 158        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 159
 160        lockdep_assert_held(&nn->client_lock);
 161
 162        if (!atomic_dec_and_test(&clp->cl_refcount))
 163                return;
 164        if (!is_client_expired(clp))
 165                renew_client_locked(clp);
 166}
 167
 168static void put_client_renew(struct nfs4_client *clp)
 169{
 170        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 171
 172        if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
 173                return;
 174        if (!is_client_expired(clp))
 175                renew_client_locked(clp);
 176        spin_unlock(&nn->client_lock);
 177}
 178
 179static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
 180{
 181        __be32 status;
 182
 183        if (is_session_dead(ses))
 184                return nfserr_badsession;
 185        status = get_client_locked(ses->se_client);
 186        if (status)
 187                return status;
 188        atomic_inc(&ses->se_ref);
 189        return nfs_ok;
 190}
 191
 192static void nfsd4_put_session_locked(struct nfsd4_session *ses)
 193{
 194        struct nfs4_client *clp = ses->se_client;
 195        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 196
 197        lockdep_assert_held(&nn->client_lock);
 198
 199        if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
 200                free_session(ses);
 201        put_client_renew_locked(clp);
 202}
 203
 204static void nfsd4_put_session(struct nfsd4_session *ses)
 205{
 206        struct nfs4_client *clp = ses->se_client;
 207        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 208
 209        spin_lock(&nn->client_lock);
 210        nfsd4_put_session_locked(ses);
 211        spin_unlock(&nn->client_lock);
 212}
 213
 214static struct nfsd4_blocked_lock *
 215find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
 216                        struct nfsd_net *nn)
 217{
 218        struct nfsd4_blocked_lock *cur, *found = NULL;
 219
 220        spin_lock(&nn->blocked_locks_lock);
 221        list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
 222                if (fh_match(fh, &cur->nbl_fh)) {
 223                        list_del_init(&cur->nbl_list);
 224                        list_del_init(&cur->nbl_lru);
 225                        found = cur;
 226                        break;
 227                }
 228        }
 229        spin_unlock(&nn->blocked_locks_lock);
 230        if (found)
 231                posix_unblock_lock(&found->nbl_lock);
 232        return found;
 233}
 234
 235static struct nfsd4_blocked_lock *
 236find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
 237                        struct nfsd_net *nn)
 238{
 239        struct nfsd4_blocked_lock *nbl;
 240
 241        nbl = find_blocked_lock(lo, fh, nn);
 242        if (!nbl) {
 243                nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
 244                if (nbl) {
 245                        fh_copy_shallow(&nbl->nbl_fh, fh);
 246                        locks_init_lock(&nbl->nbl_lock);
 247                        nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
 248                                        &nfsd4_cb_notify_lock_ops,
 249                                        NFSPROC4_CLNT_CB_NOTIFY_LOCK);
 250                }
 251        }
 252        return nbl;
 253}
 254
 255static void
 256free_blocked_lock(struct nfsd4_blocked_lock *nbl)
 257{
 258        locks_release_private(&nbl->nbl_lock);
 259        kfree(nbl);
 260}
 261
 262static void
 263remove_blocked_locks(struct nfs4_lockowner *lo)
 264{
 265        struct nfs4_client *clp = lo->lo_owner.so_client;
 266        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 267        struct nfsd4_blocked_lock *nbl;
 268        LIST_HEAD(reaplist);
 269
 270        /* Dequeue all blocked locks */
 271        spin_lock(&nn->blocked_locks_lock);
 272        while (!list_empty(&lo->lo_blocked)) {
 273                nbl = list_first_entry(&lo->lo_blocked,
 274                                        struct nfsd4_blocked_lock,
 275                                        nbl_list);
 276                list_del_init(&nbl->nbl_list);
 277                list_move(&nbl->nbl_lru, &reaplist);
 278        }
 279        spin_unlock(&nn->blocked_locks_lock);
 280
 281        /* Now free them */
 282        while (!list_empty(&reaplist)) {
 283                nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
 284                                        nbl_lru);
 285                list_del_init(&nbl->nbl_lru);
 286                posix_unblock_lock(&nbl->nbl_lock);
 287                free_blocked_lock(nbl);
 288        }
 289}
 290
 291static int
 292nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
 293{
 294        /*
 295         * Since this is just an optimization, we don't try very hard if it
 296         * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
 297         * just quit trying on anything else.
 298         */
 299        switch (task->tk_status) {
 300        case -NFS4ERR_DELAY:
 301                rpc_delay(task, 1 * HZ);
 302                return 0;
 303        default:
 304                return 1;
 305        }
 306}
 307
 308static void
 309nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
 310{
 311        struct nfsd4_blocked_lock       *nbl = container_of(cb,
 312                                                struct nfsd4_blocked_lock, nbl_cb);
 313
 314        free_blocked_lock(nbl);
 315}
 316
 317static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
 318        .done           = nfsd4_cb_notify_lock_done,
 319        .release        = nfsd4_cb_notify_lock_release,
 320};
 321
 322static inline struct nfs4_stateowner *
 323nfs4_get_stateowner(struct nfs4_stateowner *sop)
 324{
 325        atomic_inc(&sop->so_count);
 326        return sop;
 327}
 328
 329static int
 330same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
 331{
 332        return (sop->so_owner.len == owner->len) &&
 333                0 == memcmp(sop->so_owner.data, owner->data, owner->len);
 334}
 335
 336static struct nfs4_openowner *
 337find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
 338                        struct nfs4_client *clp)
 339{
 340        struct nfs4_stateowner *so;
 341
 342        lockdep_assert_held(&clp->cl_lock);
 343
 344        list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
 345                            so_strhash) {
 346                if (!so->so_is_open_owner)
 347                        continue;
 348                if (same_owner_str(so, &open->op_owner))
 349                        return openowner(nfs4_get_stateowner(so));
 350        }
 351        return NULL;
 352}
 353
 354static struct nfs4_openowner *
 355find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
 356                        struct nfs4_client *clp)
 357{
 358        struct nfs4_openowner *oo;
 359
 360        spin_lock(&clp->cl_lock);
 361        oo = find_openstateowner_str_locked(hashval, open, clp);
 362        spin_unlock(&clp->cl_lock);
 363        return oo;
 364}
 365
 366static inline u32
 367opaque_hashval(const void *ptr, int nbytes)
 368{
 369        unsigned char *cptr = (unsigned char *) ptr;
 370
 371        u32 x = 0;
 372        while (nbytes--) {
 373                x *= 37;
 374                x += *cptr++;
 375        }
 376        return x;
 377}
 378
 379static void nfsd4_free_file_rcu(struct rcu_head *rcu)
 380{
 381        struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
 382
 383        kmem_cache_free(file_slab, fp);
 384}
 385
 386void
 387put_nfs4_file(struct nfs4_file *fi)
 388{
 389        might_lock(&state_lock);
 390
 391        if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
 392                hlist_del_rcu(&fi->fi_hash);
 393                spin_unlock(&state_lock);
 394                WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
 395                WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
 396                call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
 397        }
 398}
 399
 400static struct file *
 401__nfs4_get_fd(struct nfs4_file *f, int oflag)
 402{
 403        if (f->fi_fds[oflag])
 404                return get_file(f->fi_fds[oflag]);
 405        return NULL;
 406}
 407
 408static struct file *
 409find_writeable_file_locked(struct nfs4_file *f)
 410{
 411        struct file *ret;
 412
 413        lockdep_assert_held(&f->fi_lock);
 414
 415        ret = __nfs4_get_fd(f, O_WRONLY);
 416        if (!ret)
 417                ret = __nfs4_get_fd(f, O_RDWR);
 418        return ret;
 419}
 420
 421static struct file *
 422find_writeable_file(struct nfs4_file *f)
 423{
 424        struct file *ret;
 425
 426        spin_lock(&f->fi_lock);
 427        ret = find_writeable_file_locked(f);
 428        spin_unlock(&f->fi_lock);
 429
 430        return ret;
 431}
 432
 433static struct file *find_readable_file_locked(struct nfs4_file *f)
 434{
 435        struct file *ret;
 436
 437        lockdep_assert_held(&f->fi_lock);
 438
 439        ret = __nfs4_get_fd(f, O_RDONLY);
 440        if (!ret)
 441                ret = __nfs4_get_fd(f, O_RDWR);
 442        return ret;
 443}
 444
 445static struct file *
 446find_readable_file(struct nfs4_file *f)
 447{
 448        struct file *ret;
 449
 450        spin_lock(&f->fi_lock);
 451        ret = find_readable_file_locked(f);
 452        spin_unlock(&f->fi_lock);
 453
 454        return ret;
 455}
 456
 457struct file *
 458find_any_file(struct nfs4_file *f)
 459{
 460        struct file *ret;
 461
 462        spin_lock(&f->fi_lock);
 463        ret = __nfs4_get_fd(f, O_RDWR);
 464        if (!ret) {
 465                ret = __nfs4_get_fd(f, O_WRONLY);
 466                if (!ret)
 467                        ret = __nfs4_get_fd(f, O_RDONLY);
 468        }
 469        spin_unlock(&f->fi_lock);
 470        return ret;
 471}
 472
 473static atomic_long_t num_delegations;
 474unsigned long max_delegations;
 475
 476/*
 477 * Open owner state (share locks)
 478 */
 479
 480/* hash tables for lock and open owners */
 481#define OWNER_HASH_BITS              8
 482#define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
 483#define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
 484
 485static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
 486{
 487        unsigned int ret;
 488
 489        ret = opaque_hashval(ownername->data, ownername->len);
 490        return ret & OWNER_HASH_MASK;
 491}
 492
 493/* hash table for nfs4_file */
 494#define FILE_HASH_BITS                   8
 495#define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
 496
 497static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
 498{
 499        return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
 500}
 501
 502static unsigned int file_hashval(struct knfsd_fh *fh)
 503{
 504        return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
 505}
 506
 507static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
 508
 509static void
 510__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
 511{
 512        lockdep_assert_held(&fp->fi_lock);
 513
 514        if (access & NFS4_SHARE_ACCESS_WRITE)
 515                atomic_inc(&fp->fi_access[O_WRONLY]);
 516        if (access & NFS4_SHARE_ACCESS_READ)
 517                atomic_inc(&fp->fi_access[O_RDONLY]);
 518}
 519
 520static __be32
 521nfs4_file_get_access(struct nfs4_file *fp, u32 access)
 522{
 523        lockdep_assert_held(&fp->fi_lock);
 524
 525        /* Does this access mode make sense? */
 526        if (access & ~NFS4_SHARE_ACCESS_BOTH)
 527                return nfserr_inval;
 528
 529        /* Does it conflict with a deny mode already set? */
 530        if ((access & fp->fi_share_deny) != 0)
 531                return nfserr_share_denied;
 532
 533        __nfs4_file_get_access(fp, access);
 534        return nfs_ok;
 535}
 536
 537static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
 538{
 539        /* Common case is that there is no deny mode. */
 540        if (deny) {
 541                /* Does this deny mode make sense? */
 542                if (deny & ~NFS4_SHARE_DENY_BOTH)
 543                        return nfserr_inval;
 544
 545                if ((deny & NFS4_SHARE_DENY_READ) &&
 546                    atomic_read(&fp->fi_access[O_RDONLY]))
 547                        return nfserr_share_denied;
 548
 549                if ((deny & NFS4_SHARE_DENY_WRITE) &&
 550                    atomic_read(&fp->fi_access[O_WRONLY]))
 551                        return nfserr_share_denied;
 552        }
 553        return nfs_ok;
 554}
 555
 556static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
 557{
 558        might_lock(&fp->fi_lock);
 559
 560        if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
 561                struct file *f1 = NULL;
 562                struct file *f2 = NULL;
 563
 564                swap(f1, fp->fi_fds[oflag]);
 565                if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
 566                        swap(f2, fp->fi_fds[O_RDWR]);
 567                spin_unlock(&fp->fi_lock);
 568                if (f1)
 569                        fput(f1);
 570                if (f2)
 571                        fput(f2);
 572        }
 573}
 574
 575static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
 576{
 577        WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
 578
 579        if (access & NFS4_SHARE_ACCESS_WRITE)
 580                __nfs4_file_put_access(fp, O_WRONLY);
 581        if (access & NFS4_SHARE_ACCESS_READ)
 582                __nfs4_file_put_access(fp, O_RDONLY);
 583}
 584
 585/*
 586 * Allocate a new open/delegation state counter. This is needed for
 587 * pNFS for proper return on close semantics.
 588 *
 589 * Note that we only allocate it for pNFS-enabled exports, otherwise
 590 * all pointers to struct nfs4_clnt_odstate are always NULL.
 591 */
 592static struct nfs4_clnt_odstate *
 593alloc_clnt_odstate(struct nfs4_client *clp)
 594{
 595        struct nfs4_clnt_odstate *co;
 596
 597        co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
 598        if (co) {
 599                co->co_client = clp;
 600                atomic_set(&co->co_odcount, 1);
 601        }
 602        return co;
 603}
 604
 605static void
 606hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
 607{
 608        struct nfs4_file *fp = co->co_file;
 609
 610        lockdep_assert_held(&fp->fi_lock);
 611        list_add(&co->co_perfile, &fp->fi_clnt_odstate);
 612}
 613
 614static inline void
 615get_clnt_odstate(struct nfs4_clnt_odstate *co)
 616{
 617        if (co)
 618                atomic_inc(&co->co_odcount);
 619}
 620
 621static void
 622put_clnt_odstate(struct nfs4_clnt_odstate *co)
 623{
 624        struct nfs4_file *fp;
 625
 626        if (!co)
 627                return;
 628
 629        fp = co->co_file;
 630        if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
 631                list_del(&co->co_perfile);
 632                spin_unlock(&fp->fi_lock);
 633
 634                nfsd4_return_all_file_layouts(co->co_client, fp);
 635                kmem_cache_free(odstate_slab, co);
 636        }
 637}
 638
 639static struct nfs4_clnt_odstate *
 640find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
 641{
 642        struct nfs4_clnt_odstate *co;
 643        struct nfs4_client *cl;
 644
 645        if (!new)
 646                return NULL;
 647
 648        cl = new->co_client;
 649
 650        spin_lock(&fp->fi_lock);
 651        list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
 652                if (co->co_client == cl) {
 653                        get_clnt_odstate(co);
 654                        goto out;
 655                }
 656        }
 657        co = new;
 658        co->co_file = fp;
 659        hash_clnt_odstate_locked(new);
 660out:
 661        spin_unlock(&fp->fi_lock);
 662        return co;
 663}
 664
 665struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
 666                                         struct kmem_cache *slab)
 667{
 668        struct nfs4_stid *stid;
 669        int new_id;
 670
 671        stid = kmem_cache_zalloc(slab, GFP_KERNEL);
 672        if (!stid)
 673                return NULL;
 674
 675        idr_preload(GFP_KERNEL);
 676        spin_lock(&cl->cl_lock);
 677        new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
 678        spin_unlock(&cl->cl_lock);
 679        idr_preload_end();
 680        if (new_id < 0)
 681                goto out_free;
 682        stid->sc_client = cl;
 683        stid->sc_stateid.si_opaque.so_id = new_id;
 684        stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
 685        /* Will be incremented before return to client: */
 686        atomic_set(&stid->sc_count, 1);
 687        spin_lock_init(&stid->sc_lock);
 688
 689        /*
 690         * It shouldn't be a problem to reuse an opaque stateid value.
 691         * I don't think it is for 4.1.  But with 4.0 I worry that, for
 692         * example, a stray write retransmission could be accepted by
 693         * the server when it should have been rejected.  Therefore,
 694         * adopt a trick from the sctp code to attempt to maximize the
 695         * amount of time until an id is reused, by ensuring they always
 696         * "increase" (mod INT_MAX):
 697         */
 698        return stid;
 699out_free:
 700        kmem_cache_free(slab, stid);
 701        return NULL;
 702}
 703
 704static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
 705{
 706        struct nfs4_stid *stid;
 707        struct nfs4_ol_stateid *stp;
 708
 709        stid = nfs4_alloc_stid(clp, stateid_slab);
 710        if (!stid)
 711                return NULL;
 712
 713        stp = openlockstateid(stid);
 714        stp->st_stid.sc_free = nfs4_free_ol_stateid;
 715        return stp;
 716}
 717
 718static void nfs4_free_deleg(struct nfs4_stid *stid)
 719{
 720        kmem_cache_free(deleg_slab, stid);
 721        atomic_long_dec(&num_delegations);
 722}
 723
 724/*
 725 * When we recall a delegation, we should be careful not to hand it
 726 * out again straight away.
 727 * To ensure this we keep a pair of bloom filters ('new' and 'old')
 728 * in which the filehandles of recalled delegations are "stored".
 729 * If a filehandle appear in either filter, a delegation is blocked.
 730 * When a delegation is recalled, the filehandle is stored in the "new"
 731 * filter.
 732 * Every 30 seconds we swap the filters and clear the "new" one,
 733 * unless both are empty of course.
 734 *
 735 * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
 736 * low 3 bytes as hash-table indices.
 737 *
 738 * 'blocked_delegations_lock', which is always taken in block_delegations(),
 739 * is used to manage concurrent access.  Testing does not need the lock
 740 * except when swapping the two filters.
 741 */
 742static DEFINE_SPINLOCK(blocked_delegations_lock);
 743static struct bloom_pair {
 744        int     entries, old_entries;
 745        time_t  swap_time;
 746        int     new; /* index into 'set' */
 747        DECLARE_BITMAP(set[2], 256);
 748} blocked_delegations;
 749
 750static int delegation_blocked(struct knfsd_fh *fh)
 751{
 752        u32 hash;
 753        struct bloom_pair *bd = &blocked_delegations;
 754
 755        if (bd->entries == 0)
 756                return 0;
 757        if (seconds_since_boot() - bd->swap_time > 30) {
 758                spin_lock(&blocked_delegations_lock);
 759                if (seconds_since_boot() - bd->swap_time > 30) {
 760                        bd->entries -= bd->old_entries;
 761                        bd->old_entries = bd->entries;
 762                        memset(bd->set[bd->new], 0,
 763                               sizeof(bd->set[0]));
 764                        bd->new = 1-bd->new;
 765                        bd->swap_time = seconds_since_boot();
 766                }
 767                spin_unlock(&blocked_delegations_lock);
 768        }
 769        hash = jhash(&fh->fh_base, fh->fh_size, 0);
 770        if (test_bit(hash&255, bd->set[0]) &&
 771            test_bit((hash>>8)&255, bd->set[0]) &&
 772            test_bit((hash>>16)&255, bd->set[0]))
 773                return 1;
 774
 775        if (test_bit(hash&255, bd->set[1]) &&
 776            test_bit((hash>>8)&255, bd->set[1]) &&
 777            test_bit((hash>>16)&255, bd->set[1]))
 778                return 1;
 779
 780        return 0;
 781}
 782
 783static void block_delegations(struct knfsd_fh *fh)
 784{
 785        u32 hash;
 786        struct bloom_pair *bd = &blocked_delegations;
 787
 788        hash = jhash(&fh->fh_base, fh->fh_size, 0);
 789
 790        spin_lock(&blocked_delegations_lock);
 791        __set_bit(hash&255, bd->set[bd->new]);
 792        __set_bit((hash>>8)&255, bd->set[bd->new]);
 793        __set_bit((hash>>16)&255, bd->set[bd->new]);
 794        if (bd->entries == 0)
 795                bd->swap_time = seconds_since_boot();
 796        bd->entries += 1;
 797        spin_unlock(&blocked_delegations_lock);
 798}
 799
 800static struct nfs4_delegation *
 801alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
 802                 struct nfs4_clnt_odstate *odstate)
 803{
 804        struct nfs4_delegation *dp;
 805        long n;
 806
 807        dprintk("NFSD alloc_init_deleg\n");
 808        n = atomic_long_inc_return(&num_delegations);
 809        if (n < 0 || n > max_delegations)
 810                goto out_dec;
 811        if (delegation_blocked(&current_fh->fh_handle))
 812                goto out_dec;
 813        dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
 814        if (dp == NULL)
 815                goto out_dec;
 816
 817        dp->dl_stid.sc_free = nfs4_free_deleg;
 818        /*
 819         * delegation seqid's are never incremented.  The 4.1 special
 820         * meaning of seqid 0 isn't meaningful, really, but let's avoid
 821         * 0 anyway just for consistency and use 1:
 822         */
 823        dp->dl_stid.sc_stateid.si_generation = 1;
 824        INIT_LIST_HEAD(&dp->dl_perfile);
 825        INIT_LIST_HEAD(&dp->dl_perclnt);
 826        INIT_LIST_HEAD(&dp->dl_recall_lru);
 827        dp->dl_clnt_odstate = odstate;
 828        get_clnt_odstate(odstate);
 829        dp->dl_type = NFS4_OPEN_DELEGATE_READ;
 830        dp->dl_retries = 1;
 831        nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
 832                      &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
 833        return dp;
 834out_dec:
 835        atomic_long_dec(&num_delegations);
 836        return NULL;
 837}
 838
 839void
 840nfs4_put_stid(struct nfs4_stid *s)
 841{
 842        struct nfs4_file *fp = s->sc_file;
 843        struct nfs4_client *clp = s->sc_client;
 844
 845        might_lock(&clp->cl_lock);
 846
 847        if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
 848                wake_up_all(&close_wq);
 849                return;
 850        }
 851        idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
 852        spin_unlock(&clp->cl_lock);
 853        s->sc_free(s);
 854        if (fp)
 855                put_nfs4_file(fp);
 856}
 857
 858void
 859nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
 860{
 861        stateid_t *src = &stid->sc_stateid;
 862
 863        spin_lock(&stid->sc_lock);
 864        if (unlikely(++src->si_generation == 0))
 865                src->si_generation = 1;
 866        memcpy(dst, src, sizeof(*dst));
 867        spin_unlock(&stid->sc_lock);
 868}
 869
 870static void nfs4_put_deleg_lease(struct nfs4_file *fp)
 871{
 872        struct file *filp = NULL;
 873
 874        spin_lock(&fp->fi_lock);
 875        if (fp->fi_deleg_file && --fp->fi_delegees == 0)
 876                swap(filp, fp->fi_deleg_file);
 877        spin_unlock(&fp->fi_lock);
 878
 879        if (filp) {
 880                vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
 881                fput(filp);
 882        }
 883}
 884
 885void nfs4_unhash_stid(struct nfs4_stid *s)
 886{
 887        s->sc_type = 0;
 888}
 889
 890/**
 891 * nfs4_get_existing_delegation - Discover if this delegation already exists
 892 * @clp:     a pointer to the nfs4_client we're granting a delegation to
 893 * @fp:      a pointer to the nfs4_file we're granting a delegation on
 894 *
 895 * Return:
 896 *      On success: NULL if an existing delegation was not found.
 897 *
 898 *      On error: -EAGAIN if one was previously granted to this nfs4_client
 899 *                 for this nfs4_file.
 900 *
 901 */
 902
 903static int
 904nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
 905{
 906        struct nfs4_delegation *searchdp = NULL;
 907        struct nfs4_client *searchclp = NULL;
 908
 909        lockdep_assert_held(&state_lock);
 910        lockdep_assert_held(&fp->fi_lock);
 911
 912        list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
 913                searchclp = searchdp->dl_stid.sc_client;
 914                if (clp == searchclp) {
 915                        return -EAGAIN;
 916                }
 917        }
 918        return 0;
 919}
 920
 921/**
 922 * hash_delegation_locked - Add a delegation to the appropriate lists
 923 * @dp:     a pointer to the nfs4_delegation we are adding.
 924 * @fp:     a pointer to the nfs4_file we're granting a delegation on
 925 *
 926 * Return:
 927 *      On success: NULL if the delegation was successfully hashed.
 928 *
 929 *      On error: -EAGAIN if one was previously granted to this
 930 *                 nfs4_client for this nfs4_file. Delegation is not hashed.
 931 *
 932 */
 933
 934static int
 935hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
 936{
 937        int status;
 938        struct nfs4_client *clp = dp->dl_stid.sc_client;
 939
 940        lockdep_assert_held(&state_lock);
 941        lockdep_assert_held(&fp->fi_lock);
 942
 943        status = nfs4_get_existing_delegation(clp, fp);
 944        if (status)
 945                return status;
 946        ++fp->fi_delegees;
 947        atomic_inc(&dp->dl_stid.sc_count);
 948        dp->dl_stid.sc_type = NFS4_DELEG_STID;
 949        list_add(&dp->dl_perfile, &fp->fi_delegations);
 950        list_add(&dp->dl_perclnt, &clp->cl_delegations);
 951        return 0;
 952}
 953
 954static bool
 955unhash_delegation_locked(struct nfs4_delegation *dp)
 956{
 957        struct nfs4_file *fp = dp->dl_stid.sc_file;
 958
 959        lockdep_assert_held(&state_lock);
 960
 961        if (list_empty(&dp->dl_perfile))
 962                return false;
 963
 964        dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
 965        /* Ensure that deleg break won't try to requeue it */
 966        ++dp->dl_time;
 967        spin_lock(&fp->fi_lock);
 968        list_del_init(&dp->dl_perclnt);
 969        list_del_init(&dp->dl_recall_lru);
 970        list_del_init(&dp->dl_perfile);
 971        spin_unlock(&fp->fi_lock);
 972        return true;
 973}
 974
 975static void destroy_delegation(struct nfs4_delegation *dp)
 976{
 977        bool unhashed;
 978
 979        spin_lock(&state_lock);
 980        unhashed = unhash_delegation_locked(dp);
 981        spin_unlock(&state_lock);
 982        if (unhashed) {
 983                put_clnt_odstate(dp->dl_clnt_odstate);
 984                nfs4_put_deleg_lease(dp->dl_stid.sc_file);
 985                nfs4_put_stid(&dp->dl_stid);
 986        }
 987}
 988
 989static void revoke_delegation(struct nfs4_delegation *dp)
 990{
 991        struct nfs4_client *clp = dp->dl_stid.sc_client;
 992
 993        WARN_ON(!list_empty(&dp->dl_recall_lru));
 994
 995        put_clnt_odstate(dp->dl_clnt_odstate);
 996        nfs4_put_deleg_lease(dp->dl_stid.sc_file);
 997
 998        if (clp->cl_minorversion == 0)
 999                nfs4_put_stid(&dp->dl_stid);
1000        else {
1001                dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1002                spin_lock(&clp->cl_lock);
1003                list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1004                spin_unlock(&clp->cl_lock);
1005        }
1006}
1007
1008/* 
1009 * SETCLIENTID state 
1010 */
1011
1012static unsigned int clientid_hashval(u32 id)
1013{
1014        return id & CLIENT_HASH_MASK;
1015}
1016
1017static unsigned int clientstr_hashval(const char *name)
1018{
1019        return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1020}
1021
1022/*
1023 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1024 * st_{access,deny}_bmap field of the stateid, in order to track not
1025 * only what share bits are currently in force, but also what
1026 * combinations of share bits previous opens have used.  This allows us
1027 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1028 * return an error if the client attempt to downgrade to a combination
1029 * of share bits not explicable by closing some of its previous opens.
1030 *
1031 * XXX: This enforcement is actually incomplete, since we don't keep
1032 * track of access/deny bit combinations; so, e.g., we allow:
1033 *
1034 *      OPEN allow read, deny write
1035 *      OPEN allow both, deny none
1036 *      DOWNGRADE allow read, deny none
1037 *
1038 * which we should reject.
1039 */
1040static unsigned int
1041bmap_to_share_mode(unsigned long bmap) {
1042        int i;
1043        unsigned int access = 0;
1044
1045        for (i = 1; i < 4; i++) {
1046                if (test_bit(i, &bmap))
1047                        access |= i;
1048        }
1049        return access;
1050}
1051
1052/* set share access for a given stateid */
1053static inline void
1054set_access(u32 access, struct nfs4_ol_stateid *stp)
1055{
1056        unsigned char mask = 1 << access;
1057
1058        WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1059        stp->st_access_bmap |= mask;
1060}
1061
1062/* clear share access for a given stateid */
1063static inline void
1064clear_access(u32 access, struct nfs4_ol_stateid *stp)
1065{
1066        unsigned char mask = 1 << access;
1067
1068        WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1069        stp->st_access_bmap &= ~mask;
1070}
1071
1072/* test whether a given stateid has access */
1073static inline bool
1074test_access(u32 access, struct nfs4_ol_stateid *stp)
1075{
1076        unsigned char mask = 1 << access;
1077
1078        return (bool)(stp->st_access_bmap & mask);
1079}
1080
1081/* set share deny for a given stateid */
1082static inline void
1083set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1084{
1085        unsigned char mask = 1 << deny;
1086
1087        WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1088        stp->st_deny_bmap |= mask;
1089}
1090
1091/* clear share deny for a given stateid */
1092static inline void
1093clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1094{
1095        unsigned char mask = 1 << deny;
1096
1097        WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1098        stp->st_deny_bmap &= ~mask;
1099}
1100
1101/* test whether a given stateid is denying specific access */
1102static inline bool
1103test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1104{
1105        unsigned char mask = 1 << deny;
1106
1107        return (bool)(stp->st_deny_bmap & mask);
1108}
1109
1110static int nfs4_access_to_omode(u32 access)
1111{
1112        switch (access & NFS4_SHARE_ACCESS_BOTH) {
1113        case NFS4_SHARE_ACCESS_READ:
1114                return O_RDONLY;
1115        case NFS4_SHARE_ACCESS_WRITE:
1116                return O_WRONLY;
1117        case NFS4_SHARE_ACCESS_BOTH:
1118                return O_RDWR;
1119        }
1120        WARN_ON_ONCE(1);
1121        return O_RDONLY;
1122}
1123
1124/*
1125 * A stateid that had a deny mode associated with it is being released
1126 * or downgraded. Recalculate the deny mode on the file.
1127 */
1128static void
1129recalculate_deny_mode(struct nfs4_file *fp)
1130{
1131        struct nfs4_ol_stateid *stp;
1132
1133        spin_lock(&fp->fi_lock);
1134        fp->fi_share_deny = 0;
1135        list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1136                fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1137        spin_unlock(&fp->fi_lock);
1138}
1139
1140static void
1141reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1142{
1143        int i;
1144        bool change = false;
1145
1146        for (i = 1; i < 4; i++) {
1147                if ((i & deny) != i) {
1148                        change = true;
1149                        clear_deny(i, stp);
1150                }
1151        }
1152
1153        /* Recalculate per-file deny mode if there was a change */
1154        if (change)
1155                recalculate_deny_mode(stp->st_stid.sc_file);
1156}
1157
1158/* release all access and file references for a given stateid */
1159static void
1160release_all_access(struct nfs4_ol_stateid *stp)
1161{
1162        int i;
1163        struct nfs4_file *fp = stp->st_stid.sc_file;
1164
1165        if (fp && stp->st_deny_bmap != 0)
1166                recalculate_deny_mode(fp);
1167
1168        for (i = 1; i < 4; i++) {
1169                if (test_access(i, stp))
1170                        nfs4_file_put_access(stp->st_stid.sc_file, i);
1171                clear_access(i, stp);
1172        }
1173}
1174
1175static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1176{
1177        kfree(sop->so_owner.data);
1178        sop->so_ops->so_free(sop);
1179}
1180
1181static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1182{
1183        struct nfs4_client *clp = sop->so_client;
1184
1185        might_lock(&clp->cl_lock);
1186
1187        if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1188                return;
1189        sop->so_ops->so_unhash(sop);
1190        spin_unlock(&clp->cl_lock);
1191        nfs4_free_stateowner(sop);
1192}
1193
1194static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1195{
1196        struct nfs4_file *fp = stp->st_stid.sc_file;
1197
1198        lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1199
1200        if (list_empty(&stp->st_perfile))
1201                return false;
1202
1203        spin_lock(&fp->fi_lock);
1204        list_del_init(&stp->st_perfile);
1205        spin_unlock(&fp->fi_lock);
1206        list_del(&stp->st_perstateowner);
1207        return true;
1208}
1209
1210static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1211{
1212        struct nfs4_ol_stateid *stp = openlockstateid(stid);
1213
1214        put_clnt_odstate(stp->st_clnt_odstate);
1215        release_all_access(stp);
1216        if (stp->st_stateowner)
1217                nfs4_put_stateowner(stp->st_stateowner);
1218        kmem_cache_free(stateid_slab, stid);
1219}
1220
1221static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1222{
1223        struct nfs4_ol_stateid *stp = openlockstateid(stid);
1224        struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1225        struct file *file;
1226
1227        file = find_any_file(stp->st_stid.sc_file);
1228        if (file)
1229                filp_close(file, (fl_owner_t)lo);
1230        nfs4_free_ol_stateid(stid);
1231}
1232
1233/*
1234 * Put the persistent reference to an already unhashed generic stateid, while
1235 * holding the cl_lock. If it's the last reference, then put it onto the
1236 * reaplist for later destruction.
1237 */
1238static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1239                                       struct list_head *reaplist)
1240{
1241        struct nfs4_stid *s = &stp->st_stid;
1242        struct nfs4_client *clp = s->sc_client;
1243
1244        lockdep_assert_held(&clp->cl_lock);
1245
1246        WARN_ON_ONCE(!list_empty(&stp->st_locks));
1247
1248        if (!atomic_dec_and_test(&s->sc_count)) {
1249                wake_up_all(&close_wq);
1250                return;
1251        }
1252
1253        idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1254        list_add(&stp->st_locks, reaplist);
1255}
1256
1257static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1258{
1259        lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1260
1261        list_del_init(&stp->st_locks);
1262        nfs4_unhash_stid(&stp->st_stid);
1263        return unhash_ol_stateid(stp);
1264}
1265
1266static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1267{
1268        struct nfs4_client *clp = stp->st_stid.sc_client;
1269        bool unhashed;
1270
1271        spin_lock(&clp->cl_lock);
1272        unhashed = unhash_lock_stateid(stp);
1273        spin_unlock(&clp->cl_lock);
1274        if (unhashed)
1275                nfs4_put_stid(&stp->st_stid);
1276}
1277
1278static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1279{
1280        struct nfs4_client *clp = lo->lo_owner.so_client;
1281
1282        lockdep_assert_held(&clp->cl_lock);
1283
1284        list_del_init(&lo->lo_owner.so_strhash);
1285}
1286
1287/*
1288 * Free a list of generic stateids that were collected earlier after being
1289 * fully unhashed.
1290 */
1291static void
1292free_ol_stateid_reaplist(struct list_head *reaplist)
1293{
1294        struct nfs4_ol_stateid *stp;
1295        struct nfs4_file *fp;
1296
1297        might_sleep();
1298
1299        while (!list_empty(reaplist)) {
1300                stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1301                                       st_locks);
1302                list_del(&stp->st_locks);
1303                fp = stp->st_stid.sc_file;
1304                stp->st_stid.sc_free(&stp->st_stid);
1305                if (fp)
1306                        put_nfs4_file(fp);
1307        }
1308}
1309
1310static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1311                                       struct list_head *reaplist)
1312{
1313        struct nfs4_ol_stateid *stp;
1314
1315        lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1316
1317        while (!list_empty(&open_stp->st_locks)) {
1318                stp = list_entry(open_stp->st_locks.next,
1319                                struct nfs4_ol_stateid, st_locks);
1320                WARN_ON(!unhash_lock_stateid(stp));
1321                put_ol_stateid_locked(stp, reaplist);
1322        }
1323}
1324
1325static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1326                                struct list_head *reaplist)
1327{
1328        bool unhashed;
1329
1330        lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1331
1332        unhashed = unhash_ol_stateid(stp);
1333        release_open_stateid_locks(stp, reaplist);
1334        return unhashed;
1335}
1336
1337static void release_open_stateid(struct nfs4_ol_stateid *stp)
1338{
1339        LIST_HEAD(reaplist);
1340
1341        spin_lock(&stp->st_stid.sc_client->cl_lock);
1342        if (unhash_open_stateid(stp, &reaplist))
1343                put_ol_stateid_locked(stp, &reaplist);
1344        spin_unlock(&stp->st_stid.sc_client->cl_lock);
1345        free_ol_stateid_reaplist(&reaplist);
1346}
1347
1348static void unhash_openowner_locked(struct nfs4_openowner *oo)
1349{
1350        struct nfs4_client *clp = oo->oo_owner.so_client;
1351
1352        lockdep_assert_held(&clp->cl_lock);
1353
1354        list_del_init(&oo->oo_owner.so_strhash);
1355        list_del_init(&oo->oo_perclient);
1356}
1357
1358static void release_last_closed_stateid(struct nfs4_openowner *oo)
1359{
1360        struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1361                                          nfsd_net_id);
1362        struct nfs4_ol_stateid *s;
1363
1364        spin_lock(&nn->client_lock);
1365        s = oo->oo_last_closed_stid;
1366        if (s) {
1367                list_del_init(&oo->oo_close_lru);
1368                oo->oo_last_closed_stid = NULL;
1369        }
1370        spin_unlock(&nn->client_lock);
1371        if (s)
1372                nfs4_put_stid(&s->st_stid);
1373}
1374
1375static void release_openowner(struct nfs4_openowner *oo)
1376{
1377        struct nfs4_ol_stateid *stp;
1378        struct nfs4_client *clp = oo->oo_owner.so_client;
1379        struct list_head reaplist;
1380
1381        INIT_LIST_HEAD(&reaplist);
1382
1383        spin_lock(&clp->cl_lock);
1384        unhash_openowner_locked(oo);
1385        while (!list_empty(&oo->oo_owner.so_stateids)) {
1386                stp = list_first_entry(&oo->oo_owner.so_stateids,
1387                                struct nfs4_ol_stateid, st_perstateowner);
1388                if (unhash_open_stateid(stp, &reaplist))
1389                        put_ol_stateid_locked(stp, &reaplist);
1390        }
1391        spin_unlock(&clp->cl_lock);
1392        free_ol_stateid_reaplist(&reaplist);
1393        release_last_closed_stateid(oo);
1394        nfs4_put_stateowner(&oo->oo_owner);
1395}
1396
1397static inline int
1398hash_sessionid(struct nfs4_sessionid *sessionid)
1399{
1400        struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1401
1402        return sid->sequence % SESSION_HASH_SIZE;
1403}
1404
1405#ifdef CONFIG_SUNRPC_DEBUG
1406static inline void
1407dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1408{
1409        u32 *ptr = (u32 *)(&sessionid->data[0]);
1410        dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1411}
1412#else
1413static inline void
1414dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1415{
1416}
1417#endif
1418
1419/*
1420 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1421 * won't be used for replay.
1422 */
1423void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1424{
1425        struct nfs4_stateowner *so = cstate->replay_owner;
1426
1427        if (nfserr == nfserr_replay_me)
1428                return;
1429
1430        if (!seqid_mutating_err(ntohl(nfserr))) {
1431                nfsd4_cstate_clear_replay(cstate);
1432                return;
1433        }
1434        if (!so)
1435                return;
1436        if (so->so_is_open_owner)
1437                release_last_closed_stateid(openowner(so));
1438        so->so_seqid++;
1439        return;
1440}
1441
1442static void
1443gen_sessionid(struct nfsd4_session *ses)
1444{
1445        struct nfs4_client *clp = ses->se_client;
1446        struct nfsd4_sessionid *sid;
1447
1448        sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1449        sid->clientid = clp->cl_clientid;
1450        sid->sequence = current_sessionid++;
1451        sid->reserved = 0;
1452}
1453
1454/*
1455 * The protocol defines ca_maxresponssize_cached to include the size of
1456 * the rpc header, but all we need to cache is the data starting after
1457 * the end of the initial SEQUENCE operation--the rest we regenerate
1458 * each time.  Therefore we can advertise a ca_maxresponssize_cached
1459 * value that is the number of bytes in our cache plus a few additional
1460 * bytes.  In order to stay on the safe side, and not promise more than
1461 * we can cache, those additional bytes must be the minimum possible: 24
1462 * bytes of rpc header (xid through accept state, with AUTH_NULL
1463 * verifier), 12 for the compound header (with zero-length tag), and 44
1464 * for the SEQUENCE op response:
1465 */
1466#define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1467
1468static void
1469free_session_slots(struct nfsd4_session *ses)
1470{
1471        int i;
1472
1473        for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1474                kfree(ses->se_slots[i]);
1475}
1476
1477/*
1478 * We don't actually need to cache the rpc and session headers, so we
1479 * can allocate a little less for each slot:
1480 */
1481static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1482{
1483        u32 size;
1484
1485        if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1486                size = 0;
1487        else
1488                size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1489        return size + sizeof(struct nfsd4_slot);
1490}
1491
1492/*
1493 * XXX: If we run out of reserved DRC memory we could (up to a point)
1494 * re-negotiate active sessions and reduce their slot usage to make
1495 * room for new connections. For now we just fail the create session.
1496 */
1497static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1498{
1499        u32 slotsize = slot_bytes(ca);
1500        u32 num = ca->maxreqs;
1501        int avail;
1502
1503        spin_lock(&nfsd_drc_lock);
1504        avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1505                    nfsd_drc_max_mem - nfsd_drc_mem_used);
1506        /*
1507         * Never use more than a third of the remaining memory,
1508         * unless it's the only way to give this client a slot:
1509         */
1510        avail = clamp_t(int, avail, slotsize, avail/3);
1511        num = min_t(int, num, avail / slotsize);
1512        nfsd_drc_mem_used += num * slotsize;
1513        spin_unlock(&nfsd_drc_lock);
1514
1515        return num;
1516}
1517
1518static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1519{
1520        int slotsize = slot_bytes(ca);
1521
1522        spin_lock(&nfsd_drc_lock);
1523        nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1524        spin_unlock(&nfsd_drc_lock);
1525}
1526
1527static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1528                                           struct nfsd4_channel_attrs *battrs)
1529{
1530        int numslots = fattrs->maxreqs;
1531        int slotsize = slot_bytes(fattrs);
1532        struct nfsd4_session *new;
1533        int mem, i;
1534
1535        BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1536                        + sizeof(struct nfsd4_session) > PAGE_SIZE);
1537        mem = numslots * sizeof(struct nfsd4_slot *);
1538
1539        new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1540        if (!new)
1541                return NULL;
1542        /* allocate each struct nfsd4_slot and data cache in one piece */
1543        for (i = 0; i < numslots; i++) {
1544                new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1545                if (!new->se_slots[i])
1546                        goto out_free;
1547        }
1548
1549        memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1550        memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1551
1552        return new;
1553out_free:
1554        while (i--)
1555                kfree(new->se_slots[i]);
1556        kfree(new);
1557        return NULL;
1558}
1559
1560static void free_conn(struct nfsd4_conn *c)
1561{
1562        svc_xprt_put(c->cn_xprt);
1563        kfree(c);
1564}
1565
1566static void nfsd4_conn_lost(struct svc_xpt_user *u)
1567{
1568        struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1569        struct nfs4_client *clp = c->cn_session->se_client;
1570
1571        spin_lock(&clp->cl_lock);
1572        if (!list_empty(&c->cn_persession)) {
1573                list_del(&c->cn_persession);
1574                free_conn(c);
1575        }
1576        nfsd4_probe_callback(clp);
1577        spin_unlock(&clp->cl_lock);
1578}
1579
1580static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1581{
1582        struct nfsd4_conn *conn;
1583
1584        conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1585        if (!conn)
1586                return NULL;
1587        svc_xprt_get(rqstp->rq_xprt);
1588        conn->cn_xprt = rqstp->rq_xprt;
1589        conn->cn_flags = flags;
1590        INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1591        return conn;
1592}
1593
1594static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1595{
1596        conn->cn_session = ses;
1597        list_add(&conn->cn_persession, &ses->se_conns);
1598}
1599
1600static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1601{
1602        struct nfs4_client *clp = ses->se_client;
1603
1604        spin_lock(&clp->cl_lock);
1605        __nfsd4_hash_conn(conn, ses);
1606        spin_unlock(&clp->cl_lock);
1607}
1608
1609static int nfsd4_register_conn(struct nfsd4_conn *conn)
1610{
1611        conn->cn_xpt_user.callback = nfsd4_conn_lost;
1612        return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1613}
1614
1615static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1616{
1617        int ret;
1618
1619        nfsd4_hash_conn(conn, ses);
1620        ret = nfsd4_register_conn(conn);
1621        if (ret)
1622                /* oops; xprt is already down: */
1623                nfsd4_conn_lost(&conn->cn_xpt_user);
1624        /* We may have gained or lost a callback channel: */
1625        nfsd4_probe_callback_sync(ses->se_client);
1626}
1627
1628static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1629{
1630        u32 dir = NFS4_CDFC4_FORE;
1631
1632        if (cses->flags & SESSION4_BACK_CHAN)
1633                dir |= NFS4_CDFC4_BACK;
1634        return alloc_conn(rqstp, dir);
1635}
1636
1637/* must be called under client_lock */
1638static void nfsd4_del_conns(struct nfsd4_session *s)
1639{
1640        struct nfs4_client *clp = s->se_client;
1641        struct nfsd4_conn *c;
1642
1643        spin_lock(&clp->cl_lock);
1644        while (!list_empty(&s->se_conns)) {
1645                c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1646                list_del_init(&c->cn_persession);
1647                spin_unlock(&clp->cl_lock);
1648
1649                unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1650                free_conn(c);
1651
1652                spin_lock(&clp->cl_lock);
1653        }
1654        spin_unlock(&clp->cl_lock);
1655}
1656
1657static void __free_session(struct nfsd4_session *ses)
1658{
1659        free_session_slots(ses);
1660        kfree(ses);
1661}
1662
1663static void free_session(struct nfsd4_session *ses)
1664{
1665        nfsd4_del_conns(ses);
1666        nfsd4_put_drc_mem(&ses->se_fchannel);
1667        __free_session(ses);
1668}
1669
1670static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1671{
1672        int idx;
1673        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1674
1675        new->se_client = clp;
1676        gen_sessionid(new);
1677
1678        INIT_LIST_HEAD(&new->se_conns);
1679
1680        new->se_cb_seq_nr = 1;
1681        new->se_flags = cses->flags;
1682        new->se_cb_prog = cses->callback_prog;
1683        new->se_cb_sec = cses->cb_sec;
1684        atomic_set(&new->se_ref, 0);
1685        idx = hash_sessionid(&new->se_sessionid);
1686        list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1687        spin_lock(&clp->cl_lock);
1688        list_add(&new->se_perclnt, &clp->cl_sessions);
1689        spin_unlock(&clp->cl_lock);
1690
1691        {
1692                struct sockaddr *sa = svc_addr(rqstp);
1693                /*
1694                 * This is a little silly; with sessions there's no real
1695                 * use for the callback address.  Use the peer address
1696                 * as a reasonable default for now, but consider fixing
1697                 * the rpc client not to require an address in the
1698                 * future:
1699                 */
1700                rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1701                clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1702        }
1703}
1704
1705/* caller must hold client_lock */
1706static struct nfsd4_session *
1707__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1708{
1709        struct nfsd4_session *elem;
1710        int idx;
1711        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1712
1713        lockdep_assert_held(&nn->client_lock);
1714
1715        dump_sessionid(__func__, sessionid);
1716        idx = hash_sessionid(sessionid);
1717        /* Search in the appropriate list */
1718        list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1719                if (!memcmp(elem->se_sessionid.data, sessionid->data,
1720                            NFS4_MAX_SESSIONID_LEN)) {
1721                        return elem;
1722                }
1723        }
1724
1725        dprintk("%s: session not found\n", __func__);
1726        return NULL;
1727}
1728
1729static struct nfsd4_session *
1730find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1731                __be32 *ret)
1732{
1733        struct nfsd4_session *session;
1734        __be32 status = nfserr_badsession;
1735
1736        session = __find_in_sessionid_hashtbl(sessionid, net);
1737        if (!session)
1738                goto out;
1739        status = nfsd4_get_session_locked(session);
1740        if (status)
1741                session = NULL;
1742out:
1743        *ret = status;
1744        return session;
1745}
1746
1747/* caller must hold client_lock */
1748static void
1749unhash_session(struct nfsd4_session *ses)
1750{
1751        struct nfs4_client *clp = ses->se_client;
1752        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1753
1754        lockdep_assert_held(&nn->client_lock);
1755
1756        list_del(&ses->se_hash);
1757        spin_lock(&ses->se_client->cl_lock);
1758        list_del(&ses->se_perclnt);
1759        spin_unlock(&ses->se_client->cl_lock);
1760}
1761
1762/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1763static int
1764STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1765{
1766        /*
1767         * We're assuming the clid was not given out from a boot
1768         * precisely 2^32 (about 136 years) before this one.  That seems
1769         * a safe assumption:
1770         */
1771        if (clid->cl_boot == (u32)nn->boot_time)
1772                return 0;
1773        dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1774                clid->cl_boot, clid->cl_id, nn->boot_time);
1775        return 1;
1776}
1777
1778/* 
1779 * XXX Should we use a slab cache ?
1780 * This type of memory management is somewhat inefficient, but we use it
1781 * anyway since SETCLIENTID is not a common operation.
1782 */
1783static struct nfs4_client *alloc_client(struct xdr_netobj name)
1784{
1785        struct nfs4_client *clp;
1786        int i;
1787
1788        clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1789        if (clp == NULL)
1790                return NULL;
1791        clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1792        if (clp->cl_name.data == NULL)
1793                goto err_no_name;
1794        clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1795                        OWNER_HASH_SIZE, GFP_KERNEL);
1796        if (!clp->cl_ownerstr_hashtbl)
1797                goto err_no_hashtbl;
1798        for (i = 0; i < OWNER_HASH_SIZE; i++)
1799                INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1800        clp->cl_name.len = name.len;
1801        INIT_LIST_HEAD(&clp->cl_sessions);
1802        idr_init(&clp->cl_stateids);
1803        atomic_set(&clp->cl_refcount, 0);
1804        clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1805        INIT_LIST_HEAD(&clp->cl_idhash);
1806        INIT_LIST_HEAD(&clp->cl_openowners);
1807        INIT_LIST_HEAD(&clp->cl_delegations);
1808        INIT_LIST_HEAD(&clp->cl_lru);
1809        INIT_LIST_HEAD(&clp->cl_revoked);
1810#ifdef CONFIG_NFSD_PNFS
1811        INIT_LIST_HEAD(&clp->cl_lo_states);
1812#endif
1813        spin_lock_init(&clp->cl_lock);
1814        rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1815        return clp;
1816err_no_hashtbl:
1817        kfree(clp->cl_name.data);
1818err_no_name:
1819        kfree(clp);
1820        return NULL;
1821}
1822
1823static void
1824free_client(struct nfs4_client *clp)
1825{
1826        while (!list_empty(&clp->cl_sessions)) {
1827                struct nfsd4_session *ses;
1828                ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1829                                se_perclnt);
1830                list_del(&ses->se_perclnt);
1831                WARN_ON_ONCE(atomic_read(&ses->se_ref));
1832                free_session(ses);
1833        }
1834        rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1835        free_svc_cred(&clp->cl_cred);
1836        kfree(clp->cl_ownerstr_hashtbl);
1837        kfree(clp->cl_name.data);
1838        idr_destroy(&clp->cl_stateids);
1839        kfree(clp);
1840}
1841
1842/* must be called under the client_lock */
1843static void
1844unhash_client_locked(struct nfs4_client *clp)
1845{
1846        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1847        struct nfsd4_session *ses;
1848
1849        lockdep_assert_held(&nn->client_lock);
1850
1851        /* Mark the client as expired! */
1852        clp->cl_time = 0;
1853        /* Make it invisible */
1854        if (!list_empty(&clp->cl_idhash)) {
1855                list_del_init(&clp->cl_idhash);
1856                if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1857                        rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1858                else
1859                        rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1860        }
1861        list_del_init(&clp->cl_lru);
1862        spin_lock(&clp->cl_lock);
1863        list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1864                list_del_init(&ses->se_hash);
1865        spin_unlock(&clp->cl_lock);
1866}
1867
1868static void
1869unhash_client(struct nfs4_client *clp)
1870{
1871        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1872
1873        spin_lock(&nn->client_lock);
1874        unhash_client_locked(clp);
1875        spin_unlock(&nn->client_lock);
1876}
1877
1878static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1879{
1880        if (atomic_read(&clp->cl_refcount))
1881                return nfserr_jukebox;
1882        unhash_client_locked(clp);
1883        return nfs_ok;
1884}
1885
1886static void
1887__destroy_client(struct nfs4_client *clp)
1888{
1889        int i;
1890        struct nfs4_openowner *oo;
1891        struct nfs4_delegation *dp;
1892        struct list_head reaplist;
1893
1894        INIT_LIST_HEAD(&reaplist);
1895        spin_lock(&state_lock);
1896        while (!list_empty(&clp->cl_delegations)) {
1897                dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1898                WARN_ON(!unhash_delegation_locked(dp));
1899                list_add(&dp->dl_recall_lru, &reaplist);
1900        }
1901        spin_unlock(&state_lock);
1902        while (!list_empty(&reaplist)) {
1903                dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1904                list_del_init(&dp->dl_recall_lru);
1905                put_clnt_odstate(dp->dl_clnt_odstate);
1906                nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1907                nfs4_put_stid(&dp->dl_stid);
1908        }
1909        while (!list_empty(&clp->cl_revoked)) {
1910                dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1911                list_del_init(&dp->dl_recall_lru);
1912                nfs4_put_stid(&dp->dl_stid);
1913        }
1914        while (!list_empty(&clp->cl_openowners)) {
1915                oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1916                nfs4_get_stateowner(&oo->oo_owner);
1917                release_openowner(oo);
1918        }
1919        for (i = 0; i < OWNER_HASH_SIZE; i++) {
1920                struct nfs4_stateowner *so, *tmp;
1921
1922                list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1923                                         so_strhash) {
1924                        /* Should be no openowners at this point */
1925                        WARN_ON_ONCE(so->so_is_open_owner);
1926                        remove_blocked_locks(lockowner(so));
1927                }
1928        }
1929        nfsd4_return_all_client_layouts(clp);
1930        nfsd4_shutdown_callback(clp);
1931        if (clp->cl_cb_conn.cb_xprt)
1932                svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1933        free_client(clp);
1934}
1935
1936static void
1937destroy_client(struct nfs4_client *clp)
1938{
1939        unhash_client(clp);
1940        __destroy_client(clp);
1941}
1942
1943static void expire_client(struct nfs4_client *clp)
1944{
1945        unhash_client(clp);
1946        nfsd4_client_record_remove(clp);
1947        __destroy_client(clp);
1948}
1949
1950static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1951{
1952        memcpy(target->cl_verifier.data, source->data,
1953                        sizeof(target->cl_verifier.data));
1954}
1955
1956static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1957{
1958        target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 
1959        target->cl_clientid.cl_id = source->cl_clientid.cl_id; 
1960}
1961
1962int strdup_if_nonnull(char **target, char *source)
1963{
1964        if (source) {
1965                *target = kstrdup(source, GFP_KERNEL);
1966                if (!*target)
1967                        return -ENOMEM;
1968        } else
1969                *target = NULL;
1970        return 0;
1971}
1972
1973static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1974{
1975        int ret;
1976
1977        ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1978        if (ret)
1979                return ret;
1980        ret = strdup_if_nonnull(&target->cr_raw_principal,
1981                                        source->cr_raw_principal);
1982        if (ret)
1983                return ret;
1984        target->cr_flavor = source->cr_flavor;
1985        target->cr_uid = source->cr_uid;
1986        target->cr_gid = source->cr_gid;
1987        target->cr_group_info = source->cr_group_info;
1988        get_group_info(target->cr_group_info);
1989        target->cr_gss_mech = source->cr_gss_mech;
1990        if (source->cr_gss_mech)
1991                gss_mech_get(source->cr_gss_mech);
1992        return 0;
1993}
1994
1995static int
1996compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1997{
1998        if (o1->len < o2->len)
1999                return -1;
2000        if (o1->len > o2->len)
2001                return 1;
2002        return memcmp(o1->data, o2->data, o1->len);
2003}
2004
2005static int same_name(const char *n1, const char *n2)
2006{
2007        return 0 == memcmp(n1, n2, HEXDIR_LEN);
2008}
2009
2010static int
2011same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2012{
2013        return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2014}
2015
2016static int
2017same_clid(clientid_t *cl1, clientid_t *cl2)
2018{
2019        return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2020}
2021
2022static bool groups_equal(struct group_info *g1, struct group_info *g2)
2023{
2024        int i;
2025
2026        if (g1->ngroups != g2->ngroups)
2027                return false;
2028        for (i=0; i<g1->ngroups; i++)
2029                if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
2030                        return false;
2031        return true;
2032}
2033
2034/*
2035 * RFC 3530 language requires clid_inuse be returned when the
2036 * "principal" associated with a requests differs from that previously
2037 * used.  We use uid, gid's, and gss principal string as our best
2038 * approximation.  We also don't want to allow non-gss use of a client
2039 * established using gss: in theory cr_principal should catch that
2040 * change, but in practice cr_principal can be null even in the gss case
2041 * since gssd doesn't always pass down a principal string.
2042 */
2043static bool is_gss_cred(struct svc_cred *cr)
2044{
2045        /* Is cr_flavor one of the gss "pseudoflavors"?: */
2046        return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2047}
2048
2049
2050static bool
2051same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2052{
2053        if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2054                || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2055                || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2056                || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2057                return false;
2058        if (cr1->cr_principal == cr2->cr_principal)
2059                return true;
2060        if (!cr1->cr_principal || !cr2->cr_principal)
2061                return false;
2062        return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2063}
2064
2065static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2066{
2067        struct svc_cred *cr = &rqstp->rq_cred;
2068        u32 service;
2069
2070        if (!cr->cr_gss_mech)
2071                return false;
2072        service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2073        return service == RPC_GSS_SVC_INTEGRITY ||
2074               service == RPC_GSS_SVC_PRIVACY;
2075}
2076
2077static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2078{
2079        struct svc_cred *cr = &rqstp->rq_cred;
2080
2081        if (!cl->cl_mach_cred)
2082                return true;
2083        if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2084                return false;
2085        if (!svc_rqst_integrity_protected(rqstp))
2086                return false;
2087        if (cl->cl_cred.cr_raw_principal)
2088                return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2089                                                cr->cr_raw_principal);
2090        if (!cr->cr_principal)
2091                return false;
2092        return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2093}
2094
2095static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2096{
2097        __be32 verf[2];
2098
2099        /*
2100         * This is opaque to client, so no need to byte-swap. Use
2101         * __force to keep sparse happy
2102         */
2103        verf[0] = (__force __be32)get_seconds();
2104        verf[1] = (__force __be32)nn->clverifier_counter++;
2105        memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2106}
2107
2108static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2109{
2110        clp->cl_clientid.cl_boot = nn->boot_time;
2111        clp->cl_clientid.cl_id = nn->clientid_counter++;
2112        gen_confirm(clp, nn);
2113}
2114
2115static struct nfs4_stid *
2116find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2117{
2118        struct nfs4_stid *ret;
2119
2120        ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2121        if (!ret || !ret->sc_type)
2122                return NULL;
2123        return ret;
2124}
2125
2126static struct nfs4_stid *
2127find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2128{
2129        struct nfs4_stid *s;
2130
2131        spin_lock(&cl->cl_lock);
2132        s = find_stateid_locked(cl, t);
2133        if (s != NULL) {
2134                if (typemask & s->sc_type)
2135                        atomic_inc(&s->sc_count);
2136                else
2137                        s = NULL;
2138        }
2139        spin_unlock(&cl->cl_lock);
2140        return s;
2141}
2142
2143static struct nfs4_client *create_client(struct xdr_netobj name,
2144                struct svc_rqst *rqstp, nfs4_verifier *verf)
2145{
2146        struct nfs4_client *clp;
2147        struct sockaddr *sa = svc_addr(rqstp);
2148        int ret;
2149        struct net *net = SVC_NET(rqstp);
2150
2151        clp = alloc_client(name);
2152        if (clp == NULL)
2153                return NULL;
2154
2155        ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2156        if (ret) {
2157                free_client(clp);
2158                return NULL;
2159        }
2160        nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2161        clp->cl_time = get_seconds();
2162        clear_bit(0, &clp->cl_cb_slot_busy);
2163        copy_verf(clp, verf);
2164        rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2165        clp->cl_cb_session = NULL;
2166        clp->net = net;
2167        return clp;
2168}
2169
2170static void
2171add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2172{
2173        struct rb_node **new = &(root->rb_node), *parent = NULL;
2174        struct nfs4_client *clp;
2175
2176        while (*new) {
2177                clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2178                parent = *new;
2179
2180                if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2181                        new = &((*new)->rb_left);
2182                else
2183                        new = &((*new)->rb_right);
2184        }
2185
2186        rb_link_node(&new_clp->cl_namenode, parent, new);
2187        rb_insert_color(&new_clp->cl_namenode, root);
2188}
2189
2190static struct nfs4_client *
2191find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2192{
2193        int cmp;
2194        struct rb_node *node = root->rb_node;
2195        struct nfs4_client *clp;
2196
2197        while (node) {
2198                clp = rb_entry(node, struct nfs4_client, cl_namenode);
2199                cmp = compare_blob(&clp->cl_name, name);
2200                if (cmp > 0)
2201                        node = node->rb_left;
2202                else if (cmp < 0)
2203                        node = node->rb_right;
2204                else
2205                        return clp;
2206        }
2207        return NULL;
2208}
2209
2210static void
2211add_to_unconfirmed(struct nfs4_client *clp)
2212{
2213        unsigned int idhashval;
2214        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2215
2216        lockdep_assert_held(&nn->client_lock);
2217
2218        clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2219        add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2220        idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2221        list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2222        renew_client_locked(clp);
2223}
2224
2225static void
2226move_to_confirmed(struct nfs4_client *clp)
2227{
2228        unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2229        struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2230
2231        lockdep_assert_held(&nn->client_lock);
2232
2233        dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2234        list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2235        rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2236        add_clp_to_name_tree(clp, &nn->conf_name_tree);
2237        set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2238        renew_client_locked(clp);
2239}
2240
2241static struct nfs4_client *
2242find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2243{
2244        struct nfs4_client *clp;
2245        unsigned int idhashval = clientid_hashval(clid->cl_id);
2246
2247        list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2248                if (same_clid(&clp->cl_clientid, clid)) {
2249                        if ((bool)clp->cl_minorversion != sessions)
2250                                return NULL;
2251                        renew_client_locked(clp);
2252                        return clp;
2253                }
2254        }
2255        return NULL;
2256}
2257
2258static struct nfs4_client *
2259find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2260{
2261        struct list_head *tbl = nn->conf_id_hashtbl;
2262
2263        lockdep_assert_held(&nn->client_lock);
2264        return find_client_in_id_table(tbl, clid, sessions);
2265}
2266
2267static struct nfs4_client *
2268find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2269{
2270        struct list_head *tbl = nn->unconf_id_hashtbl;
2271
2272        lockdep_assert_held(&nn->client_lock);
2273        return find_client_in_id_table(tbl, clid, sessions);
2274}
2275
2276static bool clp_used_exchangeid(struct nfs4_client *clp)
2277{
2278        return clp->cl_exchange_flags != 0;
2279} 
2280
2281static struct nfs4_client *
2282find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2283{
2284        lockdep_assert_held(&nn->client_lock);
2285        return find_clp_in_name_tree(name, &nn->conf_name_tree);
2286}
2287
2288static struct nfs4_client *
2289find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2290{
2291        lockdep_assert_held(&nn->client_lock);
2292        return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2293}
2294
2295static void
2296gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2297{
2298        struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2299        struct sockaddr *sa = svc_addr(rqstp);
2300        u32 scopeid = rpc_get_scope_id(sa);
2301        unsigned short expected_family;
2302
2303        /* Currently, we only support tcp and tcp6 for the callback channel */
2304        if (se->se_callback_netid_len == 3 &&
2305            !memcmp(se->se_callback_netid_val, "tcp", 3))
2306                expected_family = AF_INET;
2307        else if (se->se_callback_netid_len == 4 &&
2308                 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2309                expected_family = AF_INET6;
2310        else
2311                goto out_err;
2312
2313        conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2314                                            se->se_callback_addr_len,
2315                                            (struct sockaddr *)&conn->cb_addr,
2316                                            sizeof(conn->cb_addr));
2317
2318        if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2319                goto out_err;
2320
2321        if (conn->cb_addr.ss_family == AF_INET6)
2322                ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2323
2324        conn->cb_prog = se->se_callback_prog;
2325        conn->cb_ident = se->se_callback_ident;
2326        memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2327        return;
2328out_err:
2329        conn->cb_addr.ss_family = AF_UNSPEC;
2330        conn->cb_addrlen = 0;
2331        dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2332                "will not receive delegations\n",
2333                clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2334
2335        return;
2336}
2337
2338/*
2339 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2340 */
2341static void
2342nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2343{
2344        struct xdr_buf *buf = resp->xdr.buf;
2345        struct nfsd4_slot *slot = resp->cstate.slot;
2346        unsigned int base;
2347
2348        dprintk("--> %s slot %p\n", __func__, slot);
2349
2350        slot->sl_opcnt = resp->opcnt;
2351        slot->sl_status = resp->cstate.status;
2352
2353        slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2354        if (nfsd4_not_cached(resp)) {
2355                slot->sl_datalen = 0;
2356                return;
2357        }
2358        base = resp->cstate.data_offset;
2359        slot->sl_datalen = buf->len - base;
2360        if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2361                WARN(1, "%s: sessions DRC could not cache compound\n",
2362                     __func__);
2363        return;
2364}
2365
2366/*
2367 * Encode the replay sequence operation from the slot values.
2368 * If cachethis is FALSE encode the uncached rep error on the next
2369 * operation which sets resp->p and increments resp->opcnt for
2370 * nfs4svc_encode_compoundres.
2371 *
2372 */
2373static __be32
2374nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2375                          struct nfsd4_compoundres *resp)
2376{
2377        struct nfsd4_op *op;
2378        struct nfsd4_slot *slot = resp->cstate.slot;
2379
2380        /* Encode the replayed sequence operation */
2381        op = &args->ops[resp->opcnt - 1];
2382        nfsd4_encode_operation(resp, op);
2383
2384        /* Return nfserr_retry_uncached_rep in next operation. */
2385        if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2386                op = &args->ops[resp->opcnt++];
2387                op->status = nfserr_retry_uncached_rep;
2388                nfsd4_encode_operation(resp, op);
2389        }
2390        return op->status;
2391}
2392
2393/*
2394 * The sequence operation is not cached because we can use the slot and
2395 * session values.
2396 */
2397static __be32
2398nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2399                         struct nfsd4_sequence *seq)
2400{
2401        struct nfsd4_slot *slot = resp->cstate.slot;
2402        struct xdr_stream *xdr = &resp->xdr;
2403        __be32 *p;
2404        __be32 status;
2405
2406        dprintk("--> %s slot %p\n", __func__, slot);
2407
2408        status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2409        if (status)
2410                return status;
2411
2412        p = xdr_reserve_space(xdr, slot->sl_datalen);
2413        if (!p) {
2414                WARN_ON_ONCE(1);
2415                return nfserr_serverfault;
2416        }
2417        xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2418        xdr_commit_encode(xdr);
2419
2420        resp->opcnt = slot->sl_opcnt;
2421        return slot->sl_status;
2422}
2423
2424/*
2425 * Set the exchange_id flags returned by the server.
2426 */
2427static void
2428nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2429{
2430#ifdef CONFIG_NFSD_PNFS
2431        new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2432#else
2433        new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2434#endif
2435
2436        /* Referrals are supported, Migration is not. */
2437        new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2438
2439        /* set the wire flags to return to client. */
2440        clid->flags = new->cl_exchange_flags;
2441}
2442
2443static bool client_has_openowners(struct nfs4_client *clp)
2444{
2445        struct nfs4_openowner *oo;
2446
2447        list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2448                if (!list_empty(&oo->oo_owner.so_stateids))
2449                        return true;
2450        }
2451        return false;
2452}
2453
2454static bool client_has_state(struct nfs4_client *clp)
2455{
2456        return client_has_openowners(clp)
2457#ifdef CONFIG_NFSD_PNFS
2458                || !list_empty(&clp->cl_lo_states)
2459#endif
2460                || !list_empty(&clp->cl_delegations)
2461                || !list_empty(&clp->cl_sessions);
2462}
2463
2464__be32
2465nfsd4_exchange_id(struct svc_rqst *rqstp,
2466                  struct nfsd4_compound_state *cstate,
2467                  struct nfsd4_exchange_id *exid)
2468{
2469        struct nfs4_client *conf, *new;
2470        struct nfs4_client *unconf = NULL;
2471        __be32 status;
2472        char                    addr_str[INET6_ADDRSTRLEN];
2473        nfs4_verifier           verf = exid->verifier;
2474        struct sockaddr         *sa = svc_addr(rqstp);
2475        bool    update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2476        struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2477
2478        rpc_ntop(sa, addr_str, sizeof(addr_str));
2479        dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2480                "ip_addr=%s flags %x, spa_how %d\n",
2481                __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2482                addr_str, exid->flags, exid->spa_how);
2483
2484        if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2485                return nfserr_inval;
2486
2487        new = create_client(exid->clname, rqstp, &verf);
2488        if (new == NULL)
2489                return nfserr_jukebox;
2490
2491        switch (exid->spa_how) {
2492        case SP4_MACH_CRED:
2493                if (!svc_rqst_integrity_protected(rqstp)) {
2494                        status = nfserr_inval;
2495                        goto out_nolock;
2496                }
2497                /*
2498                 * Sometimes userspace doesn't give us a principal.
2499                 * Which is a bug, really.  Anyway, we can't enforce
2500                 * MACH_CRED in that case, better to give up now:
2501                 */
2502                if (!new->cl_cred.cr_principal &&
2503                                        !new->cl_cred.cr_raw_principal) {
2504                        status = nfserr_serverfault;
2505                        goto out_nolock;
2506                }
2507                new->cl_mach_cred = true;
2508        case SP4_NONE:
2509                break;
2510        default:                                /* checked by xdr code */
2511                WARN_ON_ONCE(1);
2512        case SP4_SSV:
2513                status = nfserr_encr_alg_unsupp;
2514                goto out_nolock;
2515        }
2516
2517        /* Cases below refer to rfc 5661 section 18.35.4: */
2518        spin_lock(&nn->client_lock);
2519        conf = find_confirmed_client_by_name(&exid->clname, nn);
2520        if (conf) {
2521                bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2522                bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2523
2524                if (update) {
2525                        if (!clp_used_exchangeid(conf)) { /* buggy client */
2526                                status = nfserr_inval;
2527                                goto out;
2528                        }
2529                        if (!mach_creds_match(conf, rqstp)) {
2530                                status = nfserr_wrong_cred;
2531                                goto out;
2532                        }
2533                        if (!creds_match) { /* case 9 */
2534                                status = nfserr_perm;
2535                                goto out;
2536                        }
2537                        if (!verfs_match) { /* case 8 */
2538                                status = nfserr_not_same;
2539                                goto out;
2540                        }
2541                        /* case 6 */
2542                        exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2543                        goto out_copy;
2544                }
2545                if (!creds_match) { /* case 3 */
2546                        if (client_has_state(conf)) {
2547                                status = nfserr_clid_inuse;
2548                                goto out;
2549                        }
2550                        goto out_new;
2551                }
2552                if (verfs_match) { /* case 2 */
2553                        conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2554                        goto out_copy;
2555                }
2556                /* case 5, client reboot */
2557                conf = NULL;
2558                goto out_new;
2559        }
2560
2561        if (update) { /* case 7 */
2562                status = nfserr_noent;
2563                goto out;
2564        }
2565
2566        unconf  = find_unconfirmed_client_by_name(&exid->clname, nn);
2567        if (unconf) /* case 4, possible retry or client restart */
2568                unhash_client_locked(unconf);
2569
2570        /* case 1 (normal case) */
2571out_new:
2572        if (conf) {
2573                status = mark_client_expired_locked(conf);
2574                if (status)
2575                        goto out;
2576        }
2577        new->cl_minorversion = cstate->minorversion;
2578
2579        gen_clid(new, nn);
2580        add_to_unconfirmed(new);
2581        swap(new, conf);
2582out_copy:
2583        exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2584        exid->clientid.cl_id = conf->cl_clientid.cl_id;
2585
2586        exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2587        nfsd4_set_ex_flags(conf, exid);
2588
2589        dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2590                conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2591        status = nfs_ok;
2592
2593out:
2594        spin_unlock(&nn->client_lock);
2595out_nolock:
2596        if (new)
2597                expire_client(new);
2598        if (unconf)
2599                expire_client(unconf);
2600        return status;
2601}
2602
2603static __be32
2604check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2605{
2606        dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2607                slot_seqid);
2608
2609        /* The slot is in use, and no response has been sent. */
2610        if (slot_inuse) {
2611                if (seqid == slot_seqid)
2612                        return nfserr_jukebox;
2613                else
2614                        return nfserr_seq_misordered;
2615        }
2616        /* Note unsigned 32-bit arithmetic handles wraparound: */
2617        if (likely(seqid == slot_seqid + 1))
2618                return nfs_ok;
2619        if (seqid == slot_seqid)
2620                return nfserr_replay_cache;
2621        return nfserr_seq_misordered;
2622}
2623
2624/*
2625 * Cache the create session result into the create session single DRC
2626 * slot cache by saving the xdr structure. sl_seqid has been set.
2627 * Do this for solo or embedded create session operations.
2628 */
2629static void
2630nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2631                           struct nfsd4_clid_slot *slot, __be32 nfserr)
2632{
2633        slot->sl_status = nfserr;
2634        memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2635}
2636
2637static __be32
2638nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2639                            struct nfsd4_clid_slot *slot)
2640{
2641        memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2642        return slot->sl_status;
2643}
2644
2645#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2646                        2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2647                        1 +     /* MIN tag is length with zero, only length */ \
2648                        3 +     /* version, opcount, opcode */ \
2649                        XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2650                                /* seqid, slotID, slotID, cache */ \
2651                        4 ) * sizeof(__be32))
2652
2653#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2654                        2 +     /* verifier: AUTH_NULL, length 0 */\
2655                        1 +     /* status */ \
2656                        1 +     /* MIN tag is length with zero, only length */ \
2657                        3 +     /* opcount, opcode, opstatus*/ \
2658                        XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2659                                /* seqid, slotID, slotID, slotID, status */ \
2660                        5 ) * sizeof(__be32))
2661
2662static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2663{
2664        u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2665
2666        if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2667                return nfserr_toosmall;
2668        if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2669                return nfserr_toosmall;
2670        ca->headerpadsz = 0;
2671        ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2672        ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2673        ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2674        ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2675                        NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2676        ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2677        /*
2678         * Note decreasing slot size below client's request may make it
2679         * difficult for client to function correctly, whereas
2680         * decreasing the number of slots will (just?) affect
2681         * performance.  When short on memory we therefore prefer to
2682         * decrease number of slots instead of their size.  Clients that
2683         * request larger slots than they need will get poor results:
2684         */
2685        ca->maxreqs = nfsd4_get_drc_mem(ca);
2686        if (!ca->maxreqs)
2687                return nfserr_jukebox;
2688
2689        return nfs_ok;
2690}
2691
2692#define NFSD_CB_MAX_REQ_SZ      ((NFS4_enc_cb_recall_sz + \
2693                                 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2694#define NFSD_CB_MAX_RESP_SZ     ((NFS4_dec_cb_recall_sz + \
2695                                 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2696
2697static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2698{
2699        ca->headerpadsz = 0;
2700
2701        /*
2702         * These RPC_MAX_HEADER macros are overkill, especially since we
2703         * don't even do gss on the backchannel yet.  But this is still
2704         * less than 1k.  Tighten up this estimate in the unlikely event
2705         * it turns out to be a problem for some client:
2706         */
2707        if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2708                return nfserr_toosmall;
2709        if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2710                return nfserr_toosmall;
2711        ca->maxresp_cached = 0;
2712        if (ca->maxops < 2)
2713                return nfserr_toosmall;
2714
2715        return nfs_ok;
2716}
2717
2718static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2719{
2720        switch (cbs->flavor) {
2721        case RPC_AUTH_NULL:
2722        case RPC_AUTH_UNIX:
2723                return nfs_ok;
2724        default:
2725                /*
2726                 * GSS case: the spec doesn't allow us to return this
2727                 * error.  But it also doesn't allow us not to support
2728                 * GSS.
2729                 * I'd rather this fail hard than return some error the
2730                 * client might think it can already handle:
2731                 */
2732                return nfserr_encr_alg_unsupp;
2733        }
2734}
2735
2736__be32
2737nfsd4_create_session(struct svc_rqst *rqstp,
2738                     struct nfsd4_compound_state *cstate,
2739                     struct nfsd4_create_session *cr_ses)
2740{
2741        struct sockaddr *sa = svc_addr(rqstp);
2742        struct nfs4_client *conf, *unconf;
2743        struct nfs4_client *old = NULL;
2744        struct nfsd4_session *new;
2745        struct nfsd4_conn *conn;
2746        struct nfsd4_clid_slot *cs_slot = NULL;
2747        __be32 status = 0;
2748        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2749
2750        if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2751                return nfserr_inval;
2752        status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2753        if (status)
2754                return status;
2755        status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2756        if (status)
2757                return status;
2758        status = check_backchannel_attrs(&cr_ses->back_channel);
2759        if (status)
2760                goto out_release_drc_mem;
2761        status = nfserr_jukebox;
2762        new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2763        if (!new)
2764                goto out_release_drc_mem;
2765        conn = alloc_conn_from_crses(rqstp, cr_ses);
2766        if (!conn)
2767                goto out_free_session;
2768
2769        spin_lock(&nn->client_lock);
2770        unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2771        conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2772        WARN_ON_ONCE(conf && unconf);
2773
2774        if (conf) {
2775                status = nfserr_wrong_cred;
2776                if (!mach_creds_match(conf, rqstp))
2777                        goto out_free_conn;
2778                cs_slot = &conf->cl_cs_slot;
2779                status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2780                if (status) {
2781                        if (status == nfserr_replay_cache)
2782                                status = nfsd4_replay_create_session(cr_ses, cs_slot);
2783                        goto out_free_conn;
2784                }
2785        } else if (unconf) {
2786                if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2787                    !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2788                        status = nfserr_clid_inuse;
2789                        goto out_free_conn;
2790                }
2791                status = nfserr_wrong_cred;
2792                if (!mach_creds_match(unconf, rqstp))
2793                        goto out_free_conn;
2794                cs_slot = &unconf->cl_cs_slot;
2795                status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2796                if (status) {
2797                        /* an unconfirmed replay returns misordered */
2798                        status = nfserr_seq_misordered;
2799                        goto out_free_conn;
2800                }
2801                old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2802                if (old) {
2803                        status = mark_client_expired_locked(old);
2804                        if (status) {
2805                                old = NULL;
2806                                goto out_free_conn;
2807                        }
2808                }
2809                move_to_confirmed(unconf);
2810                conf = unconf;
2811        } else {
2812                status = nfserr_stale_clientid;
2813                goto out_free_conn;
2814        }
2815        status = nfs_ok;
2816        /*
2817         * We do not support RDMA or persistent sessions
2818         */
2819        cr_ses->flags &= ~SESSION4_PERSIST;
2820        cr_ses->flags &= ~SESSION4_RDMA;
2821
2822        init_session(rqstp, new, conf, cr_ses);
2823        nfsd4_get_session_locked(new);
2824
2825        memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2826               NFS4_MAX_SESSIONID_LEN);
2827        cs_slot->sl_seqid++;
2828        cr_ses->seqid = cs_slot->sl_seqid;
2829
2830        /* cache solo and embedded create sessions under the client_lock */
2831        nfsd4_cache_create_session(cr_ses, cs_slot, status);
2832        spin_unlock(&nn->client_lock);
2833        /* init connection and backchannel */
2834        nfsd4_init_conn(rqstp, conn, new);
2835        nfsd4_put_session(new);
2836        if (old)
2837                expire_client(old);
2838        return status;
2839out_free_conn:
2840        spin_unlock(&nn->client_lock);
2841        free_conn(conn);
2842        if (old)
2843                expire_client(old);
2844out_free_session:
2845        __free_session(new);
2846out_release_drc_mem:
2847        nfsd4_put_drc_mem(&cr_ses->fore_channel);
2848        return status;
2849}
2850
2851static __be32 nfsd4_map_bcts_dir(u32 *dir)
2852{
2853        switch (*dir) {
2854        case NFS4_CDFC4_FORE:
2855        case NFS4_CDFC4_BACK:
2856                return nfs_ok;
2857        case NFS4_CDFC4_FORE_OR_BOTH:
2858        case NFS4_CDFC4_BACK_OR_BOTH:
2859                *dir = NFS4_CDFC4_BOTH;
2860                return nfs_ok;
2861        };
2862        return nfserr_inval;
2863}
2864
2865__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2866{
2867        struct nfsd4_session *session = cstate->session;
2868        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2869        __be32 status;
2870
2871        status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2872        if (status)
2873                return status;
2874        spin_lock(&nn->client_lock);
2875        session->se_cb_prog = bc->bc_cb_program;
2876        session->se_cb_sec = bc->bc_cb_sec;
2877        spin_unlock(&nn->client_lock);
2878
2879        nfsd4_probe_callback(session->se_client);
2880
2881        return nfs_ok;
2882}
2883
2884__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2885                     struct nfsd4_compound_state *cstate,
2886                     struct nfsd4_bind_conn_to_session *bcts)
2887{
2888        __be32 status;
2889        struct nfsd4_conn *conn;
2890        struct nfsd4_session *session;
2891        struct net *net = SVC_NET(rqstp);
2892        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2893
2894        if (!nfsd4_last_compound_op(rqstp))
2895                return nfserr_not_only_op;
2896        spin_lock(&nn->client_lock);
2897        session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2898        spin_unlock(&nn->client_lock);
2899        if (!session)
2900                goto out_no_session;
2901        status = nfserr_wrong_cred;
2902        if (!mach_creds_match(session->se_client, rqstp))
2903                goto out;
2904        status = nfsd4_map_bcts_dir(&bcts->dir);
2905        if (status)
2906                goto out;
2907        conn = alloc_conn(rqstp, bcts->dir);
2908        status = nfserr_jukebox;
2909        if (!conn)
2910                goto out;
2911        nfsd4_init_conn(rqstp, conn, session);
2912        status = nfs_ok;
2913out:
2914        nfsd4_put_session(session);
2915out_no_session:
2916        return status;
2917}
2918
2919static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2920{
2921        if (!session)
2922                return 0;
2923        return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2924}
2925
2926__be32
2927nfsd4_destroy_session(struct svc_rqst *r,
2928                      struct nfsd4_compound_state *cstate,
2929                      struct nfsd4_destroy_session *sessionid)
2930{
2931        struct nfsd4_session *ses;
2932        __be32 status;
2933        int ref_held_by_me = 0;
2934        struct net *net = SVC_NET(r);
2935        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2936
2937        status = nfserr_not_only_op;
2938        if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2939                if (!nfsd4_last_compound_op(r))
2940                        goto out;
2941                ref_held_by_me++;
2942        }
2943        dump_sessionid(__func__, &sessionid->sessionid);
2944        spin_lock(&nn->client_lock);
2945        ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2946        if (!ses)
2947                goto out_client_lock;
2948        status = nfserr_wrong_cred;
2949        if (!mach_creds_match(ses->se_client, r))
2950                goto out_put_session;
2951        status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2952        if (status)
2953                goto out_put_session;
2954        unhash_session(ses);
2955        spin_unlock(&nn->client_lock);
2956
2957        nfsd4_probe_callback_sync(ses->se_client);
2958
2959        spin_lock(&nn->client_lock);
2960        status = nfs_ok;
2961out_put_session:
2962        nfsd4_put_session_locked(ses);
2963out_client_lock:
2964        spin_unlock(&nn->client_lock);
2965out:
2966        return status;
2967}
2968
2969static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2970{
2971        struct nfsd4_conn *c;
2972
2973        list_for_each_entry(c, &s->se_conns, cn_persession) {
2974                if (c->cn_xprt == xpt) {
2975                        return c;
2976                }
2977        }
2978        return NULL;
2979}
2980
2981static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2982{
2983        struct nfs4_client *clp = ses->se_client;
2984        struct nfsd4_conn *c;
2985        __be32 status = nfs_ok;
2986        int ret;
2987
2988        spin_lock(&clp->cl_lock);
2989        c = __nfsd4_find_conn(new->cn_xprt, ses);
2990        if (c)
2991                goto out_free;
2992        status = nfserr_conn_not_bound_to_session;
2993        if (clp->cl_mach_cred)
2994                goto out_free;
2995        __nfsd4_hash_conn(new, ses);
2996        spin_unlock(&clp->cl_lock);
2997        ret = nfsd4_register_conn(new);
2998        if (ret)
2999                /* oops; xprt is already down: */
3000                nfsd4_conn_lost(&new->cn_xpt_user);
3001        return nfs_ok;
3002out_free:
3003        spin_unlock(&clp->cl_lock);
3004        free_conn(new);
3005        return status;
3006}
3007
3008static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3009{
3010        struct nfsd4_compoundargs *args = rqstp->rq_argp;
3011
3012        return args->opcnt > session->se_fchannel.maxops;
3013}
3014
3015static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3016                                  struct nfsd4_session *session)
3017{
3018        struct xdr_buf *xb = &rqstp->rq_arg;
3019
3020        return xb->len > session->se_fchannel.maxreq_sz;
3021}
3022
3023__be32
3024nfsd4_sequence(struct svc_rqst *rqstp,
3025               struct nfsd4_compound_state *cstate,
3026               struct nfsd4_sequence *seq)
3027{
3028        struct nfsd4_compoundres *resp = rqstp->rq_resp;
3029        struct xdr_stream *xdr = &resp->xdr;
3030        struct nfsd4_session *session;
3031        struct nfs4_client *clp;
3032        struct nfsd4_slot *slot;
3033        struct nfsd4_conn *conn;
3034        __be32 status;
3035        int buflen;
3036        struct net *net = SVC_NET(rqstp);
3037        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3038
3039        if (resp->opcnt != 1)
3040                return nfserr_sequence_pos;
3041
3042        /*
3043         * Will be either used or freed by nfsd4_sequence_check_conn
3044         * below.
3045         */
3046        conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3047        if (!conn)
3048                return nfserr_jukebox;
3049
3050        spin_lock(&nn->client_lock);
3051        session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3052        if (!session)
3053                goto out_no_session;
3054        clp = session->se_client;
3055
3056        status = nfserr_too_many_ops;
3057        if (nfsd4_session_too_many_ops(rqstp, session))
3058                goto out_put_session;
3059
3060        status = nfserr_req_too_big;
3061        if (nfsd4_request_too_big(rqstp, session))
3062                goto out_put_session;
3063
3064        status = nfserr_badslot;
3065        if (seq->slotid >= session->se_fchannel.maxreqs)
3066                goto out_put_session;
3067
3068        slot = session->se_slots[seq->slotid];
3069        dprintk("%s: slotid %d\n", __func__, seq->slotid);
3070
3071        /* We do not negotiate the number of slots yet, so set the
3072         * maxslots to the session maxreqs which is used to encode
3073         * sr_highest_slotid and the sr_target_slot id to maxslots */
3074        seq->maxslots = session->se_fchannel.maxreqs;
3075
3076        status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3077                                        slot->sl_flags & NFSD4_SLOT_INUSE);
3078        if (status == nfserr_replay_cache) {
3079                status = nfserr_seq_misordered;
3080                if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3081                        goto out_put_session;
3082                cstate->slot = slot;
3083                cstate->session = session;
3084                cstate->clp = clp;
3085                /* Return the cached reply status and set cstate->status
3086                 * for nfsd4_proc_compound processing */
3087                status = nfsd4_replay_cache_entry(resp, seq);
3088                cstate->status = nfserr_replay_cache;
3089                goto out;
3090        }
3091        if (status)
3092                goto out_put_session;
3093
3094        status = nfsd4_sequence_check_conn(conn, session);
3095        conn = NULL;
3096        if (status)
3097                goto out_put_session;
3098
3099        buflen = (seq->cachethis) ?
3100                        session->se_fchannel.maxresp_cached :
3101                        session->se_fchannel.maxresp_sz;
3102        status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3103                                    nfserr_rep_too_big;
3104        if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3105                goto out_put_session;
3106        svc_reserve(rqstp, buflen);
3107
3108        status = nfs_ok;
3109        /* Success! bump slot seqid */
3110        slot->sl_seqid = seq->seqid;
3111        slot->sl_flags |= NFSD4_SLOT_INUSE;
3112        if (seq->cachethis)
3113                slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3114        else
3115                slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3116
3117        cstate->slot = slot;
3118        cstate->session = session;
3119        cstate->clp = clp;
3120
3121out:
3122        switch (clp->cl_cb_state) {
3123        case NFSD4_CB_DOWN:
3124                seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3125                break;
3126        case NFSD4_CB_FAULT:
3127                seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3128                break;
3129        default:
3130                seq->status_flags = 0;
3131        }
3132        if (!list_empty(&clp->cl_revoked))
3133                seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3134out_no_session:
3135        if (conn)
3136                free_conn(conn);
3137        spin_unlock(&nn->client_lock);
3138        return status;
3139out_put_session:
3140        nfsd4_put_session_locked(session);
3141        goto out_no_session;
3142}
3143
3144void
3145nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3146{
3147        struct nfsd4_compound_state *cs = &resp->cstate;
3148
3149        if (nfsd4_has_session(cs)) {
3150                if (cs->status != nfserr_replay_cache) {
3151                        nfsd4_store_cache_entry(resp);
3152                        cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3153                }
3154                /* Drop session reference that was taken in nfsd4_sequence() */
3155                nfsd4_put_session(cs->session);
3156        } else if (cs->clp)
3157                put_client_renew(cs->clp);
3158}
3159
3160__be32
3161nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3162{
3163        struct nfs4_client *conf, *unconf;
3164        struct nfs4_client *clp = NULL;
3165        __be32 status = 0;
3166        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3167
3168        spin_lock(&nn->client_lock);
3169        unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3170        conf = find_confirmed_client(&dc->clientid, true, nn);
3171        WARN_ON_ONCE(conf && unconf);
3172
3173        if (conf) {
3174                if (client_has_state(conf)) {
3175                        status = nfserr_clientid_busy;
3176                        goto out;
3177                }
3178                status = mark_client_expired_locked(conf);
3179                if (status)
3180                        goto out;
3181                clp = conf;
3182        } else if (unconf)
3183                clp = unconf;
3184        else {
3185                status = nfserr_stale_clientid;
3186                goto out;
3187        }
3188        if (!mach_creds_match(clp, rqstp)) {
3189                clp = NULL;
3190                status = nfserr_wrong_cred;
3191                goto out;
3192        }
3193        unhash_client_locked(clp);
3194out:
3195        spin_unlock(&nn->client_lock);
3196        if (clp)
3197                expire_client(clp);
3198        return status;
3199}
3200
3201__be32
3202nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3203{
3204        __be32 status = 0;
3205
3206        if (rc->rca_one_fs) {
3207                if (!cstate->current_fh.fh_dentry)
3208                        return nfserr_nofilehandle;
3209                /*
3210                 * We don't take advantage of the rca_one_fs case.
3211                 * That's OK, it's optional, we can safely ignore it.
3212                 */
3213                 return nfs_ok;
3214        }
3215
3216        status = nfserr_complete_already;
3217        if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3218                             &cstate->session->se_client->cl_flags))
3219                goto out;
3220
3221        status = nfserr_stale_clientid;
3222        if (is_client_expired(cstate->session->se_client))
3223                /*
3224                 * The following error isn't really legal.
3225                 * But we only get here if the client just explicitly
3226                 * destroyed the client.  Surely it no longer cares what
3227                 * error it gets back on an operation for the dead
3228                 * client.
3229                 */
3230                goto out;
3231
3232        status = nfs_ok;
3233        nfsd4_client_record_create(cstate->session->se_client);
3234out:
3235        return status;
3236}
3237
3238__be32
3239nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3240                  struct nfsd4_setclientid *setclid)
3241{
3242        struct xdr_netobj       clname = setclid->se_name;
3243        nfs4_verifier           clverifier = setclid->se_verf;
3244        struct nfs4_client      *conf, *new;
3245        struct nfs4_client      *unconf = NULL;
3246        __be32                  status;
3247        struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3248
3249        new = create_client(clname, rqstp, &clverifier);
3250        if (new == NULL)
3251                return nfserr_jukebox;
3252        /* Cases below refer to rfc 3530 section 14.2.33: */
3253        spin_lock(&nn->client_lock);
3254        conf = find_confirmed_client_by_name(&clname, nn);
3255        if (conf && client_has_state(conf)) {
3256                /* case 0: */
3257                status = nfserr_clid_inuse;
3258                if (clp_used_exchangeid(conf))
3259                        goto out;
3260                if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3261                        char addr_str[INET6_ADDRSTRLEN];
3262                        rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3263                                 sizeof(addr_str));
3264                        dprintk("NFSD: setclientid: string in use by client "
3265                                "at %s\n", addr_str);
3266                        goto out;
3267                }
3268        }
3269        unconf = find_unconfirmed_client_by_name(&clname, nn);
3270        if (unconf)
3271                unhash_client_locked(unconf);
3272        if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3273                /* case 1: probable callback update */
3274                copy_clid(new, conf);
3275                gen_confirm(new, nn);
3276        } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3277                gen_clid(new, nn);
3278        new->cl_minorversion = 0;
3279        gen_callback(new, setclid, rqstp);
3280        add_to_unconfirmed(new);
3281        setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3282        setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3283        memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3284        new = NULL;
3285        status = nfs_ok;
3286out:
3287        spin_unlock(&nn->client_lock);
3288        if (new)
3289                free_client(new);
3290        if (unconf)
3291                expire_client(unconf);
3292        return status;
3293}
3294
3295
3296__be32
3297nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3298                         struct nfsd4_compound_state *cstate,
3299                         struct nfsd4_setclientid_confirm *setclientid_confirm)
3300{
3301        struct nfs4_client *conf, *unconf;
3302        struct nfs4_client *old = NULL;
3303        nfs4_verifier confirm = setclientid_confirm->sc_confirm; 
3304        clientid_t * clid = &setclientid_confirm->sc_clientid;
3305        __be32 status;
3306        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3307
3308        if (STALE_CLIENTID(clid, nn))
3309                return nfserr_stale_clientid;
3310
3311        spin_lock(&nn->client_lock);
3312        conf = find_confirmed_client(clid, false, nn);
3313        unconf = find_unconfirmed_client(clid, false, nn);
3314        /*
3315         * We try hard to give out unique clientid's, so if we get an
3316         * attempt to confirm the same clientid with a different cred,
3317         * the client may be buggy; this should never happen.
3318         *
3319         * Nevertheless, RFC 7530 recommends INUSE for this case:
3320         */
3321        status = nfserr_clid_inuse;
3322        if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3323                goto out;
3324        if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3325                goto out;
3326        /* cases below refer to rfc 3530 section 14.2.34: */
3327        if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3328                if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3329                        /* case 2: probable retransmit */
3330                        status = nfs_ok;
3331                } else /* case 4: client hasn't noticed we rebooted yet? */
3332                        status = nfserr_stale_clientid;
3333                goto out;
3334        }
3335        status = nfs_ok;
3336        if (conf) { /* case 1: callback update */
3337                old = unconf;
3338                unhash_client_locked(old);
3339                nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3340        } else { /* case 3: normal case; new or rebooted client */
3341                old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3342                if (old) {
3343                        status = nfserr_clid_inuse;
3344                        if (client_has_state(old)
3345                                        && !same_creds(&unconf->cl_cred,
3346                                                        &old->cl_cred))
3347                                goto out;
3348                        status = mark_client_expired_locked(old);
3349                        if (status) {
3350                                old = NULL;
3351                                goto out;
3352                        }
3353                }
3354                move_to_confirmed(unconf);
3355                conf = unconf;
3356        }
3357        get_client_locked(conf);
3358        spin_unlock(&nn->client_lock);
3359        nfsd4_probe_callback(conf);
3360        spin_lock(&nn->client_lock);
3361        put_client_renew_locked(conf);
3362out:
3363        spin_unlock(&nn->client_lock);
3364        if (old)
3365                expire_client(old);
3366        return status;
3367}
3368
3369static struct nfs4_file *nfsd4_alloc_file(void)
3370{
3371        return kmem_cache_alloc(file_slab, GFP_KERNEL);
3372}
3373
3374/* OPEN Share state helper functions */
3375static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3376                                struct nfs4_file *fp)
3377{
3378        lockdep_assert_held(&state_lock);
3379
3380        atomic_set(&fp->fi_ref, 1);
3381        spin_lock_init(&fp->fi_lock);
3382        INIT_LIST_HEAD(&fp->fi_stateids);
3383        INIT_LIST_HEAD(&fp->fi_delegations);
3384        INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3385        fh_copy_shallow(&fp->fi_fhandle, fh);
3386        fp->fi_deleg_file = NULL;
3387        fp->fi_had_conflict = false;
3388        fp->fi_share_deny = 0;
3389        memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3390        memset(fp->fi_access, 0, sizeof(fp->fi_access));
3391#ifdef CONFIG_NFSD_PNFS
3392        INIT_LIST_HEAD(&fp->fi_lo_states);
3393        atomic_set(&fp->fi_lo_recalls, 0);
3394#endif
3395        hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3396}
3397
3398void
3399nfsd4_free_slabs(void)
3400{
3401        kmem_cache_destroy(odstate_slab);
3402        kmem_cache_destroy(openowner_slab);
3403        kmem_cache_destroy(lockowner_slab);
3404        kmem_cache_destroy(file_slab);
3405        kmem_cache_destroy(stateid_slab);
3406        kmem_cache_destroy(deleg_slab);
3407}
3408
3409int
3410nfsd4_init_slabs(void)
3411{
3412        openowner_slab = kmem_cache_create("nfsd4_openowners",
3413                        sizeof(struct nfs4_openowner), 0, 0, NULL);
3414        if (openowner_slab == NULL)
3415                goto out;
3416        lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3417                        sizeof(struct nfs4_lockowner), 0, 0, NULL);
3418        if (lockowner_slab == NULL)
3419                goto out_free_openowner_slab;
3420        file_slab = kmem_cache_create("nfsd4_files",
3421                        sizeof(struct nfs4_file), 0, 0, NULL);
3422        if (file_slab == NULL)
3423                goto out_free_lockowner_slab;
3424        stateid_slab = kmem_cache_create("nfsd4_stateids",
3425                        sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3426        if (stateid_slab == NULL)
3427                goto out_free_file_slab;
3428        deleg_slab = kmem_cache_create("nfsd4_delegations",
3429                        sizeof(struct nfs4_delegation), 0, 0, NULL);
3430        if (deleg_slab == NULL)
3431                goto out_free_stateid_slab;
3432        odstate_slab = kmem_cache_create("nfsd4_odstate",
3433                        sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3434        if (odstate_slab == NULL)
3435                goto out_free_deleg_slab;
3436        return 0;
3437
3438out_free_deleg_slab:
3439        kmem_cache_destroy(deleg_slab);
3440out_free_stateid_slab:
3441        kmem_cache_destroy(stateid_slab);
3442out_free_file_slab:
3443        kmem_cache_destroy(file_slab);
3444out_free_lockowner_slab:
3445        kmem_cache_destroy(lockowner_slab);
3446out_free_openowner_slab:
3447        kmem_cache_destroy(openowner_slab);
3448out:
3449        dprintk("nfsd4: out of memory while initializing nfsv4\n");
3450        return -ENOMEM;
3451}
3452
3453static void init_nfs4_replay(struct nfs4_replay *rp)
3454{
3455        rp->rp_status = nfserr_serverfault;
3456        rp->rp_buflen = 0;
3457        rp->rp_buf = rp->rp_ibuf;
3458        mutex_init(&rp->rp_mutex);
3459}
3460
3461static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3462                struct nfs4_stateowner *so)
3463{
3464        if (!nfsd4_has_session(cstate)) {
3465                mutex_lock(&so->so_replay.rp_mutex);
3466                cstate->replay_owner = nfs4_get_stateowner(so);
3467        }
3468}
3469
3470void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3471{
3472        struct nfs4_stateowner *so = cstate->replay_owner;
3473
3474        if (so != NULL) {
3475                cstate->replay_owner = NULL;
3476                mutex_unlock(&so->so_replay.rp_mutex);
3477                nfs4_put_stateowner(so);
3478        }
3479}
3480
3481static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3482{
3483        struct nfs4_stateowner *sop;
3484
3485        sop = kmem_cache_alloc(slab, GFP_KERNEL);
3486        if (!sop)
3487                return NULL;
3488
3489        sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3490        if (!sop->so_owner.data) {
3491                kmem_cache_free(slab, sop);
3492                return NULL;
3493        }
3494        sop->so_owner.len = owner->len;
3495
3496        INIT_LIST_HEAD(&sop->so_stateids);
3497        sop->so_client = clp;
3498        init_nfs4_replay(&sop->so_replay);
3499        atomic_set(&sop->so_count, 1);
3500        return sop;
3501}
3502
3503static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3504{
3505        lockdep_assert_held(&clp->cl_lock);
3506
3507        list_add(&oo->oo_owner.so_strhash,
3508                 &clp->cl_ownerstr_hashtbl[strhashval]);
3509        list_add(&oo->oo_perclient, &clp->cl_openowners);
3510}
3511
3512static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3513{
3514        unhash_openowner_locked(openowner(so));
3515}
3516
3517static void nfs4_free_openowner(struct nfs4_stateowner *so)
3518{
3519        struct nfs4_openowner *oo = openowner(so);
3520
3521        kmem_cache_free(openowner_slab, oo);
3522}
3523
3524static const struct nfs4_stateowner_operations openowner_ops = {
3525        .so_unhash =    nfs4_unhash_openowner,
3526        .so_free =      nfs4_free_openowner,
3527};
3528
3529static struct nfs4_ol_stateid *
3530nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3531{
3532        struct nfs4_ol_stateid *local, *ret = NULL;
3533        struct nfs4_openowner *oo = open->op_openowner;
3534
3535        lockdep_assert_held(&fp->fi_lock);
3536
3537        list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3538                /* ignore lock owners */
3539                if (local->st_stateowner->so_is_open_owner == 0)
3540                        continue;
3541                if (local->st_stateowner == &oo->oo_owner) {
3542                        ret = local;
3543                        atomic_inc(&ret->st_stid.sc_count);
3544                        break;
3545                }
3546        }
3547        return ret;
3548}
3549
3550static struct nfs4_openowner *
3551alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3552                           struct nfsd4_compound_state *cstate)
3553{
3554        struct nfs4_client *clp = cstate->clp;
3555        struct nfs4_openowner *oo, *ret;
3556
3557        oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3558        if (!oo)
3559                return NULL;
3560        oo->oo_owner.so_ops = &openowner_ops;
3561        oo->oo_owner.so_is_open_owner = 1;
3562        oo->oo_owner.so_seqid = open->op_seqid;
3563        oo->oo_flags = 0;
3564        if (nfsd4_has_session(cstate))
3565                oo->oo_flags |= NFS4_OO_CONFIRMED;
3566        oo->oo_time = 0;
3567        oo->oo_last_closed_stid = NULL;
3568        INIT_LIST_HEAD(&oo->oo_close_lru);
3569        spin_lock(&clp->cl_lock);
3570        ret = find_openstateowner_str_locked(strhashval, open, clp);
3571        if (ret == NULL) {
3572                hash_openowner(oo, clp, strhashval);
3573                ret = oo;
3574        } else
3575                nfs4_free_stateowner(&oo->oo_owner);
3576
3577        spin_unlock(&clp->cl_lock);
3578        return ret;
3579}
3580
3581static struct nfs4_ol_stateid *
3582init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp,
3583                struct nfsd4_open *open)
3584{
3585
3586        struct nfs4_openowner *oo = open->op_openowner;
3587        struct nfs4_ol_stateid *retstp = NULL;
3588
3589        /* We are moving these outside of the spinlocks to avoid the warnings */
3590        mutex_init(&stp->st_mutex);
3591        mutex_lock(&stp->st_mutex);
3592
3593        spin_lock(&oo->oo_owner.so_client->cl_lock);
3594        spin_lock(&fp->fi_lock);
3595
3596        retstp = nfsd4_find_existing_open(fp, open);
3597        if (retstp)
3598                goto out_unlock;
3599        atomic_inc(&stp->st_stid.sc_count);
3600        stp->st_stid.sc_type = NFS4_OPEN_STID;
3601        INIT_LIST_HEAD(&stp->st_locks);
3602        stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3603        get_nfs4_file(fp);
3604        stp->st_stid.sc_file = fp;
3605        stp->st_access_bmap = 0;
3606        stp->st_deny_bmap = 0;
3607        stp->st_openstp = NULL;
3608        list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3609        list_add(&stp->st_perfile, &fp->fi_stateids);
3610
3611out_unlock:
3612        spin_unlock(&fp->fi_lock);
3613        spin_unlock(&oo->oo_owner.so_client->cl_lock);
3614        if (retstp) {
3615                mutex_lock(&retstp->st_mutex);
3616                /* Not that we need to, just for neatness */
3617                mutex_unlock(&stp->st_mutex);
3618        }
3619        return retstp;
3620}
3621
3622/*
3623 * In the 4.0 case we need to keep the owners around a little while to handle
3624 * CLOSE replay. We still do need to release any file access that is held by
3625 * them before returning however.
3626 */
3627static void
3628move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3629{
3630        struct nfs4_ol_stateid *last;
3631        struct nfs4_openowner *oo = openowner(s->st_stateowner);
3632        struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3633                                                nfsd_net_id);
3634
3635        dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3636
3637        /*
3638         * We know that we hold one reference via nfsd4_close, and another
3639         * "persistent" reference for the client. If the refcount is higher
3640         * than 2, then there are still calls in progress that are using this
3641         * stateid. We can't put the sc_file reference until they are finished.
3642         * Wait for the refcount to drop to 2. Since it has been unhashed,
3643         * there should be no danger of the refcount going back up again at
3644         * this point.
3645         */
3646        wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3647
3648        release_all_access(s);
3649        if (s->st_stid.sc_file) {
3650                put_nfs4_file(s->st_stid.sc_file);
3651                s->st_stid.sc_file = NULL;
3652        }
3653
3654        spin_lock(&nn->client_lock);
3655        last = oo->oo_last_closed_stid;
3656        oo->oo_last_closed_stid = s;
3657        list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3658        oo->oo_time = get_seconds();
3659        spin_unlock(&nn->client_lock);
3660        if (last)
3661                nfs4_put_stid(&last->st_stid);
3662}
3663
3664/* search file_hashtbl[] for file */
3665static struct nfs4_file *
3666find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3667{
3668        struct nfs4_file *fp;
3669
3670        hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3671                if (fh_match(&fp->fi_fhandle, fh)) {
3672                        if (atomic_inc_not_zero(&fp->fi_ref))
3673                                return fp;
3674                }
3675        }
3676        return NULL;
3677}
3678
3679struct nfs4_file *
3680find_file(struct knfsd_fh *fh)
3681{
3682        struct nfs4_file *fp;
3683        unsigned int hashval = file_hashval(fh);
3684
3685        rcu_read_lock();
3686        fp = find_file_locked(fh, hashval);
3687        rcu_read_unlock();
3688        return fp;
3689}
3690
3691static struct nfs4_file *
3692find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3693{
3694        struct nfs4_file *fp;
3695        unsigned int hashval = file_hashval(fh);
3696
3697        rcu_read_lock();
3698        fp = find_file_locked(fh, hashval);
3699        rcu_read_unlock();
3700        if (fp)
3701                return fp;
3702
3703        spin_lock(&state_lock);
3704        fp = find_file_locked(fh, hashval);
3705        if (likely(fp == NULL)) {
3706                nfsd4_init_file(fh, hashval, new);
3707                fp = new;
3708        }
3709        spin_unlock(&state_lock);
3710
3711        return fp;
3712}
3713
3714/*
3715 * Called to check deny when READ with all zero stateid or
3716 * WRITE with all zero or all one stateid
3717 */
3718static __be32
3719nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3720{
3721        struct nfs4_file *fp;
3722        __be32 ret = nfs_ok;
3723
3724        fp = find_file(&current_fh->fh_handle);
3725        if (!fp)
3726                return ret;
3727        /* Check for conflicting share reservations */
3728        spin_lock(&fp->fi_lock);
3729        if (fp->fi_share_deny & deny_type)
3730                ret = nfserr_locked;
3731        spin_unlock(&fp->fi_lock);
3732        put_nfs4_file(fp);
3733        return ret;
3734}
3735
3736#define cb_to_delegation(cb) \
3737        container_of(cb, struct nfs4_delegation, dl_recall)
3738
3739static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3740{
3741        struct nfs4_delegation *dp = cb_to_delegation(cb);
3742        struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3743                                          nfsd_net_id);
3744
3745        block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3746
3747        /*
3748         * We can't do this in nfsd_break_deleg_cb because it is
3749         * already holding inode->i_lock.
3750         *
3751         * If the dl_time != 0, then we know that it has already been
3752         * queued for a lease break. Don't queue it again.
3753         */
3754        spin_lock(&state_lock);
3755        if (dp->dl_time == 0) {
3756                dp->dl_time = get_seconds();
3757                list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3758        }
3759        spin_unlock(&state_lock);
3760}
3761
3762static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3763                struct rpc_task *task)
3764{
3765        struct nfs4_delegation *dp = cb_to_delegation(cb);
3766
3767        if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3768                return 1;
3769
3770        switch (task->tk_status) {
3771        case 0:
3772                return 1;
3773        case -EBADHANDLE:
3774        case -NFS4ERR_BAD_STATEID:
3775                /*
3776                 * Race: client probably got cb_recall before open reply
3777                 * granting delegation.
3778                 */
3779                if (dp->dl_retries--) {
3780                        rpc_delay(task, 2 * HZ);
3781                        return 0;
3782                }
3783                /*FALLTHRU*/
3784        default:
3785                return -1;
3786        }
3787}
3788
3789static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3790{
3791        struct nfs4_delegation *dp = cb_to_delegation(cb);
3792
3793        nfs4_put_stid(&dp->dl_stid);
3794}
3795
3796static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3797        .prepare        = nfsd4_cb_recall_prepare,
3798        .done           = nfsd4_cb_recall_done,
3799        .release        = nfsd4_cb_recall_release,
3800};
3801
3802static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3803{
3804        /*
3805         * We're assuming the state code never drops its reference
3806         * without first removing the lease.  Since we're in this lease
3807         * callback (and since the lease code is serialized by the kernel
3808         * lock) we know the server hasn't removed the lease yet, we know
3809         * it's safe to take a reference.
3810         */
3811        atomic_inc(&dp->dl_stid.sc_count);
3812        nfsd4_run_cb(&dp->dl_recall);
3813}
3814
3815/* Called from break_lease() with i_lock held. */
3816static void nfsd_break_deleg_cb(struct file_lock *fl)
3817{
3818        struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3819        struct nfs4_delegation *dp;
3820
3821        if (!fp) {
3822                WARN(1, "(%p)->fl_owner NULL\n", fl);
3823                return;
3824        }
3825        if (fp->fi_had_conflict) {
3826                WARN(1, "duplicate break on %p\n", fp);
3827                return;
3828        }
3829        /*
3830         * We don't want the locks code to timeout the lease for us;
3831         * we'll remove it ourself if a delegation isn't returned
3832         * in time:
3833         */
3834        fl->fl_break_time = 0;
3835
3836        spin_lock(&fp->fi_lock);
3837        fp->fi_had_conflict = true;
3838        /*
3839         * If there are no delegations on the list, then we can't count on this
3840         * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3841         * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3842         * true should keep any new delegations from being hashed.
3843         */
3844        if (list_empty(&fp->fi_delegations))
3845                fl->fl_break_time = jiffies;
3846        else
3847                list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3848                        nfsd_break_one_deleg(dp);
3849        spin_unlock(&fp->fi_lock);
3850}
3851
3852static
3853int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3854{
3855        if (arg & F_UNLCK)
3856                return lease_modify(onlist, arg);
3857        else
3858                return -EAGAIN;
3859}
3860
3861static const struct lock_manager_operations nfsd_lease_mng_ops = {
3862        .lm_break = nfsd_break_deleg_cb,
3863        .lm_change = nfsd_change_deleg_cb,
3864};
3865
3866static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3867{
3868        if (nfsd4_has_session(cstate))
3869                return nfs_ok;
3870        if (seqid == so->so_seqid - 1)
3871                return nfserr_replay_me;
3872        if (seqid == so->so_seqid)
3873                return nfs_ok;
3874        return nfserr_bad_seqid;
3875}
3876
3877static __be32 lookup_clientid(clientid_t *clid,
3878                struct nfsd4_compound_state *cstate,
3879                struct nfsd_net *nn)
3880{
3881        struct nfs4_client *found;
3882
3883        if (cstate->clp) {
3884                found = cstate->clp;
3885                if (!same_clid(&found->cl_clientid, clid))
3886                        return nfserr_stale_clientid;
3887                return nfs_ok;
3888        }
3889
3890        if (STALE_CLIENTID(clid, nn))
3891                return nfserr_stale_clientid;
3892
3893        /*
3894         * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3895         * cached already then we know this is for is for v4.0 and "sessions"
3896         * will be false.
3897         */
3898        WARN_ON_ONCE(cstate->session);
3899        spin_lock(&nn->client_lock);
3900        found = find_confirmed_client(clid, false, nn);
3901        if (!found) {
3902                spin_unlock(&nn->client_lock);
3903                return nfserr_expired;
3904        }
3905        atomic_inc(&found->cl_refcount);
3906        spin_unlock(&nn->client_lock);
3907
3908        /* Cache the nfs4_client in cstate! */
3909        cstate->clp = found;
3910        return nfs_ok;
3911}
3912
3913__be32
3914nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3915                    struct nfsd4_open *open, struct nfsd_net *nn)
3916{
3917        clientid_t *clientid = &open->op_clientid;
3918        struct nfs4_client *clp = NULL;
3919        unsigned int strhashval;
3920        struct nfs4_openowner *oo = NULL;
3921        __be32 status;
3922
3923        if (STALE_CLIENTID(&open->op_clientid, nn))
3924                return nfserr_stale_clientid;
3925        /*
3926         * In case we need it later, after we've already created the
3927         * file and don't want to risk a further failure:
3928         */
3929        open->op_file = nfsd4_alloc_file();
3930        if (open->op_file == NULL)
3931                return nfserr_jukebox;
3932
3933        status = lookup_clientid(clientid, cstate, nn);
3934        if (status)
3935                return status;
3936        clp = cstate->clp;
3937
3938        strhashval = ownerstr_hashval(&open->op_owner);
3939        oo = find_openstateowner_str(strhashval, open, clp);
3940        open->op_openowner = oo;
3941        if (!oo) {
3942                goto new_owner;
3943        }
3944        if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3945                /* Replace unconfirmed owners without checking for replay. */
3946                release_openowner(oo);
3947                open->op_openowner = NULL;
3948                goto new_owner;
3949        }
3950        status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3951        if (status)
3952                return status;
3953        goto alloc_stateid;
3954new_owner:
3955        oo = alloc_init_open_stateowner(strhashval, open, cstate);
3956        if (oo == NULL)
3957                return nfserr_jukebox;
3958        open->op_openowner = oo;
3959alloc_stateid:
3960        open->op_stp = nfs4_alloc_open_stateid(clp);
3961        if (!open->op_stp)
3962                return nfserr_jukebox;
3963
3964        if (nfsd4_has_session(cstate) &&
3965            (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3966                open->op_odstate = alloc_clnt_odstate(clp);
3967                if (!open->op_odstate)
3968                        return nfserr_jukebox;
3969        }
3970
3971        return nfs_ok;
3972}
3973
3974static inline __be32
3975nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3976{
3977        if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3978                return nfserr_openmode;
3979        else
3980                return nfs_ok;
3981}
3982
3983static int share_access_to_flags(u32 share_access)
3984{
3985        return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3986}
3987
3988static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3989{
3990        struct nfs4_stid *ret;
3991
3992        ret = find_stateid_by_type(cl, s,
3993                                NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
3994        if (!ret)
3995                return NULL;
3996        return delegstateid(ret);
3997}
3998
3999static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4000{
4001        return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4002               open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4003}
4004
4005static __be32
4006nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4007                struct nfs4_delegation **dp)
4008{
4009        int flags;
4010        __be32 status = nfserr_bad_stateid;
4011        struct nfs4_delegation *deleg;
4012
4013        deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4014        if (deleg == NULL)
4015                goto out;
4016        if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4017                nfs4_put_stid(&deleg->dl_stid);
4018                if (cl->cl_minorversion)
4019                        status = nfserr_deleg_revoked;
4020                goto out;
4021        }
4022        flags = share_access_to_flags(open->op_share_access);
4023        status = nfs4_check_delegmode(deleg, flags);
4024        if (status) {
4025                nfs4_put_stid(&deleg->dl_stid);
4026                goto out;
4027        }
4028        *dp = deleg;
4029out:
4030        if (!nfsd4_is_deleg_cur(open))
4031                return nfs_ok;
4032        if (status)
4033                return status;
4034        open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4035        return nfs_ok;
4036}
4037
4038static inline int nfs4_access_to_access(u32 nfs4_access)
4039{
4040        int flags = 0;
4041
4042        if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4043                flags |= NFSD_MAY_READ;
4044        if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4045                flags |= NFSD_MAY_WRITE;
4046        return flags;
4047}
4048
4049static inline __be32
4050nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4051                struct nfsd4_open *open)
4052{
4053        struct iattr iattr = {
4054                .ia_valid = ATTR_SIZE,
4055                .ia_size = 0,
4056        };
4057        if (!open->op_truncate)
4058                return 0;
4059        if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4060                return nfserr_inval;
4061        return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4062}
4063
4064static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4065                struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4066                struct nfsd4_open *open)
4067{
4068        struct file *filp = NULL;
4069        __be32 status;
4070        int oflag = nfs4_access_to_omode(open->op_share_access);
4071        int access = nfs4_access_to_access(open->op_share_access);
4072        unsigned char old_access_bmap, old_deny_bmap;
4073
4074        spin_lock(&fp->fi_lock);
4075
4076        /*
4077         * Are we trying to set a deny mode that would conflict with
4078         * current access?
4079         */
4080        status = nfs4_file_check_deny(fp, open->op_share_deny);
4081        if (status != nfs_ok) {
4082                spin_unlock(&fp->fi_lock);
4083                goto out;
4084        }
4085
4086        /* set access to the file */
4087        status = nfs4_file_get_access(fp, open->op_share_access);
4088        if (status != nfs_ok) {
4089                spin_unlock(&fp->fi_lock);
4090                goto out;
4091        }
4092
4093        /* Set access bits in stateid */
4094        old_access_bmap = stp->st_access_bmap;
4095        set_access(open->op_share_access, stp);
4096
4097        /* Set new deny mask */
4098        old_deny_bmap = stp->st_deny_bmap;
4099        set_deny(open->op_share_deny, stp);
4100        fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4101
4102        if (!fp->fi_fds[oflag]) {
4103                spin_unlock(&fp->fi_lock);
4104                status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4105                if (status)
4106                        goto out_put_access;
4107                spin_lock(&fp->fi_lock);
4108                if (!fp->fi_fds[oflag]) {
4109                        fp->fi_fds[oflag] = filp;
4110                        filp = NULL;
4111                }
4112        }
4113        spin_unlock(&fp->fi_lock);
4114        if (filp)
4115                fput(filp);
4116
4117        status = nfsd4_truncate(rqstp, cur_fh, open);
4118        if (status)
4119                goto out_put_access;
4120out:
4121        return status;
4122out_put_access:
4123        stp->st_access_bmap = old_access_bmap;
4124        nfs4_file_put_access(fp, open->op_share_access);
4125        reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4126        goto out;
4127}
4128
4129static __be32
4130nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
4131{
4132        __be32 status;
4133        unsigned char old_deny_bmap = stp->st_deny_bmap;
4134
4135        if (!test_access(open->op_share_access, stp))
4136                return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4137
4138        /* test and set deny mode */
4139        spin_lock(&fp->fi_lock);
4140        status = nfs4_file_check_deny(fp, open->op_share_deny);
4141        if (status == nfs_ok) {
4142                set_deny(open->op_share_deny, stp);
4143                fp->fi_share_deny |=
4144                                (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4145        }
4146        spin_unlock(&fp->fi_lock);
4147
4148        if (status != nfs_ok)
4149                return status;
4150
4151        status = nfsd4_truncate(rqstp, cur_fh, open);
4152        if (status != nfs_ok)
4153                reset_union_bmap_deny(old_deny_bmap, stp);
4154        return status;
4155}
4156
4157/* Should we give out recallable state?: */
4158static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4159{
4160        if (clp->cl_cb_state == NFSD4_CB_UP)
4161                return true;
4162        /*
4163         * In the sessions case, since we don't have to establish a
4164         * separate connection for callbacks, we assume it's OK
4165         * until we hear otherwise:
4166         */
4167        return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4168}
4169
4170static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4171{
4172        struct file_lock *fl;
4173
4174        fl = locks_alloc_lock();
4175        if (!fl)
4176                return NULL;
4177        fl->fl_lmops = &nfsd_lease_mng_ops;
4178        fl->fl_flags = FL_DELEG;
4179        fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4180        fl->fl_end = OFFSET_MAX;
4181        fl->fl_owner = (fl_owner_t)fp;
4182        fl->fl_pid = current->tgid;
4183        return fl;
4184}
4185
4186/**
4187 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4188 * @dp:   a pointer to the nfs4_delegation we're adding.
4189 *
4190 * Return:
4191 *      On success: Return code will be 0 on success.
4192 *
4193 *      On error: -EAGAIN if there was an existing delegation.
4194 *                 nonzero if there is an error in other cases.
4195 *
4196 */
4197
4198static int nfs4_setlease(struct nfs4_delegation *dp)
4199{
4200        struct nfs4_file *fp = dp->dl_stid.sc_file;
4201        struct file_lock *fl;
4202        struct file *filp;
4203        int status = 0;
4204
4205        fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4206        if (!fl)
4207                return -ENOMEM;
4208        filp = find_readable_file(fp);
4209        if (!filp) {
4210                /* We should always have a readable file here */
4211                WARN_ON_ONCE(1);
4212                locks_free_lock(fl);
4213                return -EBADF;
4214        }
4215        fl->fl_file = filp;
4216        status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4217        if (status) {
4218                locks_free_lock(fl);
4219                goto out_fput;
4220        }
4221        spin_lock(&state_lock);
4222        spin_lock(&fp->fi_lock);
4223        /* Did the lease get broken before we took the lock? */
4224        status = -EAGAIN;
4225        if (fp->fi_had_conflict)
4226                goto out_unlock;
4227        /* Race breaker */
4228        if (fp->fi_deleg_file) {
4229                status = hash_delegation_locked(dp, fp);
4230                goto out_unlock;
4231        }
4232        fp->fi_deleg_file = filp;
4233        fp->fi_delegees = 0;
4234        status = hash_delegation_locked(dp, fp);
4235        spin_unlock(&fp->fi_lock);
4236        spin_unlock(&state_lock);
4237        if (status) {
4238                /* Should never happen, this is a new fi_deleg_file  */
4239                WARN_ON_ONCE(1);
4240                goto out_fput;
4241        }
4242        return 0;
4243out_unlock:
4244        spin_unlock(&fp->fi_lock);
4245        spin_unlock(&state_lock);
4246out_fput:
4247        fput(filp);
4248        return status;
4249}
4250
4251static struct nfs4_delegation *
4252nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4253                    struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4254{
4255        int status;
4256        struct nfs4_delegation *dp;
4257
4258        if (fp->fi_had_conflict)
4259                return ERR_PTR(-EAGAIN);
4260
4261        spin_lock(&state_lock);
4262        spin_lock(&fp->fi_lock);
4263        status = nfs4_get_existing_delegation(clp, fp);
4264        spin_unlock(&fp->fi_lock);
4265        spin_unlock(&state_lock);
4266
4267        if (status)
4268                return ERR_PTR(status);
4269
4270        dp = alloc_init_deleg(clp, fh, odstate);
4271        if (!dp)
4272                return ERR_PTR(-ENOMEM);
4273
4274        get_nfs4_file(fp);
4275        spin_lock(&state_lock);
4276        spin_lock(&fp->fi_lock);
4277        dp->dl_stid.sc_file = fp;
4278        if (!fp->fi_deleg_file) {
4279                spin_unlock(&fp->fi_lock);
4280                spin_unlock(&state_lock);
4281                status = nfs4_setlease(dp);
4282                goto out;
4283        }
4284        if (fp->fi_had_conflict) {
4285                status = -EAGAIN;
4286                goto out_unlock;
4287        }
4288        status = hash_delegation_locked(dp, fp);
4289out_unlock:
4290        spin_unlock(&fp->fi_lock);
4291        spin_unlock(&state_lock);
4292out:
4293        if (status) {
4294                put_clnt_odstate(dp->dl_clnt_odstate);
4295                nfs4_put_stid(&dp->dl_stid);
4296                return ERR_PTR(status);
4297        }
4298        return dp;
4299}
4300
4301static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4302{
4303        open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4304        if (status == -EAGAIN)
4305                open->op_why_no_deleg = WND4_CONTENTION;
4306        else {
4307                open->op_why_no_deleg = WND4_RESOURCE;
4308                switch (open->op_deleg_want) {
4309                case NFS4_SHARE_WANT_READ_DELEG:
4310                case NFS4_SHARE_WANT_WRITE_DELEG:
4311                case NFS4_SHARE_WANT_ANY_DELEG:
4312                        break;
4313                case NFS4_SHARE_WANT_CANCEL:
4314                        open->op_why_no_deleg = WND4_CANCELLED;
4315                        break;
4316                case NFS4_SHARE_WANT_NO_DELEG:
4317                        WARN_ON_ONCE(1);
4318                }
4319        }
4320}
4321
4322/*
4323 * Attempt to hand out a delegation.
4324 *
4325 * Note we don't support write delegations, and won't until the vfs has
4326 * proper support for them.
4327 */
4328static void
4329nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4330                        struct nfs4_ol_stateid *stp)
4331{
4332        struct nfs4_delegation *dp;
4333        struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4334        struct nfs4_client *clp = stp->st_stid.sc_client;
4335        int cb_up;
4336        int status = 0;
4337
4338        cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4339        open->op_recall = 0;
4340        switch (open->op_claim_type) {
4341                case NFS4_OPEN_CLAIM_PREVIOUS:
4342                        if (!cb_up)
4343                                open->op_recall = 1;
4344                        if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4345                                goto out_no_deleg;
4346                        break;
4347                case NFS4_OPEN_CLAIM_NULL:
4348                case NFS4_OPEN_CLAIM_FH:
4349                        /*
4350                         * Let's not give out any delegations till everyone's
4351                         * had the chance to reclaim theirs, *and* until
4352                         * NLM locks have all been reclaimed:
4353                         */
4354                        if (locks_in_grace(clp->net))
4355                                goto out_no_deleg;
4356                        if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4357                                goto out_no_deleg;
4358                        /*
4359                         * Also, if the file was opened for write or
4360                         * create, there's a good chance the client's
4361                         * about to write to it, resulting in an
4362                         * immediate recall (since we don't support
4363                         * write delegations):
4364                         */
4365                        if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4366                                goto out_no_deleg;
4367                        if (open->op_create == NFS4_OPEN_CREATE)
4368                                goto out_no_deleg;
4369                        break;
4370                default:
4371                        goto out_no_deleg;
4372        }
4373        dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4374        if (IS_ERR(dp))
4375                goto out_no_deleg;
4376
4377        memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4378
4379        dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4380                STATEID_VAL(&dp->dl_stid.sc_stateid));
4381        open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4382        nfs4_put_stid(&dp->dl_stid);
4383        return;
4384out_no_deleg:
4385        open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4386        if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4387            open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4388                dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4389                open->op_recall = 1;
4390        }
4391
4392        /* 4.1 client asking for a delegation? */
4393        if (open->op_deleg_want)
4394                nfsd4_open_deleg_none_ext(open, status);
4395        return;
4396}
4397
4398static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4399                                        struct nfs4_delegation *dp)
4400{
4401        if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4402            dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4403                open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4404                open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4405        } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4406                   dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4407                open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4408                open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4409        }
4410        /* Otherwise the client must be confused wanting a delegation
4411         * it already has, therefore we don't return
4412         * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4413         */
4414}
4415
4416__be32
4417nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4418{
4419        struct nfsd4_compoundres *resp = rqstp->rq_resp;
4420        struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4421        struct nfs4_file *fp = NULL;
4422        struct nfs4_ol_stateid *stp = NULL;
4423        struct nfs4_ol_stateid *swapstp = NULL;
4424        struct nfs4_delegation *dp = NULL;
4425        __be32 status;
4426
4427        /*
4428         * Lookup file; if found, lookup stateid and check open request,
4429         * and check for delegations in the process of being recalled.
4430         * If not found, create the nfs4_file struct
4431         */
4432        fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
4433        if (fp != open->op_file) {
4434                status = nfs4_check_deleg(cl, open, &dp);
4435                if (status)
4436                        goto out;
4437                spin_lock(&fp->fi_lock);
4438                stp = nfsd4_find_existing_open(fp, open);
4439                spin_unlock(&fp->fi_lock);
4440        } else {
4441                open->op_file = NULL;
4442                status = nfserr_bad_stateid;
4443                if (nfsd4_is_deleg_cur(open))
4444                        goto out;
4445        }
4446
4447        /*
4448         * OPEN the file, or upgrade an existing OPEN.
4449         * If truncate fails, the OPEN fails.
4450         */
4451        if (stp) {
4452                /* Stateid was found, this is an OPEN upgrade */
4453                mutex_lock(&stp->st_mutex);
4454                status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4455                if (status) {
4456                        mutex_unlock(&stp->st_mutex);
4457                        goto out;
4458                }
4459        } else {
4460                stp = open->op_stp;
4461                open->op_stp = NULL;
4462                /*
4463                 * init_open_stateid() either returns a locked stateid
4464                 * it found, or initializes and locks the new one we passed in
4465                 */
4466                swapstp = init_open_stateid(stp, fp, open);
4467                if (swapstp) {
4468                        nfs4_put_stid(&stp->st_stid);
4469                        stp = swapstp;
4470                        status = nfs4_upgrade_open(rqstp, fp, current_fh,
4471                                                stp, open);
4472                        if (status) {
4473                                mutex_unlock(&stp->st_mutex);
4474                                goto out;
4475                        }
4476                        goto upgrade_out;
4477                }
4478                status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4479                if (status) {
4480                        mutex_unlock(&stp->st_mutex);
4481                        release_open_stateid(stp);
4482                        goto out;
4483                }
4484
4485                stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4486                                                        open->op_odstate);
4487                if (stp->st_clnt_odstate == open->op_odstate)
4488                        open->op_odstate = NULL;
4489        }
4490upgrade_out:
4491        nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4492        mutex_unlock(&stp->st_mutex);
4493
4494        if (nfsd4_has_session(&resp->cstate)) {
4495                if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4496                        open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4497                        open->op_why_no_deleg = WND4_NOT_WANTED;
4498                        goto nodeleg;
4499                }
4500        }
4501
4502        /*
4503        * Attempt to hand out a delegation. No error return, because the
4504        * OPEN succeeds even if we fail.
4505        */
4506        nfs4_open_delegation(current_fh, open, stp);
4507nodeleg:
4508        status = nfs_ok;
4509
4510        dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4511                STATEID_VAL(&stp->st_stid.sc_stateid));
4512out:
4513        /* 4.1 client trying to upgrade/downgrade delegation? */
4514        if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4515            open->op_deleg_want)
4516                nfsd4_deleg_xgrade_none_ext(open, dp);
4517
4518        if (fp)
4519                put_nfs4_file(fp);
4520        if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4521                open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4522        /*
4523        * To finish the open response, we just need to set the rflags.
4524        */
4525        open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4526        if (nfsd4_has_session(&resp->cstate))
4527                open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4528        else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
4529                open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4530
4531        if (dp)
4532                nfs4_put_stid(&dp->dl_stid);
4533        if (stp)
4534                nfs4_put_stid(&stp->st_stid);
4535
4536        return status;
4537}
4538
4539void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4540                              struct nfsd4_open *open)
4541{
4542        if (open->op_openowner) {
4543                struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4544
4545                nfsd4_cstate_assign_replay(cstate, so);
4546                nfs4_put_stateowner(so);
4547        }
4548        if (open->op_file)
4549                kmem_cache_free(file_slab, open->op_file);
4550        if (open->op_stp)
4551                nfs4_put_stid(&open->op_stp->st_stid);
4552        if (open->op_odstate)
4553                kmem_cache_free(odstate_slab, open->op_odstate);
4554}
4555
4556__be32
4557nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4558            clientid_t *clid)
4559{
4560        struct nfs4_client *clp;
4561        __be32 status;
4562        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4563
4564        dprintk("process_renew(%08x/%08x): starting\n", 
4565                        clid->cl_boot, clid->cl_id);
4566        status = lookup_clientid(clid, cstate, nn);
4567        if (status)
4568                goto out;
4569        clp = cstate->clp;
4570        status = nfserr_cb_path_down;
4571        if (!list_empty(&clp->cl_delegations)
4572                        && clp->cl_cb_state != NFSD4_CB_UP)
4573                goto out;
4574        status = nfs_ok;
4575out:
4576        return status;
4577}
4578
4579void
4580nfsd4_end_grace(struct nfsd_net *nn)
4581{
4582        /* do nothing if grace period already ended */
4583        if (nn->grace_ended)
4584                return;
4585
4586        dprintk("NFSD: end of grace period\n");
4587        nn->grace_ended = true;
4588        /*
4589         * If the server goes down again right now, an NFSv4
4590         * client will still be allowed to reclaim after it comes back up,
4591         * even if it hasn't yet had a chance to reclaim state this time.
4592         *
4593         */
4594        nfsd4_record_grace_done(nn);
4595        /*
4596         * At this point, NFSv4 clients can still reclaim.  But if the
4597         * server crashes, any that have not yet reclaimed will be out
4598         * of luck on the next boot.
4599         *
4600         * (NFSv4.1+ clients are considered to have reclaimed once they
4601         * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
4602         * have reclaimed after their first OPEN.)
4603         */
4604        locks_end_grace(&nn->nfsd4_manager);
4605        /*
4606         * At this point, and once lockd and/or any other containers
4607         * exit their grace period, further reclaims will fail and
4608         * regular locking can resume.
4609         */
4610}
4611
4612static time_t
4613nfs4_laundromat(struct nfsd_net *nn)
4614{
4615        struct nfs4_client *clp;
4616        struct nfs4_openowner *oo;
4617        struct nfs4_delegation *dp;
4618        struct nfs4_ol_stateid *stp;
4619        struct nfsd4_blocked_lock *nbl;
4620        struct list_head *pos, *next, reaplist;
4621        time_t cutoff = get_seconds() - nn->nfsd4_lease;
4622        time_t t, new_timeo = nn->nfsd4_lease;
4623
4624        dprintk("NFSD: laundromat service - starting\n");
4625        nfsd4_end_grace(nn);
4626        INIT_LIST_HEAD(&reaplist);
4627        spin_lock(&nn->client_lock);
4628        list_for_each_safe(pos, next, &nn->client_lru) {
4629                clp = list_entry(pos, struct nfs4_client, cl_lru);
4630                if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4631                        t = clp->cl_time - cutoff;
4632                        new_timeo = min(new_timeo, t);
4633                        break;
4634                }
4635                if (mark_client_expired_locked(clp)) {
4636                        dprintk("NFSD: client in use (clientid %08x)\n",
4637                                clp->cl_clientid.cl_id);
4638                        continue;
4639                }
4640                list_add(&clp->cl_lru, &reaplist);
4641        }
4642        spin_unlock(&nn->client_lock);
4643        list_for_each_safe(pos, next, &reaplist) {
4644                clp = list_entry(pos, struct nfs4_client, cl_lru);
4645                dprintk("NFSD: purging unused client (clientid %08x)\n",
4646                        clp->cl_clientid.cl_id);
4647                list_del_init(&clp->cl_lru);
4648                expire_client(clp);
4649        }
4650        spin_lock(&state_lock);
4651        list_for_each_safe(pos, next, &nn->del_recall_lru) {
4652                dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4653                if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4654                        t = dp->dl_time - cutoff;
4655                        new_timeo = min(new_timeo, t);
4656                        break;
4657                }
4658                WARN_ON(!unhash_delegation_locked(dp));
4659                list_add(&dp->dl_recall_lru, &reaplist);
4660        }
4661        spin_unlock(&state_lock);
4662        while (!list_empty(&reaplist)) {
4663                dp = list_first_entry(&reaplist, struct nfs4_delegation,
4664                                        dl_recall_lru);
4665                list_del_init(&dp->dl_recall_lru);
4666                revoke_delegation(dp);
4667        }
4668
4669        spin_lock(&nn->client_lock);
4670        while (!list_empty(&nn->close_lru)) {
4671                oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4672                                        oo_close_lru);
4673                if (time_after((unsigned long)oo->oo_time,
4674                               (unsigned long)cutoff)) {
4675                        t = oo->oo_time - cutoff;
4676                        new_timeo = min(new_timeo, t);
4677                        break;
4678                }
4679                list_del_init(&oo->oo_close_lru);
4680                stp = oo->oo_last_closed_stid;
4681                oo->oo_last_closed_stid = NULL;
4682                spin_unlock(&nn->client_lock);
4683                nfs4_put_stid(&stp->st_stid);
4684                spin_lock(&nn->client_lock);
4685        }
4686        spin_unlock(&nn->client_lock);
4687
4688        /*
4689         * It's possible for a client to try and acquire an already held lock
4690         * that is being held for a long time, and then lose interest in it.
4691         * So, we clean out any un-revisited request after a lease period
4692         * under the assumption that the client is no longer interested.
4693         *
4694         * RFC5661, sec. 9.6 states that the client must not rely on getting
4695         * notifications and must continue to poll for locks, even when the
4696         * server supports them. Thus this shouldn't lead to clients blocking
4697         * indefinitely once the lock does become free.
4698         */
4699        BUG_ON(!list_empty(&reaplist));
4700        spin_lock(&nn->blocked_locks_lock);
4701        while (!list_empty(&nn->blocked_locks_lru)) {
4702                nbl = list_first_entry(&nn->blocked_locks_lru,
4703                                        struct nfsd4_blocked_lock, nbl_lru);
4704                if (time_after((unsigned long)nbl->nbl_time,
4705                               (unsigned long)cutoff)) {
4706                        t = nbl->nbl_time - cutoff;
4707                        new_timeo = min(new_timeo, t);
4708                        break;
4709                }
4710                list_move(&nbl->nbl_lru, &reaplist);
4711                list_del_init(&nbl->nbl_list);
4712        }
4713        spin_unlock(&nn->blocked_locks_lock);
4714
4715        while (!list_empty(&reaplist)) {
4716                nbl = list_first_entry(&reaplist,
4717                                        struct nfsd4_blocked_lock, nbl_lru);
4718                list_del_init(&nbl->nbl_lru);
4719                posix_unblock_lock(&nbl->nbl_lock);
4720                free_blocked_lock(nbl);
4721        }
4722
4723        new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4724        return new_timeo;
4725}
4726
4727static struct workqueue_struct *laundry_wq;
4728static void laundromat_main(struct work_struct *);
4729
4730static void
4731laundromat_main(struct work_struct *laundry)
4732{
4733        time_t t;
4734        struct delayed_work *dwork = to_delayed_work(laundry);
4735        struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4736                                           laundromat_work);
4737
4738        t = nfs4_laundromat(nn);
4739        dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4740        queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4741}
4742
4743static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4744{
4745        if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4746                return nfserr_bad_stateid;
4747        return nfs_ok;
4748}
4749
4750static inline int
4751access_permit_read(struct nfs4_ol_stateid *stp)
4752{
4753        return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4754                test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4755                test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4756}
4757
4758static inline int
4759access_permit_write(struct nfs4_ol_stateid *stp)
4760{
4761        return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4762                test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4763}
4764
4765static
4766__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4767{
4768        __be32 status = nfserr_openmode;
4769
4770        /* For lock stateid's, we test the parent open, not the lock: */
4771        if (stp->st_openstp)
4772                stp = stp->st_openstp;
4773        if ((flags & WR_STATE) && !access_permit_write(stp))
4774                goto out;
4775        if ((flags & RD_STATE) && !access_permit_read(stp))
4776                goto out;
4777        status = nfs_ok;
4778out:
4779        return status;
4780}
4781
4782static inline __be32
4783check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4784{
4785        if (ONE_STATEID(stateid) && (flags & RD_STATE))
4786                return nfs_ok;
4787        else if (opens_in_grace(net)) {
4788                /* Answer in remaining cases depends on existence of
4789                 * conflicting state; so we must wait out the grace period. */
4790                return nfserr_grace;
4791        } else if (flags & WR_STATE)
4792                return nfs4_share_conflict(current_fh,
4793                                NFS4_SHARE_DENY_WRITE);
4794        else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4795                return nfs4_share_conflict(current_fh,
4796                                NFS4_SHARE_DENY_READ);
4797}
4798
4799/*
4800 * Allow READ/WRITE during grace period on recovered state only for files
4801 * that are not able to provide mandatory locking.
4802 */
4803static inline int
4804grace_disallows_io(struct net *net, struct inode *inode)
4805{
4806        return opens_in_grace(net) && mandatory_lock(inode);
4807}
4808
4809/* Returns true iff a is later than b: */
4810static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4811{
4812        return (s32)(a->si_generation - b->si_generation) > 0;
4813}
4814
4815static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4816{
4817        /*
4818         * When sessions are used the stateid generation number is ignored
4819         * when it is zero.
4820         */
4821        if (has_session && in->si_generation == 0)
4822                return nfs_ok;
4823
4824        if (in->si_generation == ref->si_generation)
4825                return nfs_ok;
4826
4827        /* If the client sends us a stateid from the future, it's buggy: */
4828        if (stateid_generation_after(in, ref))
4829                return nfserr_bad_stateid;
4830        /*
4831         * However, we could see a stateid from the past, even from a
4832         * non-buggy client.  For example, if the client sends a lock
4833         * while some IO is outstanding, the lock may bump si_generation
4834         * while the IO is still in flight.  The client could avoid that
4835         * situation by waiting for responses on all the IO requests,
4836         * but better performance may result in retrying IO that
4837         * receives an old_stateid error if requests are rarely
4838         * reordered in flight:
4839         */
4840        return nfserr_old_stateid;
4841}
4842
4843static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4844{
4845        if (ols->st_stateowner->so_is_open_owner &&
4846            !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4847                return nfserr_bad_stateid;
4848        return nfs_ok;
4849}
4850
4851static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4852{
4853        struct nfs4_stid *s;
4854        __be32 status = nfserr_bad_stateid;
4855
4856        if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4857                return status;
4858        /* Client debugging aid. */
4859        if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4860                char addr_str[INET6_ADDRSTRLEN];
4861                rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4862                                 sizeof(addr_str));
4863                pr_warn_ratelimited("NFSD: client %s testing state ID "
4864                                        "with incorrect client ID\n", addr_str);
4865                return status;
4866        }
4867        spin_lock(&cl->cl_lock);
4868        s = find_stateid_locked(cl, stateid);
4869        if (!s)
4870                goto out_unlock;
4871        status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4872        if (status)
4873                goto out_unlock;
4874        switch (s->sc_type) {
4875        case NFS4_DELEG_STID:
4876                status = nfs_ok;
4877                break;
4878        case NFS4_REVOKED_DELEG_STID:
4879                status = nfserr_deleg_revoked;
4880                break;
4881        case NFS4_OPEN_STID:
4882        case NFS4_LOCK_STID:
4883                status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4884                break;
4885        default:
4886                printk("unknown stateid type %x\n", s->sc_type);
4887                /* Fallthrough */
4888        case NFS4_CLOSED_STID:
4889        case NFS4_CLOSED_DELEG_STID:
4890                status = nfserr_bad_stateid;
4891        }
4892out_unlock:
4893        spin_unlock(&cl->cl_lock);
4894        return status;
4895}
4896
4897__be32
4898nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4899                     stateid_t *stateid, unsigned char typemask,
4900                     struct nfs4_stid **s, struct nfsd_net *nn)
4901{
4902        __be32 status;
4903        bool return_revoked = false;
4904
4905        /*
4906         *  only return revoked delegations if explicitly asked.
4907         *  otherwise we report revoked or bad_stateid status.
4908         */
4909        if (typemask & NFS4_REVOKED_DELEG_STID)
4910                return_revoked = true;
4911        else if (typemask & NFS4_DELEG_STID)
4912                typemask |= NFS4_REVOKED_DELEG_STID;
4913
4914        if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4915                return nfserr_bad_stateid;
4916        status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4917        if (status == nfserr_stale_clientid) {
4918                if (cstate->session)
4919                        return nfserr_bad_stateid;
4920                return nfserr_stale_stateid;
4921        }
4922        if (status)
4923                return status;
4924        *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4925        if (!*s)
4926                return nfserr_bad_stateid;
4927        if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
4928                nfs4_put_stid(*s);
4929                if (cstate->minorversion)
4930                        return nfserr_deleg_revoked;
4931                return nfserr_bad_stateid;
4932        }
4933        return nfs_ok;
4934}
4935
4936static struct file *
4937nfs4_find_file(struct nfs4_stid *s, int flags)
4938{
4939        if (!s)
4940                return NULL;
4941
4942        switch (s->sc_type) {
4943        case NFS4_DELEG_STID:
4944                if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4945                        return NULL;
4946                return get_file(s->sc_file->fi_deleg_file);
4947        case NFS4_OPEN_STID:
4948        case NFS4_LOCK_STID:
4949                if (flags & RD_STATE)
4950                        return find_readable_file(s->sc_file);
4951                else
4952                        return find_writeable_file(s->sc_file);
4953                break;
4954        }
4955
4956        return NULL;
4957}
4958
4959static __be32
4960nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4961{
4962        __be32 status;
4963
4964        status = nfsd4_check_openowner_confirmed(ols);
4965        if (status)
4966                return status;
4967        return nfs4_check_openmode(ols, flags);
4968}
4969
4970static __be32
4971nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4972                struct file **filpp, bool *tmp_file, int flags)
4973{
4974        int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4975        struct file *file;
4976        __be32 status;
4977
4978        file = nfs4_find_file(s, flags);
4979        if (file) {
4980                status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4981                                acc | NFSD_MAY_OWNER_OVERRIDE);
4982                if (status) {
4983                        fput(file);
4984                        return status;
4985                }
4986
4987                *filpp = file;
4988        } else {
4989                status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4990                if (status)
4991                        return status;
4992
4993                if (tmp_file)
4994                        *tmp_file = true;
4995        }
4996
4997        return 0;
4998}
4999
5000/*
5001 * Checks for stateid operations
5002 */
5003__be32
5004nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5005                struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5006                stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
5007{
5008        struct inode *ino = fhp->fh_dentry->d_inode;
5009        struct net *net = SVC_NET(rqstp);
5010        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5011        struct nfs4_stid *s = NULL;
5012        __be32 status;
5013
5014        if (filpp)
5015                *filpp = NULL;
5016        if (tmp_file)
5017                *tmp_file = false;
5018
5019        if (grace_disallows_io(net, ino))
5020                return nfserr_grace;
5021
5022        if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5023                status = check_special_stateids(net, fhp, stateid, flags);
5024                goto done;
5025        }
5026
5027        status = nfsd4_lookup_stateid(cstate, stateid,
5028                                NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5029                                &s, nn);
5030        if (status)
5031                return status;
5032        status = check_stateid_generation(stateid, &s->sc_stateid,
5033                        nfsd4_has_session(cstate));
5034        if (status)
5035                goto out;
5036
5037        switch (s->sc_type) {
5038        case NFS4_DELEG_STID:
5039                status = nfs4_check_delegmode(delegstateid(s), flags);
5040                break;
5041        case NFS4_OPEN_STID:
5042        case NFS4_LOCK_STID:
5043                status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
5044                break;
5045        default:
5046                status = nfserr_bad_stateid;
5047                break;
5048        }
5049        if (status)
5050                goto out;
5051        status = nfs4_check_fh(fhp, s);
5052
5053done:
5054        if (!status && filpp)
5055                status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
5056out:
5057        if (s)
5058                nfs4_put_stid(s);
5059        return status;
5060}
5061
5062/*
5063 * Test if the stateid is valid
5064 */
5065__be32
5066nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5067                   struct nfsd4_test_stateid *test_stateid)
5068{
5069        struct nfsd4_test_stateid_id *stateid;
5070        struct nfs4_client *cl = cstate->session->se_client;
5071
5072        list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5073                stateid->ts_id_status =
5074                        nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5075
5076        return nfs_ok;
5077}
5078
5079static __be32
5080nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5081{
5082        struct nfs4_ol_stateid *stp = openlockstateid(s);
5083        __be32 ret;
5084
5085        mutex_lock(&stp->st_mutex);
5086
5087        ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5088        if (ret)
5089                goto out;
5090
5091        ret = nfserr_locks_held;
5092        if (check_for_locks(stp->st_stid.sc_file,
5093                            lockowner(stp->st_stateowner)))
5094                goto out;
5095
5096        release_lock_stateid(stp);
5097        ret = nfs_ok;
5098
5099out:
5100        mutex_unlock(&stp->st_mutex);
5101        nfs4_put_stid(s);
5102        return ret;
5103}
5104
5105__be32
5106nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5107                   struct nfsd4_free_stateid *free_stateid)
5108{
5109        stateid_t *stateid = &free_stateid->fr_stateid;
5110        struct nfs4_stid *s;
5111        struct nfs4_delegation *dp;
5112        struct nfs4_client *cl = cstate->session->se_client;
5113        __be32 ret = nfserr_bad_stateid;
5114
5115        spin_lock(&cl->cl_lock);
5116        s = find_stateid_locked(cl, stateid);
5117        if (!s)
5118                goto out_unlock;
5119        switch (s->sc_type) {
5120        case NFS4_DELEG_STID:
5121                ret = nfserr_locks_held;
5122                break;
5123        case NFS4_OPEN_STID:
5124                ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5125                if (ret)
5126                        break;
5127                ret = nfserr_locks_held;
5128                break;
5129        case NFS4_LOCK_STID:
5130                atomic_inc(&s->sc_count);
5131                spin_unlock(&cl->cl_lock);
5132                ret = nfsd4_free_lock_stateid(stateid, s);
5133                goto out;
5134        case NFS4_REVOKED_DELEG_STID:
5135                dp = delegstateid(s);
5136                list_del_init(&dp->dl_recall_lru);
5137                spin_unlock(&cl->cl_lock);
5138                nfs4_put_stid(s);
5139                ret = nfs_ok;
5140                goto out;
5141        /* Default falls through and returns nfserr_bad_stateid */
5142        }
5143out_unlock:
5144        spin_unlock(&cl->cl_lock);
5145out:
5146        return ret;
5147}
5148
5149static inline int
5150setlkflg (int type)
5151{
5152        return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5153                RD_STATE : WR_STATE;
5154}
5155
5156static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5157{
5158        struct svc_fh *current_fh = &cstate->current_fh;
5159        struct nfs4_stateowner *sop = stp->st_stateowner;
5160        __be32 status;
5161
5162        status = nfsd4_check_seqid(cstate, sop, seqid);
5163        if (status)
5164                return status;
5165        if (stp->st_stid.sc_type == NFS4_CLOSED_STID
5166                || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
5167                /*
5168                 * "Closed" stateid's exist *only* to return
5169                 * nfserr_replay_me from the previous step, and
5170                 * revoked delegations are kept only for free_stateid.
5171                 */
5172                return nfserr_bad_stateid;
5173        mutex_lock(&stp->st_mutex);
5174        status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5175        if (status == nfs_ok)
5176                status = nfs4_check_fh(current_fh, &stp->st_stid);
5177        if (status != nfs_ok)
5178                mutex_unlock(&stp->st_mutex);
5179        return status;
5180}
5181
5182/* 
5183 * Checks for sequence id mutating operations. 
5184 */
5185static __be32
5186nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5187                         stateid_t *stateid, char typemask,
5188                         struct nfs4_ol_stateid **stpp,
5189                         struct nfsd_net *nn)
5190{
5191        __be32 status;
5192        struct nfs4_stid *s;
5193        struct nfs4_ol_stateid *stp = NULL;
5194
5195        dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5196                seqid, STATEID_VAL(stateid));
5197
5198        *stpp = NULL;
5199        status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5200        if (status)
5201                return status;
5202        stp = openlockstateid(s);
5203        nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5204
5205        status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5206        if (!status)
5207                *stpp = stp;
5208        else
5209                nfs4_put_stid(&stp->st_stid);
5210        return status;
5211}
5212
5213static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5214                                                 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5215{
5216        __be32 status;
5217        struct nfs4_openowner *oo;
5218        struct nfs4_ol_stateid *stp;
5219
5220        status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5221                                                NFS4_OPEN_STID, &stp, nn);
5222        if (status)
5223                return status;
5224        oo = openowner(stp->st_stateowner);
5225        if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5226                mutex_unlock(&stp->st_mutex);
5227                nfs4_put_stid(&stp->st_stid);
5228                return nfserr_bad_stateid;
5229        }
5230        *stpp = stp;
5231        return nfs_ok;
5232}
5233
5234__be32
5235nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5236                   struct nfsd4_open_confirm *oc)
5237{
5238        __be32 status;
5239        struct nfs4_openowner *oo;
5240        struct nfs4_ol_stateid *stp;
5241        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5242
5243        dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5244                        cstate->current_fh.fh_dentry);
5245
5246        status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5247        if (status)
5248                return status;
5249
5250        status = nfs4_preprocess_seqid_op(cstate,
5251                                        oc->oc_seqid, &oc->oc_req_stateid,
5252                                        NFS4_OPEN_STID, &stp, nn);
5253        if (status)
5254                goto out;
5255        oo = openowner(stp->st_stateowner);
5256        status = nfserr_bad_stateid;
5257        if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5258                mutex_unlock(&stp->st_mutex);
5259                goto put_stateid;
5260        }
5261        oo->oo_flags |= NFS4_OO_CONFIRMED;
5262        nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5263        mutex_unlock(&stp->st_mutex);
5264        dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5265                __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5266
5267        nfsd4_client_record_create(oo->oo_owner.so_client);
5268        status = nfs_ok;
5269put_stateid:
5270        nfs4_put_stid(&stp->st_stid);
5271out:
5272        nfsd4_bump_seqid(cstate, status);
5273        return status;
5274}
5275
5276static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5277{
5278        if (!test_access(access, stp))
5279                return;
5280        nfs4_file_put_access(stp->st_stid.sc_file, access);
5281        clear_access(access, stp);
5282}
5283
5284static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5285{
5286        switch (to_access) {
5287        case NFS4_SHARE_ACCESS_READ:
5288                nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5289                nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5290                break;
5291        case NFS4_SHARE_ACCESS_WRITE:
5292                nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5293                nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5294                break;
5295        case NFS4_SHARE_ACCESS_BOTH:
5296                break;
5297        default:
5298                WARN_ON_ONCE(1);
5299        }
5300}
5301
5302__be32
5303nfsd4_open_downgrade(struct svc_rqst *rqstp,
5304                     struct nfsd4_compound_state *cstate,
5305                     struct nfsd4_open_downgrade *od)
5306{
5307        __be32 status;
5308        struct nfs4_ol_stateid *stp;
5309        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5310
5311        dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 
5312                        cstate->current_fh.fh_dentry);
5313
5314        /* We don't yet support WANT bits: */
5315        if (od->od_deleg_want)
5316                dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5317                        od->od_deleg_want);
5318
5319        status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5320                                        &od->od_stateid, &stp, nn);
5321        if (status)
5322                goto out; 
5323        status = nfserr_inval;
5324        if (!test_access(od->od_share_access, stp)) {
5325                dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5326                        stp->st_access_bmap, od->od_share_access);
5327                goto put_stateid;
5328        }
5329        if (!test_deny(od->od_share_deny, stp)) {
5330                dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5331                        stp->st_deny_bmap, od->od_share_deny);
5332                goto put_stateid;
5333        }
5334        nfs4_stateid_downgrade(stp, od->od_share_access);
5335        reset_union_bmap_deny(od->od_share_deny, stp);
5336        nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5337        status = nfs_ok;
5338put_stateid:
5339        mutex_unlock(&stp->st_mutex);
5340        nfs4_put_stid(&stp->st_stid);
5341out:
5342        nfsd4_bump_seqid(cstate, status);
5343        return status;
5344}
5345
5346static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5347{
5348        struct nfs4_client *clp = s->st_stid.sc_client;
5349        bool unhashed;
5350        LIST_HEAD(reaplist);
5351
5352        s->st_stid.sc_type = NFS4_CLOSED_STID;
5353        spin_lock(&clp->cl_lock);
5354        unhashed = unhash_open_stateid(s, &reaplist);
5355
5356        if (clp->cl_minorversion) {
5357                if (unhashed)
5358                        put_ol_stateid_locked(s, &reaplist);
5359                spin_unlock(&clp->cl_lock);
5360                free_ol_stateid_reaplist(&reaplist);
5361        } else {
5362                spin_unlock(&clp->cl_lock);
5363                free_ol_stateid_reaplist(&reaplist);
5364                if (unhashed)
5365                        move_to_close_lru(s, clp->net);
5366        }
5367}
5368
5369/*
5370 * nfs4_unlock_state() called after encode
5371 */
5372__be32
5373nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5374            struct nfsd4_close *close)
5375{
5376        __be32 status;
5377        struct nfs4_ol_stateid *stp;
5378        struct net *net = SVC_NET(rqstp);
5379        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5380
5381        dprintk("NFSD: nfsd4_close on file %pd\n", 
5382                        cstate->current_fh.fh_dentry);
5383
5384        status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5385                                        &close->cl_stateid,
5386                                        NFS4_OPEN_STID|NFS4_CLOSED_STID,
5387                                        &stp, nn);
5388        nfsd4_bump_seqid(cstate, status);
5389        if (status)
5390                goto out; 
5391        nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5392        mutex_unlock(&stp->st_mutex);
5393
5394        nfsd4_close_open_stateid(stp);
5395
5396        /* put reference from nfs4_preprocess_seqid_op */
5397        nfs4_put_stid(&stp->st_stid);
5398out:
5399        return status;
5400}
5401
5402__be32
5403nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5404                  struct nfsd4_delegreturn *dr)
5405{
5406        struct nfs4_delegation *dp;
5407        stateid_t *stateid = &dr->dr_stateid;
5408        struct nfs4_stid *s;
5409        __be32 status;
5410        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5411
5412        if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5413                return status;
5414
5415        status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5416        if (status)
5417                goto out;
5418        dp = delegstateid(s);
5419        status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5420        if (status)
5421                goto put_stateid;
5422
5423        destroy_delegation(dp);
5424put_stateid:
5425        nfs4_put_stid(&dp->dl_stid);
5426out:
5427        return status;
5428}
5429
5430static inline u64
5431end_offset(u64 start, u64 len)
5432{
5433        u64 end;
5434
5435        end = start + len;
5436        return end >= start ? end: NFS4_MAX_UINT64;
5437}
5438
5439/* last octet in a range */
5440static inline u64
5441last_byte_offset(u64 start, u64 len)
5442{
5443        u64 end;
5444
5445        WARN_ON_ONCE(!len);
5446        end = start + len;
5447        return end > start ? end - 1: NFS4_MAX_UINT64;
5448}
5449
5450/*
5451 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5452 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5453 * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
5454 * locking, this prevents us from being completely protocol-compliant.  The
5455 * real solution to this problem is to start using unsigned file offsets in
5456 * the VFS, but this is a very deep change!
5457 */
5458static inline void
5459nfs4_transform_lock_offset(struct file_lock *lock)
5460{
5461        if (lock->fl_start < 0)
5462                lock->fl_start = OFFSET_MAX;
5463        if (lock->fl_end < 0)
5464                lock->fl_end = OFFSET_MAX;
5465}
5466
5467static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
5468{
5469        struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
5470        dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
5471}
5472
5473static void nfsd4_fl_put_owner(struct file_lock *fl)
5474{
5475        struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5476
5477        if (lo) {
5478                nfs4_put_stateowner(&lo->lo_owner);
5479                fl->fl_owner = NULL;
5480        }
5481}
5482
5483static void
5484nfsd4_lm_notify(struct file_lock *fl)
5485{
5486        struct nfs4_lockowner           *lo = (struct nfs4_lockowner *)fl->fl_owner;
5487        struct net                      *net = lo->lo_owner.so_client->net;
5488        struct nfsd_net                 *nn = net_generic(net, nfsd_net_id);
5489        struct nfsd4_blocked_lock       *nbl = container_of(fl,
5490                                                struct nfsd4_blocked_lock, nbl_lock);
5491        bool queue = false;
5492
5493        /* An empty list means that something else is going to be using it */
5494        spin_lock(&nn->blocked_locks_lock);
5495        if (!list_empty(&nbl->nbl_list)) {
5496                list_del_init(&nbl->nbl_list);
5497                list_del_init(&nbl->nbl_lru);
5498                queue = true;
5499        }
5500        spin_unlock(&nn->blocked_locks_lock);
5501
5502        if (queue)
5503                nfsd4_run_cb(&nbl->nbl_cb);
5504}
5505
5506static const struct lock_manager_operations_extend nfsd_posix_mng_ops  = {
5507        .kabi_lmops = {
5508                .lm_notify = nfsd4_lm_notify,
5509        },
5510        .lm_get_owner = nfsd4_fl_get_owner,
5511        .lm_put_owner = nfsd4_fl_put_owner,
5512};
5513
5514static inline void
5515nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5516{
5517        struct nfs4_lockowner *lo;
5518
5519        if (fl->fl_lmops == &nfsd_posix_mng_ops.kabi_lmops) {
5520                lo = (struct nfs4_lockowner *) fl->fl_owner;
5521                deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5522                                        lo->lo_owner.so_owner.len, GFP_KERNEL);
5523                if (!deny->ld_owner.data)
5524                        /* We just don't care that much */
5525                        goto nevermind;
5526                deny->ld_owner.len = lo->lo_owner.so_owner.len;
5527                deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5528        } else {
5529nevermind:
5530                deny->ld_owner.len = 0;
5531                deny->ld_owner.data = NULL;
5532                deny->ld_clientid.cl_boot = 0;
5533                deny->ld_clientid.cl_id = 0;
5534        }
5535        deny->ld_start = fl->fl_start;
5536        deny->ld_length = NFS4_MAX_UINT64;
5537        if (fl->fl_end != NFS4_MAX_UINT64)
5538                deny->ld_length = fl->fl_end - fl->fl_start + 1;        
5539        deny->ld_type = NFS4_READ_LT;
5540        if (fl->fl_type != F_RDLCK)
5541                deny->ld_type = NFS4_WRITE_LT;
5542}
5543
5544static struct nfs4_lockowner *
5545find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5546{
5547        unsigned int strhashval = ownerstr_hashval(owner);
5548        struct nfs4_stateowner *so;
5549
5550        lockdep_assert_held(&clp->cl_lock);
5551
5552        list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5553                            so_strhash) {
5554                if (so->so_is_open_owner)
5555                        continue;
5556                if (same_owner_str(so, owner))
5557                        return lockowner(nfs4_get_stateowner(so));
5558        }
5559        return NULL;
5560}
5561
5562static struct nfs4_lockowner *
5563find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5564{
5565        struct nfs4_lockowner *lo;
5566
5567        spin_lock(&clp->cl_lock);
5568        lo = find_lockowner_str_locked(clp, owner);
5569        spin_unlock(&clp->cl_lock);
5570        return lo;
5571}
5572
5573static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5574{
5575        unhash_lockowner_locked(lockowner(sop));
5576}
5577
5578static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5579{
5580        struct nfs4_lockowner *lo = lockowner(sop);
5581
5582        kmem_cache_free(lockowner_slab, lo);
5583}
5584
5585static const struct nfs4_stateowner_operations lockowner_ops = {
5586        .so_unhash =    nfs4_unhash_lockowner,
5587        .so_free =      nfs4_free_lockowner,
5588};
5589
5590/*
5591 * Alloc a lock owner structure.
5592 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 
5593 * occurred. 
5594 *
5595 * strhashval = ownerstr_hashval
5596 */
5597static struct nfs4_lockowner *
5598alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5599                           struct nfs4_ol_stateid *open_stp,
5600                           struct nfsd4_lock *lock)
5601{
5602        struct nfs4_lockowner *lo, *ret;
5603
5604        lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5605        if (!lo)
5606                return NULL;
5607        INIT_LIST_HEAD(&lo->lo_blocked);
5608        INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5609        lo->lo_owner.so_is_open_owner = 0;
5610        lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5611        lo->lo_owner.so_ops = &lockowner_ops;
5612        spin_lock(&clp->cl_lock);
5613        ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5614        if (ret == NULL) {
5615                list_add(&lo->lo_owner.so_strhash,
5616                         &clp->cl_ownerstr_hashtbl[strhashval]);
5617                ret = lo;
5618        } else
5619                nfs4_free_stateowner(&lo->lo_owner);
5620
5621        spin_unlock(&clp->cl_lock);
5622        return ret;
5623}
5624
5625static void
5626init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5627                  struct nfs4_file *fp, struct inode *inode,
5628                  struct nfs4_ol_stateid *open_stp)
5629{
5630        struct nfs4_client *clp = lo->lo_owner.so_client;
5631
5632        lockdep_assert_held(&clp->cl_lock);
5633
5634        atomic_inc(&stp->st_stid.sc_count);
5635        stp->st_stid.sc_type = NFS4_LOCK_STID;
5636        stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5637        get_nfs4_file(fp);
5638        stp->st_stid.sc_file = fp;
5639        stp->st_stid.sc_free = nfs4_free_lock_stateid;
5640        stp->st_access_bmap = 0;
5641        stp->st_deny_bmap = open_stp->st_deny_bmap;
5642        stp->st_openstp = open_stp;
5643        mutex_init(&stp->st_mutex);
5644        list_add(&stp->st_locks, &open_stp->st_locks);
5645        list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5646        spin_lock(&fp->fi_lock);
5647        list_add(&stp->st_perfile, &fp->fi_stateids);
5648        spin_unlock(&fp->fi_lock);
5649}
5650
5651static struct nfs4_ol_stateid *
5652find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5653{
5654        struct nfs4_ol_stateid *lst;
5655        struct nfs4_client *clp = lo->lo_owner.so_client;
5656
5657        lockdep_assert_held(&clp->cl_lock);
5658
5659        list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5660                if (lst->st_stid.sc_file == fp) {
5661                        atomic_inc(&lst->st_stid.sc_count);
5662                        return lst;
5663                }
5664        }
5665        return NULL;
5666}
5667
5668static struct nfs4_ol_stateid *
5669find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5670                            struct inode *inode, struct nfs4_ol_stateid *ost,
5671                            bool *new)
5672{
5673        struct nfs4_stid *ns = NULL;
5674        struct nfs4_ol_stateid *lst;
5675        struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5676        struct nfs4_client *clp = oo->oo_owner.so_client;
5677
5678        spin_lock(&clp->cl_lock);
5679        lst = find_lock_stateid(lo, fi);
5680        if (lst == NULL) {
5681                spin_unlock(&clp->cl_lock);
5682                ns = nfs4_alloc_stid(clp, stateid_slab);
5683                if (ns == NULL)
5684                        return NULL;
5685
5686                spin_lock(&clp->cl_lock);
5687                lst = find_lock_stateid(lo, fi);
5688                if (likely(!lst)) {
5689                        lst = openlockstateid(ns);
5690                        init_lock_stateid(lst, lo, fi, inode, ost);
5691                        ns = NULL;
5692                        *new = true;
5693                }
5694        }
5695        spin_unlock(&clp->cl_lock);
5696        if (ns)
5697                nfs4_put_stid(ns);
5698        return lst;
5699}
5700
5701static int
5702check_lock_length(u64 offset, u64 length)
5703{
5704        return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5705                (length > ~offset)));
5706}
5707
5708static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5709{
5710        struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5711
5712        lockdep_assert_held(&fp->fi_lock);
5713
5714        if (test_access(access, lock_stp))
5715                return;
5716        __nfs4_file_get_access(fp, access);
5717        set_access(access, lock_stp);
5718}
5719
5720static __be32
5721lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5722                            struct nfs4_ol_stateid *ost,
5723                            struct nfsd4_lock *lock,
5724                            struct nfs4_ol_stateid **plst, bool *new)
5725{
5726        __be32 status;
5727        struct nfs4_file *fi = ost->st_stid.sc_file;
5728        struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5729        struct nfs4_client *cl = oo->oo_owner.so_client;
5730        struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5731        struct nfs4_lockowner *lo;
5732        struct nfs4_ol_stateid *lst;
5733        unsigned int strhashval;
5734        bool hashed;
5735
5736        lo = find_lockowner_str(cl, &lock->lk_new_owner);
5737        if (!lo) {
5738                strhashval = ownerstr_hashval(&lock->lk_new_owner);
5739                lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5740                if (lo == NULL)
5741                        return nfserr_jukebox;
5742        } else {
5743                /* with an existing lockowner, seqids must be the same */
5744                status = nfserr_bad_seqid;
5745                if (!cstate->minorversion &&
5746                    lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5747                        goto out;
5748        }
5749
5750retry:
5751        lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5752        if (lst == NULL) {
5753                status = nfserr_jukebox;
5754                goto out;
5755        }
5756
5757        mutex_lock(&lst->st_mutex);
5758
5759        /* See if it's still hashed to avoid race with FREE_STATEID */
5760        spin_lock(&cl->cl_lock);
5761        hashed = !list_empty(&lst->st_perfile);
5762        spin_unlock(&cl->cl_lock);
5763
5764        if (!hashed) {
5765                mutex_unlock(&lst->st_mutex);
5766                nfs4_put_stid(&lst->st_stid);
5767                goto retry;
5768        }
5769        status = nfs_ok;
5770        *plst = lst;
5771out:
5772        nfs4_put_stateowner(&lo->lo_owner);
5773        return status;
5774}
5775
5776/*
5777 *  LOCK operation 
5778 */
5779__be32
5780nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5781           struct nfsd4_lock *lock)
5782{
5783        struct nfs4_openowner *open_sop = NULL;
5784        struct nfs4_lockowner *lock_sop = NULL;
5785        struct nfs4_ol_stateid *lock_stp = NULL;
5786        struct nfs4_ol_stateid *open_stp = NULL;
5787        struct nfs4_file *fp;
5788        struct file *filp = NULL;
5789        struct nfsd4_blocked_lock *nbl = NULL;
5790        struct file_lock *file_lock = NULL;
5791        struct file_lock *conflock = NULL;
5792        __be32 status = 0;
5793        int lkflg;
5794        int err;
5795        bool new = false;
5796        unsigned char fl_type;
5797        unsigned int fl_flags = FL_POSIX;
5798        struct net *net = SVC_NET(rqstp);
5799        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5800
5801        dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5802                (long long) lock->lk_offset,
5803                (long long) lock->lk_length);
5804
5805        if (check_lock_length(lock->lk_offset, lock->lk_length))
5806                 return nfserr_inval;
5807
5808        if ((status = fh_verify(rqstp, &cstate->current_fh,
5809                                S_IFREG, NFSD_MAY_LOCK))) {
5810                dprintk("NFSD: nfsd4_lock: permission denied!\n");
5811                return status;
5812        }
5813
5814        if (lock->lk_is_new) {
5815                if (nfsd4_has_session(cstate))
5816                        /* See rfc 5661 18.10.3: given clientid is ignored: */
5817                        memcpy(&lock->lk_new_clientid,
5818                                &cstate->session->se_client->cl_clientid,
5819                                sizeof(clientid_t));
5820
5821                status = nfserr_stale_clientid;
5822                if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5823                        goto out;
5824
5825                /* validate and update open stateid and open seqid */
5826                status = nfs4_preprocess_confirmed_seqid_op(cstate,
5827                                        lock->lk_new_open_seqid,
5828                                        &lock->lk_new_open_stateid,
5829                                        &open_stp, nn);
5830                if (status)
5831                        goto out;
5832                mutex_unlock(&open_stp->st_mutex);
5833                open_sop = openowner(open_stp->st_stateowner);
5834                status = nfserr_bad_stateid;
5835                if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5836                                                &lock->lk_new_clientid))
5837                        goto out;
5838                status = lookup_or_create_lock_state(cstate, open_stp, lock,
5839                                                        &lock_stp, &new);
5840        } else {
5841                status = nfs4_preprocess_seqid_op(cstate,
5842                                       lock->lk_old_lock_seqid,
5843                                       &lock->lk_old_lock_stateid,
5844                                       NFS4_LOCK_STID, &lock_stp, nn);
5845        }
5846        if (status)
5847                goto out;
5848        lock_sop = lockowner(lock_stp->st_stateowner);
5849
5850        lkflg = setlkflg(lock->lk_type);
5851        status = nfs4_check_openmode(lock_stp, lkflg);
5852        if (status)
5853                goto out;
5854
5855        status = nfserr_grace;
5856        if (locks_in_grace(net) && !lock->lk_reclaim)
5857                goto out;
5858        status = nfserr_no_grace;
5859        if (!locks_in_grace(net) && lock->lk_reclaim)
5860                goto out;
5861
5862        fp = lock_stp->st_stid.sc_file;
5863        switch (lock->lk_type) {
5864                case NFS4_READW_LT:
5865                        if (nfsd4_has_session(cstate))
5866                                fl_flags |= FL_SLEEP;
5867                        /* Fallthrough */
5868                case NFS4_READ_LT:
5869                        spin_lock(&fp->fi_lock);
5870                        filp = find_readable_file_locked(fp);
5871                        if (filp)
5872                                get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5873                        spin_unlock(&fp->fi_lock);
5874                        fl_type = F_RDLCK;
5875                        break;
5876                case NFS4_WRITEW_LT:
5877                        if (nfsd4_has_session(cstate))
5878                                fl_flags |= FL_SLEEP;
5879                        /* Fallthrough */
5880                case NFS4_WRITE_LT:
5881                        spin_lock(&fp->fi_lock);
5882                        filp = find_writeable_file_locked(fp);
5883                        if (filp)
5884                                get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5885                        spin_unlock(&fp->fi_lock);
5886                        fl_type = F_WRLCK;
5887                        break;
5888                default:
5889                        status = nfserr_inval;
5890                goto out;
5891        }
5892
5893        if (!filp) {
5894                status = nfserr_openmode;
5895                goto out;
5896        }
5897
5898        nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
5899        if (!nbl) {
5900                dprintk("NFSD: %s: unable to allocate block!\n", __func__);
5901                status = nfserr_jukebox;
5902                goto out;
5903        }
5904
5905        file_lock = &nbl->nbl_lock;
5906        file_lock->fl_type = fl_type;
5907        file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5908        file_lock->fl_pid = current->tgid;
5909        file_lock->fl_file = filp;
5910        file_lock->fl_flags = fl_flags | FL_LM_OPS_EXTEND;
5911        file_lock->fl_lmops = &nfsd_posix_mng_ops.kabi_lmops;
5912        file_lock->fl_start = lock->lk_offset;
5913        file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5914        nfs4_transform_lock_offset(file_lock);
5915
5916        conflock = locks_alloc_lock();
5917        if (!conflock) {
5918                dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5919                status = nfserr_jukebox;
5920                goto out;
5921        }
5922
5923        if (fl_flags & FL_SLEEP) {
5924                nbl->nbl_time = jiffies;
5925                spin_lock(&nn->blocked_locks_lock);
5926                list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
5927                list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
5928                spin_unlock(&nn->blocked_locks_lock);
5929        }
5930
5931        err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5932        switch (err) {
5933        case 0: /* success! */
5934                nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
5935                status = 0;
5936                break;
5937        case FILE_LOCK_DEFERRED:
5938                nbl = NULL;
5939                /* Fallthrough */
5940        case -EAGAIN:           /* conflock holds conflicting lock */
5941                status = nfserr_denied;
5942                dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5943                nfs4_set_lock_denied(conflock, &lock->lk_denied);
5944                break;
5945        case -EDEADLK:
5946                status = nfserr_deadlock;
5947                break;
5948        default:
5949                dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5950                status = nfserrno(err);
5951                break;
5952        }
5953out:
5954        if (nbl) {
5955                /* dequeue it if we queued it before */
5956                if (fl_flags & FL_SLEEP) {
5957                        spin_lock(&nn->blocked_locks_lock);
5958                        list_del_init(&nbl->nbl_list);
5959                        list_del_init(&nbl->nbl_lru);
5960                        spin_unlock(&nn->blocked_locks_lock);
5961                }
5962                free_blocked_lock(nbl);
5963        }
5964        if (filp)
5965                fput(filp);
5966        if (lock_stp) {
5967                /* Bump seqid manually if the 4.0 replay owner is openowner */
5968                if (cstate->replay_owner &&
5969                    cstate->replay_owner != &lock_sop->lo_owner &&
5970                    seqid_mutating_err(ntohl(status)))
5971                        lock_sop->lo_owner.so_seqid++;
5972
5973                mutex_unlock(&lock_stp->st_mutex);
5974
5975                /*
5976                 * If this is a new, never-before-used stateid, and we are
5977                 * returning an error, then just go ahead and release it.
5978                 */
5979                if (status && new)
5980                        release_lock_stateid(lock_stp);
5981
5982                nfs4_put_stid(&lock_stp->st_stid);
5983        }
5984        if (open_stp)
5985                nfs4_put_stid(&open_stp->st_stid);
5986        nfsd4_bump_seqid(cstate, status);
5987        if (conflock)
5988                locks_free_lock(conflock);
5989        return status;
5990}
5991
5992/*
5993 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5994 * so we do a temporary open here just to get an open file to pass to
5995 * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
5996 * inode operation.)
5997 */
5998static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5999{
6000        struct file *file;
6001        __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6002        if (!err) {
6003                err = nfserrno(vfs_test_lock(file, lock));
6004                fput(file);
6005        }
6006        return err;
6007}
6008
6009/*
6010 * LOCKT operation
6011 */
6012__be32
6013nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6014            struct nfsd4_lockt *lockt)
6015{
6016        struct file_lock *file_lock = NULL;
6017        struct nfs4_lockowner *lo = NULL;
6018        __be32 status;
6019        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6020
6021        if (locks_in_grace(SVC_NET(rqstp)))
6022                return nfserr_grace;
6023
6024        if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6025                 return nfserr_inval;
6026
6027        if (!nfsd4_has_session(cstate)) {
6028                status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
6029                if (status)
6030                        goto out;
6031        }
6032
6033        if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6034                goto out;
6035
6036        file_lock = locks_alloc_lock();
6037        if (!file_lock) {
6038                dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6039                status = nfserr_jukebox;
6040                goto out;
6041        }
6042
6043        switch (lockt->lt_type) {
6044                case NFS4_READ_LT:
6045                case NFS4_READW_LT:
6046                        file_lock->fl_type = F_RDLCK;
6047                break;
6048                case NFS4_WRITE_LT:
6049                case NFS4_WRITEW_LT:
6050                        file_lock->fl_type = F_WRLCK;
6051                break;
6052                default:
6053                        dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6054                        status = nfserr_inval;
6055                goto out;
6056        }
6057
6058        lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6059        if (lo)
6060                file_lock->fl_owner = (fl_owner_t)lo;
6061        file_lock->fl_pid = current->tgid;
6062        file_lock->fl_flags = FL_POSIX;
6063
6064        file_lock->fl_start = lockt->lt_offset;
6065        file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6066
6067        nfs4_transform_lock_offset(file_lock);
6068
6069        status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6070        if (status)
6071                goto out;
6072
6073        if (file_lock->fl_type != F_UNLCK) {
6074                status = nfserr_denied;
6075                nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6076        }
6077out:
6078        if (lo)
6079                nfs4_put_stateowner(&lo->lo_owner);
6080        if (file_lock)
6081                locks_free_lock(file_lock);
6082        return status;
6083}
6084
6085__be32
6086nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6087            struct nfsd4_locku *locku)
6088{
6089        struct nfs4_ol_stateid *stp;
6090        struct file *filp = NULL;
6091        struct file_lock *file_lock = NULL;
6092        __be32 status;
6093        int err;
6094        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6095
6096        dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6097                (long long) locku->lu_offset,
6098                (long long) locku->lu_length);
6099
6100        if (check_lock_length(locku->lu_offset, locku->lu_length))
6101                 return nfserr_inval;
6102
6103        status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6104                                        &locku->lu_stateid, NFS4_LOCK_STID,
6105                                        &stp, nn);
6106        if (status)
6107                goto out;
6108        filp = find_any_file(stp->st_stid.sc_file);
6109        if (!filp) {
6110                status = nfserr_lock_range;
6111                goto put_stateid;
6112        }
6113        file_lock = locks_alloc_lock();
6114        if (!file_lock) {
6115                dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6116                status = nfserr_jukebox;
6117                goto fput;
6118        }
6119
6120        file_lock->fl_type = F_UNLCK;
6121        file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6122        file_lock->fl_pid = current->tgid;
6123        file_lock->fl_file = filp;
6124        file_lock->fl_flags = FL_POSIX | FL_LM_OPS_EXTEND;
6125        file_lock->fl_lmops = &nfsd_posix_mng_ops.kabi_lmops;
6126        file_lock->fl_start = locku->lu_offset;
6127
6128        file_lock->fl_end = last_byte_offset(locku->lu_offset,
6129                                                locku->lu_length);
6130        nfs4_transform_lock_offset(file_lock);
6131
6132        err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
6133        if (err) {
6134                dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6135                goto out_nfserr;
6136        }
6137        nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6138fput:
6139        fput(filp);
6140put_stateid:
6141        mutex_unlock(&stp->st_mutex);
6142        nfs4_put_stid(&stp->st_stid);
6143out:
6144        nfsd4_bump_seqid(cstate, status);
6145        if (file_lock)
6146                locks_free_lock(file_lock);
6147        return status;
6148
6149out_nfserr:
6150        status = nfserrno(err);
6151        goto fput;
6152}
6153
6154/*
6155 * returns
6156 *      true:  locks held by lockowner
6157 *      false: no locks held by lockowner
6158 */
6159static bool
6160check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6161{
6162        struct file_lock **flpp;
6163        int status = false;
6164        struct file *filp = find_any_file(fp);
6165        struct inode *inode;
6166
6167        if (!filp) {
6168                /* Any valid lock stateid should have some sort of access */
6169                WARN_ON_ONCE(1);
6170                return status;
6171        }
6172
6173        inode = locks_inode(filp);
6174
6175        spin_lock(&inode->i_lock);
6176        for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
6177                if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
6178                        status = true;
6179                        break;
6180                }
6181        }
6182        spin_unlock(&inode->i_lock);
6183        fput(filp);
6184        return status;
6185}
6186
6187__be32
6188nfsd4_release_lockowner(struct svc_rqst *rqstp,
6189                        struct nfsd4_compound_state *cstate,
6190                        struct nfsd4_release_lockowner *rlockowner)
6191{
6192        clientid_t *clid = &rlockowner->rl_clientid;
6193        struct nfs4_stateowner *sop;
6194        struct nfs4_lockowner *lo = NULL;
6195        struct nfs4_ol_stateid *stp;
6196        struct xdr_netobj *owner = &rlockowner->rl_owner;
6197        unsigned int hashval = ownerstr_hashval(owner);
6198        __be32 status;
6199        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6200        struct nfs4_client *clp;
6201        LIST_HEAD (reaplist);
6202
6203        dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6204                clid->cl_boot, clid->cl_id);
6205
6206        status = lookup_clientid(clid, cstate, nn);
6207        if (status)
6208                return status;
6209
6210        clp = cstate->clp;
6211        /* Find the matching lock stateowner */
6212        spin_lock(&clp->cl_lock);
6213        list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6214                            so_strhash) {
6215
6216                if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6217                        continue;
6218
6219                /* see if there are still any locks associated with it */
6220                lo = lockowner(sop);
6221                list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6222                        if (check_for_locks(stp->st_stid.sc_file, lo)) {
6223                                status = nfserr_locks_held;
6224                                spin_unlock(&clp->cl_lock);
6225                                return status;
6226                        }
6227                }
6228
6229                nfs4_get_stateowner(sop);
6230                break;
6231        }
6232        if (!lo) {
6233                spin_unlock(&clp->cl_lock);
6234                return status;
6235        }
6236
6237        unhash_lockowner_locked(lo);
6238        while (!list_empty(&lo->lo_owner.so_stateids)) {
6239                stp = list_first_entry(&lo->lo_owner.so_stateids,
6240                                       struct nfs4_ol_stateid,
6241                                       st_perstateowner);
6242                WARN_ON(!unhash_lock_stateid(stp));
6243                put_ol_stateid_locked(stp, &reaplist);
6244        }
6245        spin_unlock(&clp->cl_lock);
6246        free_ol_stateid_reaplist(&reaplist);
6247        remove_blocked_locks(lo);
6248        nfs4_put_stateowner(&lo->lo_owner);
6249
6250        return status;
6251}
6252
6253static inline struct nfs4_client_reclaim *
6254alloc_reclaim(void)
6255{
6256        return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6257}
6258
6259bool
6260nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6261{
6262        struct nfs4_client_reclaim *crp;
6263
6264        crp = nfsd4_find_reclaim_client(name, nn);
6265        return (crp && crp->cr_clp);
6266}
6267
6268/*
6269 * failure => all reset bets are off, nfserr_no_grace...
6270 */
6271struct nfs4_client_reclaim *
6272nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6273{
6274        unsigned int strhashval;
6275        struct nfs4_client_reclaim *crp;
6276
6277        dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6278        crp = alloc_reclaim();
6279        if (crp) {
6280                strhashval = clientstr_hashval(name);
6281                INIT_LIST_HEAD(&crp->cr_strhash);
6282                list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6283                memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6284                crp->cr_clp = NULL;
6285                nn->reclaim_str_hashtbl_size++;
6286        }
6287        return crp;
6288}
6289
6290void
6291nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6292{
6293        list_del(&crp->cr_strhash);
6294        kfree(crp);
6295        nn->reclaim_str_hashtbl_size--;
6296}
6297
6298void
6299nfs4_release_reclaim(struct nfsd_net *nn)
6300{
6301        struct nfs4_client_reclaim *crp = NULL;
6302        int i;
6303
6304        for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6305                while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6306                        crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6307                                        struct nfs4_client_reclaim, cr_strhash);
6308                        nfs4_remove_reclaim_record(crp, nn);
6309                }
6310        }
6311        WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6312}
6313
6314/*
6315 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6316struct nfs4_client_reclaim *
6317nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6318{
6319        unsigned int strhashval;
6320        struct nfs4_client_reclaim *crp = NULL;
6321
6322        dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6323
6324        strhashval = clientstr_hashval(recdir);
6325        list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6326                if (same_name(crp->cr_recdir, recdir)) {
6327                        return crp;
6328                }
6329        }
6330        return NULL;
6331}
6332
6333/*
6334* Called from OPEN. Look for clientid in reclaim list.
6335*/
6336__be32
6337nfs4_check_open_reclaim(clientid_t *clid,
6338                struct nfsd4_compound_state *cstate,
6339                struct nfsd_net *nn)
6340{
6341        __be32 status;
6342
6343        /* find clientid in conf_id_hashtbl */
6344        status = lookup_clientid(clid, cstate, nn);
6345        if (status)
6346                return nfserr_reclaim_bad;
6347
6348        if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6349                return nfserr_no_grace;
6350
6351        if (nfsd4_client_record_check(cstate->clp))
6352                return nfserr_reclaim_bad;
6353
6354        return nfs_ok;
6355}
6356
6357#ifdef CONFIG_NFSD_FAULT_INJECTION
6358static inline void
6359put_client(struct nfs4_client *clp)
6360{
6361        atomic_dec(&clp->cl_refcount);
6362}
6363
6364static struct nfs4_client *
6365nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6366{
6367        struct nfs4_client *clp;
6368        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6369                                          nfsd_net_id);
6370
6371        if (!nfsd_netns_ready(nn))
6372                return NULL;
6373
6374        list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6375                if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6376                        return clp;
6377        }
6378        return NULL;
6379}
6380
6381u64
6382nfsd_inject_print_clients(void)
6383{
6384        struct nfs4_client *clp;
6385        u64 count = 0;
6386        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6387                                          nfsd_net_id);
6388        char buf[INET6_ADDRSTRLEN];
6389
6390        if (!nfsd_netns_ready(nn))
6391                return 0;
6392
6393        spin_lock(&nn->client_lock);
6394        list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6395                rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6396                pr_info("NFS Client: %s\n", buf);
6397                ++count;
6398        }
6399        spin_unlock(&nn->client_lock);
6400
6401        return count;
6402}
6403
6404u64
6405nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6406{
6407        u64 count = 0;
6408        struct nfs4_client *clp;
6409        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6410                                          nfsd_net_id);
6411
6412        if (!nfsd_netns_ready(nn))
6413                return count;
6414
6415        spin_lock(&nn->client_lock);
6416        clp = nfsd_find_client(addr, addr_size);
6417        if (clp) {
6418                if (mark_client_expired_locked(clp) == nfs_ok)
6419                        ++count;
6420                else
6421                        clp = NULL;
6422        }
6423        spin_unlock(&nn->client_lock);
6424
6425        if (clp)
6426                expire_client(clp);
6427
6428        return count;
6429}
6430
6431u64
6432nfsd_inject_forget_clients(u64 max)
6433{
6434        u64 count = 0;
6435        struct nfs4_client *clp, *next;
6436        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6437                                                nfsd_net_id);
6438        LIST_HEAD(reaplist);
6439
6440        if (!nfsd_netns_ready(nn))
6441                return count;
6442
6443        spin_lock(&nn->client_lock);
6444        list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6445                if (mark_client_expired_locked(clp) == nfs_ok) {
6446                        list_add(&clp->cl_lru, &reaplist);
6447                        if (max != 0 && ++count >= max)
6448                                break;
6449                }
6450        }
6451        spin_unlock(&nn->client_lock);
6452
6453        list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6454                expire_client(clp);
6455
6456        return count;
6457}
6458
6459static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6460                             const char *type)
6461{
6462        char buf[INET6_ADDRSTRLEN];
6463        rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6464        printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6465}
6466
6467static void
6468nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6469                             struct list_head *collect)
6470{
6471        struct nfs4_client *clp = lst->st_stid.sc_client;
6472        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6473                                          nfsd_net_id);
6474
6475        if (!collect)
6476                return;
6477
6478        lockdep_assert_held(&nn->client_lock);
6479        atomic_inc(&clp->cl_refcount);
6480        list_add(&lst->st_locks, collect);
6481}
6482
6483static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6484                                    struct list_head *collect,
6485                                    bool (*func)(struct nfs4_ol_stateid *))
6486{
6487        struct nfs4_openowner *oop;
6488        struct nfs4_ol_stateid *stp, *st_next;
6489        struct nfs4_ol_stateid *lst, *lst_next;
6490        u64 count = 0;
6491
6492        spin_lock(&clp->cl_lock);
6493        list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6494                list_for_each_entry_safe(stp, st_next,
6495                                &oop->oo_owner.so_stateids, st_perstateowner) {
6496                        list_for_each_entry_safe(lst, lst_next,
6497                                        &stp->st_locks, st_locks) {
6498                                if (func) {
6499                                        if (func(lst))
6500                                                nfsd_inject_add_lock_to_list(lst,
6501                                                                        collect);
6502                                }
6503                                ++count;
6504                                /*
6505                                 * Despite the fact that these functions deal
6506                                 * with 64-bit integers for "count", we must
6507                                 * ensure that it doesn't blow up the
6508                                 * clp->cl_refcount. Throw a warning if we
6509                                 * start to approach INT_MAX here.
6510                                 */
6511                                WARN_ON_ONCE(count == (INT_MAX / 2));
6512                                if (count == max)
6513                                        goto out;
6514                        }
6515                }
6516        }
6517out:
6518        spin_unlock(&clp->cl_lock);
6519
6520        return count;
6521}
6522
6523static u64
6524nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6525                          u64 max)
6526{
6527        return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6528}
6529
6530static u64
6531nfsd_print_client_locks(struct nfs4_client *clp)
6532{
6533        u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6534        nfsd_print_count(clp, count, "locked files");
6535        return count;
6536}
6537
6538u64
6539nfsd_inject_print_locks(void)
6540{
6541        struct nfs4_client *clp;
6542        u64 count = 0;
6543        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6544                                                nfsd_net_id);
6545
6546        if (!nfsd_netns_ready(nn))
6547                return 0;
6548
6549        spin_lock(&nn->client_lock);
6550        list_for_each_entry(clp, &nn->client_lru, cl_lru)
6551                count += nfsd_print_client_locks(clp);
6552        spin_unlock(&nn->client_lock);
6553
6554        return count;
6555}
6556
6557static void
6558nfsd_reap_locks(struct list_head *reaplist)
6559{
6560        struct nfs4_client *clp;
6561        struct nfs4_ol_stateid *stp, *next;
6562
6563        list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6564                list_del_init(&stp->st_locks);
6565                clp = stp->st_stid.sc_client;
6566                nfs4_put_stid(&stp->st_stid);
6567                put_client(clp);
6568        }
6569}
6570
6571u64
6572nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6573{
6574        unsigned int count = 0;
6575        struct nfs4_client *clp;
6576        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6577                                                nfsd_net_id);
6578        LIST_HEAD(reaplist);
6579
6580        if (!nfsd_netns_ready(nn))
6581                return count;
6582
6583        spin_lock(&nn->client_lock);
6584        clp = nfsd_find_client(addr, addr_size);
6585        if (clp)
6586                count = nfsd_collect_client_locks(clp, &reaplist, 0);
6587        spin_unlock(&nn->client_lock);
6588        nfsd_reap_locks(&reaplist);
6589        return count;
6590}
6591
6592u64
6593nfsd_inject_forget_locks(u64 max)
6594{
6595        u64 count = 0;
6596        struct nfs4_client *clp;
6597        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6598                                                nfsd_net_id);
6599        LIST_HEAD(reaplist);
6600
6601        if (!nfsd_netns_ready(nn))
6602                return count;
6603
6604        spin_lock(&nn->client_lock);
6605        list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6606                count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6607                if (max != 0 && count >= max)
6608                        break;
6609        }
6610        spin_unlock(&nn->client_lock);
6611        nfsd_reap_locks(&reaplist);
6612        return count;
6613}
6614
6615static u64
6616nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6617                              struct list_head *collect,
6618                              void (*func)(struct nfs4_openowner *))
6619{
6620        struct nfs4_openowner *oop, *next;
6621        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6622                                                nfsd_net_id);
6623        u64 count = 0;
6624
6625        lockdep_assert_held(&nn->client_lock);
6626
6627        spin_lock(&clp->cl_lock);
6628        list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6629                if (func) {
6630                        func(oop);
6631                        if (collect) {
6632                                atomic_inc(&clp->cl_refcount);
6633                                list_add(&oop->oo_perclient, collect);
6634                        }
6635                }
6636                ++count;
6637                /*
6638                 * Despite the fact that these functions deal with
6639                 * 64-bit integers for "count", we must ensure that
6640                 * it doesn't blow up the clp->cl_refcount. Throw a
6641                 * warning if we start to approach INT_MAX here.
6642                 */
6643                WARN_ON_ONCE(count == (INT_MAX / 2));
6644                if (count == max)
6645                        break;
6646        }
6647        spin_unlock(&clp->cl_lock);
6648
6649        return count;
6650}
6651
6652static u64
6653nfsd_print_client_openowners(struct nfs4_client *clp)
6654{
6655        u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6656
6657        nfsd_print_count(clp, count, "openowners");
6658        return count;
6659}
6660
6661static u64
6662nfsd_collect_client_openowners(struct nfs4_client *clp,
6663                               struct list_head *collect, u64 max)
6664{
6665        return nfsd_foreach_client_openowner(clp, max, collect,
6666                                                unhash_openowner_locked);
6667}
6668
6669u64
6670nfsd_inject_print_openowners(void)
6671{
6672        struct nfs4_client *clp;
6673        u64 count = 0;
6674        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6675                                                nfsd_net_id);
6676
6677        if (!nfsd_netns_ready(nn))
6678                return 0;
6679
6680        spin_lock(&nn->client_lock);
6681        list_for_each_entry(clp, &nn->client_lru, cl_lru)
6682                count += nfsd_print_client_openowners(clp);
6683        spin_unlock(&nn->client_lock);
6684
6685        return count;
6686}
6687
6688static void
6689nfsd_reap_openowners(struct list_head *reaplist)
6690{
6691        struct nfs4_client *clp;
6692        struct nfs4_openowner *oop, *next;
6693
6694        list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6695                list_del_init(&oop->oo_perclient);
6696                clp = oop->oo_owner.so_client;
6697                release_openowner(oop);
6698                put_client(clp);
6699        }
6700}
6701
6702u64
6703nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6704                                     size_t addr_size)
6705{
6706        unsigned int count = 0;
6707        struct nfs4_client *clp;
6708        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6709                                                nfsd_net_id);
6710        LIST_HEAD(reaplist);
6711
6712        if (!nfsd_netns_ready(nn))
6713                return count;
6714
6715        spin_lock(&nn->client_lock);
6716        clp = nfsd_find_client(addr, addr_size);
6717        if (clp)
6718                count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6719        spin_unlock(&nn->client_lock);
6720        nfsd_reap_openowners(&reaplist);
6721        return count;
6722}
6723
6724u64
6725nfsd_inject_forget_openowners(u64 max)
6726{
6727        u64 count = 0;
6728        struct nfs4_client *clp;
6729        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6730                                                nfsd_net_id);
6731        LIST_HEAD(reaplist);
6732
6733        if (!nfsd_netns_ready(nn))
6734                return count;
6735
6736        spin_lock(&nn->client_lock);
6737        list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6738                count += nfsd_collect_client_openowners(clp, &reaplist,
6739                                                        max - count);
6740                if (max != 0 && count >= max)
6741                        break;
6742        }
6743        spin_unlock(&nn->client_lock);
6744        nfsd_reap_openowners(&reaplist);
6745        return count;
6746}
6747
6748static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6749                                     struct list_head *victims)
6750{
6751        struct nfs4_delegation *dp, *next;
6752        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6753                                                nfsd_net_id);
6754        u64 count = 0;
6755
6756        lockdep_assert_held(&nn->client_lock);
6757
6758        spin_lock(&state_lock);
6759        list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6760                if (victims) {
6761                        /*
6762                         * It's not safe to mess with delegations that have a
6763                         * non-zero dl_time. They might have already been broken
6764                         * and could be processed by the laundromat outside of
6765                         * the state_lock. Just leave them be.
6766                         */
6767                        if (dp->dl_time != 0)
6768                                continue;
6769
6770                        atomic_inc(&clp->cl_refcount);
6771                        WARN_ON(!unhash_delegation_locked(dp));
6772                        list_add(&dp->dl_recall_lru, victims);
6773                }
6774                ++count;
6775                /*
6776                 * Despite the fact that these functions deal with
6777                 * 64-bit integers for "count", we must ensure that
6778                 * it doesn't blow up the clp->cl_refcount. Throw a
6779                 * warning if we start to approach INT_MAX here.
6780                 */
6781                WARN_ON_ONCE(count == (INT_MAX / 2));
6782                if (count == max)
6783                        break;
6784        }
6785        spin_unlock(&state_lock);
6786        return count;
6787}
6788
6789static u64
6790nfsd_print_client_delegations(struct nfs4_client *clp)
6791{
6792        u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6793
6794        nfsd_print_count(clp, count, "delegations");
6795        return count;
6796}
6797
6798u64
6799nfsd_inject_print_delegations(void)
6800{
6801        struct nfs4_client *clp;
6802        u64 count = 0;
6803        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6804                                                nfsd_net_id);
6805
6806        if (!nfsd_netns_ready(nn))
6807                return 0;
6808
6809        spin_lock(&nn->client_lock);
6810        list_for_each_entry(clp, &nn->client_lru, cl_lru)
6811                count += nfsd_print_client_delegations(clp);
6812        spin_unlock(&nn->client_lock);
6813
6814        return count;
6815}
6816
6817static void
6818nfsd_forget_delegations(struct list_head *reaplist)
6819{
6820        struct nfs4_client *clp;
6821        struct nfs4_delegation *dp, *next;
6822
6823        list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6824                list_del_init(&dp->dl_recall_lru);
6825                clp = dp->dl_stid.sc_client;
6826                revoke_delegation(dp);
6827                put_client(clp);
6828        }
6829}
6830
6831u64
6832nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6833                                      size_t addr_size)
6834{
6835        u64 count = 0;
6836        struct nfs4_client *clp;
6837        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6838                                                nfsd_net_id);
6839        LIST_HEAD(reaplist);
6840
6841        if (!nfsd_netns_ready(nn))
6842                return count;
6843
6844        spin_lock(&nn->client_lock);
6845        clp = nfsd_find_client(addr, addr_size);
6846        if (clp)
6847                count = nfsd_find_all_delegations(clp, 0, &reaplist);
6848        spin_unlock(&nn->client_lock);
6849
6850        nfsd_forget_delegations(&reaplist);
6851        return count;
6852}
6853
6854u64
6855nfsd_inject_forget_delegations(u64 max)
6856{
6857        u64 count = 0;
6858        struct nfs4_client *clp;
6859        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6860                                                nfsd_net_id);
6861        LIST_HEAD(reaplist);
6862
6863        if (!nfsd_netns_ready(nn))
6864                return count;
6865
6866        spin_lock(&nn->client_lock);
6867        list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6868                count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6869                if (max != 0 && count >= max)
6870                        break;
6871        }
6872        spin_unlock(&nn->client_lock);
6873        nfsd_forget_delegations(&reaplist);
6874        return count;
6875}
6876
6877static void
6878nfsd_recall_delegations(struct list_head *reaplist)
6879{
6880        struct nfs4_client *clp;
6881        struct nfs4_delegation *dp, *next;
6882
6883        list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6884                list_del_init(&dp->dl_recall_lru);
6885                clp = dp->dl_stid.sc_client;
6886                /*
6887                 * We skipped all entries that had a zero dl_time before,
6888                 * so we can now reset the dl_time back to 0. If a delegation
6889                 * break comes in now, then it won't make any difference since
6890                 * we're recalling it either way.
6891                 */
6892                spin_lock(&state_lock);
6893                dp->dl_time = 0;
6894                spin_unlock(&state_lock);
6895                nfsd_break_one_deleg(dp);
6896                put_client(clp);
6897        }
6898}
6899
6900u64
6901nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6902                                      size_t addr_size)
6903{
6904        u64 count = 0;
6905        struct nfs4_client *clp;
6906        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6907                                                nfsd_net_id);
6908        LIST_HEAD(reaplist);
6909
6910        if (!nfsd_netns_ready(nn))
6911                return count;
6912
6913        spin_lock(&nn->client_lock);
6914        clp = nfsd_find_client(addr, addr_size);
6915        if (clp)
6916                count = nfsd_find_all_delegations(clp, 0, &reaplist);
6917        spin_unlock(&nn->client_lock);
6918
6919        nfsd_recall_delegations(&reaplist);
6920        return count;
6921}
6922
6923u64
6924nfsd_inject_recall_delegations(u64 max)
6925{
6926        u64 count = 0;
6927        struct nfs4_client *clp, *next;
6928        struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6929                                                nfsd_net_id);
6930        LIST_HEAD(reaplist);
6931
6932        if (!nfsd_netns_ready(nn))
6933                return count;
6934
6935        spin_lock(&nn->client_lock);
6936        list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6937                count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6938                if (max != 0 && ++count >= max)
6939                        break;
6940        }
6941        spin_unlock(&nn->client_lock);
6942        nfsd_recall_delegations(&reaplist);
6943        return count;
6944}
6945#endif /* CONFIG_NFSD_FAULT_INJECTION */
6946
6947/*
6948 * Since the lifetime of a delegation isn't limited to that of an open, a
6949 * client may quite reasonably hang on to a delegation as long as it has
6950 * the inode cached.  This becomes an obvious problem the first time a
6951 * client's inode cache approaches the size of the server's total memory.
6952 *
6953 * For now we avoid this problem by imposing a hard limit on the number
6954 * of delegations, which varies according to the server's memory size.
6955 */
6956static void
6957set_max_delegations(void)
6958{
6959        /*
6960         * Allow at most 4 delegations per megabyte of RAM.  Quick
6961         * estimates suggest that in the worst case (where every delegation
6962         * is for a different inode), a delegation could take about 1.5K,
6963         * giving a worst case usage of about 6% of memory.
6964         */
6965        max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6966}
6967
6968static int nfs4_state_create_net(struct net *net)
6969{
6970        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6971        int i;
6972
6973        nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6974                        CLIENT_HASH_SIZE, GFP_KERNEL);
6975        if (!nn->conf_id_hashtbl)
6976                goto err;
6977        nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6978                        CLIENT_HASH_SIZE, GFP_KERNEL);
6979        if (!nn->unconf_id_hashtbl)
6980                goto err_unconf_id;
6981        nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6982                        SESSION_HASH_SIZE, GFP_KERNEL);
6983        if (!nn->sessionid_hashtbl)
6984                goto err_sessionid;
6985
6986        for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6987                INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6988                INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6989        }
6990        for (i = 0; i < SESSION_HASH_SIZE; i++)
6991                INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6992        nn->conf_name_tree = RB_ROOT;
6993        nn->unconf_name_tree = RB_ROOT;
6994        INIT_LIST_HEAD(&nn->client_lru);
6995        INIT_LIST_HEAD(&nn->close_lru);
6996        INIT_LIST_HEAD(&nn->del_recall_lru);
6997        spin_lock_init(&nn->client_lock);
6998
6999        spin_lock_init(&nn->blocked_locks_lock);
7000        INIT_LIST_HEAD(&nn->blocked_locks_lru);
7001
7002        INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7003        get_net(net);
7004
7005        return 0;
7006
7007err_sessionid:
7008        kfree(nn->unconf_id_hashtbl);
7009err_unconf_id:
7010        kfree(nn->conf_id_hashtbl);
7011err:
7012        return -ENOMEM;
7013}
7014
7015static void
7016nfs4_state_destroy_net(struct net *net)
7017{
7018        int i;
7019        struct nfs4_client *clp = NULL;
7020        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7021
7022        for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7023                while (!list_empty(&nn->conf_id_hashtbl[i])) {
7024                        clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7025                        destroy_client(clp);
7026                }
7027        }
7028
7029        WARN_ON(!list_empty(&nn->blocked_locks_lru));
7030
7031        for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7032                while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7033                        clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7034                        destroy_client(clp);
7035                }
7036        }
7037
7038        kfree(nn->sessionid_hashtbl);
7039        kfree(nn->unconf_id_hashtbl);
7040        kfree(nn->conf_id_hashtbl);
7041        put_net(net);
7042}
7043
7044int
7045nfs4_state_start_net(struct net *net)
7046{
7047        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7048        int ret;
7049
7050        ret = nfs4_state_create_net(net);
7051        if (ret)
7052                return ret;
7053        nn->boot_time = get_seconds();
7054        nn->grace_ended = false;
7055        nn->nfsd4_manager.block_opens = true;
7056        locks_start_grace(net, &nn->nfsd4_manager);
7057        nfsd4_client_tracking_init(net);
7058        printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
7059               nn->nfsd4_grace, net);
7060        queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7061        return 0;
7062}
7063
7064/* initialization to perform when the nfsd service is started: */
7065
7066int
7067nfs4_state_start(void)
7068{
7069        int ret;
7070
7071        ret = set_callback_cred();
7072        if (ret)
7073                return -ENOMEM;
7074        laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7075        if (laundry_wq == NULL) {
7076                ret = -ENOMEM;
7077                goto out_recovery;
7078        }
7079        ret = nfsd4_create_callback_queue();
7080        if (ret)
7081                goto out_free_laundry;
7082
7083        set_max_delegations();
7084
7085        return 0;
7086
7087out_free_laundry:
7088        destroy_workqueue(laundry_wq);
7089out_recovery:
7090        return ret;
7091}
7092
7093void
7094nfs4_state_shutdown_net(struct net *net)
7095{
7096        struct nfs4_delegation *dp = NULL;
7097        struct list_head *pos, *next, reaplist;
7098        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7099
7100        cancel_delayed_work_sync(&nn->laundromat_work);
7101        locks_end_grace(&nn->nfsd4_manager);
7102
7103        INIT_LIST_HEAD(&reaplist);
7104        spin_lock(&state_lock);
7105        list_for_each_safe(pos, next, &nn->del_recall_lru) {
7106                dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7107                WARN_ON(!unhash_delegation_locked(dp));
7108                list_add(&dp->dl_recall_lru, &reaplist);
7109        }
7110        spin_unlock(&state_lock);
7111        list_for_each_safe(pos, next, &reaplist) {
7112                dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7113                list_del_init(&dp->dl_recall_lru);
7114                put_clnt_odstate(dp->dl_clnt_odstate);
7115                nfs4_put_deleg_lease(dp->dl_stid.sc_file);
7116                nfs4_put_stid(&dp->dl_stid);
7117        }
7118
7119        nfsd4_client_tracking_exit(net);
7120        nfs4_state_destroy_net(net);
7121}
7122
7123void
7124nfs4_state_shutdown(void)
7125{
7126        destroy_workqueue(laundry_wq);
7127        nfsd4_destroy_callback_queue();
7128}
7129
7130static void
7131get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7132{
7133        if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7134                memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7135}
7136
7137static void
7138put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7139{
7140        if (cstate->minorversion) {
7141                memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7142                SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7143        }
7144}
7145
7146void
7147clear_current_stateid(struct nfsd4_compound_state *cstate)
7148{
7149        CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7150}
7151
7152/*
7153 * functions to set current state id
7154 */
7155void
7156nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7157{
7158        put_stateid(cstate, &odp->od_stateid);
7159}
7160
7161void
7162nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
7163{
7164        put_stateid(cstate, &open->op_stateid);
7165}
7166
7167void
7168nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7169{
7170        put_stateid(cstate, &close->cl_stateid);
7171}
7172
7173void
7174nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
7175{
7176        put_stateid(cstate, &lock->lk_resp_stateid);
7177}
7178
7179/*
7180 * functions to consume current state id
7181 */
7182
7183void
7184nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7185{
7186        get_stateid(cstate, &odp->od_stateid);
7187}
7188
7189void
7190nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7191{
7192        get_stateid(cstate, &drp->dr_stateid);
7193}
7194
7195void
7196nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7197{
7198        get_stateid(cstate, &fsp->fr_stateid);
7199}
7200
7201void
7202nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7203{
7204        get_stateid(cstate, &setattr->sa_stateid);
7205}
7206
7207void
7208nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7209{
7210        get_stateid(cstate, &close->cl_stateid);
7211}
7212
7213void
7214nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
7215{
7216        get_stateid(cstate, &locku->lu_stateid);
7217}
7218
7219void
7220nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7221{
7222        get_stateid(cstate, &read->rd_stateid);
7223}
7224
7225void
7226nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7227{
7228        get_stateid(cstate, &write->wr_stateid);
7229}
7230