nfsd: fix error values returned by nfsd4_lockt() when nfsd_open() fails
[pandora-kernel.git] / fs / nfsd / nfs4state.c
index c5cddd6..7f71c69 100644 (file)
@@ -58,11 +58,15 @@ static const stateid_t one_stateid = {
 static const stateid_t zero_stateid = {
        /* all fields zero */
 };
+static const stateid_t currentstateid = {
+       .si_generation = 1,
+};
 
 static u64 current_sessionid = 1;
 
 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
 #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
+#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
 
 /* forward declarations */
 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
@@ -91,6 +95,19 @@ nfs4_lock_state(void)
        mutex_lock(&client_mutex);
 }
 
+static void free_session(struct kref *);
+
+/* Must be called under the client_lock */
+static void nfsd4_put_session_locked(struct nfsd4_session *ses)
+{
+       kref_put(&ses->se_ref, free_session);
+}
+
+static void nfsd4_get_session(struct nfsd4_session *ses)
+{
+       kref_get(&ses->se_ref);
+}
+
 void
 nfs4_unlock_state(void)
 {
@@ -605,12 +622,20 @@ hash_sessionid(struct nfs4_sessionid *sessionid)
        return sid->sequence % SESSION_HASH_SIZE;
 }
 
+#ifdef NFSD_DEBUG
 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]);
 }
+#else
+static inline void
+dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
+{
+}
+#endif
+
 
 static void
 gen_sessionid(struct nfsd4_session *ses)
@@ -832,11 +857,12 @@ static void nfsd4_del_conns(struct nfsd4_session *s)
        spin_unlock(&clp->cl_lock);
 }
 
-void free_session(struct kref *kref)
+static void free_session(struct kref *kref)
 {
        struct nfsd4_session *ses;
        int mem;
 
+       BUG_ON(!spin_is_locked(&client_lock));
        ses = container_of(kref, struct nfsd4_session, se_ref);
        nfsd4_del_conns(ses);
        spin_lock(&nfsd_drc_lock);
@@ -847,6 +873,13 @@ void free_session(struct kref *kref)
        kfree(ses);
 }
 
+void nfsd4_put_session(struct nfsd4_session *ses)
+{
+       spin_lock(&client_lock);
+       nfsd4_put_session_locked(ses);
+       spin_unlock(&client_lock);
+}
+
 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
 {
        struct nfsd4_session *new;
@@ -894,7 +927,9 @@ static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct n
        status = nfsd4_new_conn_from_crses(rqstp, new);
        /* whoops: benny points out, status is ignored! (err, or bogus) */
        if (status) {
+               spin_lock(&client_lock);
                free_session(&new->se_ref);
+               spin_unlock(&client_lock);
                return NULL;
        }
        if (cses->flags & SESSION4_BACK_CHAN) {
@@ -1006,12 +1041,13 @@ static struct nfs4_client *alloc_client(struct xdr_netobj name)
 static inline void
 free_client(struct nfs4_client *clp)
 {
+       BUG_ON(!spin_is_locked(&client_lock));
        while (!list_empty(&clp->cl_sessions)) {
                struct nfsd4_session *ses;
                ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
                                se_perclnt);
                list_del(&ses->se_perclnt);
-               nfsd4_put_session(ses);
+               nfsd4_put_session_locked(ses);
        }
        if (clp->cl_cred.cr_group_info)
                put_group_info(clp->cl_cred.cr_group_info);
@@ -1138,12 +1174,12 @@ static void gen_clid(struct nfs4_client *clp)
 
 static void gen_confirm(struct nfs4_client *clp)
 {
+       __be32 verf[2];
        static u32 i;
-       u32 *p;
 
-       p = (u32 *)clp->cl_confirm.data;
-       *p++ = get_seconds();
-       *p++ = i++;
+       verf[0] = (__be32)get_seconds();
+       verf[1] = (__be32)i++;
+       memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
 }
 
 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
@@ -1180,7 +1216,9 @@ static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
        if (princ) {
                clp->cl_principal = kstrdup(princ, GFP_KERNEL);
                if (clp->cl_principal == NULL) {
+                       spin_lock(&client_lock);
                        free_client(clp);
+                       spin_unlock(&client_lock);
                        return NULL;
                }
        }
@@ -1347,6 +1385,7 @@ nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
        slot->sl_opcnt = resp->opcnt;
        slot->sl_status = resp->cstate.status;
 
+       slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
        if (nfsd4_not_cached(resp)) {
                slot->sl_datalen = 0;
                return;
@@ -1374,15 +1413,12 @@ nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
        struct nfsd4_op *op;
        struct nfsd4_slot *slot = resp->cstate.slot;
 
-       dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
-               resp->opcnt, resp->cstate.slot->sl_cachethis);
-
        /* Encode the replayed sequence operation */
        op = &args->ops[resp->opcnt - 1];
        nfsd4_encode_operation(resp, op);
 
        /* Return nfserr_retry_uncached_rep in next operation. */
-       if (args->opcnt > 1 && slot->sl_cachethis == 0) {
+       if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
                op = &args->ops[resp->opcnt++];
                op->status = nfserr_retry_uncached_rep;
                nfsd4_encode_operation(resp, op);
@@ -1575,16 +1611,11 @@ check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
                else
                        return nfserr_seq_misordered;
        }
-       /* Normal */
+       /* Note unsigned 32-bit arithmetic handles wraparound: */
        if (likely(seqid == slot_seqid + 1))
                return nfs_ok;
-       /* Replay */
        if (seqid == slot_seqid)
                return nfserr_replay_cache;
-       /* Wraparound */
-       if (seqid == 1 && (slot_seqid + 1) == 0)
-               return nfs_ok;
-       /* Misordered replay or misordered new request */
        return nfserr_seq_misordered;
 }
 
@@ -1815,9 +1846,10 @@ nfsd4_destroy_session(struct svc_rqst *r,
        nfsd4_probe_callback_sync(ses->se_client);
        nfs4_unlock_state();
 
+       spin_lock(&client_lock);
        nfsd4_del_conns(ses);
-
-       nfsd4_put_session(ses);
+       nfsd4_put_session_locked(ses);
+       spin_unlock(&client_lock);
        status = nfs_ok;
 out:
        dprintk("%s returns %d\n", __func__, ntohl(status));
@@ -1921,8 +1953,12 @@ nfsd4_sequence(struct svc_rqst *rqstp,
         * sr_highest_slotid and the sr_target_slot id to maxslots */
        seq->maxslots = session->se_fchannel.maxreqs;
 
-       status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
+       status = check_slot_seqid(seq->seqid, slot->sl_seqid,
+                                       slot->sl_flags & NFSD4_SLOT_INUSE);
        if (status == nfserr_replay_cache) {
+               status = nfserr_seq_misordered;
+               if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
+                       goto out;
                cstate->slot = slot;
                cstate->session = session;
                /* Return the cached reply status and set cstate->status
@@ -1938,9 +1974,12 @@ nfsd4_sequence(struct svc_rqst *rqstp,
        conn = NULL;
 
        /* Success! bump slot seqid */
-       slot->sl_inuse = true;
        slot->sl_seqid = seq->seqid;
-       slot->sl_cachethis = seq->cachethis;
+       slot->sl_flags |= NFSD4_SLOT_INUSE;
+       if (seq->cachethis)
+               slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
+       else
+               slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
 
        cstate->slot = slot;
        cstate->session = session;
@@ -2030,7 +2069,8 @@ nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *csta
 
        nfs4_lock_state();
        status = nfserr_complete_already;
-       if (cstate->session->se_client->cl_firststate)
+       if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
+                            &cstate->session->se_client->cl_flags))
                goto out;
 
        status = nfserr_stale_clientid;
@@ -2045,7 +2085,7 @@ nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *csta
                goto out;
 
        status = nfs_ok;
-       nfsd4_create_clid_dir(cstate->session->se_client);
+       nfsd4_client_record_create(cstate->session->se_client);
 out:
        nfs4_unlock_state();
        return status;
@@ -2240,7 +2280,7 @@ nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
                        conf = find_confirmed_client_by_str(unconf->cl_recdir,
                                                            hash);
                        if (conf) {
-                               nfsd4_remove_clid_dir(conf);
+                               nfsd4_client_record_remove(conf);
                                expire_client(conf);
                        }
                        move_to_confirmed(unconf);
@@ -2633,8 +2673,6 @@ nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
 
 static int share_access_to_flags(u32 share_access)
 {
-       share_access &= ~NFS4_SHARE_WANT_MASK;
-
        return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
 }
 
@@ -2776,10 +2814,9 @@ 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)
+nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
 {
        open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
-       open->op_openowner->oo_owner.so_client->cl_firststate = 1;
 }
 
 /* Should we give out recallable state?: */
@@ -2855,6 +2892,27 @@ static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
        return 0;
 }
 
+static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
+{
+       open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
+       if (status == -EAGAIN)
+               open->op_why_no_deleg = WND4_CONTENTION;
+       else {
+               open->op_why_no_deleg = WND4_RESOURCE;
+               switch (open->op_deleg_want) {
+               case NFS4_SHARE_WANT_READ_DELEG:
+               case NFS4_SHARE_WANT_WRITE_DELEG:
+               case NFS4_SHARE_WANT_ANY_DELEG:
+                       break;
+               case NFS4_SHARE_WANT_CANCEL:
+                       open->op_why_no_deleg = WND4_CANCELLED;
+                       break;
+               case NFS4_SHARE_WANT_NO_DELEG:
+                       BUG();  /* not supposed to get here */
+               }
+       }
+}
+
 /*
  * Attempt to hand out a delegation.
  */
@@ -2864,7 +2922,7 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_
        struct nfs4_delegation *dp;
        struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
        int cb_up;
-       int status, flag = 0;
+       int status = 0, flag = 0;
 
        cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
        flag = NFS4_OPEN_DELEGATE_NONE;
@@ -2905,11 +2963,16 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_
        dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
                STATEID_VAL(&dp->dl_stid.sc_stateid));
 out:
-       if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
-                       && flag == NFS4_OPEN_DELEGATE_NONE
-                       && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
-               dprintk("NFSD: WARNING: refusing delegation reclaim\n");
        open->op_delegate_type = flag;
+       if (flag == NFS4_OPEN_DELEGATE_NONE) {
+               if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
+                   open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
+                       dprintk("NFSD: WARNING: refusing delegation reclaim\n");
+
+               /* 4.1 client asking for a delegation? */
+               if (open->op_deleg_want)
+                       nfsd4_open_deleg_none_ext(open, status);
+       }
        return;
 out_free:
        nfs4_put_delegation(dp);
@@ -2918,6 +2981,24 @@ out_no_deleg:
        goto out;
 }
 
+static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
+                                       struct nfs4_delegation *dp)
+{
+       if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
+           dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
+               open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
+               open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
+       } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
+                  dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
+               open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
+               open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
+       }
+       /* Otherwise the client must be confused wanting a delegation
+        * it already has, therefore we don't return
+        * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
+        */
+}
+
 /*
  * called with nfs4_lock_state() held.
  */
@@ -2979,24 +3060,36 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
        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))
+       if (nfsd4_has_session(&resp->cstate)) {
                open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
 
+               if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
+                       open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
+                       open->op_why_no_deleg = WND4_NOT_WANTED;
+                       goto nodeleg;
+               }
+       }
+
        /*
        * Attempt to hand out a delegation. No error return, because the
        * OPEN succeeds even if we fail.
        */
        nfs4_open_delegation(current_fh, open, stp);
-
+nodeleg:
        status = nfs_ok;
 
        dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
                STATEID_VAL(&stp->st_stid.sc_stateid));
 out:
+       /* 4.1 client trying to upgrade/downgrade delegation? */
+       if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
+           open->op_deleg_want)
+               nfsd4_deleg_xgrade_none_ext(open, dp);
+
        if (fp)
                put_nfs4_file(fp);
        if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
-               nfs4_set_claim_prev(open);
+               nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
        /*
        * To finish the open response, we just need to set the rflags.
        */
@@ -3066,7 +3159,7 @@ static void
 nfsd4_end_grace(void)
 {
        dprintk("NFSD: end of grace period\n");
-       nfsd4_recdir_purge_old();
+       nfsd4_record_grace_done(&init_net, boot_time);
        locks_end_grace(&nfsd4_manager);
        /*
         * Now that every NFSv4 client has had the chance to recover and
@@ -3115,7 +3208,7 @@ nfs4_laundromat(void)
                clp = list_entry(pos, struct nfs4_client, cl_lru);
                dprintk("NFSD: purging unused client (clientid %08x)\n",
                        clp->cl_clientid.cl_id);
-               nfsd4_remove_clid_dir(clp);
+               nfsd4_client_record_remove(clp);
                expire_client(clp);
        }
        spin_lock(&recall_lock);
@@ -3400,7 +3493,14 @@ __be32
 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                   struct nfsd4_test_stateid *test_stateid)
 {
-       /* real work is done during encoding */
+       struct nfsd4_test_stateid_id *stateid;
+       struct nfs4_client *cl = cstate->session->se_client;
+
+       nfs4_lock_state();
+       list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
+               stateid->ts_id_status = nfs4_validate_stateid(cl, &stateid->ts_id_stateid);
+       nfs4_unlock_state();
+
        return nfs_ok;
 }
 
@@ -3539,7 +3639,7 @@ nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
                __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
 
-       nfsd4_create_clid_dir(oo->oo_owner.so_client);
+       nfsd4_client_record_create(oo->oo_owner.so_client);
        status = nfs_ok;
 out:
        if (!cstate->replay_owner)
@@ -3596,7 +3696,9 @@ nfsd4_open_downgrade(struct svc_rqst *rqstp,
                        cstate->current_fh.fh_dentry->d_name.name);
 
        /* We don't yet support WANT bits: */
-       od->od_share_access &= NFS4_SHARE_ACCESS_MASK;
+       if (od->od_deleg_want)
+               dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
+                       od->od_deleg_want);
 
        nfs4_lock_state();
        status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
@@ -4109,16 +4211,14 @@ out:
  * 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)
+static __be32 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);
+       __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
+       if (!err) {
+               err = nfserrno(vfs_test_lock(file, lock));
+               nfsd_close(file);
+       }
        return err;
 }
 
@@ -4132,7 +4232,6 @@ nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        struct inode *inode;
        struct file_lock file_lock;
        struct nfs4_lockowner *lo;
-       int error;
        __be32 status;
 
        if (locks_in_grace())
@@ -4178,12 +4277,10 @@ nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
        nfs4_transform_lock_offset(&file_lock);
 
-       status = nfs_ok;
-       error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
-       if (error) {
-               status = nfserrno(error);
+       status = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
+       if (status)
                goto out;
-       }
+
        if (file_lock.fl_type != F_UNLCK) {
                status = nfserr_denied;
                nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
@@ -4353,7 +4450,9 @@ nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
        struct nfs4_client *clp;
 
        clp = find_confirmed_client_by_str(name, strhashval);
-       return clp ? 1 : 0;
+       if (!clp)
+               return 0;
+       return test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
 }
 
 /*
@@ -4377,7 +4476,7 @@ nfs4_client_to_reclaim(const char *name)
        return 1;
 }
 
-static void
+void
 nfs4_release_reclaim(void)
 {
        struct nfs4_client_reclaim *crp = NULL;
@@ -4397,19 +4496,12 @@ nfs4_release_reclaim(void)
 
 /*
  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
-static struct nfs4_client_reclaim *
-nfs4_find_reclaim_client(clientid_t *clid)
+struct nfs4_client_reclaim *
+nfsd4_find_reclaim_client(struct nfs4_client *clp)
 {
        unsigned int strhashval;
-       struct nfs4_client *clp;
        struct nfs4_client_reclaim *crp = NULL;
 
-
-       /* find clientid in conf_id_hashtbl */
-       clp = find_confirmed_client(clid);
-       if (clp == NULL)
-               return NULL;
-
        dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
                            clp->cl_name.len, clp->cl_name.data,
                            clp->cl_recdir);
@@ -4430,7 +4522,14 @@ nfs4_find_reclaim_client(clientid_t *clid)
 __be32
 nfs4_check_open_reclaim(clientid_t *clid)
 {
-       return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
+       struct nfs4_client *clp;
+
+       /* find clientid in conf_id_hashtbl */
+       clp = find_confirmed_client(clid);
+       if (clp == NULL)
+               return nfserr_reclaim_bad;
+
+       return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
 }
 
 #ifdef CONFIG_NFSD_FAULT_INJECTION
@@ -4442,7 +4541,7 @@ void nfsd_forget_clients(u64 num)
 
        nfs4_lock_state();
        list_for_each_entry_safe(clp, next, &client_lru, cl_lru) {
-               nfsd4_remove_clid_dir(clp);
+               nfsd4_client_record_remove(clp);
                expire_client(clp);
                if (++count == num)
                        break;
@@ -4577,19 +4676,6 @@ nfs4_state_init(void)
        reclaim_str_hashtbl_size = 0;
 }
 
-static void
-nfsd4_load_reboot_recovery_data(void)
-{
-       int status;
-
-       nfs4_lock_state();
-       nfsd4_init_recdir();
-       status = nfsd4_recdir_load();
-       nfs4_unlock_state();
-       if (status)
-               printk("NFSD: Failure reading reboot recovery data\n");
-}
-
 /*
  * Since the lifetime of a delegation isn't limited to that of an open, a
  * client may quite reasonably hang on to a delegation as long as it has
@@ -4613,21 +4699,34 @@ set_max_delegations(void)
 
 /* initialization to perform when the nfsd service is started: */
 
-static int
-__nfs4_state_start(void)
+int
+nfs4_state_start(void)
 {
        int ret;
 
+       /*
+        * FIXME: For now, we hang most of the pernet global stuff off of
+        * init_net until nfsd is fully containerized. Eventually, we'll
+        * need to pass a net pointer into this function, take a reference
+        * to that instead and then do most of the rest of this on a per-net
+        * basis.
+        */
+       get_net(&init_net);
+       nfsd4_client_tracking_init(&init_net);
        boot_time = get_seconds();
        locks_start_grace(&nfsd4_manager);
        printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
               nfsd4_grace);
        ret = set_callback_cred();
-       if (ret)
-               return -ENOMEM;
+       if (ret) {
+               ret = -ENOMEM;
+               goto out_recovery;
+       }
        laundry_wq = create_singlethread_workqueue("nfsd4");
-       if (laundry_wq == NULL)
-               return -ENOMEM;
+       if (laundry_wq == NULL) {
+               ret = -ENOMEM;
+               goto out_recovery;
+       }
        ret = nfsd4_create_callback_queue();
        if (ret)
                goto out_free_laundry;
@@ -4636,16 +4735,12 @@ __nfs4_state_start(void)
        return 0;
 out_free_laundry:
        destroy_workqueue(laundry_wq);
+out_recovery:
+       nfsd4_client_tracking_exit(&init_net);
+       put_net(&init_net);
        return ret;
 }
 
-int
-nfs4_state_start(void)
-{
-       nfsd4_load_reboot_recovery_data();
-       return __nfs4_state_start();
-}
-
 static void
 __nfs4_state_shutdown(void)
 {
@@ -4676,7 +4771,8 @@ __nfs4_state_shutdown(void)
                unhash_delegation(dp);
        }
 
-       nfsd4_shutdown_recdir();
+       nfsd4_client_tracking_exit(&init_net);
+       put_net(&init_net);
 }
 
 void
@@ -4686,8 +4782,108 @@ nfs4_state_shutdown(void)
        destroy_workqueue(laundry_wq);
        locks_end_grace(&nfsd4_manager);
        nfs4_lock_state();
-       nfs4_release_reclaim();
        __nfs4_state_shutdown();
        nfs4_unlock_state();
        nfsd4_destroy_callback_queue();
 }
+
+static void
+get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
+{
+       if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
+               memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
+}
+
+static void
+put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
+{
+       if (cstate->minorversion) {
+               memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
+               SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
+       }
+}
+
+void
+clear_current_stateid(struct nfsd4_compound_state *cstate)
+{
+       CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
+}
+
+/*
+ * functions to set current state id
+ */
+void
+nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
+{
+       put_stateid(cstate, &odp->od_stateid);
+}
+
+void
+nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
+{
+       put_stateid(cstate, &open->op_stateid);
+}
+
+void
+nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
+{
+       put_stateid(cstate, &close->cl_stateid);
+}
+
+void
+nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
+{
+       put_stateid(cstate, &lock->lk_resp_stateid);
+}
+
+/*
+ * functions to consume current state id
+ */
+
+void
+nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
+{
+       get_stateid(cstate, &odp->od_stateid);
+}
+
+void
+nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
+{
+       get_stateid(cstate, &drp->dr_stateid);
+}
+
+void
+nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
+{
+       get_stateid(cstate, &fsp->fr_stateid);
+}
+
+void
+nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
+{
+       get_stateid(cstate, &setattr->sa_stateid);
+}
+
+void
+nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
+{
+       get_stateid(cstate, &close->cl_stateid);
+}
+
+void
+nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
+{
+       get_stateid(cstate, &locku->lu_stateid);
+}
+
+void
+nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
+{
+       get_stateid(cstate, &read->rd_stateid);
+}
+
+void
+nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
+{
+       get_stateid(cstate, &write->wr_stateid);
+}