nfs4session.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 resources attached to a slot table
  209. * @tbl: slot table to shut down
  210. *
  211. */
  212. void nfs4_release_slot_table(struct nfs4_slot_table *tbl)
  213. {
  214. nfs4_shrink_slot_table(tbl, 0);
  215. }
  216. /**
  217. * nfs4_setup_slot_table - prepare a stand-alone slot table for use
  218. * @tbl: slot table to set up
  219. * @max_reqs: maximum number of requests allowed
  220. * @queue: name to give RPC wait queue
  221. *
  222. * Returns zero on success, or a negative errno.
  223. */
  224. int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs,
  225. const char *queue)
  226. {
  227. nfs4_init_slot_table(tbl, queue);
  228. return nfs4_realloc_slot_table(tbl, max_reqs, 0);
  229. }
  230. static bool nfs41_assign_slot(struct rpc_task *task, void *pslot)
  231. {
  232. struct nfs4_sequence_args *args = task->tk_msg.rpc_argp;
  233. struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
  234. struct nfs4_slot *slot = pslot;
  235. struct nfs4_slot_table *tbl = slot->table;
  236. if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
  237. return false;
  238. slot->generation = tbl->generation;
  239. args->sa_slot = slot;
  240. res->sr_timestamp = jiffies;
  241. res->sr_slot = slot;
  242. res->sr_status_flags = 0;
  243. res->sr_status = 1;
  244. return true;
  245. }
  246. static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
  247. struct nfs4_slot *slot)
  248. {
  249. if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot))
  250. return true;
  251. return false;
  252. }
  253. bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
  254. struct nfs4_slot *slot)
  255. {
  256. if (slot->slot_nr > tbl->max_slotid)
  257. return false;
  258. return __nfs41_wake_and_assign_slot(tbl, slot);
  259. }
  260. static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl)
  261. {
  262. struct nfs4_slot *slot = nfs4_alloc_slot(tbl);
  263. if (!IS_ERR(slot)) {
  264. bool ret = __nfs41_wake_and_assign_slot(tbl, slot);
  265. if (ret)
  266. return ret;
  267. nfs4_free_slot(tbl, slot);
  268. }
  269. return false;
  270. }
  271. void nfs41_wake_slot_table(struct nfs4_slot_table *tbl)
  272. {
  273. for (;;) {
  274. if (!nfs41_try_wake_next_slot_table_entry(tbl))
  275. break;
  276. }
  277. }
  278. #if defined(CONFIG_NFS_V4_1)
  279. static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
  280. u32 target_highest_slotid)
  281. {
  282. u32 max_slotid;
  283. max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
  284. if (max_slotid > tbl->server_highest_slotid)
  285. max_slotid = tbl->server_highest_slotid;
  286. if (max_slotid > tbl->target_highest_slotid)
  287. max_slotid = tbl->target_highest_slotid;
  288. tbl->max_slotid = max_slotid;
  289. nfs41_wake_slot_table(tbl);
  290. }
  291. /* Update the client's idea of target_highest_slotid */
  292. static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
  293. u32 target_highest_slotid)
  294. {
  295. if (tbl->target_highest_slotid == target_highest_slotid)
  296. return;
  297. tbl->target_highest_slotid = target_highest_slotid;
  298. tbl->generation++;
  299. }
  300. void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
  301. u32 target_highest_slotid)
  302. {
  303. spin_lock(&tbl->slot_tbl_lock);
  304. nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
  305. tbl->d_target_highest_slotid = 0;
  306. tbl->d2_target_highest_slotid = 0;
  307. nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
  308. spin_unlock(&tbl->slot_tbl_lock);
  309. }
  310. static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
  311. u32 highest_slotid)
  312. {
  313. if (tbl->server_highest_slotid == highest_slotid)
  314. return;
  315. if (tbl->highest_used_slotid > highest_slotid)
  316. return;
  317. /* Deallocate slots */
  318. nfs4_shrink_slot_table(tbl, highest_slotid + 1);
  319. tbl->server_highest_slotid = highest_slotid;
  320. }
  321. static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
  322. {
  323. s1 -= s2;
  324. if (s1 == 0)
  325. return 0;
  326. if (s1 < 0)
  327. return (s1 - 1) >> 1;
  328. return (s1 + 1) >> 1;
  329. }
  330. static int nfs41_sign_s32(s32 s1)
  331. {
  332. if (s1 > 0)
  333. return 1;
  334. if (s1 < 0)
  335. return -1;
  336. return 0;
  337. }
  338. static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
  339. {
  340. if (!s1 || !s2)
  341. return true;
  342. return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
  343. }
  344. /* Try to eliminate outliers by checking for sharp changes in the
  345. * derivatives and second derivatives
  346. */
  347. static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
  348. u32 new_target)
  349. {
  350. s32 d_target, d2_target;
  351. bool ret = true;
  352. d_target = nfs41_derivative_target_slotid(new_target,
  353. tbl->target_highest_slotid);
  354. d2_target = nfs41_derivative_target_slotid(d_target,
  355. tbl->d_target_highest_slotid);
  356. /* Is first derivative same sign? */
  357. if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
  358. ret = false;
  359. /* Is second derivative same sign? */
  360. if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
  361. ret = false;
  362. tbl->d_target_highest_slotid = d_target;
  363. tbl->d2_target_highest_slotid = d2_target;
  364. return ret;
  365. }
  366. void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
  367. struct nfs4_slot *slot,
  368. struct nfs4_sequence_res *res)
  369. {
  370. spin_lock(&tbl->slot_tbl_lock);
  371. if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
  372. nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
  373. if (tbl->generation == slot->generation)
  374. nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
  375. nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
  376. spin_unlock(&tbl->slot_tbl_lock);
  377. }
  378. static void nfs4_destroy_session_slot_tables(struct nfs4_session *session)
  379. {
  380. nfs4_release_slot_table(&session->fc_slot_table);
  381. nfs4_release_slot_table(&session->bc_slot_table);
  382. }
  383. /*
  384. * Initialize or reset the forechannel and backchannel tables
  385. */
  386. int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
  387. {
  388. struct nfs4_slot_table *tbl;
  389. int status;
  390. dprintk("--> %s\n", __func__);
  391. /* Fore channel */
  392. tbl = &ses->fc_slot_table;
  393. tbl->session = ses;
  394. status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
  395. if (status) /* -ENOMEM */
  396. return status;
  397. /* Back channel */
  398. tbl = &ses->bc_slot_table;
  399. tbl->session = ses;
  400. status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
  401. if (status && tbl->slots == NULL)
  402. /* Fore and back channel share a connection so get
  403. * both slot tables or neither */
  404. nfs4_destroy_session_slot_tables(ses);
  405. return status;
  406. }
  407. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  408. {
  409. struct nfs4_session *session;
  410. session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  411. if (!session)
  412. return NULL;
  413. nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table");
  414. nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table");
  415. session->session_state = 1<<NFS4_SESSION_INITING;
  416. session->clp = clp;
  417. return session;
  418. }
  419. void nfs4_destroy_session(struct nfs4_session *session)
  420. {
  421. struct rpc_xprt *xprt;
  422. struct rpc_cred *cred;
  423. cred = nfs4_get_clid_cred(session->clp);
  424. nfs4_proc_destroy_session(session, cred);
  425. if (cred)
  426. put_rpccred(cred);
  427. rcu_read_lock();
  428. xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
  429. rcu_read_unlock();
  430. dprintk("%s Destroy backchannel for xprt %p\n",
  431. __func__, xprt);
  432. xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
  433. nfs4_destroy_session_slot_tables(session);
  434. kfree(session);
  435. }
  436. /*
  437. * With sessions, the client is not marked ready until after a
  438. * successful EXCHANGE_ID and CREATE_SESSION.
  439. *
  440. * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
  441. * other versions of NFS can be tried.
  442. */
  443. static int nfs41_check_session_ready(struct nfs_client *clp)
  444. {
  445. int ret;
  446. if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
  447. ret = nfs4_client_recover_expired_lease(clp);
  448. if (ret)
  449. return ret;
  450. }
  451. if (clp->cl_cons_state < NFS_CS_READY)
  452. return -EPROTONOSUPPORT;
  453. smp_rmb();
  454. return 0;
  455. }
  456. int nfs4_init_session(struct nfs_client *clp)
  457. {
  458. if (!nfs4_has_session(clp))
  459. return 0;
  460. clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state);
  461. return nfs41_check_session_ready(clp);
  462. }
  463. int 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. spin_lock(&clp->cl_lock);
  468. if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
  469. /*
  470. * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
  471. * DS lease to be equal to the MDS lease.
  472. */
  473. clp->cl_lease_time = lease_time;
  474. clp->cl_last_renewal = jiffies;
  475. }
  476. spin_unlock(&clp->cl_lock);
  477. ret = nfs41_check_session_ready(clp);
  478. if (ret)
  479. return ret;
  480. /* Test for the DS role */
  481. if (!is_ds_client(clp))
  482. return -ENODEV;
  483. return 0;
  484. }
  485. EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
  486. #endif /* defined(CONFIG_NFS_V4_1) */