blob: 0e1cc1f4e51aa826aefb8f56e0fccdfeb0da86b3 [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;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500146 ret->generation = tbl->generation;
147
148out:
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);
152 return ret;
153}
154
155static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
156 u32 max_reqs, u32 ivalue)
157{
158 if (max_reqs <= tbl->max_slots)
159 return 0;
160 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
161 return 0;
162 return -ENOMEM;
163}
164
165static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
166 u32 server_highest_slotid,
167 u32 ivalue)
168{
169 struct nfs4_slot **p;
170
171 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
172 p = &tbl->slots;
173 while (*p) {
174 (*p)->seq_nr = ivalue;
175 p = &(*p)->next;
176 }
177 tbl->highest_used_slotid = NFS4_NO_SLOT;
178 tbl->target_highest_slotid = server_highest_slotid;
179 tbl->server_highest_slotid = server_highest_slotid;
Trond Myklebust1fa80642012-12-02 13:54:59 -0500180 tbl->d_target_highest_slotid = 0;
181 tbl->d2_target_highest_slotid = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500182 tbl->max_slotid = server_highest_slotid;
183}
184
185/*
186 * (re)Initialise a slot table
187 */
188static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
189 u32 max_reqs, u32 ivalue)
190{
191 int ret;
192
193 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
194 max_reqs, tbl->max_slots);
195
196 if (max_reqs > NFS4_MAX_SLOT_TABLE)
197 max_reqs = NFS4_MAX_SLOT_TABLE;
198
199 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
200 if (ret)
201 goto out;
202
203 spin_lock(&tbl->slot_tbl_lock);
204 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
205 spin_unlock(&tbl->slot_tbl_lock);
206
207 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
208 tbl, tbl->slots, tbl->max_slots);
209out:
210 dprintk("<-- %s: return %d\n", __func__, ret);
211 return ret;
212}
213
214/* Destroy the slot table */
215static void nfs4_destroy_slot_tables(struct nfs4_session *session)
216{
217 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
218 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
219}
220
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500221static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
222{
223 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
224 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
225 struct nfs4_slot *slot = pslot;
226 struct nfs4_slot_table *tbl = slot->table;
227
228 if (nfs4_session_draining(tbl->session) && !args->sa_privileged)
229 return false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500230 slot->generation = tbl->generation;
231 args->sa_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500232 res->sr_timestamp = jiffies;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500233 res->sr_slot = slot;
234 res->sr_status_flags = 0;
235 res->sr_status = 1;
236 return true;
237}
238
239static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
240 struct nfs4_slot *slot)
241{
242 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
243 return true;
244 return false;
245}
246
247bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
248 struct nfs4_slot *slot)
249{
250 if (slot->slot_nr > tbl->max_slotid)
251 return false;
252 return __nfs41_wake_and_assign_slot(tbl, slot);
253}
254
255static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
256{
257 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
258 if (!IS_ERR(slot)) {
259 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
260 if (ret)
261 return ret;
262 nfs4_free_slot(tbl, slot);
263 }
264 return false;
265}
266
267void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
268{
269 for (;;) {
270 if (!nfs41_try_wake_next_slot_table_entry(tbl))
271 break;
272 }
273}
274
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500275static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
276 u32 target_highest_slotid)
277{
278 u32 max_slotid;
279
280 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
281 if (max_slotid > tbl->server_highest_slotid)
282 max_slotid = tbl->server_highest_slotid;
283 if (max_slotid > tbl->target_highest_slotid)
284 max_slotid = tbl->target_highest_slotid;
285 tbl->max_slotid = max_slotid;
286 nfs41_wake_slot_table(tbl);
287}
288
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500289/* Update the client's idea of target_highest_slotid */
290static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
291 u32 target_highest_slotid)
292{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500293 if (tbl->target_highest_slotid == target_highest_slotid)
294 return;
295 tbl->target_highest_slotid = target_highest_slotid;
296 tbl->generation++;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500297}
298
299void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
300 u32 target_highest_slotid)
301{
302 spin_lock(&tbl->slot_tbl_lock);
303 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500304 tbl->d_target_highest_slotid = 0;
305 tbl->d2_target_highest_slotid = 0;
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500306 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500307 spin_unlock(&tbl->slot_tbl_lock);
308}
309
310static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
311 u32 highest_slotid)
312{
313 if (tbl->server_highest_slotid == highest_slotid)
314 return;
315 if (tbl->highest_used_slotid > highest_slotid)
316 return;
317 /* Deallocate slots */
318 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
319 tbl->server_highest_slotid = highest_slotid;
320}
321
Trond Myklebust1fa80642012-12-02 13:54:59 -0500322static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
323{
324 s1 -= s2;
325 if (s1 == 0)
326 return 0;
327 if (s1 < 0)
328 return (s1 - 1) >> 1;
329 return (s1 + 1) >> 1;
330}
331
332static int nfs41_sign_s32(s32 s1)
333{
334 if (s1 > 0)
335 return 1;
336 if (s1 < 0)
337 return -1;
338 return 0;
339}
340
341static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
342{
343 if (!s1 || !s2)
344 return true;
345 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
346}
347
348/* Try to eliminate outliers by checking for sharp changes in the
349 * derivatives and second derivatives
350 */
351static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
352 u32 new_target)
353{
354 s32 d_target, d2_target;
355 bool ret = true;
356
357 d_target = nfs41_derivative_target_slotid(new_target,
358 tbl->target_highest_slotid);
359 d2_target = nfs41_derivative_target_slotid(d_target,
360 tbl->d_target_highest_slotid);
361 /* Is first derivative same sign? */
362 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
363 ret = false;
364 /* Is second derivative same sign? */
365 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
366 ret = false;
367 tbl->d_target_highest_slotid = d_target;
368 tbl->d2_target_highest_slotid = d2_target;
369 return ret;
370}
371
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500372void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
373 struct nfs4_slot *slot,
374 struct nfs4_sequence_res *res)
375{
376 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500377 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
378 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
379 if (tbl->generation == slot->generation)
380 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500381 nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500382 spin_unlock(&tbl->slot_tbl_lock);
383}
384
385/*
386 * Initialize or reset the forechannel and backchannel tables
387 */
388int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
389{
390 struct nfs4_slot_table *tbl;
391 int status;
392
393 dprintk("--> %s\n", __func__);
394 /* Fore channel */
395 tbl = &ses->fc_slot_table;
396 tbl->session = ses;
397 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
398 if (status) /* -ENOMEM */
399 return status;
400 /* Back channel */
401 tbl = &ses->bc_slot_table;
402 tbl->session = ses;
403 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
404 if (status && tbl->slots == NULL)
405 /* Fore and back channel share a connection so get
406 * both slot tables or neither */
407 nfs4_destroy_slot_tables(ses);
408 return status;
409}
410
411struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
412{
413 struct nfs4_session *session;
414 struct nfs4_slot_table *tbl;
415
416 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
417 if (!session)
418 return NULL;
419
420 tbl = &session->fc_slot_table;
421 tbl->highest_used_slotid = NFS4_NO_SLOT;
422 spin_lock_init(&tbl->slot_tbl_lock);
423 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
424 init_completion(&tbl->complete);
425
426 tbl = &session->bc_slot_table;
427 tbl->highest_used_slotid = NFS4_NO_SLOT;
428 spin_lock_init(&tbl->slot_tbl_lock);
429 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
430 init_completion(&tbl->complete);
431
432 session->session_state = 1<<NFS4_SESSION_INITING;
433
434 session->clp = clp;
435 return session;
436}
437
438void nfs4_destroy_session(struct nfs4_session *session)
439{
440 struct rpc_xprt *xprt;
441 struct rpc_cred *cred;
442
443 cred = nfs4_get_exchange_id_cred(session->clp);
444 nfs4_proc_destroy_session(session, cred);
445 if (cred)
446 put_rpccred(cred);
447
448 rcu_read_lock();
449 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
450 rcu_read_unlock();
451 dprintk("%s Destroy backchannel for xprt %p\n",
452 __func__, xprt);
453 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
454 nfs4_destroy_slot_tables(session);
455 kfree(session);
456}
457
458/*
459 * With sessions, the client is not marked ready until after a
460 * successful EXCHANGE_ID and CREATE_SESSION.
461 *
462 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
463 * other versions of NFS can be tried.
464 */
465static int nfs41_check_session_ready(struct nfs_client *clp)
466{
467 int ret;
468
469 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
470 ret = nfs4_client_recover_expired_lease(clp);
471 if (ret)
472 return ret;
473 }
474 if (clp->cl_cons_state < NFS_CS_READY)
475 return -EPROTONOSUPPORT;
476 smp_rmb();
477 return 0;
478}
479
480int nfs4_init_session(struct nfs_server *server)
481{
482 struct nfs_client *clp = server->nfs_client;
483 struct nfs4_session *session;
484 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
485 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
486
487 if (!nfs4_has_session(clp))
488 return 0;
489
490 if (server->rsize != 0)
491 target_max_resp_sz = server->rsize;
492 target_max_resp_sz += nfs41_maxread_overhead;
493
494 if (server->wsize != 0)
495 target_max_rqst_sz = server->wsize;
496 target_max_rqst_sz += nfs41_maxwrite_overhead;
497
498 session = clp->cl_session;
499 spin_lock(&clp->cl_lock);
500 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
501 /* Initialise targets and channel attributes */
502 session->fc_target_max_rqst_sz = target_max_rqst_sz;
503 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
504 session->fc_target_max_resp_sz = target_max_resp_sz;
505 session->fc_attrs.max_resp_sz = target_max_resp_sz;
506 } else {
507 /* Just adjust the targets */
508 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
509 session->fc_target_max_rqst_sz = target_max_rqst_sz;
510 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
511 }
512 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
513 session->fc_target_max_resp_sz = target_max_resp_sz;
514 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
515 }
516 }
517 spin_unlock(&clp->cl_lock);
518
519 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
520 nfs4_schedule_lease_recovery(clp);
521
522 return nfs41_check_session_ready(clp);
523}
524
525int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
526{
527 struct nfs4_session *session = clp->cl_session;
528 int ret;
529
530 spin_lock(&clp->cl_lock);
531 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
532 /*
533 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
534 * DS lease to be equal to the MDS lease.
535 */
536 clp->cl_lease_time = lease_time;
537 clp->cl_last_renewal = jiffies;
538 }
539 spin_unlock(&clp->cl_lock);
540
541 ret = nfs41_check_session_ready(clp);
542 if (ret)
543 return ret;
544 /* Test for the DS role */
545 if (!is_ds_client(clp))
546 return -ENODEV;
547 return 0;
548}
549EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
550
551