/******************************************************************************12(c) 2007 Network Appliance, Inc. All Rights Reserved.3(c) 2009 NetApp. All Rights Reserved.45NetApp provides this source code under the GPL v2 License.6The GPL v2 license is available at7http://opensource.org/licenses/gpl-license.php.89THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS10"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT11LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR12A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR13CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,14EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,15PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR16PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF17LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING18NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS19SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2021******************************************************************************/2223#include <linux/tcp.h>24#include <linux/slab.h>25#include <linux/sunrpc/xprt.h>2627#ifdef RPC_DEBUG28#define RPCDBG_FACILITY RPCDBG_TRANS29#endif3031#if defined(CONFIG_NFS_V4_1)3233/*34* Helper routines that track the number of preallocation elements35* on the transport.36*/37static inline int xprt_need_to_requeue(struct rpc_xprt *xprt)38{39return xprt->bc_alloc_count > 0;40}4142static inline void xprt_inc_alloc_count(struct rpc_xprt *xprt, unsigned int n)43{44xprt->bc_alloc_count += n;45}4647static inline int xprt_dec_alloc_count(struct rpc_xprt *xprt, unsigned int n)48{49return xprt->bc_alloc_count -= n;50}5152/*53* Free the preallocated rpc_rqst structure and the memory54* buffers hanging off of it.55*/56static void xprt_free_allocation(struct rpc_rqst *req)57{58struct xdr_buf *xbufp;5960dprintk("RPC: free allocations for req= %p\n", req);61BUG_ON(test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));62xbufp = &req->rq_private_buf;63free_page((unsigned long)xbufp->head[0].iov_base);64xbufp = &req->rq_snd_buf;65free_page((unsigned long)xbufp->head[0].iov_base);66list_del(&req->rq_bc_pa_list);67kfree(req);68}6970/*71* Preallocate up to min_reqs structures and related buffers for use72* by the backchannel. This function can be called multiple times73* when creating new sessions that use the same rpc_xprt. The74* preallocated buffers are added to the pool of resources used by75* the rpc_xprt. Anyone of these resources may be used used by an76* incoming callback request. It's up to the higher levels in the77* stack to enforce that the maximum number of session slots is not78* being exceeded.79*80* Some callback arguments can be large. For example, a pNFS server81* using multiple deviceids. The list can be unbound, but the client82* has the ability to tell the server the maximum size of the callback83* requests. Each deviceID is 16 bytes, so allocate one page84* for the arguments to have enough room to receive a number of these85* deviceIDs. The NFS client indicates to the pNFS server that its86* callback requests can be up to 4096 bytes in size.87*/88int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)89{90struct page *page_rcv = NULL, *page_snd = NULL;91struct xdr_buf *xbufp = NULL;92struct rpc_rqst *req, *tmp;93struct list_head tmp_list;94int i;9596dprintk("RPC: setup backchannel transport\n");9798/*99* We use a temporary list to keep track of the preallocated100* buffers. Once we're done building the list we splice it101* into the backchannel preallocation list off of the rpc_xprt102* struct. This helps minimize the amount of time the list103* lock is held on the rpc_xprt struct. It also makes cleanup104* easier in case of memory allocation errors.105*/106INIT_LIST_HEAD(&tmp_list);107for (i = 0; i < min_reqs; i++) {108/* Pre-allocate one backchannel rpc_rqst */109req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);110if (req == NULL) {111printk(KERN_ERR "Failed to create bc rpc_rqst\n");112goto out_free;113}114115/* Add the allocated buffer to the tmp list */116dprintk("RPC: adding req= %p\n", req);117list_add(&req->rq_bc_pa_list, &tmp_list);118119req->rq_xprt = xprt;120INIT_LIST_HEAD(&req->rq_list);121INIT_LIST_HEAD(&req->rq_bc_list);122123/* Preallocate one XDR receive buffer */124page_rcv = alloc_page(GFP_KERNEL);125if (page_rcv == NULL) {126printk(KERN_ERR "Failed to create bc receive xbuf\n");127goto out_free;128}129xbufp = &req->rq_rcv_buf;130xbufp->head[0].iov_base = page_address(page_rcv);131xbufp->head[0].iov_len = PAGE_SIZE;132xbufp->tail[0].iov_base = NULL;133xbufp->tail[0].iov_len = 0;134xbufp->page_len = 0;135xbufp->len = PAGE_SIZE;136xbufp->buflen = PAGE_SIZE;137138/* Preallocate one XDR send buffer */139page_snd = alloc_page(GFP_KERNEL);140if (page_snd == NULL) {141printk(KERN_ERR "Failed to create bc snd xbuf\n");142goto out_free;143}144145xbufp = &req->rq_snd_buf;146xbufp->head[0].iov_base = page_address(page_snd);147xbufp->head[0].iov_len = 0;148xbufp->tail[0].iov_base = NULL;149xbufp->tail[0].iov_len = 0;150xbufp->page_len = 0;151xbufp->len = 0;152xbufp->buflen = PAGE_SIZE;153}154155/*156* Add the temporary list to the backchannel preallocation list157*/158spin_lock_bh(&xprt->bc_pa_lock);159list_splice(&tmp_list, &xprt->bc_pa_list);160xprt_inc_alloc_count(xprt, min_reqs);161spin_unlock_bh(&xprt->bc_pa_lock);162163dprintk("RPC: setup backchannel transport done\n");164return 0;165166out_free:167/*168* Memory allocation failed, free the temporary list169*/170list_for_each_entry_safe(req, tmp, &tmp_list, rq_bc_pa_list)171xprt_free_allocation(req);172173dprintk("RPC: setup backchannel transport failed\n");174return -1;175}176EXPORT_SYMBOL(xprt_setup_backchannel);177178/*179* Destroys the backchannel preallocated structures.180* Since these structures may have been allocated by multiple calls181* to xprt_setup_backchannel, we only destroy up to the maximum number182* of reqs specified by the caller.183* @xprt: the transport holding the preallocated strucures184* @max_reqs the maximum number of preallocated structures to destroy185*/186void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)187{188struct rpc_rqst *req = NULL, *tmp = NULL;189190dprintk("RPC: destroy backchannel transport\n");191192BUG_ON(max_reqs == 0);193spin_lock_bh(&xprt->bc_pa_lock);194xprt_dec_alloc_count(xprt, max_reqs);195list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {196dprintk("RPC: req=%p\n", req);197xprt_free_allocation(req);198if (--max_reqs == 0)199break;200}201spin_unlock_bh(&xprt->bc_pa_lock);202203dprintk("RPC: backchannel list empty= %s\n",204list_empty(&xprt->bc_pa_list) ? "true" : "false");205}206EXPORT_SYMBOL(xprt_destroy_backchannel);207208/*209* One or more rpc_rqst structure have been preallocated during the210* backchannel setup. Buffer space for the send and private XDR buffers211* has been preallocated as well. Use xprt_alloc_bc_request to allocate212* to this request. Use xprt_free_bc_request to return it.213*214* We know that we're called in soft interrupt context, grab the spin_lock215* since there is no need to grab the bottom half spin_lock.216*217* Return an available rpc_rqst, otherwise NULL if non are available.218*/219struct rpc_rqst *xprt_alloc_bc_request(struct rpc_xprt *xprt)220{221struct rpc_rqst *req;222223dprintk("RPC: allocate a backchannel request\n");224spin_lock(&xprt->bc_pa_lock);225if (!list_empty(&xprt->bc_pa_list)) {226req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,227rq_bc_pa_list);228list_del(&req->rq_bc_pa_list);229} else {230req = NULL;231}232spin_unlock(&xprt->bc_pa_lock);233234if (req != NULL) {235set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);236req->rq_reply_bytes_recvd = 0;237req->rq_bytes_sent = 0;238memcpy(&req->rq_private_buf, &req->rq_rcv_buf,239sizeof(req->rq_private_buf));240}241dprintk("RPC: backchannel req=%p\n", req);242return req;243}244245/*246* Return the preallocated rpc_rqst structure and XDR buffers247* associated with this rpc_task.248*/249void xprt_free_bc_request(struct rpc_rqst *req)250{251struct rpc_xprt *xprt = req->rq_xprt;252253dprintk("RPC: free backchannel req=%p\n", req);254255smp_mb__before_clear_bit();256BUG_ON(!test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));257clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);258smp_mb__after_clear_bit();259260if (!xprt_need_to_requeue(xprt)) {261/*262* The last remaining session was destroyed while this263* entry was in use. Free the entry and don't attempt264* to add back to the list because there is no need to265* have anymore preallocated entries.266*/267dprintk("RPC: Last session removed req=%p\n", req);268xprt_free_allocation(req);269return;270}271272/*273* Return it to the list of preallocations so that it274* may be reused by a new callback request.275*/276spin_lock_bh(&xprt->bc_pa_lock);277list_add(&req->rq_bc_pa_list, &xprt->bc_pa_list);278spin_unlock_bh(&xprt->bc_pa_lock);279}280281#endif /* CONFIG_NFS_V4_1 */282283284