nfs4session.c 13 KB

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