nfsd41: sequence operation
[safe/jmp/linux-2.6] / fs / nfsd / nfs4state.c
index 06b89df..24b6e05 100644 (file)
@@ -75,18 +75,21 @@ static stateid_t onestateid;              /* bits all 1 */
 /* forward declarations */
 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
-static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
 static void nfs4_set_recdir(char *recdir);
 
-/* Locking:
- *
- * client_mutex:
- *     protects clientid_hashtbl[], clientstr_hashtbl[],
- *     unconfstr_hashtbl[], uncofid_hashtbl[].
- */
+/* Locking: */
+
+/* Currently used for almost all code touching nfsv4 state: */
 static DEFINE_MUTEX(client_mutex);
 
+/*
+ * Currently used for the del_recall_lru and file hash table.  In an
+ * effort to decrease the scope of the client_mutex, this spinlock may
+ * eventually cover more:
+ */
+static DEFINE_SPINLOCK(recall_lock);
+
 static struct kmem_cache *stateowner_slab = NULL;
 static struct kmem_cache *file_slab = NULL;
 static struct kmem_cache *stateid_slab = NULL;
@@ -117,37 +120,23 @@ opaque_hashval(const void *ptr, int nbytes)
        return x;
 }
 
-/* forward declarations */
-static void release_stateowner(struct nfs4_stateowner *sop);
-static void release_stateid(struct nfs4_stateid *stp, int flags);
-
-/*
- * Delegation state
- */
-
-/* recall_lock protects the del_recall_lru */
-static DEFINE_SPINLOCK(recall_lock);
 static struct list_head del_recall_lru;
 
-static void
-free_nfs4_file(struct kref *kref)
-{
-       struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
-       list_del(&fp->fi_hash);
-       iput(fp->fi_inode);
-       kmem_cache_free(file_slab, fp);
-}
-
 static inline void
 put_nfs4_file(struct nfs4_file *fi)
 {
-       kref_put(&fi->fi_ref, free_nfs4_file);
+       if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
+               list_del(&fi->fi_hash);
+               spin_unlock(&recall_lock);
+               iput(fi->fi_inode);
+               kmem_cache_free(file_slab, fi);
+       }
 }
 
 static inline void
 get_nfs4_file(struct nfs4_file *fi)
 {
-       kref_get(&fi->fi_ref);
+       atomic_inc(&fi->fi_ref);
 }
 
 static int num_delegations;
@@ -220,9 +209,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_f
        dp->dl_stateid.si_stateownerid = current_delegid++;
        dp->dl_stateid.si_fileid = 0;
        dp->dl_stateid.si_generation = 0;
-       dp->dl_fhlen = current_fh->fh_handle.fh_size;
-       memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
-                       current_fh->fh_handle.fh_size);
+       fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
        dp->dl_time = 0;
        atomic_set(&dp->dl_count, 1);
        list_add(&dp->dl_perfile, &fp->fi_delegations);
@@ -311,6 +298,159 @@ static struct list_head   unconf_id_hashtbl[CLIENT_HASH_SIZE];
 static struct list_head client_lru;
 static struct list_head close_lru;
 
+static void unhash_generic_stateid(struct nfs4_stateid *stp)
+{
+       list_del(&stp->st_hash);
+       list_del(&stp->st_perfile);
+       list_del(&stp->st_perstateowner);
+}
+
+static void free_generic_stateid(struct nfs4_stateid *stp)
+{
+       put_nfs4_file(stp->st_file);
+       kmem_cache_free(stateid_slab, stp);
+}
+
+static void release_lock_stateid(struct nfs4_stateid *stp)
+{
+       unhash_generic_stateid(stp);
+       locks_remove_posix(stp->st_vfs_file, (fl_owner_t)stp->st_stateowner);
+       free_generic_stateid(stp);
+}
+
+static void unhash_lockowner(struct nfs4_stateowner *sop)
+{
+       struct nfs4_stateid *stp;
+
+       list_del(&sop->so_idhash);
+       list_del(&sop->so_strhash);
+       list_del(&sop->so_perstateid);
+       while (!list_empty(&sop->so_stateids)) {
+               stp = list_first_entry(&sop->so_stateids,
+                               struct nfs4_stateid, st_perstateowner);
+               release_lock_stateid(stp);
+       }
+}
+
+static void release_lockowner(struct nfs4_stateowner *sop)
+{
+       unhash_lockowner(sop);
+       nfs4_put_stateowner(sop);
+}
+
+static void
+release_stateid_lockowners(struct nfs4_stateid *open_stp)
+{
+       struct nfs4_stateowner *lock_sop;
+
+       while (!list_empty(&open_stp->st_lockowners)) {
+               lock_sop = list_entry(open_stp->st_lockowners.next,
+                               struct nfs4_stateowner, so_perstateid);
+               /* list_del(&open_stp->st_lockowners);  */
+               BUG_ON(lock_sop->so_is_open_owner);
+               release_lockowner(lock_sop);
+       }
+}
+
+static void release_open_stateid(struct nfs4_stateid *stp)
+{
+       unhash_generic_stateid(stp);
+       release_stateid_lockowners(stp);
+       nfsd_close(stp->st_vfs_file);
+       free_generic_stateid(stp);
+}
+
+static void unhash_openowner(struct nfs4_stateowner *sop)
+{
+       struct nfs4_stateid *stp;
+
+       list_del(&sop->so_idhash);
+       list_del(&sop->so_strhash);
+       list_del(&sop->so_perclient);
+       list_del(&sop->so_perstateid); /* XXX: necessary? */
+       while (!list_empty(&sop->so_stateids)) {
+               stp = list_first_entry(&sop->so_stateids,
+                               struct nfs4_stateid, st_perstateowner);
+               release_open_stateid(stp);
+       }
+}
+
+static void release_openowner(struct nfs4_stateowner *sop)
+{
+       unhash_openowner(sop);
+       list_del(&sop->so_close_lru);
+       nfs4_put_stateowner(sop);
+}
+
+static DEFINE_SPINLOCK(sessionid_lock);
+#define SESSION_HASH_SIZE      512
+static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
+
+static inline int
+hash_sessionid(struct nfs4_sessionid *sessionid)
+{
+       struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
+
+       return sid->sequence % SESSION_HASH_SIZE;
+}
+
+static inline void
+dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
+{
+       u32 *ptr = (u32 *)(&sessionid->data[0]);
+       dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
+}
+
+/* caller must hold sessionid_lock */
+static struct nfsd4_session *
+find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
+{
+       struct nfsd4_session *elem;
+       int idx;
+
+       dump_sessionid(__func__, sessionid);
+       idx = hash_sessionid(sessionid);
+       dprintk("%s: idx is %d\n", __func__, idx);
+       /* Search in the appropriate list */
+       list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
+               dump_sessionid("list traversal", &elem->se_sessionid);
+               if (!memcmp(elem->se_sessionid.data, sessionid->data,
+                           NFS4_MAX_SESSIONID_LEN)) {
+                       return elem;
+               }
+       }
+
+       dprintk("%s: session not found\n", __func__);
+       return NULL;
+}
+
+/* caller must hold sessionid_lock */
+static void
+unhash_session(struct nfsd4_session *ses)
+{
+       list_del(&ses->se_hash);
+       list_del(&ses->se_perclnt);
+}
+
+static void
+release_session(struct nfsd4_session *ses)
+{
+       spin_lock(&sessionid_lock);
+       unhash_session(ses);
+       spin_unlock(&sessionid_lock);
+       nfsd4_put_session(ses);
+}
+
+void
+free_session(struct kref *kref)
+{
+       struct nfsd4_session *ses;
+
+       ses = container_of(kref, struct nfsd4_session, se_ref);
+       kfree(ses->se_slots);
+       kfree(ses);
+}
+
 static inline void
 renew_client(struct nfs4_client *clp)
 {
@@ -420,7 +560,13 @@ expire_client(struct nfs4_client *clp)
        list_del(&clp->cl_lru);
        while (!list_empty(&clp->cl_openowners)) {
                sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
-               release_stateowner(sop);
+               release_openowner(sop);
+       }
+       while (!list_empty(&clp->cl_sessions)) {
+               struct nfsd4_session  *ses;
+               ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
+                                se_perclnt);
+               release_session(ses);
        }
        put_nfs4_client(clp);
 }
@@ -439,6 +585,7 @@ static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
        INIT_LIST_HEAD(&clp->cl_strhash);
        INIT_LIST_HEAD(&clp->cl_openowners);
        INIT_LIST_HEAD(&clp->cl_delegations);
+       INIT_LIST_HEAD(&clp->cl_sessions);
        INIT_LIST_HEAD(&clp->cl_lru);
        return clp;
 }
@@ -568,25 +715,45 @@ find_unconfirmed_client(clientid_t *clid)
        return NULL;
 }
 
+/*
+ * Return 1 iff clp's clientid establishment method matches the use_exchange_id
+ * parameter. Matching is based on the fact the at least one of the
+ * EXCHGID4_FLAG_USE_{NON_PNFS,PNFS_MDS,PNFS_DS} flags must be set for v4.1
+ *
+ * FIXME: we need to unify the clientid namespaces for nfsv4.x
+ * and correctly deal with client upgrade/downgrade in EXCHANGE_ID
+ * and SET_CLIENTID{,_CONFIRM}
+ */
+static inline int
+match_clientid_establishment(struct nfs4_client *clp, bool use_exchange_id)
+{
+       bool has_exchange_flags = (clp->cl_exchange_flags != 0);
+       return use_exchange_id == has_exchange_flags;
+}
+
 static struct nfs4_client *
-find_confirmed_client_by_str(const char *dname, unsigned int hashval)
+find_confirmed_client_by_str(const char *dname, unsigned int hashval,
+                            bool use_exchange_id)
 {
        struct nfs4_client *clp;
 
        list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
-               if (same_name(clp->cl_recdir, dname))
+               if (same_name(clp->cl_recdir, dname) &&
+                   match_clientid_establishment(clp, use_exchange_id))
                        return clp;
        }
        return NULL;
 }
 
 static struct nfs4_client *
-find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
+find_unconfirmed_client_by_str(const char *dname, unsigned int hashval,
+                              bool use_exchange_id)
 {
        struct nfs4_client *clp;
 
        list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
-               if (same_name(clp->cl_recdir, dname))
+               if (same_name(clp->cl_recdir, dname) &&
+                   match_clientid_establishment(clp, use_exchange_id))
                        return clp;
        }
        return NULL;
@@ -685,6 +852,247 @@ out_err:
        return;
 }
 
+/*
+ * Set the exchange_id flags returned by the server.
+ */
+static void
+nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
+{
+       /* pNFS is not supported */
+       new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
+
+       /* Referrals are supported, Migration is not. */
+       new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
+
+       /* set the wire flags to return to client. */
+       clid->flags = new->cl_exchange_flags;
+}
+
+__be32
+nfsd4_exchange_id(struct svc_rqst *rqstp,
+                 struct nfsd4_compound_state *cstate,
+                 struct nfsd4_exchange_id *exid)
+{
+       struct nfs4_client *unconf, *conf, *new;
+       int status;
+       unsigned int            strhashval;
+       char                    dname[HEXDIR_LEN];
+       nfs4_verifier           verf = exid->verifier;
+       u32                     ip_addr = svc_addr_in(rqstp)->sin_addr.s_addr;
+
+       dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
+               " ip_addr=%u flags %x, spa_how %d\n",
+               __func__, rqstp, exid, exid->clname.len, exid->clname.data,
+               ip_addr, exid->flags, exid->spa_how);
+
+       if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
+               return nfserr_inval;
+
+       /* Currently only support SP4_NONE */
+       switch (exid->spa_how) {
+       case SP4_NONE:
+               break;
+       case SP4_SSV:
+               return nfserr_encr_alg_unsupp;
+       default:
+               BUG();                          /* checked by xdr code */
+       case SP4_MACH_CRED:
+               return nfserr_serverfault;      /* no excuse :-/ */
+       }
+
+       status = nfs4_make_rec_clidname(dname, &exid->clname);
+
+       if (status)
+               goto error;
+
+       strhashval = clientstr_hashval(dname);
+
+       nfs4_lock_state();
+       status = nfs_ok;
+
+       conf = find_confirmed_client_by_str(dname, strhashval, true);
+       if (conf) {
+               if (!same_verf(&verf, &conf->cl_verifier)) {
+                       /* 18.35.4 case 8 */
+                       if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
+                               status = nfserr_not_same;
+                               goto out;
+                       }
+                       /* Client reboot: destroy old state */
+                       expire_client(conf);
+                       goto out_new;
+               }
+               if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
+                       /* 18.35.4 case 9 */
+                       if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
+                               status = nfserr_perm;
+                               goto out;
+                       }
+                       expire_client(conf);
+                       goto out_new;
+               }
+               if (ip_addr != conf->cl_addr &&
+                   !(exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A)) {
+                       /* Client collision. 18.35.4 case 3 */
+                       status = nfserr_clid_inuse;
+                       goto out;
+               }
+               /*
+                * Set bit when the owner id and verifier map to an already
+                * confirmed client id (18.35.3).
+                */
+               exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
+
+               /*
+                * Falling into 18.35.4 case 2, possible router replay.
+                * Leave confirmed record intact and return same result.
+                */
+               copy_verf(conf, &verf);
+               new = conf;
+               goto out_copy;
+       } else {
+               /* 18.35.4 case 7 */
+               if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
+                       status = nfserr_noent;
+                       goto out;
+               }
+       }
+
+       unconf  = find_unconfirmed_client_by_str(dname, strhashval, true);
+       if (unconf) {
+               /*
+                * Possible retry or client restart.  Per 18.35.4 case 4,
+                * a new unconfirmed record should be generated regardless
+                * of whether any properties have changed.
+                */
+               expire_client(unconf);
+       }
+
+out_new:
+       /* Normal case */
+       new = create_client(exid->clname, dname);
+       if (new == NULL) {
+               status = nfserr_resource;
+               goto out;
+       }
+
+       copy_verf(new, &verf);
+       copy_cred(&new->cl_cred, &rqstp->rq_cred);
+       new->cl_addr = ip_addr;
+       gen_clid(new);
+       gen_confirm(new);
+       add_to_unconfirmed(new, strhashval);
+out_copy:
+       exid->clientid.cl_boot = new->cl_clientid.cl_boot;
+       exid->clientid.cl_id = new->cl_clientid.cl_id;
+
+       new->cl_seqid = exid->seqid = 1;
+       nfsd4_set_ex_flags(new, exid);
+
+       dprintk("nfsd4_exchange_id seqid %d flags %x\n",
+               new->cl_seqid, new->cl_exchange_flags);
+       status = nfs_ok;
+
+out:
+       nfs4_unlock_state();
+error:
+       dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
+       return status;
+}
+
+static int
+check_slot_seqid(u32 seqid, struct nfsd4_slot *slot)
+{
+       dprintk("%s enter. seqid %d slot->sl_seqid %d\n", __func__, seqid,
+               slot->sl_seqid);
+
+       /* The slot is in use, and no response has been sent. */
+       if (slot->sl_inuse) {
+               if (seqid == slot->sl_seqid)
+                       return nfserr_jukebox;
+               else
+                       return nfserr_seq_misordered;
+       }
+       /* Normal */
+       if (likely(seqid == slot->sl_seqid + 1))
+               return nfs_ok;
+       /* Replay */
+       if (seqid == slot->sl_seqid)
+               return nfserr_replay_cache;
+       /* Wraparound */
+       if (seqid == 1 && (slot->sl_seqid + 1) == 0)
+               return nfs_ok;
+       /* Misordered replay or misordered new request */
+       return nfserr_seq_misordered;
+}
+
+__be32
+nfsd4_create_session(struct svc_rqst *rqstp,
+                    struct nfsd4_compound_state *cstate,
+                    struct nfsd4_create_session *cr_ses)
+{
+       return -1;      /* stub */
+}
+
+__be32
+nfsd4_destroy_session(struct svc_rqst *r,
+                     struct nfsd4_compound_state *cstate,
+                     struct nfsd4_destroy_session *sessionid)
+{
+       return -1;      /* stub */
+}
+
+__be32
+nfsd4_sequence(struct svc_rqst *rqstp,
+              struct nfsd4_compound_state *cstate,
+              struct nfsd4_sequence *seq)
+{
+       struct nfsd4_session *session;
+       struct nfsd4_slot *slot;
+       int status;
+
+       spin_lock(&sessionid_lock);
+       status = nfserr_badsession;
+       session = find_in_sessionid_hashtbl(&seq->sessionid);
+       if (!session)
+               goto out;
+
+       status = nfserr_badslot;
+       if (seq->slotid >= session->se_fnumslots)
+               goto out;
+
+       slot = &session->se_slots[seq->slotid];
+       dprintk("%s: slotid %d\n", __func__, seq->slotid);
+
+       status = check_slot_seqid(seq->seqid, slot);
+       if (status == nfserr_replay_cache) {
+               cstate->slot = slot;
+               cstate->session = session;
+               goto replay_cache;
+       }
+       if (status)
+               goto out;
+
+       /* Success! bump slot seqid */
+       slot->sl_inuse = true;
+       slot->sl_seqid = seq->seqid;
+
+       cstate->slot = slot;
+       cstate->session = session;
+
+replay_cache:
+       /* Renew the clientid on success and on replay.
+        * Hold a session reference until done processing the compound:
+        * nfsd4_put_session called only if the cstate slot is set.
+        */
+       renew_client(session->se_client);
+       nfsd4_get_session(session);
+out:
+       spin_unlock(&sessionid_lock);
+       dprintk("%s: return %d\n", __func__, ntohl(status));
+       return status;
+}
+
 __be32
 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                  struct nfsd4_setclientid *setclid)
@@ -716,14 +1124,13 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        strhashval = clientstr_hashval(dname);
 
        nfs4_lock_state();
-       conf = find_confirmed_client_by_str(dname, strhashval);
+       conf = find_confirmed_client_by_str(dname, strhashval, false);
        if (conf) {
                /* RFC 3530 14.2.33 CASE 0: */
                status = nfserr_clid_inuse;
-               if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)
-                               || conf->cl_addr != sin->sin_addr.s_addr) {
-                       dprintk("NFSD: setclientid: string in use by clientat %pI4\n",
-                               &conf->cl_addr);
+               if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
+                       dprintk("NFSD: setclientid: string in use by client"
+                               " at %pI4\n", &conf->cl_addr);
                        goto out;
                }
        }
@@ -732,7 +1139,7 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
         * has a description of SETCLIENTID request processing consisting
         * of 5 bullet points, labeled as CASE0 - CASE4 below.
         */
-       unconf = find_unconfirmed_client_by_str(dname, strhashval);
+       unconf = find_unconfirmed_client_by_str(dname, strhashval, false);
        status = nfserr_resource;
        if (!conf) {
                /*
@@ -887,7 +1294,7 @@ nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
                        unsigned int hash =
                                clientstr_hashval(unconf->cl_recdir);
                        conf = find_confirmed_client_by_str(unconf->cl_recdir,
-                                                                       hash);
+                                                           hash, false);
                        if (conf) {
                                nfsd4_remove_clid_dir(conf);
                                expire_client(conf);
@@ -923,11 +1330,13 @@ alloc_init_file(struct inode *ino)
 
        fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
        if (fp) {
-               kref_init(&fp->fi_ref);
+               atomic_set(&fp->fi_ref, 1);
                INIT_LIST_HEAD(&fp->fi_hash);
                INIT_LIST_HEAD(&fp->fi_stateids);
                INIT_LIST_HEAD(&fp->fi_delegations);
+               spin_lock(&recall_lock);
                list_add(&fp->fi_hash, &file_hashtbl[hashval]);
+               spin_unlock(&recall_lock);
                fp->fi_inode = igrab(ino);
                fp->fi_id = current_fileid++;
                fp->fi_had_conflict = false;
@@ -1037,48 +1446,6 @@ alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, str
        return sop;
 }
 
-static void
-release_stateid_lockowners(struct nfs4_stateid *open_stp)
-{
-       struct nfs4_stateowner *lock_sop;
-
-       while (!list_empty(&open_stp->st_lockowners)) {
-               lock_sop = list_entry(open_stp->st_lockowners.next,
-                               struct nfs4_stateowner, so_perstateid);
-               /* list_del(&open_stp->st_lockowners);  */
-               BUG_ON(lock_sop->so_is_open_owner);
-               release_stateowner(lock_sop);
-       }
-}
-
-static void
-unhash_stateowner(struct nfs4_stateowner *sop)
-{
-       struct nfs4_stateid *stp;
-
-       list_del(&sop->so_idhash);
-       list_del(&sop->so_strhash);
-       if (sop->so_is_open_owner)
-               list_del(&sop->so_perclient);
-       list_del(&sop->so_perstateid);
-       while (!list_empty(&sop->so_stateids)) {
-               stp = list_entry(sop->so_stateids.next,
-                       struct nfs4_stateid, st_perstateowner);
-               if (sop->so_is_open_owner)
-                       release_stateid(stp, OPEN_STATE);
-               else
-                       release_stateid(stp, LOCK_STATE);
-       }
-}
-
-static void
-release_stateowner(struct nfs4_stateowner *sop)
-{
-       unhash_stateowner(sop);
-       list_del(&sop->so_close_lru);
-       nfs4_put_stateowner(sop);
-}
-
 static inline void
 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
        struct nfs4_stateowner *sop = open->op_stateowner;
@@ -1106,24 +1473,6 @@ init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *
 }
 
 static void
-release_stateid(struct nfs4_stateid *stp, int flags)
-{
-       struct file *filp = stp->st_vfs_file;
-
-       list_del(&stp->st_hash);
-       list_del(&stp->st_perfile);
-       list_del(&stp->st_perstateowner);
-       if (flags & OPEN_STATE) {
-               release_stateid_lockowners(stp);
-               stp->st_vfs_file = NULL;
-               nfsd_close(filp);
-       } else if (flags & LOCK_STATE)
-               locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
-       put_nfs4_file(stp->st_file);
-       kmem_cache_free(stateid_slab, stp);
-}
-
-static void
 move_to_close_lru(struct nfs4_stateowner *sop)
 {
        dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
@@ -1160,12 +1509,15 @@ find_file(struct inode *ino)
        unsigned int hashval = file_hashval(ino);
        struct nfs4_file *fp;
 
+       spin_lock(&recall_lock);
        list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
                if (fp->fi_inode == ino) {
                        get_nfs4_file(fp);
+                       spin_unlock(&recall_lock);
                        return fp;
                }
        }
+       spin_unlock(&recall_lock);
        return NULL;
 }
 
@@ -1435,7 +1787,7 @@ nfsd4_process_open1(struct nfsd4_open *open)
        if (!sop->so_confirmed) {
                /* Replace unconfirmed owners without checking for replay. */
                clp = sop->so_client;
-               release_stateowner(sop);
+               release_openowner(sop);
                open->op_stateowner = NULL;
                goto renew;
        }
@@ -1764,7 +2116,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
                init_stateid(stp, fp, open);
                status = nfsd4_truncate(rqstp, current_fh, open);
                if (status) {
-                       release_stateid(stp, OPEN_STATE);
+                       release_open_stateid(stp);
                        goto out;
                }
        }
@@ -1898,7 +2250,7 @@ nfs4_laundromat(void)
                }
                dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
                        sop->so_id);
-               release_stateowner(sop);
+               release_openowner(sop);
        }
        if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
                clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
@@ -1983,10 +2335,7 @@ out:
 static inline __be32
 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
 {
-       /* Trying to call delegreturn with a special stateid? Yuch: */
-       if (!(flags & (RD_STATE | WR_STATE)))
-               return nfserr_bad_stateid;
-       else if (ONE_STATEID(stateid) && (flags & RD_STATE))
+       if (ONE_STATEID(stateid) && (flags & RD_STATE))
                return nfs_ok;
        else if (locks_in_grace()) {
                /* Answer in remaining cases depends on existance of
@@ -2005,10 +2354,9 @@ check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
  * that are not able to provide mandatory locking.
  */
 static inline int
-io_during_grace_disallowed(struct inode *inode, int flags)
+grace_disallows_io(struct inode *inode)
 {
-       return locks_in_grace() && (flags & (RD_STATE | WR_STATE))
-               && mandatory_lock(inode);
+       return locks_in_grace() && mandatory_lock(inode);
 }
 
 static int check_stateid_generation(stateid_t *in, stateid_t *ref)
@@ -2031,6 +2379,11 @@ static int check_stateid_generation(stateid_t *in, stateid_t *ref)
        return nfs_ok;
 }
 
+static int is_delegation_stateid(stateid_t *stateid)
+{
+       return stateid->si_fileid == 0;
+}
+
 /*
 * Checks for stateid operations
 */
@@ -2039,63 +2392,53 @@ nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int fl
 {
        struct nfs4_stateid *stp = NULL;
        struct nfs4_delegation *dp = NULL;
-       stateid_t *stidp;
        struct inode *ino = current_fh->fh_dentry->d_inode;
        __be32 status;
 
-       dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
-               stateid->si_boot, stateid->si_stateownerid, 
-               stateid->si_fileid, stateid->si_generation); 
        if (filpp)
                *filpp = NULL;
 
-       if (io_during_grace_disallowed(ino, flags))
+       if (grace_disallows_io(ino))
                return nfserr_grace;
 
        if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
                return check_special_stateids(current_fh, stateid, flags);
 
-       /* STALE STATEID */
        status = nfserr_stale_stateid;
        if (STALE_STATEID(stateid)) 
                goto out;
 
-       /* BAD STATEID */
        status = nfserr_bad_stateid;
-       if (!stateid->si_fileid) { /* delegation stateid */
-               if(!(dp = find_delegation_stateid(ino, stateid))) {
-                       dprintk("NFSD: delegation stateid not found\n");
+       if (is_delegation_stateid(stateid)) {
+               dp = find_delegation_stateid(ino, stateid);
+               if (!dp)
                        goto out;
-               }
-               stidp = &dp->dl_stateid;
+               status = check_stateid_generation(stateid, &dp->dl_stateid);
+               if (status)
+                       goto out;
+               status = nfs4_check_delegmode(dp, flags);
+               if (status)
+                       goto out;
+               renew_client(dp->dl_client);
+               if (filpp)
+                       *filpp = dp->dl_vfs_file;
        } else { /* open or lock stateid */
-               if (!(stp = find_stateid(stateid, flags))) {
-                       dprintk("NFSD: open or lock stateid not found\n");
+               stp = find_stateid(stateid, flags);
+               if (!stp)
                        goto out;
-               }
-               if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
+               if (nfs4_check_fh(current_fh, stp))
                        goto out;
                if (!stp->st_stateowner->so_confirmed)
                        goto out;
-               stidp = &stp->st_stateid;
-       }
-       status = check_stateid_generation(stateid, stidp);
-       if (status)
-               goto out;
-       if (stp) {
-               if ((status = nfs4_check_openmode(stp,flags)))
+               status = check_stateid_generation(stateid, &stp->st_stateid);
+               if (status)
+                       goto out;
+               status = nfs4_check_openmode(stp, flags);
+               if (status)
                        goto out;
                renew_client(stp->st_stateowner->so_client);
                if (filpp)
                        *filpp = stp->st_vfs_file;
-       } else {
-               if ((status = nfs4_check_delegmode(dp, flags)))
-                       goto out;
-               renew_client(dp->dl_client);
-               if (flags & DELEG_RET)
-                       unhash_delegation(dp);
-               if (filpp)
-                       *filpp = dp->dl_vfs_file;
        }
        status = nfs_ok;
 out:
@@ -2373,7 +2716,7 @@ nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
 
        /* release_stateid() calls nfsd_close() if needed */
-       release_stateid(stp, OPEN_STATE);
+       release_open_stateid(stp);
 
        /* place unused nfs4_stateowners on so_close_lru list to be
         * released by the laundromat service after the lease period
@@ -2394,16 +2737,37 @@ __be32
 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                  struct nfsd4_delegreturn *dr)
 {
+       struct nfs4_delegation *dp;
+       stateid_t *stateid = &dr->dr_stateid;
+       struct inode *inode;
        __be32 status;
 
        if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
-               goto out;
+               return status;
+       inode = cstate->current_fh.fh_dentry->d_inode;
 
        nfs4_lock_state();
-       status = nfs4_preprocess_stateid_op(&cstate->current_fh,
-                                           &dr->dr_stateid, DELEG_RET, NULL);
-       nfs4_unlock_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;
+       dp = find_delegation_stateid(inode, stateid);
+       if (!dp)
+               goto out;
+       status = check_stateid_generation(stateid, &dp->dl_stateid);
+       if (status)
+               goto out;
+       renew_client(dp->dl_client);
+
+       unhash_delegation(dp);
 out:
+       nfs4_unlock_state();
+
        return status;
 }
 
@@ -2416,6 +2780,26 @@ out:
 #define LOCK_HASH_SIZE             (1 << LOCK_HASH_BITS)
 #define LOCK_HASH_MASK             (LOCK_HASH_SIZE - 1)
 
+static inline u64
+end_offset(u64 start, u64 len)
+{
+       u64 end;
+
+       end = start + len;
+       return end >= start ? end: NFS4_MAX_UINT64;
+}
+
+/* last octet in a range */
+static inline u64
+last_byte_offset(u64 start, u64 len)
+{
+       u64 end;
+
+       BUG_ON(!len);
+       end = start + len;
+       return end > start ? end - 1: NFS4_MAX_UINT64;
+}
+
 #define lockownerid_hashval(id) \
         ((id) & LOCK_HASH_MASK)
 
@@ -2519,8 +2903,8 @@ nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
                deny->ld_clientid.cl_id = 0;
        }
        deny->ld_start = fl->fl_start;
-       deny->ld_length = ~(u64)0;
-       if (fl->fl_end != ~(u64)0)
+       deny->ld_length = NFS4_MAX_UINT64;
+       if (fl->fl_end != NFS4_MAX_UINT64)
                deny->ld_length = fl->fl_end - fl->fl_start + 1;        
        deny->ld_type = NFS4_READ_LT;
        if (fl->fl_type != F_RDLCK)
@@ -2617,7 +3001,7 @@ out:
 static int
 check_lock_length(u64 offset, u64 length)
 {
-       return ((length == 0)  || ((length != ~(u64)0) &&
+       return ((length == 0)  || ((length != NFS4_MAX_UINT64) &&
             LOFF_OVERFLOW(offset, length)));
 }
 
@@ -2737,11 +3121,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        file_lock.fl_lmops = &nfsd_posix_mng_ops;
 
        file_lock.fl_start = lock->lk_offset;
-       if ((lock->lk_length == ~(u64)0) || 
-                       LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
-               file_lock.fl_end = ~(u64)0;
-       else
-               file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
+       file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
        nfs4_transform_lock_offset(&file_lock);
 
        /*
@@ -2772,7 +3152,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        }
 out:
        if (status && lock->lk_is_new && lock_sop)
-               release_stateowner(lock_sop);
+               release_lockowner(lock_sop);
        if (lock->lk_replay_owner) {
                nfs4_get_stateowner(lock->lk_replay_owner);
                cstate->replay_owner = lock->lk_replay_owner;
@@ -2782,6 +3162,25 @@ out:
 }
 
 /*
+ * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
+ * so we do a temporary open here just to get an open file to pass to
+ * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
+ * inode operation.)
+ */
+static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
+{
+       struct file *file;
+       int err;
+
+       err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
+       if (err)
+               return err;
+       err = vfs_test_lock(file, lock);
+       nfsd_close(file);
+       return err;
+}
+
+/*
  * LOCKT operation
  */
 __be32
@@ -2789,7 +3188,6 @@ nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
            struct nfsd4_lockt *lockt)
 {
        struct inode *inode;
-       struct file file;
        struct file_lock file_lock;
        int error;
        __be32 status;
@@ -2837,26 +3235,14 @@ nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
        file_lock.fl_pid = current->tgid;
        file_lock.fl_flags = FL_POSIX;
-       file_lock.fl_lmops = &nfsd_posix_mng_ops;
 
        file_lock.fl_start = lockt->lt_offset;
-       if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
-               file_lock.fl_end = ~(u64)0;
-       else
-               file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
+       file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
 
        nfs4_transform_lock_offset(&file_lock);
 
-       /* vfs_test_lock uses the struct file _only_ to resolve the inode.
-        * since LOCKT doesn't require an OPEN, and therefore a struct
-        * file may not exist, pass vfs_test_lock a struct file with
-        * only the dentry:inode set.
-        */
-       memset(&file, 0, sizeof (struct file));
-       file.f_path.dentry = cstate->current_fh.fh_dentry;
-
        status = nfs_ok;
-       error = vfs_test_lock(&file, &file_lock);
+       error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
        if (error) {
                status = nfserrno(error);
                goto out;
@@ -2907,10 +3293,7 @@ nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        file_lock.fl_lmops = &nfsd_posix_mng_ops;
        file_lock.fl_start = locku->lu_offset;
 
-       if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
-               file_lock.fl_end = ~(u64)0;
-       else
-               file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
+       file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
        nfs4_transform_lock_offset(&file_lock);
 
        /*
@@ -3018,7 +3401,7 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp,
                /* unhash_stateowner deletes so_perclient only
                 * for openowners. */
                list_del(&sop->so_perclient);
-               release_stateowner(sop);
+               release_lockowner(sop);
        }
 out:
        nfs4_unlock_state();
@@ -3032,12 +3415,12 @@ alloc_reclaim(void)
 }
 
 int
-nfs4_has_reclaimed_state(const char *name)
+nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
 {
        unsigned int strhashval = clientstr_hashval(name);
        struct nfs4_client *clp;
 
-       clp = find_confirmed_client_by_str(name, strhashval);
+       clp = find_confirmed_client_by_str(name, strhashval, use_exchange_id);
        return clp ? 1 : 0;
 }
 
@@ -3134,6 +3517,8 @@ nfs4_state_init(void)
                INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
                INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
        }
+       for (i = 0; i < SESSION_HASH_SIZE; i++)
+               INIT_LIST_HEAD(&sessionid_hashtbl[i]);
        for (i = 0; i < FILE_HASH_SIZE; i++) {
                INIT_LIST_HEAD(&file_hashtbl[i]);
        }