nfs4session.c 14 KB

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