backchannel_rqst.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /******************************************************************************
  2. (c) 2007 Network Appliance, Inc. All Rights Reserved.
  3. (c) 2009 NetApp. All Rights Reserved.
  4. NetApp provides this source code under the GPL v2 License.
  5. The GPL v2 license is available at
  6. http://opensource.org/licenses/gpl-license.php.
  7. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  10. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  14. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  15. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  16. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  17. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. ******************************************************************************/
  19. #include <linux/tcp.h>
  20. #include <linux/slab.h>
  21. #include <linux/sunrpc/xprt.h>
  22. #include <linux/export.h>
  23. #include <linux/sunrpc/bc_xprt.h>
  24. #ifdef RPC_DEBUG
  25. #define RPCDBG_FACILITY RPCDBG_TRANS
  26. #endif
  27. /*
  28. * Helper routines that track the number of preallocation elements
  29. * on the transport.
  30. */
  31. static inline int xprt_need_to_requeue(struct rpc_xprt *xprt)
  32. {
  33. return xprt->bc_alloc_count > 0;
  34. }
  35. static inline void xprt_inc_alloc_count(struct rpc_xprt *xprt, unsigned int n)
  36. {
  37. xprt->bc_alloc_count += n;
  38. }
  39. static inline int xprt_dec_alloc_count(struct rpc_xprt *xprt, unsigned int n)
  40. {
  41. return xprt->bc_alloc_count -= n;
  42. }
  43. /*
  44. * Free the preallocated rpc_rqst structure and the memory
  45. * buffers hanging off of it.
  46. */
  47. static void xprt_free_allocation(struct rpc_rqst *req)
  48. {
  49. struct xdr_buf *xbufp;
  50. dprintk("RPC: free allocations for req= %p\n", req);
  51. BUG_ON(test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));
  52. xbufp = &req->rq_rcv_buf;
  53. free_page((unsigned long)xbufp->head[0].iov_base);
  54. xbufp = &req->rq_snd_buf;
  55. free_page((unsigned long)xbufp->head[0].iov_base);
  56. list_del(&req->rq_bc_pa_list);
  57. kfree(req);
  58. }
  59. /*
  60. * Preallocate up to min_reqs structures and related buffers for use
  61. * by the backchannel. This function can be called multiple times
  62. * when creating new sessions that use the same rpc_xprt. The
  63. * preallocated buffers are added to the pool of resources used by
  64. * the rpc_xprt. Anyone of these resources may be used used by an
  65. * incoming callback request. It's up to the higher levels in the
  66. * stack to enforce that the maximum number of session slots is not
  67. * being exceeded.
  68. *
  69. * Some callback arguments can be large. For example, a pNFS server
  70. * using multiple deviceids. The list can be unbound, but the client
  71. * has the ability to tell the server the maximum size of the callback
  72. * requests. Each deviceID is 16 bytes, so allocate one page
  73. * for the arguments to have enough room to receive a number of these
  74. * deviceIDs. The NFS client indicates to the pNFS server that its
  75. * callback requests can be up to 4096 bytes in size.
  76. */
  77. int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)
  78. {
  79. struct page *page_rcv = NULL, *page_snd = NULL;
  80. struct xdr_buf *xbufp = NULL;
  81. struct rpc_rqst *req, *tmp;
  82. struct list_head tmp_list;
  83. int i;
  84. dprintk("RPC: setup backchannel transport\n");
  85. /*
  86. * We use a temporary list to keep track of the preallocated
  87. * buffers. Once we're done building the list we splice it
  88. * into the backchannel preallocation list off of the rpc_xprt
  89. * struct. This helps minimize the amount of time the list
  90. * lock is held on the rpc_xprt struct. It also makes cleanup
  91. * easier in case of memory allocation errors.
  92. */
  93. INIT_LIST_HEAD(&tmp_list);
  94. for (i = 0; i < min_reqs; i++) {
  95. /* Pre-allocate one backchannel rpc_rqst */
  96. req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
  97. if (req == NULL) {
  98. printk(KERN_ERR "Failed to create bc rpc_rqst\n");
  99. goto out_free;
  100. }
  101. /* Add the allocated buffer to the tmp list */
  102. dprintk("RPC: adding req= %p\n", req);
  103. list_add(&req->rq_bc_pa_list, &tmp_list);
  104. req->rq_xprt = xprt;
  105. INIT_LIST_HEAD(&req->rq_list);
  106. INIT_LIST_HEAD(&req->rq_bc_list);
  107. /* Preallocate one XDR receive buffer */
  108. page_rcv = alloc_page(GFP_KERNEL);
  109. if (page_rcv == NULL) {
  110. printk(KERN_ERR "Failed to create bc receive xbuf\n");
  111. goto out_free;
  112. }
  113. xbufp = &req->rq_rcv_buf;
  114. xbufp->head[0].iov_base = page_address(page_rcv);
  115. xbufp->head[0].iov_len = PAGE_SIZE;
  116. xbufp->tail[0].iov_base = NULL;
  117. xbufp->tail[0].iov_len = 0;
  118. xbufp->page_len = 0;
  119. xbufp->len = PAGE_SIZE;
  120. xbufp->buflen = PAGE_SIZE;
  121. /* Preallocate one XDR send buffer */
  122. page_snd = alloc_page(GFP_KERNEL);
  123. if (page_snd == NULL) {
  124. printk(KERN_ERR "Failed to create bc snd xbuf\n");
  125. goto out_free;
  126. }
  127. xbufp = &req->rq_snd_buf;
  128. xbufp->head[0].iov_base = page_address(page_snd);
  129. xbufp->head[0].iov_len = 0;
  130. xbufp->tail[0].iov_base = NULL;
  131. xbufp->tail[0].iov_len = 0;
  132. xbufp->page_len = 0;
  133. xbufp->len = 0;
  134. xbufp->buflen = PAGE_SIZE;
  135. }
  136. /*
  137. * Add the temporary list to the backchannel preallocation list
  138. */
  139. spin_lock_bh(&xprt->bc_pa_lock);
  140. list_splice(&tmp_list, &xprt->bc_pa_list);
  141. xprt_inc_alloc_count(xprt, min_reqs);
  142. spin_unlock_bh(&xprt->bc_pa_lock);
  143. dprintk("RPC: setup backchannel transport done\n");
  144. return 0;
  145. out_free:
  146. /*
  147. * Memory allocation failed, free the temporary list
  148. */
  149. list_for_each_entry_safe(req, tmp, &tmp_list, rq_bc_pa_list)
  150. xprt_free_allocation(req);
  151. dprintk("RPC: setup backchannel transport failed\n");
  152. return -1;
  153. }
  154. EXPORT_SYMBOL_GPL(xprt_setup_backchannel);
  155. /*
  156. * Destroys the backchannel preallocated structures.
  157. * Since these structures may have been allocated by multiple calls
  158. * to xprt_setup_backchannel, we only destroy up to the maximum number
  159. * of reqs specified by the caller.
  160. * @xprt: the transport holding the preallocated strucures
  161. * @max_reqs the maximum number of preallocated structures to destroy
  162. */
  163. void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)
  164. {
  165. struct rpc_rqst *req = NULL, *tmp = NULL;
  166. dprintk("RPC: destroy backchannel transport\n");
  167. BUG_ON(max_reqs == 0);
  168. spin_lock_bh(&xprt->bc_pa_lock);
  169. xprt_dec_alloc_count(xprt, max_reqs);
  170. list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  171. dprintk("RPC: req=%p\n", req);
  172. xprt_free_allocation(req);
  173. if (--max_reqs == 0)
  174. break;
  175. }
  176. spin_unlock_bh(&xprt->bc_pa_lock);
  177. dprintk("RPC: backchannel list empty= %s\n",
  178. list_empty(&xprt->bc_pa_list) ? "true" : "false");
  179. }
  180. EXPORT_SYMBOL_GPL(xprt_destroy_backchannel);
  181. /*
  182. * One or more rpc_rqst structure have been preallocated during the
  183. * backchannel setup. Buffer space for the send and private XDR buffers
  184. * has been preallocated as well. Use xprt_alloc_bc_request to allocate
  185. * to this request. Use xprt_free_bc_request to return it.
  186. *
  187. * We know that we're called in soft interrupt context, grab the spin_lock
  188. * since there is no need to grab the bottom half spin_lock.
  189. *
  190. * Return an available rpc_rqst, otherwise NULL if non are available.
  191. */
  192. struct rpc_rqst *xprt_alloc_bc_request(struct rpc_xprt *xprt)
  193. {
  194. struct rpc_rqst *req;
  195. dprintk("RPC: allocate a backchannel request\n");
  196. spin_lock(&xprt->bc_pa_lock);
  197. if (!list_empty(&xprt->bc_pa_list)) {
  198. req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,
  199. rq_bc_pa_list);
  200. list_del(&req->rq_bc_pa_list);
  201. } else {
  202. req = NULL;
  203. }
  204. spin_unlock(&xprt->bc_pa_lock);
  205. if (req != NULL) {
  206. set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  207. req->rq_reply_bytes_recvd = 0;
  208. req->rq_bytes_sent = 0;
  209. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  210. sizeof(req->rq_private_buf));
  211. }
  212. dprintk("RPC: backchannel req=%p\n", req);
  213. return req;
  214. }
  215. /*
  216. * Return the preallocated rpc_rqst structure and XDR buffers
  217. * associated with this rpc_task.
  218. */
  219. void xprt_free_bc_request(struct rpc_rqst *req)
  220. {
  221. struct rpc_xprt *xprt = req->rq_xprt;
  222. dprintk("RPC: free backchannel req=%p\n", req);
  223. smp_mb__before_clear_bit();
  224. BUG_ON(!test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));
  225. clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  226. smp_mb__after_clear_bit();
  227. if (!xprt_need_to_requeue(xprt)) {
  228. /*
  229. * The last remaining session was destroyed while this
  230. * entry was in use. Free the entry and don't attempt
  231. * to add back to the list because there is no need to
  232. * have anymore preallocated entries.
  233. */
  234. dprintk("RPC: Last session removed req=%p\n", req);
  235. xprt_free_allocation(req);
  236. return;
  237. }
  238. /*
  239. * Return it to the list of preallocations so that it
  240. * may be reused by a new callback request.
  241. */
  242. spin_lock_bh(&xprt->bc_pa_lock);
  243. list_add(&req->rq_bc_pa_list, &xprt->bc_pa_list);
  244. spin_unlock_bh(&xprt->bc_pa_lock);
  245. }