0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/errno.h>
0010 #include <linux/string.h>
0011 #include <linux/printk.h>
0012 #include <linux/slab.h>
0013 #include <linux/sunrpc/sched.h>
0014 #include <linux/sunrpc/bc_xprt.h>
0015 #include <linux/nfs.h>
0016 #include <linux/nfs4.h>
0017 #include <linux/nfs_fs.h>
0018 #include <linux/module.h>
0019
0020 #include "nfs4_fs.h"
0021 #include "internal.h"
0022 #include "nfs4session.h"
0023 #include "callback.h"
0024
0025 #define NFSDBG_FACILITY NFSDBG_STATE
0026
0027 static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue)
0028 {
0029 tbl->highest_used_slotid = NFS4_NO_SLOT;
0030 spin_lock_init(&tbl->slot_tbl_lock);
0031 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, queue);
0032 init_waitqueue_head(&tbl->slot_waitq);
0033 init_completion(&tbl->complete);
0034 }
0035
0036
0037
0038
0039 static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
0040 {
0041 struct nfs4_slot **p;
0042 if (newsize >= tbl->max_slots)
0043 return;
0044
0045 p = &tbl->slots;
0046 while (newsize--)
0047 p = &(*p)->next;
0048 while (*p) {
0049 struct nfs4_slot *slot = *p;
0050
0051 *p = slot->next;
0052 kfree(slot);
0053 tbl->max_slots--;
0054 }
0055 }
0056
0057
0058
0059
0060
0061
0062 void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
0063 {
0064 if (nfs4_slot_tbl_draining(tbl))
0065 complete(&tbl->complete);
0066 }
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
0084 {
0085 u32 slotid = slot->slot_nr;
0086
0087
0088 __clear_bit(slotid, tbl->used_slots);
0089
0090
0091 if (slotid == tbl->highest_used_slotid) {
0092 u32 new_max = find_last_bit(tbl->used_slots, slotid);
0093 if (new_max < slotid)
0094 tbl->highest_used_slotid = new_max;
0095 else {
0096 tbl->highest_used_slotid = NFS4_NO_SLOT;
0097 nfs4_slot_tbl_drain_complete(tbl);
0098 }
0099 }
0100 dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
0101 slotid, tbl->highest_used_slotid);
0102 }
0103
0104 static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
0105 u32 slotid, u32 seq_init, gfp_t gfp_mask)
0106 {
0107 struct nfs4_slot *slot;
0108
0109 slot = kzalloc(sizeof(*slot), gfp_mask);
0110 if (slot) {
0111 slot->table = tbl;
0112 slot->slot_nr = slotid;
0113 slot->seq_nr = seq_init;
0114 slot->seq_nr_highest_sent = seq_init;
0115 slot->seq_nr_last_acked = seq_init - 1;
0116 }
0117 return slot;
0118 }
0119
0120 static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
0121 u32 slotid, u32 seq_init, gfp_t gfp_mask)
0122 {
0123 struct nfs4_slot **p, *slot;
0124
0125 p = &tbl->slots;
0126 for (;;) {
0127 if (*p == NULL) {
0128 *p = nfs4_new_slot(tbl, tbl->max_slots,
0129 seq_init, gfp_mask);
0130 if (*p == NULL)
0131 break;
0132 tbl->max_slots++;
0133 }
0134 slot = *p;
0135 if (slot->slot_nr == slotid)
0136 return slot;
0137 p = &slot->next;
0138 }
0139 return ERR_PTR(-ENOMEM);
0140 }
0141
0142 static void nfs4_lock_slot(struct nfs4_slot_table *tbl,
0143 struct nfs4_slot *slot)
0144 {
0145 u32 slotid = slot->slot_nr;
0146
0147 __set_bit(slotid, tbl->used_slots);
0148 if (slotid > tbl->highest_used_slotid ||
0149 tbl->highest_used_slotid == NFS4_NO_SLOT)
0150 tbl->highest_used_slotid = slotid;
0151 slot->generation = tbl->generation;
0152 }
0153
0154
0155
0156
0157
0158
0159 bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
0160 {
0161 if (nfs4_test_locked_slot(tbl, slot->slot_nr))
0162 return false;
0163 nfs4_lock_slot(tbl, slot);
0164 return true;
0165 }
0166
0167
0168
0169
0170
0171
0172 struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid)
0173 {
0174 if (slotid <= tbl->max_slotid)
0175 return nfs4_find_or_create_slot(tbl, slotid, 0, GFP_NOWAIT);
0176 return ERR_PTR(-E2BIG);
0177 }
0178
0179 static int nfs4_slot_get_seqid(struct nfs4_slot_table *tbl, u32 slotid,
0180 u32 *seq_nr)
0181 __must_hold(&tbl->slot_tbl_lock)
0182 {
0183 struct nfs4_slot *slot;
0184 int ret;
0185
0186 slot = nfs4_lookup_slot(tbl, slotid);
0187 ret = PTR_ERR_OR_ZERO(slot);
0188 if (!ret)
0189 *seq_nr = slot->seq_nr;
0190
0191 return ret;
0192 }
0193
0194
0195
0196
0197
0198
0199
0200
0201 static bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl,
0202 u32 slotid, u32 seq_nr)
0203 {
0204 u32 cur_seq = 0;
0205 bool ret = false;
0206
0207 spin_lock(&tbl->slot_tbl_lock);
0208 if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 &&
0209 cur_seq == seq_nr && test_bit(slotid, tbl->used_slots))
0210 ret = true;
0211 spin_unlock(&tbl->slot_tbl_lock);
0212 return ret;
0213 }
0214
0215
0216
0217
0218
0219
0220
0221
0222 int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl,
0223 u32 slotid, u32 seq_nr,
0224 unsigned long timeout)
0225 {
0226 if (wait_event_timeout(tbl->slot_waitq,
0227 !nfs4_slot_seqid_in_use(tbl, slotid, seq_nr),
0228 timeout) == 0)
0229 return -ETIMEDOUT;
0230 return 0;
0231 }
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
0243 {
0244 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
0245 u32 slotid;
0246
0247 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
0248 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
0249 tbl->max_slotid + 1);
0250 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
0251 if (slotid <= tbl->max_slotid) {
0252 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
0253 if (!IS_ERR(ret))
0254 nfs4_lock_slot(tbl, ret);
0255 }
0256 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
0257 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
0258 !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
0259 return ret;
0260 }
0261
0262 static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
0263 u32 max_reqs, u32 ivalue)
0264 {
0265 if (max_reqs <= tbl->max_slots)
0266 return 0;
0267 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
0268 return 0;
0269 return -ENOMEM;
0270 }
0271
0272 static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
0273 u32 server_highest_slotid,
0274 u32 ivalue)
0275 {
0276 struct nfs4_slot **p;
0277
0278 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
0279 p = &tbl->slots;
0280 while (*p) {
0281 (*p)->seq_nr = ivalue;
0282 (*p)->seq_nr_highest_sent = ivalue;
0283 (*p)->seq_nr_last_acked = ivalue - 1;
0284 p = &(*p)->next;
0285 }
0286 tbl->highest_used_slotid = NFS4_NO_SLOT;
0287 tbl->target_highest_slotid = server_highest_slotid;
0288 tbl->server_highest_slotid = server_highest_slotid;
0289 tbl->d_target_highest_slotid = 0;
0290 tbl->d2_target_highest_slotid = 0;
0291 tbl->max_slotid = server_highest_slotid;
0292 }
0293
0294
0295
0296
0297 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
0298 u32 max_reqs, u32 ivalue)
0299 {
0300 int ret;
0301
0302 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
0303 max_reqs, tbl->max_slots);
0304
0305 if (max_reqs > NFS4_MAX_SLOT_TABLE)
0306 max_reqs = NFS4_MAX_SLOT_TABLE;
0307
0308 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
0309 if (ret)
0310 goto out;
0311
0312 spin_lock(&tbl->slot_tbl_lock);
0313 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
0314 spin_unlock(&tbl->slot_tbl_lock);
0315
0316 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
0317 tbl, tbl->slots, tbl->max_slots);
0318 out:
0319 dprintk("<-- %s: return %d\n", __func__, ret);
0320 return ret;
0321 }
0322
0323
0324
0325
0326 static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
0327 {
0328 nfs4_shrink_slot_table(tbl, 0);
0329 }
0330
0331
0332
0333
0334
0335
0336 void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
0337 {
0338 nfs4_release_slot_table(tbl);
0339 rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
0340 }
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350 int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
0351 const char *queue)
0352 {
0353 nfs4_init_slot_table(tbl, queue);
0354 return nfs4_realloc_slot_table(tbl, max_reqs, 0);
0355 }
0356
0357 static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
0358 {
0359 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
0360 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
0361 struct nfs4_slot *slot = pslot;
0362 struct nfs4_slot_table *tbl = slot->table;
0363
0364 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
0365 return false;
0366 slot->generation = tbl->generation;
0367 args->sa_slot = slot;
0368 res->sr_timestamp = jiffies;
0369 res->sr_slot = slot;
0370 res->sr_status_flags = 0;
0371 res->sr_status = 1;
0372 return true;
0373 }
0374
0375 static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
0376 struct nfs4_slot *slot)
0377 {
0378 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
0379 return true;
0380 return false;
0381 }
0382
0383 bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
0384 struct nfs4_slot *slot)
0385 {
0386 if (slot->slot_nr > tbl->max_slotid)
0387 return false;
0388 return __nfs41_wake_and_assign_slot(tbl, slot);
0389 }
0390
0391 static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
0392 {
0393 struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
0394 if (!IS_ERR(slot)) {
0395 bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
0396 if (ret)
0397 return ret;
0398 nfs4_free_slot(tbl, slot);
0399 }
0400 return false;
0401 }
0402
0403 void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
0404 {
0405 for (;;) {
0406 if (!nfs41_try_wake_next_slot_table_entry(tbl))
0407 break;
0408 }
0409 }
0410
0411 #if defined(CONFIG_NFS_V4_1)
0412
0413 static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
0414 u32 target_highest_slotid)
0415 {
0416 u32 max_slotid;
0417
0418 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
0419 if (max_slotid > tbl->server_highest_slotid)
0420 max_slotid = tbl->server_highest_slotid;
0421 if (max_slotid > tbl->target_highest_slotid)
0422 max_slotid = tbl->target_highest_slotid;
0423 tbl->max_slotid = max_slotid;
0424 nfs41_wake_slot_table(tbl);
0425 }
0426
0427
0428 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
0429 u32 target_highest_slotid)
0430 {
0431 if (tbl->target_highest_slotid == target_highest_slotid)
0432 return;
0433 tbl->target_highest_slotid = target_highest_slotid;
0434 tbl->generation++;
0435 }
0436
0437 void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
0438 u32 target_highest_slotid)
0439 {
0440 spin_lock(&tbl->slot_tbl_lock);
0441 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
0442 tbl->d_target_highest_slotid = 0;
0443 tbl->d2_target_highest_slotid = 0;
0444 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
0445 spin_unlock(&tbl->slot_tbl_lock);
0446 }
0447
0448 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
0449 u32 highest_slotid)
0450 {
0451 if (tbl->server_highest_slotid == highest_slotid)
0452 return;
0453 if (tbl->highest_used_slotid > highest_slotid)
0454 return;
0455
0456 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
0457 tbl->server_highest_slotid = highest_slotid;
0458 }
0459
0460 static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
0461 {
0462 s1 -= s2;
0463 if (s1 == 0)
0464 return 0;
0465 if (s1 < 0)
0466 return (s1 - 1) >> 1;
0467 return (s1 + 1) >> 1;
0468 }
0469
0470 static int nfs41_sign_s32(s32 s1)
0471 {
0472 if (s1 > 0)
0473 return 1;
0474 if (s1 < 0)
0475 return -1;
0476 return 0;
0477 }
0478
0479 static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
0480 {
0481 if (!s1 || !s2)
0482 return true;
0483 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
0484 }
0485
0486
0487
0488
0489 static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
0490 u32 new_target)
0491 {
0492 s32 d_target, d2_target;
0493 bool ret = true;
0494
0495 d_target = nfs41_derivative_target_slotid(new_target,
0496 tbl->target_highest_slotid);
0497 d2_target = nfs41_derivative_target_slotid(d_target,
0498 tbl->d_target_highest_slotid);
0499
0500 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
0501 ret = false;
0502
0503 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
0504 ret = false;
0505 tbl->d_target_highest_slotid = d_target;
0506 tbl->d2_target_highest_slotid = d2_target;
0507 return ret;
0508 }
0509
0510 void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
0511 struct nfs4_slot *slot,
0512 struct nfs4_sequence_res *res)
0513 {
0514 u32 target_highest_slotid = min(res->sr_target_highest_slotid,
0515 NFS4_MAX_SLOTID);
0516 u32 highest_slotid = min(res->sr_highest_slotid, NFS4_MAX_SLOTID);
0517
0518 spin_lock(&tbl->slot_tbl_lock);
0519 if (!nfs41_is_outlier_target_slotid(tbl, target_highest_slotid))
0520 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
0521 if (tbl->generation == slot->generation)
0522 nfs41_set_server_slotid_locked(tbl, highest_slotid);
0523 nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
0524 spin_unlock(&tbl->slot_tbl_lock);
0525 }
0526
0527 static void nfs4_release_session_slot_tables(struct nfs4_session *session)
0528 {
0529 nfs4_release_slot_table(&session->fc_slot_table);
0530 nfs4_release_slot_table(&session->bc_slot_table);
0531 }
0532
0533
0534
0535
0536 int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
0537 {
0538 struct nfs4_slot_table *tbl;
0539 int status;
0540
0541 dprintk("--> %s\n", __func__);
0542
0543 tbl = &ses->fc_slot_table;
0544 tbl->session = ses;
0545 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
0546 if (status || !(ses->flags & SESSION4_BACK_CHAN))
0547 return status;
0548
0549 tbl = &ses->bc_slot_table;
0550 tbl->session = ses;
0551 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
0552 if (status && tbl->slots == NULL)
0553
0554
0555 nfs4_release_session_slot_tables(ses);
0556 return status;
0557 }
0558
0559 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
0560 {
0561 struct nfs4_session *session;
0562
0563 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
0564 if (!session)
0565 return NULL;
0566
0567 nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
0568 nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
0569 session->session_state = 1<<NFS4_SESSION_INITING;
0570
0571 session->clp = clp;
0572 return session;
0573 }
0574
0575 static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
0576 {
0577 nfs4_shutdown_slot_table(&session->fc_slot_table);
0578 nfs4_shutdown_slot_table(&session->bc_slot_table);
0579 }
0580
0581 void nfs4_destroy_session(struct nfs4_session *session)
0582 {
0583 struct rpc_xprt *xprt;
0584 const struct cred *cred;
0585
0586 cred = nfs4_get_clid_cred(session->clp);
0587 nfs4_proc_destroy_session(session, cred);
0588 put_cred(cred);
0589
0590 rcu_read_lock();
0591 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
0592 rcu_read_unlock();
0593 dprintk("%s Destroy backchannel for xprt %p\n",
0594 __func__, xprt);
0595 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
0596 nfs4_destroy_session_slot_tables(session);
0597 kfree(session);
0598 }
0599
0600
0601
0602
0603
0604
0605
0606
0607 static int nfs41_check_session_ready(struct nfs_client *clp)
0608 {
0609 int ret;
0610
0611 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
0612 ret = nfs4_client_recover_expired_lease(clp);
0613 if (ret)
0614 return ret;
0615 }
0616 if (clp->cl_cons_state < NFS_CS_READY)
0617 return -EPROTONOSUPPORT;
0618 smp_rmb();
0619 return 0;
0620 }
0621
0622 int nfs4_init_session(struct nfs_client *clp)
0623 {
0624 if (!nfs4_has_session(clp))
0625 return 0;
0626
0627 clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
0628 return nfs41_check_session_ready(clp);
0629 }
0630
0631 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
0632 {
0633 struct nfs4_session *session = clp->cl_session;
0634 int ret;
0635
0636 spin_lock(&clp->cl_lock);
0637 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
0638
0639
0640
0641
0642 clp->cl_lease_time = lease_time;
0643 clp->cl_last_renewal = jiffies;
0644 }
0645 spin_unlock(&clp->cl_lock);
0646
0647 ret = nfs41_check_session_ready(clp);
0648 if (ret)
0649 return ret;
0650
0651 if (!is_ds_client(clp))
0652 return -ENODEV;
0653 return 0;
0654 }
0655 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
0656
0657 #endif