blob: bcb532def9e2bead7d82de63ea64915d9ab0fff4 [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);
Trond Myklebust045d2a62016-08-28 13:25:43 -040031 init_waitqueue_head(&tbl->slot_waitq);
Chuck Lever744aa522013-08-09 12:48:53 -040032 init_completion(&tbl->complete);
33}
34
Trond Myklebust73e39aa2012-11-26 12:49:34 -050035/*
36 * nfs4_shrink_slot_table - free retired slots from the slot table
37 */
38static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
39{
40 struct nfs4_slot **p;
41 if (newsize >= tbl->max_slots)
42 return;
43
44 p = &tbl->slots;
45 while (newsize--)
46 p = &(*p)->next;
47 while (*p) {
48 struct nfs4_slot *slot = *p;
49
50 *p = slot->next;
51 kfree(slot);
52 tbl->max_slots--;
53 }
54}
55
Chuck Lever9d330592013-08-09 12:48:44 -040056/**
57 * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
Trond Myklebust302fad72019-02-18 13:32:38 -050058 * @tbl: controlling slot table
Chuck Lever9d330592013-08-09 12:48:44 -040059 *
60 */
61void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
62{
63 if (nfs4_slot_tbl_draining(tbl))
64 complete(&tbl->complete);
65}
66
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067/*
68 * nfs4_free_slot - free a slot and efficiently update slot table.
69 *
70 * freeing a slot is trivially done by clearing its respective bit
71 * in the bitmap.
72 * If the freed slotid equals highest_used_slotid we want to update it
73 * so that the server would be able to size down the slot table if needed,
74 * otherwise we know that the highest_used_slotid is still in use.
75 * When updating highest_used_slotid there may be "holes" in the bitmap
76 * so we need to scan down from highest_used_slotid to 0 looking for the now
77 * highest slotid in use.
78 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
79 *
80 * Must be called while holding tbl->slot_tbl_lock
81 */
82void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
83{
84 u32 slotid = slot->slot_nr;
85
86 /* clear used bit in bitmap */
87 __clear_bit(slotid, tbl->used_slots);
88
89 /* update highest_used_slotid when it is freed */
90 if (slotid == tbl->highest_used_slotid) {
91 u32 new_max = find_last_bit(tbl->used_slots, slotid);
92 if (new_max < slotid)
93 tbl->highest_used_slotid = new_max;
94 else {
95 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson774d5f12013-05-20 14:13:50 -040096 nfs4_slot_tbl_drain_complete(tbl);
Trond Myklebust73e39aa2012-11-26 12:49:34 -050097 }
98 }
Chuck Levere8d92382013-08-09 12:47:51 -040099 dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500100 slotid, tbl->highest_used_slotid);
101}
102
103static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
104 u32 slotid, u32 seq_init, gfp_t gfp_mask)
105{
106 struct nfs4_slot *slot;
107
108 slot = kzalloc(sizeof(*slot), gfp_mask);
109 if (slot) {
110 slot->table = tbl;
111 slot->slot_nr = slotid;
112 slot->seq_nr = seq_init;
Trond Myklebust3453d572018-06-20 17:53:34 -0400113 slot->seq_nr_highest_sent = seq_init;
114 slot->seq_nr_last_acked = seq_init - 1;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500115 }
116 return slot;
117}
118
119static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
120 u32 slotid, u32 seq_init, gfp_t gfp_mask)
121{
122 struct nfs4_slot **p, *slot;
123
124 p = &tbl->slots;
125 for (;;) {
126 if (*p == NULL) {
127 *p = nfs4_new_slot(tbl, tbl->max_slots,
128 seq_init, gfp_mask);
129 if (*p == NULL)
130 break;
131 tbl->max_slots++;
132 }
133 slot = *p;
134 if (slot->slot_nr == slotid)
135 return slot;
136 p = &slot->next;
137 }
138 return ERR_PTR(-ENOMEM);
139}
140
Trond Myklebust810d82e2016-01-23 15:18:18 -0500141static void nfs4_lock_slot(struct nfs4_slot_table *tbl,
142 struct nfs4_slot *slot)
143{
144 u32 slotid = slot->slot_nr;
145
146 __set_bit(slotid, tbl->used_slots);
147 if (slotid > tbl->highest_used_slotid ||
148 tbl->highest_used_slotid == NFS4_NO_SLOT)
149 tbl->highest_used_slotid = slotid;
150 slot->generation = tbl->generation;
151}
152
153/*
154 * nfs4_try_to_lock_slot - Given a slot try to allocate it
155 *
156 * Note: must be called with the slot_tbl_lock held.
157 */
158bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
159{
160 if (nfs4_test_locked_slot(tbl, slot->slot_nr))
161 return false;
162 nfs4_lock_slot(tbl, slot);
163 return true;
164}
165
166/*
167 * nfs4_lookup_slot - Find a slot but don't allocate it
168 *
169 * Note: must be called with the slot_tbl_lock held.
170 */
171struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid)
172{
173 if (slotid <= tbl->max_slotid)
Fred Isaman9a837852016-09-27 15:37:11 -0400174 return nfs4_find_or_create_slot(tbl, slotid, 0, GFP_NOWAIT);
Trond Myklebust810d82e2016-01-23 15:18:18 -0500175 return ERR_PTR(-E2BIG);
176}
177
Trond Myklebuste09c9782016-08-27 23:44:04 -0400178static int nfs4_slot_get_seqid(struct nfs4_slot_table *tbl, u32 slotid,
179 u32 *seq_nr)
180 __must_hold(&tbl->slot_tbl_lock)
181{
182 struct nfs4_slot *slot;
Arnd Bergmann68a56402016-10-18 00:05:35 +0200183 int ret;
Trond Myklebuste09c9782016-08-27 23:44:04 -0400184
185 slot = nfs4_lookup_slot(tbl, slotid);
Arnd Bergmann68a56402016-10-18 00:05:35 +0200186 ret = PTR_ERR_OR_ZERO(slot);
187 if (!ret)
188 *seq_nr = slot->seq_nr;
189
190 return ret;
Trond Myklebuste09c9782016-08-27 23:44:04 -0400191}
192
193/*
194 * nfs4_slot_seqid_in_use - test if a slot sequence id is still in use
195 *
196 * Given a slot table, slot id and sequence number, determine if the
197 * RPC call in question is still in flight. This function is mainly
198 * intended for use by the callback channel.
199 */
Trond Myklebust045d2a62016-08-28 13:25:43 -0400200static bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl,
201 u32 slotid, u32 seq_nr)
Trond Myklebuste09c9782016-08-27 23:44:04 -0400202{
Shuah Khan0ac84b72016-11-07 10:48:16 -0700203 u32 cur_seq = 0;
Trond Myklebuste09c9782016-08-27 23:44:04 -0400204 bool ret = false;
205
206 spin_lock(&tbl->slot_tbl_lock);
207 if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 &&
208 cur_seq == seq_nr && test_bit(slotid, tbl->used_slots))
209 ret = true;
210 spin_unlock(&tbl->slot_tbl_lock);
211 return ret;
212}
213
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500214/*
Trond Myklebust045d2a62016-08-28 13:25:43 -0400215 * nfs4_slot_wait_on_seqid - wait until a slot sequence id is complete
216 *
217 * Given a slot table, slot id and sequence number, wait until the
218 * corresponding RPC call completes. This function is mainly
219 * intended for use by the callback channel.
220 */
221int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl,
222 u32 slotid, u32 seq_nr,
223 unsigned long timeout)
224{
225 if (wait_event_timeout(tbl->slot_waitq,
226 !nfs4_slot_seqid_in_use(tbl, slotid, seq_nr),
227 timeout) == 0)
228 return -ETIMEDOUT;
229 return 0;
230}
231
232/*
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500233 * nfs4_alloc_slot - efficiently look for a free slot
234 *
235 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
236 * If found, we mark the slot as used, update the highest_used_slotid,
237 * and respectively set up the sequence operation args.
238 *
239 * Note: must be called with under the slot_tbl_lock.
240 */
241struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
242{
243 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
244 u32 slotid;
245
246 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
247 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
248 tbl->max_slotid + 1);
249 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
Trond Myklebust810d82e2016-01-23 15:18:18 -0500250 if (slotid <= tbl->max_slotid) {
251 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
252 if (!IS_ERR(ret))
253 nfs4_lock_slot(tbl, ret);
254 }
Chuck Levere8d92382013-08-09 12:47:51 -0400255 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500256 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
Chuck Levere8d92382013-08-09 12:47:51 -0400257 !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500258 return ret;
259}
260
261static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
262 u32 max_reqs, u32 ivalue)
263{
264 if (max_reqs <= tbl->max_slots)
265 return 0;
266 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
267 return 0;
268 return -ENOMEM;
269}
270
271static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
272 u32 server_highest_slotid,
273 u32 ivalue)
274{
275 struct nfs4_slot **p;
276
277 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
278 p = &tbl->slots;
279 while (*p) {
280 (*p)->seq_nr = ivalue;
Trond Myklebust3453d572018-06-20 17:53:34 -0400281 (*p)->seq_nr_highest_sent = ivalue;
282 (*p)->seq_nr_last_acked = ivalue - 1;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500283 p = &(*p)->next;
284 }
285 tbl->highest_used_slotid = NFS4_NO_SLOT;
286 tbl->target_highest_slotid = server_highest_slotid;
287 tbl->server_highest_slotid = server_highest_slotid;
Trond Myklebust1fa80642012-12-02 13:54:59 -0500288 tbl->d_target_highest_slotid = 0;
289 tbl->d2_target_highest_slotid = 0;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500290 tbl->max_slotid = server_highest_slotid;
291}
292
293/*
294 * (re)Initialise a slot table
295 */
296static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
297 u32 max_reqs, u32 ivalue)
298{
299 int ret;
300
Chuck Levere8d92382013-08-09 12:47:51 -0400301 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500302 max_reqs, tbl->max_slots);
303
304 if (max_reqs > NFS4_MAX_SLOT_TABLE)
305 max_reqs = NFS4_MAX_SLOT_TABLE;
306
307 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
308 if (ret)
309 goto out;
310
311 spin_lock(&tbl->slot_tbl_lock);
312 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
313 spin_unlock(&tbl->slot_tbl_lock);
314
Chuck Levere8d92382013-08-09 12:47:51 -0400315 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500316 tbl, tbl->slots, tbl->max_slots);
317out:
318 dprintk("<-- %s: return %d\n", __func__, ret);
319 return ret;
320}
321
Trond Myklebust20b9a902014-02-01 13:47:06 -0500322/*
323 * nfs4_release_slot_table - release all slot table entries
324 */
325static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
326{
327 nfs4_shrink_slot_table(tbl, 0);
328}
329
Chuck Lever744aa522013-08-09 12:48:53 -0400330/**
Trond Myklebust20b9a902014-02-01 13:47:06 -0500331 * nfs4_shutdown_slot_table - release resources attached to a slot table
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400332 * @tbl: slot table to shut down
333 *
334 */
Trond Myklebust20b9a902014-02-01 13:47:06 -0500335void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400336{
Trond Myklebust20b9a902014-02-01 13:47:06 -0500337 nfs4_release_slot_table(tbl);
338 rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400339}
340
341/**
Chuck Lever744aa522013-08-09 12:48:53 -0400342 * nfs4_setup_slot_table - prepare a stand-alone slot table for use
343 * @tbl: slot table to set up
344 * @max_reqs: maximum number of requests allowed
345 * @queue: name to give RPC wait queue
346 *
347 * Returns zero on success, or a negative errno.
348 */
349int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
350 const char *queue)
351{
352 nfs4_init_slot_table(tbl, queue);
353 return nfs4_realloc_slot_table(tbl, max_reqs, 0);
354}
355
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500356static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
357{
358 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
359 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
360 struct nfs4_slot *slot = pslot;
361 struct nfs4_slot_table *tbl = slot->table;
362
Andy Adamson774d5f12013-05-20 14:13:50 -0400363 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500364 return false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500365 slot->generation = tbl->generation;
366 args->sa_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500367 res->sr_timestamp = jiffies;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500368 res->sr_slot = slot;
369 res->sr_status_flags = 0;
370 res->sr_status = 1;
371 return true;
372}
373
374static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
375 struct nfs4_slot *slot)
376{
377 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
378 return true;
379 return false;
380}
381
382bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
383 struct nfs4_slot *slot)
384{
385 if (slot->slot_nr > tbl->max_slotid)
386 return false;
387 return __nfs41_wake_and_assign_slot(tbl, slot);
388}
389
390static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
391{
392 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
393 if (!IS_ERR(slot)) {
394 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
395 if (ret)
396 return ret;
397 nfs4_free_slot(tbl, slot);
398 }
399 return false;
400}
401
402void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
403{
404 for (;;) {
405 if (!nfs41_try_wake_next_slot_table_entry(tbl))
406 break;
407 }
408}
409
Chuck Lever1cec16a2013-09-04 12:26:03 -0400410#if defined(CONFIG_NFS_V4_1)
411
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500412static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
413 u32 target_highest_slotid)
414{
415 u32 max_slotid;
416
417 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
418 if (max_slotid > tbl->server_highest_slotid)
419 max_slotid = tbl->server_highest_slotid;
420 if (max_slotid > tbl->target_highest_slotid)
421 max_slotid = tbl->target_highest_slotid;
422 tbl->max_slotid = max_slotid;
423 nfs41_wake_slot_table(tbl);
424}
425
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500426/* Update the client's idea of target_highest_slotid */
427static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
428 u32 target_highest_slotid)
429{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500430 if (tbl->target_highest_slotid == target_highest_slotid)
431 return;
432 tbl->target_highest_slotid = target_highest_slotid;
433 tbl->generation++;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500434}
435
436void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
437 u32 target_highest_slotid)
438{
439 spin_lock(&tbl->slot_tbl_lock);
440 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500441 tbl->d_target_highest_slotid = 0;
442 tbl->d2_target_highest_slotid = 0;
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500443 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500444 spin_unlock(&tbl->slot_tbl_lock);
445}
446
447static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
448 u32 highest_slotid)
449{
450 if (tbl->server_highest_slotid == highest_slotid)
451 return;
452 if (tbl->highest_used_slotid > highest_slotid)
453 return;
454 /* Deallocate slots */
455 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
456 tbl->server_highest_slotid = highest_slotid;
457}
458
Trond Myklebust1fa80642012-12-02 13:54:59 -0500459static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
460{
461 s1 -= s2;
462 if (s1 == 0)
463 return 0;
464 if (s1 < 0)
465 return (s1 - 1) >> 1;
466 return (s1 + 1) >> 1;
467}
468
469static int nfs41_sign_s32(s32 s1)
470{
471 if (s1 > 0)
472 return 1;
473 if (s1 < 0)
474 return -1;
475 return 0;
476}
477
478static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
479{
480 if (!s1 || !s2)
481 return true;
482 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
483}
484
485/* Try to eliminate outliers by checking for sharp changes in the
486 * derivatives and second derivatives
487 */
488static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
489 u32 new_target)
490{
491 s32 d_target, d2_target;
492 bool ret = true;
493
494 d_target = nfs41_derivative_target_slotid(new_target,
495 tbl->target_highest_slotid);
496 d2_target = nfs41_derivative_target_slotid(d_target,
497 tbl->d_target_highest_slotid);
498 /* Is first derivative same sign? */
499 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
500 ret = false;
501 /* Is second derivative same sign? */
502 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
503 ret = false;
504 tbl->d_target_highest_slotid = d_target;
505 tbl->d2_target_highest_slotid = d2_target;
506 return ret;
507}
508
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500509void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
510 struct nfs4_slot *slot,
511 struct nfs4_sequence_res *res)
512{
513 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebust1fa80642012-12-02 13:54:59 -0500514 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
515 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
516 if (tbl->generation == slot->generation)
517 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
Trond Myklebustb0ef9642012-12-11 12:10:14 -0500518 nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500519 spin_unlock(&tbl->slot_tbl_lock);
520}
521
Trond Myklebust20b9a902014-02-01 13:47:06 -0500522static void nfs4_release_session_slot_tables(struct nfs4_session *session)
Chuck Lever9d330592013-08-09 12:48:44 -0400523{
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400524 nfs4_release_slot_table(&session->fc_slot_table);
525 nfs4_release_slot_table(&session->bc_slot_table);
Chuck Lever9d330592013-08-09 12:48:44 -0400526}
527
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500528/*
529 * Initialize or reset the forechannel and backchannel tables
530 */
531int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
532{
533 struct nfs4_slot_table *tbl;
534 int status;
535
536 dprintk("--> %s\n", __func__);
537 /* Fore channel */
538 tbl = &ses->fc_slot_table;
539 tbl->session = ses;
540 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustb1c0df52015-02-18 11:34:58 -0800541 if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500542 return status;
543 /* Back channel */
544 tbl = &ses->bc_slot_table;
545 tbl->session = ses;
546 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
547 if (status && tbl->slots == NULL)
548 /* Fore and back channel share a connection so get
549 * both slot tables or neither */
Trond Myklebust20b9a902014-02-01 13:47:06 -0500550 nfs4_release_session_slot_tables(ses);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500551 return status;
552}
553
554struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
555{
556 struct nfs4_session *session;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500557
558 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
559 if (!session)
560 return NULL;
561
Chuck Lever744aa522013-08-09 12:48:53 -0400562 nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
563 nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500564 session->session_state = 1<<NFS4_SESSION_INITING;
565
566 session->clp = clp;
567 return session;
568}
569
Trond Myklebust20b9a902014-02-01 13:47:06 -0500570static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
571{
572 nfs4_shutdown_slot_table(&session->fc_slot_table);
573 nfs4_shutdown_slot_table(&session->bc_slot_table);
574}
575
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500576void nfs4_destroy_session(struct nfs4_session *session)
577{
578 struct rpc_xprt *xprt;
NeilBrowna52458b2018-12-03 11:30:31 +1100579 const struct cred *cred;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500580
Chuck Lever73d8bde2013-07-24 12:28:37 -0400581 cred = nfs4_get_clid_cred(session->clp);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500582 nfs4_proc_destroy_session(session, cred);
NeilBrowna52458b2018-12-03 11:30:31 +1100583 put_cred(cred);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500584
585 rcu_read_lock();
586 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
587 rcu_read_unlock();
588 dprintk("%s Destroy backchannel for xprt %p\n",
589 __func__, xprt);
590 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Chuck Levereb2a1cd2013-08-09 12:49:02 -0400591 nfs4_destroy_session_slot_tables(session);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500592 kfree(session);
593}
594
595/*
596 * With sessions, the client is not marked ready until after a
597 * successful EXCHANGE_ID and CREATE_SESSION.
598 *
599 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
600 * other versions of NFS can be tried.
601 */
602static int nfs41_check_session_ready(struct nfs_client *clp)
603{
604 int ret;
605
606 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
607 ret = nfs4_client_recover_expired_lease(clp);
608 if (ret)
609 return ret;
610 }
611 if (clp->cl_cons_state < NFS_CS_READY)
612 return -EPROTONOSUPPORT;
613 smp_rmb();
614 return 0;
615}
616
Andy Adamson18aad3d2013-06-26 12:21:49 -0400617int nfs4_init_session(struct nfs_client *clp)
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500618{
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500619 if (!nfs4_has_session(clp))
620 return 0;
621
Andy Adamson18aad3d2013-06-26 12:21:49 -0400622 clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500623 return nfs41_check_session_ready(clp);
624}
625
626int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
627{
628 struct nfs4_session *session = clp->cl_session;
629 int ret;
630
631 spin_lock(&clp->cl_lock);
632 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
633 /*
634 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
635 * DS lease to be equal to the MDS lease.
636 */
637 clp->cl_lease_time = lease_time;
638 clp->cl_last_renewal = jiffies;
639 }
640 spin_unlock(&clp->cl_lock);
641
642 ret = nfs41_check_session_ready(clp);
643 if (ret)
644 return ret;
645 /* Test for the DS role */
646 if (!is_ds_client(clp))
647 return -ENODEV;
648 return 0;
649}
650EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
651
Chuck Lever9d330592013-08-09 12:48:44 -0400652#endif /* defined(CONFIG_NFS_V4_1) */