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
27 * nfs4_shrink_slot_table - free retired slots from the slot table
29 static void nfs4_shrink_slot_table(struct nfs4_slot_table
*tbl
, u32 newsize
)
32 if (newsize
>= tbl
->max_slots
)
39 struct nfs4_slot
*slot
= *p
;
48 * nfs4_free_slot - free a slot and efficiently update slot table.
50 * freeing a slot is trivially done by clearing its respective bit
52 * If the freed slotid equals highest_used_slotid we want to update it
53 * so that the server would be able to size down the slot table if needed,
54 * otherwise we know that the highest_used_slotid is still in use.
55 * When updating highest_used_slotid there may be "holes" in the bitmap
56 * so we need to scan down from highest_used_slotid to 0 looking for the now
57 * highest slotid in use.
58 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
60 * Must be called while holding tbl->slot_tbl_lock
62 void nfs4_free_slot(struct nfs4_slot_table
*tbl
, struct nfs4_slot
*slot
)
64 u32 slotid
= slot
->slot_nr
;
66 /* clear used bit in bitmap */
67 __clear_bit(slotid
, tbl
->used_slots
);
69 /* update highest_used_slotid when it is freed */
70 if (slotid
== tbl
->highest_used_slotid
) {
71 u32 new_max
= find_last_bit(tbl
->used_slots
, slotid
);
73 tbl
->highest_used_slotid
= new_max
;
75 tbl
->highest_used_slotid
= NFS4_NO_SLOT
;
76 nfs4_session_drain_complete(tbl
->session
, tbl
);
79 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__
,
80 slotid
, tbl
->highest_used_slotid
);
83 static struct nfs4_slot
*nfs4_new_slot(struct nfs4_slot_table
*tbl
,
84 u32 slotid
, u32 seq_init
, gfp_t gfp_mask
)
86 struct nfs4_slot
*slot
;
88 slot
= kzalloc(sizeof(*slot
), gfp_mask
);
91 slot
->slot_nr
= slotid
;
92 slot
->seq_nr
= seq_init
;
97 static struct nfs4_slot
*nfs4_find_or_create_slot(struct nfs4_slot_table
*tbl
,
98 u32 slotid
, u32 seq_init
, gfp_t gfp_mask
)
100 struct nfs4_slot
**p
, *slot
;
105 *p
= nfs4_new_slot(tbl
, tbl
->max_slots
,
112 if (slot
->slot_nr
== slotid
)
116 return ERR_PTR(-ENOMEM
);
120 * nfs4_alloc_slot - efficiently look for a free slot
122 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
123 * If found, we mark the slot as used, update the highest_used_slotid,
124 * and respectively set up the sequence operation args.
126 * Note: must be called with under the slot_tbl_lock.
128 struct nfs4_slot
*nfs4_alloc_slot(struct nfs4_slot_table
*tbl
)
130 struct nfs4_slot
*ret
= ERR_PTR(-EBUSY
);
133 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
134 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
135 tbl
->max_slotid
+ 1);
136 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slotid
+ 1);
137 if (slotid
> tbl
->max_slotid
)
139 ret
= nfs4_find_or_create_slot(tbl
, slotid
, 1, GFP_NOWAIT
);
142 __set_bit(slotid
, tbl
->used_slots
);
143 if (slotid
> tbl
->highest_used_slotid
||
144 tbl
->highest_used_slotid
== NFS4_NO_SLOT
)
145 tbl
->highest_used_slotid
= slotid
;
146 ret
->generation
= tbl
->generation
;
149 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
150 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
151 !IS_ERR(ret
) ? ret
->slot_nr
: -1);
155 static int nfs4_grow_slot_table(struct nfs4_slot_table
*tbl
,
156 u32 max_reqs
, u32 ivalue
)
158 if (max_reqs
<= tbl
->max_slots
)
160 if (!IS_ERR(nfs4_find_or_create_slot(tbl
, max_reqs
- 1, ivalue
, GFP_NOFS
)))
165 static void nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
,
166 u32 server_highest_slotid
,
169 struct nfs4_slot
**p
;
171 nfs4_shrink_slot_table(tbl
, server_highest_slotid
+ 1);
174 (*p
)->seq_nr
= ivalue
;
175 (*p
)->interrupted
= 0;
178 tbl
->highest_used_slotid
= NFS4_NO_SLOT
;
179 tbl
->target_highest_slotid
= server_highest_slotid
;
180 tbl
->server_highest_slotid
= server_highest_slotid
;
181 tbl
->d_target_highest_slotid
= 0;
182 tbl
->d2_target_highest_slotid
= 0;
183 tbl
->max_slotid
= server_highest_slotid
;
187 * (re)Initialise a slot table
189 static int nfs4_realloc_slot_table(struct nfs4_slot_table
*tbl
,
190 u32 max_reqs
, u32 ivalue
)
194 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__
,
195 max_reqs
, tbl
->max_slots
);
197 if (max_reqs
> NFS4_MAX_SLOT_TABLE
)
198 max_reqs
= NFS4_MAX_SLOT_TABLE
;
200 ret
= nfs4_grow_slot_table(tbl
, max_reqs
, ivalue
);
204 spin_lock(&tbl
->slot_tbl_lock
);
205 nfs4_reset_slot_table(tbl
, max_reqs
- 1, ivalue
);
206 spin_unlock(&tbl
->slot_tbl_lock
);
208 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
209 tbl
, tbl
->slots
, tbl
->max_slots
);
211 dprintk("<-- %s: return %d\n", __func__
, ret
);
215 /* Destroy the slot table */
216 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
218 nfs4_shrink_slot_table(&session
->fc_slot_table
, 0);
219 nfs4_shrink_slot_table(&session
->bc_slot_table
, 0);
222 static bool nfs41_assign_slot(struct rpc_task
*task
, void *pslot
)
224 struct nfs4_sequence_args
*args
= task
->tk_msg
.rpc_argp
;
225 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
226 struct nfs4_slot
*slot
= pslot
;
227 struct nfs4_slot_table
*tbl
= slot
->table
;
229 if (nfs4_session_draining(tbl
->session
) && !args
->sa_privileged
)
231 slot
->generation
= tbl
->generation
;
232 args
->sa_slot
= slot
;
233 res
->sr_timestamp
= jiffies
;
235 res
->sr_status_flags
= 0;
240 static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table
*tbl
,
241 struct nfs4_slot
*slot
)
243 if (rpc_wake_up_first(&tbl
->slot_tbl_waitq
, nfs41_assign_slot
, slot
))
248 bool nfs41_wake_and_assign_slot(struct nfs4_slot_table
*tbl
,
249 struct nfs4_slot
*slot
)
251 if (slot
->slot_nr
> tbl
->max_slotid
)
253 return __nfs41_wake_and_assign_slot(tbl
, slot
);
256 static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table
*tbl
)
258 struct nfs4_slot
*slot
= nfs4_alloc_slot(tbl
);
260 bool ret
= __nfs41_wake_and_assign_slot(tbl
, slot
);
263 nfs4_free_slot(tbl
, slot
);
268 void nfs41_wake_slot_table(struct nfs4_slot_table
*tbl
)
271 if (!nfs41_try_wake_next_slot_table_entry(tbl
))
276 static void nfs41_set_max_slotid_locked(struct nfs4_slot_table
*tbl
,
277 u32 target_highest_slotid
)
281 max_slotid
= min(NFS4_MAX_SLOT_TABLE
- 1, target_highest_slotid
);
282 if (max_slotid
> tbl
->server_highest_slotid
)
283 max_slotid
= tbl
->server_highest_slotid
;
284 if (max_slotid
> tbl
->target_highest_slotid
)
285 max_slotid
= tbl
->target_highest_slotid
;
286 tbl
->max_slotid
= max_slotid
;
287 nfs41_wake_slot_table(tbl
);
290 /* Update the client's idea of target_highest_slotid */
291 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table
*tbl
,
292 u32 target_highest_slotid
)
294 if (tbl
->target_highest_slotid
== target_highest_slotid
)
296 tbl
->target_highest_slotid
= target_highest_slotid
;
300 void nfs41_set_target_slotid(struct nfs4_slot_table
*tbl
,
301 u32 target_highest_slotid
)
303 spin_lock(&tbl
->slot_tbl_lock
);
304 nfs41_set_target_slotid_locked(tbl
, target_highest_slotid
);
305 tbl
->d_target_highest_slotid
= 0;
306 tbl
->d2_target_highest_slotid
= 0;
307 nfs41_set_max_slotid_locked(tbl
, target_highest_slotid
);
308 spin_unlock(&tbl
->slot_tbl_lock
);
311 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table
*tbl
,
314 if (tbl
->server_highest_slotid
== highest_slotid
)
316 if (tbl
->highest_used_slotid
> highest_slotid
)
318 /* Deallocate slots */
319 nfs4_shrink_slot_table(tbl
, highest_slotid
+ 1);
320 tbl
->server_highest_slotid
= highest_slotid
;
323 static s32
nfs41_derivative_target_slotid(s32 s1
, s32 s2
)
329 return (s1
- 1) >> 1;
330 return (s1
+ 1) >> 1;
333 static int nfs41_sign_s32(s32 s1
)
342 static bool nfs41_same_sign_or_zero_s32(s32 s1
, s32 s2
)
346 return nfs41_sign_s32(s1
) == nfs41_sign_s32(s2
);
349 /* Try to eliminate outliers by checking for sharp changes in the
350 * derivatives and second derivatives
352 static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table
*tbl
,
355 s32 d_target
, d2_target
;
358 d_target
= nfs41_derivative_target_slotid(new_target
,
359 tbl
->target_highest_slotid
);
360 d2_target
= nfs41_derivative_target_slotid(d_target
,
361 tbl
->d_target_highest_slotid
);
362 /* Is first derivative same sign? */
363 if (nfs41_same_sign_or_zero_s32(d_target
, tbl
->d_target_highest_slotid
))
365 /* Is second derivative same sign? */
366 if (nfs41_same_sign_or_zero_s32(d2_target
, tbl
->d2_target_highest_slotid
))
368 tbl
->d_target_highest_slotid
= d_target
;
369 tbl
->d2_target_highest_slotid
= d2_target
;
373 void nfs41_update_target_slotid(struct nfs4_slot_table
*tbl
,
374 struct nfs4_slot
*slot
,
375 struct nfs4_sequence_res
*res
)
377 spin_lock(&tbl
->slot_tbl_lock
);
378 if (!nfs41_is_outlier_target_slotid(tbl
, res
->sr_target_highest_slotid
))
379 nfs41_set_target_slotid_locked(tbl
, res
->sr_target_highest_slotid
);
380 if (tbl
->generation
== slot
->generation
)
381 nfs41_set_server_slotid_locked(tbl
, res
->sr_highest_slotid
);
382 nfs41_set_max_slotid_locked(tbl
, res
->sr_target_highest_slotid
);
383 spin_unlock(&tbl
->slot_tbl_lock
);
387 * Initialize or reset the forechannel and backchannel tables
389 int nfs4_setup_session_slot_tables(struct nfs4_session
*ses
)
391 struct nfs4_slot_table
*tbl
;
394 dprintk("--> %s\n", __func__
);
396 tbl
= &ses
->fc_slot_table
;
398 status
= nfs4_realloc_slot_table(tbl
, ses
->fc_attrs
.max_reqs
, 1);
399 if (status
) /* -ENOMEM */
402 tbl
= &ses
->bc_slot_table
;
404 status
= nfs4_realloc_slot_table(tbl
, ses
->bc_attrs
.max_reqs
, 0);
405 if (status
&& tbl
->slots
== NULL
)
406 /* Fore and back channel share a connection so get
407 * both slot tables or neither */
408 nfs4_destroy_slot_tables(ses
);
412 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
414 struct nfs4_session
*session
;
415 struct nfs4_slot_table
*tbl
;
417 session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
421 tbl
= &session
->fc_slot_table
;
422 tbl
->highest_used_slotid
= NFS4_NO_SLOT
;
423 spin_lock_init(&tbl
->slot_tbl_lock
);
424 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
425 init_completion(&tbl
->complete
);
427 tbl
= &session
->bc_slot_table
;
428 tbl
->highest_used_slotid
= NFS4_NO_SLOT
;
429 spin_lock_init(&tbl
->slot_tbl_lock
);
430 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
431 init_completion(&tbl
->complete
);
433 session
->session_state
= 1<<NFS4_SESSION_INITING
;
439 void nfs4_destroy_session(struct nfs4_session
*session
)
441 struct rpc_xprt
*xprt
;
442 struct rpc_cred
*cred
;
444 cred
= nfs4_get_exchange_id_cred(session
->clp
);
445 nfs4_proc_destroy_session(session
, cred
);
450 xprt
= rcu_dereference(session
->clp
->cl_rpcclient
->cl_xprt
);
452 dprintk("%s Destroy backchannel for xprt %p\n",
454 xprt_destroy_backchannel(xprt
, NFS41_BC_MIN_CALLBACKS
);
455 nfs4_destroy_slot_tables(session
);
460 * With sessions, the client is not marked ready until after a
461 * successful EXCHANGE_ID and CREATE_SESSION.
463 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
464 * other versions of NFS can be tried.
466 static int nfs41_check_session_ready(struct nfs_client
*clp
)
470 if (clp
->cl_cons_state
== NFS_CS_SESSION_INITING
) {
471 ret
= nfs4_client_recover_expired_lease(clp
);
475 if (clp
->cl_cons_state
< NFS_CS_READY
)
476 return -EPROTONOSUPPORT
;
481 int nfs4_init_session(struct nfs_server
*server
)
483 struct nfs_client
*clp
= server
->nfs_client
;
484 struct nfs4_session
*session
;
485 unsigned int target_max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
;
486 unsigned int target_max_resp_sz
= NFS_MAX_FILE_IO_SIZE
;
488 if (!nfs4_has_session(clp
))
491 if (server
->rsize
!= 0)
492 target_max_resp_sz
= server
->rsize
;
493 target_max_resp_sz
+= nfs41_maxread_overhead
;
495 if (server
->wsize
!= 0)
496 target_max_rqst_sz
= server
->wsize
;
497 target_max_rqst_sz
+= nfs41_maxwrite_overhead
;
499 session
= clp
->cl_session
;
500 spin_lock(&clp
->cl_lock
);
501 if (test_and_clear_bit(NFS4_SESSION_INITING
, &session
->session_state
)) {
502 /* Initialise targets and channel attributes */
503 session
->fc_target_max_rqst_sz
= target_max_rqst_sz
;
504 session
->fc_attrs
.max_rqst_sz
= target_max_rqst_sz
;
505 session
->fc_target_max_resp_sz
= target_max_resp_sz
;
506 session
->fc_attrs
.max_resp_sz
= target_max_resp_sz
;
508 /* Just adjust the targets */
509 if (target_max_rqst_sz
> session
->fc_target_max_rqst_sz
) {
510 session
->fc_target_max_rqst_sz
= target_max_rqst_sz
;
511 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
513 if (target_max_resp_sz
> session
->fc_target_max_resp_sz
) {
514 session
->fc_target_max_resp_sz
= target_max_resp_sz
;
515 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
518 spin_unlock(&clp
->cl_lock
);
520 if (test_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
))
521 nfs4_schedule_lease_recovery(clp
);
523 return nfs41_check_session_ready(clp
);
526 int nfs4_init_ds_session(struct nfs_client
*clp
, unsigned long lease_time
)
528 struct nfs4_session
*session
= clp
->cl_session
;
531 spin_lock(&clp
->cl_lock
);
532 if (test_and_clear_bit(NFS4_SESSION_INITING
, &session
->session_state
)) {
534 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
535 * DS lease to be equal to the MDS lease.
537 clp
->cl_lease_time
= lease_time
;
538 clp
->cl_last_renewal
= jiffies
;
540 spin_unlock(&clp
->cl_lock
);
542 ret
= nfs41_check_session_ready(clp
);
545 /* Test for the DS role */
546 if (!is_ds_client(clp
))
550 EXPORT_SYMBOL_GPL(nfs4_init_ds_session
);