]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - fs/nfsd/nfs4state.c
Merge git://github.com/rustyrussell/linux
[karo-tx-linux.git] / fs / nfsd / nfs4state.c
index f0eccc236a0da515bbd7775b7f3a70c7e9f36f9d..47e94e33a975d801ddefbf36d4ed793333ff141b 100644 (file)
@@ -49,9 +49,6 @@
 time_t nfsd4_lease = 90;     /* default lease time */
 time_t nfsd4_grace = 90;
 static time_t boot_time;
-static u32 current_ownerid = 1;
-static u32 current_fileid = 1;
-static u32 current_delegid = 1;
 static stateid_t zerostateid;             /* bits all 0 */
 static stateid_t onestateid;              /* bits all 1 */
 static u64 current_sessionid = 1;
@@ -60,8 +57,6 @@ static u64 current_sessionid = 1;
 #define ONE_STATEID(stateid)  (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
 
 /* forward declarations */
-static struct nfs4_delegation * search_for_delegation(stateid_t *stid);
-static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
 
 /* Locking: */
@@ -109,6 +104,11 @@ opaque_hashval(const void *ptr, int nbytes)
 
 static struct list_head del_recall_lru;
 
+static void nfsd4_free_file(struct nfs4_file *f)
+{
+       kmem_cache_free(file_slab, f);
+}
+
 static inline void
 put_nfs4_file(struct nfs4_file *fi)
 {
@@ -116,7 +116,7 @@ put_nfs4_file(struct nfs4_file *fi)
                list_del(&fi->fi_hash);
                spin_unlock(&recall_lock);
                iput(fi->fi_inode);
-               kmem_cache_free(file_slab, fi);
+               nfsd4_free_file(fi);
        }
 }
 
@@ -138,11 +138,6 @@ unsigned int max_delegations;
 #define OPEN_OWNER_HASH_SIZE             (1 << OPEN_OWNER_HASH_BITS)
 #define OPEN_OWNER_HASH_MASK             (OPEN_OWNER_HASH_SIZE - 1)
 
-static unsigned int open_ownerid_hashval(const u32 id)
-{
-       return id & OPEN_OWNER_HASH_MASK;
-}
-
 static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
 {
        unsigned int ret;
@@ -152,31 +147,19 @@ static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *owner
        return ret & OPEN_OWNER_HASH_MASK;
 }
 
-static struct list_head        open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
 static struct list_head        open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
 
 /* hash table for nfs4_file */
 #define FILE_HASH_BITS                   8
 #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
 
-/* hash table for (open)nfs4_stateid */
-#define STATEID_HASH_BITS              10
-#define STATEID_HASH_SIZE              (1 << STATEID_HASH_BITS)
-#define STATEID_HASH_MASK              (STATEID_HASH_SIZE - 1)
-
 static unsigned int file_hashval(struct inode *ino)
 {
        /* XXX: why are we hashing on inode pointer, anyway? */
        return hash_ptr(ino, FILE_HASH_BITS);
 }
 
-static unsigned int stateid_hashval(u32 owner_id, u32 file_id)
-{
-       return (owner_id + file_id) & STATEID_HASH_MASK;
-}
-
 static struct list_head file_hashtbl[FILE_HASH_SIZE];
-static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
 
 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
 {
@@ -204,8 +187,15 @@ static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
 {
        if (atomic_dec_and_test(&fp->fi_access[oflag])) {
-               nfs4_file_put_fd(fp, O_RDWR);
                nfs4_file_put_fd(fp, oflag);
+               /*
+                * It's also safe to get rid of the RDWR open *if*
+                * we no longer have need of the other kind of access
+                * or if we already have the other kind of open:
+                */
+               if (fp->fi_fds[1-oflag]
+                       || atomic_read(&fp->fi_access[1 - oflag]) == 0)
+                       nfs4_file_put_fd(fp, O_RDWR);
        }
 }
 
@@ -218,8 +208,73 @@ static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
                __nfs4_file_put_access(fp, oflag);
 }
 
+static inline int get_new_stid(struct nfs4_stid *stid)
+{
+       static int min_stateid = 0;
+       struct idr *stateids = &stid->sc_client->cl_stateids;
+       int new_stid;
+       int error;
+
+       error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
+       /*
+        * Note: the necessary preallocation was done in
+        * nfs4_alloc_stateid().  The idr code caps the number of
+        * preallocations that can exist at a time, but the state lock
+        * prevents anyone from using ours before we get here:
+        */
+       BUG_ON(error);
+       /*
+        * It shouldn't be a problem to reuse an opaque stateid value.
+        * I don't think it is for 4.1.  But with 4.0 I worry that, for
+        * example, a stray write retransmission could be accepted by
+        * the server when it should have been rejected.  Therefore,
+        * adopt a trick from the sctp code to attempt to maximize the
+        * amount of time until an id is reused, by ensuring they always
+        * "increase" (mod INT_MAX):
+        */
+
+       min_stateid = new_stid+1;
+       if (min_stateid == INT_MAX)
+               min_stateid = 0;
+       return new_stid;
+}
+
+static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
+{
+       stateid_t *s = &stid->sc_stateid;
+       int new_id;
+
+       stid->sc_type = type;
+       stid->sc_client = cl;
+       s->si_opaque.so_clid = cl->cl_clientid;
+       new_id = get_new_stid(stid);
+       s->si_opaque.so_id = (u32)new_id;
+       /* Will be incremented before return to client: */
+       s->si_generation = 0;
+}
+
+static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
+{
+       struct idr *stateids = &cl->cl_stateids;
+
+       if (!idr_pre_get(stateids, GFP_KERNEL))
+               return NULL;
+       /*
+        * Note: if we fail here (or any time between now and the time
+        * we actually get the new idr), we won't need to undo the idr
+        * preallocation, since the idr code caps the number of
+        * preallocated entries.
+        */
+       return kmem_cache_alloc(slab, GFP_KERNEL);
+}
+
+static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
+{
+       return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
+}
+
 static struct nfs4_delegation *
-alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
+alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
 {
        struct nfs4_delegation *dp;
        struct nfs4_file *fp = stp->st_file;
@@ -236,21 +291,23 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_f
                return NULL;
        if (num_delegations > max_delegations)
                return NULL;
-       dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
+       dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
        if (dp == NULL)
                return dp;
+       init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
+       /*
+        * delegation seqid's are never incremented.  The 4.1 special
+        * meaning of seqid 0 isn't meaningful, really, but let's avoid
+        * 0 anyway just for consistency and use 1:
+        */
+       dp->dl_stid.sc_stateid.si_generation = 1;
        num_delegations++;
        INIT_LIST_HEAD(&dp->dl_perfile);
        INIT_LIST_HEAD(&dp->dl_perclnt);
        INIT_LIST_HEAD(&dp->dl_recall_lru);
-       dp->dl_client = clp;
        get_nfs4_file(fp);
        dp->dl_file = fp;
        dp->dl_type = type;
-       dp->dl_stateid.si_boot = boot_time;
-       dp->dl_stateid.si_stateownerid = current_delegid++;
-       dp->dl_stateid.si_fileid = 0;
-       dp->dl_stateid.si_generation = 1;
        fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
        dp->dl_time = 0;
        atomic_set(&dp->dl_count, 1);
@@ -279,10 +336,18 @@ static void nfs4_put_deleg_lease(struct nfs4_file *fp)
        }
 }
 
+static void unhash_stid(struct nfs4_stid *s)
+{
+       struct idr *stateids = &s->sc_client->cl_stateids;
+
+       idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
+}
+
 /* Called under the state lock. */
 static void
 unhash_delegation(struct nfs4_delegation *dp)
 {
+       unhash_stid(&dp->dl_stid);
        list_del_init(&dp->dl_perclnt);
        spin_lock(&recall_lock);
        list_del_init(&dp->dl_perfile);
@@ -380,7 +445,7 @@ set_deny(unsigned int *deny, unsigned long bmap) {
 }
 
 static int
-test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
+test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
        unsigned int access, deny;
 
        set_access(&access, stp->st_access_bmap);
@@ -403,14 +468,13 @@ static int nfs4_access_to_omode(u32 access)
        BUG();
 }
 
-static void unhash_generic_stateid(struct nfs4_stateid *stp)
+static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
 {
-       list_del(&stp->st_hash);
        list_del(&stp->st_perfile);
        list_del(&stp->st_perstateowner);
 }
 
-static void close_generic_stateid(struct nfs4_stateid *stp)
+static void close_generic_stateid(struct nfs4_ol_stateid *stp)
 {
        int i;
 
@@ -426,33 +490,33 @@ static void close_generic_stateid(struct nfs4_stateid *stp)
        stp->st_file = NULL;
 }
 
-static void free_generic_stateid(struct nfs4_stateid *stp)
+static void free_generic_stateid(struct nfs4_ol_stateid *stp)
 {
-       close_generic_stateid(stp);
        kmem_cache_free(stateid_slab, stp);
 }
 
-static void release_lock_stateid(struct nfs4_stateid *stp)
+static void release_lock_stateid(struct nfs4_ol_stateid *stp)
 {
        struct file *file;
 
        unhash_generic_stateid(stp);
+       unhash_stid(&stp->st_stid);
        file = find_any_file(stp->st_file);
        if (file)
                locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
+       close_generic_stateid(stp);
        free_generic_stateid(stp);
 }
 
 static void unhash_lockowner(struct nfs4_lockowner *lo)
 {
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
 
-       list_del(&lo->lo_owner.so_idhash);
        list_del(&lo->lo_owner.so_strhash);
        list_del(&lo->lo_perstateid);
        while (!list_empty(&lo->lo_owner.so_stateids)) {
                stp = list_first_entry(&lo->lo_owner.so_stateids,
-                               struct nfs4_stateid, st_perstateowner);
+                               struct nfs4_ol_stateid, st_perstateowner);
                release_lock_stateid(stp);
        }
 }
@@ -464,7 +528,7 @@ static void release_lockowner(struct nfs4_lockowner *lo)
 }
 
 static void
-release_stateid_lockowners(struct nfs4_stateid *open_stp)
+release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
 {
        struct nfs4_lockowner *lo;
 
@@ -475,31 +539,49 @@ release_stateid_lockowners(struct nfs4_stateid *open_stp)
        }
 }
 
-static void release_open_stateid(struct nfs4_stateid *stp)
+static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
 {
        unhash_generic_stateid(stp);
        release_stateid_lockowners(stp);
+       close_generic_stateid(stp);
+}
+
+static void release_open_stateid(struct nfs4_ol_stateid *stp)
+{
+       unhash_open_stateid(stp);
+       unhash_stid(&stp->st_stid);
        free_generic_stateid(stp);
 }
 
 static void unhash_openowner(struct nfs4_openowner *oo)
 {
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
 
-       list_del(&oo->oo_owner.so_idhash);
        list_del(&oo->oo_owner.so_strhash);
        list_del(&oo->oo_perclient);
        while (!list_empty(&oo->oo_owner.so_stateids)) {
                stp = list_first_entry(&oo->oo_owner.so_stateids,
-                               struct nfs4_stateid, st_perstateowner);
+                               struct nfs4_ol_stateid, st_perstateowner);
                release_open_stateid(stp);
        }
 }
 
+static void release_last_closed_stateid(struct nfs4_openowner *oo)
+{
+       struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
+
+       if (s) {
+               unhash_stid(&s->st_stid);
+               free_generic_stateid(s);
+               oo->oo_last_closed_stid = NULL;
+       }
+}
+
 static void release_openowner(struct nfs4_openowner *oo)
 {
        unhash_openowner(oo);
        list_del(&oo->oo_close_lru);
+       release_last_closed_stateid(oo);
        nfs4_free_openowner(oo);
 }
 
@@ -865,9 +947,6 @@ renew_client_locked(struct nfs4_client *clp)
                return;
        }
 
-       /*
-       * Move client to the end to the LRU list.
-       */
        dprintk("renewing client (clientid %08x/%08x)\n", 
                        clp->cl_clientid.cl_boot, 
                        clp->cl_clientid.cl_id);
@@ -1060,29 +1139,20 @@ static void gen_confirm(struct nfs4_client *clp)
        *p++ = i++;
 }
 
-static int
-same_stateid(stateid_t *id_one, stateid_t *id_two)
+static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
 {
-       if (id_one->si_stateownerid != id_two->si_stateownerid)
-               return 0;
-       return id_one->si_fileid == id_two->si_fileid;
+       return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
 }
 
-static struct nfs4_stateid *find_stateid(stateid_t *t, int flags)
+static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
 {
-       struct nfs4_stateid *s;
-       unsigned int hashval;
+       struct nfs4_stid *s;
 
-       hashval = stateid_hashval(t->si_stateownerid, t->si_fileid);
-       list_for_each_entry(s, &stateid_hashtbl[hashval], st_hash) {
-               if (!same_stateid(&s->st_stateid, t))
-                       continue;
-               if (flags & LOCK_STATE && s->st_type != NFS4_LOCK_STID)
-                       return NULL;
-               if (flags & OPEN_STATE && s->st_type != NFS4_OPEN_STID)
-                       return NULL;
+       s = find_stateid(cl, t);
+       if (!s)
+               return NULL;
+       if (typemask & s->sc_type)
                return s;
-               }
        return NULL;
 }
 
@@ -1108,6 +1178,7 @@ static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
                }
        }
 
+       idr_init(&clp->cl_stateids);
        memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
        atomic_set(&clp->cl_refcount, 0);
        clp->cl_cb_state = NFSD4_CB_UNKNOWN;
@@ -1131,17 +1202,6 @@ static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
        return clp;
 }
 
-static int check_name(struct xdr_netobj name)
-{
-       if (name.len == 0) 
-               return 0;
-       if (name.len > NFS4_OPAQUE_LIMIT) {
-               dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
-               return 0;
-       }
-       return 1;
-}
-
 static void
 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
 {
@@ -1173,8 +1233,10 @@ find_confirmed_client(clientid_t *clid)
        unsigned int idhashval = clientid_hashval(clid->cl_id);
 
        list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
-               if (same_clid(&clp->cl_clientid, clid))
+               if (same_clid(&clp->cl_clientid, clid)) {
+                       renew_client(clp);
                        return clp;
+               }
        }
        return NULL;
 }
@@ -1221,20 +1283,6 @@ find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
        return NULL;
 }
 
-static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
-{
-       switch (family) {
-       case AF_INET:
-               ((struct sockaddr_in *)sa)->sin_family = AF_INET;
-               ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
-               return;
-       case AF_INET6:
-               ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
-               ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
-               return;
-       }
-}
-
 static void
 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
 {
@@ -1266,7 +1314,7 @@ gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_r
 
        conn->cb_prog = se->se_callback_prog;
        conn->cb_ident = se->se_callback_ident;
-       rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
+       memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
        return;
 out_err:
        conn->cb_addr.ss_family = AF_UNSPEC;
@@ -1398,7 +1446,7 @@ nfsd4_exchange_id(struct svc_rqst *rqstp,
                __func__, rqstp, exid, exid->clname.len, exid->clname.data,
                addr_str, exid->flags, exid->spa_how);
 
-       if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
+       if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
                return nfserr_inval;
 
        /* Currently only support SP4_NONE */
@@ -1897,8 +1945,16 @@ out:
 
                nfsd4_get_session(cstate->session);
                atomic_inc(&clp->cl_refcount);
-               if (clp->cl_cb_state == NFSD4_CB_DOWN)
-                       seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
+               switch (clp->cl_cb_state) {
+               case NFSD4_CB_DOWN:
+                       seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
+                       break;
+               case NFSD4_CB_FAULT:
+                       seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
+                       break;
+               default:
+                       seq->status_flags = 0;
+               }
        }
        kfree(conn);
        spin_unlock(&client_lock);
@@ -1906,6 +1962,50 @@ out:
        return status;
 }
 
+static inline bool has_resources(struct nfs4_client *clp)
+{
+       return !list_empty(&clp->cl_openowners)
+               || !list_empty(&clp->cl_delegations)
+               || !list_empty(&clp->cl_sessions);
+}
+
+__be32
+nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
+{
+       struct nfs4_client *conf, *unconf, *clp;
+       int status = 0;
+
+       nfs4_lock_state();
+       unconf = find_unconfirmed_client(&dc->clientid);
+       conf = find_confirmed_client(&dc->clientid);
+
+       if (conf) {
+               clp = conf;
+
+               if (!is_client_expired(conf) && has_resources(conf)) {
+                       status = nfserr_clientid_busy;
+                       goto out;
+               }
+
+               /* rfc5661 18.50.3 */
+               if (cstate->session && conf == cstate->session->se_client) {
+                       status = nfserr_clientid_busy;
+                       goto out;
+               }
+       } else if (unconf)
+               clp = unconf;
+       else {
+               status = nfserr_stale_clientid;
+               goto out;
+       }
+
+       expire_client(clp);
+out:
+       nfs4_unlock_state();
+       dprintk("%s return %d\n", __func__, ntohl(status));
+       return status;
+}
+
 __be32
 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
 {
@@ -1948,19 +2048,13 @@ __be32
 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                  struct nfsd4_setclientid *setclid)
 {
-       struct xdr_netobj       clname = { 
-               .len = setclid->se_namelen,
-               .data = setclid->se_name,
-       };
+       struct xdr_netobj       clname = setclid->se_name;
        nfs4_verifier           clverifier = setclid->se_verf;
        unsigned int            strhashval;
        struct nfs4_client      *conf, *unconf, *new;
        __be32                  status;
        char                    dname[HEXDIR_LEN];
        
-       if (!check_name(clname))
-               return nfserr_inval;
-
        status = nfs4_make_rec_clidname(dname, &clname);
        if (status)
                return status;
@@ -2164,31 +2258,28 @@ out:
        return status;
 }
 
+static struct nfs4_file *nfsd4_alloc_file(void)
+{
+       return kmem_cache_alloc(file_slab, GFP_KERNEL);
+}
+
 /* OPEN Share state helper functions */
-static inline struct nfs4_file *
-alloc_init_file(struct inode *ino)
+static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
 {
-       struct nfs4_file *fp;
        unsigned int hashval = file_hashval(ino);
 
-       fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
-       if (fp) {
-               atomic_set(&fp->fi_ref, 1);
-               INIT_LIST_HEAD(&fp->fi_hash);
-               INIT_LIST_HEAD(&fp->fi_stateids);
-               INIT_LIST_HEAD(&fp->fi_delegations);
-               fp->fi_inode = igrab(ino);
-               fp->fi_id = current_fileid++;
-               fp->fi_had_conflict = false;
-               fp->fi_lease = NULL;
-               memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
-               memset(fp->fi_access, 0, sizeof(fp->fi_access));
-               spin_lock(&recall_lock);
-               list_add(&fp->fi_hash, &file_hashtbl[hashval]);
-               spin_unlock(&recall_lock);
-               return fp;
-       }
-       return NULL;
+       atomic_set(&fp->fi_ref, 1);
+       INIT_LIST_HEAD(&fp->fi_hash);
+       INIT_LIST_HEAD(&fp->fi_stateids);
+       INIT_LIST_HEAD(&fp->fi_delegations);
+       fp->fi_inode = igrab(ino);
+       fp->fi_had_conflict = false;
+       fp->fi_lease = NULL;
+       memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
+       memset(fp->fi_access, 0, sizeof(fp->fi_access));
+       spin_lock(&recall_lock);
+       list_add(&fp->fi_hash, &file_hashtbl[hashval]);
+       spin_unlock(&recall_lock);
 }
 
 static void
@@ -2226,7 +2317,7 @@ nfsd4_init_slabs(void)
        if (file_slab == NULL)
                goto out_nomem;
        stateid_slab = kmem_cache_create("nfsd4_stateids",
-                       sizeof(struct nfs4_stateid), 0, 0, NULL);
+                       sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
        if (stateid_slab == NULL)
                goto out_nomem;
        deleg_slab = kmem_cache_create("nfsd4_delegations",
@@ -2275,7 +2366,6 @@ static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj
        sop->so_owner.len = owner->len;
 
        INIT_LIST_HEAD(&sop->so_stateids);
-       sop->so_id = current_ownerid++;
        sop->so_client = clp;
        init_nfs4_replay(&sop->so_replay);
        return sop;
@@ -2283,10 +2373,6 @@ static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj
 
 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
 {
-       unsigned int idhashval;
-
-       idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
-       list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
        list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
        list_add(&oo->oo_perclient, &clp->cl_openowners);
 }
@@ -2300,38 +2386,28 @@ alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, str
                return NULL;
        oo->oo_owner.so_is_open_owner = 1;
        oo->oo_owner.so_seqid = open->op_seqid;
-       oo->oo_confirmed = 0;
+       oo->oo_flags = NFS4_OO_NEW;
        oo->oo_time = 0;
+       oo->oo_last_closed_stid = NULL;
        INIT_LIST_HEAD(&oo->oo_close_lru);
        hash_openowner(oo, clp, strhashval);
        return oo;
 }
 
-static inline void
-init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
+static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
        struct nfs4_openowner *oo = open->op_openowner;
-       unsigned int hashval = stateid_hashval(oo->oo_owner.so_id, fp->fi_id);
+       struct nfs4_client *clp = oo->oo_owner.so_client;
 
-       INIT_LIST_HEAD(&stp->st_hash);
-       INIT_LIST_HEAD(&stp->st_perstateowner);
+       init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
        INIT_LIST_HEAD(&stp->st_lockowners);
-       INIT_LIST_HEAD(&stp->st_perfile);
-       list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
        list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
        list_add(&stp->st_perfile, &fp->fi_stateids);
-       stp->st_type = NFS4_OPEN_STID;
        stp->st_stateowner = &oo->oo_owner;
        get_nfs4_file(fp);
        stp->st_file = fp;
-       stp->st_stateid.si_boot = boot_time;
-       stp->st_stateid.si_stateownerid = oo->oo_owner.so_id;
-       stp->st_stateid.si_fileid = fp->fi_id;
-       /* note will be incremented before first return to client: */
-       stp->st_stateid.si_generation = 0;
        stp->st_access_bmap = 0;
        stp->st_deny_bmap = 0;
-       __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
-                 &stp->st_access_bmap);
+       __set_bit(open->op_share_access, &stp->st_access_bmap);
        __set_bit(open->op_share_deny, &stp->st_deny_bmap);
        stp->st_openstp = NULL;
 }
@@ -2357,11 +2433,15 @@ same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
 static struct nfs4_openowner *
 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
 {
-       struct nfs4_stateowner *so = NULL;
+       struct nfs4_stateowner *so;
+       struct nfs4_openowner *oo;
 
        list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
-               if (same_owner_str(so, &open->op_owner, &open->op_clientid))
-                       return container_of(so, struct nfs4_openowner, oo_owner);
+               if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
+                       oo = openowner(so);
+                       renew_client(oo->oo_owner.so_client);
+                       return oo;
+               }
        }
        return NULL;
 }
@@ -2385,31 +2465,6 @@ find_file(struct inode *ino)
        return NULL;
 }
 
-static inline int access_valid(u32 x, u32 minorversion)
-{
-       if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
-               return 0;
-       if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
-               return 0;
-       x &= ~NFS4_SHARE_ACCESS_MASK;
-       if (minorversion && x) {
-               if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
-                       return 0;
-               if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
-                       return 0;
-               x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
-       }
-       if (x)
-               return 0;
-       return 1;
-}
-
-static inline int deny_valid(u32 x)
-{
-       /* Note: unlike access bits, deny bits may be zero. */
-       return x <= NFS4_SHARE_DENY_BOTH;
-}
-
 /*
  * Called to check deny when READ with all zero stateid or
  * WRITE with all zero or all one stateid
@@ -2419,7 +2474,7 @@ nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
 {
        struct inode *ino = current_fh->fh_dentry->d_inode;
        struct nfs4_file *fp;
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
        __be32 ret;
 
        dprintk("NFSD: nfs4_share_conflict\n");
@@ -2515,41 +2570,46 @@ nfsd4_process_open1(struct nfsd4_compound_state *cstate,
        struct nfs4_openowner *oo = NULL;
        __be32 status;
 
-       if (!check_name(open->op_owner))
-               return nfserr_inval;
-
        if (STALE_CLIENTID(&open->op_clientid))
                return nfserr_stale_clientid;
+       /*
+        * In case we need it later, after we've already created the
+        * file and don't want to risk a further failure:
+        */
+       open->op_file = nfsd4_alloc_file();
+       if (open->op_file == NULL)
+               return nfserr_jukebox;
 
        strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
        oo = find_openstateowner_str(strhashval, open);
        open->op_openowner = oo;
        if (!oo) {
-               /* Make sure the client's lease hasn't expired. */
                clp = find_confirmed_client(clientid);
                if (clp == NULL)
                        return nfserr_expired;
-               goto renew;
+               goto new_owner;
        }
-       if (!oo->oo_confirmed) {
+       if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
                /* Replace unconfirmed owners without checking for replay. */
                clp = oo->oo_owner.so_client;
                release_openowner(oo);
                open->op_openowner = NULL;
-               goto renew;
+               goto new_owner;
        }
        status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
        if (status)
                return status;
-renew:
-       if (open->op_openowner == NULL) {
-               oo = alloc_init_open_stateowner(strhashval, clp, open);
-               if (oo == NULL)
-                       return nfserr_jukebox;
-               open->op_openowner = oo;
-       }
-       list_del_init(&oo->oo_close_lru);
-       renew_client(oo->oo_owner.so_client);
+       clp = oo->oo_owner.so_client;
+       goto alloc_stateid;
+new_owner:
+       oo = alloc_init_open_stateowner(strhashval, clp, open);
+       if (oo == NULL)
+               return nfserr_jukebox;
+       open->op_openowner = oo;
+alloc_stateid:
+       open->op_stp = nfs4_alloc_stateid(clp);
+       if (!open->op_stp)
+               return nfserr_jukebox;
        return nfs_ok;
 }
 
@@ -2562,36 +2622,37 @@ nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
                return nfs_ok;
 }
 
-static struct nfs4_delegation *
-find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
+static int share_access_to_flags(u32 share_access)
 {
-       struct nfs4_delegation *dp;
+       share_access &= ~NFS4_SHARE_WANT_MASK;
 
-       spin_lock(&recall_lock);
-       list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
-               if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
-                       spin_unlock(&recall_lock);
-                       return dp;
-               }
-       spin_unlock(&recall_lock);
-       return NULL;
+       return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
 }
 
-static int share_access_to_flags(u32 share_access)
+static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
 {
-       share_access &= ~NFS4_SHARE_WANT_MASK;
+       struct nfs4_stid *ret;
 
-       return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
+       ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
+       if (!ret)
+               return NULL;
+       return delegstateid(ret);
+}
+
+static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
+{
+       return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
+              open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
 }
 
 static __be32
-nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
+nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
                struct nfs4_delegation **dp)
 {
        int flags;
        __be32 status = nfserr_bad_stateid;
 
-       *dp = find_delegation_file(fp, &open->op_delegate_stateid);
+       *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
        if (*dp == NULL)
                goto out;
        flags = share_access_to_flags(open->op_share_access);
@@ -2599,18 +2660,18 @@ nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
        if (status)
                *dp = NULL;
 out:
-       if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
+       if (!nfsd4_is_deleg_cur(open))
                return nfs_ok;
        if (status)
                return status;
-       open->op_openowner->oo_confirmed = 1;
+       open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
        return nfs_ok;
 }
 
 static __be32
-nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
+nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
 {
-       struct nfs4_stateid *local;
+       struct nfs4_ol_stateid *local;
        struct nfs4_openowner *oo = open->op_openowner;
 
        list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
@@ -2627,10 +2688,9 @@ nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_state
        return nfs_ok;
 }
 
-static inline struct nfs4_stateid *
-nfs4_alloc_stateid(void)
+static void nfs4_free_stateid(struct nfs4_ol_stateid *s)
 {
-       return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
+       kmem_cache_free(stateid_slab, s);
 }
 
 static inline int nfs4_access_to_access(u32 nfs4_access)
@@ -2651,12 +2711,6 @@ static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
        int oflag = nfs4_access_to_omode(open->op_share_access);
        int access = nfs4_access_to_access(open->op_share_access);
 
-       /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
-        * allowing it to break the lease and return EAGAIN leaves the
-        * client unable to make progress in returning the delegation */
-       if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
-               access |= NFSD_MAY_NOT_BREAK_LEASE;
-
        if (!fp->fi_fds[oflag]) {
                status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
                        &fp->fi_fds[oflag]);
@@ -2668,27 +2722,6 @@ static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
        return nfs_ok;
 }
 
-static __be32
-nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
-               struct nfs4_file *fp, struct svc_fh *cur_fh,
-               struct nfsd4_open *open)
-{
-       struct nfs4_stateid *stp;
-       __be32 status;
-
-       stp = nfs4_alloc_stateid();
-       if (stp == NULL)
-               return nfserr_jukebox;
-
-       status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
-       if (status) {
-               kmem_cache_free(stateid_slab, stp);
-               return status;
-       }
-       *stpp = stp;
-       return 0;
-}
-
 static inline __be32
 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
                struct nfsd4_open *open)
@@ -2705,9 +2738,9 @@ nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
 }
 
 static __be32
-nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
+nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
 {
-       u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
+       u32 op_share_access = open->op_share_access;
        bool new_access;
        __be32 status;
 
@@ -2736,7 +2769,7 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c
 static void
 nfs4_set_claim_prev(struct nfsd4_open *open)
 {
-       open->op_openowner->oo_confirmed = 1;
+       open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
        open->op_openowner->oo_owner.so_client->cl_firststate = 1;
 }
 
@@ -2780,7 +2813,7 @@ static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
        if (!fl)
                return -ENOMEM;
        fl->fl_file = find_readable_file(fp);
-       list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
+       list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
        status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
        if (status) {
                list_del_init(&dp->dl_perclnt);
@@ -2809,7 +2842,7 @@ static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
        atomic_inc(&fp->fi_delegees);
        list_add(&dp->dl_perfile, &fp->fi_delegations);
        spin_unlock(&recall_lock);
-       list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
+       list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
        return 0;
 }
 
@@ -2817,7 +2850,7 @@ static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  * Attempt to hand out a delegation.
  */
 static void
-nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_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);
@@ -2840,7 +2873,7 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_sta
                         * had the chance to reclaim theirs.... */
                        if (locks_in_grace())
                                goto out;
-                       if (!cb_up || !oo->oo_confirmed)
+                       if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
                                goto out;
                        if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
                                flag = NFS4_OPEN_DELEGATE_WRITE;
@@ -2858,10 +2891,10 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_sta
        if (status)
                goto out_free;
 
-       memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
+       memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
        dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
-               STATEID_VAL(&dp->dl_stateid));
+               STATEID_VAL(&dp->dl_stid.sc_stateid));
 out:
        if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
                        && flag == NFS4_OPEN_DELEGATE_NONE
@@ -2883,16 +2916,13 @@ __be32
 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
 {
        struct nfsd4_compoundres *resp = rqstp->rq_resp;
+       struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
        struct nfs4_file *fp = NULL;
        struct inode *ino = current_fh->fh_dentry->d_inode;
-       struct nfs4_stateid *stp = NULL;
+       struct nfs4_ol_stateid *stp = NULL;
        struct nfs4_delegation *dp = NULL;
        __be32 status;
 
-       status = nfserr_inval;
-       if (!access_valid(open->op_share_access, resp->cstate.minorversion)
-                       || !deny_valid(open->op_share_deny))
-               goto out;
        /*
         * Lookup file; if found, lookup stateid and check open request,
         * and check for delegations in the process of being recalled.
@@ -2902,17 +2932,17 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
        if (fp) {
                if ((status = nfs4_check_open(fp, open, &stp)))
                        goto out;
-               status = nfs4_check_deleg(fp, open, &dp);
+               status = nfs4_check_deleg(cl, fp, open, &dp);
                if (status)
                        goto out;
        } else {
                status = nfserr_bad_stateid;
-               if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
+               if (nfsd4_is_deleg_cur(open))
                        goto out;
                status = nfserr_jukebox;
-               fp = alloc_init_file(ino);
-               if (fp == NULL)
-                       goto out;
+               fp = open->op_file;
+               open->op_file = NULL;
+               nfsd4_init_file(fp, ino);
        }
 
        /*
@@ -2925,21 +2955,23 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
                if (status)
                        goto out;
        } else {
-               status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
+               status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
                if (status)
                        goto out;
-               init_stateid(stp, fp, open);
+               stp = open->op_stp;
+               open->op_stp = NULL;
+               init_open_stateid(stp, fp, open);
                status = nfsd4_truncate(rqstp, current_fh, open);
                if (status) {
                        release_open_stateid(stp);
                        goto out;
                }
        }
-       update_stateid(&stp->st_stateid);
-       memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
+       update_stateid(&stp->st_stid.sc_stateid);
+       memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
 
        if (nfsd4_has_session(&resp->cstate))
-               open->op_openowner->oo_confirmed = 1;
+               open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
 
        /*
        * Attempt to hand out a delegation. No error return, because the
@@ -2950,7 +2982,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
        status = nfs_ok;
 
        dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
-               STATEID_VAL(&stp->st_stateid));
+               STATEID_VAL(&stp->st_stid.sc_stateid));
 out:
        if (fp)
                put_nfs4_file(fp);
@@ -2960,13 +2992,34 @@ out:
        * To finish the open response, we just need to set the rflags.
        */
        open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
-       if (!open->op_openowner->oo_confirmed &&
+       if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
            !nfsd4_has_session(&resp->cstate))
                open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
 
        return status;
 }
 
+void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
+{
+       if (open->op_openowner) {
+               struct nfs4_openowner *oo = open->op_openowner;
+
+               if (!list_empty(&oo->oo_owner.so_stateids))
+                       list_del_init(&oo->oo_close_lru);
+               if (oo->oo_flags & NFS4_OO_NEW) {
+                       if (status) {
+                               release_openowner(oo);
+                               open->op_openowner = NULL;
+                       } else
+                               oo->oo_flags &= ~NFS4_OO_NEW;
+               }
+       }
+       if (open->op_file)
+               nfsd4_free_file(open->op_file);
+       if (open->op_stp)
+               nfs4_free_stateid(open->op_stp);
+}
+
 __be32
 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
            clientid_t *clid)
@@ -2987,7 +3040,6 @@ nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                dprintk("nfsd4_renew: clientid not found!\n");
                goto out;
        }
-       renew_client(clp);
        status = nfserr_cb_path_down;
        if (!list_empty(&clp->cl_delegations)
                        && clp->cl_cb_state != NFSD4_CB_UP)
@@ -3083,8 +3135,6 @@ nfs4_laundromat(void)
                                test_val = u;
                        break;
                }
-               dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
-                       oo->oo_owner.so_id);
                release_openowner(oo);
        }
        if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
@@ -3107,27 +3157,17 @@ laundromat_main(struct work_struct *not_used)
        queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
 }
 
-static struct nfs4_openowner * search_close_lru(u32 st_id)
-{
-       struct nfs4_openowner *local;
-
-       list_for_each_entry(local, &close_lru, oo_close_lru) {
-               if (local->oo_owner.so_id == st_id)
-                       return local;
-       }
-       return NULL;
-}
-
-static inline int
-nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
+static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
 {
-       return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
+       if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
+               return nfserr_bad_stateid;
+       return nfs_ok;
 }
 
 static int
 STALE_STATEID(stateid_t *stateid)
 {
-       if (stateid->si_boot == boot_time)
+       if (stateid->si_opaque.so_clid.cl_boot == boot_time)
                return 0;
        dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
                STATEID_VAL(stateid));
@@ -3150,7 +3190,7 @@ access_permit_write(unsigned long access_bmap)
 }
 
 static
-__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
+__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
 {
         __be32 status = nfserr_openmode;
 
@@ -3227,36 +3267,46 @@ static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_sess
        return nfserr_old_stateid;
 }
 
-static int is_delegation_stateid(stateid_t *stateid)
-{
-       return stateid->si_fileid == 0;
-}
-
-__be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
+__be32 nfs4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
 {
-       struct nfs4_stateid *stp = NULL;
-       __be32 status = nfserr_stale_stateid;
+       struct nfs4_stid *s;
+       struct nfs4_ol_stateid *ols;
+       __be32 status;
 
        if (STALE_STATEID(stateid))
-               goto out;
+               return nfserr_stale_stateid;
 
-       status = nfserr_expired;
-       stp = find_stateid(stateid, 0);
-       if (!stp)
-               goto out;
-       status = nfserr_bad_stateid;
+       s = find_stateid(cl, stateid);
+       if (!s)
+                return nfserr_stale_stateid;
+       status = check_stateid_generation(stateid, &s->sc_stateid, 1);
+       if (status)
+               return status;
+       if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
+               return nfs_ok;
+       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;
+}
 
-       if (stp->st_stateowner->so_is_open_owner
-           && !openowner(stp->st_stateowner)->oo_confirmed)
-               goto out;
+static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s)
+{
+       struct nfs4_client *cl;
 
-       status = check_stateid_generation(stateid, &stp->st_stateid, has_session);
-       if (status)
-               goto out;
+       if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
+               return nfserr_bad_stateid;
+       if (STALE_STATEID(stateid))
+               return nfserr_stale_stateid;
+       cl = find_confirmed_client(&stateid->si_opaque.so_clid);
+       if (!cl)
+               return nfserr_expired;
+       *s = find_stateid_by_type(cl, stateid, typemask);
+       if (!*s)
+               return nfserr_bad_stateid;
+       return nfs_ok;
 
-       status = nfs_ok;
-out:
-       return status;
 }
 
 /*
@@ -3266,7 +3316,8 @@ __be32
 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
                           stateid_t *stateid, int flags, struct file **filpp)
 {
-       struct nfs4_stateid *stp = NULL;
+       struct nfs4_stid *s;
+       struct nfs4_ol_stateid *stp = NULL;
        struct nfs4_delegation *dp = NULL;
        struct svc_fh *current_fh = &cstate->current_fh;
        struct inode *ino = current_fh->fh_dentry->d_inode;
@@ -3281,54 +3332,44 @@ nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
        if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
                return check_special_stateids(current_fh, stateid, flags);
 
-       status = nfserr_stale_stateid;
-       if (STALE_STATEID(stateid)) 
+       status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s);
+       if (status)
+               return status;
+       status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
+       if (status)
                goto out;
-
-       /*
-        * We assume that any stateid that has the current boot time,
-        * but that we can't find, is expired:
-        */
-       status = nfserr_expired;
-       if (is_delegation_stateid(stateid)) {
-               dp = find_delegation_stateid(ino, stateid);
-               if (!dp)
-                       goto out;
-               status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
-               if (status)
-                       goto out;
+       switch (s->sc_type) {
+       case NFS4_DELEG_STID:
+               dp = delegstateid(s);
                status = nfs4_check_delegmode(dp, flags);
                if (status)
                        goto out;
-               renew_client(dp->dl_client);
                if (filpp) {
                        *filpp = dp->dl_file->fi_deleg_file;
                        BUG_ON(!*filpp);
                }
-       } else { /* open or lock stateid */
-               stp = find_stateid(stateid, flags);
-               if (!stp)
-                       goto out;
-               status = nfserr_bad_stateid;
-               if (nfs4_check_fh(current_fh, stp))
+               break;
+       case NFS4_OPEN_STID:
+       case NFS4_LOCK_STID:
+               stp = openlockstateid(s);
+               status = nfs4_check_fh(current_fh, stp);
+               if (status)
                        goto out;
                if (stp->st_stateowner->so_is_open_owner
-                   && !openowner(stp->st_stateowner)->oo_confirmed)
-                       goto out;
-               status = check_stateid_generation(stateid, &stp->st_stateid,
-                                                 nfsd4_has_session(cstate));
-               if (status)
+                   && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
                        goto out;
                status = nfs4_check_openmode(stp, flags);
                if (status)
                        goto out;
-               renew_client(stp->st_stateowner->so_client);
                if (filpp) {
                        if (flags & RD_STATE)
                                *filpp = find_readable_file(stp->st_file);
                        else
                                *filpp = find_writeable_file(stp->st_file);
                }
+               break;
+       default:
+               return nfserr_bad_stateid;
        }
        status = nfs_ok;
 out:
@@ -3336,16 +3377,7 @@ out:
 }
 
 static __be32
-nfsd4_free_delegation_stateid(stateid_t *stateid)
-{
-       struct nfs4_delegation *dp = search_for_delegation(stateid);
-       if (dp)
-               return nfserr_locks_held;
-       return nfserr_bad_stateid;
-}
-
-static __be32
-nfsd4_free_lock_stateid(struct nfs4_stateid *stp)
+nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
 {
        if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
                return nfserr_locks_held;
@@ -3360,44 +3392,40 @@ __be32
 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                   struct nfsd4_test_stateid *test_stateid)
 {
-       test_stateid->ts_has_session = nfsd4_has_session(cstate);
+       /* real work is done during encoding */
        return nfs_ok;
 }
 
-/*
- * Free a state id
- */
 __be32
 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                   struct nfsd4_free_stateid *free_stateid)
 {
        stateid_t *stateid = &free_stateid->fr_stateid;
-       struct nfs4_stateid *stp;
-       __be32 ret;
+       struct nfs4_stid *s;
+       struct nfs4_client *cl = cstate->session->se_client;
+       __be32 ret = nfserr_bad_stateid;
 
        nfs4_lock_state();
-       if (is_delegation_stateid(stateid)) {
-               ret = nfsd4_free_delegation_stateid(stateid);
-               goto out;
-       }
-
-       stp = find_stateid(stateid, 0);
-       if (!stp) {
-               ret = nfserr_bad_stateid;
-               goto out;
-       }
-       ret = check_stateid_generation(stateid, &stp->st_stateid, 1);
-       if (ret)
+       s = find_stateid(cl, stateid);
+       if (!s)
                goto out;
-
-       if (stp->st_type == NFS4_OPEN_STID) {
+       switch (s->sc_type) {
+       case NFS4_DELEG_STID:
                ret = nfserr_locks_held;
                goto out;
-       } else {
-               ret = nfsd4_free_lock_stateid(stp);
-               goto out;
+       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;
+       default:
+               ret = nfserr_bad_stateid;
        }
-
 out:
        nfs4_unlock_state();
        return ret;
@@ -3410,62 +3438,63 @@ setlkflg (int type)
                RD_STATE : WR_STATE;
 }
 
+static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
+{
+       struct svc_fh *current_fh = &cstate->current_fh;
+       struct nfs4_stateowner *sop = stp->st_stateowner;
+       __be32 status;
+
+       status = nfsd4_check_seqid(cstate, sop, seqid);
+       if (status)
+               return status;
+       if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
+               /*
+                * "Closed" stateid's exist *only* to return
+                * nfserr_replay_me from the previous step.
+                */
+               return nfserr_bad_stateid;
+       status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
+       if (status)
+               return status;
+       return nfs4_check_fh(current_fh, stp);
+}
+
 /* 
  * Checks for sequence id mutating operations. 
  */
 static __be32
 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
-                        stateid_t *stateid, int flags,
-                        struct nfs4_stateid **stpp)
+                        stateid_t *stateid, char typemask,
+                        struct nfs4_ol_stateid **stpp)
 {
-       struct nfs4_stateowner *sop;
-       struct svc_fh *current_fh = &cstate->current_fh;
        __be32 status;
+       struct nfs4_stid *s;
 
        dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
                seqid, STATEID_VAL(stateid));
 
        *stpp = NULL;
+       status = nfsd4_lookup_stateid(stateid, typemask, &s);
+       if (status)
+               return status;
+       *stpp = openlockstateid(s);
+       cstate->replay_owner = (*stpp)->st_stateowner;
 
-       if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
-               dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
-               return nfserr_bad_stateid;
-       }
-
-       if (STALE_STATEID(stateid))
-               return nfserr_stale_stateid;
-
-       /*
-       * We return BAD_STATEID if filehandle doesn't match stateid, 
-       * the confirmed flag is incorrecly set, or the generation 
-       * number is incorrect.  
-       */
-       *stpp = find_stateid(stateid, flags);
-       if (*stpp == NULL)
-               return nfserr_expired;
-
-       sop = (*stpp)->st_stateowner;
-       cstate->replay_owner = sop;
+       return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
+}
 
-       if (nfs4_check_fh(current_fh, *stpp)) {
-               dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
-               return nfserr_bad_stateid;
-       }
+static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
+{
+       __be32 status;
+       struct nfs4_openowner *oo;
 
-       status = nfsd4_check_seqid(cstate, sop, seqid);
+       status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
+                                               NFS4_OPEN_STID, stpp);
        if (status)
                return status;
-
-       if (sop->so_is_open_owner && !openowner(sop)->oo_confirmed
-                       && !(flags & CONFIRM)) {
-               dprintk("NFSD: preprocess_seqid_op: stateowner not"
-                               " confirmed yet!\n");
+       oo = openowner((*stpp)->st_stateowner);
+       if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
                return nfserr_bad_stateid;
-       }
-       status = check_stateid_generation(stateid, &(*stpp)->st_stateid, nfsd4_has_session(cstate));
-       if (status)
-               return status;
-       renew_client(sop->so_client);
        return nfs_ok;
 }
 
@@ -3475,7 +3504,7 @@ nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 {
        __be32 status;
        struct nfs4_openowner *oo;
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
 
        dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
                        (int)cstate->current_fh.fh_dentry->d_name.len,
@@ -3489,18 +3518,18 @@ nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
        status = nfs4_preprocess_seqid_op(cstate,
                                        oc->oc_seqid, &oc->oc_req_stateid,
-                                       CONFIRM | OPEN_STATE, &stp);
+                                       NFS4_OPEN_STID, &stp);
        if (status)
                goto out;
        oo = openowner(stp->st_stateowner);
        status = nfserr_bad_stateid;
-       if (oo->oo_confirmed)
+       if (oo->oo_flags & NFS4_OO_CONFIRMED)
                goto out;
-       oo->oo_confirmed = 1;
-       update_stateid(&stp->st_stateid);
-       memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
+       oo->oo_flags |= NFS4_OO_CONFIRMED;
+       update_stateid(&stp->st_stid.sc_stateid);
+       memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
        dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
-               __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
+               __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
 
        nfsd4_create_clid_dir(oo->oo_owner.so_client);
        status = nfs_ok;
@@ -3510,15 +3539,29 @@ out:
        return status;
 }
 
-static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
+static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
 {
-       int i;
+       if (!test_bit(access, &stp->st_access_bmap))
+               return;
+       nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
+       __clear_bit(access, &stp->st_access_bmap);
+}
 
-       for (i = 1; i < 4; i++) {
-               if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
-                       nfs4_file_put_access(stp->st_file, i);
-                       __clear_bit(i, &stp->st_access_bmap);
-               }
+static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
+{
+       switch (to_access) {
+       case NFS4_SHARE_ACCESS_READ:
+               nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
+               nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
+               break;
+       case NFS4_SHARE_ACCESS_WRITE:
+               nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
+               nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
+               break;
+       case NFS4_SHARE_ACCESS_BOTH:
+               break;
+       default:
+               BUG();
        }
 }
 
@@ -3538,19 +3581,18 @@ nfsd4_open_downgrade(struct svc_rqst *rqstp,
                     struct nfsd4_open_downgrade *od)
 {
        __be32 status;
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
 
        dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", 
                        (int)cstate->current_fh.fh_dentry->d_name.len,
                        cstate->current_fh.fh_dentry->d_name.name);
 
-       if (!access_valid(od->od_share_access, cstate->minorversion)
-                       || !deny_valid(od->od_share_deny))
-               return nfserr_inval;
+       /* We don't yet support WANT bits: */
+       od->od_share_access &= NFS4_SHARE_ACCESS_MASK;
 
        nfs4_lock_state();
-       status = nfs4_preprocess_seqid_op(cstate, od->od_seqid,
-                                       &od->od_stateid, OPEN_STATE, &stp);
+       status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
+                                       &od->od_stateid, &stp);
        if (status)
                goto out; 
        status = nfserr_inval;
@@ -3564,12 +3606,12 @@ nfsd4_open_downgrade(struct svc_rqst *rqstp,
                        stp->st_deny_bmap, od->od_share_deny);
                goto out;
        }
-       nfs4_file_downgrade(stp, od->od_share_access);
+       nfs4_stateid_downgrade(stp, od->od_share_access);
 
        reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
 
-       update_stateid(&stp->st_stateid);
-       memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
+       update_stateid(&stp->st_stid.sc_stateid);
+       memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
        status = nfs_ok;
 out:
        if (!cstate->replay_owner)
@@ -3577,6 +3619,32 @@ out:
        return status;
 }
 
+void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
+{
+       struct nfs4_openowner *oo;
+       struct nfs4_ol_stateid *s;
+
+       if (!so->so_is_open_owner)
+               return;
+       oo = openowner(so);
+       s = oo->oo_last_closed_stid;
+       if (!s)
+               return;
+       if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
+               /* Release the last_closed_stid on the next seqid bump: */
+               oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
+               return;
+       }
+       oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
+       release_last_closed_stateid(oo);
+}
+
+static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
+{
+       unhash_open_stateid(s);
+       s->st_stid.sc_type = NFS4_CLOSED_STID;
+}
+
 /*
  * nfs4_unlock_state() called after encode
  */
@@ -3586,41 +3654,26 @@ nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 {
        __be32 status;
        struct nfs4_openowner *oo;
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
 
        dprintk("NFSD: nfsd4_close on file %.*s\n", 
                        (int)cstate->current_fh.fh_dentry->d_name.len,
                        cstate->current_fh.fh_dentry->d_name.name);
 
        nfs4_lock_state();
-       /* check close_lru for replay */
        status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
-                                       &close->cl_stateid, 
-                                       OPEN_STATE, &stp);
-       if (stp == NULL && status == nfserr_expired) {
-               /*
-                * Also, we should make sure this isn't just the result of
-                * a replayed close:
-                */
-               oo = search_close_lru(close->cl_stateid.si_stateownerid);
-               /* It's not stale; let's assume it's expired: */
-               if (oo == NULL)
-                       goto out;
-               cstate->replay_owner = &oo->oo_owner;
-               status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
-               if (status)
-                       goto out;
-               status = nfserr_bad_seqid;
-       }
+                                       &close->cl_stateid,
+                                       NFS4_OPEN_STID|NFS4_CLOSED_STID,
+                                       &stp);
        if (status)
                goto out; 
        oo = openowner(stp->st_stateowner);
        status = nfs_ok;
-       update_stateid(&stp->st_stateid);
-       memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
+       update_stateid(&stp->st_stid.sc_stateid);
+       memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
 
-       /* release_stateid() calls nfsd_close() if needed */
-       release_open_stateid(stp);
+       nfsd4_close_open_stateid(stp);
+       oo->oo_last_closed_stid = stp;
 
        /* place unused nfs4_stateowners on so_close_lru list to be
         * released by the laundromat service after the lease period
@@ -3640,6 +3693,7 @@ nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 {
        struct nfs4_delegation *dp;
        stateid_t *stateid = &dr->dr_stateid;
+       struct nfs4_stid *s;
        struct inode *inode;
        __be32 status;
 
@@ -3648,23 +3702,13 @@ nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        inode = cstate->current_fh.fh_dentry->d_inode;
 
        nfs4_lock_state();
-       status = nfserr_bad_stateid;
-       if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
-               goto out;
-       status = nfserr_stale_stateid;
-       if (STALE_STATEID(stateid))
-               goto out;
-       status = nfserr_bad_stateid;
-       if (!is_delegation_stateid(stateid))
-               goto out;
-       status = nfserr_expired;
-       dp = find_delegation_stateid(inode, stateid);
-       if (!dp)
+       status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s);
+       if (status)
                goto out;
-       status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
+       dp = delegstateid(s);
+       status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
        if (status)
                goto out;
-       renew_client(dp->dl_client);
 
        unhash_delegation(dp);
 out:
@@ -3702,11 +3746,6 @@ last_byte_offset(u64 start, u64 len)
        return end > start ? end - 1: NFS4_MAX_UINT64;
 }
 
-static unsigned int lockownerid_hashval(u32 id)
-{
-       return id & LOCK_HASH_MASK;
-}
-
 static inline unsigned int
 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
                struct xdr_netobj *ownername)
@@ -3716,46 +3755,8 @@ lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
                & LOCK_HASH_MASK;
 }
 
-static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
 static struct list_head        lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
 
-static struct nfs4_delegation *
-search_for_delegation(stateid_t *stid)
-{
-       struct nfs4_file *fp;
-       struct nfs4_delegation *dp;
-       struct list_head *pos;
-       int i;
-
-       for (i = 0; i < FILE_HASH_SIZE; i++) {
-               list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
-                       list_for_each(pos, &fp->fi_delegations) {
-                               dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
-                               if (same_stateid(&dp->dl_stateid, stid))
-                                       return dp;
-                       }
-               }
-       }
-       return NULL;
-}
-
-static struct nfs4_delegation *
-find_delegation_stateid(struct inode *ino, stateid_t *stid)
-{
-       struct nfs4_file *fp;
-       struct nfs4_delegation *dl;
-
-       dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
-               STATEID_VAL(stid));
-
-       fp = find_file(ino);
-       if (!fp)
-               return NULL;
-       dl = find_delegation_file(fp, stid);
-       put_nfs4_file(fp);
-       return dl;
-}
-
 /*
  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
@@ -3822,12 +3823,8 @@ find_lockowner_str(struct inode *inode, clientid_t *clid,
        return NULL;
 }
 
-static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp)
+static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
 {
-       unsigned int idhashval;
-
-       idhashval = lockownerid_hashval(lo->lo_owner.so_id);
-       list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
        list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
        list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
 }
@@ -3841,7 +3838,7 @@ static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, s
  */
 
 static struct nfs4_lockowner *
-alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
+alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
        struct nfs4_lockowner *lo;
 
        lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
@@ -3856,36 +3853,24 @@ alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, str
        return lo;
 }
 
-static struct nfs4_stateid *
-alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
+static struct nfs4_ol_stateid *
+alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
 {
-       struct nfs4_stateid *stp;
-       unsigned int hashval = stateid_hashval(lo->lo_owner.so_id, fp->fi_id);
+       struct nfs4_ol_stateid *stp;
+       struct nfs4_client *clp = lo->lo_owner.so_client;
 
-       stp = nfs4_alloc_stateid();
+       stp = nfs4_alloc_stateid(clp);
        if (stp == NULL)
-               goto out;
-       INIT_LIST_HEAD(&stp->st_hash);
-       INIT_LIST_HEAD(&stp->st_perfile);
-       INIT_LIST_HEAD(&stp->st_perstateowner);
-       INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
-       list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
+               return NULL;
+       init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
        list_add(&stp->st_perfile, &fp->fi_stateids);
        list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
        stp->st_stateowner = &lo->lo_owner;
-       stp->st_type = NFS4_LOCK_STID;
        get_nfs4_file(fp);
        stp->st_file = fp;
-       stp->st_stateid.si_boot = boot_time;
-       stp->st_stateid.si_stateownerid = lo->lo_owner.so_id;
-       stp->st_stateid.si_fileid = fp->fi_id;
-       /* note will be incremented before first return to client: */
-       stp->st_stateid.si_generation = 0;
        stp->st_access_bmap = 0;
        stp->st_deny_bmap = open_stp->st_deny_bmap;
        stp->st_openstp = open_stp;
-
-out:
        return stp;
 }
 
@@ -3896,7 +3881,7 @@ check_lock_length(u64 offset, u64 length)
             LOFF_OVERFLOW(offset, length)));
 }
 
-static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
+static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
 {
        struct nfs4_file *fp = lock_stp->st_file;
        int oflag = nfs4_access_to_omode(access);
@@ -3916,7 +3901,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_stateid *lock_stp;
+       struct nfs4_ol_stateid *lock_stp;
        struct nfs4_file *fp;
        struct file *filp = NULL;
        struct file_lock file_lock;
@@ -3947,7 +3932,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                 * Use open owner and open stateid to create lock owner and
                 * lock stateid.
                 */
-               struct nfs4_stateid *open_stp = NULL;
+               struct nfs4_ol_stateid *open_stp = NULL;
                
                status = nfserr_stale_clientid;
                if (!nfsd4_has_session(cstate) &&
@@ -3955,10 +3940,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                        goto out;
 
                /* validate and update open stateid and open seqid */
-               status = nfs4_preprocess_seqid_op(cstate,
+               status = nfs4_preprocess_confirmed_seqid_op(cstate,
                                        lock->lk_new_open_seqid,
                                        &lock->lk_new_open_stateid,
-                                       OPEN_STATE, &open_stp);
+                                       &open_stp);
                if (status)
                        goto out;
                open_sop = openowner(open_stp->st_stateowner);
@@ -3988,7 +3973,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                status = nfs4_preprocess_seqid_op(cstate,
                                       lock->lk_old_lock_seqid,
                                       &lock->lk_old_lock_stateid,
-                                      LOCK_STATE, &lock_stp);
+                                      NFS4_LOCK_STID, &lock_stp);
                if (status)
                        goto out;
                lock_sop = lockowner(lock_stp->st_stateowner);
@@ -4050,8 +4035,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
        switch (-err) {
        case 0: /* success! */
-               update_stateid(&lock_stp->st_stateid);
-               memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid, 
+               update_stateid(&lock_stp->st_stid.sc_stateid);
+               memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, 
                                sizeof(stateid_t));
                status = 0;
                break;
@@ -4170,7 +4155,7 @@ __be32
 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
            struct nfsd4_locku *locku)
 {
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
        struct file *filp = NULL;
        struct file_lock file_lock;
        __be32 status;
@@ -4186,7 +4171,7 @@ nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        nfs4_lock_state();
                                                                                
        status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
-                                       &locku->lu_stateid, LOCK_STATE, &stp);
+                                       &locku->lu_stateid, NFS4_LOCK_STID, &stp);
        if (status)
                goto out;
        filp = find_any_file(stp->st_file);
@@ -4218,11 +4203,12 @@ nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        /*
        * OK, unlock succeeded; the only thing left to do is update the stateid.
        */
-       update_stateid(&stp->st_stateid);
-       memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
+       update_stateid(&stp->st_stid.sc_stateid);
+       memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
 
 out:
-       nfs4_unlock_state();
+       if (!cstate->replay_owner)
+               nfs4_unlock_state();
        return status;
 
 out_nfserr:
@@ -4262,7 +4248,7 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp,
        clientid_t *clid = &rlockowner->rl_clientid;
        struct nfs4_stateowner *sop;
        struct nfs4_lockowner *lo;
-       struct nfs4_stateid *stp;
+       struct nfs4_ol_stateid *stp;
        struct xdr_netobj *owner = &rlockowner->rl_owner;
        struct list_head matches;
        int i;
@@ -4286,7 +4272,7 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp,
         * data structures. */
        INIT_LIST_HEAD(&matches);
        for (i = 0; i < LOCK_HASH_SIZE; i++) {
-               list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
+               list_for_each_entry(sop, &lock_ownerstr_hashtbl[i], so_strhash) {
                        if (!same_owner_str(sop, owner, clid))
                                continue;
                        list_for_each_entry(stp, &sop->so_stateids,
@@ -4432,12 +4418,8 @@ nfs4_state_init(void)
        }
        for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
                INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
-               INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
        }
-       for (i = 0; i < STATEID_HASH_SIZE; i++)
-               INIT_LIST_HEAD(&stateid_hashtbl[i]);
        for (i = 0; i < LOCK_HASH_SIZE; i++) {
-               INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
                INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
        }
        memset(&onestateid, ~0, sizeof(stateid_t));