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