blob: 066cfa101b4133517e324a7305d6917338da0a93 [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;
181 tbl->max_slotid = server_highest_slotid;
182}
183
184/*
185 * (re)Initialise a slot table
186 */
187static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
188 u32 max_reqs, u32 ivalue)
189{
190 int ret;
191
192 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
193 max_reqs, tbl->max_slots);
194
195 if (max_reqs > NFS4_MAX_SLOT_TABLE)
196 max_reqs = NFS4_MAX_SLOT_TABLE;
197
198 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
199 if (ret)
200 goto out;
201
202 spin_lock(&tbl->slot_tbl_lock);
203 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
204 spin_unlock(&tbl->slot_tbl_lock);
205
206 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
207 tbl, tbl->slots, tbl->max_slots);
208out:
209 dprintk("<-- %s: return %d\n", __func__, ret);
210 return ret;
211}
212
213/* Destroy the slot table */
214static void nfs4_destroy_slot_tables(struct nfs4_session *session)
215{
216 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
217 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
218}
219
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500220static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
221{
222 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
223 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
224 struct nfs4_slot *slot = pslot;
225 struct nfs4_slot_table *tbl = slot->table;
226
227 if (nfs4_session_draining(tbl->session) && !args->sa_privileged)
228 return false;
229 slot->renewal_time = jiffies;
230 slot->generation = tbl->generation;
231 args->sa_slot = slot;
232 res->sr_slot = slot;
233 res->sr_status_flags = 0;
234 res->sr_status = 1;
235 return true;
236}
237
238static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
239 struct nfs4_slot *slot)
240{
241 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
242 return true;
243 return false;
244}
245
246bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
247 struct nfs4_slot *slot)
248{
249 if (slot->slot_nr > tbl->max_slotid)
250 return false;
251 return __nfs41_wake_and_assign_slot(tbl, slot);
252}
253
254static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
255{
256 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
257 if (!IS_ERR(slot)) {
258 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
259 if (ret)
260 return ret;
261 nfs4_free_slot(tbl, slot);
262 }
263 return false;
264}
265
266void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
267{
268 for (;;) {
269 if (!nfs41_try_wake_next_slot_table_entry(tbl))
270 break;
271 }
272}
273
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500274/* Update the client's idea of target_highest_slotid */
275static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
276 u32 target_highest_slotid)
277{
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500278 unsigned int max_slotid;
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500279
280 if (tbl->target_highest_slotid == target_highest_slotid)
281 return;
282 tbl->target_highest_slotid = target_highest_slotid;
283 tbl->generation++;
284
285 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500286 tbl->max_slotid = max_slotid;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500287 nfs41_wake_slot_table(tbl);
Trond Myklebust73e39aa2012-11-26 12:49:34 -0500288}
289
290void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
291 u32 target_highest_slotid)
292{
293 spin_lock(&tbl->slot_tbl_lock);
294 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
295 spin_unlock(&tbl->slot_tbl_lock);
296}
297
298static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
299 u32 highest_slotid)
300{
301 if (tbl->server_highest_slotid == highest_slotid)
302 return;
303 if (tbl->highest_used_slotid > highest_slotid)
304 return;
305 /* Deallocate slots */
306 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
307 tbl->server_highest_slotid = highest_slotid;
308}
309
310void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
311 struct nfs4_slot *slot,
312 struct nfs4_sequence_res *res)
313{
314 spin_lock(&tbl->slot_tbl_lock);
315 if (tbl->generation != slot->generation)
316 goto out;
317 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
318 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
319out:
320 spin_unlock(&tbl->slot_tbl_lock);
321}
322
323/*
324 * Initialize or reset the forechannel and backchannel tables
325 */
326int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
327{
328 struct nfs4_slot_table *tbl;
329 int status;
330
331 dprintk("--> %s\n", __func__);
332 /* Fore channel */
333 tbl = &ses->fc_slot_table;
334 tbl->session = ses;
335 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
336 if (status) /* -ENOMEM */
337 return status;
338 /* Back channel */
339 tbl = &ses->bc_slot_table;
340 tbl->session = ses;
341 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
342 if (status && tbl->slots == NULL)
343 /* Fore and back channel share a connection so get
344 * both slot tables or neither */
345 nfs4_destroy_slot_tables(ses);
346 return status;
347}
348
349struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
350{
351 struct nfs4_session *session;
352 struct nfs4_slot_table *tbl;
353
354 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
355 if (!session)
356 return NULL;
357
358 tbl = &session->fc_slot_table;
359 tbl->highest_used_slotid = NFS4_NO_SLOT;
360 spin_lock_init(&tbl->slot_tbl_lock);
361 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
362 init_completion(&tbl->complete);
363
364 tbl = &session->bc_slot_table;
365 tbl->highest_used_slotid = NFS4_NO_SLOT;
366 spin_lock_init(&tbl->slot_tbl_lock);
367 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
368 init_completion(&tbl->complete);
369
370 session->session_state = 1<<NFS4_SESSION_INITING;
371
372 session->clp = clp;
373 return session;
374}
375
376void nfs4_destroy_session(struct nfs4_session *session)
377{
378 struct rpc_xprt *xprt;
379 struct rpc_cred *cred;
380
381 cred = nfs4_get_exchange_id_cred(session->clp);
382 nfs4_proc_destroy_session(session, cred);
383 if (cred)
384 put_rpccred(cred);
385
386 rcu_read_lock();
387 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
388 rcu_read_unlock();
389 dprintk("%s Destroy backchannel for xprt %p\n",
390 __func__, xprt);
391 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
392 nfs4_destroy_slot_tables(session);
393 kfree(session);
394}
395
396/*
397 * With sessions, the client is not marked ready until after a
398 * successful EXCHANGE_ID and CREATE_SESSION.
399 *
400 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
401 * other versions of NFS can be tried.
402 */
403static int nfs41_check_session_ready(struct nfs_client *clp)
404{
405 int ret;
406
407 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
408 ret = nfs4_client_recover_expired_lease(clp);
409 if (ret)
410 return ret;
411 }
412 if (clp->cl_cons_state < NFS_CS_READY)
413 return -EPROTONOSUPPORT;
414 smp_rmb();
415 return 0;
416}
417
418int nfs4_init_session(struct nfs_server *server)
419{
420 struct nfs_client *clp = server->nfs_client;
421 struct nfs4_session *session;
422 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
423 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
424
425 if (!nfs4_has_session(clp))
426 return 0;
427
428 if (server->rsize != 0)
429 target_max_resp_sz = server->rsize;
430 target_max_resp_sz += nfs41_maxread_overhead;
431
432 if (server->wsize != 0)
433 target_max_rqst_sz = server->wsize;
434 target_max_rqst_sz += nfs41_maxwrite_overhead;
435
436 session = clp->cl_session;
437 spin_lock(&clp->cl_lock);
438 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
439 /* Initialise targets and channel attributes */
440 session->fc_target_max_rqst_sz = target_max_rqst_sz;
441 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
442 session->fc_target_max_resp_sz = target_max_resp_sz;
443 session->fc_attrs.max_resp_sz = target_max_resp_sz;
444 } else {
445 /* Just adjust the targets */
446 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
447 session->fc_target_max_rqst_sz = target_max_rqst_sz;
448 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
449 }
450 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
451 session->fc_target_max_resp_sz = target_max_resp_sz;
452 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
453 }
454 }
455 spin_unlock(&clp->cl_lock);
456
457 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
458 nfs4_schedule_lease_recovery(clp);
459
460 return nfs41_check_session_ready(clp);
461}
462
463int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
464{
465 struct nfs4_session *session = clp->cl_session;
466 int ret;
467
468 spin_lock(&clp->cl_lock);
469 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
470 /*
471 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
472 * DS lease to be equal to the MDS lease.
473 */
474 clp->cl_lease_time = lease_time;
475 clp->cl_last_renewal = jiffies;
476 }
477 spin_unlock(&clp->cl_lock);
478
479 ret = nfs41_check_session_ready(clp);
480 if (ret)
481 return ret;
482 /* Test for the DS role */
483 if (!is_ds_client(clp))
484 return -ENODEV;
485 return 0;
486}
487EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
488
489