xref: /openbmc/linux/net/sunrpc/backchannel_rqst.c (revision 6622e3a7)
1fb7a0b9aSRicardo Labiaga // SPDX-License-Identifier: GPL-2.0-only
2fb7a0b9aSRicardo Labiaga /******************************************************************************
3fb7a0b9aSRicardo Labiaga 
4fb7a0b9aSRicardo Labiaga (c) 2007 Network Appliance, Inc.  All Rights Reserved.
5fb7a0b9aSRicardo Labiaga (c) 2009 NetApp.  All Rights Reserved.
6fb7a0b9aSRicardo Labiaga 
7fb7a0b9aSRicardo Labiaga 
80bdd4ceaSAlexander A. Klimov ******************************************************************************/
9fb7a0b9aSRicardo Labiaga 
10fb7a0b9aSRicardo Labiaga #include <linux/tcp.h>
11fb7a0b9aSRicardo Labiaga #include <linux/slab.h>
12fb7a0b9aSRicardo Labiaga #include <linux/sunrpc/xprt.h>
13fb7a0b9aSRicardo Labiaga #include <linux/export.h>
14fb7a0b9aSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
15fb7a0b9aSRicardo Labiaga 
16fb7a0b9aSRicardo Labiaga #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
17fb7a0b9aSRicardo Labiaga #define RPCDBG_FACILITY	RPCDBG_TRANS
18fb7a0b9aSRicardo Labiaga #endif
19fb7a0b9aSRicardo Labiaga 
20fb7a0b9aSRicardo Labiaga #define BC_MAX_SLOTS	64U
21fb7a0b9aSRicardo Labiaga 
xprt_bc_max_slots(struct rpc_xprt * xprt)22fb7a0b9aSRicardo Labiaga unsigned int xprt_bc_max_slots(struct rpc_xprt *xprt)
23fb7a0b9aSRicardo Labiaga {
24fb7a0b9aSRicardo Labiaga 	return BC_MAX_SLOTS;
255a0e3ad6STejun Heo }
26fb7a0b9aSRicardo Labiaga 
27bc3b2d7fSPaul Gortmaker /*
2809acfea5STrond Myklebust  * Helper routines that track the number of preallocation elements
29fb7a0b9aSRicardo Labiaga  * on the transport.
30f895b252SJeff Layton  */
xprt_need_to_requeue(struct rpc_xprt * xprt)31fb7a0b9aSRicardo Labiaga static inline int xprt_need_to_requeue(struct rpc_xprt *xprt)
32fb7a0b9aSRicardo Labiaga {
33fb7a0b9aSRicardo Labiaga 	return xprt->bc_alloc_count < xprt->bc_alloc_max;
347402a4feSTrond Myklebust }
357402a4feSTrond Myklebust 
367402a4feSTrond Myklebust /*
377402a4feSTrond Myklebust  * Free the preallocated rpc_rqst structure and the memory
387402a4feSTrond Myklebust  * buffers hanging off of it.
397402a4feSTrond Myklebust  */
xprt_free_allocation(struct rpc_rqst * req)407402a4feSTrond Myklebust static void xprt_free_allocation(struct rpc_rqst *req)
41fb7a0b9aSRicardo Labiaga {
42fb7a0b9aSRicardo Labiaga 	struct xdr_buf *xbufp;
43fb7a0b9aSRicardo Labiaga 
44fb7a0b9aSRicardo Labiaga 	dprintk("RPC:        free allocations for req= %p\n", req);
45fb7a0b9aSRicardo Labiaga 	WARN_ON_ONCE(test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));
46fb7a0b9aSRicardo Labiaga 	xbufp = &req->rq_rcv_buf;
477402a4feSTrond Myklebust 	free_page((unsigned long)xbufp->head[0].iov_base);
48fb7a0b9aSRicardo Labiaga 	xbufp = &req->rq_snd_buf;
49fb7a0b9aSRicardo Labiaga 	free_page((unsigned long)xbufp->head[0].iov_base);
50fb7a0b9aSRicardo Labiaga 	kfree(req);
51fb7a0b9aSRicardo Labiaga }
52fb7a0b9aSRicardo Labiaga 
xprt_bc_reinit_xdr_buf(struct xdr_buf * buf)53fb7a0b9aSRicardo Labiaga static void xprt_bc_reinit_xdr_buf(struct xdr_buf *buf)
54fb7a0b9aSRicardo Labiaga {
55fb7a0b9aSRicardo Labiaga 	buf->head[0].iov_len = PAGE_SIZE;
56fb7a0b9aSRicardo Labiaga 	buf->tail[0].iov_len = 0;
57fb7a0b9aSRicardo Labiaga 	buf->pages = NULL;
58fb7a0b9aSRicardo Labiaga 	buf->page_len = 0;
59f30dfbbaSWeston Andros Adamson 	buf->flags = 0;
6088de6af2STrond Myklebust 	buf->len = 0;
61fb7a0b9aSRicardo Labiaga 	buf->buflen = PAGE_SIZE;
62fb7a0b9aSRicardo Labiaga }
63fb7a0b9aSRicardo Labiaga 
xprt_alloc_xdr_buf(struct xdr_buf * buf,gfp_t gfp_flags)64fb7a0b9aSRicardo Labiaga static int xprt_alloc_xdr_buf(struct xdr_buf *buf, gfp_t gfp_flags)
65fb7a0b9aSRicardo Labiaga {
66fb7a0b9aSRicardo Labiaga 	struct page *page;
67*6622e3a7STrond Myklebust 	/* Preallocate one XDR receive buffer */
68*6622e3a7STrond Myklebust 	page = alloc_page(gfp_flags);
69*6622e3a7STrond Myklebust 	if (page == NULL)
70*6622e3a7STrond Myklebust 		return -ENOMEM;
71*6622e3a7STrond Myklebust 	xdr_buf_init(buf, page_address(page), PAGE_SIZE);
72*6622e3a7STrond Myklebust 	return 0;
73*6622e3a7STrond Myklebust }
74*6622e3a7STrond Myklebust 
xprt_alloc_bc_req(struct rpc_xprt * xprt)75*6622e3a7STrond Myklebust static struct rpc_rqst *xprt_alloc_bc_req(struct rpc_xprt *xprt)
76*6622e3a7STrond Myklebust {
77*6622e3a7STrond Myklebust 	gfp_t gfp_flags = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
781dddda86STrond Myklebust 	struct rpc_rqst *req;
791dddda86STrond Myklebust 
801dddda86STrond Myklebust 	/* Pre-allocate one backchannel rpc_rqst */
811dddda86STrond Myklebust 	req = kzalloc(sizeof(*req), gfp_flags);
821dddda86STrond Myklebust 	if (req == NULL)
831dddda86STrond Myklebust 		return NULL;
841dddda86STrond Myklebust 
85b9c5bc03SChuck Lever 	req->rq_xprt = xprt;
861dddda86STrond Myklebust 	INIT_LIST_HEAD(&req->rq_bc_list);
871dddda86STrond Myklebust 
881dddda86STrond Myklebust 	/* Preallocate one XDR receive buffer */
89b2648015STrond Myklebust 	if (xprt_alloc_xdr_buf(&req->rq_rcv_buf, gfp_flags) < 0) {
901dddda86STrond Myklebust 		printk(KERN_ERR "Failed to create bc receive xbuf\n");
91b2648015STrond Myklebust 		goto out_free;
921dddda86STrond Myklebust 	}
931dddda86STrond Myklebust 	req->rq_rcv_buf.len = PAGE_SIZE;
941dddda86STrond Myklebust 
951dddda86STrond Myklebust 	/* Preallocate one XDR send buffer */
961dddda86STrond Myklebust 	if (xprt_alloc_xdr_buf(&req->rq_snd_buf, gfp_flags) < 0) {
971dddda86STrond Myklebust 		printk(KERN_ERR "Failed to create bc snd xbuf\n");
981dddda86STrond Myklebust 		goto out_free;
991dddda86STrond Myklebust 	}
1001dddda86STrond Myklebust 	return req;
1011dddda86STrond Myklebust out_free:
1021dddda86STrond Myklebust 	xprt_free_allocation(req);
1031dddda86STrond Myklebust 	return NULL;
1041dddda86STrond Myklebust }
1051dddda86STrond Myklebust 
1061dddda86STrond Myklebust /*
1071dddda86STrond Myklebust  * Preallocate up to min_reqs structures and related buffers for use
1081dddda86STrond Myklebust  * by the backchannel.  This function can be called multiple times
1091dddda86STrond Myklebust  * when creating new sessions that use the same rpc_xprt.  The
1101dddda86STrond Myklebust  * preallocated buffers are added to the pool of resources used by
1111dddda86STrond Myklebust  * the rpc_xprt.  Any one of these resources may be used by an
1121dddda86STrond Myklebust  * incoming callback request.  It's up to the higher levels in the
1131dddda86STrond Myklebust  * stack to enforce that the maximum number of session slots is not
1141dddda86STrond Myklebust  * being exceeded.
1151dddda86STrond Myklebust  *
1161dddda86STrond Myklebust  * Some callback arguments can be large.  For example, a pNFS server
1171dddda86STrond Myklebust  * using multiple deviceids.  The list can be unbound, but the client
1181dddda86STrond Myklebust  * has the ability to tell the server the maximum size of the callback
1191dddda86STrond Myklebust  * requests.  Each deviceID is 16 bytes, so allocate one page
120fb7a0b9aSRicardo Labiaga  * for the arguments to have enough room to receive a number of these
121fb7a0b9aSRicardo Labiaga  * deviceIDs.  The NFS client indicates to the pNFS server that its
122fb7a0b9aSRicardo Labiaga  * callback requests can be up to 4096 bytes in size.
123fb7a0b9aSRicardo Labiaga  */
xprt_setup_backchannel(struct rpc_xprt * xprt,unsigned int min_reqs)124fb7a0b9aSRicardo Labiaga int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)
1251cc5213bSRandy Dunlap {
126fb7a0b9aSRicardo Labiaga 	if (!xprt->ops->bc_setup)
127fb7a0b9aSRicardo Labiaga 		return 0;
128fb7a0b9aSRicardo Labiaga 	return xprt->ops->bc_setup(xprt, min_reqs);
129fb7a0b9aSRicardo Labiaga }
130fb7a0b9aSRicardo Labiaga EXPORT_SYMBOL_GPL(xprt_setup_backchannel);
131fb7a0b9aSRicardo Labiaga 
xprt_setup_bc(struct rpc_xprt * xprt,unsigned int min_reqs)132fb7a0b9aSRicardo Labiaga int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs)
133fb7a0b9aSRicardo Labiaga {
134fb7a0b9aSRicardo Labiaga 	struct rpc_rqst *req;
135fb7a0b9aSRicardo Labiaga 	struct list_head tmp_list;
136fb7a0b9aSRicardo Labiaga 	int i;
137fb7a0b9aSRicardo Labiaga 
138fb7a0b9aSRicardo Labiaga 	dprintk("RPC:       setup backchannel transport\n");
139fb7a0b9aSRicardo Labiaga 
14042e5c3e2SChuck Lever 	if (min_reqs > BC_MAX_SLOTS)
14142e5c3e2SChuck Lever 		min_reqs = BC_MAX_SLOTS;
14242e5c3e2SChuck Lever 
14342e5c3e2SChuck Lever 	/*
14442e5c3e2SChuck Lever 	 * We use a temporary list to keep track of the preallocated
14542e5c3e2SChuck Lever 	 * buffers.  Once we're done building the list we splice it
14642e5c3e2SChuck Lever 	 * into the backchannel preallocation list off of the rpc_xprt
14742e5c3e2SChuck Lever 	 * struct.  This helps minimize the amount of time the list
1481dddda86STrond Myklebust 	 * lock is held on the rpc_xprt struct.  It also makes cleanup
149fb7a0b9aSRicardo Labiaga 	 * easier in case of memory allocation errors.
150fb7a0b9aSRicardo Labiaga 	 */
151fb7a0b9aSRicardo Labiaga 	INIT_LIST_HEAD(&tmp_list);
152fb7a0b9aSRicardo Labiaga 	for (i = 0; i < min_reqs; i++) {
153fb7a0b9aSRicardo Labiaga 		/* Pre-allocate one backchannel rpc_rqst */
1547402a4feSTrond Myklebust 		req = xprt_alloc_bc_req(xprt);
1557402a4feSTrond Myklebust 		if (req == NULL) {
1567402a4feSTrond Myklebust 			printk(KERN_ERR "Failed to create bc rpc_rqst\n");
157fb7a0b9aSRicardo Labiaga 			goto out_free;
158fb7a0b9aSRicardo Labiaga 		}
159fb7a0b9aSRicardo Labiaga 
160fb7a0b9aSRicardo Labiaga 		/* Add the allocated buffer to the tmp list */
161fb7a0b9aSRicardo Labiaga 		dprintk("RPC:       adding req= %p\n", req);
162fb7a0b9aSRicardo Labiaga 		list_add(&req->rq_bc_pa_list, &tmp_list);
163fb7a0b9aSRicardo Labiaga 	}
164fb7a0b9aSRicardo Labiaga 
165fb7a0b9aSRicardo Labiaga 	/*
166fb7a0b9aSRicardo Labiaga 	 * Add the temporary list to the backchannel preallocation list
167fb7a0b9aSRicardo Labiaga 	 */
168b2648015STrond Myklebust 	spin_lock(&xprt->bc_pa_lock);
169fb7a0b9aSRicardo Labiaga 	list_splice(&tmp_list, &xprt->bc_pa_list);
170fb7a0b9aSRicardo Labiaga 	xprt->bc_alloc_count += min_reqs;
171fb7a0b9aSRicardo Labiaga 	xprt->bc_alloc_max += min_reqs;
172fb7a0b9aSRicardo Labiaga 	atomic_add(min_reqs, &xprt->bc_slot_count);
173fb7a0b9aSRicardo Labiaga 	spin_unlock(&xprt->bc_pa_lock);
174fb7a0b9aSRicardo Labiaga 
175fb7a0b9aSRicardo Labiaga 	dprintk("RPC:       setup backchannel transport done\n");
176fb7a0b9aSRicardo Labiaga 	return 0;
177fb7a0b9aSRicardo Labiaga 
178fb7a0b9aSRicardo Labiaga out_free:
179fb7a0b9aSRicardo Labiaga 	/*
180fb7a0b9aSRicardo Labiaga 	 * Memory allocation failed, free the temporary list
181fb7a0b9aSRicardo Labiaga 	 */
182c89091c8STrond Myklebust 	while (!list_empty(&tmp_list)) {
183fb7a0b9aSRicardo Labiaga 		req = list_first_entry(&tmp_list,
1847402a4feSTrond Myklebust 				struct rpc_rqst,
1857402a4feSTrond Myklebust 				rq_bc_pa_list);
1867402a4feSTrond Myklebust 		list_del(&req->rq_bc_pa_list);
187c89091c8STrond Myklebust 		xprt_free_allocation(req);
188fb7a0b9aSRicardo Labiaga 	}
189fb7a0b9aSRicardo Labiaga 
190fb7a0b9aSRicardo Labiaga 	dprintk("RPC:       setup backchannel transport failed\n");
191fb7a0b9aSRicardo Labiaga 	return -ENOMEM;
192fb7a0b9aSRicardo Labiaga }
193fb7a0b9aSRicardo Labiaga 
194fb7a0b9aSRicardo Labiaga /**
195fb7a0b9aSRicardo Labiaga  * xprt_destroy_backchannel - Destroys the backchannel preallocated structures.
1961dddda86STrond Myklebust  * @xprt:	the transport holding the preallocated strucures
1971dddda86STrond Myklebust  * @max_reqs:	the maximum number of preallocated structures to destroy
1981dddda86STrond Myklebust  *
1991dddda86STrond Myklebust  * Since these structures may have been allocated by multiple calls
20062835679STrond Myklebust  * to xprt_setup_backchannel, we only destroy up to the maximum number
201fb7a0b9aSRicardo Labiaga  * of reqs specified by the caller.
20262835679STrond Myklebust  */
xprt_destroy_backchannel(struct rpc_xprt * xprt,unsigned int max_reqs)203fb7a0b9aSRicardo Labiaga void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)
204fb7a0b9aSRicardo Labiaga {
205d24bab93SWeston Andros Adamson 	if (xprt->ops->bc_destroy)
206fb7a0b9aSRicardo Labiaga 		xprt->ops->bc_destroy(xprt, max_reqs);
207fb7a0b9aSRicardo Labiaga }
2082c53040fSBen Hutchings EXPORT_SYMBOL_GPL(xprt_destroy_backchannel);
2092c53040fSBen Hutchings 
xprt_destroy_bc(struct rpc_xprt * xprt,unsigned int max_reqs)2102c53040fSBen Hutchings void xprt_destroy_bc(struct rpc_xprt *xprt, unsigned int max_reqs)
211acf0a39fSChuck Lever {
2122c53040fSBen Hutchings 	struct rpc_rqst *req = NULL, *tmp = NULL;
213fb7a0b9aSRicardo Labiaga 
214fb7a0b9aSRicardo Labiaga 	dprintk("RPC:        destroy backchannel transport\n");
215fb7a0b9aSRicardo Labiaga 
216fb7a0b9aSRicardo Labiaga 	if (max_reqs == 0)
217fb7a0b9aSRicardo Labiaga 		goto out;
218fb7a0b9aSRicardo Labiaga 
21942e5c3e2SChuck Lever 	spin_lock_bh(&xprt->bc_pa_lock);
22042e5c3e2SChuck Lever 	xprt->bc_alloc_max -= min(max_reqs, xprt->bc_alloc_max);
22142e5c3e2SChuck Lever 	list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
22242e5c3e2SChuck Lever 		dprintk("RPC:        req=%p\n", req);
22342e5c3e2SChuck Lever 		list_del(&req->rq_bc_pa_list);
22442e5c3e2SChuck Lever 		xprt_free_allocation(req);
22542e5c3e2SChuck Lever 		xprt->bc_alloc_count--;
226fb7a0b9aSRicardo Labiaga 		atomic_dec(&xprt->bc_slot_count);
227fb7a0b9aSRicardo Labiaga 		if (--max_reqs == 0)
228fb7a0b9aSRicardo Labiaga 			break;
229fb7a0b9aSRicardo Labiaga 	}
230c4ded8d9SWeston Andros Adamson 	spin_unlock_bh(&xprt->bc_pa_lock);
231c4ded8d9SWeston Andros Adamson 
232c4ded8d9SWeston Andros Adamson out:
233fb7a0b9aSRicardo Labiaga 	dprintk("RPC:        backchannel list empty= %s\n",
234669996adSTrond Myklebust 		list_empty(&xprt->bc_pa_list) ? "true" : "false");
235fb7a0b9aSRicardo Labiaga }
236fb7a0b9aSRicardo Labiaga 
xprt_get_bc_request(struct rpc_xprt * xprt,__be32 xid,struct rpc_rqst * new)23762835679STrond Myklebust static struct rpc_rqst *xprt_get_bc_request(struct rpc_xprt *xprt, __be32 xid,
238fb7a0b9aSRicardo Labiaga 		struct rpc_rqst *new)
2397402a4feSTrond Myklebust {
2407402a4feSTrond Myklebust 	struct rpc_rqst *req = NULL;
241fb7a0b9aSRicardo Labiaga 
242fb7a0b9aSRicardo Labiaga 	dprintk("RPC:       allocate a backchannel request\n");
243fb7a0b9aSRicardo Labiaga 	if (list_empty(&xprt->bc_pa_list)) {
244fb7a0b9aSRicardo Labiaga 		if (!new)
245fb7a0b9aSRicardo Labiaga 			goto not_found;
246c4ded8d9SWeston Andros Adamson 		if (atomic_read(&xprt->bc_slot_count) >= BC_MAX_SLOTS)
247fb7a0b9aSRicardo Labiaga 			goto not_found;
248fb7a0b9aSRicardo Labiaga 		list_add_tail(&new->rq_bc_pa_list, &xprt->bc_pa_list);
249fb7a0b9aSRicardo Labiaga 		xprt->bc_alloc_count++;
250fb7a0b9aSRicardo Labiaga 		atomic_inc(&xprt->bc_slot_count);
2510d1bf340STrond Myklebust 	}
2520d1bf340STrond Myklebust 	req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,
253fb7a0b9aSRicardo Labiaga 				rq_bc_pa_list);
2542ea24497STrond Myklebust 	req->rq_reply_bytes_recvd = 0;
255fb7a0b9aSRicardo Labiaga 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
256fb7a0b9aSRicardo Labiaga 			sizeof(req->rq_private_buf));
2570d2a970dSTrond Myklebust 	req->rq_xid = xid;
2580d1bf340STrond Myklebust 	req->rq_connect_cookie = xprt->connect_cookie;
2590d2a970dSTrond Myklebust 	dprintk("RPC:       backchannel req=%p\n", req);
2607402a4feSTrond Myklebust not_found:
2617402a4feSTrond Myklebust 	return req;
2620d1bf340STrond Myklebust }
26368514471STrond Myklebust 
2647402a4feSTrond Myklebust /*
2650d2a970dSTrond Myklebust  * Return the preallocated rpc_rqst structure and XDR buffers
266fb7a0b9aSRicardo Labiaga  * associated with this rpc_task.
267fb7a0b9aSRicardo Labiaga  */
xprt_free_bc_request(struct rpc_rqst * req)268dd2b63d0SRicardo Labiaga void xprt_free_bc_request(struct rpc_rqst *req)
269fb7a0b9aSRicardo Labiaga {
270fb7a0b9aSRicardo Labiaga 	struct rpc_xprt *xprt = req->rq_xprt;
2712ea24497STrond Myklebust 
2722ea24497STrond Myklebust 	xprt->ops->bc_free_rqst(req);
273fb7a0b9aSRicardo Labiaga }
2740d1bf340STrond Myklebust 
xprt_free_bc_rqst(struct rpc_rqst * req)275fb7a0b9aSRicardo Labiaga void xprt_free_bc_rqst(struct rpc_rqst *req)
276fb7a0b9aSRicardo Labiaga {
277fb7a0b9aSRicardo Labiaga 	struct rpc_xprt *xprt = req->rq_xprt;
278fb7a0b9aSRicardo Labiaga 
279fb7a0b9aSRicardo Labiaga 	dprintk("RPC:       free backchannel req=%p\n", req);
280fb7a0b9aSRicardo Labiaga 
281fb7a0b9aSRicardo Labiaga 	req->rq_connect_cookie = xprt->connect_cookie - 1;
282fb7a0b9aSRicardo Labiaga 	smp_mb__before_atomic();
283fb7a0b9aSRicardo Labiaga 	clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
284fb7a0b9aSRicardo Labiaga 	smp_mb__after_atomic();
285fb7a0b9aSRicardo Labiaga 
28642e5c3e2SChuck Lever 	/*
28742e5c3e2SChuck Lever 	 * Return it to the list of preallocations so that it
28842e5c3e2SChuck Lever 	 * may be reused by a new callback request.
28942e5c3e2SChuck Lever 	 */
29042e5c3e2SChuck Lever 	spin_lock_bh(&xprt->bc_pa_lock);
29142e5c3e2SChuck Lever 	if (xprt_need_to_requeue(xprt)) {
29242e5c3e2SChuck Lever 		xprt_bc_reinit_xdr_buf(&req->rq_snd_buf);
293fb7a0b9aSRicardo Labiaga 		xprt_bc_reinit_xdr_buf(&req->rq_rcv_buf);
294fb7a0b9aSRicardo Labiaga 		req->rq_rcv_buf.len = PAGE_SIZE;
2952ea24497STrond Myklebust 		list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
2964e857c58SPeter Zijlstra 		xprt->bc_alloc_count++;
297fb7a0b9aSRicardo Labiaga 		atomic_inc(&xprt->bc_slot_count);
2984e857c58SPeter Zijlstra 		req = NULL;
299fb7a0b9aSRicardo Labiaga 	}
3000d2a970dSTrond Myklebust 	spin_unlock_bh(&xprt->bc_pa_lock);
3010d2a970dSTrond Myklebust 	if (req != NULL) {
3020d2a970dSTrond Myklebust 		/*
3030d2a970dSTrond Myklebust 		 * The last remaining session was destroyed while this
3040d2a970dSTrond Myklebust 		 * entry was in use.  Free the entry and don't attempt
3050d2a970dSTrond Myklebust 		 * to add back to the list because there is no need to
306*6622e3a7STrond Myklebust 		 * have anymore preallocated entries.
307*6622e3a7STrond Myklebust 		 */
308*6622e3a7STrond Myklebust 		dprintk("RPC:       Last session removed req=%p\n", req);
3090d2a970dSTrond Myklebust 		xprt_free_allocation(req);
3100d2a970dSTrond Myklebust 	}
3117402a4feSTrond Myklebust 	xprt_put(xprt);
3120d2a970dSTrond Myklebust }
3130d2a970dSTrond Myklebust 
3140d2a970dSTrond Myklebust /*
3150d2a970dSTrond Myklebust  * One or more rpc_rqst structure have been preallocated during the
316fb7a0b9aSRicardo Labiaga  * backchannel setup.  Buffer space for the send and private XDR buffers
317fb7a0b9aSRicardo Labiaga  * has been preallocated as well.  Use xprt_alloc_bc_request to allocate
318fb7a0b9aSRicardo Labiaga  * to this request.  Use xprt_free_bc_request to return it.
319fb7a0b9aSRicardo Labiaga  *
320fb7a0b9aSRicardo Labiaga  * We know that we're called in soft interrupt context, grab the spin_lock
321fb7a0b9aSRicardo Labiaga  * since there is no need to grab the bottom half spin_lock.
322fb7a0b9aSRicardo Labiaga  *
323fb7a0b9aSRicardo Labiaga  * Return an available rpc_rqst, otherwise NULL if non are available.
324fb7a0b9aSRicardo Labiaga  */
xprt_lookup_bc_request(struct rpc_xprt * xprt,__be32 xid)325875f0706STrond Myklebust struct rpc_rqst *xprt_lookup_bc_request(struct rpc_xprt *xprt, __be32 xid)
326fb7a0b9aSRicardo Labiaga {
327fb7a0b9aSRicardo Labiaga 	struct rpc_rqst *req, *new = NULL;
3282ea24497STrond Myklebust 
3292ea24497STrond Myklebust 	do {
3302ea24497STrond Myklebust 		spin_lock(&xprt->bc_pa_lock);
3312ea24497STrond Myklebust 		list_for_each_entry(req, &xprt->bc_pa_list, rq_bc_pa_list) {
3322ea24497STrond Myklebust 			if (req->rq_connect_cookie != xprt->connect_cookie)
3332ea24497STrond Myklebust 				continue;
3342ea24497STrond Myklebust 			if (req->rq_xid == xid)
3352ea24497STrond Myklebust 				goto found;
3362ea24497STrond Myklebust 		}
3372ea24497STrond Myklebust 		req = xprt_get_bc_request(xprt, xid, new);
3382ea24497STrond Myklebust found:
3392ea24497STrond Myklebust 		spin_unlock(&xprt->bc_pa_lock);
3402ea24497STrond Myklebust 		if (new) {
3410d1bf340STrond Myklebust 			if (req != new)
3422ea24497STrond Myklebust 				xprt_free_allocation(new);
3430d1bf340STrond Myklebust 			break;
3442ea24497STrond Myklebust 		} else if (req)
3452ea24497STrond Myklebust 			break;
3462ea24497STrond Myklebust 		new = xprt_alloc_bc_req(xprt);
3472ea24497STrond Myklebust 	} while (new);
3482ea24497STrond Myklebust 	return req;
3492ea24497STrond Myklebust }
3502ea24497STrond Myklebust 
3510d1bf340STrond Myklebust /*
3522ea24497STrond Myklebust  * Add callback request to callback list.  The callback
3532ea24497STrond Myklebust  * service sleeps on the sv_cb_waitq waiting for new
3540d1bf340STrond Myklebust  * requests.  Wake it up after adding enqueing the
3550d1bf340STrond Myklebust  * request.
356875f0706STrond Myklebust  */
xprt_complete_bc_request(struct rpc_rqst * req,uint32_t copied)3570d1bf340STrond Myklebust void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)
3580d1bf340STrond Myklebust {
3590d1bf340STrond Myklebust 	struct rpc_xprt *xprt = req->rq_xprt;
360b2648015STrond Myklebust 	struct svc_serv *bc_serv = xprt->bc_serv;
3610d1bf340STrond Myklebust 
3622ea24497STrond Myklebust 	spin_lock(&xprt->bc_pa_lock);
3632ea24497STrond Myklebust 	list_del(&req->rq_bc_pa_list);
3642ea24497STrond Myklebust 	xprt->bc_alloc_count--;
3652ea24497STrond Myklebust 	spin_unlock(&xprt->bc_pa_lock);
3662ea24497STrond Myklebust 
3672ea24497STrond Myklebust 	req->rq_private_buf.len = copied;
3682ea24497STrond Myklebust 	set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
3692ea24497STrond Myklebust 
3702ea24497STrond Myklebust 	dprintk("RPC:       add callback request to list\n");
3712ea24497STrond Myklebust 	xprt_get(xprt);
3722ea24497STrond Myklebust 	spin_lock(&bc_serv->sv_cb_lock);
3732ea24497STrond Myklebust 	list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
3742ea24497STrond Myklebust 	wake_up(&bc_serv->sv_cb_waitq);
3752ea24497STrond Myklebust 	spin_unlock(&bc_serv->sv_cb_lock);
376813b00d6SChuck Lever }
377813b00d6SChuck Lever