blob: be273c589b039930fa2541c83af26a15b0c94822 [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
Chuck Lever744aa522013-08-09 12:48:53 -040026static 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
Trond Myklebust73e39aa2012-11-26 12:49:34 -050034/*
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
Chuck Lever9d330592013-08-09 12:48:44 -040055/**
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
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066/*
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;
Andy Adamson774d5f12013-05-20 14:13:50 -040095 nfs4_slot_tbl_drain_complete(tbl);
Trond Myklebust73e39aa2012-11-26 12:49:34 -050096 }
97 }
Chuck Levere8d92382013-08-09 12:47:51 -040098 dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -050099 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
138/*
139 * nfs4_alloc_slot - efficiently look for a free slot
140 *
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.
144 *
145 * Note: must be called with under the slot_tbl_lock.
146 */
147struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
148{
149 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
150 u32 slotid;
151
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)
157 goto out;
158 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
159 if (IS_ERR(ret))
160 goto out;
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;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500165 ret->generation = tbl->generation;
166
167out:
Chuck Levere8d92382013-08-09 12:47:51 -0400168 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500169 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Chuck Levere8d92382013-08-09 12:47:51 -0400170 !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500171 return ret;
172}
173
174static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
175 u32 max_reqs, u32 ivalue)
176{
177 if (max_reqs <= tbl->max_slots)
178 return 0;
179 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
180 return 0;
181 return -ENOMEM;
182}
183
184static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
185 u32 server_highest_slotid,
186 u32 ivalue)
187{
188 struct nfs4_slot **p;
189
190 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
191 p = &tbl->slots;
192 while (*p) {
193 (*p)->seq_nr = ivalue;
Trond Myklebustac20d162012-12-15 15:36:07 -0500194 (*p)->interrupted = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500195 p = &(*p)->next;
196 }
197 tbl->highest_used_slotid = NFS4_NO_SLOT;
198 tbl->target_highest_slotid = server_highest_slotid;
199 tbl->server_highest_slotid = server_highest_slotid;
Trond Myklebust1fa80642012-12-02 13:54:59 -0500200 tbl->d_target_highest_slotid = 0;
201 tbl->d2_target_highest_slotid = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500202 tbl->max_slotid = server_highest_slotid;
203}
204
205/*
206 * (re)Initialise a slot table
207 */
208static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
209 u32 max_reqs, u32 ivalue)
210{
211 int ret;
212
Chuck Levere8d92382013-08-09 12:47:51 -0400213 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500214 max_reqs, tbl->max_slots);
215
216 if (max_reqs > NFS4_MAX_SLOT_TABLE)
217 max_reqs = NFS4_MAX_SLOT_TABLE;
218
219 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
220 if (ret)
221 goto out;
222
223 spin_lock(&tbl->slot_tbl_lock);
224 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
225 spin_unlock(&tbl->slot_tbl_lock);
226
Chuck Levere8d92382013-08-09 12:47:51 -0400227 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500228 tbl, tbl->slots, tbl->max_slots);
229out:
230 dprintk("<-- %s: return %d\n", __func__, ret);
231 return ret;
232}
233
Chuck Lever744aa522013-08-09 12:48:53 -0400234/**
235 * nfs4_setup_slot_table - prepare a stand-alone slot table for use
236 * @tbl: slot table to set up
237 * @max_reqs: maximum number of requests allowed
238 * @queue: name to give RPC wait queue
239 *
240 * Returns zero on success, or a negative errno.
241 */
242int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
243 const char *queue)
244{
245 nfs4_init_slot_table(tbl, queue);
246 return nfs4_realloc_slot_table(tbl, max_reqs, 0);
247}
248
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500249static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
250{
251 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
252 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
253 struct nfs4_slot *slot = pslot;
254 struct nfs4_slot_table *tbl = slot->table;
255
Andy Adamson774d5f12013-05-20 14:13:50 -0400256 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500257 return false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500258 slot->generation = tbl->generation;
259 args->sa_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500260 res->sr_timestamp = jiffies;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500261 res->sr_slot = slot;
262 res->sr_status_flags = 0;
263 res->sr_status = 1;
264 return true;
265}
266
267static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
268 struct nfs4_slot *slot)
269{
270 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
271 return true;
272 return false;
273}
274
275bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
276 struct nfs4_slot *slot)
277{
278 if (slot->slot_nr > tbl->max_slotid)
279 return false;
280 return __nfs41_wake_and_assign_slot(tbl, slot);
281}
282
283static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
284{
285 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
286 if (!IS_ERR(slot)) {
287 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
288 if (ret)
289 return ret;
290 nfs4_free_slot(tbl, slot);
291 }
292 return false;
293}
294
295void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
296{
297 for (;;) {
298 if (!nfs41_try_wake_next_slot_table_entry(tbl))
299 break;
300 }
301}
302
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500303static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
304 u32 target_highest_slotid)
305{
306 u32 max_slotid;
307
308 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
309 if (max_slotid > tbl->server_highest_slotid)
310 max_slotid = tbl->server_highest_slotid;
311 if (max_slotid > tbl->target_highest_slotid)
312 max_slotid = tbl->target_highest_slotid;
313 tbl->max_slotid = max_slotid;
314 nfs41_wake_slot_table(tbl);
315}
316
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500317/* Update the client's idea of target_highest_slotid */
318static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
319 u32 target_highest_slotid)
320{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500321 if (tbl->target_highest_slotid == target_highest_slotid)
322 return;
323 tbl->target_highest_slotid = target_highest_slotid;
324 tbl->generation++;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500325}
326
327void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
328 u32 target_highest_slotid)
329{
330 spin_lock(&tbl->slot_tbl_lock);
331 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500332 tbl->d_target_highest_slotid = 0;
333 tbl->d2_target_highest_slotid = 0;
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500334 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500335 spin_unlock(&tbl->slot_tbl_lock);
336}
337
338static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
339 u32 highest_slotid)
340{
341 if (tbl->server_highest_slotid == highest_slotid)
342 return;
343 if (tbl->highest_used_slotid > highest_slotid)
344 return;
345 /* Deallocate slots */
346 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
347 tbl->server_highest_slotid = highest_slotid;
348}
349
Trond Myklebust1fa80642012-12-02 13:54:59 -0500350static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
351{
352 s1 -= s2;
353 if (s1 == 0)
354 return 0;
355 if (s1 < 0)
356 return (s1 - 1) >> 1;
357 return (s1 + 1) >> 1;
358}
359
360static int nfs41_sign_s32(s32 s1)
361{
362 if (s1 > 0)
363 return 1;
364 if (s1 < 0)
365 return -1;
366 return 0;
367}
368
369static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
370{
371 if (!s1 || !s2)
372 return true;
373 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
374}
375
376/* Try to eliminate outliers by checking for sharp changes in the
377 * derivatives and second derivatives
378 */
379static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
380 u32 new_target)
381{
382 s32 d_target, d2_target;
383 bool ret = true;
384
385 d_target = nfs41_derivative_target_slotid(new_target,
386 tbl->target_highest_slotid);
387 d2_target = nfs41_derivative_target_slotid(d_target,
388 tbl->d_target_highest_slotid);
389 /* Is first derivative same sign? */
390 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
391 ret = false;
392 /* Is second derivative same sign? */
393 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
394 ret = false;
395 tbl->d_target_highest_slotid = d_target;
396 tbl->d2_target_highest_slotid = d2_target;
397 return ret;
398}
399
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500400void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
401 struct nfs4_slot *slot,
402 struct nfs4_sequence_res *res)
403{
404 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500405 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
406 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
407 if (tbl->generation == slot->generation)
408 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500409 nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500410 spin_unlock(&tbl->slot_tbl_lock);
411}
412
Chuck Lever9d330592013-08-09 12:48:44 -0400413#if defined(CONFIG_NFS_V4_1)
414
415/* Destroy the slot table */
416static void nfs4_destroy_slot_tables(struct nfs4_session *session)
417{
418 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
419 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
420}
421
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500422/*
423 * Initialize or reset the forechannel and backchannel tables
424 */
425int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
426{
427 struct nfs4_slot_table *tbl;
428 int status;
429
430 dprintk("--> %s\n", __func__);
431 /* Fore channel */
432 tbl = &ses->fc_slot_table;
433 tbl->session = ses;
434 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
435 if (status) /* -ENOMEM */
436 return status;
437 /* Back channel */
438 tbl = &ses->bc_slot_table;
439 tbl->session = ses;
440 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
441 if (status && tbl->slots == NULL)
442 /* Fore and back channel share a connection so get
443 * both slot tables or neither */
444 nfs4_destroy_slot_tables(ses);
445 return status;
446}
447
448struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
449{
450 struct nfs4_session *session;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500451
452 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
453 if (!session)
454 return NULL;
455
Chuck Lever744aa522013-08-09 12:48:53 -0400456 nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
457 nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500458 session->session_state = 1<<NFS4_SESSION_INITING;
459
460 session->clp = clp;
461 return session;
462}
463
464void nfs4_destroy_session(struct nfs4_session *session)
465{
466 struct rpc_xprt *xprt;
467 struct rpc_cred *cred;
468
Chuck Lever73d8bde2013-07-24 12:28:37 -0400469 cred = nfs4_get_clid_cred(session->clp);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500470 nfs4_proc_destroy_session(session, cred);
471 if (cred)
472 put_rpccred(cred);
473
474 rcu_read_lock();
475 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
476 rcu_read_unlock();
477 dprintk("%s Destroy backchannel for xprt %p\n",
478 __func__, xprt);
479 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
480 nfs4_destroy_slot_tables(session);
481 kfree(session);
482}
483
484/*
485 * With sessions, the client is not marked ready until after a
486 * successful EXCHANGE_ID and CREATE_SESSION.
487 *
488 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
489 * other versions of NFS can be tried.
490 */
491static int nfs41_check_session_ready(struct nfs_client *clp)
492{
493 int ret;
494
495 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
496 ret = nfs4_client_recover_expired_lease(clp);
497 if (ret)
498 return ret;
499 }
500 if (clp->cl_cons_state < NFS_CS_READY)
501 return -EPROTONOSUPPORT;
502 smp_rmb();
503 return 0;
504}
505
Andy Adamson18aad3d2013-06-26 12:21:49 -0400506int nfs4_init_session(struct nfs_client *clp)
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500507{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500508 if (!nfs4_has_session(clp))
509 return 0;
510
Andy Adamson18aad3d2013-06-26 12:21:49 -0400511 clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500512 return nfs41_check_session_ready(clp);
513}
514
515int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
516{
517 struct nfs4_session *session = clp->cl_session;
518 int ret;
519
520 spin_lock(&clp->cl_lock);
521 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
522 /*
523 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
524 * DS lease to be equal to the MDS lease.
525 */
526 clp->cl_lease_time = lease_time;
527 clp->cl_last_renewal = jiffies;
528 }
529 spin_unlock(&clp->cl_lock);
530
531 ret = nfs41_check_session_ready(clp);
532 if (ret)
533 return ret;
534 /* Test for the DS role */
535 if (!is_ds_client(clp))
536 return -ENODEV;
537 return 0;
538}
539EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
540
Chuck Lever9d330592013-08-09 12:48:44 -0400541#endif /* defined(CONFIG_NFS_V4_1) */