4 * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com>
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_completion(&tbl
->complete
);
35 * nfs4_shrink_slot_table - free retired slots from the slot table
37 static void nfs4_shrink_slot_table(struct nfs4_slot_table
*tbl
, u32 newsize
)
40 if (newsize
>= tbl
->max_slots
)
47 struct nfs4_slot
*slot
= *p
;
56 * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
57 * @tbl - controlling slot table
60 void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table
*tbl
)
62 if (nfs4_slot_tbl_draining(tbl
))
63 complete(&tbl
->complete
);
67 * nfs4_free_slot - free a slot and efficiently update slot table.
69 * freeing a slot is trivially done by clearing its respective bit
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.
79 * Must be called while holding tbl->slot_tbl_lock
81 void nfs4_free_slot(struct nfs4_slot_table
*tbl
, struct nfs4_slot
*slot
)
83 u32 slotid
= slot
->slot_nr
;
85 /* clear used bit in bitmap */
86 __clear_bit(slotid
, tbl
->used_slots
);
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
);
92 tbl
->highest_used_slotid
= new_max
;
94 tbl
->highest_used_slotid
= NFS4_NO_SLOT
;
95 nfs4_slot_tbl_drain_complete(tbl
);
98 dprintk("%s: slotid %u highest_used_slotid %u\n", __func__
,
99 slotid
, tbl
->highest_used_slotid
);
102 static struct nfs4_slot
*nfs4_new_slot(struct nfs4_slot_table
*tbl
,
103 u32 slotid
, u32 seq_init
, gfp_t gfp_mask
)
105 struct nfs4_slot
*slot
;
107 slot
= kzalloc(sizeof(*slot
), gfp_mask
);
110 slot
->slot_nr
= slotid
;
111 slot
->seq_nr
= seq_init
;
116 static struct nfs4_slot
*nfs4_find_or_create_slot(struct nfs4_slot_table
*tbl
,
117 u32 slotid
, u32 seq_init
, gfp_t gfp_mask
)
119 struct nfs4_slot
**p
, *slot
;
124 *p
= nfs4_new_slot(tbl
, tbl
->max_slots
,
131 if (slot
->slot_nr
== slotid
)
135 return ERR_PTR(-ENOMEM
);
139 * nfs4_alloc_slot - efficiently look for a free slot
141 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
142 * If found, we mark the slot as used, update the highest_used_slotid,
143 * and respectively set up the sequence operation args.
145 * Note: must be called with under the slot_tbl_lock.
147 struct nfs4_slot
*nfs4_alloc_slot(struct nfs4_slot_table
*tbl
)
149 struct nfs4_slot
*ret
= ERR_PTR(-EBUSY
);
152 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
153 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
154 tbl
->max_slotid
+ 1);
155 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slotid
+ 1);
156 if (slotid
> tbl
->max_slotid
)
158 ret
= nfs4_find_or_create_slot(tbl
, slotid
, 1, GFP_NOWAIT
);
161 __set_bit(slotid
, tbl
->used_slots
);
162 if (slotid
> tbl
->highest_used_slotid
||
163 tbl
->highest_used_slotid
== NFS4_NO_SLOT
)
164 tbl
->highest_used_slotid
= slotid
;
165 ret
->generation
= tbl
->generation
;
168 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
169 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
170 !IS_ERR(ret
) ? ret
->slot_nr
: NFS4_NO_SLOT
);
174 static int nfs4_grow_slot_table(struct nfs4_slot_table
*tbl
,
175 u32 max_reqs
, u32 ivalue
)
177 if (max_reqs
<= tbl
->max_slots
)
179 if (!IS_ERR(nfs4_find_or_create_slot(tbl
, max_reqs
- 1, ivalue
, GFP_NOFS
)))
184 static void nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
,
185 u32 server_highest_slotid
,
188 struct nfs4_slot
**p
;
190 nfs4_shrink_slot_table(tbl
, server_highest_slotid
+ 1);
193 (*p
)->seq_nr
= ivalue
;
194 (*p
)->interrupted
= 0;
197 tbl
->highest_used_slotid
= NFS4_NO_SLOT
;
198 tbl
->target_highest_slotid
= server_highest_slotid
;
199 tbl
->server_highest_slotid
= server_highest_slotid
;
200 tbl
->d_target_highest_slotid
= 0;
201 tbl
->d2_target_highest_slotid
= 0;
202 tbl
->max_slotid
= server_highest_slotid
;
206 * (re)Initialise a slot table
208 static int nfs4_realloc_slot_table(struct nfs4_slot_table
*tbl
,
209 u32 max_reqs
, u32 ivalue
)
213 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__
,
214 max_reqs
, tbl
->max_slots
);
216 if (max_reqs
> NFS4_MAX_SLOT_TABLE
)
217 max_reqs
= NFS4_MAX_SLOT_TABLE
;
219 ret
= nfs4_grow_slot_table(tbl
, max_reqs
, ivalue
);
223 spin_lock(&tbl
->slot_tbl_lock
);
224 nfs4_reset_slot_table(tbl
, max_reqs
- 1, ivalue
);
225 spin_unlock(&tbl
->slot_tbl_lock
);
227 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__
,
228 tbl
, tbl
->slots
, tbl
->max_slots
);
230 dprintk("<-- %s: return %d\n", __func__
, ret
);
235 * nfs4_release_slot_table - release resources attached to a slot table
236 * @tbl: slot table to shut down
239 void nfs4_release_slot_table(struct nfs4_slot_table
*tbl
)
241 nfs4_shrink_slot_table(tbl
, 0);
245 * nfs4_setup_slot_table - prepare a stand-alone slot table for use
246 * @tbl: slot table to set up
247 * @max_reqs: maximum number of requests allowed
248 * @queue: name to give RPC wait queue
250 * Returns zero on success, or a negative errno.
252 int nfs4_setup_slot_table(struct nfs4_slot_table
*tbl
, unsigned int max_reqs
,
255 nfs4_init_slot_table(tbl
, queue
);
256 return nfs4_realloc_slot_table(tbl
, max_reqs
, 0);
259 static bool nfs41_assign_slot(struct rpc_task
*task
, void *pslot
)
261 struct nfs4_sequence_args
*args
= task
->tk_msg
.rpc_argp
;
262 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
263 struct nfs4_slot
*slot
= pslot
;
264 struct nfs4_slot_table
*tbl
= slot
->table
;
266 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
268 slot
->generation
= tbl
->generation
;
269 args
->sa_slot
= slot
;
270 res
->sr_timestamp
= jiffies
;
272 res
->sr_status_flags
= 0;
277 static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table
*tbl
,
278 struct nfs4_slot
*slot
)
280 if (rpc_wake_up_first(&tbl
->slot_tbl_waitq
, nfs41_assign_slot
, slot
))
285 bool nfs41_wake_and_assign_slot(struct nfs4_slot_table
*tbl
,
286 struct nfs4_slot
*slot
)
288 if (slot
->slot_nr
> tbl
->max_slotid
)
290 return __nfs41_wake_and_assign_slot(tbl
, slot
);
293 static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table
*tbl
)
295 struct nfs4_slot
*slot
= nfs4_alloc_slot(tbl
);
297 bool ret
= __nfs41_wake_and_assign_slot(tbl
, slot
);
300 nfs4_free_slot(tbl
, slot
);
305 void nfs41_wake_slot_table(struct nfs4_slot_table
*tbl
)
308 if (!nfs41_try_wake_next_slot_table_entry(tbl
))
313 #if defined(CONFIG_NFS_V4_1)
315 static void nfs41_set_max_slotid_locked(struct nfs4_slot_table
*tbl
,
316 u32 target_highest_slotid
)
320 max_slotid
= min(NFS4_MAX_SLOT_TABLE
- 1, target_highest_slotid
);
321 if (max_slotid
> tbl
->server_highest_slotid
)
322 max_slotid
= tbl
->server_highest_slotid
;
323 if (max_slotid
> tbl
->target_highest_slotid
)
324 max_slotid
= tbl
->target_highest_slotid
;
325 tbl
->max_slotid
= max_slotid
;
326 nfs41_wake_slot_table(tbl
);
329 /* Update the client's idea of target_highest_slotid */
330 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table
*tbl
,
331 u32 target_highest_slotid
)
333 if (tbl
->target_highest_slotid
== target_highest_slotid
)
335 tbl
->target_highest_slotid
= target_highest_slotid
;
339 void nfs41_set_target_slotid(struct nfs4_slot_table
*tbl
,
340 u32 target_highest_slotid
)
342 spin_lock(&tbl
->slot_tbl_lock
);
343 nfs41_set_target_slotid_locked(tbl
, target_highest_slotid
);
344 tbl
->d_target_highest_slotid
= 0;
345 tbl
->d2_target_highest_slotid
= 0;
346 nfs41_set_max_slotid_locked(tbl
, target_highest_slotid
);
347 spin_unlock(&tbl
->slot_tbl_lock
);
350 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table
*tbl
,
353 if (tbl
->server_highest_slotid
== highest_slotid
)
355 if (tbl
->highest_used_slotid
> highest_slotid
)
357 /* Deallocate slots */
358 nfs4_shrink_slot_table(tbl
, highest_slotid
+ 1);
359 tbl
->server_highest_slotid
= highest_slotid
;
362 static s32
nfs41_derivative_target_slotid(s32 s1
, s32 s2
)
368 return (s1
- 1) >> 1;
369 return (s1
+ 1) >> 1;
372 static int nfs41_sign_s32(s32 s1
)
381 static bool nfs41_same_sign_or_zero_s32(s32 s1
, s32 s2
)
385 return nfs41_sign_s32(s1
) == nfs41_sign_s32(s2
);
388 /* Try to eliminate outliers by checking for sharp changes in the
389 * derivatives and second derivatives
391 static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table
*tbl
,
394 s32 d_target
, d2_target
;
397 d_target
= nfs41_derivative_target_slotid(new_target
,
398 tbl
->target_highest_slotid
);
399 d2_target
= nfs41_derivative_target_slotid(d_target
,
400 tbl
->d_target_highest_slotid
);
401 /* Is first derivative same sign? */
402 if (nfs41_same_sign_or_zero_s32(d_target
, tbl
->d_target_highest_slotid
))
404 /* Is second derivative same sign? */
405 if (nfs41_same_sign_or_zero_s32(d2_target
, tbl
->d2_target_highest_slotid
))
407 tbl
->d_target_highest_slotid
= d_target
;
408 tbl
->d2_target_highest_slotid
= d2_target
;
412 void nfs41_update_target_slotid(struct nfs4_slot_table
*tbl
,
413 struct nfs4_slot
*slot
,
414 struct nfs4_sequence_res
*res
)
416 spin_lock(&tbl
->slot_tbl_lock
);
417 if (!nfs41_is_outlier_target_slotid(tbl
, res
->sr_target_highest_slotid
))
418 nfs41_set_target_slotid_locked(tbl
, res
->sr_target_highest_slotid
);
419 if (tbl
->generation
== slot
->generation
)
420 nfs41_set_server_slotid_locked(tbl
, res
->sr_highest_slotid
);
421 nfs41_set_max_slotid_locked(tbl
, res
->sr_target_highest_slotid
);
422 spin_unlock(&tbl
->slot_tbl_lock
);
425 static void nfs4_destroy_session_slot_tables(struct nfs4_session
*session
)
427 nfs4_release_slot_table(&session
->fc_slot_table
);
428 nfs4_release_slot_table(&session
->bc_slot_table
);
432 * Initialize or reset the forechannel and backchannel tables
434 int nfs4_setup_session_slot_tables(struct nfs4_session
*ses
)
436 struct nfs4_slot_table
*tbl
;
439 dprintk("--> %s\n", __func__
);
441 tbl
= &ses
->fc_slot_table
;
443 status
= nfs4_realloc_slot_table(tbl
, ses
->fc_attrs
.max_reqs
, 1);
444 if (status
) /* -ENOMEM */
447 tbl
= &ses
->bc_slot_table
;
449 status
= nfs4_realloc_slot_table(tbl
, ses
->bc_attrs
.max_reqs
, 0);
450 if (status
&& tbl
->slots
== NULL
)
451 /* Fore and back channel share a connection so get
452 * both slot tables or neither */
453 nfs4_destroy_session_slot_tables(ses
);
457 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
459 struct nfs4_session
*session
;
461 session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
465 nfs4_init_slot_table(&session
->fc_slot_table
, "ForeChannel Slot table");
466 nfs4_init_slot_table(&session
->bc_slot_table
, "BackChannel Slot table");
467 session
->session_state
= 1<<NFS4_SESSION_INITING
;
473 void nfs4_destroy_session(struct nfs4_session
*session
)
475 struct rpc_xprt
*xprt
;
476 struct rpc_cred
*cred
;
478 cred
= nfs4_get_clid_cred(session
->clp
);
479 nfs4_proc_destroy_session(session
, cred
);
484 xprt
= rcu_dereference(session
->clp
->cl_rpcclient
->cl_xprt
);
486 dprintk("%s Destroy backchannel for xprt %p\n",
488 xprt_destroy_backchannel(xprt
, NFS41_BC_MIN_CALLBACKS
);
489 nfs4_destroy_session_slot_tables(session
);
494 * With sessions, the client is not marked ready until after a
495 * successful EXCHANGE_ID and CREATE_SESSION.
497 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
498 * other versions of NFS can be tried.
500 static int nfs41_check_session_ready(struct nfs_client
*clp
)
504 if (clp
->cl_cons_state
== NFS_CS_SESSION_INITING
) {
505 ret
= nfs4_client_recover_expired_lease(clp
);
509 if (clp
->cl_cons_state
< NFS_CS_READY
)
510 return -EPROTONOSUPPORT
;
515 int nfs4_init_session(struct nfs_client
*clp
)
517 if (!nfs4_has_session(clp
))
520 clear_bit(NFS4_SESSION_INITING
, &clp
->cl_session
->session_state
);
521 return nfs41_check_session_ready(clp
);
524 int nfs4_init_ds_session(struct nfs_client
*clp
, unsigned long lease_time
)
526 struct nfs4_session
*session
= clp
->cl_session
;
529 spin_lock(&clp
->cl_lock
);
530 if (test_and_clear_bit(NFS4_SESSION_INITING
, &session
->session_state
)) {
532 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
533 * DS lease to be equal to the MDS lease.
535 clp
->cl_lease_time
= lease_time
;
536 clp
->cl_last_renewal
= jiffies
;
538 spin_unlock(&clp
->cl_lock
);
540 ret
= nfs41_check_session_ready(clp
);
543 /* Test for the DS role */
544 if (!is_ds_client(clp
))
548 EXPORT_SYMBOL_GPL(nfs4_init_ds_session
);
550 #endif /* defined(CONFIG_NFS_V4_1) */