Linux Audio

Check our new training course

Loading...
v4.17
  1/*
  2 * fs/nfs/nfs4session.c
  3 *
  4 * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com>
  5 *
  6 */
  7#include <linux/kernel.h>
  8#include <linux/errno.h>
  9#include <linux/string.h>
 10#include <linux/printk.h>
 11#include <linux/slab.h>
 12#include <linux/sunrpc/sched.h>
 13#include <linux/sunrpc/bc_xprt.h>
 14#include <linux/nfs.h>
 15#include <linux/nfs4.h>
 16#include <linux/nfs_fs.h>
 17#include <linux/module.h>
 18
 19#include "nfs4_fs.h"
 20#include "internal.h"
 21#include "nfs4session.h"
 22#include "callback.h"
 23
 24#define NFSDBG_FACILITY		NFSDBG_STATE
 25
 26static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue)
 27{
 28	tbl->highest_used_slotid = NFS4_NO_SLOT;
 29	spin_lock_init(&tbl->slot_tbl_lock);
 30	rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, queue);
 31	init_waitqueue_head(&tbl->slot_waitq);
 32	init_completion(&tbl->complete);
 33}
 34
 35/*
 36 * nfs4_shrink_slot_table - free retired slots from the slot table
 37 */
 38static void nfs4_shrink_slot_table(struct nfs4_slot_table  *tbl, u32 newsize)
 39{
 40	struct nfs4_slot **p;
 41	if (newsize >= tbl->max_slots)
 42		return;
 43
 44	p = &tbl->slots;
 45	while (newsize--)
 46		p = &(*p)->next;
 47	while (*p) {
 48		struct nfs4_slot *slot = *p;
 49
 50		*p = slot->next;
 51		kfree(slot);
 52		tbl->max_slots--;
 53	}
 54}
 55
 56/**
 57 * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
 58 * @tbl - controlling slot table
 59 *
 60 */
 61void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
 62{
 63	if (nfs4_slot_tbl_draining(tbl))
 64		complete(&tbl->complete);
 65}
 66
 67/*
 68 * nfs4_free_slot - free a slot and efficiently update slot table.
 69 *
 70 * freeing a slot is trivially done by clearing its respective bit
 71 * in the bitmap.
 72 * If the freed slotid equals highest_used_slotid we want to update it
 73 * so that the server would be able to size down the slot table if needed,
 74 * otherwise we know that the highest_used_slotid is still in use.
 75 * When updating highest_used_slotid there may be "holes" in the bitmap
 76 * so we need to scan down from highest_used_slotid to 0 looking for the now
 77 * highest slotid in use.
 78 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
 79 *
 80 * Must be called while holding tbl->slot_tbl_lock
 81 */
 82void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
 83{
 84	u32 slotid = slot->slot_nr;
 85
 86	/* clear used bit in bitmap */
 87	__clear_bit(slotid, tbl->used_slots);
 88
 89	/* update highest_used_slotid when it is freed */
 90	if (slotid == tbl->highest_used_slotid) {
 91		u32 new_max = find_last_bit(tbl->used_slots, slotid);
 92		if (new_max < slotid)
 93			tbl->highest_used_slotid = new_max;
 94		else {
 95			tbl->highest_used_slotid = NFS4_NO_SLOT;
 96			nfs4_slot_tbl_drain_complete(tbl);
 97		}
 98	}
 99	dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
100		slotid, tbl->highest_used_slotid);
101}
102
103static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table  *tbl,
104		u32 slotid, u32 seq_init, gfp_t gfp_mask)
105{
106	struct nfs4_slot *slot;
107
108	slot = kzalloc(sizeof(*slot), gfp_mask);
109	if (slot) {
110		slot->table = tbl;
111		slot->slot_nr = slotid;
112		slot->seq_nr = seq_init;
113	}
114	return slot;
115}
116
117static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table  *tbl,
118		u32 slotid, u32 seq_init, gfp_t gfp_mask)
119{
120	struct nfs4_slot **p, *slot;
121
122	p = &tbl->slots;
123	for (;;) {
124		if (*p == NULL) {
125			*p = nfs4_new_slot(tbl, tbl->max_slots,
126					seq_init, gfp_mask);
127			if (*p == NULL)
128				break;
129			tbl->max_slots++;
130		}
131		slot = *p;
132		if (slot->slot_nr == slotid)
133			return slot;
134		p = &slot->next;
135	}
136	return ERR_PTR(-ENOMEM);
137}
138
139static void nfs4_lock_slot(struct nfs4_slot_table *tbl,
140		struct nfs4_slot *slot)
141{
142	u32 slotid = slot->slot_nr;
143
144	__set_bit(slotid, tbl->used_slots);
145	if (slotid > tbl->highest_used_slotid ||
146	    tbl->highest_used_slotid == NFS4_NO_SLOT)
147		tbl->highest_used_slotid = slotid;
148	slot->generation = tbl->generation;
149}
150
151/*
152 * nfs4_try_to_lock_slot - Given a slot try to allocate it
153 *
154 * Note: must be called with the slot_tbl_lock held.
155 */
156bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
157{
158	if (nfs4_test_locked_slot(tbl, slot->slot_nr))
159		return false;
160	nfs4_lock_slot(tbl, slot);
161	return true;
162}
163
164/*
165 * nfs4_lookup_slot - Find a slot but don't allocate it
166 *
167 * Note: must be called with the slot_tbl_lock held.
168 */
169struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid)
170{
171	if (slotid <= tbl->max_slotid)
172		return nfs4_find_or_create_slot(tbl, slotid, 0, GFP_NOWAIT);
173	return ERR_PTR(-E2BIG);
174}
175
176static int nfs4_slot_get_seqid(struct nfs4_slot_table  *tbl, u32 slotid,
177		u32 *seq_nr)
178	__must_hold(&tbl->slot_tbl_lock)
179{
180	struct nfs4_slot *slot;
181	int ret;
182
183	slot = nfs4_lookup_slot(tbl, slotid);
184	ret = PTR_ERR_OR_ZERO(slot);
185	if (!ret)
186		*seq_nr = slot->seq_nr;
187
188	return ret;
189}
190
191/*
192 * nfs4_slot_seqid_in_use - test if a slot sequence id is still in use
193 *
194 * Given a slot table, slot id and sequence number, determine if the
195 * RPC call in question is still in flight. This function is mainly
196 * intended for use by the callback channel.
197 */
198static bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl,
199		u32 slotid, u32 seq_nr)
200{
201	u32 cur_seq = 0;
202	bool ret = false;
203
204	spin_lock(&tbl->slot_tbl_lock);
205	if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 &&
206	    cur_seq == seq_nr && test_bit(slotid, tbl->used_slots))
207		ret = true;
208	spin_unlock(&tbl->slot_tbl_lock);
209	return ret;
210}
211
212/*
213 * nfs4_slot_wait_on_seqid - wait until a slot sequence id is complete
214 *
215 * Given a slot table, slot id and sequence number, wait until the
216 * corresponding RPC call completes. This function is mainly
217 * intended for use by the callback channel.
218 */
219int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl,
220		u32 slotid, u32 seq_nr,
221		unsigned long timeout)
222{
223	if (wait_event_timeout(tbl->slot_waitq,
224			!nfs4_slot_seqid_in_use(tbl, slotid, seq_nr),
225			timeout) == 0)
226		return -ETIMEDOUT;
227	return 0;
228}
229
230/*
231 * nfs4_alloc_slot - efficiently look for a free slot
232 *
233 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
234 * If found, we mark the slot as used, update the highest_used_slotid,
235 * and respectively set up the sequence operation args.
236 *
237 * Note: must be called with under the slot_tbl_lock.
238 */
239struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
240{
241	struct nfs4_slot *ret = ERR_PTR(-EBUSY);
242	u32 slotid;
243
244	dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
245		__func__, tbl->used_slots[0], tbl->highest_used_slotid,
246		tbl->max_slotid + 1);
247	slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
248	if (slotid <= tbl->max_slotid) {
249		ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
250		if (!IS_ERR(ret))
251			nfs4_lock_slot(tbl, ret);
252	}
253	dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
254		__func__, tbl->used_slots[0], tbl->highest_used_slotid,
255		!IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
256	return ret;
257}
258
259static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
260		 u32 max_reqs, u32 ivalue)
261{
262	if (max_reqs <= tbl->max_slots)
263		return 0;
264	if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
265		return 0;
266	return -ENOMEM;
267}
268
269static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
270		u32 server_highest_slotid,
271		u32 ivalue)
272{
273	struct nfs4_slot **p;
274
275	nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
276	p = &tbl->slots;
277	while (*p) {
278		(*p)->seq_nr = ivalue;
279		(*p)->interrupted = 0;
280		p = &(*p)->next;
281	}
282	tbl->highest_used_slotid = NFS4_NO_SLOT;
283	tbl->target_highest_slotid = server_highest_slotid;
284	tbl->server_highest_slotid = server_highest_slotid;
285	tbl->d_target_highest_slotid = 0;
286	tbl->d2_target_highest_slotid = 0;
287	tbl->max_slotid = server_highest_slotid;
288}
289
290/*
291 * (re)Initialise a slot table
292 */
293static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
294		u32 max_reqs, u32 ivalue)
295{
296	int ret;
297
298	dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
299		max_reqs, tbl->max_slots);
300
301	if (max_reqs > NFS4_MAX_SLOT_TABLE)
302		max_reqs = NFS4_MAX_SLOT_TABLE;
303
304	ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
305	if (ret)
306		goto out;
307
308	spin_lock(&tbl->slot_tbl_lock);
309	nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
310	spin_unlock(&tbl->slot_tbl_lock);
311
312	dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
313		tbl, tbl->slots, tbl->max_slots);
314out:
315	dprintk("<-- %s: return %d\n", __func__, ret);
316	return ret;
317}
318
319/*
320 * nfs4_release_slot_table - release all slot table entries
321 */
322static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
323{
324	nfs4_shrink_slot_table(tbl, 0);
325}
326
327/**
328 * nfs4_shutdown_slot_table - release resources attached to a slot table
329 * @tbl: slot table to shut down
330 *
331 */
332void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
333{
334	nfs4_release_slot_table(tbl);
335	rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
336}
337
338/**
339 * nfs4_setup_slot_table - prepare a stand-alone slot table for use
340 * @tbl: slot table to set up
341 * @max_reqs: maximum number of requests allowed
342 * @queue: name to give RPC wait queue
343 *
344 * Returns zero on success, or a negative errno.
345 */
346int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
347		const char *queue)
348{
349	nfs4_init_slot_table(tbl, queue);
350	return nfs4_realloc_slot_table(tbl, max_reqs, 0);
351}
352
353static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
354{
355	struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
356	struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
357	struct nfs4_slot *slot = pslot;
358	struct nfs4_slot_table *tbl = slot->table;
359
360	if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
361		return false;
362	slot->generation = tbl->generation;
363	args->sa_slot = slot;
364	res->sr_timestamp = jiffies;
365	res->sr_slot = slot;
366	res->sr_status_flags = 0;
367	res->sr_status = 1;
368	return true;
369}
370
371static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
372		struct nfs4_slot *slot)
373{
374	if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
375		return true;
376	return false;
377}
378
379bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
380		struct nfs4_slot *slot)
381{
382	if (slot->slot_nr > tbl->max_slotid)
383		return false;
384	return __nfs41_wake_and_assign_slot(tbl, slot);
385}
386
387static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
388{
389	struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
390	if (!IS_ERR(slot)) {
391		bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
392		if (ret)
393			return ret;
394		nfs4_free_slot(tbl, slot);
395	}
396	return false;
397}
398
399void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
400{
401	for (;;) {
402		if (!nfs41_try_wake_next_slot_table_entry(tbl))
403			break;
404	}
405}
406
407#if defined(CONFIG_NFS_V4_1)
408
409static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
410		u32 target_highest_slotid)
411{
412	u32 max_slotid;
413
414	max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
415	if (max_slotid > tbl->server_highest_slotid)
416		max_slotid = tbl->server_highest_slotid;
417	if (max_slotid > tbl->target_highest_slotid)
418		max_slotid = tbl->target_highest_slotid;
419	tbl->max_slotid = max_slotid;
420	nfs41_wake_slot_table(tbl);
421}
422
423/* Update the client's idea of target_highest_slotid */
424static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
425		u32 target_highest_slotid)
426{
427	if (tbl->target_highest_slotid == target_highest_slotid)
428		return;
429	tbl->target_highest_slotid = target_highest_slotid;
430	tbl->generation++;
431}
432
433void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
434		u32 target_highest_slotid)
435{
436	spin_lock(&tbl->slot_tbl_lock);
437	nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
438	tbl->d_target_highest_slotid = 0;
439	tbl->d2_target_highest_slotid = 0;
440	nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
441	spin_unlock(&tbl->slot_tbl_lock);
442}
443
444static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
445		u32 highest_slotid)
446{
447	if (tbl->server_highest_slotid == highest_slotid)
448		return;
449	if (tbl->highest_used_slotid > highest_slotid)
450		return;
451	/* Deallocate slots */
452	nfs4_shrink_slot_table(tbl, highest_slotid + 1);
453	tbl->server_highest_slotid = highest_slotid;
454}
455
456static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
457{
458	s1 -= s2;
459	if (s1 == 0)
460		return 0;
461	if (s1 < 0)
462		return (s1 - 1) >> 1;
463	return (s1 + 1) >> 1;
464}
465
466static int nfs41_sign_s32(s32 s1)
467{
468	if (s1 > 0)
469		return 1;
470	if (s1 < 0)
471		return -1;
472	return 0;
473}
474
475static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
476{
477	if (!s1 || !s2)
478		return true;
479	return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
480}
481
482/* Try to eliminate outliers by checking for sharp changes in the
483 * derivatives and second derivatives
484 */
485static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
486		u32 new_target)
487{
488	s32 d_target, d2_target;
489	bool ret = true;
490
491	d_target = nfs41_derivative_target_slotid(new_target,
492			tbl->target_highest_slotid);
493	d2_target = nfs41_derivative_target_slotid(d_target,
494			tbl->d_target_highest_slotid);
495	/* Is first derivative same sign? */
496	if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
497		ret = false;
498	/* Is second derivative same sign? */
499	if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
500		ret = false;
501	tbl->d_target_highest_slotid = d_target;
502	tbl->d2_target_highest_slotid = d2_target;
503	return ret;
504}
505
506void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
507		struct nfs4_slot *slot,
508		struct nfs4_sequence_res *res)
509{
510	spin_lock(&tbl->slot_tbl_lock);
511	if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
512		nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
513	if (tbl->generation == slot->generation)
514		nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
515	nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
516	spin_unlock(&tbl->slot_tbl_lock);
517}
518
519static void nfs4_release_session_slot_tables(struct nfs4_session *session)
520{
521	nfs4_release_slot_table(&session->fc_slot_table);
522	nfs4_release_slot_table(&session->bc_slot_table);
523}
524
525/*
526 * Initialize or reset the forechannel and backchannel tables
527 */
528int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
529{
530	struct nfs4_slot_table *tbl;
531	int status;
532
533	dprintk("--> %s\n", __func__);
534	/* Fore channel */
535	tbl = &ses->fc_slot_table;
536	tbl->session = ses;
537	status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
538	if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */
539		return status;
540	/* Back channel */
541	tbl = &ses->bc_slot_table;
542	tbl->session = ses;
543	status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
544	if (status && tbl->slots == NULL)
545		/* Fore and back channel share a connection so get
546		 * both slot tables or neither */
547		nfs4_release_session_slot_tables(ses);
548	return status;
549}
550
551struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
552{
553	struct nfs4_session *session;
554
555	session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
556	if (!session)
557		return NULL;
558
559	nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
560	nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
561	session->session_state = 1<<NFS4_SESSION_INITING;
562
563	session->clp = clp;
564	return session;
565}
566
567static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
568{
569	nfs4_shutdown_slot_table(&session->fc_slot_table);
570	nfs4_shutdown_slot_table(&session->bc_slot_table);
571}
572
573void nfs4_destroy_session(struct nfs4_session *session)
574{
575	struct rpc_xprt *xprt;
576	struct rpc_cred *cred;
577
578	cred = nfs4_get_clid_cred(session->clp);
579	nfs4_proc_destroy_session(session, cred);
580	if (cred)
581		put_rpccred(cred);
582
583	rcu_read_lock();
584	xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
585	rcu_read_unlock();
586	dprintk("%s Destroy backchannel for xprt %p\n",
587		__func__, xprt);
588	xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
589	nfs4_destroy_session_slot_tables(session);
590	kfree(session);
591}
592
593/*
594 * With sessions, the client is not marked ready until after a
595 * successful EXCHANGE_ID and CREATE_SESSION.
596 *
597 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
598 * other versions of NFS can be tried.
599 */
600static int nfs41_check_session_ready(struct nfs_client *clp)
601{
602	int ret;
603	
604	if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
605		ret = nfs4_client_recover_expired_lease(clp);
606		if (ret)
607			return ret;
608	}
609	if (clp->cl_cons_state < NFS_CS_READY)
610		return -EPROTONOSUPPORT;
611	smp_rmb();
612	return 0;
613}
614
615int nfs4_init_session(struct nfs_client *clp)
616{
617	if (!nfs4_has_session(clp))
618		return 0;
619
620	clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
621	return nfs41_check_session_ready(clp);
622}
623
624int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
625{
626	struct nfs4_session *session = clp->cl_session;
627	int ret;
628
629	spin_lock(&clp->cl_lock);
630	if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
631		/*
632		 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
633		 * DS lease to be equal to the MDS lease.
634		 */
635		clp->cl_lease_time = lease_time;
636		clp->cl_last_renewal = jiffies;
637	}
638	spin_unlock(&clp->cl_lock);
639
640	ret = nfs41_check_session_ready(clp);
641	if (ret)
642		return ret;
643	/* Test for the DS role */
644	if (!is_ds_client(clp))
645		return -ENODEV;
646	return 0;
647}
648EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
649
650#endif	/* defined(CONFIG_NFS_V4_1) */
v4.6
  1/*
  2 * fs/nfs/nfs4session.c
  3 *
  4 * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com>
  5 *
  6 */
  7#include <linux/kernel.h>
  8#include <linux/errno.h>
  9#include <linux/string.h>
 10#include <linux/printk.h>
 11#include <linux/slab.h>
 12#include <linux/sunrpc/sched.h>
 13#include <linux/sunrpc/bc_xprt.h>
 14#include <linux/nfs.h>
 15#include <linux/nfs4.h>
 16#include <linux/nfs_fs.h>
 17#include <linux/module.h>
 18
 19#include "nfs4_fs.h"
 20#include "internal.h"
 21#include "nfs4session.h"
 22#include "callback.h"
 23
 24#define NFSDBG_FACILITY		NFSDBG_STATE
 25
 26static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue)
 27{
 28	tbl->highest_used_slotid = NFS4_NO_SLOT;
 29	spin_lock_init(&tbl->slot_tbl_lock);
 30	rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, queue);
 
 31	init_completion(&tbl->complete);
 32}
 33
 34/*
 35 * nfs4_shrink_slot_table - free retired slots from the slot table
 36 */
 37static void nfs4_shrink_slot_table(struct nfs4_slot_table  *tbl, u32 newsize)
 38{
 39	struct nfs4_slot **p;
 40	if (newsize >= tbl->max_slots)
 41		return;
 42
 43	p = &tbl->slots;
 44	while (newsize--)
 45		p = &(*p)->next;
 46	while (*p) {
 47		struct nfs4_slot *slot = *p;
 48
 49		*p = slot->next;
 50		kfree(slot);
 51		tbl->max_slots--;
 52	}
 53}
 54
 55/**
 56 * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
 57 * @tbl - controlling slot table
 58 *
 59 */
 60void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
 61{
 62	if (nfs4_slot_tbl_draining(tbl))
 63		complete(&tbl->complete);
 64}
 65
 66/*
 67 * nfs4_free_slot - free a slot and efficiently update slot table.
 68 *
 69 * freeing a slot is trivially done by clearing its respective bit
 70 * in the bitmap.
 71 * If the freed slotid equals highest_used_slotid we want to update it
 72 * so that the server would be able to size down the slot table if needed,
 73 * otherwise we know that the highest_used_slotid is still in use.
 74 * When updating highest_used_slotid there may be "holes" in the bitmap
 75 * so we need to scan down from highest_used_slotid to 0 looking for the now
 76 * highest slotid in use.
 77 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
 78 *
 79 * Must be called while holding tbl->slot_tbl_lock
 80 */
 81void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
 82{
 83	u32 slotid = slot->slot_nr;
 84
 85	/* clear used bit in bitmap */
 86	__clear_bit(slotid, tbl->used_slots);
 87
 88	/* update highest_used_slotid when it is freed */
 89	if (slotid == tbl->highest_used_slotid) {
 90		u32 new_max = find_last_bit(tbl->used_slots, slotid);
 91		if (new_max < slotid)
 92			tbl->highest_used_slotid = new_max;
 93		else {
 94			tbl->highest_used_slotid = NFS4_NO_SLOT;
 95			nfs4_slot_tbl_drain_complete(tbl);
 96		}
 97	}
 98	dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
 99		slotid, tbl->highest_used_slotid);
100}
101
102static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table  *tbl,
103		u32 slotid, u32 seq_init, gfp_t gfp_mask)
104{
105	struct nfs4_slot *slot;
106
107	slot = kzalloc(sizeof(*slot), gfp_mask);
108	if (slot) {
109		slot->table = tbl;
110		slot->slot_nr = slotid;
111		slot->seq_nr = seq_init;
112	}
113	return slot;
114}
115
116static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table  *tbl,
117		u32 slotid, u32 seq_init, gfp_t gfp_mask)
118{
119	struct nfs4_slot **p, *slot;
120
121	p = &tbl->slots;
122	for (;;) {
123		if (*p == NULL) {
124			*p = nfs4_new_slot(tbl, tbl->max_slots,
125					seq_init, gfp_mask);
126			if (*p == NULL)
127				break;
128			tbl->max_slots++;
129		}
130		slot = *p;
131		if (slot->slot_nr == slotid)
132			return slot;
133		p = &slot->next;
134	}
135	return ERR_PTR(-ENOMEM);
136}
137
138static void nfs4_lock_slot(struct nfs4_slot_table *tbl,
139		struct nfs4_slot *slot)
140{
141	u32 slotid = slot->slot_nr;
142
143	__set_bit(slotid, tbl->used_slots);
144	if (slotid > tbl->highest_used_slotid ||
145	    tbl->highest_used_slotid == NFS4_NO_SLOT)
146		tbl->highest_used_slotid = slotid;
147	slot->generation = tbl->generation;
148}
149
150/*
151 * nfs4_try_to_lock_slot - Given a slot try to allocate it
152 *
153 * Note: must be called with the slot_tbl_lock held.
154 */
155bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
156{
157	if (nfs4_test_locked_slot(tbl, slot->slot_nr))
158		return false;
159	nfs4_lock_slot(tbl, slot);
160	return true;
161}
162
163/*
164 * nfs4_lookup_slot - Find a slot but don't allocate it
165 *
166 * Note: must be called with the slot_tbl_lock held.
167 */
168struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid)
169{
170	if (slotid <= tbl->max_slotid)
171		return nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
172	return ERR_PTR(-E2BIG);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173}
174
175/*
176 * nfs4_alloc_slot - efficiently look for a free slot
177 *
178 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
179 * If found, we mark the slot as used, update the highest_used_slotid,
180 * and respectively set up the sequence operation args.
181 *
182 * Note: must be called with under the slot_tbl_lock.
183 */
184struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
185{
186	struct nfs4_slot *ret = ERR_PTR(-EBUSY);
187	u32 slotid;
188
189	dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
190		__func__, tbl->used_slots[0], tbl->highest_used_slotid,
191		tbl->max_slotid + 1);
192	slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
193	if (slotid <= tbl->max_slotid) {
194		ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
195		if (!IS_ERR(ret))
196			nfs4_lock_slot(tbl, ret);
197	}
198	dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
199		__func__, tbl->used_slots[0], tbl->highest_used_slotid,
200		!IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
201	return ret;
202}
203
204static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
205		 u32 max_reqs, u32 ivalue)
206{
207	if (max_reqs <= tbl->max_slots)
208		return 0;
209	if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
210		return 0;
211	return -ENOMEM;
212}
213
214static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
215		u32 server_highest_slotid,
216		u32 ivalue)
217{
218	struct nfs4_slot **p;
219
220	nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
221	p = &tbl->slots;
222	while (*p) {
223		(*p)->seq_nr = ivalue;
224		(*p)->interrupted = 0;
225		p = &(*p)->next;
226	}
227	tbl->highest_used_slotid = NFS4_NO_SLOT;
228	tbl->target_highest_slotid = server_highest_slotid;
229	tbl->server_highest_slotid = server_highest_slotid;
230	tbl->d_target_highest_slotid = 0;
231	tbl->d2_target_highest_slotid = 0;
232	tbl->max_slotid = server_highest_slotid;
233}
234
235/*
236 * (re)Initialise a slot table
237 */
238static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
239		u32 max_reqs, u32 ivalue)
240{
241	int ret;
242
243	dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
244		max_reqs, tbl->max_slots);
245
246	if (max_reqs > NFS4_MAX_SLOT_TABLE)
247		max_reqs = NFS4_MAX_SLOT_TABLE;
248
249	ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
250	if (ret)
251		goto out;
252
253	spin_lock(&tbl->slot_tbl_lock);
254	nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
255	spin_unlock(&tbl->slot_tbl_lock);
256
257	dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
258		tbl, tbl->slots, tbl->max_slots);
259out:
260	dprintk("<-- %s: return %d\n", __func__, ret);
261	return ret;
262}
263
264/*
265 * nfs4_release_slot_table - release all slot table entries
266 */
267static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
268{
269	nfs4_shrink_slot_table(tbl, 0);
270}
271
272/**
273 * nfs4_shutdown_slot_table - release resources attached to a slot table
274 * @tbl: slot table to shut down
275 *
276 */
277void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
278{
279	nfs4_release_slot_table(tbl);
280	rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
281}
282
283/**
284 * nfs4_setup_slot_table - prepare a stand-alone slot table for use
285 * @tbl: slot table to set up
286 * @max_reqs: maximum number of requests allowed
287 * @queue: name to give RPC wait queue
288 *
289 * Returns zero on success, or a negative errno.
290 */
291int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
292		const char *queue)
293{
294	nfs4_init_slot_table(tbl, queue);
295	return nfs4_realloc_slot_table(tbl, max_reqs, 0);
296}
297
298static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
299{
300	struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
301	struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
302	struct nfs4_slot *slot = pslot;
303	struct nfs4_slot_table *tbl = slot->table;
304
305	if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
306		return false;
307	slot->generation = tbl->generation;
308	args->sa_slot = slot;
309	res->sr_timestamp = jiffies;
310	res->sr_slot = slot;
311	res->sr_status_flags = 0;
312	res->sr_status = 1;
313	return true;
314}
315
316static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
317		struct nfs4_slot *slot)
318{
319	if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
320		return true;
321	return false;
322}
323
324bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
325		struct nfs4_slot *slot)
326{
327	if (slot->slot_nr > tbl->max_slotid)
328		return false;
329	return __nfs41_wake_and_assign_slot(tbl, slot);
330}
331
332static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
333{
334	struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
335	if (!IS_ERR(slot)) {
336		bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
337		if (ret)
338			return ret;
339		nfs4_free_slot(tbl, slot);
340	}
341	return false;
342}
343
344void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
345{
346	for (;;) {
347		if (!nfs41_try_wake_next_slot_table_entry(tbl))
348			break;
349	}
350}
351
352#if defined(CONFIG_NFS_V4_1)
353
354static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
355		u32 target_highest_slotid)
356{
357	u32 max_slotid;
358
359	max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
360	if (max_slotid > tbl->server_highest_slotid)
361		max_slotid = tbl->server_highest_slotid;
362	if (max_slotid > tbl->target_highest_slotid)
363		max_slotid = tbl->target_highest_slotid;
364	tbl->max_slotid = max_slotid;
365	nfs41_wake_slot_table(tbl);
366}
367
368/* Update the client's idea of target_highest_slotid */
369static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
370		u32 target_highest_slotid)
371{
372	if (tbl->target_highest_slotid == target_highest_slotid)
373		return;
374	tbl->target_highest_slotid = target_highest_slotid;
375	tbl->generation++;
376}
377
378void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
379		u32 target_highest_slotid)
380{
381	spin_lock(&tbl->slot_tbl_lock);
382	nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
383	tbl->d_target_highest_slotid = 0;
384	tbl->d2_target_highest_slotid = 0;
385	nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
386	spin_unlock(&tbl->slot_tbl_lock);
387}
388
389static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
390		u32 highest_slotid)
391{
392	if (tbl->server_highest_slotid == highest_slotid)
393		return;
394	if (tbl->highest_used_slotid > highest_slotid)
395		return;
396	/* Deallocate slots */
397	nfs4_shrink_slot_table(tbl, highest_slotid + 1);
398	tbl->server_highest_slotid = highest_slotid;
399}
400
401static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
402{
403	s1 -= s2;
404	if (s1 == 0)
405		return 0;
406	if (s1 < 0)
407		return (s1 - 1) >> 1;
408	return (s1 + 1) >> 1;
409}
410
411static int nfs41_sign_s32(s32 s1)
412{
413	if (s1 > 0)
414		return 1;
415	if (s1 < 0)
416		return -1;
417	return 0;
418}
419
420static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
421{
422	if (!s1 || !s2)
423		return true;
424	return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
425}
426
427/* Try to eliminate outliers by checking for sharp changes in the
428 * derivatives and second derivatives
429 */
430static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
431		u32 new_target)
432{
433	s32 d_target, d2_target;
434	bool ret = true;
435
436	d_target = nfs41_derivative_target_slotid(new_target,
437			tbl->target_highest_slotid);
438	d2_target = nfs41_derivative_target_slotid(d_target,
439			tbl->d_target_highest_slotid);
440	/* Is first derivative same sign? */
441	if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
442		ret = false;
443	/* Is second derivative same sign? */
444	if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
445		ret = false;
446	tbl->d_target_highest_slotid = d_target;
447	tbl->d2_target_highest_slotid = d2_target;
448	return ret;
449}
450
451void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
452		struct nfs4_slot *slot,
453		struct nfs4_sequence_res *res)
454{
455	spin_lock(&tbl->slot_tbl_lock);
456	if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
457		nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
458	if (tbl->generation == slot->generation)
459		nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
460	nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
461	spin_unlock(&tbl->slot_tbl_lock);
462}
463
464static void nfs4_release_session_slot_tables(struct nfs4_session *session)
465{
466	nfs4_release_slot_table(&session->fc_slot_table);
467	nfs4_release_slot_table(&session->bc_slot_table);
468}
469
470/*
471 * Initialize or reset the forechannel and backchannel tables
472 */
473int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
474{
475	struct nfs4_slot_table *tbl;
476	int status;
477
478	dprintk("--> %s\n", __func__);
479	/* Fore channel */
480	tbl = &ses->fc_slot_table;
481	tbl->session = ses;
482	status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
483	if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */
484		return status;
485	/* Back channel */
486	tbl = &ses->bc_slot_table;
487	tbl->session = ses;
488	status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
489	if (status && tbl->slots == NULL)
490		/* Fore and back channel share a connection so get
491		 * both slot tables or neither */
492		nfs4_release_session_slot_tables(ses);
493	return status;
494}
495
496struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
497{
498	struct nfs4_session *session;
499
500	session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
501	if (!session)
502		return NULL;
503
504	nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
505	nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
506	session->session_state = 1<<NFS4_SESSION_INITING;
507
508	session->clp = clp;
509	return session;
510}
511
512static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
513{
514	nfs4_shutdown_slot_table(&session->fc_slot_table);
515	nfs4_shutdown_slot_table(&session->bc_slot_table);
516}
517
518void nfs4_destroy_session(struct nfs4_session *session)
519{
520	struct rpc_xprt *xprt;
521	struct rpc_cred *cred;
522
523	cred = nfs4_get_clid_cred(session->clp);
524	nfs4_proc_destroy_session(session, cred);
525	if (cred)
526		put_rpccred(cred);
527
528	rcu_read_lock();
529	xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
530	rcu_read_unlock();
531	dprintk("%s Destroy backchannel for xprt %p\n",
532		__func__, xprt);
533	xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
534	nfs4_destroy_session_slot_tables(session);
535	kfree(session);
536}
537
538/*
539 * With sessions, the client is not marked ready until after a
540 * successful EXCHANGE_ID and CREATE_SESSION.
541 *
542 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
543 * other versions of NFS can be tried.
544 */
545static int nfs41_check_session_ready(struct nfs_client *clp)
546{
547	int ret;
548	
549	if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
550		ret = nfs4_client_recover_expired_lease(clp);
551		if (ret)
552			return ret;
553	}
554	if (clp->cl_cons_state < NFS_CS_READY)
555		return -EPROTONOSUPPORT;
556	smp_rmb();
557	return 0;
558}
559
560int nfs4_init_session(struct nfs_client *clp)
561{
562	if (!nfs4_has_session(clp))
563		return 0;
564
565	clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
566	return nfs41_check_session_ready(clp);
567}
568
569int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
570{
571	struct nfs4_session *session = clp->cl_session;
572	int ret;
573
574	spin_lock(&clp->cl_lock);
575	if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
576		/*
577		 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
578		 * DS lease to be equal to the MDS lease.
579		 */
580		clp->cl_lease_time = lease_time;
581		clp->cl_last_renewal = jiffies;
582	}
583	spin_unlock(&clp->cl_lock);
584
585	ret = nfs41_check_session_ready(clp);
586	if (ret)
587		return ret;
588	/* Test for the DS role */
589	if (!is_ds_client(clp))
590		return -ENODEV;
591	return 0;
592}
593EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
594
595#endif	/* defined(CONFIG_NFS_V4_1) */