nfs4session.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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->generation = tbl->generation;
  133. out:
  134. dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
  135. __func__, tbl->used_slots[0], tbl->highest_used_slotid,
  136. !IS_ERR(ret) ? ret->slot_nr : -1);
  137. return ret;
  138. }
  139. static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
  140. u32 max_reqs, u32 ivalue)
  141. {
  142. if (max_reqs <= tbl->max_slots)
  143. return 0;
  144. if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
  145. return 0;
  146. return -ENOMEM;
  147. }
  148. static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
  149. u32 server_highest_slotid,
  150. u32 ivalue)
  151. {
  152. struct nfs4_slot **p;
  153. nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
  154. p = &tbl->slots;
  155. while (*p) {
  156. (*p)->seq_nr = ivalue;
  157. (*p)->interrupted = 0;
  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->generation = tbl->generation;
  205. args->sa_slot = slot;
  206. res->sr_timestamp = jiffies;
  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. static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl,
  245. u32 target_highest_slotid)
  246. {
  247. u32 max_slotid;
  248. max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid);
  249. if (max_slotid > tbl->server_highest_slotid)
  250. max_slotid = tbl->server_highest_slotid;
  251. if (max_slotid > tbl->target_highest_slotid)
  252. max_slotid = tbl->target_highest_slotid;
  253. tbl->max_slotid = max_slotid;
  254. nfs41_wake_slot_table(tbl);
  255. }
  256. /* Update the client's idea of target_highest_slotid */
  257. static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
  258. u32 target_highest_slotid)
  259. {
  260. if (tbl->target_highest_slotid == target_highest_slotid)
  261. return;
  262. tbl->target_highest_slotid = target_highest_slotid;
  263. tbl->generation++;
  264. }
  265. void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
  266. u32 target_highest_slotid)
  267. {
  268. spin_lock(&tbl->slot_tbl_lock);
  269. nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
  270. tbl->d_target_highest_slotid = 0;
  271. tbl->d2_target_highest_slotid = 0;
  272. nfs41_set_max_slotid_locked(tbl, target_highest_slotid);
  273. spin_unlock(&tbl->slot_tbl_lock);
  274. }
  275. static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
  276. u32 highest_slotid)
  277. {
  278. if (tbl->server_highest_slotid == highest_slotid)
  279. return;
  280. if (tbl->highest_used_slotid > highest_slotid)
  281. return;
  282. /* Deallocate slots */
  283. nfs4_shrink_slot_table(tbl, highest_slotid + 1);
  284. tbl->server_highest_slotid = highest_slotid;
  285. }
  286. static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2)
  287. {
  288. s1 -= s2;
  289. if (s1 == 0)
  290. return 0;
  291. if (s1 < 0)
  292. return (s1 - 1) >> 1;
  293. return (s1 + 1) >> 1;
  294. }
  295. static int nfs41_sign_s32(s32 s1)
  296. {
  297. if (s1 > 0)
  298. return 1;
  299. if (s1 < 0)
  300. return -1;
  301. return 0;
  302. }
  303. static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2)
  304. {
  305. if (!s1 || !s2)
  306. return true;
  307. return nfs41_sign_s32(s1) == nfs41_sign_s32(s2);
  308. }
  309. /* Try to eliminate outliers by checking for sharp changes in the
  310. * derivatives and second derivatives
  311. */
  312. static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl,
  313. u32 new_target)
  314. {
  315. s32 d_target, d2_target;
  316. bool ret = true;
  317. d_target = nfs41_derivative_target_slotid(new_target,
  318. tbl->target_highest_slotid);
  319. d2_target = nfs41_derivative_target_slotid(d_target,
  320. tbl->d_target_highest_slotid);
  321. /* Is first derivative same sign? */
  322. if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid))
  323. ret = false;
  324. /* Is second derivative same sign? */
  325. if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid))
  326. ret = false;
  327. tbl->d_target_highest_slotid = d_target;
  328. tbl->d2_target_highest_slotid = d2_target;
  329. return ret;
  330. }
  331. void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
  332. struct nfs4_slot *slot,
  333. struct nfs4_sequence_res *res)
  334. {
  335. spin_lock(&tbl->slot_tbl_lock);
  336. if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid))
  337. nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
  338. if (tbl->generation == slot->generation)
  339. nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
  340. nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid);
  341. spin_unlock(&tbl->slot_tbl_lock);
  342. }
  343. /*
  344. * Initialize or reset the forechannel and backchannel tables
  345. */
  346. int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
  347. {
  348. struct nfs4_slot_table *tbl;
  349. int status;
  350. dprintk("--> %s\n", __func__);
  351. /* Fore channel */
  352. tbl = &ses->fc_slot_table;
  353. tbl->session = ses;
  354. status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
  355. if (status) /* -ENOMEM */
  356. return status;
  357. /* Back channel */
  358. tbl = &ses->bc_slot_table;
  359. tbl->session = ses;
  360. status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
  361. if (status && tbl->slots == NULL)
  362. /* Fore and back channel share a connection so get
  363. * both slot tables or neither */
  364. nfs4_destroy_slot_tables(ses);
  365. return status;
  366. }
  367. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  368. {
  369. struct nfs4_session *session;
  370. struct nfs4_slot_table *tbl;
  371. session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  372. if (!session)
  373. return NULL;
  374. tbl = &session->fc_slot_table;
  375. tbl->highest_used_slotid = NFS4_NO_SLOT;
  376. spin_lock_init(&tbl->slot_tbl_lock);
  377. rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
  378. init_completion(&tbl->complete);
  379. tbl = &session->bc_slot_table;
  380. tbl->highest_used_slotid = NFS4_NO_SLOT;
  381. spin_lock_init(&tbl->slot_tbl_lock);
  382. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
  383. init_completion(&tbl->complete);
  384. session->session_state = 1<<NFS4_SESSION_INITING;
  385. session->clp = clp;
  386. return session;
  387. }
  388. void nfs4_destroy_session(struct nfs4_session *session)
  389. {
  390. struct rpc_xprt *xprt;
  391. struct rpc_cred *cred;
  392. cred = nfs4_get_exchange_id_cred(session->clp);
  393. nfs4_proc_destroy_session(session, cred);
  394. if (cred)
  395. put_rpccred(cred);
  396. rcu_read_lock();
  397. xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
  398. rcu_read_unlock();
  399. dprintk("%s Destroy backchannel for xprt %p\n",
  400. __func__, xprt);
  401. xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
  402. nfs4_destroy_slot_tables(session);
  403. kfree(session);
  404. }
  405. /*
  406. * With sessions, the client is not marked ready until after a
  407. * successful EXCHANGE_ID and CREATE_SESSION.
  408. *
  409. * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
  410. * other versions of NFS can be tried.
  411. */
  412. static int nfs41_check_session_ready(struct nfs_client *clp)
  413. {
  414. int ret;
  415. if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
  416. ret = nfs4_client_recover_expired_lease(clp);
  417. if (ret)
  418. return ret;
  419. }
  420. if (clp->cl_cons_state < NFS_CS_READY)
  421. return -EPROTONOSUPPORT;
  422. smp_rmb();
  423. return 0;
  424. }
  425. int nfs4_init_session(struct nfs_server *server)
  426. {
  427. struct nfs_client *clp = server->nfs_client;
  428. struct nfs4_session *session;
  429. unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
  430. unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
  431. if (!nfs4_has_session(clp))
  432. return 0;
  433. if (server->rsize != 0)
  434. target_max_resp_sz = server->rsize;
  435. target_max_resp_sz += nfs41_maxread_overhead;
  436. if (server->wsize != 0)
  437. target_max_rqst_sz = server->wsize;
  438. target_max_rqst_sz += nfs41_maxwrite_overhead;
  439. session = clp->cl_session;
  440. spin_lock(&clp->cl_lock);
  441. if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
  442. /* Initialise targets and channel attributes */
  443. session->fc_target_max_rqst_sz = target_max_rqst_sz;
  444. session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
  445. session->fc_target_max_resp_sz = target_max_resp_sz;
  446. session->fc_attrs.max_resp_sz = target_max_resp_sz;
  447. } else {
  448. /* Just adjust the targets */
  449. if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
  450. session->fc_target_max_rqst_sz = target_max_rqst_sz;
  451. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  452. }
  453. if (target_max_resp_sz > session->fc_target_max_resp_sz) {
  454. session->fc_target_max_resp_sz = target_max_resp_sz;
  455. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  456. }
  457. }
  458. spin_unlock(&clp->cl_lock);
  459. if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  460. nfs4_schedule_lease_recovery(clp);
  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);