nfs4session.c 14 KB

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