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