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>
21 #include "nfs4session.h"
24 #define NFSDBG_FACILITY NFSDBG_STATE
26 static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue)
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);
36 * nfs4_shrink_slot_table - free retired slots from the slot table
38 static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
41 if (newsize >= tbl->max_slots)
48 struct nfs4_slot *slot = *p;
57 * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
58 * @tbl: controlling slot table
61 void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
63 if (nfs4_slot_tbl_draining(tbl))
64 complete(&tbl->complete);
68 * nfs4_free_slot - free a slot and efficiently update slot table.
70 * freeing a slot is trivially done by clearing its respective bit
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.
80 * Must be called while holding tbl->slot_tbl_lock
82 void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
84 u32 slotid = slot->slot_nr;
86 /* clear used bit in bitmap */
87 __clear_bit(slotid, tbl->used_slots);
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);
93 tbl->highest_used_slotid = new_max;
95 tbl->highest_used_slotid = NFS4_NO_SLOT;
96 nfs4_slot_tbl_drain_complete(tbl);
99 dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
100 slotid, tbl->highest_used_slotid);
103 static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
104 u32 slotid, u32 seq_init, gfp_t gfp_mask)
106 struct nfs4_slot *slot;
108 slot = kzalloc(sizeof(*slot), gfp_mask);
111 slot->slot_nr = slotid;
112 slot->seq_nr = seq_init;
113 slot->seq_nr_highest_sent = seq_init;
114 slot->seq_nr_last_acked = seq_init - 1;
119 static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
120 u32 slotid, u32 seq_init, gfp_t gfp_mask)
122 struct nfs4_slot **p, *slot;
127 *p = nfs4_new_slot(tbl, tbl->max_slots,
134 if (slot->slot_nr == slotid)
138 return ERR_PTR(-ENOMEM);
141 static void nfs4_lock_slot(struct nfs4_slot_table *tbl,
142 struct nfs4_slot *slot)
144 u32 slotid = slot->slot_nr;
146 __set_bit(slotid, tbl->used_slots);
147 if (slotid > tbl->highest_used_slotid ||
148 tbl->highest_used_slotid == NFS4_NO_SLOT)
149 tbl->highest_used_slotid = slotid;
150 slot->generation = tbl->generation;
154 * nfs4_try_to_lock_slot - Given a slot try to allocate it
156 * Note: must be called with the slot_tbl_lock held.
158 bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
160 if (nfs4_test_locked_slot(tbl, slot->slot_nr))
162 nfs4_lock_slot(tbl, slot);
167 * nfs4_lookup_slot - Find a slot but don't allocate it
169 * Note: must be called with the slot_tbl_lock held.
171 struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid)
173 if (slotid <= tbl->max_slotid)
174 return nfs4_find_or_create_slot(tbl, slotid, 0, GFP_NOWAIT);
175 return ERR_PTR(-E2BIG);
178 static int nfs4_slot_get_seqid(struct nfs4_slot_table *tbl, u32 slotid,
180 __must_hold(&tbl->slot_tbl_lock)
182 struct nfs4_slot *slot;
185 slot = nfs4_lookup_slot(tbl, slotid);
186 ret = PTR_ERR_OR_ZERO(slot);
188 *seq_nr = slot->seq_nr;
194 * nfs4_slot_seqid_in_use - test if a slot sequence id is still in use
196 * Given a slot table, slot id and sequence number, determine if the
197 * RPC call in question is still in flight. This function is mainly
198 * intended for use by the callback channel.
200 static bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl,
201 u32 slotid, u32 seq_nr)
206 spin_lock(&tbl->slot_tbl_lock);
207 if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 &&
208 cur_seq == seq_nr && test_bit(slotid, tbl->used_slots))
210 spin_unlock(&tbl->slot_tbl_lock);
215 * nfs4_slot_wait_on_seqid - wait until a slot sequence id is complete
217 * Given a slot table, slot id and sequence number, wait until the
218 * corresponding RPC call completes. This function is mainly
219 * intended for use by the callback channel.
221 int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl,
222 u32 slotid, u32 seq_nr,
223 unsigned long timeout)
225 if (wait_event_timeout(tbl->slot_waitq,
226 !nfs4_slot_seqid_in_use(tbl, slotid, seq_nr),
233 * nfs4_alloc_slot - efficiently look for a free slot
235 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
236 * If found, we mark the slot as used, update the highest_used_slotid,
237 * and respectively set up the sequence operation args.
239 * Note: must be called with under the slot_tbl_lock.
241 struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
243 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
246 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
247 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
248 tbl->max_slotid + 1);
249 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
250 if (slotid <= tbl->max_slotid) {
251 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
253 nfs4_lock_slot(tbl, ret);
255 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
256 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
257 !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
261 static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
262 u32 max_reqs, u32 ivalue)
264 if (max_reqs <= tbl->max_slots)
266 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
271 static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
272 u32 server_highest_slotid,
275 struct nfs4_slot **p;
277 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
280 (*p)->seq_nr = ivalue;
281 (*p)->seq_nr_highest_sent = ivalue;
282 (*p)->seq_nr_last_acked = ivalue - 1;
285 tbl->highest_used_slotid = NFS4_NO_SLOT;
286 tbl->target_highest_slotid = server_highest_slotid;
287 tbl->server_highest_slotid = server_highest_slotid;
288 tbl->d_target_highest_slotid = 0;
289 tbl->d2_target_highest_slotid = 0;
290 tbl->max_slotid = server_highest_slotid;
294 * (re)Initialise a slot table
296 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
297 u32 max_reqs, u32 ivalue)
301 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
302 max_reqs, tbl->max_slots);
304 if (max_reqs > NFS4_MAX_SLOT_TABLE)
305 max_reqs = NFS4_MAX_SLOT_TABLE;
307 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
311 spin_lock(&tbl->slot_tbl_lock);
312 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
313 spin_unlock(&tbl->slot_tbl_lock);
315 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
316 tbl, tbl->slots, tbl->max_slots);
318 dprintk("<-- %s: return %d\n", __func__, ret);
323 * nfs4_release_slot_table - release all slot table entries
325 static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
327 nfs4_shrink_slot_table(tbl, 0);
331 * nfs4_shutdown_slot_table - release resources attached to a slot table
332 * @tbl: slot table to shut down
335 void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
337 nfs4_release_slot_table(tbl);
338 rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
342 * nfs4_setup_slot_table - prepare a stand-alone slot table for use
343 * @tbl: slot table to set up
344 * @max_reqs: maximum number of requests allowed
345 * @queue: name to give RPC wait queue
347 * Returns zero on success, or a negative errno.
349 int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
352 nfs4_init_slot_table(tbl, queue);
353 return nfs4_realloc_slot_table(tbl, max_reqs, 0);
356 static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
358 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
359 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
360 struct nfs4_slot *slot = pslot;
361 struct nfs4_slot_table *tbl = slot->table;
363 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
365 slot->generation = tbl->generation;
366 args->sa_slot = slot;
367 res->sr_timestamp = jiffies;
369 res->sr_status_flags = 0;
374 static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
375 struct nfs4_slot *slot)
377 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
382 bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
383 struct nfs4_slot *slot)
385 if (slot->slot_nr > tbl->max_slotid)
387 return __nfs41_wake_and_assign_slot(tbl, slot);
390 static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
392 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
394 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
397 nfs4_free_slot(tbl, slot);
402 void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
405 if (!nfs41_try_wake_next_slot_table_entry(tbl))
410 #if defined(CONFIG_NFS_V4_1)
412 static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
413 u32 target_highest_slotid)
417 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
418 if (max_slotid > tbl->server_highest_slotid)
419 max_slotid = tbl->server_highest_slotid;
420 if (max_slotid > tbl->target_highest_slotid)
421 max_slotid = tbl->target_highest_slotid;
422 tbl->max_slotid = max_slotid;
423 nfs41_wake_slot_table(tbl);
426 /* Update the client's idea of target_highest_slotid */
427 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
428 u32 target_highest_slotid)
430 if (tbl->target_highest_slotid == target_highest_slotid)
432 tbl->target_highest_slotid = target_highest_slotid;
436 void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
437 u32 target_highest_slotid)
439 spin_lock(&tbl->slot_tbl_lock);
440 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
441 tbl->d_target_highest_slotid = 0;
442 tbl->d2_target_highest_slotid = 0;
443 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
444 spin_unlock(&tbl->slot_tbl_lock);
447 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
450 if (tbl->server_highest_slotid == highest_slotid)
452 if (tbl->highest_used_slotid > highest_slotid)
454 /* Deallocate slots */
455 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
456 tbl->server_highest_slotid = highest_slotid;
459 static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
465 return (s1 - 1) >> 1;
466 return (s1 + 1) >> 1;
469 static int nfs41_sign_s32(s32 s1)
478 static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
482 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
485 /* Try to eliminate outliers by checking for sharp changes in the
486 * derivatives and second derivatives
488 static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
491 s32 d_target, d2_target;
494 d_target = nfs41_derivative_target_slotid(new_target,
495 tbl->target_highest_slotid);
496 d2_target = nfs41_derivative_target_slotid(d_target,
497 tbl->d_target_highest_slotid);
498 /* Is first derivative same sign? */
499 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
501 /* Is second derivative same sign? */
502 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
504 tbl->d_target_highest_slotid = d_target;
505 tbl->d2_target_highest_slotid = d2_target;
509 void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
510 struct nfs4_slot *slot,
511 struct nfs4_sequence_res *res)
513 spin_lock(&tbl->slot_tbl_lock);
514 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
515 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
516 if (tbl->generation == slot->generation)
517 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
518 nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
519 spin_unlock(&tbl->slot_tbl_lock);
522 static void nfs4_release_session_slot_tables(struct nfs4_session *session)
524 nfs4_release_slot_table(&session->fc_slot_table);
525 nfs4_release_slot_table(&session->bc_slot_table);
529 * Initialize or reset the forechannel and backchannel tables
531 int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
533 struct nfs4_slot_table *tbl;
536 dprintk("--> %s\n", __func__);
538 tbl = &ses->fc_slot_table;
540 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
541 if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */
544 tbl = &ses->bc_slot_table;
546 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
547 if (status && tbl->slots == NULL)
548 /* Fore and back channel share a connection so get
549 * both slot tables or neither */
550 nfs4_release_session_slot_tables(ses);
554 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
556 struct nfs4_session *session;
558 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
562 nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
563 nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
564 session->session_state = 1<<NFS4_SESSION_INITING;
570 static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
572 nfs4_shutdown_slot_table(&session->fc_slot_table);
573 nfs4_shutdown_slot_table(&session->bc_slot_table);
576 void nfs4_destroy_session(struct nfs4_session *session)
578 struct rpc_xprt *xprt;
579 const struct cred *cred;
581 cred = nfs4_get_clid_cred(session->clp);
582 nfs4_proc_destroy_session(session, cred);
586 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
588 dprintk("%s Destroy backchannel for xprt %p\n",
590 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
591 nfs4_destroy_session_slot_tables(session);
596 * With sessions, the client is not marked ready until after a
597 * successful EXCHANGE_ID and CREATE_SESSION.
599 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
600 * other versions of NFS can be tried.
602 static int nfs41_check_session_ready(struct nfs_client *clp)
606 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
607 ret = nfs4_client_recover_expired_lease(clp);
611 if (clp->cl_cons_state < NFS_CS_READY)
612 return -EPROTONOSUPPORT;
617 int nfs4_init_session(struct nfs_client *clp)
619 if (!nfs4_has_session(clp))
622 clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
623 return nfs41_check_session_ready(clp);
626 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
628 struct nfs4_session *session = clp->cl_session;
631 spin_lock(&clp->cl_lock);
632 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
634 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
635 * DS lease to be equal to the MDS lease.
637 clp->cl_lease_time = lease_time;
638 clp->cl_last_renewal = jiffies;
640 spin_unlock(&clp->cl_lock);
642 ret = nfs41_check_session_ready(clp);
645 /* Test for the DS role */
646 if (!is_ds_client(clp))
650 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
652 #endif /* defined(CONFIG_NFS_V4_1) */