nfsd: nfsd4_locku() must reference the lock stateid
[pandora-kernel.git] / fs / nfsd / nfs4state.c
index 049ef2c..4f19145 100644 (file)
@@ -71,6 +71,7 @@ static u64 current_sessionid = 1;
 
 /* forward declarations */
 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
+static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
 
 /* Locking: */
 
@@ -463,10 +464,9 @@ static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
                __nfs4_file_put_access(fp, O_RDONLY);
 }
 
-static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
-kmem_cache *slab)
+static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
+                                        struct kmem_cache *slab)
 {
-       struct idr *stateids = &cl->cl_stateids;
        struct nfs4_stid *stid;
        int new_id;
 
@@ -474,7 +474,11 @@ kmem_cache *slab)
        if (!stid)
                return NULL;
 
-       new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
+       idr_preload(GFP_KERNEL);
+       spin_lock(&cl->cl_lock);
+       new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
+       spin_unlock(&cl->cl_lock);
+       idr_preload_end();
        if (new_id < 0)
                goto out_free;
        stid->sc_client = cl;
@@ -498,9 +502,24 @@ out_free:
        return NULL;
 }
 
-static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
+static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
 {
-       return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
+       struct nfs4_stid *stid;
+       struct nfs4_ol_stateid *stp;
+
+       stid = nfs4_alloc_stid(clp, stateid_slab);
+       if (!stid)
+               return NULL;
+
+       stp = openlockstateid(stid);
+       stp->st_stid.sc_free = nfs4_free_ol_stateid;
+       return stp;
+}
+
+static void nfs4_free_deleg(struct nfs4_stid *stid)
+{
+       kmem_cache_free(deleg_slab, stid);
+       atomic_long_dec(&num_delegations);
 }
 
 /*
@@ -517,10 +536,11 @@ static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
  * low 3 bytes as hash-table indices.
  *
- * 'state_lock', which is always held when block_delegations() is called,
+ * 'blocked_delegations_lock', which is always taken in block_delegations(),
  * is used to manage concurrent access.  Testing does not need the lock
  * except when swapping the two filters.
  */
+static DEFINE_SPINLOCK(blocked_delegations_lock);
 static struct bloom_pair {
        int     entries, old_entries;
        time_t  swap_time;
@@ -536,7 +556,7 @@ static int delegation_blocked(struct knfsd_fh *fh)
        if (bd->entries == 0)
                return 0;
        if (seconds_since_boot() - bd->swap_time > 30) {
-               spin_lock(&state_lock);
+               spin_lock(&blocked_delegations_lock);
                if (seconds_since_boot() - bd->swap_time > 30) {
                        bd->entries -= bd->old_entries;
                        bd->old_entries = bd->entries;
@@ -545,7 +565,7 @@ static int delegation_blocked(struct knfsd_fh *fh)
                        bd->new = 1-bd->new;
                        bd->swap_time = seconds_since_boot();
                }
-               spin_unlock(&state_lock);
+               spin_unlock(&blocked_delegations_lock);
        }
        hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
        if (test_bit(hash&255, bd->set[0]) &&
@@ -566,16 +586,16 @@ static void block_delegations(struct knfsd_fh *fh)
        u32 hash;
        struct bloom_pair *bd = &blocked_delegations;
 
-       lockdep_assert_held(&state_lock);
-
        hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
 
+       spin_lock(&blocked_delegations_lock);
        __set_bit(hash&255, bd->set[bd->new]);
        __set_bit((hash>>8)&255, bd->set[bd->new]);
        __set_bit((hash>>16)&255, bd->set[bd->new]);
        if (bd->entries == 0)
                bd->swap_time = seconds_since_boot();
        bd->entries += 1;
+       spin_unlock(&blocked_delegations_lock);
 }
 
 static struct nfs4_delegation *
@@ -593,6 +613,8 @@ alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
        dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
        if (dp == NULL)
                goto out_dec;
+
+       dp->dl_stid.sc_free = nfs4_free_deleg;
        /*
         * delegation seqid's are never incremented.  The 4.1 special
         * meaning of seqid 0 isn't meaningful, really, but let's avoid
@@ -603,7 +625,6 @@ alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
        INIT_LIST_HEAD(&dp->dl_perclnt);
        INIT_LIST_HEAD(&dp->dl_recall_lru);
        dp->dl_type = NFS4_OPEN_DELEGATE_READ;
-       fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
        INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
        return dp;
 out_dec:
@@ -611,28 +632,21 @@ out_dec:
        return NULL;
 }
 
-static void remove_stid(struct nfs4_stid *s)
+void
+nfs4_put_stid(struct nfs4_stid *s)
 {
-       struct idr *stateids = &s->sc_client->cl_stateids;
-
-       idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
-}
+       struct nfs4_file *fp = s->sc_file;
+       struct nfs4_client *clp = s->sc_client;
 
-static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
-{
-       kmem_cache_free(slab, s);
-}
+       might_lock(&clp->cl_lock);
 
-void
-nfs4_put_delegation(struct nfs4_delegation *dp)
-{
-       if (atomic_dec_and_test(&dp->dl_stid.sc_count)) {
-               if (dp->dl_file)
-                       put_nfs4_file(dp->dl_file);
-               remove_stid(&dp->dl_stid);
-               nfs4_free_stid(deleg_slab, &dp->dl_stid);
-               atomic_long_dec(&num_delegations);
-       }
+       if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock))
+               return;
+       idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
+       spin_unlock(&clp->cl_lock);
+       s->sc_free(s);
+       if (fp)
+               put_nfs4_file(fp);
 }
 
 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
@@ -668,7 +682,7 @@ hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
 static void
 unhash_delegation_locked(struct nfs4_delegation *dp)
 {
-       struct nfs4_file *fp = dp->dl_file;
+       struct nfs4_file *fp = dp->dl_stid.sc_file;
 
        lockdep_assert_held(&state_lock);
 
@@ -689,7 +703,7 @@ static void destroy_delegation(struct nfs4_delegation *dp)
        spin_lock(&state_lock);
        unhash_delegation_locked(dp);
        spin_unlock(&state_lock);
-       nfs4_put_delegation(dp);
+       nfs4_put_stid(&dp->dl_stid);
 }
 
 static void revoke_delegation(struct nfs4_delegation *dp)
@@ -699,7 +713,7 @@ static void revoke_delegation(struct nfs4_delegation *dp)
        WARN_ON(!list_empty(&dp->dl_recall_lru));
 
        if (clp->cl_minorversion == 0)
-               nfs4_put_delegation(dp);
+               nfs4_put_stid(&dp->dl_stid);
        else {
                dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
                spin_lock(&clp->cl_lock);
@@ -855,7 +869,7 @@ reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
 
        /* Recalculate per-file deny mode if there was a change */
        if (change)
-               recalculate_deny_mode(stp->st_file);
+               recalculate_deny_mode(stp->st_stid.sc_file);
 }
 
 /* release all access and file references for a given stateid */
@@ -863,21 +877,23 @@ static void
 release_all_access(struct nfs4_ol_stateid *stp)
 {
        int i;
-       struct nfs4_file *fp = stp->st_file;
+       struct nfs4_file *fp = stp->st_stid.sc_file;
 
        if (fp && stp->st_deny_bmap != 0)
                recalculate_deny_mode(fp);
 
        for (i = 1; i < 4; i++) {
                if (test_access(i, stp))
-                       nfs4_file_put_access(stp->st_file, i);
+                       nfs4_file_put_access(stp->st_stid.sc_file, i);
                clear_access(i, stp);
        }
 }
 
 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
 {
-       struct nfs4_file *fp = stp->st_file;
+       struct nfs4_file *fp = stp->st_stid.sc_file;
+
+       lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
 
        spin_lock(&fp->fi_lock);
        list_del(&stp->st_perfile);
@@ -885,31 +901,36 @@ static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
        list_del(&stp->st_perstateowner);
 }
 
-static void close_generic_stateid(struct nfs4_ol_stateid *stp)
+static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
 {
+       struct nfs4_ol_stateid *stp = openlockstateid(stid);
+
        release_all_access(stp);
+       kmem_cache_free(stateid_slab, stid);
 }
 
-static void free_generic_stateid(struct nfs4_ol_stateid *stp)
+static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
 {
-       if (stp->st_file)
-               put_nfs4_file(stp->st_file);
-       remove_stid(&stp->st_stid);
-       nfs4_free_stid(stateid_slab, &stp->st_stid);
+       struct nfs4_ol_stateid *stp = openlockstateid(stid);
+       struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
+       struct file *file;
+
+       file = find_any_file(stp->st_stid.sc_file);
+       if (file)
+               filp_close(file, (fl_owner_t)lo);
+       nfs4_free_ol_stateid(stid);
 }
 
 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
 {
-       struct file *file;
+       struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
 
+       spin_lock(&oo->oo_owner.so_client->cl_lock);
        list_del(&stp->st_locks);
        unhash_generic_stateid(stp);
        unhash_stid(&stp->st_stid);
-       file = find_any_file(stp->st_file);
-       if (file)
-               filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
-       close_generic_stateid(stp);
-       free_generic_stateid(stp);
+       spin_unlock(&oo->oo_owner.so_client->cl_lock);
+       nfs4_put_stid(&stp->st_stid);
 }
 
 static void unhash_lockowner(struct nfs4_lockowner *lo)
@@ -952,27 +973,32 @@ static void release_lock_stateid(struct nfs4_ol_stateid *stp)
 }
 
 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
+       __releases(&open_stp->st_stateowner->so_client->cl_lock)
+       __acquires(&open_stp->st_stateowner->so_client->cl_lock)
 {
        struct nfs4_ol_stateid *stp;
 
        while (!list_empty(&open_stp->st_locks)) {
                stp = list_entry(open_stp->st_locks.next,
                                struct nfs4_ol_stateid, st_locks);
+               spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
                release_lock_stateid(stp);
+               spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
        }
 }
 
 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
 {
+       spin_lock(&stp->st_stateowner->so_client->cl_lock);
        unhash_generic_stateid(stp);
        release_open_stateid_locks(stp);
-       close_generic_stateid(stp);
+       spin_unlock(&stp->st_stateowner->so_client->cl_lock);
 }
 
 static void release_open_stateid(struct nfs4_ol_stateid *stp)
 {
        unhash_open_stateid(stp);
-       free_generic_stateid(stp);
+       nfs4_put_stid(&stp->st_stid);
 }
 
 static void unhash_openowner(struct nfs4_openowner *oo)
@@ -993,7 +1019,7 @@ static void release_last_closed_stateid(struct nfs4_openowner *oo)
        struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
 
        if (s) {
-               free_generic_stateid(s);
+               nfs4_put_stid(&s->st_stid);
                oo->oo_last_closed_stid = NULL;
        }
 }
@@ -1467,12 +1493,12 @@ destroy_client(struct nfs4_client *clp)
        while (!list_empty(&reaplist)) {
                dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
                list_del_init(&dp->dl_recall_lru);
-               nfs4_put_delegation(dp);
+               nfs4_put_stid(&dp->dl_stid);
        }
        while (!list_empty(&clp->cl_revoked)) {
                dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
                list_del_init(&dp->dl_recall_lru);
-               nfs4_put_delegation(dp);
+               nfs4_put_stid(&dp->dl_stid);
        }
        while (!list_empty(&clp->cl_openowners)) {
                oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
@@ -1651,7 +1677,8 @@ static void gen_confirm(struct nfs4_client *clp)
        memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
 }
 
-static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
+static struct nfs4_stid *
+find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
 {
        struct nfs4_stid *ret;
 
@@ -1661,16 +1688,17 @@ static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
        return ret;
 }
 
-static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
+static struct nfs4_stid *
+find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
 {
        struct nfs4_stid *s;
 
-       s = find_stateid(cl, t);
-       if (!s)
-               return NULL;
-       if (typemask & s->sc_type)
-               return s;
-       return NULL;
+       spin_lock(&cl->cl_lock);
+       s = find_stateid_locked(cl, t);
+       if (s != NULL && !(typemask & s->sc_type))
+               s = NULL;
+       spin_unlock(&cl->cl_lock);
+       return s;
 }
 
 static struct nfs4_client *create_client(struct xdr_netobj name,
@@ -2969,16 +2997,18 @@ static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp,
 
        stp->st_stid.sc_type = NFS4_OPEN_STID;
        INIT_LIST_HEAD(&stp->st_locks);
-       list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
        stp->st_stateowner = &oo->oo_owner;
        get_nfs4_file(fp);
-       stp->st_file = fp;
+       stp->st_stid.sc_file = fp;
        stp->st_access_bmap = 0;
        stp->st_deny_bmap = 0;
        stp->st_openstp = NULL;
+       spin_lock(&oo->oo_owner.so_client->cl_lock);
+       list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
        spin_lock(&fp->fi_lock);
        list_add(&stp->st_perfile, &fp->fi_stateids);
        spin_unlock(&fp->fi_lock);
+       spin_unlock(&oo->oo_owner.so_client->cl_lock);
 }
 
 static void
@@ -3093,19 +3123,19 @@ nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
 
 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
 {
-       struct nfs4_client *clp = dp->dl_stid.sc_client;
-       struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
+       struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
+                                         nfsd_net_id);
+
+       block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
 
        /*
         * We can't do this in nfsd_break_deleg_cb because it is
-        * already holding inode->i_lock
-        */
-       spin_lock(&state_lock);
-       block_delegations(&dp->dl_fh);
-       /*
+        * already holding inode->i_lock.
+        *
         * If the dl_time != 0, then we know that it has already been
         * queued for a lease break. Don't queue it again.
         */
+       spin_lock(&state_lock);
        if (dp->dl_time == 0) {
                dp->dl_time = get_seconds();
                list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
@@ -3267,7 +3297,7 @@ new_owner:
                return nfserr_jukebox;
        open->op_openowner = oo;
 alloc_stateid:
-       open->op_stp = nfs4_alloc_stateid(clp);
+       open->op_stp = nfs4_alloc_open_stateid(clp);
        if (!open->op_stp)
                return nfserr_jukebox;
        return nfs_ok;
@@ -3504,7 +3534,7 @@ static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
 
 static int nfs4_setlease(struct nfs4_delegation *dp)
 {
-       struct nfs4_file *fp = dp->dl_file;
+       struct nfs4_file *fp = dp->dl_stid.sc_file;
        struct file_lock *fl;
        struct file *filp;
        int status = 0;
@@ -3552,20 +3582,29 @@ out_fput:
        return status;
 }
 
-static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
+static struct nfs4_delegation *
+nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
+                   struct nfs4_file *fp)
 {
-       int status = 0;
+       int status;
+       struct nfs4_delegation *dp;
 
        if (fp->fi_had_conflict)
-               return -EAGAIN;
+               return ERR_PTR(-EAGAIN);
+
+       dp = alloc_init_deleg(clp, fh);
+       if (!dp)
+               return ERR_PTR(-ENOMEM);
+
        get_nfs4_file(fp);
        spin_lock(&state_lock);
        spin_lock(&fp->fi_lock);
-       dp->dl_file = fp;
+       dp->dl_stid.sc_file = fp;
        if (!fp->fi_lease) {
                spin_unlock(&fp->fi_lock);
                spin_unlock(&state_lock);
-               return nfs4_setlease(dp);
+               status = nfs4_setlease(dp);
+               goto out;
        }
        atomic_inc(&fp->fi_delegees);
        if (fp->fi_had_conflict) {
@@ -3573,10 +3612,16 @@ static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
                goto out_unlock;
        }
        hash_delegation_locked(dp, fp);
+       status = 0;
 out_unlock:
        spin_unlock(&fp->fi_lock);
        spin_unlock(&state_lock);
-       return status;
+out:
+       if (status) {
+               nfs4_put_stid(&dp->dl_stid);
+               return ERR_PTR(status);
+       }
+       return dp;
 }
 
 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
@@ -3607,11 +3652,12 @@ static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  * proper support for them.
  */
 static void
-nfs4_open_delegation(struct net *net, struct svc_fh *fh,
-                    struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
+nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
+                       struct nfs4_ol_stateid *stp)
 {
        struct nfs4_delegation *dp;
-       struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
+       struct nfs4_openowner *oo = openowner(stp->st_stateowner);
+       struct nfs4_client *clp = stp->st_stid.sc_client;
        int cb_up;
        int status = 0;
 
@@ -3630,7 +3676,7 @@ nfs4_open_delegation(struct net *net, struct svc_fh *fh,
                         * Let's not give out any delegations till everyone's
                         * had the chance to reclaim theirs....
                         */
-                       if (locks_in_grace(net))
+                       if (locks_in_grace(clp->net))
                                goto out_no_deleg;
                        if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
                                goto out_no_deleg;
@@ -3649,12 +3695,9 @@ nfs4_open_delegation(struct net *net, struct svc_fh *fh,
                default:
                        goto out_no_deleg;
        }
-       dp = alloc_init_deleg(oo->oo_owner.so_client, fh);
-       if (dp == NULL)
+       dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
+       if (IS_ERR(dp))
                goto out_no_deleg;
-       status = nfs4_set_delegation(dp, stp->st_file);
-       if (status)
-               goto out_free;
 
        memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
@@ -3662,8 +3705,6 @@ nfs4_open_delegation(struct net *net, struct svc_fh *fh,
                STATEID_VAL(&dp->dl_stid.sc_stateid));
        open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
        return;
-out_free:
-       nfs4_put_delegation(dp);
 out_no_deleg:
        open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
        if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
@@ -3762,7 +3803,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
        * Attempt to hand out a delegation. No error return, because the
        * OPEN succeeds even if we fail.
        */
-       nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
+       nfs4_open_delegation(current_fh, open, stp);
 nodeleg:
        status = nfs_ok;
 
@@ -3807,7 +3848,7 @@ void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
        if (open->op_file)
                nfsd4_free_file(open->op_file);
        if (open->op_stp)
-               free_generic_stateid(open->op_stp);
+               nfs4_put_stid(&open->op_stp->st_stid);
 }
 
 __be32
@@ -3944,7 +3985,7 @@ laundromat_main(struct work_struct *laundry)
 
 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
 {
-       if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
+       if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
                return nfserr_bad_stateid;
        return nfs_ok;
 }
@@ -4046,10 +4087,10 @@ static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
 {
        struct nfs4_stid *s;
        struct nfs4_ol_stateid *ols;
-       __be32 status;
+       __be32 status = nfserr_bad_stateid;
 
        if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
-               return nfserr_bad_stateid;
+               return status;
        /* Client debugging aid. */
        if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
                char addr_str[INET6_ADDRSTRLEN];
@@ -4057,34 +4098,42 @@ static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
                                 sizeof(addr_str));
                pr_warn_ratelimited("NFSD: client %s testing state ID "
                                        "with incorrect client ID\n", addr_str);
-               return nfserr_bad_stateid;
+               return status;
        }
-       s = find_stateid(cl, stateid);
+       spin_lock(&cl->cl_lock);
+       s = find_stateid_locked(cl, stateid);
        if (!s)
-               return nfserr_bad_stateid;
+               goto out_unlock;
        status = check_stateid_generation(stateid, &s->sc_stateid, 1);
        if (status)
-               return status;
+               goto out_unlock;
        switch (s->sc_type) {
        case NFS4_DELEG_STID:
-               return nfs_ok;
+               status = nfs_ok;
+               break;
        case NFS4_REVOKED_DELEG_STID:
-               return nfserr_deleg_revoked;
+               status = nfserr_deleg_revoked;
+               break;
        case NFS4_OPEN_STID:
        case NFS4_LOCK_STID:
                ols = openlockstateid(s);
                if (ols->st_stateowner->so_is_open_owner
                                && !(openowner(ols->st_stateowner)->oo_flags
                                                & NFS4_OO_CONFIRMED))
-                       return nfserr_bad_stateid;
-               return nfs_ok;
+                       status = nfserr_bad_stateid;
+               else
+                       status = nfs_ok;
+               break;
        default:
                printk("unknown stateid type %x\n", s->sc_type);
                /* Fallthrough */
        case NFS4_CLOSED_STID:
        case NFS4_CLOSED_DELEG_STID:
-               return nfserr_bad_stateid;
+               status = nfserr_bad_stateid;
        }
+out_unlock:
+       spin_unlock(&cl->cl_lock);
+       return status;
 }
 
 static __be32
@@ -4152,7 +4201,7 @@ nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
                if (status)
                        goto out;
                if (filpp) {
-                       file = dp->dl_file->fi_deleg_file;
+                       file = dp->dl_stid.sc_file->fi_deleg_file;
                        if (!file) {
                                WARN_ON_ONCE(1);
                                status = nfserr_serverfault;
@@ -4174,10 +4223,12 @@ nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
                if (status)
                        goto out;
                if (filpp) {
+                       struct nfs4_file *fp = stp->st_stid.sc_file;
+
                        if (flags & RD_STATE)
-                               file = find_readable_file(stp->st_file);
+                               file = find_readable_file(fp);
                        else
-                               file = find_writeable_file(stp->st_file);
+                               file = find_writeable_file(fp);
                }
                break;
        default:
@@ -4197,7 +4248,7 @@ nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
 {
        struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
 
-       if (check_for_locks(stp->st_file, lo))
+       if (check_for_locks(stp->st_stid.sc_file, lo))
                return nfserr_locks_held;
        release_lockowner_if_empty(lo);
        return nfs_ok;
@@ -4233,34 +4284,38 @@ nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        __be32 ret = nfserr_bad_stateid;
 
        nfs4_lock_state();
-       s = find_stateid(cl, stateid);
+       spin_lock(&cl->cl_lock);
+       s = find_stateid_locked(cl, stateid);
        if (!s)
-               goto out;
+               goto out_unlock;
        switch (s->sc_type) {
        case NFS4_DELEG_STID:
                ret = nfserr_locks_held;
-               goto out;
+               break;
        case NFS4_OPEN_STID:
-       case NFS4_LOCK_STID:
                ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
                if (ret)
-                       goto out;
-               if (s->sc_type == NFS4_LOCK_STID)
-                       ret = nfsd4_free_lock_stateid(openlockstateid(s));
-               else
-                       ret = nfserr_locks_held;
+                       break;
+               ret = nfserr_locks_held;
                break;
+       case NFS4_LOCK_STID:
+               ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
+               if (ret)
+                       break;
+               spin_unlock(&cl->cl_lock);
+               ret = nfsd4_free_lock_stateid(openlockstateid(s));
+               goto out;
        case NFS4_REVOKED_DELEG_STID:
                dp = delegstateid(s);
-               spin_lock(&cl->cl_lock);
                list_del_init(&dp->dl_recall_lru);
                spin_unlock(&cl->cl_lock);
-               nfs4_put_delegation(dp);
+               nfs4_put_stid(s);
                ret = nfs_ok;
-               break;
-       default:
-               ret = nfserr_bad_stateid;
+               goto out;
+       /* Default falls through and returns nfserr_bad_stateid */
        }
+out_unlock:
+       spin_unlock(&cl->cl_lock);
 out:
        nfs4_unlock_state();
        return ret;
@@ -4388,7 +4443,7 @@ static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 a
 {
        if (!test_access(access, stp))
                return;
-       nfs4_file_put_access(stp->st_file, access);
+       nfs4_file_put_access(stp->st_stid.sc_file, access);
        clear_access(access, stp);
 }
 
@@ -4466,19 +4521,22 @@ static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
        unhash_open_stateid(s);
 
        if (clp->cl_minorversion) {
-               free_generic_stateid(s);
                if (list_empty(&oo->oo_owner.so_stateids))
                        release_openowner(oo);
+               nfs4_put_stid(&s->st_stid);
        } else {
-               if (s->st_file) {
-                       put_nfs4_file(s->st_file);
-                       s->st_file = NULL;
-               }
-               oo->oo_last_closed_stid = s;
                /*
                 * In the 4.0 case we need to keep the owners around a
-                * little while to handle CLOSE replay.
+                * little while to handle CLOSE replay. We still do need
+                * to release any file access that is held by them
+                * before returning however.
                 */
+               release_all_access(s);
+               if (s->st_stid.sc_file) {
+                       put_nfs4_file(s->st_stid.sc_file);
+                       s->st_stid.sc_file = NULL;
+               }
+               oo->oo_last_closed_stid = s;
                if (list_empty(&oo->oo_owner.so_stateids))
                        move_to_close_lru(oo, clp->net);
        }
@@ -4662,44 +4720,80 @@ alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, str
        return lo;
 }
 
-static struct nfs4_ol_stateid *
-alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
-               struct inode *inode,
-               struct nfs4_ol_stateid *open_stp)
+static void
+init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
+                 struct nfs4_file *fp, struct inode *inode,
+                 struct nfs4_ol_stateid *open_stp)
 {
-       struct nfs4_ol_stateid *stp;
        struct nfs4_client *clp = lo->lo_owner.so_client;
 
-       stp = nfs4_alloc_stateid(clp);
-       if (stp == NULL)
-               return NULL;
+       lockdep_assert_held(&clp->cl_lock);
+
+       atomic_inc(&stp->st_stid.sc_count);
        stp->st_stid.sc_type = NFS4_LOCK_STID;
-       list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
        stp->st_stateowner = &lo->lo_owner;
        get_nfs4_file(fp);
-       stp->st_file = fp;
+       stp->st_stid.sc_file = fp;
+       stp->st_stid.sc_free = nfs4_free_lock_stateid;
        stp->st_access_bmap = 0;
        stp->st_deny_bmap = open_stp->st_deny_bmap;
        stp->st_openstp = open_stp;
        list_add(&stp->st_locks, &open_stp->st_locks);
+       list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
        spin_lock(&fp->fi_lock);
        list_add(&stp->st_perfile, &fp->fi_stateids);
        spin_unlock(&fp->fi_lock);
-       return stp;
 }
 
 static struct nfs4_ol_stateid *
 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
 {
        struct nfs4_ol_stateid *lst;
+       struct nfs4_client *clp = lo->lo_owner.so_client;
+
+       lockdep_assert_held(&clp->cl_lock);
 
        list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
-               if (lst->st_file == fp)
+               if (lst->st_stid.sc_file == fp) {
+                       atomic_inc(&lst->st_stid.sc_count);
                        return lst;
+               }
        }
        return NULL;
 }
 
+static struct nfs4_ol_stateid *
+find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
+                           struct inode *inode, struct nfs4_ol_stateid *ost,
+                           bool *new)
+{
+       struct nfs4_stid *ns = NULL;
+       struct nfs4_ol_stateid *lst;
+       struct nfs4_openowner *oo = openowner(ost->st_stateowner);
+       struct nfs4_client *clp = oo->oo_owner.so_client;
+
+       spin_lock(&clp->cl_lock);
+       lst = find_lock_stateid(lo, fi);
+       if (lst == NULL) {
+               spin_unlock(&clp->cl_lock);
+               ns = nfs4_alloc_stid(clp, stateid_slab);
+               if (ns == NULL)
+                       return NULL;
+
+               spin_lock(&clp->cl_lock);
+               lst = find_lock_stateid(lo, fi);
+               if (likely(!lst)) {
+                       lst = openlockstateid(ns);
+                       init_lock_stateid(lst, lo, fi, inode, ost);
+                       ns = NULL;
+                       *new = true;
+               }
+       }
+       spin_unlock(&clp->cl_lock);
+       if (ns)
+               nfs4_put_stid(ns);
+       return lst;
+}
 
 static int
 check_lock_length(u64 offset, u64 length)
@@ -4710,7 +4804,7 @@ check_lock_length(u64 offset, u64 length)
 
 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
 {
-       struct nfs4_file *fp = lock_stp->st_file;
+       struct nfs4_file *fp = lock_stp->st_stid.sc_file;
 
        lockdep_assert_held(&fp->fi_lock);
 
@@ -4720,9 +4814,13 @@ static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
        set_access(access, lock_stp);
 }
 
-static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
+static __be32
+lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
+                           struct nfs4_ol_stateid *ost,
+                           struct nfsd4_lock *lock,
+                           struct nfs4_ol_stateid **lst, bool *new)
 {
-       struct nfs4_file *fi = ost->st_file;
+       struct nfs4_file *fi = ost->st_stid.sc_file;
        struct nfs4_openowner *oo = openowner(ost->st_stateowner);
        struct nfs4_client *cl = oo->oo_owner.so_client;
        struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
@@ -4744,14 +4842,10 @@ static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, s
                        return nfserr_bad_seqid;
        }
 
-       *lst = find_lock_stateid(lo, fi);
+       *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
        if (*lst == NULL) {
-               *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
-               if (*lst == NULL) {
-                       release_lockowner_if_empty(lo);
-                       return nfserr_jukebox;
-               }
-               *new = true;
+               release_lockowner_if_empty(lo);
+               return nfserr_jukebox;
        }
        return nfs_ok;
 }
@@ -4765,7 +4859,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 {
        struct nfs4_openowner *open_sop = NULL;
        struct nfs4_lockowner *lock_sop = NULL;
-       struct nfs4_ol_stateid *lock_stp;
+       struct nfs4_ol_stateid *lock_stp = NULL;
        struct nfs4_file *fp;
        struct file *filp = NULL;
        struct file_lock *file_lock = NULL;
@@ -4819,11 +4913,15 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                        goto out;
                status = lookup_or_create_lock_state(cstate, open_stp, lock,
                                                        &lock_stp, &new_state);
-       } else
+       } else {
                status = nfs4_preprocess_seqid_op(cstate,
                                       lock->lk_old_lock_seqid,
                                       &lock->lk_old_lock_stateid,
                                       NFS4_LOCK_STID, &lock_stp, nn);
+               /* FIXME: move into nfs4_preprocess_seqid_op */
+               if (!status)
+                       atomic_inc(&lock_stp->st_stid.sc_count);
+       }
        if (status)
                goto out;
        lock_sop = lockowner(lock_stp->st_stateowner);
@@ -4847,7 +4945,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                goto out;
        }
 
-       fp = lock_stp->st_file;
+       fp = lock_stp->st_stid.sc_file;
        locks_init_lock(file_lock);
        switch (lock->lk_type) {
                case NFS4_READ_LT:
@@ -4916,6 +5014,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 out:
        if (filp)
                fput(filp);
+       if (lock_stp)
+               nfs4_put_stid(&lock_stp->st_stid);
        if (status && new_state)
                release_lock_stateid(lock_stp);
        nfsd4_bump_seqid(cstate, status);
@@ -5047,10 +5147,12 @@ nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                                        &stp, nn);
        if (status)
                goto out;
-       filp = find_any_file(stp->st_file);
+       /* FIXME: move into nfs4_preprocess_seqid_op */
+       atomic_inc(&stp->st_stid.sc_count);
+       filp = find_any_file(stp->st_stid.sc_file);
        if (!filp) {
                status = nfserr_lock_range;
-               goto out;
+               goto put_stateid;
        }
        file_lock = locks_alloc_lock();
        if (!file_lock) {
@@ -5080,6 +5182,8 @@ nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
 fput:
        fput(filp);
+put_stateid:
+       nfs4_put_stid(&stp->st_stid);
 out:
        nfsd4_bump_seqid(cstate, status);
        if (!cstate->replay_owner)
@@ -5170,7 +5274,7 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp,
        lo = lockowner(sop);
        /* see if there are still any locks associated with it */
        list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
-               if (check_for_locks(stp->st_file, lo))
+               if (check_for_locks(stp->st_stid.sc_file, lo))
                        goto out;
        }
 
@@ -5654,7 +5758,7 @@ nfs4_state_shutdown_net(struct net *net)
        list_for_each_safe(pos, next, &reaplist) {
                dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
                list_del_init(&dp->dl_recall_lru);
-               nfs4_put_delegation(dp);
+               nfs4_put_stid(&dp->dl_stid);
        }
 
        nfsd4_client_tracking_exit(net);