xref: /openbmc/linux/fs/nfs/nfs4session.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
273e39aaaSTrond Myklebust /*
373e39aaaSTrond Myklebust  * fs/nfs/nfs4session.c
473e39aaaSTrond Myklebust  *
573e39aaaSTrond Myklebust  * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com>
673e39aaaSTrond Myklebust  *
773e39aaaSTrond Myklebust  */
873e39aaaSTrond Myklebust #include <linux/kernel.h>
973e39aaaSTrond Myklebust #include <linux/errno.h>
1073e39aaaSTrond Myklebust #include <linux/string.h>
1173e39aaaSTrond Myklebust #include <linux/printk.h>
1273e39aaaSTrond Myklebust #include <linux/slab.h>
1373e39aaaSTrond Myklebust #include <linux/sunrpc/sched.h>
1473e39aaaSTrond Myklebust #include <linux/sunrpc/bc_xprt.h>
1573e39aaaSTrond Myklebust #include <linux/nfs.h>
1673e39aaaSTrond Myklebust #include <linux/nfs4.h>
1773e39aaaSTrond Myklebust #include <linux/nfs_fs.h>
1873e39aaaSTrond Myklebust #include <linux/module.h>
1973e39aaaSTrond Myklebust 
2073e39aaaSTrond Myklebust #include "nfs4_fs.h"
2173e39aaaSTrond Myklebust #include "internal.h"
2273e39aaaSTrond Myklebust #include "nfs4session.h"
2373e39aaaSTrond Myklebust #include "callback.h"
2473e39aaaSTrond Myklebust 
2573e39aaaSTrond Myklebust #define NFSDBG_FACILITY		NFSDBG_STATE
2673e39aaaSTrond Myklebust 
nfs4_init_slot_table(struct nfs4_slot_table * tbl,const char * queue)27744aa525SChuck Lever static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue)
28744aa525SChuck Lever {
29744aa525SChuck Lever 	tbl->highest_used_slotid = NFS4_NO_SLOT;
30744aa525SChuck Lever 	spin_lock_init(&tbl->slot_tbl_lock);
31744aa525SChuck Lever 	rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, queue);
32045d2a6dSTrond Myklebust 	init_waitqueue_head(&tbl->slot_waitq);
33744aa525SChuck Lever 	init_completion(&tbl->complete);
34744aa525SChuck Lever }
35744aa525SChuck Lever 
3673e39aaaSTrond Myklebust /*
3773e39aaaSTrond Myklebust  * nfs4_shrink_slot_table - free retired slots from the slot table
3873e39aaaSTrond Myklebust  */
nfs4_shrink_slot_table(struct nfs4_slot_table * tbl,u32 newsize)3973e39aaaSTrond Myklebust static void nfs4_shrink_slot_table(struct nfs4_slot_table  *tbl, u32 newsize)
4073e39aaaSTrond Myklebust {
4173e39aaaSTrond Myklebust 	struct nfs4_slot **p;
4273e39aaaSTrond Myklebust 	if (newsize >= tbl->max_slots)
4373e39aaaSTrond Myklebust 		return;
4473e39aaaSTrond Myklebust 
4573e39aaaSTrond Myklebust 	p = &tbl->slots;
4673e39aaaSTrond Myklebust 	while (newsize--)
4773e39aaaSTrond Myklebust 		p = &(*p)->next;
4873e39aaaSTrond Myklebust 	while (*p) {
4973e39aaaSTrond Myklebust 		struct nfs4_slot *slot = *p;
5073e39aaaSTrond Myklebust 
5173e39aaaSTrond Myklebust 		*p = slot->next;
5273e39aaaSTrond Myklebust 		kfree(slot);
5373e39aaaSTrond Myklebust 		tbl->max_slots--;
5473e39aaaSTrond Myklebust 	}
5573e39aaaSTrond Myklebust }
5673e39aaaSTrond Myklebust 
579d33059cSChuck Lever /**
589d33059cSChuck Lever  * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
59302fad7bSTrond Myklebust  * @tbl: controlling slot table
609d33059cSChuck Lever  *
619d33059cSChuck Lever  */
nfs4_slot_tbl_drain_complete(struct nfs4_slot_table * tbl)629d33059cSChuck Lever void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
639d33059cSChuck Lever {
649d33059cSChuck Lever 	if (nfs4_slot_tbl_draining(tbl))
659d33059cSChuck Lever 		complete(&tbl->complete);
669d33059cSChuck Lever }
679d33059cSChuck Lever 
6873e39aaaSTrond Myklebust /*
6973e39aaaSTrond Myklebust  * nfs4_free_slot - free a slot and efficiently update slot table.
7073e39aaaSTrond Myklebust  *
7173e39aaaSTrond Myklebust  * freeing a slot is trivially done by clearing its respective bit
7273e39aaaSTrond Myklebust  * in the bitmap.
7373e39aaaSTrond Myklebust  * If the freed slotid equals highest_used_slotid we want to update it
7473e39aaaSTrond Myklebust  * so that the server would be able to size down the slot table if needed,
7573e39aaaSTrond Myklebust  * otherwise we know that the highest_used_slotid is still in use.
7673e39aaaSTrond Myklebust  * When updating highest_used_slotid there may be "holes" in the bitmap
7773e39aaaSTrond Myklebust  * so we need to scan down from highest_used_slotid to 0 looking for the now
7873e39aaaSTrond Myklebust  * highest slotid in use.
7973e39aaaSTrond Myklebust  * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
8073e39aaaSTrond Myklebust  *
8173e39aaaSTrond Myklebust  * Must be called while holding tbl->slot_tbl_lock
8273e39aaaSTrond Myklebust  */
nfs4_free_slot(struct nfs4_slot_table * tbl,struct nfs4_slot * slot)8373e39aaaSTrond Myklebust void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
8473e39aaaSTrond Myklebust {
8573e39aaaSTrond Myklebust 	u32 slotid = slot->slot_nr;
8673e39aaaSTrond Myklebust 
8773e39aaaSTrond Myklebust 	/* clear used bit in bitmap */
8873e39aaaSTrond Myklebust 	__clear_bit(slotid, tbl->used_slots);
8973e39aaaSTrond Myklebust 
9073e39aaaSTrond Myklebust 	/* update highest_used_slotid when it is freed */
9173e39aaaSTrond Myklebust 	if (slotid == tbl->highest_used_slotid) {
9273e39aaaSTrond Myklebust 		u32 new_max = find_last_bit(tbl->used_slots, slotid);
9373e39aaaSTrond Myklebust 		if (new_max < slotid)
9473e39aaaSTrond Myklebust 			tbl->highest_used_slotid = new_max;
9573e39aaaSTrond Myklebust 		else {
9673e39aaaSTrond Myklebust 			tbl->highest_used_slotid = NFS4_NO_SLOT;
97774d5f14SAndy Adamson 			nfs4_slot_tbl_drain_complete(tbl);
9873e39aaaSTrond Myklebust 		}
9973e39aaaSTrond Myklebust 	}
100e8d92382SChuck Lever 	dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
10173e39aaaSTrond Myklebust 		slotid, tbl->highest_used_slotid);
10273e39aaaSTrond Myklebust }
10373e39aaaSTrond Myklebust 
nfs4_new_slot(struct nfs4_slot_table * tbl,u32 slotid,u32 seq_init,gfp_t gfp_mask)10473e39aaaSTrond Myklebust static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table  *tbl,
10573e39aaaSTrond Myklebust 		u32 slotid, u32 seq_init, gfp_t gfp_mask)
10673e39aaaSTrond Myklebust {
10773e39aaaSTrond Myklebust 	struct nfs4_slot *slot;
10873e39aaaSTrond Myklebust 
10973e39aaaSTrond Myklebust 	slot = kzalloc(sizeof(*slot), gfp_mask);
11073e39aaaSTrond Myklebust 	if (slot) {
11173e39aaaSTrond Myklebust 		slot->table = tbl;
11273e39aaaSTrond Myklebust 		slot->slot_nr = slotid;
11373e39aaaSTrond Myklebust 		slot->seq_nr = seq_init;
1143453d570STrond Myklebust 		slot->seq_nr_highest_sent = seq_init;
1153453d570STrond Myklebust 		slot->seq_nr_last_acked = seq_init - 1;
11673e39aaaSTrond Myklebust 	}
11773e39aaaSTrond Myklebust 	return slot;
11873e39aaaSTrond Myklebust }
11973e39aaaSTrond Myklebust 
nfs4_find_or_create_slot(struct nfs4_slot_table * tbl,u32 slotid,u32 seq_init,gfp_t gfp_mask)12073e39aaaSTrond Myklebust static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table  *tbl,
12173e39aaaSTrond Myklebust 		u32 slotid, u32 seq_init, gfp_t gfp_mask)
12273e39aaaSTrond Myklebust {
12373e39aaaSTrond Myklebust 	struct nfs4_slot **p, *slot;
12473e39aaaSTrond Myklebust 
12573e39aaaSTrond Myklebust 	p = &tbl->slots;
12673e39aaaSTrond Myklebust 	for (;;) {
12773e39aaaSTrond Myklebust 		if (*p == NULL) {
12873e39aaaSTrond Myklebust 			*p = nfs4_new_slot(tbl, tbl->max_slots,
12973e39aaaSTrond Myklebust 					seq_init, gfp_mask);
13073e39aaaSTrond Myklebust 			if (*p == NULL)
13173e39aaaSTrond Myklebust 				break;
13273e39aaaSTrond Myklebust 			tbl->max_slots++;
13373e39aaaSTrond Myklebust 		}
13473e39aaaSTrond Myklebust 		slot = *p;
13573e39aaaSTrond Myklebust 		if (slot->slot_nr == slotid)
13673e39aaaSTrond Myklebust 			return slot;
13773e39aaaSTrond Myklebust 		p = &slot->next;
13873e39aaaSTrond Myklebust 	}
13973e39aaaSTrond Myklebust 	return ERR_PTR(-ENOMEM);
14073e39aaaSTrond Myklebust }
14173e39aaaSTrond Myklebust 
nfs4_lock_slot(struct nfs4_slot_table * tbl,struct nfs4_slot * slot)142810d82e6STrond Myklebust static void nfs4_lock_slot(struct nfs4_slot_table *tbl,
143810d82e6STrond Myklebust 		struct nfs4_slot *slot)
144810d82e6STrond Myklebust {
145810d82e6STrond Myklebust 	u32 slotid = slot->slot_nr;
146810d82e6STrond Myklebust 
147810d82e6STrond Myklebust 	__set_bit(slotid, tbl->used_slots);
148810d82e6STrond Myklebust 	if (slotid > tbl->highest_used_slotid ||
149810d82e6STrond Myklebust 	    tbl->highest_used_slotid == NFS4_NO_SLOT)
150810d82e6STrond Myklebust 		tbl->highest_used_slotid = slotid;
151810d82e6STrond Myklebust 	slot->generation = tbl->generation;
152810d82e6STrond Myklebust }
153810d82e6STrond Myklebust 
154810d82e6STrond Myklebust /*
155810d82e6STrond Myklebust  * nfs4_try_to_lock_slot - Given a slot try to allocate it
156810d82e6STrond Myklebust  *
157810d82e6STrond Myklebust  * Note: must be called with the slot_tbl_lock held.
158810d82e6STrond Myklebust  */
nfs4_try_to_lock_slot(struct nfs4_slot_table * tbl,struct nfs4_slot * slot)159810d82e6STrond Myklebust bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
160810d82e6STrond Myklebust {
161810d82e6STrond Myklebust 	if (nfs4_test_locked_slot(tbl, slot->slot_nr))
162810d82e6STrond Myklebust 		return false;
163810d82e6STrond Myklebust 	nfs4_lock_slot(tbl, slot);
164810d82e6STrond Myklebust 	return true;
165810d82e6STrond Myklebust }
166810d82e6STrond Myklebust 
167810d82e6STrond Myklebust /*
168810d82e6STrond Myklebust  * nfs4_lookup_slot - Find a slot but don't allocate it
169810d82e6STrond Myklebust  *
170810d82e6STrond Myklebust  * Note: must be called with the slot_tbl_lock held.
171810d82e6STrond Myklebust  */
nfs4_lookup_slot(struct nfs4_slot_table * tbl,u32 slotid)172810d82e6STrond Myklebust struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid)
173810d82e6STrond Myklebust {
174810d82e6STrond Myklebust 	if (slotid <= tbl->max_slotid)
1759a837856SFred Isaman 		return nfs4_find_or_create_slot(tbl, slotid, 0, GFP_NOWAIT);
176810d82e6STrond Myklebust 	return ERR_PTR(-E2BIG);
177810d82e6STrond Myklebust }
178810d82e6STrond Myklebust 
nfs4_slot_get_seqid(struct nfs4_slot_table * tbl,u32 slotid,u32 * seq_nr)179e09c978aSTrond Myklebust static int nfs4_slot_get_seqid(struct nfs4_slot_table  *tbl, u32 slotid,
180e09c978aSTrond Myklebust 		u32 *seq_nr)
181e09c978aSTrond Myklebust 	__must_hold(&tbl->slot_tbl_lock)
182e09c978aSTrond Myklebust {
183e09c978aSTrond Myklebust 	struct nfs4_slot *slot;
18468a56400SArnd Bergmann 	int ret;
185e09c978aSTrond Myklebust 
186e09c978aSTrond Myklebust 	slot = nfs4_lookup_slot(tbl, slotid);
18768a56400SArnd Bergmann 	ret = PTR_ERR_OR_ZERO(slot);
18868a56400SArnd Bergmann 	if (!ret)
189e09c978aSTrond Myklebust 		*seq_nr = slot->seq_nr;
19068a56400SArnd Bergmann 
19168a56400SArnd Bergmann 	return ret;
192e09c978aSTrond Myklebust }
193e09c978aSTrond Myklebust 
194e09c978aSTrond Myklebust /*
195e09c978aSTrond Myklebust  * nfs4_slot_seqid_in_use - test if a slot sequence id is still in use
196e09c978aSTrond Myklebust  *
197e09c978aSTrond Myklebust  * Given a slot table, slot id and sequence number, determine if the
198e09c978aSTrond Myklebust  * RPC call in question is still in flight. This function is mainly
199e09c978aSTrond Myklebust  * intended for use by the callback channel.
200e09c978aSTrond Myklebust  */
nfs4_slot_seqid_in_use(struct nfs4_slot_table * tbl,u32 slotid,u32 seq_nr)201045d2a6dSTrond Myklebust static bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl,
202045d2a6dSTrond Myklebust 		u32 slotid, u32 seq_nr)
203e09c978aSTrond Myklebust {
2040ac84b72SShuah Khan 	u32 cur_seq = 0;
205e09c978aSTrond Myklebust 	bool ret = false;
206e09c978aSTrond Myklebust 
207e09c978aSTrond Myklebust 	spin_lock(&tbl->slot_tbl_lock);
208e09c978aSTrond Myklebust 	if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 &&
209e09c978aSTrond Myklebust 	    cur_seq == seq_nr && test_bit(slotid, tbl->used_slots))
210e09c978aSTrond Myklebust 		ret = true;
211e09c978aSTrond Myklebust 	spin_unlock(&tbl->slot_tbl_lock);
212e09c978aSTrond Myklebust 	return ret;
213e09c978aSTrond Myklebust }
214e09c978aSTrond Myklebust 
21573e39aaaSTrond Myklebust /*
216045d2a6dSTrond Myklebust  * nfs4_slot_wait_on_seqid - wait until a slot sequence id is complete
217045d2a6dSTrond Myklebust  *
218045d2a6dSTrond Myklebust  * Given a slot table, slot id and sequence number, wait until the
219045d2a6dSTrond Myklebust  * corresponding RPC call completes. This function is mainly
220045d2a6dSTrond Myklebust  * intended for use by the callback channel.
221045d2a6dSTrond Myklebust  */
nfs4_slot_wait_on_seqid(struct nfs4_slot_table * tbl,u32 slotid,u32 seq_nr,unsigned long timeout)222045d2a6dSTrond Myklebust int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl,
223045d2a6dSTrond Myklebust 		u32 slotid, u32 seq_nr,
224045d2a6dSTrond Myklebust 		unsigned long timeout)
225045d2a6dSTrond Myklebust {
226045d2a6dSTrond Myklebust 	if (wait_event_timeout(tbl->slot_waitq,
227045d2a6dSTrond Myklebust 			!nfs4_slot_seqid_in_use(tbl, slotid, seq_nr),
228045d2a6dSTrond Myklebust 			timeout) == 0)
229045d2a6dSTrond Myklebust 		return -ETIMEDOUT;
230045d2a6dSTrond Myklebust 	return 0;
231045d2a6dSTrond Myklebust }
232045d2a6dSTrond Myklebust 
233045d2a6dSTrond Myklebust /*
23473e39aaaSTrond Myklebust  * nfs4_alloc_slot - efficiently look for a free slot
23573e39aaaSTrond Myklebust  *
23673e39aaaSTrond Myklebust  * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
23773e39aaaSTrond Myklebust  * If found, we mark the slot as used, update the highest_used_slotid,
23873e39aaaSTrond Myklebust  * and respectively set up the sequence operation args.
23973e39aaaSTrond Myklebust  *
24073e39aaaSTrond Myklebust  * Note: must be called with under the slot_tbl_lock.
24173e39aaaSTrond Myklebust  */
nfs4_alloc_slot(struct nfs4_slot_table * tbl)24273e39aaaSTrond Myklebust struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
24373e39aaaSTrond Myklebust {
24473e39aaaSTrond Myklebust 	struct nfs4_slot *ret = ERR_PTR(-EBUSY);
24573e39aaaSTrond Myklebust 	u32 slotid;
24673e39aaaSTrond Myklebust 
24773e39aaaSTrond Myklebust 	dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
24873e39aaaSTrond Myklebust 		__func__, tbl->used_slots[0], tbl->highest_used_slotid,
24973e39aaaSTrond Myklebust 		tbl->max_slotid + 1);
25073e39aaaSTrond Myklebust 	slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
251810d82e6STrond Myklebust 	if (slotid <= tbl->max_slotid) {
25273e39aaaSTrond Myklebust 		ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
253810d82e6STrond Myklebust 		if (!IS_ERR(ret))
254810d82e6STrond Myklebust 			nfs4_lock_slot(tbl, ret);
255810d82e6STrond Myklebust 	}
256e8d92382SChuck Lever 	dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
25773e39aaaSTrond Myklebust 		__func__, tbl->used_slots[0], tbl->highest_used_slotid,
258e8d92382SChuck Lever 		!IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
25973e39aaaSTrond Myklebust 	return ret;
26073e39aaaSTrond Myklebust }
26173e39aaaSTrond Myklebust 
nfs4_grow_slot_table(struct nfs4_slot_table * tbl,u32 max_reqs,u32 ivalue)26273e39aaaSTrond Myklebust static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
26373e39aaaSTrond Myklebust 		 u32 max_reqs, u32 ivalue)
26473e39aaaSTrond Myklebust {
26573e39aaaSTrond Myklebust 	if (max_reqs <= tbl->max_slots)
26673e39aaaSTrond Myklebust 		return 0;
26773e39aaaSTrond Myklebust 	if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
26873e39aaaSTrond Myklebust 		return 0;
26973e39aaaSTrond Myklebust 	return -ENOMEM;
27073e39aaaSTrond Myklebust }
27173e39aaaSTrond Myklebust 
nfs4_reset_slot_table(struct nfs4_slot_table * tbl,u32 server_highest_slotid,u32 ivalue)27273e39aaaSTrond Myklebust static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
27373e39aaaSTrond Myklebust 		u32 server_highest_slotid,
27473e39aaaSTrond Myklebust 		u32 ivalue)
27573e39aaaSTrond Myklebust {
27673e39aaaSTrond Myklebust 	struct nfs4_slot **p;
27773e39aaaSTrond Myklebust 
27873e39aaaSTrond Myklebust 	nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
27973e39aaaSTrond Myklebust 	p = &tbl->slots;
28073e39aaaSTrond Myklebust 	while (*p) {
28173e39aaaSTrond Myklebust 		(*p)->seq_nr = ivalue;
2823453d570STrond Myklebust 		(*p)->seq_nr_highest_sent = ivalue;
2833453d570STrond Myklebust 		(*p)->seq_nr_last_acked = ivalue - 1;
28473e39aaaSTrond Myklebust 		p = &(*p)->next;
28573e39aaaSTrond Myklebust 	}
28673e39aaaSTrond Myklebust 	tbl->highest_used_slotid = NFS4_NO_SLOT;
28773e39aaaSTrond Myklebust 	tbl->target_highest_slotid = server_highest_slotid;
28873e39aaaSTrond Myklebust 	tbl->server_highest_slotid = server_highest_slotid;
2891fa80644STrond Myklebust 	tbl->d_target_highest_slotid = 0;
2901fa80644STrond Myklebust 	tbl->d2_target_highest_slotid = 0;
29173e39aaaSTrond Myklebust 	tbl->max_slotid = server_highest_slotid;
29273e39aaaSTrond Myklebust }
29373e39aaaSTrond Myklebust 
29473e39aaaSTrond Myklebust /*
29573e39aaaSTrond Myklebust  * (re)Initialise a slot table
29673e39aaaSTrond Myklebust  */
nfs4_realloc_slot_table(struct nfs4_slot_table * tbl,u32 max_reqs,u32 ivalue)29773e39aaaSTrond Myklebust static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
29873e39aaaSTrond Myklebust 		u32 max_reqs, u32 ivalue)
29973e39aaaSTrond Myklebust {
30073e39aaaSTrond Myklebust 	int ret;
30173e39aaaSTrond Myklebust 
302e8d92382SChuck Lever 	dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
30373e39aaaSTrond Myklebust 		max_reqs, tbl->max_slots);
30473e39aaaSTrond Myklebust 
30573e39aaaSTrond Myklebust 	if (max_reqs > NFS4_MAX_SLOT_TABLE)
30673e39aaaSTrond Myklebust 		max_reqs = NFS4_MAX_SLOT_TABLE;
30773e39aaaSTrond Myklebust 
30873e39aaaSTrond Myklebust 	ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
30973e39aaaSTrond Myklebust 	if (ret)
31073e39aaaSTrond Myklebust 		goto out;
31173e39aaaSTrond Myklebust 
31273e39aaaSTrond Myklebust 	spin_lock(&tbl->slot_tbl_lock);
31373e39aaaSTrond Myklebust 	nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
31473e39aaaSTrond Myklebust 	spin_unlock(&tbl->slot_tbl_lock);
31573e39aaaSTrond Myklebust 
316e8d92382SChuck Lever 	dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
31773e39aaaSTrond Myklebust 		tbl, tbl->slots, tbl->max_slots);
31873e39aaaSTrond Myklebust out:
31973e39aaaSTrond Myklebust 	dprintk("<-- %s: return %d\n", __func__, ret);
32073e39aaaSTrond Myklebust 	return ret;
32173e39aaaSTrond Myklebust }
32273e39aaaSTrond Myklebust 
32320b9a902STrond Myklebust /*
32420b9a902STrond Myklebust  * nfs4_release_slot_table - release all slot table entries
32520b9a902STrond Myklebust  */
nfs4_release_slot_table(struct nfs4_slot_table * tbl)32620b9a902STrond Myklebust static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
32720b9a902STrond Myklebust {
32820b9a902STrond Myklebust 	nfs4_shrink_slot_table(tbl, 0);
32920b9a902STrond Myklebust }
33020b9a902STrond Myklebust 
331744aa525SChuck Lever /**
33220b9a902STrond Myklebust  * nfs4_shutdown_slot_table - release resources attached to a slot table
333eb2a1cd3SChuck Lever  * @tbl: slot table to shut down
334eb2a1cd3SChuck Lever  *
335eb2a1cd3SChuck Lever  */
nfs4_shutdown_slot_table(struct nfs4_slot_table * tbl)33620b9a902STrond Myklebust void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
337eb2a1cd3SChuck Lever {
33820b9a902STrond Myklebust 	nfs4_release_slot_table(tbl);
33920b9a902STrond Myklebust 	rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
340eb2a1cd3SChuck Lever }
341eb2a1cd3SChuck Lever 
342eb2a1cd3SChuck Lever /**
343744aa525SChuck Lever  * nfs4_setup_slot_table - prepare a stand-alone slot table for use
344744aa525SChuck Lever  * @tbl: slot table to set up
345744aa525SChuck Lever  * @max_reqs: maximum number of requests allowed
346744aa525SChuck Lever  * @queue: name to give RPC wait queue
347744aa525SChuck Lever  *
348744aa525SChuck Lever  * Returns zero on success, or a negative errno.
349744aa525SChuck Lever  */
nfs4_setup_slot_table(struct nfs4_slot_table * tbl,unsigned int max_reqs,const char * queue)350744aa525SChuck Lever int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
351744aa525SChuck Lever 		const char *queue)
352744aa525SChuck Lever {
353744aa525SChuck Lever 	nfs4_init_slot_table(tbl, queue);
354744aa525SChuck Lever 	return nfs4_realloc_slot_table(tbl, max_reqs, 0);
355744aa525SChuck Lever }
356744aa525SChuck Lever 
nfs41_assign_slot(struct rpc_task * task,void * pslot)357b75ad4cdSTrond Myklebust static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
358b75ad4cdSTrond Myklebust {
359b75ad4cdSTrond Myklebust 	struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
360b75ad4cdSTrond Myklebust 	struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
361b75ad4cdSTrond Myklebust 	struct nfs4_slot *slot = pslot;
362b75ad4cdSTrond Myklebust 	struct nfs4_slot_table *tbl = slot->table;
363b75ad4cdSTrond Myklebust 
364774d5f14SAndy Adamson 	if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
365b75ad4cdSTrond Myklebust 		return false;
366b75ad4cdSTrond Myklebust 	slot->generation = tbl->generation;
367b75ad4cdSTrond Myklebust 	args->sa_slot = slot;
3688e63b6a8STrond Myklebust 	res->sr_timestamp = jiffies;
369b75ad4cdSTrond Myklebust 	res->sr_slot = slot;
370b75ad4cdSTrond Myklebust 	res->sr_status_flags = 0;
371b75ad4cdSTrond Myklebust 	res->sr_status = 1;
372b75ad4cdSTrond Myklebust 	return true;
373b75ad4cdSTrond Myklebust }
374b75ad4cdSTrond Myklebust 
__nfs41_wake_and_assign_slot(struct nfs4_slot_table * tbl,struct nfs4_slot * slot)375b75ad4cdSTrond Myklebust static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
376b75ad4cdSTrond Myklebust 		struct nfs4_slot *slot)
377b75ad4cdSTrond Myklebust {
378b75ad4cdSTrond Myklebust 	if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
379b75ad4cdSTrond Myklebust 		return true;
380b75ad4cdSTrond Myklebust 	return false;
381b75ad4cdSTrond Myklebust }
382b75ad4cdSTrond Myklebust 
nfs41_wake_and_assign_slot(struct nfs4_slot_table * tbl,struct nfs4_slot * slot)383b75ad4cdSTrond Myklebust bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
384b75ad4cdSTrond Myklebust 		struct nfs4_slot *slot)
385b75ad4cdSTrond Myklebust {
386b75ad4cdSTrond Myklebust 	if (slot->slot_nr > tbl->max_slotid)
387b75ad4cdSTrond Myklebust 		return false;
388b75ad4cdSTrond Myklebust 	return __nfs41_wake_and_assign_slot(tbl, slot);
389b75ad4cdSTrond Myklebust }
390b75ad4cdSTrond Myklebust 
nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table * tbl)391b75ad4cdSTrond Myklebust static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
392b75ad4cdSTrond Myklebust {
393b75ad4cdSTrond Myklebust 	struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
394b75ad4cdSTrond Myklebust 	if (!IS_ERR(slot)) {
395b75ad4cdSTrond Myklebust 		bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
396b75ad4cdSTrond Myklebust 		if (ret)
397b75ad4cdSTrond Myklebust 			return ret;
398b75ad4cdSTrond Myklebust 		nfs4_free_slot(tbl, slot);
399b75ad4cdSTrond Myklebust 	}
400b75ad4cdSTrond Myklebust 	return false;
401b75ad4cdSTrond Myklebust }
402b75ad4cdSTrond Myklebust 
nfs41_wake_slot_table(struct nfs4_slot_table * tbl)403b75ad4cdSTrond Myklebust void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
404b75ad4cdSTrond Myklebust {
405b75ad4cdSTrond Myklebust 	for (;;) {
406b75ad4cdSTrond Myklebust 		if (!nfs41_try_wake_next_slot_table_entry(tbl))
407b75ad4cdSTrond Myklebust 			break;
408b75ad4cdSTrond Myklebust 	}
409b75ad4cdSTrond Myklebust }
410b75ad4cdSTrond Myklebust 
4111cec16abSChuck Lever #if defined(CONFIG_NFS_V4_1)
4121cec16abSChuck Lever 
nfs41_set_max_slotid_locked(struct nfs4_slot_table * tbl,u32 target_highest_slotid)413b0ef9647STrond Myklebust static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
414b0ef9647STrond Myklebust 		u32 target_highest_slotid)
415b0ef9647STrond Myklebust {
416b0ef9647STrond Myklebust 	u32 max_slotid;
417b0ef9647STrond Myklebust 
418b0ef9647STrond Myklebust 	max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
419b0ef9647STrond Myklebust 	if (max_slotid > tbl->server_highest_slotid)
420b0ef9647STrond Myklebust 		max_slotid = tbl->server_highest_slotid;
421b0ef9647STrond Myklebust 	if (max_slotid > tbl->target_highest_slotid)
422b0ef9647STrond Myklebust 		max_slotid = tbl->target_highest_slotid;
423b0ef9647STrond Myklebust 	tbl->max_slotid = max_slotid;
424b0ef9647STrond Myklebust 	nfs41_wake_slot_table(tbl);
425b0ef9647STrond Myklebust }
426b0ef9647STrond Myklebust 
42773e39aaaSTrond Myklebust /* Update the client's idea of target_highest_slotid */
nfs41_set_target_slotid_locked(struct nfs4_slot_table * tbl,u32 target_highest_slotid)42873e39aaaSTrond Myklebust static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
42973e39aaaSTrond Myklebust 		u32 target_highest_slotid)
43073e39aaaSTrond Myklebust {
43173e39aaaSTrond Myklebust 	if (tbl->target_highest_slotid == target_highest_slotid)
43273e39aaaSTrond Myklebust 		return;
43373e39aaaSTrond Myklebust 	tbl->target_highest_slotid = target_highest_slotid;
43473e39aaaSTrond Myklebust 	tbl->generation++;
43573e39aaaSTrond Myklebust }
43673e39aaaSTrond Myklebust 
nfs41_set_target_slotid(struct nfs4_slot_table * tbl,u32 target_highest_slotid)43773e39aaaSTrond Myklebust void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
43873e39aaaSTrond Myklebust 		u32 target_highest_slotid)
43973e39aaaSTrond Myklebust {
44073e39aaaSTrond Myklebust 	spin_lock(&tbl->slot_tbl_lock);
44173e39aaaSTrond Myklebust 	nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
4421fa80644STrond Myklebust 	tbl->d_target_highest_slotid = 0;
4431fa80644STrond Myklebust 	tbl->d2_target_highest_slotid = 0;
444b0ef9647STrond Myklebust 	nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
44573e39aaaSTrond Myklebust 	spin_unlock(&tbl->slot_tbl_lock);
44673e39aaaSTrond Myklebust }
44773e39aaaSTrond Myklebust 
nfs41_set_server_slotid_locked(struct nfs4_slot_table * tbl,u32 highest_slotid)44873e39aaaSTrond Myklebust static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
44973e39aaaSTrond Myklebust 		u32 highest_slotid)
45073e39aaaSTrond Myklebust {
45173e39aaaSTrond Myklebust 	if (tbl->server_highest_slotid == highest_slotid)
45273e39aaaSTrond Myklebust 		return;
45373e39aaaSTrond Myklebust 	if (tbl->highest_used_slotid > highest_slotid)
45473e39aaaSTrond Myklebust 		return;
45573e39aaaSTrond Myklebust 	/* Deallocate slots */
45673e39aaaSTrond Myklebust 	nfs4_shrink_slot_table(tbl, highest_slotid + 1);
45773e39aaaSTrond Myklebust 	tbl->server_highest_slotid = highest_slotid;
45873e39aaaSTrond Myklebust }
45973e39aaaSTrond Myklebust 
nfs41_derivative_target_slotid(s32 s1,s32 s2)4601fa80644STrond Myklebust static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
4611fa80644STrond Myklebust {
4621fa80644STrond Myklebust 	s1 -= s2;
4631fa80644STrond Myklebust 	if (s1 == 0)
4641fa80644STrond Myklebust 		return 0;
4651fa80644STrond Myklebust 	if (s1 < 0)
4661fa80644STrond Myklebust 		return (s1 - 1) >> 1;
4671fa80644STrond Myklebust 	return (s1 + 1) >> 1;
4681fa80644STrond Myklebust }
4691fa80644STrond Myklebust 
nfs41_sign_s32(s32 s1)4701fa80644STrond Myklebust static int nfs41_sign_s32(s32 s1)
4711fa80644STrond Myklebust {
4721fa80644STrond Myklebust 	if (s1 > 0)
4731fa80644STrond Myklebust 		return 1;
4741fa80644STrond Myklebust 	if (s1 < 0)
4751fa80644STrond Myklebust 		return -1;
4761fa80644STrond Myklebust 	return 0;
4771fa80644STrond Myklebust }
4781fa80644STrond Myklebust 
nfs41_same_sign_or_zero_s32(s32 s1,s32 s2)4791fa80644STrond Myklebust static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
4801fa80644STrond Myklebust {
4811fa80644STrond Myklebust 	if (!s1 || !s2)
4821fa80644STrond Myklebust 		return true;
4831fa80644STrond Myklebust 	return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
4841fa80644STrond Myklebust }
4851fa80644STrond Myklebust 
4861fa80644STrond Myklebust /* Try to eliminate outliers by checking for sharp changes in the
4871fa80644STrond Myklebust  * derivatives and second derivatives
4881fa80644STrond Myklebust  */
nfs41_is_outlier_target_slotid(struct nfs4_slot_table * tbl,u32 new_target)4891fa80644STrond Myklebust static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
4901fa80644STrond Myklebust 		u32 new_target)
4911fa80644STrond Myklebust {
4921fa80644STrond Myklebust 	s32 d_target, d2_target;
4931fa80644STrond Myklebust 	bool ret = true;
4941fa80644STrond Myklebust 
4951fa80644STrond Myklebust 	d_target = nfs41_derivative_target_slotid(new_target,
4961fa80644STrond Myklebust 			tbl->target_highest_slotid);
4971fa80644STrond Myklebust 	d2_target = nfs41_derivative_target_slotid(d_target,
4981fa80644STrond Myklebust 			tbl->d_target_highest_slotid);
4991fa80644STrond Myklebust 	/* Is first derivative same sign? */
5001fa80644STrond Myklebust 	if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
5011fa80644STrond Myklebust 		ret = false;
5021fa80644STrond Myklebust 	/* Is second derivative same sign? */
5031fa80644STrond Myklebust 	if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
5041fa80644STrond Myklebust 		ret = false;
5051fa80644STrond Myklebust 	tbl->d_target_highest_slotid = d_target;
5061fa80644STrond Myklebust 	tbl->d2_target_highest_slotid = d2_target;
5071fa80644STrond Myklebust 	return ret;
5081fa80644STrond Myklebust }
5091fa80644STrond Myklebust 
nfs41_update_target_slotid(struct nfs4_slot_table * tbl,struct nfs4_slot * slot,struct nfs4_sequence_res * res)51073e39aaaSTrond Myklebust void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
51173e39aaaSTrond Myklebust 		struct nfs4_slot *slot,
51273e39aaaSTrond Myklebust 		struct nfs4_sequence_res *res)
51373e39aaaSTrond Myklebust {
514*f96f8cc4STrond Myklebust 	u32 target_highest_slotid = min(res->sr_target_highest_slotid,
515*f96f8cc4STrond Myklebust 					NFS4_MAX_SLOTID);
516*f96f8cc4STrond Myklebust 	u32 highest_slotid = min(res->sr_highest_slotid, NFS4_MAX_SLOTID);
517*f96f8cc4STrond Myklebust 
51873e39aaaSTrond Myklebust 	spin_lock(&tbl->slot_tbl_lock);
519*f96f8cc4STrond Myklebust 	if (!nfs41_is_outlier_target_slotid(tbl, target_highest_slotid))
520*f96f8cc4STrond Myklebust 		nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
5211fa80644STrond Myklebust 	if (tbl->generation == slot->generation)
522*f96f8cc4STrond Myklebust 		nfs41_set_server_slotid_locked(tbl, highest_slotid);
523*f96f8cc4STrond Myklebust 	nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
52473e39aaaSTrond Myklebust 	spin_unlock(&tbl->slot_tbl_lock);
52573e39aaaSTrond Myklebust }
52673e39aaaSTrond Myklebust 
nfs4_release_session_slot_tables(struct nfs4_session * session)52720b9a902STrond Myklebust static void nfs4_release_session_slot_tables(struct nfs4_session *session)
5289d33059cSChuck Lever {
529eb2a1cd3SChuck Lever 	nfs4_release_slot_table(&session->fc_slot_table);
530eb2a1cd3SChuck Lever 	nfs4_release_slot_table(&session->bc_slot_table);
5319d33059cSChuck Lever }
5329d33059cSChuck Lever 
53373e39aaaSTrond Myklebust /*
53473e39aaaSTrond Myklebust  * Initialize or reset the forechannel and backchannel tables
53573e39aaaSTrond Myklebust  */
nfs4_setup_session_slot_tables(struct nfs4_session * ses)53673e39aaaSTrond Myklebust int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
53773e39aaaSTrond Myklebust {
53873e39aaaSTrond Myklebust 	struct nfs4_slot_table *tbl;
53973e39aaaSTrond Myklebust 	int status;
54073e39aaaSTrond Myklebust 
54173e39aaaSTrond Myklebust 	dprintk("--> %s\n", __func__);
54273e39aaaSTrond Myklebust 	/* Fore channel */
54373e39aaaSTrond Myklebust 	tbl = &ses->fc_slot_table;
54473e39aaaSTrond Myklebust 	tbl->session = ses;
54573e39aaaSTrond Myklebust 	status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
546b1c0df5fSTrond Myklebust 	if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */
54773e39aaaSTrond Myklebust 		return status;
54873e39aaaSTrond Myklebust 	/* Back channel */
54973e39aaaSTrond Myklebust 	tbl = &ses->bc_slot_table;
55073e39aaaSTrond Myklebust 	tbl->session = ses;
55173e39aaaSTrond Myklebust 	status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
55273e39aaaSTrond Myklebust 	if (status && tbl->slots == NULL)
55373e39aaaSTrond Myklebust 		/* Fore and back channel share a connection so get
55473e39aaaSTrond Myklebust 		 * both slot tables or neither */
55520b9a902STrond Myklebust 		nfs4_release_session_slot_tables(ses);
55673e39aaaSTrond Myklebust 	return status;
55773e39aaaSTrond Myklebust }
55873e39aaaSTrond Myklebust 
nfs4_alloc_session(struct nfs_client * clp)55973e39aaaSTrond Myklebust struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
56073e39aaaSTrond Myklebust {
56173e39aaaSTrond Myklebust 	struct nfs4_session *session;
56273e39aaaSTrond Myklebust 
56373e39aaaSTrond Myklebust 	session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
56473e39aaaSTrond Myklebust 	if (!session)
56573e39aaaSTrond Myklebust 		return NULL;
56673e39aaaSTrond Myklebust 
567744aa525SChuck Lever 	nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
568744aa525SChuck Lever 	nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
56973e39aaaSTrond Myklebust 	session->session_state = 1<<NFS4_SESSION_INITING;
57073e39aaaSTrond Myklebust 
57173e39aaaSTrond Myklebust 	session->clp = clp;
57273e39aaaSTrond Myklebust 	return session;
57373e39aaaSTrond Myklebust }
57473e39aaaSTrond Myklebust 
nfs4_destroy_session_slot_tables(struct nfs4_session * session)57520b9a902STrond Myklebust static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
57620b9a902STrond Myklebust {
57720b9a902STrond Myklebust 	nfs4_shutdown_slot_table(&session->fc_slot_table);
57820b9a902STrond Myklebust 	nfs4_shutdown_slot_table(&session->bc_slot_table);
57920b9a902STrond Myklebust }
58020b9a902STrond Myklebust 
nfs4_destroy_session(struct nfs4_session * session)58173e39aaaSTrond Myklebust void nfs4_destroy_session(struct nfs4_session *session)
58273e39aaaSTrond Myklebust {
58373e39aaaSTrond Myklebust 	struct rpc_xprt *xprt;
584a52458b4SNeilBrown 	const struct cred *cred;
58573e39aaaSTrond Myklebust 
58673d8bde5SChuck Lever 	cred = nfs4_get_clid_cred(session->clp);
58773e39aaaSTrond Myklebust 	nfs4_proc_destroy_session(session, cred);
588a52458b4SNeilBrown 	put_cred(cred);
58973e39aaaSTrond Myklebust 
59073e39aaaSTrond Myklebust 	rcu_read_lock();
59173e39aaaSTrond Myklebust 	xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
59273e39aaaSTrond Myklebust 	rcu_read_unlock();
59373e39aaaSTrond Myklebust 	dprintk("%s Destroy backchannel for xprt %p\n",
59473e39aaaSTrond Myklebust 		__func__, xprt);
59573e39aaaSTrond Myklebust 	xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
596eb2a1cd3SChuck Lever 	nfs4_destroy_session_slot_tables(session);
59773e39aaaSTrond Myklebust 	kfree(session);
59873e39aaaSTrond Myklebust }
59973e39aaaSTrond Myklebust 
60073e39aaaSTrond Myklebust /*
60173e39aaaSTrond Myklebust  * With sessions, the client is not marked ready until after a
60273e39aaaSTrond Myklebust  * successful EXCHANGE_ID and CREATE_SESSION.
60373e39aaaSTrond Myklebust  *
60473e39aaaSTrond Myklebust  * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
60573e39aaaSTrond Myklebust  * other versions of NFS can be tried.
60673e39aaaSTrond Myklebust  */
nfs41_check_session_ready(struct nfs_client * clp)60773e39aaaSTrond Myklebust static int nfs41_check_session_ready(struct nfs_client *clp)
60873e39aaaSTrond Myklebust {
60973e39aaaSTrond Myklebust 	int ret;
61073e39aaaSTrond Myklebust 
61173e39aaaSTrond Myklebust 	if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
61273e39aaaSTrond Myklebust 		ret = nfs4_client_recover_expired_lease(clp);
61373e39aaaSTrond Myklebust 		if (ret)
61473e39aaaSTrond Myklebust 			return ret;
61573e39aaaSTrond Myklebust 	}
61673e39aaaSTrond Myklebust 	if (clp->cl_cons_state < NFS_CS_READY)
61773e39aaaSTrond Myklebust 		return -EPROTONOSUPPORT;
61873e39aaaSTrond Myklebust 	smp_rmb();
61973e39aaaSTrond Myklebust 	return 0;
62073e39aaaSTrond Myklebust }
62173e39aaaSTrond Myklebust 
nfs4_init_session(struct nfs_client * clp)62218aad3d5SAndy Adamson int nfs4_init_session(struct nfs_client *clp)
62373e39aaaSTrond Myklebust {
62473e39aaaSTrond Myklebust 	if (!nfs4_has_session(clp))
62573e39aaaSTrond Myklebust 		return 0;
62673e39aaaSTrond Myklebust 
62718aad3d5SAndy Adamson 	clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
62873e39aaaSTrond Myklebust 	return nfs41_check_session_ready(clp);
62973e39aaaSTrond Myklebust }
63073e39aaaSTrond Myklebust 
nfs4_init_ds_session(struct nfs_client * clp,unsigned long lease_time)63173e39aaaSTrond Myklebust int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
63273e39aaaSTrond Myklebust {
63373e39aaaSTrond Myklebust 	struct nfs4_session *session = clp->cl_session;
63473e39aaaSTrond Myklebust 	int ret;
63573e39aaaSTrond Myklebust 
63673e39aaaSTrond Myklebust 	spin_lock(&clp->cl_lock);
63773e39aaaSTrond Myklebust 	if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
63873e39aaaSTrond Myklebust 		/*
63973e39aaaSTrond Myklebust 		 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
64073e39aaaSTrond Myklebust 		 * DS lease to be equal to the MDS lease.
64173e39aaaSTrond Myklebust 		 */
64273e39aaaSTrond Myklebust 		clp->cl_lease_time = lease_time;
64373e39aaaSTrond Myklebust 		clp->cl_last_renewal = jiffies;
64473e39aaaSTrond Myklebust 	}
64573e39aaaSTrond Myklebust 	spin_unlock(&clp->cl_lock);
64673e39aaaSTrond Myklebust 
64773e39aaaSTrond Myklebust 	ret = nfs41_check_session_ready(clp);
64873e39aaaSTrond Myklebust 	if (ret)
64973e39aaaSTrond Myklebust 		return ret;
65073e39aaaSTrond Myklebust 	/* Test for the DS role */
65173e39aaaSTrond Myklebust 	if (!is_ds_client(clp))
65273e39aaaSTrond Myklebust 		return -ENODEV;
65373e39aaaSTrond Myklebust 	return 0;
65473e39aaaSTrond Myklebust }
65573e39aaaSTrond Myklebust EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
65673e39aaaSTrond Myklebust 
6579d33059cSChuck Lever #endif	/* defined(CONFIG_NFS_V4_1) */
658