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