blob: 746675b74624daee5fe08585aff72879f478c834 [file] [log] [blame]
Trond Myklebust73e39aa2012-11-26 12:49:34 -05001/*
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
26/*
27 * nfs4_shrink_slot_table - free retired slots from the slot table
28 */
29static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
30{
31 struct nfs4_slot **p;
32 if (newsize >= tbl->max_slots)
33 return;
34
35 p = &tbl->slots;
36 while (newsize--)
37 p = &(*p)->next;
38 while (*p) {
39 struct nfs4_slot *slot = *p;
40
41 *p = slot->next;
42 kfree(slot);
43 tbl->max_slots--;
44 }
45}
46
Chuck Lever9d330592013-08-09 12:48:44 -040047/**
48 * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
49 * @tbl - controlling slot table
50 *
51 */
52void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
53{
54 if (nfs4_slot_tbl_draining(tbl))
55 complete(&tbl->complete);
56}
57
Trond Myklebust73e39aa2012-11-26 12:49:34 -050058/*
59 * nfs4_free_slot - free a slot and efficiently update slot table.
60 *
61 * freeing a slot is trivially done by clearing its respective bit
62 * in the bitmap.
63 * If the freed slotid equals highest_used_slotid we want to update it
64 * so that the server would be able to size down the slot table if needed,
65 * otherwise we know that the highest_used_slotid is still in use.
66 * When updating highest_used_slotid there may be "holes" in the bitmap
67 * so we need to scan down from highest_used_slotid to 0 looking for the now
68 * highest slotid in use.
69 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
70 *
71 * Must be called while holding tbl->slot_tbl_lock
72 */
73void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
74{
75 u32 slotid = slot->slot_nr;
76
77 /* clear used bit in bitmap */
78 __clear_bit(slotid, tbl->used_slots);
79
80 /* update highest_used_slotid when it is freed */
81 if (slotid == tbl->highest_used_slotid) {
82 u32 new_max = find_last_bit(tbl->used_slots, slotid);
83 if (new_max < slotid)
84 tbl->highest_used_slotid = new_max;
85 else {
86 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson774d5f12013-05-20 14:13:50 -040087 nfs4_slot_tbl_drain_complete(tbl);
Trond Myklebust73e39aa2012-11-26 12:49:34 -050088 }
89 }
Chuck Levere8d92382013-08-09 12:47:51 -040090 dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -050091 slotid, tbl->highest_used_slotid);
92}
93
94static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
95 u32 slotid, u32 seq_init, gfp_t gfp_mask)
96{
97 struct nfs4_slot *slot;
98
99 slot = kzalloc(sizeof(*slot), gfp_mask);
100 if (slot) {
101 slot->table = tbl;
102 slot->slot_nr = slotid;
103 slot->seq_nr = seq_init;
104 }
105 return slot;
106}
107
108static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
109 u32 slotid, u32 seq_init, gfp_t gfp_mask)
110{
111 struct nfs4_slot **p, *slot;
112
113 p = &tbl->slots;
114 for (;;) {
115 if (*p == NULL) {
116 *p = nfs4_new_slot(tbl, tbl->max_slots,
117 seq_init, gfp_mask);
118 if (*p == NULL)
119 break;
120 tbl->max_slots++;
121 }
122 slot = *p;
123 if (slot->slot_nr == slotid)
124 return slot;
125 p = &slot->next;
126 }
127 return ERR_PTR(-ENOMEM);
128}
129
130/*
131 * nfs4_alloc_slot - efficiently look for a free slot
132 *
133 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
134 * If found, we mark the slot as used, update the highest_used_slotid,
135 * and respectively set up the sequence operation args.
136 *
137 * Note: must be called with under the slot_tbl_lock.
138 */
139struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
140{
141 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
142 u32 slotid;
143
144 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
145 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
146 tbl->max_slotid + 1);
147 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
148 if (slotid > tbl->max_slotid)
149 goto out;
150 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
151 if (IS_ERR(ret))
152 goto out;
153 __set_bit(slotid, tbl->used_slots);
154 if (slotid > tbl->highest_used_slotid ||
155 tbl->highest_used_slotid == NFS4_NO_SLOT)
156 tbl->highest_used_slotid = slotid;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500157 ret->generation = tbl->generation;
158
159out:
Chuck Levere8d92382013-08-09 12:47:51 -0400160 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500161 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Chuck Levere8d92382013-08-09 12:47:51 -0400162 !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500163 return ret;
164}
165
166static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
167 u32 max_reqs, u32 ivalue)
168{
169 if (max_reqs <= tbl->max_slots)
170 return 0;
171 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
172 return 0;
173 return -ENOMEM;
174}
175
176static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
177 u32 server_highest_slotid,
178 u32 ivalue)
179{
180 struct nfs4_slot **p;
181
182 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
183 p = &tbl->slots;
184 while (*p) {
185 (*p)->seq_nr = ivalue;
Trond Myklebustac20d162012-12-15 15:36:07 -0500186 (*p)->interrupted = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500187 p = &(*p)->next;
188 }
189 tbl->highest_used_slotid = NFS4_NO_SLOT;
190 tbl->target_highest_slotid = server_highest_slotid;
191 tbl->server_highest_slotid = server_highest_slotid;
Trond Myklebust1fa80642012-12-02 13:54:59 -0500192 tbl->d_target_highest_slotid = 0;
193 tbl->d2_target_highest_slotid = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500194 tbl->max_slotid = server_highest_slotid;
195}
196
197/*
198 * (re)Initialise a slot table
199 */
200static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
201 u32 max_reqs, u32 ivalue)
202{
203 int ret;
204
Chuck Levere8d92382013-08-09 12:47:51 -0400205 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500206 max_reqs, tbl->max_slots);
207
208 if (max_reqs > NFS4_MAX_SLOT_TABLE)
209 max_reqs = NFS4_MAX_SLOT_TABLE;
210
211 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
212 if (ret)
213 goto out;
214
215 spin_lock(&tbl->slot_tbl_lock);
216 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
217 spin_unlock(&tbl->slot_tbl_lock);
218
Chuck Levere8d92382013-08-09 12:47:51 -0400219 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500220 tbl, tbl->slots, tbl->max_slots);
221out:
222 dprintk("<-- %s: return %d\n", __func__, ret);
223 return ret;
224}
225
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500226static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
227{
228 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
229 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
230 struct nfs4_slot *slot = pslot;
231 struct nfs4_slot_table *tbl = slot->table;
232
Andy Adamson774d5f12013-05-20 14:13:50 -0400233 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500234 return false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500235 slot->generation = tbl->generation;
236 args->sa_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500237 res->sr_timestamp = jiffies;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500238 res->sr_slot = slot;
239 res->sr_status_flags = 0;
240 res->sr_status = 1;
241 return true;
242}
243
244static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
245 struct nfs4_slot *slot)
246{
247 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
248 return true;
249 return false;
250}
251
252bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
253 struct nfs4_slot *slot)
254{
255 if (slot->slot_nr > tbl->max_slotid)
256 return false;
257 return __nfs41_wake_and_assign_slot(tbl, slot);
258}
259
260static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
261{
262 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
263 if (!IS_ERR(slot)) {
264 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
265 if (ret)
266 return ret;
267 nfs4_free_slot(tbl, slot);
268 }
269 return false;
270}
271
272void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
273{
274 for (;;) {
275 if (!nfs41_try_wake_next_slot_table_entry(tbl))
276 break;
277 }
278}
279
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500280static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
281 u32 target_highest_slotid)
282{
283 u32 max_slotid;
284
285 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
286 if (max_slotid > tbl->server_highest_slotid)
287 max_slotid = tbl->server_highest_slotid;
288 if (max_slotid > tbl->target_highest_slotid)
289 max_slotid = tbl->target_highest_slotid;
290 tbl->max_slotid = max_slotid;
291 nfs41_wake_slot_table(tbl);
292}
293
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500294/* Update the client's idea of target_highest_slotid */
295static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
296 u32 target_highest_slotid)
297{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500298 if (tbl->target_highest_slotid == target_highest_slotid)
299 return;
300 tbl->target_highest_slotid = target_highest_slotid;
301 tbl->generation++;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500302}
303
304void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
305 u32 target_highest_slotid)
306{
307 spin_lock(&tbl->slot_tbl_lock);
308 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500309 tbl->d_target_highest_slotid = 0;
310 tbl->d2_target_highest_slotid = 0;
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500311 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500312 spin_unlock(&tbl->slot_tbl_lock);
313}
314
315static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
316 u32 highest_slotid)
317{
318 if (tbl->server_highest_slotid == highest_slotid)
319 return;
320 if (tbl->highest_used_slotid > highest_slotid)
321 return;
322 /* Deallocate slots */
323 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
324 tbl->server_highest_slotid = highest_slotid;
325}
326
Trond Myklebust1fa80642012-12-02 13:54:59 -0500327static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
328{
329 s1 -= s2;
330 if (s1 == 0)
331 return 0;
332 if (s1 < 0)
333 return (s1 - 1) >> 1;
334 return (s1 + 1) >> 1;
335}
336
337static int nfs41_sign_s32(s32 s1)
338{
339 if (s1 > 0)
340 return 1;
341 if (s1 < 0)
342 return -1;
343 return 0;
344}
345
346static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
347{
348 if (!s1 || !s2)
349 return true;
350 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
351}
352
353/* Try to eliminate outliers by checking for sharp changes in the
354 * derivatives and second derivatives
355 */
356static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
357 u32 new_target)
358{
359 s32 d_target, d2_target;
360 bool ret = true;
361
362 d_target = nfs41_derivative_target_slotid(new_target,
363 tbl->target_highest_slotid);
364 d2_target = nfs41_derivative_target_slotid(d_target,
365 tbl->d_target_highest_slotid);
366 /* Is first derivative same sign? */
367 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
368 ret = false;
369 /* Is second derivative same sign? */
370 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
371 ret = false;
372 tbl->d_target_highest_slotid = d_target;
373 tbl->d2_target_highest_slotid = d2_target;
374 return ret;
375}
376
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500377void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
378 struct nfs4_slot *slot,
379 struct nfs4_sequence_res *res)
380{
381 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500382 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
383 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
384 if (tbl->generation == slot->generation)
385 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500386 nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500387 spin_unlock(&tbl->slot_tbl_lock);
388}
389
Chuck Lever9d330592013-08-09 12:48:44 -0400390#if defined(CONFIG_NFS_V4_1)
391
392/* Destroy the slot table */
393static void nfs4_destroy_slot_tables(struct nfs4_session *session)
394{
395 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
396 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
397}
398
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500399/*
400 * Initialize or reset the forechannel and backchannel tables
401 */
402int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
403{
404 struct nfs4_slot_table *tbl;
405 int status;
406
407 dprintk("--> %s\n", __func__);
408 /* Fore channel */
409 tbl = &ses->fc_slot_table;
410 tbl->session = ses;
411 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
412 if (status) /* -ENOMEM */
413 return status;
414 /* Back channel */
415 tbl = &ses->bc_slot_table;
416 tbl->session = ses;
417 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
418 if (status && tbl->slots == NULL)
419 /* Fore and back channel share a connection so get
420 * both slot tables or neither */
421 nfs4_destroy_slot_tables(ses);
422 return status;
423}
424
425struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
426{
427 struct nfs4_session *session;
428 struct nfs4_slot_table *tbl;
429
430 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
431 if (!session)
432 return NULL;
433
434 tbl = &session->fc_slot_table;
435 tbl->highest_used_slotid = NFS4_NO_SLOT;
436 spin_lock_init(&tbl->slot_tbl_lock);
437 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
438 init_completion(&tbl->complete);
439
440 tbl = &session->bc_slot_table;
441 tbl->highest_used_slotid = NFS4_NO_SLOT;
442 spin_lock_init(&tbl->slot_tbl_lock);
443 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
444 init_completion(&tbl->complete);
445
446 session->session_state = 1<<NFS4_SESSION_INITING;
447
448 session->clp = clp;
449 return session;
450}
451
452void nfs4_destroy_session(struct nfs4_session *session)
453{
454 struct rpc_xprt *xprt;
455 struct rpc_cred *cred;
456
Chuck Lever73d8bde2013-07-24 12:28:37 -0400457 cred = nfs4_get_clid_cred(session->clp);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500458 nfs4_proc_destroy_session(session, cred);
459 if (cred)
460 put_rpccred(cred);
461
462 rcu_read_lock();
463 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
464 rcu_read_unlock();
465 dprintk("%s Destroy backchannel for xprt %p\n",
466 __func__, xprt);
467 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
468 nfs4_destroy_slot_tables(session);
469 kfree(session);
470}
471
472/*
473 * With sessions, the client is not marked ready until after a
474 * successful EXCHANGE_ID and CREATE_SESSION.
475 *
476 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
477 * other versions of NFS can be tried.
478 */
479static int nfs41_check_session_ready(struct nfs_client *clp)
480{
481 int ret;
482
483 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
484 ret = nfs4_client_recover_expired_lease(clp);
485 if (ret)
486 return ret;
487 }
488 if (clp->cl_cons_state < NFS_CS_READY)
489 return -EPROTONOSUPPORT;
490 smp_rmb();
491 return 0;
492}
493
Andy Adamson18aad3d2013-06-26 12:21:49 -0400494int nfs4_init_session(struct nfs_client *clp)
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500495{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500496 if (!nfs4_has_session(clp))
497 return 0;
498
Andy Adamson18aad3d2013-06-26 12:21:49 -0400499 clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500500 return nfs41_check_session_ready(clp);
501}
502
503int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
504{
505 struct nfs4_session *session = clp->cl_session;
506 int ret;
507
508 spin_lock(&clp->cl_lock);
509 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
510 /*
511 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
512 * DS lease to be equal to the MDS lease.
513 */
514 clp->cl_lease_time = lease_time;
515 clp->cl_last_renewal = jiffies;
516 }
517 spin_unlock(&clp->cl_lock);
518
519 ret = nfs41_check_session_ready(clp);
520 if (ret)
521 return ret;
522 /* Test for the DS role */
523 if (!is_ds_client(clp))
524 return -ENODEV;
525 return 0;
526}
527EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
528
Chuck Lever9d330592013-08-09 12:48:44 -0400529#endif /* defined(CONFIG_NFS_V4_1) */