nfs4session.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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->max_slotid = server_highest_slotid;
  164. }
  165. /*
  166. * (re)Initialise a slot table
  167. */
  168. static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
  169. u32 max_reqs, u32 ivalue)
  170. {
  171. int ret;
  172. dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
  173. max_reqs, tbl->max_slots);
  174. if (max_reqs > NFS4_MAX_SLOT_TABLE)
  175. max_reqs = NFS4_MAX_SLOT_TABLE;
  176. ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
  177. if (ret)
  178. goto out;
  179. spin_lock(&tbl->slot_tbl_lock);
  180. nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
  181. spin_unlock(&tbl->slot_tbl_lock);
  182. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  183. tbl, tbl->slots, tbl->max_slots);
  184. out:
  185. dprintk("<-- %s: return %d\n", __func__, ret);
  186. return ret;
  187. }
  188. /* Destroy the slot table */
  189. static void nfs4_destroy_slot_tables(struct nfs4_session *session)
  190. {
  191. nfs4_shrink_slot_table(&session->fc_slot_table, 0);
  192. nfs4_shrink_slot_table(&session->bc_slot_table, 0);
  193. }
  194. /* Update the client's idea of target_highest_slotid */
  195. static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
  196. u32 target_highest_slotid)
  197. {
  198. unsigned int max_slotid, i;
  199. if (tbl->target_highest_slotid == target_highest_slotid)
  200. return;
  201. tbl->target_highest_slotid = target_highest_slotid;
  202. tbl->generation++;
  203. max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
  204. for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
  205. rpc_wake_up_next(&tbl->slot_tbl_waitq);
  206. tbl->max_slotid = max_slotid;
  207. }
  208. void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
  209. u32 target_highest_slotid)
  210. {
  211. spin_lock(&tbl->slot_tbl_lock);
  212. nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
  213. spin_unlock(&tbl->slot_tbl_lock);
  214. }
  215. static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
  216. u32 highest_slotid)
  217. {
  218. if (tbl->server_highest_slotid == highest_slotid)
  219. return;
  220. if (tbl->highest_used_slotid > highest_slotid)
  221. return;
  222. /* Deallocate slots */
  223. nfs4_shrink_slot_table(tbl, highest_slotid + 1);
  224. tbl->server_highest_slotid = highest_slotid;
  225. }
  226. void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
  227. struct nfs4_slot *slot,
  228. struct nfs4_sequence_res *res)
  229. {
  230. spin_lock(&tbl->slot_tbl_lock);
  231. if (tbl->generation != slot->generation)
  232. goto out;
  233. nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
  234. nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
  235. out:
  236. spin_unlock(&tbl->slot_tbl_lock);
  237. }
  238. /*
  239. * Initialize or reset the forechannel and backchannel tables
  240. */
  241. int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
  242. {
  243. struct nfs4_slot_table *tbl;
  244. int status;
  245. dprintk("--> %s\n", __func__);
  246. /* Fore channel */
  247. tbl = &ses->fc_slot_table;
  248. tbl->session = ses;
  249. status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
  250. if (status) /* -ENOMEM */
  251. return status;
  252. /* Back channel */
  253. tbl = &ses->bc_slot_table;
  254. tbl->session = ses;
  255. status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
  256. if (status && tbl->slots == NULL)
  257. /* Fore and back channel share a connection so get
  258. * both slot tables or neither */
  259. nfs4_destroy_slot_tables(ses);
  260. return status;
  261. }
  262. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  263. {
  264. struct nfs4_session *session;
  265. struct nfs4_slot_table *tbl;
  266. session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  267. if (!session)
  268. return NULL;
  269. tbl = &session->fc_slot_table;
  270. tbl->highest_used_slotid = NFS4_NO_SLOT;
  271. spin_lock_init(&tbl->slot_tbl_lock);
  272. rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
  273. init_completion(&tbl->complete);
  274. tbl = &session->bc_slot_table;
  275. tbl->highest_used_slotid = NFS4_NO_SLOT;
  276. spin_lock_init(&tbl->slot_tbl_lock);
  277. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
  278. init_completion(&tbl->complete);
  279. session->session_state = 1<<NFS4_SESSION_INITING;
  280. session->clp = clp;
  281. return session;
  282. }
  283. void nfs4_destroy_session(struct nfs4_session *session)
  284. {
  285. struct rpc_xprt *xprt;
  286. struct rpc_cred *cred;
  287. cred = nfs4_get_exchange_id_cred(session->clp);
  288. nfs4_proc_destroy_session(session, cred);
  289. if (cred)
  290. put_rpccred(cred);
  291. rcu_read_lock();
  292. xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
  293. rcu_read_unlock();
  294. dprintk("%s Destroy backchannel for xprt %p\n",
  295. __func__, xprt);
  296. xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
  297. nfs4_destroy_slot_tables(session);
  298. kfree(session);
  299. }
  300. /*
  301. * With sessions, the client is not marked ready until after a
  302. * successful EXCHANGE_ID and CREATE_SESSION.
  303. *
  304. * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
  305. * other versions of NFS can be tried.
  306. */
  307. static int nfs41_check_session_ready(struct nfs_client *clp)
  308. {
  309. int ret;
  310. if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
  311. ret = nfs4_client_recover_expired_lease(clp);
  312. if (ret)
  313. return ret;
  314. }
  315. if (clp->cl_cons_state < NFS_CS_READY)
  316. return -EPROTONOSUPPORT;
  317. smp_rmb();
  318. return 0;
  319. }
  320. int nfs4_init_session(struct nfs_server *server)
  321. {
  322. struct nfs_client *clp = server->nfs_client;
  323. struct nfs4_session *session;
  324. unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
  325. unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
  326. if (!nfs4_has_session(clp))
  327. return 0;
  328. if (server->rsize != 0)
  329. target_max_resp_sz = server->rsize;
  330. target_max_resp_sz += nfs41_maxread_overhead;
  331. if (server->wsize != 0)
  332. target_max_rqst_sz = server->wsize;
  333. target_max_rqst_sz += nfs41_maxwrite_overhead;
  334. session = clp->cl_session;
  335. spin_lock(&clp->cl_lock);
  336. if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
  337. /* Initialise targets and channel attributes */
  338. session->fc_target_max_rqst_sz = target_max_rqst_sz;
  339. session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
  340. session->fc_target_max_resp_sz = target_max_resp_sz;
  341. session->fc_attrs.max_resp_sz = target_max_resp_sz;
  342. } else {
  343. /* Just adjust the targets */
  344. if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
  345. session->fc_target_max_rqst_sz = target_max_rqst_sz;
  346. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  347. }
  348. if (target_max_resp_sz > session->fc_target_max_resp_sz) {
  349. session->fc_target_max_resp_sz = target_max_resp_sz;
  350. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  351. }
  352. }
  353. spin_unlock(&clp->cl_lock);
  354. if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  355. nfs4_schedule_lease_recovery(clp);
  356. return nfs41_check_session_ready(clp);
  357. }
  358. int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
  359. {
  360. struct nfs4_session *session = clp->cl_session;
  361. int ret;
  362. spin_lock(&clp->cl_lock);
  363. if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
  364. /*
  365. * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
  366. * DS lease to be equal to the MDS lease.
  367. */
  368. clp->cl_lease_time = lease_time;
  369. clp->cl_last_renewal = jiffies;
  370. }
  371. spin_unlock(&clp->cl_lock);
  372. ret = nfs41_check_session_ready(clp);
  373. if (ret)
  374. return ret;
  375. /* Test for the DS role */
  376. if (!is_ds_client(clp))
  377. return -ENODEV;
  378. return 0;
  379. }
  380. EXPORT_SYMBOL_GPL(nfs4_init_ds_session);