blob: c1f4c208f38aee5f472d5898e406778ab8ffd84b [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
Trond Myklebust810d82e2016-01-23 15:18:18 -0500138static void nfs4_lock_slot(struct nfs4_slot_table *tbl,
139 struct nfs4_slot *slot)
140{
141 u32 slotid = slot->slot_nr;
142
143 __set_bit(slotid, tbl->used_slots);
144 if (slotid > tbl->highest_used_slotid ||
145 tbl->highest_used_slotid == NFS4_NO_SLOT)
146 tbl->highest_used_slotid = slotid;
147 slot->generation = tbl->generation;
148}
149
150/*
151 * nfs4_try_to_lock_slot - Given a slot try to allocate it
152 *
153 * Note: must be called with the slot_tbl_lock held.
154 */
155bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
156{
157 if (nfs4_test_locked_slot(tbl, slot->slot_nr))
158 return false;
159 nfs4_lock_slot(tbl, slot);
160 return true;
161}
162
163/*
164 * nfs4_lookup_slot - Find a slot but don't allocate it
165 *
166 * Note: must be called with the slot_tbl_lock held.
167 */
168struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid)
169{
170 if (slotid <= tbl->max_slotid)
171 return nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
172 return ERR_PTR(-E2BIG);
173}
174
Trond Myklebuste09c9782016-08-27 23:44:04 -0400175static int nfs4_slot_get_seqid(struct nfs4_slot_table *tbl, u32 slotid,
176 u32 *seq_nr)
177 __must_hold(&tbl->slot_tbl_lock)
178{
179 struct nfs4_slot *slot;
180
181 slot = nfs4_lookup_slot(tbl, slotid);
182 if (IS_ERR(slot))
183 return PTR_ERR(slot);
184 *seq_nr = slot->seq_nr;
185 return 0;
186}
187
188/*
189 * nfs4_slot_seqid_in_use - test if a slot sequence id is still in use
190 *
191 * Given a slot table, slot id and sequence number, determine if the
192 * RPC call in question is still in flight. This function is mainly
193 * intended for use by the callback channel.
194 */
195bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl, u32 slotid, u32 seq_nr)
196{
197 u32 cur_seq;
198 bool ret = false;
199
200 spin_lock(&tbl->slot_tbl_lock);
201 if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 &&
202 cur_seq == seq_nr && test_bit(slotid, tbl->used_slots))
203 ret = true;
204 spin_unlock(&tbl->slot_tbl_lock);
205 return ret;
206}
207
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500208/*
209 * nfs4_alloc_slot - efficiently look for a free slot
210 *
211 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
212 * If found, we mark the slot as used, update the highest_used_slotid,
213 * and respectively set up the sequence operation args.
214 *
215 * Note: must be called with under the slot_tbl_lock.
216 */
217struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
218{
219 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
220 u32 slotid;
221
222 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
223 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
224 tbl->max_slotid + 1);
225 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
Trond Myklebust810d82e2016-01-23 15:18:18 -0500226 if (slotid <= tbl->max_slotid) {
227 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
228 if (!IS_ERR(ret))
229 nfs4_lock_slot(tbl, ret);
230 }
Chuck Levere8d92382013-08-09 12:47:51 -0400231 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500232 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Chuck Levere8d92382013-08-09 12:47:51 -0400233 !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500234 return ret;
235}
236
237static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
238 u32 max_reqs, u32 ivalue)
239{
240 if (max_reqs <= tbl->max_slots)
241 return 0;
242 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
243 return 0;
244 return -ENOMEM;
245}
246
247static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
248 u32 server_highest_slotid,
249 u32 ivalue)
250{
251 struct nfs4_slot **p;
252
253 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
254 p = &tbl->slots;
255 while (*p) {
256 (*p)->seq_nr = ivalue;
Trond Myklebustac20d162012-12-15 15:36:07 -0500257 (*p)->interrupted = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500258 p = &(*p)->next;
259 }
260 tbl->highest_used_slotid = NFS4_NO_SLOT;
261 tbl->target_highest_slotid = server_highest_slotid;
262 tbl->server_highest_slotid = server_highest_slotid;
Trond Myklebust1fa80642012-12-02 13:54:59 -0500263 tbl->d_target_highest_slotid = 0;
264 tbl->d2_target_highest_slotid = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500265 tbl->max_slotid = server_highest_slotid;
266}
267
268/*
269 * (re)Initialise a slot table
270 */
271static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
272 u32 max_reqs, u32 ivalue)
273{
274 int ret;
275
Chuck Levere8d92382013-08-09 12:47:51 -0400276 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500277 max_reqs, tbl->max_slots);
278
279 if (max_reqs > NFS4_MAX_SLOT_TABLE)
280 max_reqs = NFS4_MAX_SLOT_TABLE;
281
282 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
283 if (ret)
284 goto out;
285
286 spin_lock(&tbl->slot_tbl_lock);
287 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
288 spin_unlock(&tbl->slot_tbl_lock);
289
Chuck Levere8d92382013-08-09 12:47:51 -0400290 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500291 tbl, tbl->slots, tbl->max_slots);
292out:
293 dprintk("<-- %s: return %d\n", __func__, ret);
294 return ret;
295}
296
Trond Myklebust20b9a902014-02-01 13:47:06 -0500297/*
298 * nfs4_release_slot_table - release all slot table entries
299 */
300static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
301{
302 nfs4_shrink_slot_table(tbl, 0);
303}
304
Chuck Lever744aa522013-08-09 12:48:53 -0400305/**
Trond Myklebust20b9a902014-02-01 13:47:06 -0500306 * nfs4_shutdown_slot_table - release resources attached to a slot table
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400307 * @tbl: slot table to shut down
308 *
309 */
Trond Myklebust20b9a902014-02-01 13:47:06 -0500310void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400311{
Trond Myklebust20b9a902014-02-01 13:47:06 -0500312 nfs4_release_slot_table(tbl);
313 rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400314}
315
316/**
Chuck Lever744aa522013-08-09 12:48:53 -0400317 * nfs4_setup_slot_table - prepare a stand-alone slot table for use
318 * @tbl: slot table to set up
319 * @max_reqs: maximum number of requests allowed
320 * @queue: name to give RPC wait queue
321 *
322 * Returns zero on success, or a negative errno.
323 */
324int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
325 const char *queue)
326{
327 nfs4_init_slot_table(tbl, queue);
328 return nfs4_realloc_slot_table(tbl, max_reqs, 0);
329}
330
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500331static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
332{
333 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
334 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
335 struct nfs4_slot *slot = pslot;
336 struct nfs4_slot_table *tbl = slot->table;
337
Andy Adamson774d5f12013-05-20 14:13:50 -0400338 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500339 return false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500340 slot->generation = tbl->generation;
341 args->sa_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500342 res->sr_timestamp = jiffies;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500343 res->sr_slot = slot;
344 res->sr_status_flags = 0;
345 res->sr_status = 1;
346 return true;
347}
348
349static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
350 struct nfs4_slot *slot)
351{
352 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
353 return true;
354 return false;
355}
356
357bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
358 struct nfs4_slot *slot)
359{
360 if (slot->slot_nr > tbl->max_slotid)
361 return false;
362 return __nfs41_wake_and_assign_slot(tbl, slot);
363}
364
365static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
366{
367 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
368 if (!IS_ERR(slot)) {
369 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
370 if (ret)
371 return ret;
372 nfs4_free_slot(tbl, slot);
373 }
374 return false;
375}
376
377void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
378{
379 for (;;) {
380 if (!nfs41_try_wake_next_slot_table_entry(tbl))
381 break;
382 }
383}
384
Chuck Lever1cec16a2013-09-04 12:26:03 -0400385#if defined(CONFIG_NFS_V4_1)
386
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500387static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
388 u32 target_highest_slotid)
389{
390 u32 max_slotid;
391
392 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
393 if (max_slotid > tbl->server_highest_slotid)
394 max_slotid = tbl->server_highest_slotid;
395 if (max_slotid > tbl->target_highest_slotid)
396 max_slotid = tbl->target_highest_slotid;
397 tbl->max_slotid = max_slotid;
398 nfs41_wake_slot_table(tbl);
399}
400
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500401/* Update the client's idea of target_highest_slotid */
402static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
403 u32 target_highest_slotid)
404{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500405 if (tbl->target_highest_slotid == target_highest_slotid)
406 return;
407 tbl->target_highest_slotid = target_highest_slotid;
408 tbl->generation++;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500409}
410
411void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
412 u32 target_highest_slotid)
413{
414 spin_lock(&tbl->slot_tbl_lock);
415 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500416 tbl->d_target_highest_slotid = 0;
417 tbl->d2_target_highest_slotid = 0;
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500418 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500419 spin_unlock(&tbl->slot_tbl_lock);
420}
421
422static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
423 u32 highest_slotid)
424{
425 if (tbl->server_highest_slotid == highest_slotid)
426 return;
427 if (tbl->highest_used_slotid > highest_slotid)
428 return;
429 /* Deallocate slots */
430 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
431 tbl->server_highest_slotid = highest_slotid;
432}
433
Trond Myklebust1fa80642012-12-02 13:54:59 -0500434static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
435{
436 s1 -= s2;
437 if (s1 == 0)
438 return 0;
439 if (s1 < 0)
440 return (s1 - 1) >> 1;
441 return (s1 + 1) >> 1;
442}
443
444static int nfs41_sign_s32(s32 s1)
445{
446 if (s1 > 0)
447 return 1;
448 if (s1 < 0)
449 return -1;
450 return 0;
451}
452
453static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
454{
455 if (!s1 || !s2)
456 return true;
457 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
458}
459
460/* Try to eliminate outliers by checking for sharp changes in the
461 * derivatives and second derivatives
462 */
463static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
464 u32 new_target)
465{
466 s32 d_target, d2_target;
467 bool ret = true;
468
469 d_target = nfs41_derivative_target_slotid(new_target,
470 tbl->target_highest_slotid);
471 d2_target = nfs41_derivative_target_slotid(d_target,
472 tbl->d_target_highest_slotid);
473 /* Is first derivative same sign? */
474 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
475 ret = false;
476 /* Is second derivative same sign? */
477 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
478 ret = false;
479 tbl->d_target_highest_slotid = d_target;
480 tbl->d2_target_highest_slotid = d2_target;
481 return ret;
482}
483
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500484void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
485 struct nfs4_slot *slot,
486 struct nfs4_sequence_res *res)
487{
488 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500489 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
490 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
491 if (tbl->generation == slot->generation)
492 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500493 nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500494 spin_unlock(&tbl->slot_tbl_lock);
495}
496
Trond Myklebust20b9a902014-02-01 13:47:06 -0500497static void nfs4_release_session_slot_tables(struct nfs4_session *session)
Chuck Lever9d330592013-08-09 12:48:44 -0400498{
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400499 nfs4_release_slot_table(&session->fc_slot_table);
500 nfs4_release_slot_table(&session->bc_slot_table);
Chuck Lever9d330592013-08-09 12:48:44 -0400501}
502
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500503/*
504 * Initialize or reset the forechannel and backchannel tables
505 */
506int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
507{
508 struct nfs4_slot_table *tbl;
509 int status;
510
511 dprintk("--> %s\n", __func__);
512 /* Fore channel */
513 tbl = &ses->fc_slot_table;
514 tbl->session = ses;
515 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustb1c0df52015-02-18 11:34:58 -0800516 if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500517 return status;
518 /* Back channel */
519 tbl = &ses->bc_slot_table;
520 tbl->session = ses;
521 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
522 if (status && tbl->slots == NULL)
523 /* Fore and back channel share a connection so get
524 * both slot tables or neither */
Trond Myklebust20b9a902014-02-01 13:47:06 -0500525 nfs4_release_session_slot_tables(ses);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500526 return status;
527}
528
529struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
530{
531 struct nfs4_session *session;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500532
533 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
534 if (!session)
535 return NULL;
536
Chuck Lever744aa522013-08-09 12:48:53 -0400537 nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
538 nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500539 session->session_state = 1<<NFS4_SESSION_INITING;
540
541 session->clp = clp;
542 return session;
543}
544
Trond Myklebust20b9a902014-02-01 13:47:06 -0500545static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
546{
547 nfs4_shutdown_slot_table(&session->fc_slot_table);
548 nfs4_shutdown_slot_table(&session->bc_slot_table);
549}
550
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500551void nfs4_destroy_session(struct nfs4_session *session)
552{
553 struct rpc_xprt *xprt;
554 struct rpc_cred *cred;
555
Chuck Lever73d8bde2013-07-24 12:28:37 -0400556 cred = nfs4_get_clid_cred(session->clp);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500557 nfs4_proc_destroy_session(session, cred);
558 if (cred)
559 put_rpccred(cred);
560
561 rcu_read_lock();
562 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
563 rcu_read_unlock();
564 dprintk("%s Destroy backchannel for xprt %p\n",
565 __func__, xprt);
566 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400567 nfs4_destroy_session_slot_tables(session);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500568 kfree(session);
569}
570
571/*
572 * With sessions, the client is not marked ready until after a
573 * successful EXCHANGE_ID and CREATE_SESSION.
574 *
575 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
576 * other versions of NFS can be tried.
577 */
578static int nfs41_check_session_ready(struct nfs_client *clp)
579{
580 int ret;
581
582 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
583 ret = nfs4_client_recover_expired_lease(clp);
584 if (ret)
585 return ret;
586 }
587 if (clp->cl_cons_state < NFS_CS_READY)
588 return -EPROTONOSUPPORT;
589 smp_rmb();
590 return 0;
591}
592
Andy Adamson18aad3d2013-06-26 12:21:49 -0400593int nfs4_init_session(struct nfs_client *clp)
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500594{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500595 if (!nfs4_has_session(clp))
596 return 0;
597
Andy Adamson18aad3d2013-06-26 12:21:49 -0400598 clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500599 return nfs41_check_session_ready(clp);
600}
601
602int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
603{
604 struct nfs4_session *session = clp->cl_session;
605 int ret;
606
607 spin_lock(&clp->cl_lock);
608 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
609 /*
610 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
611 * DS lease to be equal to the MDS lease.
612 */
613 clp->cl_lease_time = lease_time;
614 clp->cl_last_renewal = jiffies;
615 }
616 spin_unlock(&clp->cl_lock);
617
618 ret = nfs41_check_session_ready(clp);
619 if (ret)
620 return ret;
621 /* Test for the DS role */
622 if (!is_ds_client(clp))
623 return -ENODEV;
624 return 0;
625}
626EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
627
Chuck Lever9d330592013-08-09 12:48:44 -0400628#endif /* defined(CONFIG_NFS_V4_1) */