nfs4_free_ol_stateid(stid);
 }
 
+/*
+ * Put the persistent reference to an already unhashed generic stateid, while
+ * holding the cl_lock. If it's the last reference, then put it onto the
+ * reaplist for later destruction.
+ */
+static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
+                                      struct list_head *reaplist)
+{
+       struct nfs4_stid *s = &stp->st_stid;
+       struct nfs4_client *clp = s->sc_client;
+
+       lockdep_assert_held(&clp->cl_lock);
+
+       WARN_ON_ONCE(!list_empty(&stp->st_locks));
+
+       if (!atomic_dec_and_test(&s->sc_count)) {
+               wake_up_all(&close_wq);
+               return;
+       }
+
+       idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
+       list_add(&stp->st_locks, reaplist);
+}
+
 static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
 {
        struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
        list_del_init(&lo->lo_owner.so_strhash);
 }
 
+/*
+ * Free a list of generic stateids that were collected earlier after being
+ * fully unhashed.
+ */
+static void
+free_ol_stateid_reaplist(struct list_head *reaplist)
+{
+       struct nfs4_ol_stateid *stp;
+
+       might_sleep();
+
+       while (!list_empty(reaplist)) {
+               stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
+                                      st_locks);
+               list_del(&stp->st_locks);
+               stp->st_stid.sc_free(&stp->st_stid);
+       }
+}
+
 static void release_lockowner(struct nfs4_lockowner *lo)
 {
        struct nfs4_client *clp = lo->lo_owner.so_client;
                stp = list_first_entry(&lo->lo_owner.so_stateids,
                                struct nfs4_ol_stateid, st_perstateowner);
                unhash_lock_stateid(stp);
-               /*
-                * We now know that no new references can be added to the
-                * stateid. If ours is the last one, finish the unhashing
-                * and put it on the list to be reaped.
-                */
-               if (atomic_dec_and_test(&stp->st_stid.sc_count)) {
-                       idr_remove(&clp->cl_stateids,
-                               stp->st_stid.sc_stateid.si_opaque.so_id);
-                       list_add(&stp->st_locks, &reaplist);
-               }
+               put_ol_stateid_locked(stp, &reaplist);
        }
        spin_unlock(&clp->cl_lock);
-       while (!list_empty(&reaplist)) {
-               stp = list_first_entry(&reaplist, struct nfs4_ol_stateid,
-                                       st_locks);
-               list_del(&stp->st_locks);
-               stp->st_stid.sc_free(&stp->st_stid);
-       }
+       free_ol_stateid_reaplist(&reaplist);
        nfs4_put_stateowner(&lo->lo_owner);
 }
 
 
 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
 {
-       spin_lock(&stp->st_stateowner->so_client->cl_lock);
+       lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
+
        unhash_generic_stateid(stp);
        release_open_stateid_locks(stp);
-       spin_unlock(&stp->st_stateowner->so_client->cl_lock);
 }
 
 static void release_open_stateid(struct nfs4_ol_stateid *stp)
 {
+       LIST_HEAD(reaplist);
+
+       spin_lock(&stp->st_stid.sc_client->cl_lock);
        unhash_open_stateid(stp);
-       nfs4_put_stid(&stp->st_stid);
+       put_ol_stateid_locked(stp, &reaplist);
+       spin_unlock(&stp->st_stid.sc_client->cl_lock);
+       free_ol_stateid_reaplist(&reaplist);
 }
 
 static void unhash_openowner_locked(struct nfs4_openowner *oo)
        }
 }
 
-static void release_openowner_stateids(struct nfs4_openowner *oo)
+static void release_openowner(struct nfs4_openowner *oo)
 {
        struct nfs4_ol_stateid *stp;
        struct nfs4_client *clp = oo->oo_owner.so_client;
+       struct list_head reaplist;
 
-       lockdep_assert_held(&clp->cl_lock);
+       INIT_LIST_HEAD(&reaplist);
 
+       spin_lock(&clp->cl_lock);
+       unhash_openowner_locked(oo);
        while (!list_empty(&oo->oo_owner.so_stateids)) {
                stp = list_first_entry(&oo->oo_owner.so_stateids,
                                struct nfs4_ol_stateid, st_perstateowner);
-               spin_unlock(&clp->cl_lock);
-               release_open_stateid(stp);
-               spin_lock(&clp->cl_lock);
+               unhash_open_stateid(stp);
+               put_ol_stateid_locked(stp, &reaplist);
        }
-}
-
-static void release_openowner(struct nfs4_openowner *oo)
-{
-       struct nfs4_client *clp = oo->oo_owner.so_client;
-
-       spin_lock(&clp->cl_lock);
-       unhash_openowner_locked(oo);
-       release_openowner_stateids(oo);
        spin_unlock(&clp->cl_lock);
+       free_ol_stateid_reaplist(&reaplist);
        release_last_closed_stateid(oo);
        nfs4_put_stateowner(&oo->oo_owner);
 }
        struct nfs4_client *clp = s->st_stid.sc_client;
 
        s->st_stid.sc_type = NFS4_CLOSED_STID;
+       spin_lock(&clp->cl_lock);
        unhash_open_stateid(s);
+       spin_unlock(&clp->cl_lock);
 
        if (clp->cl_minorversion)
                nfs4_put_stid(&s->st_stid);