NFSv4.1: Reset the sequence number for slots that have been deallocated
[linux-2.6-block.git] / fs / nfs / nfs4proc.c
index 5b61c4a83191203e94aaaf14dcf385cb56a205d8..52435ec441932b878e2824a9cd5ad3a24714328e 100644 (file)
@@ -412,16 +412,18 @@ static void renew_lease(const struct nfs_server *server, unsigned long timestamp
  * Must be called while holding tbl->slot_tbl_lock
  */
 static void
-nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
+nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
 {
+       u32 slotid = slot->slot_nr;
+
        /* clear used bit in bitmap */
        __clear_bit(slotid, tbl->used_slots);
 
        /* update highest_used_slotid when it is freed */
        if (slotid == tbl->highest_used_slotid) {
-               slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
-               if (slotid < tbl->max_slots)
-                       tbl->highest_used_slotid = slotid;
+               u32 new_max = find_last_bit(tbl->used_slots, slotid);
+               if (new_max < slotid)
+                       tbl->highest_used_slotid = new_max;
                else
                        tbl->highest_used_slotid = NFS4_NO_SLOT;
        }
@@ -480,12 +482,51 @@ static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
        session = tbl->session;
 
        spin_lock(&tbl->slot_tbl_lock);
-       nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
+       nfs4_free_slot(tbl, res->sr_slot);
        nfs4_check_drain_fc_complete(session);
        spin_unlock(&tbl->slot_tbl_lock);
        res->sr_slot = NULL;
 }
 
+/* Update the client's idea of target_highest_slotid */
+static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
+               u32 target_highest_slotid)
+{
+       if (tbl->target_highest_slotid == target_highest_slotid)
+               return;
+       tbl->target_highest_slotid = target_highest_slotid;
+       tbl->generation++;
+}
+
+static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
+               u32 highest_slotid)
+{
+       unsigned int max_slotid, i;
+
+       if (tbl->server_highest_slotid == highest_slotid)
+               return;
+       if (tbl->highest_used_slotid > highest_slotid)
+               return;
+       max_slotid = min(tbl->max_slots - 1, highest_slotid);
+       /* Reset the seq_nr for deallocated slots */
+       for (i = tbl->server_highest_slotid + 1; i <= max_slotid; i++)
+               tbl->slots[i].seq_nr = 1;
+       tbl->server_highest_slotid = highest_slotid;
+}
+
+static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
+               struct nfs4_slot *slot,
+               struct nfs4_sequence_res *res)
+{
+       spin_lock(&tbl->slot_tbl_lock);
+       if (tbl->generation != slot->generation)
+               goto out;
+       nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
+       nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
+out:
+       spin_unlock(&tbl->slot_tbl_lock);
+}
+
 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
 {
        struct nfs4_session *session;
@@ -520,15 +561,16 @@ static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *
                /* Check sequence flags */
                if (res->sr_status_flags != 0)
                        nfs4_schedule_lease_recovery(clp);
+               nfs41_update_target_slotid(slot->table, slot, res);
                break;
        case -NFS4ERR_DELAY:
                /* The server detected a resend of the RPC call and
                 * returned NFS4ERR_DELAY as per Section 2.10.6.2
                 * of RFC5661.
                 */
-               dprintk("%s: slot=%td seq=%d: Operation in progress\n",
+               dprintk("%s: slot=%u seq=%u: Operation in progress\n",
                        __func__,
-                       slot - session->fc_slot_table.slots,
+                       slot->slot_nr,
                        slot->seq_nr);
                goto out_retry;
        default:
@@ -556,20 +598,18 @@ static int nfs4_sequence_done(struct rpc_task *task,
 }
 
 /*
- * nfs4_find_slot - efficiently look for a free slot
+ * nfs4_alloc_slot - efficiently look for a free slot
  *
- * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
+ * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
  * If found, we mark the slot as used, update the highest_used_slotid,
  * and respectively set up the sequence operation args.
- * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
  *
  * Note: must be called with under the slot_tbl_lock.
  */
-static u32
-nfs4_find_slot(struct nfs4_slot_table *tbl)
+static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
 {
+       struct nfs4_slot *ret = NULL;
        u32 slotid;
-       u32 ret_id = NFS4_NO_SLOT;
 
        dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
                __func__, tbl->used_slots[0], tbl->highest_used_slotid,
@@ -581,17 +621,21 @@ nfs4_find_slot(struct nfs4_slot_table *tbl)
        if (slotid > tbl->highest_used_slotid ||
                        tbl->highest_used_slotid == NFS4_NO_SLOT)
                tbl->highest_used_slotid = slotid;
-       ret_id = slotid;
+       ret = &tbl->slots[slotid];
+       ret->renewal_time = jiffies;
+       ret->generation = tbl->generation;
+
 out:
        dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
-               __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
-       return ret_id;
+               __func__, tbl->used_slots[0], tbl->highest_used_slotid,
+               ret ? ret->slot_nr : -1);
+       return ret;
 }
 
 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
                struct nfs4_sequence_res *res, int cache_reply)
 {
-       args->sa_session = NULL;
+       args->sa_slot = NULL;
        args->sa_cache_this = 0;
        if (cache_reply)
                args->sa_cache_this = 1;
@@ -605,7 +649,6 @@ int nfs41_setup_sequence(struct nfs4_session *session,
 {
        struct nfs4_slot *slot;
        struct nfs4_slot_table *tbl;
-       u32 slotid;
 
        dprintk("--> %s\n", __func__);
        /* slot already allocated? */
@@ -632,8 +675,8 @@ int nfs41_setup_sequence(struct nfs4_session *session,
                return -EAGAIN;
        }
 
-       slotid = nfs4_find_slot(tbl);
-       if (slotid == NFS4_NO_SLOT) {
+       slot = nfs4_alloc_slot(tbl);
+       if (slot == NULL) {
                rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
                spin_unlock(&tbl->slot_tbl_lock);
                dprintk("<-- %s: no free slots\n", __func__);
@@ -642,12 +685,11 @@ int nfs41_setup_sequence(struct nfs4_session *session,
        spin_unlock(&tbl->slot_tbl_lock);
 
        rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
-       slot = tbl->slots + slotid;
-       slot->renewal_time = jiffies;
-       args->sa_session = session;
-       args->sa_slotid = slotid;
 
-       dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
+       args->sa_slot = slot;
+
+       dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
+                       slot->slot_nr, slot->seq_nr);
 
        res->sr_slot = slot;
        res->sr_status_flags = 0;
@@ -671,9 +713,9 @@ int nfs4_setup_sequence(const struct nfs_server *server,
        if (session == NULL)
                goto out;
 
-       dprintk("--> %s clp %p session %p sr_slot %td\n",
+       dprintk("--> %s clp %p session %p sr_slot %d\n",
                __func__, session->clp, session, res->sr_slot ?
-                       res->sr_slot - session->fc_slot_table.slots : -1);
+                       res->sr_slot->slot_nr : -1);
 
        ret = nfs41_setup_sequence(session, args, res, task);
 out:
@@ -5669,8 +5711,10 @@ struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
 
        tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
        if (tbl != NULL) {
-               for (i = 0; i < max_slots; i++)
+               for (i = 0; i < max_slots; i++) {
                        tbl[i].table = table;
+                       tbl[i].slot_nr = i;
+               }
        }
        return tbl;
 }
@@ -5690,6 +5734,8 @@ static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
                tbl->max_slots = max_slots;
        }
        tbl->highest_used_slotid = NFS4_NO_SLOT;
+       tbl->target_highest_slotid = max_slots - 1;
+       tbl->server_highest_slotid = max_slots - 1;
        for (i = 0; i < tbl->max_slots; i++)
                tbl->slots[i].seq_nr = ivalue;
        spin_unlock(&tbl->slot_tbl_lock);