nfs4session.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  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. #include "nfs4_fs.h"
  19. #include "internal.h"
  20. #include "nfs4session.h"
  21. #include "callback.h"
  22. #define NFSDBG_FACILITY NFSDBG_STATE
  23. static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue)
  24. {
  25. tbl->highest_used_slotid = NFS4_NO_SLOT;
  26. spin_lock_init(&tbl->slot_tbl_lock);
  27. rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, queue);
  28. init_completion(&tbl->complete);
  29. }
  30. /*
  31. * nfs4_shrink_slot_table - free retired slots from the slot table
  32. */
  33. static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
  34. {
  35. struct nfs4_slot **p;
  36. if (newsize >= tbl->max_slots)
  37. return;
  38. p = &tbl->slots;
  39. while (newsize--)
  40. p = &(*p)->next;
  41. while (*p) {
  42. struct nfs4_slot *slot = *p;
  43. *p = slot->next;
  44. kfree(slot);
  45. tbl->max_slots--;
  46. }
  47. }
  48. /**
  49. * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete
  50. * @tbl - controlling slot table
  51. *
  52. */
  53. void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
  54. {
  55. if (nfs4_slot_tbl_draining(tbl))
  56. complete(&tbl->complete);
  57. }
  58. /*
  59. * nfs4_free_slot - free a slot and efficiently update slot table.
  60. *
  61. * freeing a slot is trivially done by clearing its respective bit
  62. * in the bitmap.
  63. * If the freed slotid equals highest_used_slotid we want to update it
  64. * so that the server would be able to size down the slot table if needed,
  65. * otherwise we know that the highest_used_slotid is still in use.
  66. * When updating highest_used_slotid there may be "holes" in the bitmap
  67. * so we need to scan down from highest_used_slotid to 0 looking for the now
  68. * highest slotid in use.
  69. * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
  70. *
  71. * Must be called while holding tbl->slot_tbl_lock
  72. */
  73. void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
  74. {
  75. u32 slotid = slot->slot_nr;
  76. /* clear used bit in bitmap */
  77. __clear_bit(slotid, tbl->used_slots);
  78. /* update highest_used_slotid when it is freed */
  79. if (slotid == tbl->highest_used_slotid) {
  80. u32 new_max = find_last_bit(tbl->used_slots, slotid);
  81. if (new_max < slotid)
  82. tbl->highest_used_slotid = new_max;
  83. else {
  84. tbl->highest_used_slotid = NFS4_NO_SLOT;
  85. nfs4_slot_tbl_drain_complete(tbl);
  86. }
  87. }
  88. dprintk("%s: slotid %u highest_used_slotid %u\n", __func__,
  89. slotid, tbl->highest_used_slotid);
  90. }
  91. static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
  92. u32 slotid, u32 seq_init, gfp_t gfp_mask)
  93. {
  94. struct nfs4_slot *slot;
  95. slot = kzalloc(sizeof(*slot), gfp_mask);
  96. if (slot) {
  97. slot->table = tbl;
  98. slot->slot_nr = slotid;
  99. slot->seq_nr = seq_init;
  100. }
  101. return slot;
  102. }
  103. static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
  104. u32 slotid, u32 seq_init, gfp_t gfp_mask)
  105. {
  106. struct nfs4_slot **p, *slot;
  107. p = &tbl->slots;
  108. for (;;) {
  109. if (*p == NULL) {
  110. *p = nfs4_new_slot(tbl, tbl->max_slots,
  111. seq_init, gfp_mask);
  112. if (*p == NULL)
  113. break;
  114. tbl->max_slots++;
  115. }
  116. slot = *p;
  117. if (slot->slot_nr == slotid)
  118. return slot;
  119. p = &slot->next;
  120. }
  121. return ERR_PTR(-ENOMEM);
  122. }
  123. /*
  124. * nfs4_alloc_slot - efficiently look for a free slot
  125. *
  126. * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
  127. * If found, we mark the slot as used, update the highest_used_slotid,
  128. * and respectively set up the sequence operation args.
  129. *
  130. * Note: must be called with under the slot_tbl_lock.
  131. */
  132. struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
  133. {
  134. struct nfs4_slot *ret = ERR_PTR(-EBUSY);
  135. u32 slotid;
  136. dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
  137. __func__, tbl->used_slots[0], tbl->highest_used_slotid,
  138. tbl->max_slotid + 1);
  139. slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
  140. if (slotid > tbl->max_slotid)
  141. goto out;
  142. ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
  143. if (IS_ERR(ret))
  144. goto out;
  145. __set_bit(slotid, tbl->used_slots);
  146. if (slotid > tbl->highest_used_slotid ||
  147. tbl->highest_used_slotid == NFS4_NO_SLOT)
  148. tbl->highest_used_slotid = slotid;
  149. ret->generation = tbl->generation;
  150. out:
  151. dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n",
  152. __func__, tbl->used_slots[0], tbl->highest_used_slotid,
  153. !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT);
  154. return ret;
  155. }
  156. static 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. static 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. nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
  171. p = &tbl->slots;
  172. while (*p) {
  173. (*p)->seq_nr = ivalue;
  174. (*p)->interrupted = 0;
  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;
  180. tbl->d_target_highest_slotid = 0;
  181. tbl->d2_target_highest_slotid = 0;
  182. tbl->max_slotid = server_highest_slotid;
  183. }
  184. /*
  185. * (re)Initialise a slot table
  186. */
  187. static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
  188. u32 max_reqs, u32 ivalue)
  189. {
  190. int ret;
  191. dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__,
  192. max_reqs, tbl->max_slots);
  193. if (max_reqs > NFS4_MAX_SLOT_TABLE)
  194. max_reqs = NFS4_MAX_SLOT_TABLE;
  195. ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
  196. if (ret)
  197. goto out;
  198. spin_lock(&tbl->slot_tbl_lock);
  199. nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
  200. spin_unlock(&tbl->slot_tbl_lock);
  201. dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__,
  202. tbl, tbl->slots, tbl->max_slots);
  203. out:
  204. dprintk("<-- %s: return %d\n", __func__, ret);
  205. return ret;
  206. }
  207. /*
  208. * nfs4_release_slot_table - release all slot table entries
  209. */
  210. static void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
  211. {
  212. nfs4_shrink_slot_table(tbl, 0);
  213. }
  214. /**
  215. * nfs4_shutdown_slot_table - release resources attached to a slot table
  216. * @tbl: slot table to shut down
  217. *
  218. */
  219. void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl)
  220. {
  221. nfs4_release_slot_table(tbl);
  222. rpc_destroy_wait_queue(&tbl->slot_tbl_waitq);
  223. }
  224. /**
  225. * nfs4_setup_slot_table - prepare a stand-alone slot table for use
  226. * @tbl: slot table to set up
  227. * @max_reqs: maximum number of requests allowed
  228. * @queue: name to give RPC wait queue
  229. *
  230. * Returns zero on success, or a negative errno.
  231. */
  232. int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
  233. const char *queue)
  234. {
  235. nfs4_init_slot_table(tbl, queue);
  236. return nfs4_realloc_slot_table(tbl, max_reqs, 0);
  237. }
  238. static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
  239. {
  240. struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
  241. struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
  242. struct nfs4_slot *slot = pslot;
  243. struct nfs4_slot_table *tbl = slot->table;
  244. if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
  245. return false;
  246. slot->generation = tbl->generation;
  247. args->sa_slot = slot;
  248. res->sr_timestamp = jiffies;
  249. res->sr_slot = slot;
  250. res->sr_status_flags = 0;
  251. res->sr_status = 1;
  252. return true;
  253. }
  254. static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
  255. struct nfs4_slot *slot)
  256. {
  257. if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
  258. return true;
  259. return false;
  260. }
  261. bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
  262. struct nfs4_slot *slot)
  263. {
  264. if (slot->slot_nr > tbl->max_slotid)
  265. return false;
  266. return __nfs41_wake_and_assign_slot(tbl, slot);
  267. }
  268. static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
  269. {
  270. struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
  271. if (!IS_ERR(slot)) {
  272. bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
  273. if (ret)
  274. return ret;
  275. nfs4_free_slot(tbl, slot);
  276. }
  277. return false;
  278. }
  279. void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
  280. {
  281. for (;;) {
  282. if (!nfs41_try_wake_next_slot_table_entry(tbl))
  283. break;
  284. }
  285. }
  286. #if defined(CONFIG_NFS_V4_1)
  287. static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
  288. u32 target_highest_slotid)
  289. {
  290. u32 max_slotid;
  291. max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
  292. if (max_slotid > tbl->server_highest_slotid)
  293. max_slotid = tbl->server_highest_slotid;
  294. if (max_slotid > tbl->target_highest_slotid)
  295. max_slotid = tbl->target_highest_slotid;
  296. tbl->max_slotid = max_slotid;
  297. nfs41_wake_slot_table(tbl);
  298. }
  299. /* Update the client's idea of target_highest_slotid */
  300. static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
  301. u32 target_highest_slotid)
  302. {
  303. if (tbl->target_highest_slotid == target_highest_slotid)
  304. return;
  305. tbl->target_highest_slotid = target_highest_slotid;
  306. tbl->generation++;
  307. }
  308. void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
  309. u32 target_highest_slotid)
  310. {
  311. spin_lock(&tbl->slot_tbl_lock);
  312. nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
  313. tbl->d_target_highest_slotid = 0;
  314. tbl->d2_target_highest_slotid = 0;
  315. nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
  316. spin_unlock(&tbl->slot_tbl_lock);
  317. }
  318. static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
  319. u32 highest_slotid)
  320. {
  321. if (tbl->server_highest_slotid == highest_slotid)
  322. return;
  323. if (tbl->highest_used_slotid > highest_slotid)
  324. return;
  325. /* Deallocate slots */
  326. nfs4_shrink_slot_table(tbl, highest_slotid + 1);
  327. tbl->server_highest_slotid = highest_slotid;
  328. }
  329. static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
  330. {
  331. s1 -= s2;
  332. if (s1 == 0)
  333. return 0;
  334. if (s1 < 0)
  335. return (s1 - 1) >> 1;
  336. return (s1 + 1) >> 1;
  337. }
  338. static int nfs41_sign_s32(s32 s1)
  339. {
  340. if (s1 > 0)
  341. return 1;
  342. if (s1 < 0)
  343. return -1;
  344. return 0;
  345. }
  346. static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
  347. {
  348. if (!s1 || !s2)
  349. return true;
  350. return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
  351. }
  352. /* Try to eliminate outliers by checking for sharp changes in the
  353. * derivatives and second derivatives
  354. */
  355. static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
  356. u32 new_target)
  357. {
  358. s32 d_target, d2_target;
  359. bool ret = true;
  360. d_target = nfs41_derivative_target_slotid(new_target,
  361. tbl->target_highest_slotid);
  362. d2_target = nfs41_derivative_target_slotid(d_target,
  363. tbl->d_target_highest_slotid);
  364. /* Is first derivative same sign? */
  365. if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
  366. ret = false;
  367. /* Is second derivative same sign? */
  368. if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
  369. ret = false;
  370. tbl->d_target_highest_slotid = d_target;
  371. tbl->d2_target_highest_slotid = d2_target;
  372. return ret;
  373. }
  374. void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
  375. struct nfs4_slot *slot,
  376. struct nfs4_sequence_res *res)
  377. {
  378. spin_lock(&tbl->slot_tbl_lock);
  379. if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
  380. nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
  381. if (tbl->generation == slot->generation)
  382. nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
  383. nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
  384. spin_unlock(&tbl->slot_tbl_lock);
  385. }
  386. static void nfs4_release_session_slot_tables(struct nfs4_session *session)
  387. {
  388. nfs4_release_slot_table(&session->fc_slot_table);
  389. nfs4_release_slot_table(&session->bc_slot_table);
  390. }
  391. /*
  392. * Initialize or reset the forechannel and backchannel tables
  393. */
  394. int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
  395. {
  396. struct nfs4_slot_table *tbl;
  397. int status;
  398. dprintk("--> %s\n", __func__);
  399. /* Fore channel */
  400. tbl = &ses->fc_slot_table;
  401. tbl->session = ses;
  402. status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
  403. if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */
  404. return status;
  405. /* Back channel */
  406. tbl = &ses->bc_slot_table;
  407. tbl->session = ses;
  408. status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
  409. if (status && tbl->slots == NULL)
  410. /* Fore and back channel share a connection so get
  411. * both slot tables or neither */
  412. nfs4_release_session_slot_tables(ses);
  413. return status;
  414. }
  415. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  416. {
  417. struct nfs4_session *session;
  418. session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  419. if (!session)
  420. return NULL;
  421. nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
  422. nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
  423. session->session_state = 1<<NFS4_SESSION_INITING;
  424. session->clp = clp;
  425. return session;
  426. }
  427. static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
  428. {
  429. nfs4_shutdown_slot_table(&session->fc_slot_table);
  430. nfs4_shutdown_slot_table(&session->bc_slot_table);
  431. }
  432. void nfs4_destroy_session(struct nfs4_session *session)
  433. {
  434. struct rpc_xprt *xprt;
  435. struct rpc_cred *cred;
  436. cred = nfs4_get_clid_cred(session->clp);
  437. nfs4_proc_destroy_session(session, cred);
  438. if (cred)
  439. put_rpccred(cred);
  440. rcu_read_lock();
  441. xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
  442. rcu_read_unlock();
  443. dprintk("%s Destroy backchannel for xprt %p\n",
  444. __func__, xprt);
  445. xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
  446. nfs4_destroy_session_slot_tables(session);
  447. kfree(session);
  448. }
  449. /*
  450. * With sessions, the client is not marked ready until after a
  451. * successful EXCHANGE_ID and CREATE_SESSION.
  452. *
  453. * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
  454. * other versions of NFS can be tried.
  455. */
  456. static int nfs41_check_session_ready(struct nfs_client *clp)
  457. {
  458. int ret;
  459. if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
  460. ret = nfs4_client_recover_expired_lease(clp);
  461. if (ret)
  462. return ret;
  463. }
  464. if (clp->cl_cons_state < NFS_CS_READY)
  465. return -EPROTONOSUPPORT;
  466. smp_rmb();
  467. return 0;
  468. }
  469. int nfs4_init_session(struct nfs_client *clp)
  470. {
  471. if (!nfs4_has_session(clp))
  472. return 0;
  473. clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
  474. return nfs41_check_session_ready(clp);
  475. }
  476. int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
  477. {
  478. struct nfs4_session *session = clp->cl_session;
  479. int ret;
  480. spin_lock(&clp->cl_lock);
  481. if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
  482. /*
  483. * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
  484. * DS lease to be equal to the MDS lease.
  485. */
  486. clp->cl_lease_time = lease_time;
  487. clp->cl_last_renewal = jiffies;
  488. }
  489. spin_unlock(&clp->cl_lock);
  490. ret = nfs41_check_session_ready(clp);
  491. if (ret)
  492. return ret;
  493. /* Test for the DS role */
  494. if (!is_ds_client(clp))
  495. return -ENODEV;
  496. return 0;
  497. }
  498. EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
  499. #endif /* defined(CONFIG_NFS_V4_1) */