blob: ed5aa9fa9c7b5d9d76d034329944e370185e3664 [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
47/*
48 * nfs4_free_slot - free a slot and efficiently update slot table.
49 *
50 * freeing a slot is trivially done by clearing its respective bit
51 * in the bitmap.
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.
59 *
60 * Must be called while holding tbl->slot_tbl_lock
61 */
62void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
63{
64 u32 slotid = slot->slot_nr;
65
66 /* clear used bit in bitmap */
67 __clear_bit(slotid, tbl->used_slots);
68
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);
72 if (new_max < slotid)
73 tbl->highest_used_slotid = new_max;
74 else {
75 tbl->highest_used_slotid = NFS4_NO_SLOT;
76 nfs4_session_drain_complete(tbl->session, tbl);
77 }
78 }
79 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
80 slotid, tbl->highest_used_slotid);
81}
82
83static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
84 u32 slotid, u32 seq_init, gfp_t gfp_mask)
85{
86 struct nfs4_slot *slot;
87
88 slot = kzalloc(sizeof(*slot), gfp_mask);
89 if (slot) {
90 slot->table = tbl;
91 slot->slot_nr = slotid;
92 slot->seq_nr = seq_init;
93 }
94 return slot;
95}
96
97static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
98 u32 slotid, u32 seq_init, gfp_t gfp_mask)
99{
100 struct nfs4_slot **p, *slot;
101
102 p = &tbl->slots;
103 for (;;) {
104 if (*p == NULL) {
105 *p = nfs4_new_slot(tbl, tbl->max_slots,
106 seq_init, gfp_mask);
107 if (*p == NULL)
108 break;
109 tbl->max_slots++;
110 }
111 slot = *p;
112 if (slot->slot_nr == slotid)
113 return slot;
114 p = &slot->next;
115 }
116 return ERR_PTR(-ENOMEM);
117}
118
119/*
120 * nfs4_alloc_slot - efficiently look for a free slot
121 *
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.
125 *
126 * Note: must be called with under the slot_tbl_lock.
127 */
128struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
129{
130 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
131 u32 slotid;
132
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)
138 goto out;
139 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
140 if (IS_ERR(ret))
141 goto out;
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->renewal_time = jiffies;
147 ret->generation = tbl->generation;
148
149out:
150 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
151 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
152 !IS_ERR(ret) ? ret->slot_nr : -1);
153 return ret;
154}
155
156static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
157 u32 max_reqs, u32 ivalue)
158{
159 if (max_reqs <= tbl->max_slots)
160 return 0;
161 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
162 return 0;
163 return -ENOMEM;
164}
165
166static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
167 u32 server_highest_slotid,
168 u32 ivalue)
169{
170 struct nfs4_slot **p;
171
172 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
173 p = &tbl->slots;
174 while (*p) {
175 (*p)->seq_nr = ivalue;
176 p = &(*p)->next;
177 }
178 tbl->highest_used_slotid = NFS4_NO_SLOT;
179 tbl->target_highest_slotid = server_highest_slotid;
180 tbl->server_highest_slotid = server_highest_slotid;
Trond Myklebust1fa80642012-12-02 13:54:59 -0500181 tbl->d_target_highest_slotid = 0;
182 tbl->d2_target_highest_slotid = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500183 tbl->max_slotid = server_highest_slotid;
184}
185
186/*
187 * (re)Initialise a slot table
188 */
189static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
190 u32 max_reqs, u32 ivalue)
191{
192 int ret;
193
194 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
195 max_reqs, tbl->max_slots);
196
197 if (max_reqs > NFS4_MAX_SLOT_TABLE)
198 max_reqs = NFS4_MAX_SLOT_TABLE;
199
200 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
201 if (ret)
202 goto out;
203
204 spin_lock(&tbl->slot_tbl_lock);
205 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
206 spin_unlock(&tbl->slot_tbl_lock);
207
208 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
209 tbl, tbl->slots, tbl->max_slots);
210out:
211 dprintk("<-- %s: return %d\n", __func__, ret);
212 return ret;
213}
214
215/* Destroy the slot table */
216static void nfs4_destroy_slot_tables(struct nfs4_session *session)
217{
218 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
219 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
220}
221
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500222static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
223{
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;
228
229 if (nfs4_session_draining(tbl->session) && !args->sa_privileged)
230 return false;
231 slot->renewal_time = jiffies;
232 slot->generation = tbl->generation;
233 args->sa_slot = slot;
234 res->sr_slot = slot;
235 res->sr_status_flags = 0;
236 res->sr_status = 1;
237 return true;
238}
239
240static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
241 struct nfs4_slot *slot)
242{
243 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
244 return true;
245 return false;
246}
247
248bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
249 struct nfs4_slot *slot)
250{
251 if (slot->slot_nr > tbl->max_slotid)
252 return false;
253 return __nfs41_wake_and_assign_slot(tbl, slot);
254}
255
256static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
257{
258 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
259 if (!IS_ERR(slot)) {
260 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
261 if (ret)
262 return ret;
263 nfs4_free_slot(tbl, slot);
264 }
265 return false;
266}
267
268void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
269{
270 for (;;) {
271 if (!nfs41_try_wake_next_slot_table_entry(tbl))
272 break;
273 }
274}
275
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500276/* Update the client's idea of target_highest_slotid */
277static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
278 u32 target_highest_slotid)
279{
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500280 unsigned int max_slotid;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500281
282 if (tbl->target_highest_slotid == target_highest_slotid)
283 return;
284 tbl->target_highest_slotid = target_highest_slotid;
285 tbl->generation++;
286
287 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500288 tbl->max_slotid = max_slotid;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500289 nfs41_wake_slot_table(tbl);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500290}
291
292void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
293 u32 target_highest_slotid)
294{
295 spin_lock(&tbl->slot_tbl_lock);
296 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500297 tbl->d_target_highest_slotid = 0;
298 tbl->d2_target_highest_slotid = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500299 spin_unlock(&tbl->slot_tbl_lock);
300}
301
302static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
303 u32 highest_slotid)
304{
305 if (tbl->server_highest_slotid == highest_slotid)
306 return;
307 if (tbl->highest_used_slotid > highest_slotid)
308 return;
309 /* Deallocate slots */
310 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
311 tbl->server_highest_slotid = highest_slotid;
312}
313
Trond Myklebust1fa80642012-12-02 13:54:59 -0500314static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
315{
316 s1 -= s2;
317 if (s1 == 0)
318 return 0;
319 if (s1 < 0)
320 return (s1 - 1) >> 1;
321 return (s1 + 1) >> 1;
322}
323
324static int nfs41_sign_s32(s32 s1)
325{
326 if (s1 > 0)
327 return 1;
328 if (s1 < 0)
329 return -1;
330 return 0;
331}
332
333static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
334{
335 if (!s1 || !s2)
336 return true;
337 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
338}
339
340/* Try to eliminate outliers by checking for sharp changes in the
341 * derivatives and second derivatives
342 */
343static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
344 u32 new_target)
345{
346 s32 d_target, d2_target;
347 bool ret = true;
348
349 d_target = nfs41_derivative_target_slotid(new_target,
350 tbl->target_highest_slotid);
351 d2_target = nfs41_derivative_target_slotid(d_target,
352 tbl->d_target_highest_slotid);
353 /* Is first derivative same sign? */
354 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
355 ret = false;
356 /* Is second derivative same sign? */
357 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
358 ret = false;
359 tbl->d_target_highest_slotid = d_target;
360 tbl->d2_target_highest_slotid = d2_target;
361 return ret;
362}
363
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500364void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
365 struct nfs4_slot *slot,
366 struct nfs4_sequence_res *res)
367{
368 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500369 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
370 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
371 if (tbl->generation == slot->generation)
372 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500373 spin_unlock(&tbl->slot_tbl_lock);
374}
375
376/*
377 * Initialize or reset the forechannel and backchannel tables
378 */
379int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
380{
381 struct nfs4_slot_table *tbl;
382 int status;
383
384 dprintk("--> %s\n", __func__);
385 /* Fore channel */
386 tbl = &ses->fc_slot_table;
387 tbl->session = ses;
388 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
389 if (status) /* -ENOMEM */
390 return status;
391 /* Back channel */
392 tbl = &ses->bc_slot_table;
393 tbl->session = ses;
394 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
395 if (status && tbl->slots == NULL)
396 /* Fore and back channel share a connection so get
397 * both slot tables or neither */
398 nfs4_destroy_slot_tables(ses);
399 return status;
400}
401
402struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
403{
404 struct nfs4_session *session;
405 struct nfs4_slot_table *tbl;
406
407 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
408 if (!session)
409 return NULL;
410
411 tbl = &session->fc_slot_table;
412 tbl->highest_used_slotid = NFS4_NO_SLOT;
413 spin_lock_init(&tbl->slot_tbl_lock);
414 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
415 init_completion(&tbl->complete);
416
417 tbl = &session->bc_slot_table;
418 tbl->highest_used_slotid = NFS4_NO_SLOT;
419 spin_lock_init(&tbl->slot_tbl_lock);
420 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
421 init_completion(&tbl->complete);
422
423 session->session_state = 1<<NFS4_SESSION_INITING;
424
425 session->clp = clp;
426 return session;
427}
428
429void nfs4_destroy_session(struct nfs4_session *session)
430{
431 struct rpc_xprt *xprt;
432 struct rpc_cred *cred;
433
434 cred = nfs4_get_exchange_id_cred(session->clp);
435 nfs4_proc_destroy_session(session, cred);
436 if (cred)
437 put_rpccred(cred);
438
439 rcu_read_lock();
440 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
441 rcu_read_unlock();
442 dprintk("%s Destroy backchannel for xprt %p\n",
443 __func__, xprt);
444 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
445 nfs4_destroy_slot_tables(session);
446 kfree(session);
447}
448
449/*
450 * With sessions, the client is not marked ready until after a
451 * successful EXCHANGE_ID and CREATE_SESSION.
452 *
453 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
454 * other versions of NFS can be tried.
455 */
456static int nfs41_check_session_ready(struct nfs_client *clp)
457{
458 int ret;
459
460 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
461 ret = nfs4_client_recover_expired_lease(clp);
462 if (ret)
463 return ret;
464 }
465 if (clp->cl_cons_state < NFS_CS_READY)
466 return -EPROTONOSUPPORT;
467 smp_rmb();
468 return 0;
469}
470
471int nfs4_init_session(struct nfs_server *server)
472{
473 struct nfs_client *clp = server->nfs_client;
474 struct nfs4_session *session;
475 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
476 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
477
478 if (!nfs4_has_session(clp))
479 return 0;
480
481 if (server->rsize != 0)
482 target_max_resp_sz = server->rsize;
483 target_max_resp_sz += nfs41_maxread_overhead;
484
485 if (server->wsize != 0)
486 target_max_rqst_sz = server->wsize;
487 target_max_rqst_sz += nfs41_maxwrite_overhead;
488
489 session = clp->cl_session;
490 spin_lock(&clp->cl_lock);
491 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
492 /* Initialise targets and channel attributes */
493 session->fc_target_max_rqst_sz = target_max_rqst_sz;
494 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
495 session->fc_target_max_resp_sz = target_max_resp_sz;
496 session->fc_attrs.max_resp_sz = target_max_resp_sz;
497 } else {
498 /* Just adjust the targets */
499 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
500 session->fc_target_max_rqst_sz = target_max_rqst_sz;
501 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
502 }
503 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
504 session->fc_target_max_resp_sz = target_max_resp_sz;
505 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
506 }
507 }
508 spin_unlock(&clp->cl_lock);
509
510 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
511 nfs4_schedule_lease_recovery(clp);
512
513 return nfs41_check_session_ready(clp);
514}
515
516int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
517{
518 struct nfs4_session *session = clp->cl_session;
519 int ret;
520
521 spin_lock(&clp->cl_lock);
522 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
523 /*
524 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
525 * DS lease to be equal to the MDS lease.
526 */
527 clp->cl_lease_time = lease_time;
528 clp->cl_last_renewal = jiffies;
529 }
530 spin_unlock(&clp->cl_lock);
531
532 ret = nfs41_check_session_ready(clp);
533 if (ret)
534 return ret;
535 /* Test for the DS role */
536 if (!is_ds_client(clp))
537 return -ENODEV;
538 return 0;
539}
540EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
541
542