Linux Audio

Check our new training course

Loading...
v3.1
 
   1/*
   2 * linux/net/sunrpc/xprtsock.c
   3 *
   4 * Client-side transport implementation for sockets.
   5 *
   6 * TCP callback races fixes (C) 1998 Red Hat
   7 * TCP send fixes (C) 1998 Red Hat
   8 * TCP NFS related read + write fixes
   9 *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
  10 *
  11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
  12 * Fix behaviour when socket buffer is full.
  13 *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
  14 *
  15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
  16 *
  17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
  18 *   <gilles.quillard@bull.net>
  19 */
  20
  21#include <linux/types.h>
  22#include <linux/string.h>
  23#include <linux/slab.h>
  24#include <linux/module.h>
  25#include <linux/capability.h>
  26#include <linux/pagemap.h>
  27#include <linux/errno.h>
  28#include <linux/socket.h>
  29#include <linux/in.h>
  30#include <linux/net.h>
  31#include <linux/mm.h>
  32#include <linux/un.h>
  33#include <linux/udp.h>
  34#include <linux/tcp.h>
  35#include <linux/sunrpc/clnt.h>
 
  36#include <linux/sunrpc/sched.h>
  37#include <linux/sunrpc/svcsock.h>
  38#include <linux/sunrpc/xprtsock.h>
  39#include <linux/file.h>
  40#ifdef CONFIG_SUNRPC_BACKCHANNEL
  41#include <linux/sunrpc/bc_xprt.h>
  42#endif
  43
  44#include <net/sock.h>
  45#include <net/checksum.h>
  46#include <net/udp.h>
  47#include <net/tcp.h>
 
 
 
 
  48
 
 
 
  49#include "sunrpc.h"
  50
  51static void xs_close(struct rpc_xprt *xprt);
 
 
 
  52
  53/*
  54 * xprtsock tunables
  55 */
  56unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
  57unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
  58unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
  59
  60unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
  61unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
  62
  63#define XS_TCP_LINGER_TO	(15U * HZ)
  64static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
  65
  66/*
  67 * We can register our own files under /proc/sys/sunrpc by
  68 * calling register_sysctl_table() again.  The files in that
  69 * directory become the union of all files registered there.
  70 *
  71 * We simply need to make sure that we don't collide with
  72 * someone else's file names!
  73 */
  74
  75#ifdef RPC_DEBUG
  76
  77static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
  78static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
  79static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
  80static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
  81static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
  82
  83static struct ctl_table_header *sunrpc_table_header;
  84
 
 
 
 
 
  85/*
  86 * FIXME: changing the UDP slot table size should also resize the UDP
  87 *        socket buffers for existing UDP transports
  88 */
  89static ctl_table xs_tunables_table[] = {
  90	{
  91		.procname	= "udp_slot_table_entries",
  92		.data		= &xprt_udp_slot_table_entries,
  93		.maxlen		= sizeof(unsigned int),
  94		.mode		= 0644,
  95		.proc_handler	= proc_dointvec_minmax,
  96		.extra1		= &min_slot_table_size,
  97		.extra2		= &max_slot_table_size
  98	},
  99	{
 100		.procname	= "tcp_slot_table_entries",
 101		.data		= &xprt_tcp_slot_table_entries,
 102		.maxlen		= sizeof(unsigned int),
 103		.mode		= 0644,
 104		.proc_handler	= proc_dointvec_minmax,
 105		.extra1		= &min_slot_table_size,
 106		.extra2		= &max_slot_table_size
 107	},
 108	{
 109		.procname	= "tcp_max_slot_table_entries",
 110		.data		= &xprt_max_tcp_slot_table_entries,
 111		.maxlen		= sizeof(unsigned int),
 112		.mode		= 0644,
 113		.proc_handler	= proc_dointvec_minmax,
 114		.extra1		= &min_slot_table_size,
 115		.extra2		= &max_tcp_slot_table_limit
 116	},
 117	{
 118		.procname	= "min_resvport",
 119		.data		= &xprt_min_resvport,
 120		.maxlen		= sizeof(unsigned int),
 121		.mode		= 0644,
 122		.proc_handler	= proc_dointvec_minmax,
 123		.extra1		= &xprt_min_resvport_limit,
 124		.extra2		= &xprt_max_resvport_limit
 125	},
 126	{
 127		.procname	= "max_resvport",
 128		.data		= &xprt_max_resvport,
 129		.maxlen		= sizeof(unsigned int),
 130		.mode		= 0644,
 131		.proc_handler	= proc_dointvec_minmax,
 132		.extra1		= &xprt_min_resvport_limit,
 133		.extra2		= &xprt_max_resvport_limit
 134	},
 135	{
 136		.procname	= "tcp_fin_timeout",
 137		.data		= &xs_tcp_fin_timeout,
 138		.maxlen		= sizeof(xs_tcp_fin_timeout),
 139		.mode		= 0644,
 140		.proc_handler	= proc_dointvec_jiffies,
 141	},
 142	{ },
 143};
 144
 145static ctl_table sunrpc_table[] = {
 146	{
 147		.procname	= "sunrpc",
 148		.mode		= 0555,
 149		.child		= xs_tunables_table
 150	},
 151	{ },
 152};
 153
 154#endif
 155
 156/*
 157 * Wait duration for a reply from the RPC portmapper.
 158 */
 159#define XS_BIND_TO		(60U * HZ)
 160
 161/*
 162 * Delay if a UDP socket connect error occurs.  This is most likely some
 163 * kind of resource problem on the local host.
 164 */
 165#define XS_UDP_REEST_TO		(2U * HZ)
 166
 167/*
 168 * The reestablish timeout allows clients to delay for a bit before attempting
 169 * to reconnect to a server that just dropped our connection.
 170 *
 171 * We implement an exponential backoff when trying to reestablish a TCP
 172 * transport connection with the server.  Some servers like to drop a TCP
 173 * connection when they are overworked, so we start with a short timeout and
 174 * increase over time if the server is down or not responding.
 175 */
 176#define XS_TCP_INIT_REEST_TO	(3U * HZ)
 177#define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
 178
 179/*
 180 * TCP idle timeout; client drops the transport socket if it is idle
 181 * for this long.  Note that we also timeout UDP sockets to prevent
 182 * holding port numbers when there is no RPC traffic.
 183 */
 184#define XS_IDLE_DISC_TO		(5U * 60 * HZ)
 185
 186#ifdef RPC_DEBUG
 187# undef  RPC_DEBUG_DATA
 188# define RPCDBG_FACILITY	RPCDBG_TRANS
 189#endif
 190
 191#ifdef RPC_DEBUG_DATA
 192static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
 193{
 194	u8 *buf = (u8 *) packet;
 195	int j;
 196
 197	dprintk("RPC:       %s\n", msg);
 198	for (j = 0; j < count && j < 128; j += 4) {
 199		if (!(j & 31)) {
 200			if (j)
 201				dprintk("\n");
 202			dprintk("0x%04x ", j);
 203		}
 204		dprintk("%02x%02x%02x%02x ",
 205			buf[j], buf[j+1], buf[j+2], buf[j+3]);
 206	}
 207	dprintk("\n");
 208}
 209#else
 210static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
 211{
 212	/* NOP */
 213}
 214#endif
 215
 216struct sock_xprt {
 217	struct rpc_xprt		xprt;
 218
 219	/*
 220	 * Network layer
 221	 */
 222	struct socket *		sock;
 223	struct sock *		inet;
 224
 225	/*
 226	 * State of TCP reply receive
 227	 */
 228	__be32			tcp_fraghdr,
 229				tcp_xid,
 230				tcp_calldir;
 231
 232	u32			tcp_offset,
 233				tcp_reclen;
 234
 235	unsigned long		tcp_copied,
 236				tcp_flags;
 237
 238	/*
 239	 * Connection of transports
 240	 */
 241	struct delayed_work	connect_worker;
 242	struct sockaddr_storage	srcaddr;
 243	unsigned short		srcport;
 244
 245	/*
 246	 * UDP socket buffer size parameters
 247	 */
 248	size_t			rcvsize,
 249				sndsize;
 250
 251	/*
 252	 * Saved socket callback addresses
 253	 */
 254	void			(*old_data_ready)(struct sock *, int);
 255	void			(*old_state_change)(struct sock *);
 256	void			(*old_write_space)(struct sock *);
 257	void			(*old_error_report)(struct sock *);
 258};
 259
 260/*
 261 * TCP receive state flags
 262 */
 263#define TCP_RCV_LAST_FRAG	(1UL << 0)
 264#define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
 265#define TCP_RCV_COPY_XID	(1UL << 2)
 266#define TCP_RCV_COPY_DATA	(1UL << 3)
 267#define TCP_RCV_READ_CALLDIR	(1UL << 4)
 268#define TCP_RCV_COPY_CALLDIR	(1UL << 5)
 269
 270/*
 271 * TCP RPC flags
 272 */
 273#define TCP_RPC_REPLY		(1UL << 6)
 274
 275static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
 276{
 277	return (struct sockaddr *) &xprt->addr;
 278}
 279
 280static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
 281{
 282	return (struct sockaddr_un *) &xprt->addr;
 283}
 284
 285static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
 286{
 287	return (struct sockaddr_in *) &xprt->addr;
 288}
 289
 290static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
 291{
 292	return (struct sockaddr_in6 *) &xprt->addr;
 293}
 294
 295static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
 296{
 297	struct sockaddr *sap = xs_addr(xprt);
 298	struct sockaddr_in6 *sin6;
 299	struct sockaddr_in *sin;
 300	struct sockaddr_un *sun;
 301	char buf[128];
 302
 303	switch (sap->sa_family) {
 304	case AF_LOCAL:
 305		sun = xs_addr_un(xprt);
 306		strlcpy(buf, sun->sun_path, sizeof(buf));
 307		xprt->address_strings[RPC_DISPLAY_ADDR] =
 308						kstrdup(buf, GFP_KERNEL);
 309		break;
 310	case AF_INET:
 311		(void)rpc_ntop(sap, buf, sizeof(buf));
 312		xprt->address_strings[RPC_DISPLAY_ADDR] =
 313						kstrdup(buf, GFP_KERNEL);
 314		sin = xs_addr_in(xprt);
 315		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
 316		break;
 317	case AF_INET6:
 318		(void)rpc_ntop(sap, buf, sizeof(buf));
 319		xprt->address_strings[RPC_DISPLAY_ADDR] =
 320						kstrdup(buf, GFP_KERNEL);
 321		sin6 = xs_addr_in6(xprt);
 322		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
 323		break;
 324	default:
 325		BUG();
 326	}
 327
 328	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
 329}
 330
 331static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
 332{
 333	struct sockaddr *sap = xs_addr(xprt);
 334	char buf[128];
 335
 336	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
 337	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
 338
 339	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
 340	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
 341}
 342
 343static void xs_format_peer_addresses(struct rpc_xprt *xprt,
 344				     const char *protocol,
 345				     const char *netid)
 346{
 347	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
 348	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
 349	xs_format_common_peer_addresses(xprt);
 350	xs_format_common_peer_ports(xprt);
 351}
 352
 353static void xs_update_peer_port(struct rpc_xprt *xprt)
 354{
 355	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
 356	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
 357
 358	xs_format_common_peer_ports(xprt);
 359}
 360
 361static void xs_free_peer_addresses(struct rpc_xprt *xprt)
 362{
 363	unsigned int i;
 364
 365	for (i = 0; i < RPC_DISPLAY_MAX; i++)
 366		switch (i) {
 367		case RPC_DISPLAY_PROTO:
 368		case RPC_DISPLAY_NETID:
 369			continue;
 370		default:
 371			kfree(xprt->address_strings[i]);
 372		}
 373}
 374
 375#define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 376
 377static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
 
 378{
 379	struct msghdr msg = {
 380		.msg_name	= addr,
 381		.msg_namelen	= addrlen,
 382		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 383	};
 384	struct kvec iov = {
 385		.iov_base	= vec->iov_base + base,
 386		.iov_len	= vec->iov_len - base,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 387	};
 
 
 388
 389	if (iov.iov_len != 0)
 390		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
 391	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
 392}
 393
 394static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
 395{
 396	struct page **ppage;
 397	unsigned int remainder;
 398	int err, sent = 0;
 399
 400	remainder = xdr->page_len - base;
 401	base += xdr->page_base;
 402	ppage = xdr->pages + (base >> PAGE_SHIFT);
 403	base &= ~PAGE_MASK;
 404	for(;;) {
 405		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
 406		int flags = XS_SENDMSG_FLAGS;
 407
 408		remainder -= len;
 409		if (remainder != 0 || more)
 410			flags |= MSG_MORE;
 411		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
 412		if (remainder == 0 || err != len)
 413			break;
 414		sent += err;
 415		ppage++;
 416		base = 0;
 417	}
 418	if (sent == 0)
 419		return err;
 420	if (err > 0)
 421		sent += err;
 422	return sent;
 
 
 
 
 
 423}
 
 
 
 
 
 
 
 424
 425/**
 426 * xs_sendpages - write pages directly to a socket
 427 * @sock: socket to send on
 428 * @addr: UDP only -- address of destination
 429 * @addrlen: UDP only -- length of destination address
 430 * @xdr: buffer containing this request
 431 * @base: starting position in the buffer
 432 *
 433 */
 434static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
 435{
 436	unsigned int remainder = xdr->len - base;
 437	int err, sent = 0;
 
 438
 439	if (unlikely(!sock))
 440		return -ENOTSOCK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 441
 442	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 443	if (base != 0) {
 444		addr = NULL;
 445		addrlen = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 446	}
 447
 448	if (base < xdr->head[0].iov_len || addr != NULL) {
 449		unsigned int len = xdr->head[0].iov_len - base;
 450		remainder -= len;
 451		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
 452		if (remainder == 0 || err != len)
 453			goto out;
 454		sent += err;
 455		base = 0;
 456	} else
 457		base -= xdr->head[0].iov_len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 458
 459	if (base < xdr->page_len) {
 460		unsigned int len = xdr->page_len - base;
 461		remainder -= len;
 462		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
 463		if (remainder == 0 || err != len)
 464			goto out;
 465		sent += err;
 466		base = 0;
 467	} else
 468		base -= xdr->page_len;
 
 
 
 
 
 469
 470	if (base >= xdr->tail[0].iov_len)
 471		return sent;
 472	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 473out:
 474	if (sent == 0)
 475		return err;
 476	if (err > 0)
 477		sent += err;
 478	return sent;
 
 
 
 
 
 
 
 479}
 480
 481static void xs_nospace_callback(struct rpc_task *task)
 
 482{
 483	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
 
 
 
 
 484
 485	transport->inet->sk_write_pending--;
 486	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
 
 
 
 487}
 488
 
 
 489/**
 490 * xs_nospace - place task on wait queue if transmit was incomplete
 491 * @task: task to put to sleep
 
 492 *
 493 */
 494static int xs_nospace(struct rpc_task *task)
 495{
 496	struct rpc_rqst *req = task->tk_rqstp;
 497	struct rpc_xprt *xprt = req->rq_xprt;
 498	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 499	int ret = 0;
 500
 501	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
 502			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
 503			req->rq_slen);
 504
 505	/* Protect against races with write_space */
 506	spin_lock_bh(&xprt->transport_lock);
 507
 508	/* Don't race with disconnect */
 509	if (xprt_connected(xprt)) {
 510		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
 511			ret = -EAGAIN;
 512			/*
 513			 * Notify TCP that we're limited by the application
 514			 * window size
 515			 */
 516			set_bit(SOCK_NOSPACE, &transport->sock->flags);
 517			transport->inet->sk_write_pending++;
 518			/* ...and wait for more buffer space */
 519			xprt_wait_for_buffer_space(task, xs_nospace_callback);
 520		}
 521	} else {
 522		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
 523		ret = -ENOTCONN;
 524	}
 525
 526	spin_unlock_bh(&xprt->transport_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 527	return ret;
 528}
 529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 530/*
 531 * Construct a stream transport record marker in @buf.
 532 */
 533static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
 
 534{
 535	u32 reclen = buf->len - sizeof(rpc_fraghdr);
 536	rpc_fraghdr *base = buf->head[0].iov_base;
 537	*base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
 538}
 539
 540/**
 541 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
 542 * @task: RPC task that manages the state of an RPC request
 543 *
 544 * Return values:
 545 *        0:	The request has been sent
 546 *   EAGAIN:	The socket was blocked, please call again later to
 547 *		complete the request
 548 * ENOTCONN:	Caller needs to invoke connect logic then call again
 549 *    other:	Some other error occured, the request was not sent
 550 */
 551static int xs_local_send_request(struct rpc_task *task)
 552{
 553	struct rpc_rqst *req = task->tk_rqstp;
 554	struct rpc_xprt *xprt = req->rq_xprt;
 555	struct sock_xprt *transport =
 556				container_of(xprt, struct sock_xprt, xprt);
 557	struct xdr_buf *xdr = &req->rq_snd_buf;
 
 
 
 
 
 
 
 558	int status;
 559
 560	xs_encode_stream_record_marker(&req->rq_snd_buf);
 
 
 
 
 561
 562	xs_pktdump("packet data:",
 563			req->rq_svec->iov_base, req->rq_svec->iov_len);
 564
 565	status = xs_sendpages(transport->sock, NULL, 0,
 566						xdr, req->rq_bytes_sent);
 
 
 
 567	dprintk("RPC:       %s(%u) = %d\n",
 568			__func__, xdr->len - req->rq_bytes_sent, status);
 569	if (likely(status >= 0)) {
 570		req->rq_bytes_sent += status;
 571		req->rq_xmit_bytes_sent += status;
 572		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
 573			req->rq_bytes_sent = 0;
 
 
 574			return 0;
 575		}
 576		status = -EAGAIN;
 
 577	}
 578
 579	switch (status) {
 580	case -EAGAIN:
 581		status = xs_nospace(task);
 582		break;
 583	default:
 584		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
 585			-status);
 
 586	case -EPIPE:
 587		xs_close(xprt);
 588		status = -ENOTCONN;
 589	}
 590
 591	return status;
 592}
 593
 594/**
 595 * xs_udp_send_request - write an RPC request to a UDP socket
 596 * @task: address of RPC task that manages the state of an RPC request
 597 *
 598 * Return values:
 599 *        0:	The request has been sent
 600 *   EAGAIN:	The socket was blocked, please call again later to
 601 *		complete the request
 602 * ENOTCONN:	Caller needs to invoke connect logic then call again
 603 *    other:	Some other error occurred, the request was not sent
 604 */
 605static int xs_udp_send_request(struct rpc_task *task)
 606{
 607	struct rpc_rqst *req = task->tk_rqstp;
 608	struct rpc_xprt *xprt = req->rq_xprt;
 609	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 610	struct xdr_buf *xdr = &req->rq_snd_buf;
 
 
 
 
 
 
 611	int status;
 612
 613	xs_pktdump("packet data:",
 614				req->rq_svec->iov_base,
 615				req->rq_svec->iov_len);
 616
 617	if (!xprt_bound(xprt))
 618		return -ENOTCONN;
 619	status = xs_sendpages(transport->sock,
 620			      xs_addr(xprt),
 621			      xprt->addrlen, xdr,
 622			      req->rq_bytes_sent);
 
 
 
 
 
 623
 624	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
 625			xdr->len - req->rq_bytes_sent, status);
 626
 627	if (status >= 0) {
 628		req->rq_xmit_bytes_sent += status;
 629		if (status >= req->rq_slen)
 
 
 
 
 
 
 
 630			return 0;
 631		/* Still some bytes left; set up for a retry later. */
 632		status = -EAGAIN;
 633	}
 634
 
 635	switch (status) {
 636	case -ENOTSOCK:
 637		status = -ENOTCONN;
 638		/* Should we call xs_close() here? */
 639		break;
 640	case -EAGAIN:
 641		status = xs_nospace(task);
 642		break;
 643	default:
 644		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
 645			-status);
 646	case -ENETUNREACH:
 
 647	case -EPIPE:
 648	case -ECONNREFUSED:
 
 649		/* When the server has died, an ICMP port unreachable message
 650		 * prompts ECONNREFUSED. */
 651		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
 
 
 
 652	}
 653
 654	return status;
 655}
 656
 657/**
 658 * xs_tcp_shutdown - gracefully shut down a TCP socket
 659 * @xprt: transport
 660 *
 661 * Initiates a graceful shutdown of the TCP socket by calling the
 662 * equivalent of shutdown(SHUT_WR);
 663 */
 664static void xs_tcp_shutdown(struct rpc_xprt *xprt)
 665{
 666	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 667	struct socket *sock = transport->sock;
 668
 669	if (sock != NULL)
 670		kernel_sock_shutdown(sock, SHUT_WR);
 671}
 672
 673/**
 674 * xs_tcp_send_request - write an RPC request to a TCP socket
 675 * @task: address of RPC task that manages the state of an RPC request
 676 *
 677 * Return values:
 678 *        0:	The request has been sent
 679 *   EAGAIN:	The socket was blocked, please call again later to
 680 *		complete the request
 681 * ENOTCONN:	Caller needs to invoke connect logic then call again
 682 *    other:	Some other error occurred, the request was not sent
 683 *
 684 * XXX: In the case of soft timeouts, should we eventually give up
 685 *	if sendmsg is not able to make progress?
 686 */
 687static int xs_tcp_send_request(struct rpc_task *task)
 688{
 689	struct rpc_rqst *req = task->tk_rqstp;
 690	struct rpc_xprt *xprt = req->rq_xprt;
 691	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 692	struct xdr_buf *xdr = &req->rq_snd_buf;
 
 
 
 
 
 
 
 693	int status;
 694
 695	xs_encode_stream_record_marker(&req->rq_snd_buf);
 
 
 
 
 
 
 
 696
 697	xs_pktdump("packet data:",
 698				req->rq_svec->iov_base,
 699				req->rq_svec->iov_len);
 700
 
 
 
 
 
 701	/* Continue transmitting the packet/record. We must be careful
 702	 * to cope with writespace callbacks arriving _after_ we have
 703	 * called sendmsg(). */
 704	while (1) {
 705		status = xs_sendpages(transport->sock,
 706					NULL, 0, xdr, req->rq_bytes_sent);
 707
 708		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
 709				xdr->len - req->rq_bytes_sent, status);
 710
 711		if (unlikely(status < 0))
 712			break;
 
 
 
 
 713
 714		/* If we've sent the entire packet, immediately
 715		 * reset the count of bytes sent. */
 716		req->rq_bytes_sent += status;
 717		req->rq_xmit_bytes_sent += status;
 718		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
 719			req->rq_bytes_sent = 0;
 
 
 
 720			return 0;
 721		}
 722
 723		if (status != 0)
 724			continue;
 725		status = -EAGAIN;
 726		break;
 727	}
 
 728
 729	switch (status) {
 730	case -ENOTSOCK:
 731		status = -ENOTCONN;
 732		/* Should we call xs_close() here? */
 733		break;
 734	case -EAGAIN:
 735		status = xs_nospace(task);
 736		break;
 737	default:
 738		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
 739			-status);
 740	case -ECONNRESET:
 741	case -EPIPE:
 742		xs_tcp_shutdown(xprt);
 743	case -ECONNREFUSED:
 744	case -ENOTCONN:
 745		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
 
 
 
 
 
 
 746	}
 747
 748	return status;
 749}
 750
 751/**
 752 * xs_tcp_release_xprt - clean up after a tcp transmission
 753 * @xprt: transport
 754 * @task: rpc task
 755 *
 756 * This cleans up if an error causes us to abort the transmission of a request.
 757 * In this case, the socket may need to be reset in order to avoid confusing
 758 * the server.
 759 */
 760static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
 761{
 762	struct rpc_rqst *req;
 763
 764	if (task != xprt->snd_task)
 765		return;
 766	if (task == NULL)
 767		goto out_release;
 768	req = task->tk_rqstp;
 769	if (req == NULL)
 770		goto out_release;
 771	if (req->rq_bytes_sent == 0)
 772		goto out_release;
 773	if (req->rq_bytes_sent == req->rq_snd_buf.len)
 774		goto out_release;
 775	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
 776out_release:
 777	xprt_release_xprt(xprt, task);
 778}
 779
 780static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
 781{
 782	transport->old_data_ready = sk->sk_data_ready;
 783	transport->old_state_change = sk->sk_state_change;
 784	transport->old_write_space = sk->sk_write_space;
 785	transport->old_error_report = sk->sk_error_report;
 786}
 787
 788static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
 789{
 790	sk->sk_data_ready = transport->old_data_ready;
 791	sk->sk_state_change = transport->old_state_change;
 792	sk->sk_write_space = transport->old_write_space;
 793	sk->sk_error_report = transport->old_error_report;
 794}
 795
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 796static void xs_reset_transport(struct sock_xprt *transport)
 797{
 798	struct socket *sock = transport->sock;
 799	struct sock *sk = transport->inet;
 
 
 800
 801	if (sk == NULL)
 802		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 803
 804	transport->srcport = 0;
 805
 806	write_lock_bh(&sk->sk_callback_lock);
 
 807	transport->inet = NULL;
 808	transport->sock = NULL;
 
 809
 810	sk->sk_user_data = NULL;
 811
 812	xs_restore_old_callbacks(transport, sk);
 813	write_unlock_bh(&sk->sk_callback_lock);
 
 
 
 
 
 814
 815	sk->sk_no_check = 0;
 
 816
 817	sock_release(sock);
 818}
 819
 820/**
 821 * xs_close - close a socket
 822 * @xprt: transport
 823 *
 824 * This is used when all requests are complete; ie, no DRC state remains
 825 * on the server we want to save.
 826 *
 827 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
 828 * xs_reset_transport() zeroing the socket from underneath a writer.
 829 */
 830static void xs_close(struct rpc_xprt *xprt)
 831{
 832	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 833
 834	dprintk("RPC:       xs_close xprt %p\n", xprt);
 835
 836	xs_reset_transport(transport);
 837	xprt->reestablish_timeout = 0;
 
 838
 839	smp_mb__before_clear_bit();
 840	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
 841	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
 842	clear_bit(XPRT_CLOSING, &xprt->state);
 843	smp_mb__after_clear_bit();
 844	xprt_disconnect_done(xprt);
 845}
 846
 847static void xs_tcp_close(struct rpc_xprt *xprt)
 848{
 849	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
 850		xs_close(xprt);
 851	else
 852		xs_tcp_shutdown(xprt);
 853}
 854
 855/**
 856 * xs_destroy - prepare to shutdown a transport
 857 * @xprt: doomed transport
 858 *
 859 */
 860static void xs_destroy(struct rpc_xprt *xprt)
 861{
 862	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 863
 864	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
 865
 866	cancel_delayed_work_sync(&transport->connect_worker);
 867
 868	xs_close(xprt);
 869	xs_free_peer_addresses(xprt);
 870	xprt_free(xprt);
 
 871	module_put(THIS_MODULE);
 872}
 873
 874static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
 875{
 876	return (struct rpc_xprt *) sk->sk_user_data;
 877}
 878
 879static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
 880{
 881	struct xdr_skb_reader desc = {
 882		.skb		= skb,
 883		.offset		= sizeof(rpc_fraghdr),
 884		.count		= skb->len - sizeof(rpc_fraghdr),
 885	};
 886
 887	if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
 888		return -1;
 889	if (desc.count)
 890		return -1;
 891	return 0;
 892}
 893
 894/**
 895 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
 896 * @sk: socket with data to read
 897 * @len: how much data to read
 
 898 *
 899 * Currently this assumes we can read the whole reply in a single gulp.
 900 */
 901static void xs_local_data_ready(struct sock *sk, int len)
 
 
 902{
 903	struct rpc_task *task;
 904	struct rpc_xprt *xprt;
 905	struct rpc_rqst *rovr;
 906	struct sk_buff *skb;
 907	int err, repsize, copied;
 908	u32 _xid;
 909	__be32 *xp;
 910
 911	read_lock_bh(&sk->sk_callback_lock);
 912	dprintk("RPC:       %s...\n", __func__);
 913	xprt = xprt_from_sock(sk);
 914	if (xprt == NULL)
 915		goto out;
 916
 917	skb = skb_recv_datagram(sk, 0, 1, &err);
 918	if (skb == NULL)
 919		goto out;
 920
 921	if (xprt->shutdown)
 922		goto dropit;
 923
 924	repsize = skb->len - sizeof(rpc_fraghdr);
 925	if (repsize < 4) {
 926		dprintk("RPC:       impossible RPC reply size %d\n", repsize);
 927		goto dropit;
 928	}
 929
 930	/* Copy the XID from the skb... */
 931	xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
 932	if (xp == NULL)
 933		goto dropit;
 934
 935	/* Look up and lock the request corresponding to the given XID */
 936	spin_lock(&xprt->transport_lock);
 937	rovr = xprt_lookup_rqst(xprt, *xp);
 938	if (!rovr)
 939		goto out_unlock;
 940	task = rovr->rq_task;
 941
 942	copied = rovr->rq_private_buf.buflen;
 943	if (copied > repsize)
 944		copied = repsize;
 945
 946	if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
 947		dprintk("RPC:       sk_buff copy failed\n");
 948		goto out_unlock;
 949	}
 950
 951	xprt_complete_rqst(task, copied);
 952
 953 out_unlock:
 954	spin_unlock(&xprt->transport_lock);
 955 dropit:
 956	skb_free_datagram(sk, skb);
 957 out:
 958	read_unlock_bh(&sk->sk_callback_lock);
 959}
 960
 961/**
 962 * xs_udp_data_ready - "data ready" callback for UDP sockets
 963 * @sk: socket with data to read
 964 * @len: how much data to read
 965 *
 966 */
 967static void xs_udp_data_ready(struct sock *sk, int len)
 968{
 969	struct rpc_task *task;
 970	struct rpc_xprt *xprt;
 971	struct rpc_rqst *rovr;
 972	struct sk_buff *skb;
 973	int err, repsize, copied;
 974	u32 _xid;
 975	__be32 *xp;
 976
 977	read_lock_bh(&sk->sk_callback_lock);
 978	dprintk("RPC:       xs_udp_data_ready...\n");
 979	if (!(xprt = xprt_from_sock(sk)))
 980		goto out;
 981
 982	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
 983		goto out;
 984
 985	if (xprt->shutdown)
 986		goto dropit;
 987
 988	repsize = skb->len - sizeof(struct udphdr);
 989	if (repsize < 4) {
 990		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
 991		goto dropit;
 992	}
 993
 994	/* Copy the XID from the skb... */
 995	xp = skb_header_pointer(skb, sizeof(struct udphdr),
 996				sizeof(_xid), &_xid);
 997	if (xp == NULL)
 998		goto dropit;
 999
1000	/* Look up and lock the request corresponding to the given XID */
1001	spin_lock(&xprt->transport_lock);
1002	rovr = xprt_lookup_rqst(xprt, *xp);
1003	if (!rovr)
1004		goto out_unlock;
 
 
 
1005	task = rovr->rq_task;
1006
1007	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1008		copied = repsize;
1009
1010	/* Suck it into the iovec, verify checksum if not done by hw. */
1011	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1012		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
1013		goto out_unlock;
 
1014	}
1015
1016	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
1017
1018	/* Something worked... */
1019	dst_confirm(skb_dst(skb));
1020
1021	xprt_adjust_cwnd(task, copied);
 
 
 
1022	xprt_complete_rqst(task, copied);
1023
 
 
1024 out_unlock:
1025	spin_unlock(&xprt->transport_lock);
1026 dropit:
1027	skb_free_datagram(sk, skb);
1028 out:
1029	read_unlock_bh(&sk->sk_callback_lock);
1030}
1031
1032static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1033{
1034	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1035	size_t len, used;
1036	char *p;
1037
1038	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1039	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
1040	used = xdr_skb_read_bits(desc, p, len);
1041	transport->tcp_offset += used;
1042	if (used != len)
1043		return;
1044
1045	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1046	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1047		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
1048	else
1049		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
1050	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1051
1052	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
1053	transport->tcp_offset = 0;
1054
1055	/* Sanity check of the record length */
1056	if (unlikely(transport->tcp_reclen < 8)) {
1057		dprintk("RPC:       invalid TCP record fragment length\n");
1058		xprt_force_disconnect(xprt);
1059		return;
1060	}
1061	dprintk("RPC:       reading TCP record fragment of length %d\n",
1062			transport->tcp_reclen);
1063}
1064
1065static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
1066{
1067	if (transport->tcp_offset == transport->tcp_reclen) {
1068		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
1069		transport->tcp_offset = 0;
1070		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1071			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1072			transport->tcp_flags |= TCP_RCV_COPY_XID;
1073			transport->tcp_copied = 0;
1074		}
1075	}
1076}
1077
1078static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1079{
1080	size_t len, used;
1081	char *p;
1082
1083	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
1084	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
1085	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
1086	used = xdr_skb_read_bits(desc, p, len);
1087	transport->tcp_offset += used;
1088	if (used != len)
1089		return;
1090	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1091	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
1092	transport->tcp_copied = 4;
1093	dprintk("RPC:       reading %s XID %08x\n",
1094			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1095							      : "request with",
1096			ntohl(transport->tcp_xid));
1097	xs_tcp_check_fraghdr(transport);
1098}
1099
1100static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1101				       struct xdr_skb_reader *desc)
1102{
1103	size_t len, used;
1104	u32 offset;
1105	char *p;
1106
1107	/*
1108	 * We want transport->tcp_offset to be 8 at the end of this routine
1109	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1110	 * When this function is called for the first time,
1111	 * transport->tcp_offset is 4 (after having already read the xid).
1112	 */
1113	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1114	len = sizeof(transport->tcp_calldir) - offset;
1115	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
1116	p = ((char *) &transport->tcp_calldir) + offset;
1117	used = xdr_skb_read_bits(desc, p, len);
1118	transport->tcp_offset += used;
1119	if (used != len)
1120		return;
1121	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1122	/*
1123	 * We don't yet have the XDR buffer, so we will write the calldir
1124	 * out after we get the buffer from the 'struct rpc_rqst'
1125	 */
1126	switch (ntohl(transport->tcp_calldir)) {
1127	case RPC_REPLY:
1128		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1129		transport->tcp_flags |= TCP_RCV_COPY_DATA;
1130		transport->tcp_flags |= TCP_RPC_REPLY;
1131		break;
1132	case RPC_CALL:
1133		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1134		transport->tcp_flags |= TCP_RCV_COPY_DATA;
1135		transport->tcp_flags &= ~TCP_RPC_REPLY;
1136		break;
1137	default:
1138		dprintk("RPC:       invalid request message type\n");
1139		xprt_force_disconnect(&transport->xprt);
1140	}
1141	xs_tcp_check_fraghdr(transport);
1142}
1143
1144static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1145				     struct xdr_skb_reader *desc,
1146				     struct rpc_rqst *req)
1147{
1148	struct sock_xprt *transport =
1149				container_of(xprt, struct sock_xprt, xprt);
1150	struct xdr_buf *rcvbuf;
1151	size_t len;
1152	ssize_t r;
1153
1154	rcvbuf = &req->rq_private_buf;
1155
1156	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1157		/*
1158		 * Save the RPC direction in the XDR buffer
1159		 */
1160		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1161			&transport->tcp_calldir,
1162			sizeof(transport->tcp_calldir));
1163		transport->tcp_copied += sizeof(transport->tcp_calldir);
1164		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1165	}
1166
1167	len = desc->count;
1168	if (len > transport->tcp_reclen - transport->tcp_offset) {
1169		struct xdr_skb_reader my_desc;
1170
1171		len = transport->tcp_reclen - transport->tcp_offset;
1172		memcpy(&my_desc, desc, sizeof(my_desc));
1173		my_desc.count = len;
1174		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1175					  &my_desc, xdr_skb_read_bits);
1176		desc->count -= r;
1177		desc->offset += r;
1178	} else
1179		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1180					  desc, xdr_skb_read_bits);
1181
1182	if (r > 0) {
1183		transport->tcp_copied += r;
1184		transport->tcp_offset += r;
1185	}
1186	if (r != len) {
1187		/* Error when copying to the receive buffer,
1188		 * usually because we weren't able to allocate
1189		 * additional buffer pages. All we can do now
1190		 * is turn off TCP_RCV_COPY_DATA, so the request
1191		 * will not receive any additional updates,
1192		 * and time out.
1193		 * Any remaining data from this record will
1194		 * be discarded.
1195		 */
1196		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1197		dprintk("RPC:       XID %08x truncated request\n",
1198				ntohl(transport->tcp_xid));
1199		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
1200				"tcp_offset = %u, tcp_reclen = %u\n",
1201				xprt, transport->tcp_copied,
1202				transport->tcp_offset, transport->tcp_reclen);
1203		return;
1204	}
1205
1206	dprintk("RPC:       XID %08x read %Zd bytes\n",
1207			ntohl(transport->tcp_xid), r);
1208	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1209			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
1210			transport->tcp_offset, transport->tcp_reclen);
1211
1212	if (transport->tcp_copied == req->rq_private_buf.buflen)
1213		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1214	else if (transport->tcp_offset == transport->tcp_reclen) {
1215		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1216			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1217	}
 
 
 
1218}
1219
1220/*
1221 * Finds the request corresponding to the RPC xid and invokes the common
1222 * tcp read code to read the data.
1223 */
1224static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1225				    struct xdr_skb_reader *desc)
1226{
1227	struct sock_xprt *transport =
1228				container_of(xprt, struct sock_xprt, xprt);
1229	struct rpc_rqst *req;
1230
1231	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
1232
1233	/* Find and lock the request corresponding to this xid */
1234	spin_lock(&xprt->transport_lock);
1235	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1236	if (!req) {
1237		dprintk("RPC:       XID %08x request not found!\n",
1238				ntohl(transport->tcp_xid));
1239		spin_unlock(&xprt->transport_lock);
1240		return -1;
1241	}
1242
1243	xs_tcp_read_common(xprt, desc, req);
1244
1245	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1246		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1247
1248	spin_unlock(&xprt->transport_lock);
1249	return 0;
1250}
1251
1252#if defined(CONFIG_SUNRPC_BACKCHANNEL)
1253/*
1254 * Obtains an rpc_rqst previously allocated and invokes the common
1255 * tcp read code to read the data.  The result is placed in the callback
1256 * queue.
1257 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1258 * connection and return -1.
1259 */
1260static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1261				       struct xdr_skb_reader *desc)
1262{
1263	struct sock_xprt *transport =
1264				container_of(xprt, struct sock_xprt, xprt);
1265	struct rpc_rqst *req;
1266
1267	req = xprt_alloc_bc_request(xprt);
1268	if (req == NULL) {
1269		printk(KERN_WARNING "Callback slot table overflowed\n");
1270		xprt_force_disconnect(xprt);
1271		return -1;
1272	}
1273
1274	req->rq_xid = transport->tcp_xid;
1275	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
1276	xs_tcp_read_common(xprt, desc, req);
1277
1278	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1279		struct svc_serv *bc_serv = xprt->bc_serv;
1280
1281		/*
1282		 * Add callback request to callback list.  The callback
1283		 * service sleeps on the sv_cb_waitq waiting for new
1284		 * requests.  Wake it up after adding enqueing the
1285		 * request.
 
 
 
 
 
1286		 */
1287		dprintk("RPC:       add callback request to list\n");
1288		spin_lock(&bc_serv->sv_cb_lock);
1289		list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1290		spin_unlock(&bc_serv->sv_cb_lock);
1291		wake_up(&bc_serv->sv_cb_waitq);
1292	}
1293
1294	req->rq_private_buf.len = transport->tcp_copied;
1295
1296	return 0;
1297}
1298
1299static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1300					struct xdr_skb_reader *desc)
1301{
1302	struct sock_xprt *transport =
1303				container_of(xprt, struct sock_xprt, xprt);
1304
1305	return (transport->tcp_flags & TCP_RPC_REPLY) ?
1306		xs_tcp_read_reply(xprt, desc) :
1307		xs_tcp_read_callback(xprt, desc);
1308}
1309#else
1310static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1311					struct xdr_skb_reader *desc)
1312{
1313	return xs_tcp_read_reply(xprt, desc);
1314}
1315#endif /* CONFIG_SUNRPC_BACKCHANNEL */
1316
1317/*
1318 * Read data off the transport.  This can be either an RPC_CALL or an
1319 * RPC_REPLY.  Relay the processing to helper functions.
1320 */
1321static void xs_tcp_read_data(struct rpc_xprt *xprt,
1322				    struct xdr_skb_reader *desc)
1323{
1324	struct sock_xprt *transport =
1325				container_of(xprt, struct sock_xprt, xprt);
1326
1327	if (_xs_tcp_read_data(xprt, desc) == 0)
1328		xs_tcp_check_fraghdr(transport);
1329	else {
1330		/*
1331		 * The transport_lock protects the request handling.
1332		 * There's no need to hold it to update the tcp_flags.
1333		 */
1334		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1335	}
1336}
1337
1338static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1339{
1340	size_t len;
1341
1342	len = transport->tcp_reclen - transport->tcp_offset;
1343	if (len > desc->count)
1344		len = desc->count;
1345	desc->count -= len;
1346	desc->offset += len;
1347	transport->tcp_offset += len;
1348	dprintk("RPC:       discarded %Zu bytes\n", len);
1349	xs_tcp_check_fraghdr(transport);
1350}
1351
1352static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
 
1353{
1354	struct rpc_xprt *xprt = rd_desc->arg.data;
1355	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1356	struct xdr_skb_reader desc = {
1357		.skb	= skb,
1358		.offset	= offset,
1359		.count	= len,
1360	};
1361
1362	dprintk("RPC:       xs_tcp_data_recv started\n");
1363	do {
1364		/* Read in a new fragment marker if necessary */
1365		/* Can we ever really expect to get completely empty fragments? */
1366		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1367			xs_tcp_read_fraghdr(xprt, &desc);
1368			continue;
1369		}
1370		/* Read in the xid if necessary */
1371		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1372			xs_tcp_read_xid(transport, &desc);
1373			continue;
1374		}
1375		/* Read in the call/reply flag */
1376		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1377			xs_tcp_read_calldir(transport, &desc);
1378			continue;
1379		}
1380		/* Read in the request data */
1381		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1382			xs_tcp_read_data(xprt, &desc);
1383			continue;
1384		}
1385		/* Skip over any trailing bytes on short reads */
1386		xs_tcp_read_discard(transport, &desc);
1387	} while (desc.count);
1388	dprintk("RPC:       xs_tcp_data_recv done\n");
1389	return len - desc.count;
1390}
 
1391
1392/**
1393 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1394 * @sk: socket with data to read
1395 * @bytes: how much data to read
1396 *
1397 */
1398static void xs_tcp_data_ready(struct sock *sk, int bytes)
1399{
1400	struct rpc_xprt *xprt;
1401	read_descriptor_t rd_desc;
1402	int read;
1403
1404	dprintk("RPC:       xs_tcp_data_ready...\n");
1405
1406	read_lock_bh(&sk->sk_callback_lock);
1407	if (!(xprt = xprt_from_sock(sk)))
1408		goto out;
1409	if (xprt->shutdown)
1410		goto out;
1411
1412	/* Any data means we had a useful conversation, so
1413	 * the we don't need to delay the next reconnect
1414	 */
1415	if (xprt->reestablish_timeout)
1416		xprt->reestablish_timeout = 0;
1417
1418	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1419	rd_desc.arg.data = xprt;
1420	do {
1421		rd_desc.count = 65536;
1422		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1423	} while (read > 0);
1424out:
1425	read_unlock_bh(&sk->sk_callback_lock);
1426}
1427
1428/*
1429 * Do the equivalent of linger/linger2 handling for dealing with
1430 * broken servers that don't close the socket in a timely
1431 * fashion
1432 */
1433static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1434		unsigned long timeout)
1435{
1436	struct sock_xprt *transport;
1437
1438	if (xprt_test_and_set_connecting(xprt))
1439		return;
1440	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1441	transport = container_of(xprt, struct sock_xprt, xprt);
1442	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1443			   timeout);
1444}
1445
1446static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1447{
1448	struct sock_xprt *transport;
1449
1450	transport = container_of(xprt, struct sock_xprt, xprt);
1451
1452	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1453	    !cancel_delayed_work(&transport->connect_worker))
1454		return;
1455	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1456	xprt_clear_connecting(xprt);
1457}
1458
1459static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1460{
1461	smp_mb__before_clear_bit();
1462	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1463	clear_bit(XPRT_CLOSING, &xprt->state);
1464	smp_mb__after_clear_bit();
1465	/* Mark transport as closed and wake up all pending tasks */
1466	xprt_disconnect_done(xprt);
1467}
1468
1469/**
1470 * xs_tcp_state_change - callback to handle TCP socket state changes
1471 * @sk: socket whose state has changed
1472 *
1473 */
1474static void xs_tcp_state_change(struct sock *sk)
1475{
1476	struct rpc_xprt *xprt;
 
1477
1478	read_lock_bh(&sk->sk_callback_lock);
1479	if (!(xprt = xprt_from_sock(sk)))
1480		goto out;
1481	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1482	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1483			sk->sk_state, xprt_connected(xprt),
1484			sock_flag(sk, SOCK_DEAD),
1485			sock_flag(sk, SOCK_ZAPPED),
1486			sk->sk_shutdown);
1487
 
 
1488	switch (sk->sk_state) {
1489	case TCP_ESTABLISHED:
1490		spin_lock(&xprt->transport_lock);
1491		if (!xprt_test_and_set_connected(xprt)) {
1492			struct sock_xprt *transport = container_of(xprt,
1493					struct sock_xprt, xprt);
1494
1495			/* Reset TCP record info */
1496			transport->tcp_offset = 0;
1497			transport->tcp_reclen = 0;
1498			transport->tcp_copied = 0;
1499			transport->tcp_flags =
1500				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1501
1502			xprt_wake_pending_tasks(xprt, -EAGAIN);
1503		}
1504		spin_unlock(&xprt->transport_lock);
1505		break;
1506	case TCP_FIN_WAIT1:
1507		/* The client initiated a shutdown of the socket */
1508		xprt->connect_cookie++;
1509		xprt->reestablish_timeout = 0;
1510		set_bit(XPRT_CLOSING, &xprt->state);
1511		smp_mb__before_clear_bit();
1512		clear_bit(XPRT_CONNECTED, &xprt->state);
1513		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1514		smp_mb__after_clear_bit();
1515		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1516		break;
1517	case TCP_CLOSE_WAIT:
1518		/* The server initiated a shutdown of the socket */
1519		xprt_force_disconnect(xprt);
1520		xprt->connect_cookie++;
 
 
 
1521	case TCP_CLOSING:
1522		/*
1523		 * If the server closed down the connection, make sure that
1524		 * we back off before reconnecting
1525		 */
1526		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1527			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1528		break;
1529	case TCP_LAST_ACK:
1530		set_bit(XPRT_CLOSING, &xprt->state);
1531		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1532		smp_mb__before_clear_bit();
1533		clear_bit(XPRT_CONNECTED, &xprt->state);
1534		smp_mb__after_clear_bit();
1535		break;
1536	case TCP_CLOSE:
1537		xs_tcp_cancel_linger_timeout(xprt);
1538		xs_sock_mark_closed(xprt);
 
 
 
 
1539	}
1540 out:
1541	read_unlock_bh(&sk->sk_callback_lock);
1542}
1543
1544/**
1545 * xs_error_report - callback mainly for catching socket errors
1546 * @sk: socket
1547 */
1548static void xs_error_report(struct sock *sk)
1549{
1550	struct rpc_xprt *xprt;
1551
1552	read_lock_bh(&sk->sk_callback_lock);
1553	if (!(xprt = xprt_from_sock(sk)))
1554		goto out;
1555	dprintk("RPC:       %s client %p...\n"
1556			"RPC:       error %d\n",
1557			__func__, xprt, sk->sk_err);
1558	xprt_wake_pending_tasks(xprt, -EAGAIN);
1559out:
1560	read_unlock_bh(&sk->sk_callback_lock);
1561}
1562
1563static void xs_write_space(struct sock *sk)
1564{
1565	struct socket *sock;
1566	struct rpc_xprt *xprt;
1567
1568	if (unlikely(!(sock = sk->sk_socket)))
1569		return;
1570	clear_bit(SOCK_NOSPACE, &sock->flags);
1571
1572	if (unlikely(!(xprt = xprt_from_sock(sk))))
1573		return;
1574	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
 
1575		return;
1576
1577	xprt_write_space(xprt);
1578}
1579
1580/**
1581 * xs_udp_write_space - callback invoked when socket buffer space
1582 *                             becomes available
1583 * @sk: socket whose state has changed
1584 *
1585 * Called when more output buffer space is available for this socket.
1586 * We try not to wake our writers until they can make "significant"
1587 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1588 * with a bunch of small requests.
1589 */
1590static void xs_udp_write_space(struct sock *sk)
1591{
1592	read_lock_bh(&sk->sk_callback_lock);
1593
1594	/* from net/core/sock.c:sock_def_write_space */
1595	if (sock_writeable(sk))
1596		xs_write_space(sk);
1597
1598	read_unlock_bh(&sk->sk_callback_lock);
1599}
1600
1601/**
1602 * xs_tcp_write_space - callback invoked when socket buffer space
1603 *                             becomes available
1604 * @sk: socket whose state has changed
1605 *
1606 * Called when more output buffer space is available for this socket.
1607 * We try not to wake our writers until they can make "significant"
1608 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1609 * with a bunch of small requests.
1610 */
1611static void xs_tcp_write_space(struct sock *sk)
1612{
1613	read_lock_bh(&sk->sk_callback_lock);
1614
1615	/* from net/core/stream.c:sk_stream_write_space */
1616	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1617		xs_write_space(sk);
1618
1619	read_unlock_bh(&sk->sk_callback_lock);
1620}
1621
1622static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1623{
1624	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1625	struct sock *sk = transport->inet;
1626
1627	if (transport->rcvsize) {
1628		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1629		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1630	}
1631	if (transport->sndsize) {
1632		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1633		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1634		sk->sk_write_space(sk);
1635	}
1636}
1637
1638/**
1639 * xs_udp_set_buffer_size - set send and receive limits
1640 * @xprt: generic transport
1641 * @sndsize: requested size of send buffer, in bytes
1642 * @rcvsize: requested size of receive buffer, in bytes
1643 *
1644 * Set socket send and receive buffer size limits.
1645 */
1646static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1647{
1648	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1649
1650	transport->sndsize = 0;
1651	if (sndsize)
1652		transport->sndsize = sndsize + 1024;
1653	transport->rcvsize = 0;
1654	if (rcvsize)
1655		transport->rcvsize = rcvsize + 1024;
1656
1657	xs_udp_do_set_buffer_size(xprt);
1658}
1659
1660/**
1661 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
 
1662 * @task: task that timed out
1663 *
1664 * Adjust the congestion window after a retransmit timeout has occurred.
1665 */
1666static void xs_udp_timer(struct rpc_task *task)
1667{
1668	xprt_adjust_cwnd(task, -ETIMEDOUT);
 
 
1669}
1670
1671static unsigned short xs_get_random_port(void)
1672{
1673	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1674	unsigned short rand = (unsigned short) net_random() % range;
1675	return rand + xprt_min_resvport;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1676}
1677
1678/**
1679 * xs_set_port - reset the port number in the remote endpoint address
1680 * @xprt: generic transport
1681 * @port: new port number
1682 *
1683 */
1684static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1685{
1686	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1687
1688	rpc_set_port(xs_addr(xprt), port);
1689	xs_update_peer_port(xprt);
1690}
1691
1692static unsigned short xs_get_srcport(struct sock_xprt *transport)
 
 
 
 
 
 
1693{
1694	unsigned short port = transport->srcport;
1695
1696	if (port == 0 && transport->xprt.resvport)
1697		port = xs_get_random_port();
1698	return port;
1699}
1700
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1701static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1702{
1703	if (transport->srcport != 0)
1704		transport->srcport = 0;
1705	if (!transport->xprt.resvport)
1706		return 0;
1707	if (port <= xprt_min_resvport || port > xprt_max_resvport)
1708		return xprt_max_resvport;
1709	return --port;
1710}
1711static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1712{
1713	struct sockaddr_storage myaddr;
1714	int err, nloop = 0;
1715	unsigned short port = xs_get_srcport(transport);
1716	unsigned short last;
1717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1718	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1719	do {
1720		rpc_set_port((struct sockaddr *)&myaddr, port);
1721		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1722				transport->xprt.addrlen);
1723		if (port == 0)
1724			break;
1725		if (err == 0) {
1726			transport->srcport = port;
 
1727			break;
1728		}
1729		last = port;
1730		port = xs_next_srcport(transport, port);
1731		if (port > last)
1732			nloop++;
1733	} while (err == -EADDRINUSE && nloop != 2);
1734
1735	if (myaddr.ss_family == AF_INET)
1736		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1737				&((struct sockaddr_in *)&myaddr)->sin_addr,
1738				port, err ? "failed" : "ok", err);
1739	else
1740		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1741				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1742				port, err ? "failed" : "ok", err);
1743	return err;
1744}
1745
1746/*
1747 * We don't support autobind on AF_LOCAL sockets
1748 */
1749static void xs_local_rpcbind(struct rpc_task *task)
1750{
1751	xprt_set_bound(task->tk_xprt);
1752}
1753
1754static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1755{
1756}
1757
1758#ifdef CONFIG_DEBUG_LOCK_ALLOC
1759static struct lock_class_key xs_key[2];
1760static struct lock_class_key xs_slock_key[2];
1761
1762static inline void xs_reclassify_socketu(struct socket *sock)
1763{
1764	struct sock *sk = sock->sk;
1765
1766	BUG_ON(sock_owned_by_user(sk));
1767	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1768		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1769}
1770
1771static inline void xs_reclassify_socket4(struct socket *sock)
1772{
1773	struct sock *sk = sock->sk;
1774
1775	BUG_ON(sock_owned_by_user(sk));
1776	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1777		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1778}
1779
1780static inline void xs_reclassify_socket6(struct socket *sock)
1781{
1782	struct sock *sk = sock->sk;
1783
1784	BUG_ON(sock_owned_by_user(sk));
1785	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1786		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1787}
1788
1789static inline void xs_reclassify_socket(int family, struct socket *sock)
1790{
 
 
 
1791	switch (family) {
1792	case AF_LOCAL:
1793		xs_reclassify_socketu(sock);
1794		break;
1795	case AF_INET:
1796		xs_reclassify_socket4(sock);
1797		break;
1798	case AF_INET6:
1799		xs_reclassify_socket6(sock);
1800		break;
1801	}
1802}
1803#else
1804static inline void xs_reclassify_socketu(struct socket *sock)
1805{
1806}
1807
1808static inline void xs_reclassify_socket4(struct socket *sock)
1809{
1810}
1811
1812static inline void xs_reclassify_socket6(struct socket *sock)
1813{
1814}
 
1815
1816static inline void xs_reclassify_socket(int family, struct socket *sock)
1817{
1818}
1819#endif
1820
1821static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1822		struct sock_xprt *transport, int family, int type, int protocol)
 
1823{
 
1824	struct socket *sock;
1825	int err;
1826
1827	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1828	if (err < 0) {
1829		dprintk("RPC:       can't create %d transport socket (%d).\n",
1830				protocol, -err);
1831		goto out;
1832	}
1833	xs_reclassify_socket(family, sock);
1834
 
 
 
1835	err = xs_bind(transport, sock);
1836	if (err) {
1837		sock_release(sock);
1838		goto out;
1839	}
1840
 
 
 
 
 
1841	return sock;
1842out:
1843	return ERR_PTR(err);
1844}
1845
1846static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1847				      struct socket *sock)
1848{
1849	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1850									xprt);
1851
1852	if (!transport->inet) {
1853		struct sock *sk = sock->sk;
1854
1855		write_lock_bh(&sk->sk_callback_lock);
1856
1857		xs_save_old_callbacks(transport, sk);
1858
1859		sk->sk_user_data = xprt;
1860		sk->sk_data_ready = xs_local_data_ready;
1861		sk->sk_write_space = xs_udp_write_space;
 
1862		sk->sk_error_report = xs_error_report;
1863		sk->sk_allocation = GFP_ATOMIC;
1864
1865		xprt_clear_connected(xprt);
1866
1867		/* Reset to new socket */
1868		transport->sock = sock;
1869		transport->inet = sk;
1870
1871		write_unlock_bh(&sk->sk_callback_lock);
1872	}
1873
1874	/* Tell the socket layer to start connecting... */
1875	xprt->stat.connect_count++;
1876	xprt->stat.connect_start = jiffies;
1877	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1878}
1879
1880/**
1881 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1882 * @xprt: RPC transport to connect
1883 * @transport: socket transport to connect
1884 * @create_sock: function to create a socket of the correct type
1885 *
1886 * Invoked by a work queue tasklet.
1887 */
1888static void xs_local_setup_socket(struct work_struct *work)
1889{
1890	struct sock_xprt *transport =
1891		container_of(work, struct sock_xprt, connect_worker.work);
1892	struct rpc_xprt *xprt = &transport->xprt;
 
1893	struct socket *sock;
1894	int status = -EIO;
1895
1896	if (xprt->shutdown)
1897		goto out;
1898
1899	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1900	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1901					SOCK_STREAM, 0, &sock, 1);
1902	if (status < 0) {
1903		dprintk("RPC:       can't create AF_LOCAL "
1904			"transport socket (%d).\n", -status);
1905		goto out;
1906	}
1907	xs_reclassify_socketu(sock);
 
 
 
 
 
 
 
1908
1909	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1910			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1911
1912	status = xs_local_finish_connecting(xprt, sock);
 
1913	switch (status) {
1914	case 0:
1915		dprintk("RPC:       xprt %p connected to %s\n",
1916				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
 
 
 
1917		xprt_set_connected(xprt);
1918		break;
 
 
1919	case -ENOENT:
1920		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1921				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1922		break;
 
 
 
 
1923	default:
1924		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1925				__func__, -status,
1926				xprt->address_strings[RPC_DISPLAY_ADDR]);
1927	}
1928
1929out:
1930	xprt_clear_connecting(xprt);
1931	xprt_wake_pending_tasks(xprt, status);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1932}
1933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1934static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1935{
1936	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1937
1938	if (!transport->inet) {
1939		struct sock *sk = sock->sk;
1940
1941		write_lock_bh(&sk->sk_callback_lock);
1942
1943		xs_save_old_callbacks(transport, sk);
1944
1945		sk->sk_user_data = xprt;
1946		sk->sk_data_ready = xs_udp_data_ready;
1947		sk->sk_write_space = xs_udp_write_space;
1948		sk->sk_error_report = xs_error_report;
1949		sk->sk_no_check = UDP_CSUM_NORCV;
1950		sk->sk_allocation = GFP_ATOMIC;
1951
1952		xprt_set_connected(xprt);
1953
1954		/* Reset to new socket */
1955		transport->sock = sock;
1956		transport->inet = sk;
1957
1958		write_unlock_bh(&sk->sk_callback_lock);
 
 
1959	}
1960	xs_udp_do_set_buffer_size(xprt);
 
 
1961}
1962
1963static void xs_udp_setup_socket(struct work_struct *work)
1964{
1965	struct sock_xprt *transport =
1966		container_of(work, struct sock_xprt, connect_worker.work);
1967	struct rpc_xprt *xprt = &transport->xprt;
1968	struct socket *sock = transport->sock;
1969	int status = -EIO;
 
1970
1971	if (xprt->shutdown)
1972		goto out;
1973
1974	/* Start by resetting any existing state */
1975	xs_reset_transport(transport);
1976	sock = xs_create_sock(xprt, transport,
1977			xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
 
1978	if (IS_ERR(sock))
1979		goto out;
1980
1981	dprintk("RPC:       worker connecting xprt %p via %s to "
1982				"%s (port %s)\n", xprt,
1983			xprt->address_strings[RPC_DISPLAY_PROTO],
1984			xprt->address_strings[RPC_DISPLAY_ADDR],
1985			xprt->address_strings[RPC_DISPLAY_PORT]);
1986
1987	xs_udp_finish_connecting(xprt, sock);
 
1988	status = 0;
1989out:
1990	xprt_clear_connecting(xprt);
 
1991	xprt_wake_pending_tasks(xprt, status);
 
1992}
1993
1994/*
1995 * We need to preserve the port number so the reply cache on the server can
1996 * find our cached RPC replies when we get around to reconnecting.
 
 
 
1997 */
1998static void xs_abort_connection(struct sock_xprt *transport)
1999{
2000	int result;
2001	struct sockaddr any;
 
2002
2003	dprintk("RPC:       disconnecting xprt %p to reuse port\n", transport);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2004
2005	/*
2006	 * Disconnect the transport socket by doing a connect operation
2007	 * with AF_UNSPEC.  This should return immediately...
2008	 */
2009	memset(&any, 0, sizeof(any));
2010	any.sa_family = AF_UNSPEC;
2011	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
2012	if (!result)
2013		xs_sock_mark_closed(&transport->xprt);
2014	else
2015		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
2016				result);
 
 
 
 
 
 
 
 
 
 
 
 
2017}
2018
2019static void xs_tcp_reuse_connection(struct sock_xprt *transport)
 
 
2020{
2021	unsigned int state = transport->inet->sk_state;
 
 
2022
2023	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2024		/* we don't need to abort the connection if the socket
2025		 * hasn't undergone a shutdown
2026		 */
2027		if (transport->inet->sk_shutdown == 0)
2028			return;
2029		dprintk("RPC:       %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2030				__func__, transport->inet->sk_shutdown);
2031	}
2032	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2033		/* we don't need to abort the connection if the socket
2034		 * hasn't undergone a shutdown
2035		 */
2036		if (transport->inet->sk_shutdown == 0)
2037			return;
2038		dprintk("RPC:       %s: ESTABLISHED/SYN_SENT "
2039				"sk_shutdown set to %d\n",
2040				__func__, transport->inet->sk_shutdown);
2041	}
2042	xs_abort_connection(transport);
 
2043}
2044
2045static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2046{
2047	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2048	int ret = -ENOTCONN;
2049
2050	if (!transport->inet) {
2051		struct sock *sk = sock->sk;
2052
2053		write_lock_bh(&sk->sk_callback_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2054
2055		xs_save_old_callbacks(transport, sk);
2056
2057		sk->sk_user_data = xprt;
2058		sk->sk_data_ready = xs_tcp_data_ready;
2059		sk->sk_state_change = xs_tcp_state_change;
2060		sk->sk_write_space = xs_tcp_write_space;
2061		sk->sk_error_report = xs_error_report;
2062		sk->sk_allocation = GFP_ATOMIC;
2063
2064		/* socket options */
2065		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2066		sock_reset_flag(sk, SOCK_LINGER);
2067		tcp_sk(sk)->linger2 = 0;
2068		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2069
2070		xprt_clear_connected(xprt);
2071
2072		/* Reset to new socket */
2073		transport->sock = sock;
2074		transport->inet = sk;
2075
2076		write_unlock_bh(&sk->sk_callback_lock);
2077	}
2078
2079	if (!xprt_bound(xprt))
2080		goto out;
 
 
 
 
2081
2082	/* Tell the socket layer to start connecting... */
2083	xprt->stat.connect_count++;
2084	xprt->stat.connect_start = jiffies;
2085	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2086	switch (ret) {
2087	case 0:
2088	case -EINPROGRESS:
2089		/* SYN_SENT! */
2090		xprt->connect_cookie++;
2091		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2092			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2093	}
2094out:
2095	return ret;
2096}
2097
2098/**
2099 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2100 * @xprt: RPC transport to connect
2101 * @transport: socket transport to connect
2102 * @create_sock: function to create a socket of the correct type
2103 *
2104 * Invoked by a work queue tasklet.
2105 */
2106static void xs_tcp_setup_socket(struct work_struct *work)
2107{
2108	struct sock_xprt *transport =
2109		container_of(work, struct sock_xprt, connect_worker.work);
2110	struct socket *sock = transport->sock;
2111	struct rpc_xprt *xprt = &transport->xprt;
2112	int status = -EIO;
 
2113
2114	if (xprt->shutdown)
2115		goto out;
2116
2117	if (!sock) {
2118		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
2119		sock = xs_create_sock(xprt, transport,
2120				xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
 
 
 
 
2121		if (IS_ERR(sock)) {
2122			status = PTR_ERR(sock);
2123			goto out;
2124		}
2125	} else {
2126		int abort_and_exit;
2127
2128		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2129				&xprt->state);
2130		/* "close" the socket, preserving the local port */
2131		xs_tcp_reuse_connection(transport);
2132
2133		if (abort_and_exit)
2134			goto out_eagain;
2135	}
2136
2137	dprintk("RPC:       worker connecting xprt %p via %s to "
2138				"%s (port %s)\n", xprt,
2139			xprt->address_strings[RPC_DISPLAY_PROTO],
2140			xprt->address_strings[RPC_DISPLAY_ADDR],
2141			xprt->address_strings[RPC_DISPLAY_PORT]);
2142
2143	status = xs_tcp_finish_connecting(xprt, sock);
 
2144	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
2145			xprt, -status, xprt_connected(xprt),
2146			sock->sk->sk_state);
2147	switch (status) {
2148	default:
2149		printk("%s: connect returned unhandled error %d\n",
2150			__func__, status);
2151	case -EADDRNOTAVAIL:
2152		/* We're probably in TIME_WAIT. Get rid of existing socket,
2153		 * and retry
2154		 */
2155		set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
2156		xprt_force_disconnect(xprt);
2157		break;
2158	case -ECONNREFUSED:
2159	case -ECONNRESET:
2160	case -ENETUNREACH:
2161		/* retry with existing socket, after a delay */
2162	case 0:
2163	case -EINPROGRESS:
 
 
 
 
 
2164	case -EALREADY:
2165		xprt_clear_connecting(xprt);
2166		return;
 
 
 
 
2167	case -EINVAL:
2168		/* Happens, for instance, if the user specified a link
2169		 * local IPv6 address without a scope-id.
2170		 */
2171		goto out;
 
 
 
 
 
 
 
 
 
 
 
2172	}
2173out_eagain:
2174	status = -EAGAIN;
 
 
 
 
2175out:
2176	xprt_clear_connecting(xprt);
2177	xprt_wake_pending_tasks(xprt, status);
 
 
2178}
2179
2180/**
2181 * xs_connect - connect a socket to a remote endpoint
 
2182 * @task: address of RPC task that manages state of connect request
2183 *
2184 * TCP: If the remote end dropped the connection, delay reconnecting.
2185 *
2186 * UDP socket connects are synchronous, but we use a work queue anyway
2187 * to guarantee that even unprivileged user processes can set up a
2188 * socket on a privileged port.
2189 *
2190 * If a UDP socket connect fails, the delay behavior here prevents
2191 * retry floods (hard mounts).
2192 */
2193static void xs_connect(struct rpc_task *task)
2194{
2195	struct rpc_xprt *xprt = task->tk_xprt;
2196	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 
2197
2198	if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
 
 
2199		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
2200				"seconds\n",
2201				xprt, xprt->reestablish_timeout / HZ);
2202		queue_delayed_work(rpciod_workqueue,
2203				   &transport->connect_worker,
2204				   xprt->reestablish_timeout);
2205		xprt->reestablish_timeout <<= 1;
2206		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2207			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2208		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2209			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2210	} else {
2211		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2212		queue_delayed_work(rpciod_workqueue,
2213				   &transport->connect_worker, 0);
2214	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2215}
2216
2217/**
2218 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2219 * @xprt: rpc_xprt struct containing statistics
2220 * @seq: output file
2221 *
2222 */
2223static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2224{
2225	long idle_time = 0;
2226
2227	if (xprt_connected(xprt))
2228		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2229
2230	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2231			"%llu %llu\n",
2232			xprt->stat.bind_count,
2233			xprt->stat.connect_count,
2234			xprt->stat.connect_time,
2235			idle_time,
2236			xprt->stat.sends,
2237			xprt->stat.recvs,
2238			xprt->stat.bad_xids,
2239			xprt->stat.req_u,
2240			xprt->stat.bklog_u);
 
 
 
2241}
2242
2243/**
2244 * xs_udp_print_stats - display UDP socket-specifc stats
2245 * @xprt: rpc_xprt struct containing statistics
2246 * @seq: output file
2247 *
2248 */
2249static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2250{
2251	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2252
2253	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
 
2254			transport->srcport,
2255			xprt->stat.bind_count,
2256			xprt->stat.sends,
2257			xprt->stat.recvs,
2258			xprt->stat.bad_xids,
2259			xprt->stat.req_u,
2260			xprt->stat.bklog_u);
 
 
 
2261}
2262
2263/**
2264 * xs_tcp_print_stats - display TCP socket-specifc stats
2265 * @xprt: rpc_xprt struct containing statistics
2266 * @seq: output file
2267 *
2268 */
2269static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2270{
2271	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2272	long idle_time = 0;
2273
2274	if (xprt_connected(xprt))
2275		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2276
2277	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
 
2278			transport->srcport,
2279			xprt->stat.bind_count,
2280			xprt->stat.connect_count,
2281			xprt->stat.connect_time,
2282			idle_time,
2283			xprt->stat.sends,
2284			xprt->stat.recvs,
2285			xprt->stat.bad_xids,
2286			xprt->stat.req_u,
2287			xprt->stat.bklog_u);
 
 
 
2288}
2289
2290/*
2291 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2292 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2293 * to use the server side send routines.
2294 */
2295static void *bc_malloc(struct rpc_task *task, size_t size)
2296{
 
 
2297	struct page *page;
2298	struct rpc_buffer *buf;
2299
2300	BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2301	page = alloc_page(GFP_KERNEL);
 
 
 
2302
 
2303	if (!page)
2304		return NULL;
2305
2306	buf = page_address(page);
2307	buf->len = PAGE_SIZE;
2308
2309	return buf->data;
 
 
2310}
2311
2312/*
2313 * Free the space allocated in the bc_alloc routine
2314 */
2315static void bc_free(void *buffer)
2316{
 
2317	struct rpc_buffer *buf;
2318
2319	if (!buffer)
2320		return;
2321
2322	buf = container_of(buffer, struct rpc_buffer, data);
2323	free_page((unsigned long)buf);
2324}
2325
2326/*
2327 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2328 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2329 */
2330static int bc_sendto(struct rpc_rqst *req)
2331{
2332	int len;
2333	struct xdr_buf *xbufp = &req->rq_snd_buf;
2334	struct rpc_xprt *xprt = req->rq_xprt;
2335	struct sock_xprt *transport =
2336				container_of(xprt, struct sock_xprt, xprt);
2337	struct socket *sock = transport->sock;
2338	unsigned long headoff;
2339	unsigned long tailoff;
2340
2341	xs_encode_stream_record_marker(xbufp);
2342
2343	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2344	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2345	len = svc_send_common(sock, xbufp,
2346			      virt_to_page(xbufp->head[0].iov_base), headoff,
2347			      xbufp->tail[0].iov_base, tailoff);
2348
2349	if (len != xbufp->len) {
2350		printk(KERN_NOTICE "Error sending entire callback!\n");
2351		len = -EAGAIN;
2352	}
2353
2354	return len;
 
 
 
 
 
 
 
 
2355}
2356
2357/*
2358 * The send routine. Borrows from svc_send
 
 
 
 
 
 
 
 
2359 */
2360static int bc_send_request(struct rpc_task *task)
2361{
2362	struct rpc_rqst *req = task->tk_rqstp;
2363	struct svc_xprt	*xprt;
2364	struct svc_sock         *svsk;
2365	u32                     len;
2366
2367	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2368	/*
2369	 * Get the server socket associated with this callback xprt
2370	 */
2371	xprt = req->rq_xprt->bc_xprt;
2372	svsk = container_of(xprt, struct svc_sock, sk_xprt);
2373
2374	/*
2375	 * Grab the mutex to serialize data as the connection is shared
2376	 * with the fore channel
2377	 */
2378	if (!mutex_trylock(&xprt->xpt_mutex)) {
2379		rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2380		if (!mutex_trylock(&xprt->xpt_mutex))
2381			return -EAGAIN;
2382		rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2383	}
2384	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2385		len = -ENOTCONN;
2386	else
2387		len = bc_sendto(req);
2388	mutex_unlock(&xprt->xpt_mutex);
2389
2390	if (len > 0)
2391		len = 0;
2392
2393	return len;
2394}
2395
2396/*
2397 * The close routine. Since this is client initiated, we do nothing
2398 */
2399
2400static void bc_close(struct rpc_xprt *xprt)
2401{
 
2402}
2403
2404/*
2405 * The xprt destroy routine. Again, because this connection is client
2406 * initiated, we do nothing
2407 */
2408
2409static void bc_destroy(struct rpc_xprt *xprt)
2410{
 
 
 
 
2411}
2412
2413static struct rpc_xprt_ops xs_local_ops = {
2414	.reserve_xprt		= xprt_reserve_xprt,
2415	.release_xprt		= xs_tcp_release_xprt,
 
 
2416	.rpcbind		= xs_local_rpcbind,
2417	.set_port		= xs_local_set_port,
2418	.connect		= xs_connect,
2419	.buf_alloc		= rpc_malloc,
2420	.buf_free		= rpc_free,
 
2421	.send_request		= xs_local_send_request,
2422	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2423	.close			= xs_close,
2424	.destroy		= xs_destroy,
2425	.print_stats		= xs_local_print_stats,
 
 
2426};
2427
2428static struct rpc_xprt_ops xs_udp_ops = {
2429	.set_buffer_size	= xs_udp_set_buffer_size,
2430	.reserve_xprt		= xprt_reserve_xprt_cong,
2431	.release_xprt		= xprt_release_xprt_cong,
 
 
2432	.rpcbind		= rpcb_getport_async,
2433	.set_port		= xs_set_port,
2434	.connect		= xs_connect,
 
 
2435	.buf_alloc		= rpc_malloc,
2436	.buf_free		= rpc_free,
2437	.send_request		= xs_udp_send_request,
2438	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
2439	.timer			= xs_udp_timer,
2440	.release_request	= xprt_release_rqst_cong,
2441	.close			= xs_close,
2442	.destroy		= xs_destroy,
2443	.print_stats		= xs_udp_print_stats,
 
 
 
2444};
2445
2446static struct rpc_xprt_ops xs_tcp_ops = {
2447	.reserve_xprt		= xprt_reserve_xprt,
2448	.release_xprt		= xs_tcp_release_xprt,
 
 
2449	.rpcbind		= rpcb_getport_async,
2450	.set_port		= xs_set_port,
2451	.connect		= xs_connect,
 
 
2452	.buf_alloc		= rpc_malloc,
2453	.buf_free		= rpc_free,
 
2454	.send_request		= xs_tcp_send_request,
2455	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2456	.close			= xs_tcp_close,
2457	.destroy		= xs_destroy,
 
2458	.print_stats		= xs_tcp_print_stats,
 
 
 
 
 
 
 
 
 
 
2459};
2460
2461/*
2462 * The rpc_xprt_ops for the server backchannel
2463 */
2464
2465static struct rpc_xprt_ops bc_tcp_ops = {
2466	.reserve_xprt		= xprt_reserve_xprt,
2467	.release_xprt		= xprt_release_xprt,
 
 
2468	.buf_alloc		= bc_malloc,
2469	.buf_free		= bc_free,
2470	.send_request		= bc_send_request,
2471	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2472	.close			= bc_close,
2473	.destroy		= bc_destroy,
2474	.print_stats		= xs_tcp_print_stats,
 
 
 
2475};
2476
2477static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2478{
2479	static const struct sockaddr_in sin = {
2480		.sin_family		= AF_INET,
2481		.sin_addr.s_addr	= htonl(INADDR_ANY),
2482	};
2483	static const struct sockaddr_in6 sin6 = {
2484		.sin6_family		= AF_INET6,
2485		.sin6_addr		= IN6ADDR_ANY_INIT,
2486	};
2487
2488	switch (family) {
2489	case AF_LOCAL:
2490		break;
2491	case AF_INET:
2492		memcpy(sap, &sin, sizeof(sin));
2493		break;
2494	case AF_INET6:
2495		memcpy(sap, &sin6, sizeof(sin6));
2496		break;
2497	default:
2498		dprintk("RPC:       %s: Bad address family\n", __func__);
2499		return -EAFNOSUPPORT;
2500	}
2501	return 0;
2502}
2503
2504static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2505				      unsigned int slot_table_size,
2506				      unsigned int max_slot_table_size)
2507{
2508	struct rpc_xprt *xprt;
2509	struct sock_xprt *new;
2510
2511	if (args->addrlen > sizeof(xprt->addr)) {
2512		dprintk("RPC:       xs_setup_xprt: address too large\n");
2513		return ERR_PTR(-EBADF);
2514	}
2515
2516	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2517			max_slot_table_size);
2518	if (xprt == NULL) {
2519		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
2520				"rpc_xprt\n");
2521		return ERR_PTR(-ENOMEM);
2522	}
2523
2524	new = container_of(xprt, struct sock_xprt, xprt);
 
2525	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2526	xprt->addrlen = args->addrlen;
2527	if (args->srcaddr)
2528		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2529	else {
2530		int err;
2531		err = xs_init_anyaddr(args->dstaddr->sa_family,
2532					(struct sockaddr *)&new->srcaddr);
2533		if (err != 0)
 
2534			return ERR_PTR(err);
 
2535	}
2536
2537	return xprt;
2538}
2539
2540static const struct rpc_timeout xs_local_default_timeout = {
2541	.to_initval = 10 * HZ,
2542	.to_maxval = 10 * HZ,
2543	.to_retries = 2,
2544};
2545
2546/**
2547 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2548 * @args: rpc transport creation arguments
2549 *
2550 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2551 */
2552static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2553{
2554	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2555	struct sock_xprt *transport;
2556	struct rpc_xprt *xprt;
2557	struct rpc_xprt *ret;
2558
2559	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2560			xprt_max_tcp_slot_table_entries);
2561	if (IS_ERR(xprt))
2562		return xprt;
2563	transport = container_of(xprt, struct sock_xprt, xprt);
2564
2565	xprt->prot = 0;
2566	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2567	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2568
2569	xprt->bind_timeout = XS_BIND_TO;
2570	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2571	xprt->idle_timeout = XS_IDLE_DISC_TO;
2572
2573	xprt->ops = &xs_local_ops;
2574	xprt->timeout = &xs_local_default_timeout;
2575
 
 
 
 
2576	switch (sun->sun_family) {
2577	case AF_LOCAL:
2578		if (sun->sun_path[0] != '/') {
2579			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2580					sun->sun_path);
2581			ret = ERR_PTR(-EINVAL);
2582			goto out_err;
2583		}
2584		xprt_set_bound(xprt);
2585		INIT_DELAYED_WORK(&transport->connect_worker,
2586					xs_local_setup_socket);
2587		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2588		break;
2589	default:
2590		ret = ERR_PTR(-EAFNOSUPPORT);
2591		goto out_err;
2592	}
2593
2594	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2595			xprt->address_strings[RPC_DISPLAY_ADDR]);
2596
2597	if (try_module_get(THIS_MODULE))
2598		return xprt;
2599	ret = ERR_PTR(-EINVAL);
2600out_err:
2601	xprt_free(xprt);
2602	return ret;
2603}
2604
2605static const struct rpc_timeout xs_udp_default_timeout = {
2606	.to_initval = 5 * HZ,
2607	.to_maxval = 30 * HZ,
2608	.to_increment = 5 * HZ,
2609	.to_retries = 5,
2610};
2611
2612/**
2613 * xs_setup_udp - Set up transport to use a UDP socket
2614 * @args: rpc transport creation arguments
2615 *
2616 */
2617static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2618{
2619	struct sockaddr *addr = args->dstaddr;
2620	struct rpc_xprt *xprt;
2621	struct sock_xprt *transport;
2622	struct rpc_xprt *ret;
2623
2624	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2625			xprt_udp_slot_table_entries);
2626	if (IS_ERR(xprt))
2627		return xprt;
2628	transport = container_of(xprt, struct sock_xprt, xprt);
2629
2630	xprt->prot = IPPROTO_UDP;
2631	xprt->tsh_size = 0;
2632	/* XXX: header size can vary due to auth type, IPv6, etc. */
2633	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2634
2635	xprt->bind_timeout = XS_BIND_TO;
2636	xprt->reestablish_timeout = XS_UDP_REEST_TO;
2637	xprt->idle_timeout = XS_IDLE_DISC_TO;
2638
2639	xprt->ops = &xs_udp_ops;
2640
2641	xprt->timeout = &xs_udp_default_timeout;
2642
 
 
 
 
2643	switch (addr->sa_family) {
2644	case AF_INET:
2645		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2646			xprt_set_bound(xprt);
2647
2648		INIT_DELAYED_WORK(&transport->connect_worker,
2649					xs_udp_setup_socket);
2650		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2651		break;
2652	case AF_INET6:
2653		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2654			xprt_set_bound(xprt);
2655
2656		INIT_DELAYED_WORK(&transport->connect_worker,
2657					xs_udp_setup_socket);
2658		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2659		break;
2660	default:
2661		ret = ERR_PTR(-EAFNOSUPPORT);
2662		goto out_err;
2663	}
2664
2665	if (xprt_bound(xprt))
2666		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2667				xprt->address_strings[RPC_DISPLAY_ADDR],
2668				xprt->address_strings[RPC_DISPLAY_PORT],
2669				xprt->address_strings[RPC_DISPLAY_PROTO]);
2670	else
2671		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2672				xprt->address_strings[RPC_DISPLAY_ADDR],
2673				xprt->address_strings[RPC_DISPLAY_PROTO]);
2674
2675	if (try_module_get(THIS_MODULE))
2676		return xprt;
2677	ret = ERR_PTR(-EINVAL);
2678out_err:
2679	xprt_free(xprt);
2680	return ret;
2681}
2682
2683static const struct rpc_timeout xs_tcp_default_timeout = {
2684	.to_initval = 60 * HZ,
2685	.to_maxval = 60 * HZ,
2686	.to_retries = 2,
2687};
2688
2689/**
2690 * xs_setup_tcp - Set up transport to use a TCP socket
2691 * @args: rpc transport creation arguments
2692 *
2693 */
2694static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2695{
2696	struct sockaddr *addr = args->dstaddr;
2697	struct rpc_xprt *xprt;
2698	struct sock_xprt *transport;
2699	struct rpc_xprt *ret;
 
 
 
 
2700
2701	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2702			xprt_max_tcp_slot_table_entries);
2703	if (IS_ERR(xprt))
2704		return xprt;
2705	transport = container_of(xprt, struct sock_xprt, xprt);
2706
2707	xprt->prot = IPPROTO_TCP;
2708	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2709	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2710
2711	xprt->bind_timeout = XS_BIND_TO;
2712	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2713	xprt->idle_timeout = XS_IDLE_DISC_TO;
2714
2715	xprt->ops = &xs_tcp_ops;
2716	xprt->timeout = &xs_tcp_default_timeout;
2717
 
 
 
 
 
 
 
 
2718	switch (addr->sa_family) {
2719	case AF_INET:
2720		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2721			xprt_set_bound(xprt);
2722
2723		INIT_DELAYED_WORK(&transport->connect_worker,
2724					xs_tcp_setup_socket);
2725		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2726		break;
2727	case AF_INET6:
2728		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2729			xprt_set_bound(xprt);
2730
2731		INIT_DELAYED_WORK(&transport->connect_worker,
2732					xs_tcp_setup_socket);
2733		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2734		break;
2735	default:
2736		ret = ERR_PTR(-EAFNOSUPPORT);
2737		goto out_err;
2738	}
2739
2740	if (xprt_bound(xprt))
2741		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2742				xprt->address_strings[RPC_DISPLAY_ADDR],
2743				xprt->address_strings[RPC_DISPLAY_PORT],
2744				xprt->address_strings[RPC_DISPLAY_PROTO]);
2745	else
2746		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2747				xprt->address_strings[RPC_DISPLAY_ADDR],
2748				xprt->address_strings[RPC_DISPLAY_PROTO]);
2749
2750
2751	if (try_module_get(THIS_MODULE))
2752		return xprt;
2753	ret = ERR_PTR(-EINVAL);
2754out_err:
2755	xprt_free(xprt);
2756	return ret;
2757}
2758
2759/**
2760 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2761 * @args: rpc transport creation arguments
2762 *
2763 */
2764static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2765{
2766	struct sockaddr *addr = args->dstaddr;
2767	struct rpc_xprt *xprt;
2768	struct sock_xprt *transport;
2769	struct svc_sock *bc_sock;
2770	struct rpc_xprt *ret;
2771
2772	if (args->bc_xprt->xpt_bc_xprt) {
2773		/*
2774		 * This server connection already has a backchannel
2775		 * export; we can't create a new one, as we wouldn't be
2776		 * able to match replies based on xid any more.  So,
2777		 * reuse the already-existing one:
2778		 */
2779		 return args->bc_xprt->xpt_bc_xprt;
2780	}
2781	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2782			xprt_tcp_slot_table_entries);
2783	if (IS_ERR(xprt))
2784		return xprt;
2785	transport = container_of(xprt, struct sock_xprt, xprt);
2786
2787	xprt->prot = IPPROTO_TCP;
2788	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2789	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2790	xprt->timeout = &xs_tcp_default_timeout;
2791
2792	/* backchannel */
2793	xprt_set_bound(xprt);
2794	xprt->bind_timeout = 0;
2795	xprt->reestablish_timeout = 0;
2796	xprt->idle_timeout = 0;
2797
2798	xprt->ops = &bc_tcp_ops;
2799
2800	switch (addr->sa_family) {
2801	case AF_INET:
2802		xs_format_peer_addresses(xprt, "tcp",
2803					 RPCBIND_NETID_TCP);
2804		break;
2805	case AF_INET6:
2806		xs_format_peer_addresses(xprt, "tcp",
2807				   RPCBIND_NETID_TCP6);
2808		break;
2809	default:
2810		ret = ERR_PTR(-EAFNOSUPPORT);
2811		goto out_err;
2812	}
2813
2814	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2815			xprt->address_strings[RPC_DISPLAY_ADDR],
2816			xprt->address_strings[RPC_DISPLAY_PORT],
2817			xprt->address_strings[RPC_DISPLAY_PROTO]);
2818
2819	/*
2820	 * Once we've associated a backchannel xprt with a connection,
2821	 * we want to keep it around as long as long as the connection
2822	 * lasts, in case we need to start using it for a backchannel
2823	 * again; this reference won't be dropped until bc_xprt is
2824	 * destroyed.
2825	 */
2826	xprt_get(xprt);
2827	args->bc_xprt->xpt_bc_xprt = xprt;
2828	xprt->bc_xprt = args->bc_xprt;
2829	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2830	transport->sock = bc_sock->sk_sock;
2831	transport->inet = bc_sock->sk_sk;
2832
2833	/*
2834	 * Since we don't want connections for the backchannel, we set
2835	 * the xprt status to connected
2836	 */
2837	xprt_set_connected(xprt);
2838
2839
2840	if (try_module_get(THIS_MODULE))
2841		return xprt;
 
 
 
2842	xprt_put(xprt);
2843	ret = ERR_PTR(-EINVAL);
2844out_err:
2845	xprt_free(xprt);
2846	return ret;
2847}
2848
2849static struct xprt_class	xs_local_transport = {
2850	.list		= LIST_HEAD_INIT(xs_local_transport.list),
2851	.name		= "named UNIX socket",
2852	.owner		= THIS_MODULE,
2853	.ident		= XPRT_TRANSPORT_LOCAL,
2854	.setup		= xs_setup_local,
 
2855};
2856
2857static struct xprt_class	xs_udp_transport = {
2858	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2859	.name		= "udp",
2860	.owner		= THIS_MODULE,
2861	.ident		= XPRT_TRANSPORT_UDP,
2862	.setup		= xs_setup_udp,
 
2863};
2864
2865static struct xprt_class	xs_tcp_transport = {
2866	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2867	.name		= "tcp",
2868	.owner		= THIS_MODULE,
2869	.ident		= XPRT_TRANSPORT_TCP,
2870	.setup		= xs_setup_tcp,
 
2871};
2872
2873static struct xprt_class	xs_bc_tcp_transport = {
2874	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2875	.name		= "tcp NFSv4.1 backchannel",
2876	.owner		= THIS_MODULE,
2877	.ident		= XPRT_TRANSPORT_BC_TCP,
2878	.setup		= xs_setup_bc_tcp,
 
2879};
2880
2881/**
2882 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2883 *
2884 */
2885int init_socket_xprt(void)
2886{
2887#ifdef RPC_DEBUG
2888	if (!sunrpc_table_header)
2889		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2890#endif
2891
2892	xprt_register_transport(&xs_local_transport);
2893	xprt_register_transport(&xs_udp_transport);
2894	xprt_register_transport(&xs_tcp_transport);
2895	xprt_register_transport(&xs_bc_tcp_transport);
2896
2897	return 0;
2898}
2899
2900/**
2901 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2902 *
2903 */
2904void cleanup_socket_xprt(void)
2905{
2906#ifdef RPC_DEBUG
2907	if (sunrpc_table_header) {
2908		unregister_sysctl_table(sunrpc_table_header);
2909		sunrpc_table_header = NULL;
2910	}
2911#endif
2912
2913	xprt_unregister_transport(&xs_local_transport);
2914	xprt_unregister_transport(&xs_udp_transport);
2915	xprt_unregister_transport(&xs_tcp_transport);
2916	xprt_unregister_transport(&xs_bc_tcp_transport);
2917}
2918
2919static int param_set_uint_minmax(const char *val,
2920		const struct kernel_param *kp,
2921		unsigned int min, unsigned int max)
2922{
2923	unsigned long num;
2924	int ret;
2925
2926	if (!val)
2927		return -EINVAL;
2928	ret = strict_strtoul(val, 0, &num);
2929	if (ret == -EINVAL || num < min || num > max)
2930		return -EINVAL;
2931	*((unsigned int *)kp->arg) = num;
2932	return 0;
2933}
2934
2935static int param_set_portnr(const char *val, const struct kernel_param *kp)
2936{
2937	return param_set_uint_minmax(val, kp,
2938			RPC_MIN_RESVPORT,
2939			RPC_MAX_RESVPORT);
2940}
2941
2942static struct kernel_param_ops param_ops_portnr = {
2943	.set = param_set_portnr,
2944	.get = param_get_uint,
2945};
2946
2947#define param_check_portnr(name, p) \
2948	__param_check(name, p, unsigned int);
2949
2950module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2951module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2952
2953static int param_set_slot_table_size(const char *val,
2954				     const struct kernel_param *kp)
2955{
2956	return param_set_uint_minmax(val, kp,
2957			RPC_MIN_SLOT_TABLE,
2958			RPC_MAX_SLOT_TABLE);
2959}
2960
2961static struct kernel_param_ops param_ops_slot_table_size = {
2962	.set = param_set_slot_table_size,
2963	.get = param_get_uint,
2964};
2965
2966#define param_check_slot_table_size(name, p) \
2967	__param_check(name, p, unsigned int);
2968
2969static int param_set_max_slot_table_size(const char *val,
2970				     const struct kernel_param *kp)
2971{
2972	return param_set_uint_minmax(val, kp,
2973			RPC_MIN_SLOT_TABLE,
2974			RPC_MAX_SLOT_TABLE_LIMIT);
2975}
2976
2977static struct kernel_param_ops param_ops_max_slot_table_size = {
2978	.set = param_set_max_slot_table_size,
2979	.get = param_get_uint,
2980};
2981
2982#define param_check_max_slot_table_size(name, p) \
2983	__param_check(name, p, unsigned int);
2984
2985module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2986		   slot_table_size, 0644);
2987module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
2988		   max_slot_table_size, 0644);
2989module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2990		   slot_table_size, 0644);
2991
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * linux/net/sunrpc/xprtsock.c
   4 *
   5 * Client-side transport implementation for sockets.
   6 *
   7 * TCP callback races fixes (C) 1998 Red Hat
   8 * TCP send fixes (C) 1998 Red Hat
   9 * TCP NFS related read + write fixes
  10 *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
  11 *
  12 * Rewrite of larges part of the code in order to stabilize TCP stuff.
  13 * Fix behaviour when socket buffer is full.
  14 *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
  15 *
  16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
  17 *
  18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
  19 *   <gilles.quillard@bull.net>
  20 */
  21
  22#include <linux/types.h>
  23#include <linux/string.h>
  24#include <linux/slab.h>
  25#include <linux/module.h>
  26#include <linux/capability.h>
  27#include <linux/pagemap.h>
  28#include <linux/errno.h>
  29#include <linux/socket.h>
  30#include <linux/in.h>
  31#include <linux/net.h>
  32#include <linux/mm.h>
  33#include <linux/un.h>
  34#include <linux/udp.h>
  35#include <linux/tcp.h>
  36#include <linux/sunrpc/clnt.h>
  37#include <linux/sunrpc/addr.h>
  38#include <linux/sunrpc/sched.h>
  39#include <linux/sunrpc/svcsock.h>
  40#include <linux/sunrpc/xprtsock.h>
  41#include <linux/file.h>
  42#ifdef CONFIG_SUNRPC_BACKCHANNEL
  43#include <linux/sunrpc/bc_xprt.h>
  44#endif
  45
  46#include <net/sock.h>
  47#include <net/checksum.h>
  48#include <net/udp.h>
  49#include <net/tcp.h>
  50#include <linux/bvec.h>
  51#include <linux/highmem.h>
  52#include <linux/uio.h>
  53#include <linux/sched/mm.h>
  54
  55#include <trace/events/sunrpc.h>
  56
  57#include "socklib.h"
  58#include "sunrpc.h"
  59
  60static void xs_close(struct rpc_xprt *xprt);
  61static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
  62static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
  63		struct socket *sock);
  64
  65/*
  66 * xprtsock tunables
  67 */
  68static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
  69static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
  70static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
  71
  72static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
  73static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
  74
  75#define XS_TCP_LINGER_TO	(15U * HZ)
  76static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
  77
  78/*
  79 * We can register our own files under /proc/sys/sunrpc by
  80 * calling register_sysctl_table() again.  The files in that
  81 * directory become the union of all files registered there.
  82 *
  83 * We simply need to make sure that we don't collide with
  84 * someone else's file names!
  85 */
  86
 
 
  87static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
  88static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
  89static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
  90static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
  91static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
  92
  93static struct ctl_table_header *sunrpc_table_header;
  94
  95static struct xprt_class xs_local_transport;
  96static struct xprt_class xs_udp_transport;
  97static struct xprt_class xs_tcp_transport;
  98static struct xprt_class xs_bc_tcp_transport;
  99
 100/*
 101 * FIXME: changing the UDP slot table size should also resize the UDP
 102 *        socket buffers for existing UDP transports
 103 */
 104static struct ctl_table xs_tunables_table[] = {
 105	{
 106		.procname	= "udp_slot_table_entries",
 107		.data		= &xprt_udp_slot_table_entries,
 108		.maxlen		= sizeof(unsigned int),
 109		.mode		= 0644,
 110		.proc_handler	= proc_dointvec_minmax,
 111		.extra1		= &min_slot_table_size,
 112		.extra2		= &max_slot_table_size
 113	},
 114	{
 115		.procname	= "tcp_slot_table_entries",
 116		.data		= &xprt_tcp_slot_table_entries,
 117		.maxlen		= sizeof(unsigned int),
 118		.mode		= 0644,
 119		.proc_handler	= proc_dointvec_minmax,
 120		.extra1		= &min_slot_table_size,
 121		.extra2		= &max_slot_table_size
 122	},
 123	{
 124		.procname	= "tcp_max_slot_table_entries",
 125		.data		= &xprt_max_tcp_slot_table_entries,
 126		.maxlen		= sizeof(unsigned int),
 127		.mode		= 0644,
 128		.proc_handler	= proc_dointvec_minmax,
 129		.extra1		= &min_slot_table_size,
 130		.extra2		= &max_tcp_slot_table_limit
 131	},
 132	{
 133		.procname	= "min_resvport",
 134		.data		= &xprt_min_resvport,
 135		.maxlen		= sizeof(unsigned int),
 136		.mode		= 0644,
 137		.proc_handler	= proc_dointvec_minmax,
 138		.extra1		= &xprt_min_resvport_limit,
 139		.extra2		= &xprt_max_resvport_limit
 140	},
 141	{
 142		.procname	= "max_resvport",
 143		.data		= &xprt_max_resvport,
 144		.maxlen		= sizeof(unsigned int),
 145		.mode		= 0644,
 146		.proc_handler	= proc_dointvec_minmax,
 147		.extra1		= &xprt_min_resvport_limit,
 148		.extra2		= &xprt_max_resvport_limit
 149	},
 150	{
 151		.procname	= "tcp_fin_timeout",
 152		.data		= &xs_tcp_fin_timeout,
 153		.maxlen		= sizeof(xs_tcp_fin_timeout),
 154		.mode		= 0644,
 155		.proc_handler	= proc_dointvec_jiffies,
 156	},
 157	{ },
 158};
 159
 160static struct ctl_table sunrpc_table[] = {
 161	{
 162		.procname	= "sunrpc",
 163		.mode		= 0555,
 164		.child		= xs_tunables_table
 165	},
 166	{ },
 167};
 168
 
 
 169/*
 170 * Wait duration for a reply from the RPC portmapper.
 171 */
 172#define XS_BIND_TO		(60U * HZ)
 173
 174/*
 175 * Delay if a UDP socket connect error occurs.  This is most likely some
 176 * kind of resource problem on the local host.
 177 */
 178#define XS_UDP_REEST_TO		(2U * HZ)
 179
 180/*
 181 * The reestablish timeout allows clients to delay for a bit before attempting
 182 * to reconnect to a server that just dropped our connection.
 183 *
 184 * We implement an exponential backoff when trying to reestablish a TCP
 185 * transport connection with the server.  Some servers like to drop a TCP
 186 * connection when they are overworked, so we start with a short timeout and
 187 * increase over time if the server is down or not responding.
 188 */
 189#define XS_TCP_INIT_REEST_TO	(3U * HZ)
 
 190
 191/*
 192 * TCP idle timeout; client drops the transport socket if it is idle
 193 * for this long.  Note that we also timeout UDP sockets to prevent
 194 * holding port numbers when there is no RPC traffic.
 195 */
 196#define XS_IDLE_DISC_TO		(5U * 60 * HZ)
 197
 198#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 199# undef  RPC_DEBUG_DATA
 200# define RPCDBG_FACILITY	RPCDBG_TRANS
 201#endif
 202
 203#ifdef RPC_DEBUG_DATA
 204static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
 205{
 206	u8 *buf = (u8 *) packet;
 207	int j;
 208
 209	dprintk("RPC:       %s\n", msg);
 210	for (j = 0; j < count && j < 128; j += 4) {
 211		if (!(j & 31)) {
 212			if (j)
 213				dprintk("\n");
 214			dprintk("0x%04x ", j);
 215		}
 216		dprintk("%02x%02x%02x%02x ",
 217			buf[j], buf[j+1], buf[j+2], buf[j+3]);
 218	}
 219	dprintk("\n");
 220}
 221#else
 222static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
 223{
 224	/* NOP */
 225}
 226#endif
 227
 228static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
 229{
 230	return (struct rpc_xprt *) sk->sk_user_data;
 231}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 232
 233static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
 234{
 235	return (struct sockaddr *) &xprt->addr;
 236}
 237
 238static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
 239{
 240	return (struct sockaddr_un *) &xprt->addr;
 241}
 242
 243static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
 244{
 245	return (struct sockaddr_in *) &xprt->addr;
 246}
 247
 248static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
 249{
 250	return (struct sockaddr_in6 *) &xprt->addr;
 251}
 252
 253static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
 254{
 255	struct sockaddr *sap = xs_addr(xprt);
 256	struct sockaddr_in6 *sin6;
 257	struct sockaddr_in *sin;
 258	struct sockaddr_un *sun;
 259	char buf[128];
 260
 261	switch (sap->sa_family) {
 262	case AF_LOCAL:
 263		sun = xs_addr_un(xprt);
 264		strscpy(buf, sun->sun_path, sizeof(buf));
 265		xprt->address_strings[RPC_DISPLAY_ADDR] =
 266						kstrdup(buf, GFP_KERNEL);
 267		break;
 268	case AF_INET:
 269		(void)rpc_ntop(sap, buf, sizeof(buf));
 270		xprt->address_strings[RPC_DISPLAY_ADDR] =
 271						kstrdup(buf, GFP_KERNEL);
 272		sin = xs_addr_in(xprt);
 273		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
 274		break;
 275	case AF_INET6:
 276		(void)rpc_ntop(sap, buf, sizeof(buf));
 277		xprt->address_strings[RPC_DISPLAY_ADDR] =
 278						kstrdup(buf, GFP_KERNEL);
 279		sin6 = xs_addr_in6(xprt);
 280		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
 281		break;
 282	default:
 283		BUG();
 284	}
 285
 286	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
 287}
 288
 289static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
 290{
 291	struct sockaddr *sap = xs_addr(xprt);
 292	char buf[128];
 293
 294	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
 295	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
 296
 297	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
 298	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
 299}
 300
 301static void xs_format_peer_addresses(struct rpc_xprt *xprt,
 302				     const char *protocol,
 303				     const char *netid)
 304{
 305	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
 306	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
 307	xs_format_common_peer_addresses(xprt);
 308	xs_format_common_peer_ports(xprt);
 309}
 310
 311static void xs_update_peer_port(struct rpc_xprt *xprt)
 312{
 313	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
 314	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
 315
 316	xs_format_common_peer_ports(xprt);
 317}
 318
 319static void xs_free_peer_addresses(struct rpc_xprt *xprt)
 320{
 321	unsigned int i;
 322
 323	for (i = 0; i < RPC_DISPLAY_MAX; i++)
 324		switch (i) {
 325		case RPC_DISPLAY_PROTO:
 326		case RPC_DISPLAY_NETID:
 327			continue;
 328		default:
 329			kfree(xprt->address_strings[i]);
 330		}
 331}
 332
 333static size_t
 334xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
 335{
 336	size_t i,n;
 337
 338	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
 339		return want;
 340	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
 341	for (i = 0; i < n; i++) {
 342		if (buf->pages[i])
 343			continue;
 344		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
 345		if (!buf->pages[i]) {
 346			i *= PAGE_SIZE;
 347			return i > buf->page_base ? i - buf->page_base : 0;
 348		}
 349	}
 350	return want;
 351}
 352
 353static ssize_t
 354xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
 355{
 356	ssize_t ret;
 357	if (seek != 0)
 358		iov_iter_advance(&msg->msg_iter, seek);
 359	ret = sock_recvmsg(sock, msg, flags);
 360	return ret > 0 ? ret + seek : ret;
 361}
 362
 363static ssize_t
 364xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
 365		struct kvec *kvec, size_t count, size_t seek)
 366{
 367	iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count);
 368	return xs_sock_recvmsg(sock, msg, flags, seek);
 369}
 370
 371static ssize_t
 372xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
 373		struct bio_vec *bvec, unsigned long nr, size_t count,
 374		size_t seek)
 375{
 376	iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count);
 377	return xs_sock_recvmsg(sock, msg, flags, seek);
 378}
 379
 380static ssize_t
 381xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
 382		size_t count)
 383{
 384	iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
 385	return sock_recvmsg(sock, msg, flags);
 386}
 387
 388#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
 389static void
 390xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
 391{
 392	struct bvec_iter bi = {
 393		.bi_size = count,
 394	};
 395	struct bio_vec bv;
 396
 397	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
 398	for_each_bvec(bv, bvec, bi, bi)
 399		flush_dcache_page(bv.bv_page);
 400}
 401#else
 402static inline void
 403xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
 404{
 405}
 406#endif
 407
 408static ssize_t
 409xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
 410		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
 411{
 412	size_t want, seek_init = seek, offset = 0;
 413	ssize_t ret;
 414
 415	want = min_t(size_t, count, buf->head[0].iov_len);
 416	if (seek < want) {
 417		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
 418		if (ret <= 0)
 419			goto sock_err;
 420		offset += ret;
 421		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
 422			goto out;
 423		if (ret != want)
 424			goto out;
 425		seek = 0;
 426	} else {
 427		seek -= want;
 428		offset += want;
 429	}
 430
 431	want = xs_alloc_sparse_pages(
 432		buf, min_t(size_t, count - offset, buf->page_len),
 433		GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
 434	if (seek < want) {
 435		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
 436				xdr_buf_pagecount(buf),
 437				want + buf->page_base,
 438				seek + buf->page_base);
 439		if (ret <= 0)
 440			goto sock_err;
 441		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
 442		ret -= buf->page_base;
 443		offset += ret;
 444		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
 445			goto out;
 446		if (ret != want)
 447			goto out;
 448		seek = 0;
 449	} else {
 450		seek -= want;
 451		offset += want;
 452	}
 453
 454	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
 455	if (seek < want) {
 456		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
 457		if (ret <= 0)
 458			goto sock_err;
 459		offset += ret;
 460		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
 461			goto out;
 462		if (ret != want)
 463			goto out;
 464	} else if (offset < seek_init)
 465		offset = seek_init;
 466	ret = -EMSGSIZE;
 467out:
 468	*read = offset - seek_init;
 469	return ret;
 470sock_err:
 471	offset += seek;
 472	goto out;
 473}
 474
 475static void
 476xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
 477{
 478	if (!transport->recv.copied) {
 479		if (buf->head[0].iov_len >= transport->recv.offset)
 480			memcpy(buf->head[0].iov_base,
 481					&transport->recv.xid,
 482					transport->recv.offset);
 483		transport->recv.copied = transport->recv.offset;
 484	}
 485}
 486
 487static bool
 488xs_read_stream_request_done(struct sock_xprt *transport)
 489{
 490	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
 491}
 492
 493static void
 494xs_read_stream_check_eor(struct sock_xprt *transport,
 495		struct msghdr *msg)
 496{
 497	if (xs_read_stream_request_done(transport))
 498		msg->msg_flags |= MSG_EOR;
 499}
 500
 501static ssize_t
 502xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
 503		int flags, struct rpc_rqst *req)
 504{
 505	struct xdr_buf *buf = &req->rq_private_buf;
 506	size_t want, read;
 507	ssize_t ret;
 508
 509	xs_read_header(transport, buf);
 510
 511	want = transport->recv.len - transport->recv.offset;
 512	if (want != 0) {
 513		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
 514				transport->recv.copied + want,
 515				transport->recv.copied,
 516				&read);
 517		transport->recv.offset += read;
 518		transport->recv.copied += read;
 519	}
 520
 521	if (transport->recv.offset == transport->recv.len)
 522		xs_read_stream_check_eor(transport, msg);
 523
 524	if (want == 0)
 525		return 0;
 526
 527	switch (ret) {
 528	default:
 529		break;
 530	case -EFAULT:
 531	case -EMSGSIZE:
 532		msg->msg_flags |= MSG_TRUNC;
 533		return read;
 534	case 0:
 535		return -ESHUTDOWN;
 536	}
 537	return ret < 0 ? ret : read;
 538}
 539
 540static size_t
 541xs_read_stream_headersize(bool isfrag)
 542{
 543	if (isfrag)
 544		return sizeof(__be32);
 545	return 3 * sizeof(__be32);
 546}
 547
 548static ssize_t
 549xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
 550		int flags, size_t want, size_t seek)
 551{
 552	struct kvec kvec = {
 553		.iov_base = &transport->recv.fraghdr,
 554		.iov_len = want,
 555	};
 556	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
 557}
 558
 559#if defined(CONFIG_SUNRPC_BACKCHANNEL)
 560static ssize_t
 561xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
 562{
 563	struct rpc_xprt *xprt = &transport->xprt;
 564	struct rpc_rqst *req;
 565	ssize_t ret;
 566
 567	/* Is this transport associated with the backchannel? */
 568	if (!xprt->bc_serv)
 569		return -ESHUTDOWN;
 570
 571	/* Look up and lock the request corresponding to the given XID */
 572	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
 573	if (!req) {
 574		printk(KERN_WARNING "Callback slot table overflowed\n");
 575		return -ESHUTDOWN;
 
 
 
 
 
 
 
 
 
 
 
 576	}
 577	if (transport->recv.copied && !req->rq_private_buf.len)
 578		return -ESHUTDOWN;
 579
 580	ret = xs_read_stream_request(transport, msg, flags, req);
 581	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
 582		xprt_complete_bc_request(req, transport->recv.copied);
 583	else
 584		req->rq_private_buf.len = transport->recv.copied;
 585
 586	return ret;
 587}
 588#else /* CONFIG_SUNRPC_BACKCHANNEL */
 589static ssize_t
 590xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
 591{
 592	return -ESHUTDOWN;
 593}
 594#endif /* CONFIG_SUNRPC_BACKCHANNEL */
 595
 596static ssize_t
 597xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
 
 
 
 
 
 
 
 
 598{
 599	struct rpc_xprt *xprt = &transport->xprt;
 600	struct rpc_rqst *req;
 601	ssize_t ret = 0;
 602
 603	/* Look up and lock the request corresponding to the given XID */
 604	spin_lock(&xprt->queue_lock);
 605	req = xprt_lookup_rqst(xprt, transport->recv.xid);
 606	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
 607		msg->msg_flags |= MSG_TRUNC;
 608		goto out;
 609	}
 610	xprt_pin_rqst(req);
 611	spin_unlock(&xprt->queue_lock);
 612
 613	ret = xs_read_stream_request(transport, msg, flags, req);
 614
 615	spin_lock(&xprt->queue_lock);
 616	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
 617		xprt_complete_rqst(req->rq_task, transport->recv.copied);
 618	else
 619		req->rq_private_buf.len = transport->recv.copied;
 620	xprt_unpin_rqst(req);
 621out:
 622	spin_unlock(&xprt->queue_lock);
 623	return ret;
 624}
 625
 626static ssize_t
 627xs_read_stream(struct sock_xprt *transport, int flags)
 628{
 629	struct msghdr msg = { 0 };
 630	size_t want, read = 0;
 631	ssize_t ret = 0;
 632
 633	if (transport->recv.len == 0) {
 634		want = xs_read_stream_headersize(transport->recv.copied != 0);
 635		ret = xs_read_stream_header(transport, &msg, flags, want,
 636				transport->recv.offset);
 637		if (ret <= 0)
 638			goto out_err;
 639		transport->recv.offset = ret;
 640		if (transport->recv.offset != want)
 641			return transport->recv.offset;
 642		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
 643			RPC_FRAGMENT_SIZE_MASK;
 644		transport->recv.offset -= sizeof(transport->recv.fraghdr);
 645		read = ret;
 646	}
 647
 648	switch (be32_to_cpu(transport->recv.calldir)) {
 649	default:
 650		msg.msg_flags |= MSG_TRUNC;
 651		break;
 652	case RPC_CALL:
 653		ret = xs_read_stream_call(transport, &msg, flags);
 654		break;
 655	case RPC_REPLY:
 656		ret = xs_read_stream_reply(transport, &msg, flags);
 657	}
 658	if (msg.msg_flags & MSG_TRUNC) {
 659		transport->recv.calldir = cpu_to_be32(-1);
 660		transport->recv.copied = -1;
 661	}
 662	if (ret < 0)
 663		goto out_err;
 664	read += ret;
 665	if (transport->recv.offset < transport->recv.len) {
 666		if (!(msg.msg_flags & MSG_TRUNC))
 667			return read;
 668		msg.msg_flags = 0;
 669		ret = xs_read_discard(transport->sock, &msg, flags,
 670				transport->recv.len - transport->recv.offset);
 671		if (ret <= 0)
 672			goto out_err;
 673		transport->recv.offset += ret;
 674		read += ret;
 675		if (transport->recv.offset != transport->recv.len)
 676			return read;
 677	}
 678	if (xs_read_stream_request_done(transport)) {
 679		trace_xs_stream_read_request(transport);
 680		transport->recv.copied = 0;
 681	}
 682	transport->recv.offset = 0;
 683	transport->recv.len = 0;
 684	return read;
 685out_err:
 686	return ret != 0 ? ret : -ESHUTDOWN;
 687}
 688
 689static __poll_t xs_poll_socket(struct sock_xprt *transport)
 690{
 691	return transport->sock->ops->poll(transport->file, transport->sock,
 692			NULL);
 693}
 694
 695static bool xs_poll_socket_readable(struct sock_xprt *transport)
 696{
 697	__poll_t events = xs_poll_socket(transport);
 698
 699	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
 700}
 701
 702static void xs_poll_check_readable(struct sock_xprt *transport)
 703{
 704
 705	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
 706	if (!xs_poll_socket_readable(transport))
 707		return;
 708	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
 709		queue_work(xprtiod_workqueue, &transport->recv_worker);
 710}
 711
 712static void xs_stream_data_receive(struct sock_xprt *transport)
 713{
 714	size_t read = 0;
 715	ssize_t ret = 0;
 716
 717	mutex_lock(&transport->recv_mutex);
 718	if (transport->sock == NULL)
 719		goto out;
 720	for (;;) {
 721		ret = xs_read_stream(transport, MSG_DONTWAIT);
 722		if (ret < 0)
 723			break;
 724		read += ret;
 725		cond_resched();
 726	}
 727	if (ret == -ESHUTDOWN)
 728		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
 729	else
 730		xs_poll_check_readable(transport);
 731out:
 732	mutex_unlock(&transport->recv_mutex);
 733	trace_xs_stream_read_data(&transport->xprt, ret, read);
 734}
 735
 736static void xs_stream_data_receive_workfn(struct work_struct *work)
 737{
 738	struct sock_xprt *transport =
 739		container_of(work, struct sock_xprt, recv_worker);
 740	unsigned int pflags = memalloc_nofs_save();
 741
 742	xs_stream_data_receive(transport);
 743	memalloc_nofs_restore(pflags);
 744}
 745
 746static void
 747xs_stream_reset_connect(struct sock_xprt *transport)
 748{
 749	transport->recv.offset = 0;
 750	transport->recv.len = 0;
 751	transport->recv.copied = 0;
 752	transport->xmit.offset = 0;
 753}
 754
 755static void
 756xs_stream_start_connect(struct sock_xprt *transport)
 757{
 758	transport->xprt.stat.connect_count++;
 759	transport->xprt.stat.connect_start = jiffies;
 760}
 761
 762#define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
 763
 764/**
 765 * xs_nospace - handle transmit was incomplete
 766 * @req: pointer to RPC request
 767 * @transport: pointer to struct sock_xprt
 768 *
 769 */
 770static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
 771{
 772	struct rpc_xprt *xprt = &transport->xprt;
 773	struct sock *sk = transport->inet;
 774	int ret = -EAGAIN;
 
 775
 776	trace_rpc_socket_nospace(req, transport);
 
 
 777
 778	/* Protect against races with write_space */
 779	spin_lock(&xprt->transport_lock);
 780
 781	/* Don't race with disconnect */
 782	if (xprt_connected(xprt)) {
 783		/* wait for more buffer space */
 784		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
 785		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
 786		sk->sk_write_pending++;
 787		xprt_wait_for_buffer_space(xprt);
 788	} else
 
 
 
 
 
 
 
 789		ret = -ENOTCONN;
 
 790
 791	spin_unlock(&xprt->transport_lock);
 792	return ret;
 793}
 794
 795static int xs_sock_nospace(struct rpc_rqst *req)
 796{
 797	struct sock_xprt *transport =
 798		container_of(req->rq_xprt, struct sock_xprt, xprt);
 799	struct sock *sk = transport->inet;
 800	int ret = -EAGAIN;
 801
 802	lock_sock(sk);
 803	if (!sock_writeable(sk))
 804		ret = xs_nospace(req, transport);
 805	release_sock(sk);
 806	return ret;
 807}
 808
 809static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
 810{
 811	struct sock_xprt *transport =
 812		container_of(req->rq_xprt, struct sock_xprt, xprt);
 813	struct sock *sk = transport->inet;
 814	int ret = -EAGAIN;
 815
 816	if (vm_wait)
 817		return -ENOBUFS;
 818	lock_sock(sk);
 819	if (!sk_stream_memory_free(sk))
 820		ret = xs_nospace(req, transport);
 821	release_sock(sk);
 822	return ret;
 823}
 824
 825static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf)
 826{
 827	return xdr_alloc_bvec(buf, rpc_task_gfp_mask());
 828}
 829
 830/*
 831 * Determine if the previous message in the stream was aborted before it
 832 * could complete transmission.
 833 */
 834static bool
 835xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
 836{
 837	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
 838}
 839
 840/*
 841 * Return the stream record marker field for a record of length < 2^31-1
 842 */
 843static rpc_fraghdr
 844xs_stream_record_marker(struct xdr_buf *xdr)
 845{
 846	if (!xdr->len)
 847		return 0;
 848	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
 849}
 850
 851/**
 852 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
 853 * @req: pointer to RPC request
 854 *
 855 * Return values:
 856 *        0:	The request has been sent
 857 *   EAGAIN:	The socket was blocked, please call again later to
 858 *		complete the request
 859 * ENOTCONN:	Caller needs to invoke connect logic then call again
 860 *    other:	Some other error occurred, the request was not sent
 861 */
 862static int xs_local_send_request(struct rpc_rqst *req)
 863{
 
 864	struct rpc_xprt *xprt = req->rq_xprt;
 865	struct sock_xprt *transport =
 866				container_of(xprt, struct sock_xprt, xprt);
 867	struct xdr_buf *xdr = &req->rq_snd_buf;
 868	rpc_fraghdr rm = xs_stream_record_marker(xdr);
 869	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
 870	struct msghdr msg = {
 871		.msg_flags	= XS_SENDMSG_FLAGS,
 872	};
 873	bool vm_wait;
 874	unsigned int sent;
 875	int status;
 876
 877	/* Close the stream if the previous transmission was incomplete */
 878	if (xs_send_request_was_aborted(transport, req)) {
 879		xprt_force_disconnect(xprt);
 880		return -ENOTCONN;
 881	}
 882
 883	xs_pktdump("packet data:",
 884			req->rq_svec->iov_base, req->rq_svec->iov_len);
 885
 886	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
 887
 888	req->rq_xtime = ktime_get();
 889	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
 890				   transport->xmit.offset, rm, &sent);
 891	dprintk("RPC:       %s(%u) = %d\n",
 892			__func__, xdr->len - transport->xmit.offset, status);
 893
 894	if (likely(sent > 0) || status == 0) {
 895		transport->xmit.offset += sent;
 896		req->rq_bytes_sent = transport->xmit.offset;
 897		if (likely(req->rq_bytes_sent >= msglen)) {
 898			req->rq_xmit_bytes_sent += transport->xmit.offset;
 899			transport->xmit.offset = 0;
 900			return 0;
 901		}
 902		status = -EAGAIN;
 903		vm_wait = false;
 904	}
 905
 906	switch (status) {
 907	case -EAGAIN:
 908		status = xs_stream_nospace(req, vm_wait);
 909		break;
 910	default:
 911		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
 912			-status);
 913		fallthrough;
 914	case -EPIPE:
 915		xprt_force_disconnect(xprt);
 916		status = -ENOTCONN;
 917	}
 918
 919	return status;
 920}
 921
 922/**
 923 * xs_udp_send_request - write an RPC request to a UDP socket
 924 * @req: pointer to RPC request
 925 *
 926 * Return values:
 927 *        0:	The request has been sent
 928 *   EAGAIN:	The socket was blocked, please call again later to
 929 *		complete the request
 930 * ENOTCONN:	Caller needs to invoke connect logic then call again
 931 *    other:	Some other error occurred, the request was not sent
 932 */
 933static int xs_udp_send_request(struct rpc_rqst *req)
 934{
 
 935	struct rpc_xprt *xprt = req->rq_xprt;
 936	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 937	struct xdr_buf *xdr = &req->rq_snd_buf;
 938	struct msghdr msg = {
 939		.msg_name	= xs_addr(xprt),
 940		.msg_namelen	= xprt->addrlen,
 941		.msg_flags	= XS_SENDMSG_FLAGS,
 942	};
 943	unsigned int sent;
 944	int status;
 945
 946	xs_pktdump("packet data:",
 947				req->rq_svec->iov_base,
 948				req->rq_svec->iov_len);
 949
 950	if (!xprt_bound(xprt))
 951		return -ENOTCONN;
 952
 953	if (!xprt_request_get_cong(xprt, req))
 954		return -EBADSLT;
 955
 956	status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
 957	if (status < 0)
 958		return status;
 959	req->rq_xtime = ktime_get();
 960	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
 961
 962	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
 963			xdr->len, status);
 964
 965	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
 966	if (status == -EPERM)
 967		goto process_status;
 968
 969	if (status == -EAGAIN && sock_writeable(transport->inet))
 970		status = -ENOBUFS;
 971
 972	if (sent > 0 || status == 0) {
 973		req->rq_xmit_bytes_sent += sent;
 974		if (sent >= req->rq_slen)
 975			return 0;
 976		/* Still some bytes left; set up for a retry later. */
 977		status = -EAGAIN;
 978	}
 979
 980process_status:
 981	switch (status) {
 982	case -ENOTSOCK:
 983		status = -ENOTCONN;
 984		/* Should we call xs_close() here? */
 985		break;
 986	case -EAGAIN:
 987		status = xs_sock_nospace(req);
 988		break;
 
 
 
 989	case -ENETUNREACH:
 990	case -ENOBUFS:
 991	case -EPIPE:
 992	case -ECONNREFUSED:
 993	case -EPERM:
 994		/* When the server has died, an ICMP port unreachable message
 995		 * prompts ECONNREFUSED. */
 996		break;
 997	default:
 998		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
 999			-status);
1000	}
1001
1002	return status;
1003}
1004
1005/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1006 * xs_tcp_send_request - write an RPC request to a TCP socket
1007 * @req: pointer to RPC request
1008 *
1009 * Return values:
1010 *        0:	The request has been sent
1011 *   EAGAIN:	The socket was blocked, please call again later to
1012 *		complete the request
1013 * ENOTCONN:	Caller needs to invoke connect logic then call again
1014 *    other:	Some other error occurred, the request was not sent
1015 *
1016 * XXX: In the case of soft timeouts, should we eventually give up
1017 *	if sendmsg is not able to make progress?
1018 */
1019static int xs_tcp_send_request(struct rpc_rqst *req)
1020{
 
1021	struct rpc_xprt *xprt = req->rq_xprt;
1022	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1023	struct xdr_buf *xdr = &req->rq_snd_buf;
1024	rpc_fraghdr rm = xs_stream_record_marker(xdr);
1025	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
1026	struct msghdr msg = {
1027		.msg_flags	= XS_SENDMSG_FLAGS,
1028	};
1029	bool vm_wait;
1030	unsigned int sent;
1031	int status;
1032
1033	/* Close the stream if the previous transmission was incomplete */
1034	if (xs_send_request_was_aborted(transport, req)) {
1035		if (transport->sock != NULL)
1036			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1037		return -ENOTCONN;
1038	}
1039	if (!transport->inet)
1040		return -ENOTCONN;
1041
1042	xs_pktdump("packet data:",
1043				req->rq_svec->iov_base,
1044				req->rq_svec->iov_len);
1045
1046	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1047		xs_tcp_set_socket_timeouts(xprt, transport->sock);
1048
1049	xs_set_srcport(transport, transport->sock);
1050
1051	/* Continue transmitting the packet/record. We must be careful
1052	 * to cope with writespace callbacks arriving _after_ we have
1053	 * called sendmsg(). */
1054	req->rq_xtime = ktime_get();
1055	tcp_sock_set_cork(transport->inet, true);
 
1056
1057	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
 
1058
1059	do {
1060		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
1061					   transport->xmit.offset, rm, &sent);
1062
1063		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
1064				xdr->len - transport->xmit.offset, status);
1065
1066		/* If we've sent the entire packet, immediately
1067		 * reset the count of bytes sent. */
1068		transport->xmit.offset += sent;
1069		req->rq_bytes_sent = transport->xmit.offset;
1070		if (likely(req->rq_bytes_sent >= msglen)) {
1071			req->rq_xmit_bytes_sent += transport->xmit.offset;
1072			transport->xmit.offset = 0;
1073			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1074				tcp_sock_set_cork(transport->inet, false);
1075			return 0;
1076		}
1077
1078		WARN_ON_ONCE(sent == 0 && status == 0);
1079
1080		if (sent > 0)
1081			vm_wait = false;
1082
1083	} while (status == 0);
1084
1085	switch (status) {
1086	case -ENOTSOCK:
1087		status = -ENOTCONN;
1088		/* Should we call xs_close() here? */
1089		break;
1090	case -EAGAIN:
1091		status = xs_stream_nospace(req, vm_wait);
1092		break;
 
 
 
1093	case -ECONNRESET:
 
 
1094	case -ECONNREFUSED:
1095	case -ENOTCONN:
1096	case -EADDRINUSE:
1097	case -ENOBUFS:
1098	case -EPIPE:
1099		break;
1100	default:
1101		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
1102			-status);
1103	}
1104
1105	return status;
1106}
1107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1108static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1109{
1110	transport->old_data_ready = sk->sk_data_ready;
1111	transport->old_state_change = sk->sk_state_change;
1112	transport->old_write_space = sk->sk_write_space;
1113	transport->old_error_report = sk->sk_error_report;
1114}
1115
1116static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1117{
1118	sk->sk_data_ready = transport->old_data_ready;
1119	sk->sk_state_change = transport->old_state_change;
1120	sk->sk_write_space = transport->old_write_space;
1121	sk->sk_error_report = transport->old_error_report;
1122}
1123
1124static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1125{
1126	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1127
1128	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1129	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
1130	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
1131	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
1132	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
1133}
1134
1135static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
1136{
1137	set_bit(nr, &transport->sock_state);
1138	queue_work(xprtiod_workqueue, &transport->error_worker);
1139}
1140
1141static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1142{
1143	xprt->connect_cookie++;
1144	smp_mb__before_atomic();
1145	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1146	clear_bit(XPRT_CLOSING, &xprt->state);
1147	xs_sock_reset_state_flags(xprt);
1148	smp_mb__after_atomic();
1149}
1150
1151/**
1152 * xs_error_report - callback to handle TCP socket state errors
1153 * @sk: socket
1154 *
1155 * Note: we don't call sock_error() since there may be a rpc_task
1156 * using the socket, and so we don't want to clear sk->sk_err.
1157 */
1158static void xs_error_report(struct sock *sk)
1159{
1160	struct sock_xprt *transport;
1161	struct rpc_xprt *xprt;
1162
1163	if (!(xprt = xprt_from_sock(sk)))
1164		return;
1165
1166	transport = container_of(xprt, struct sock_xprt, xprt);
1167	transport->xprt_err = -sk->sk_err;
1168	if (transport->xprt_err == 0)
1169		return;
1170	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1171			xprt, -transport->xprt_err);
1172	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1173
1174	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1175	smp_mb__before_atomic();
1176	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
1177}
1178
1179static void xs_reset_transport(struct sock_xprt *transport)
1180{
1181	struct socket *sock = transport->sock;
1182	struct sock *sk = transport->inet;
1183	struct rpc_xprt *xprt = &transport->xprt;
1184	struct file *filp = transport->file;
1185
1186	if (sk == NULL)
1187		return;
1188	/*
1189	 * Make sure we're calling this in a context from which it is safe
1190	 * to call __fput_sync(). In practice that means rpciod and the
1191	 * system workqueue.
1192	 */
1193	if (!(current->flags & PF_WQ_WORKER)) {
1194		WARN_ON_ONCE(1);
1195		set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1196		return;
1197	}
1198
1199	if (atomic_read(&transport->xprt.swapper))
1200		sk_clear_memalloc(sk);
1201
1202	kernel_sock_shutdown(sock, SHUT_RDWR);
1203
1204	mutex_lock(&transport->recv_mutex);
1205	lock_sock(sk);
1206	transport->inet = NULL;
1207	transport->sock = NULL;
1208	transport->file = NULL;
1209
1210	sk->sk_user_data = NULL;
1211
1212	xs_restore_old_callbacks(transport, sk);
1213	xprt_clear_connected(xprt);
1214	xs_sock_reset_connection_flags(xprt);
1215	/* Reset stream record info */
1216	xs_stream_reset_connect(transport);
1217	release_sock(sk);
1218	mutex_unlock(&transport->recv_mutex);
1219
1220	trace_rpc_socket_close(xprt, sock);
1221	__fput_sync(filp);
1222
1223	xprt_disconnect_done(xprt);
1224}
1225
1226/**
1227 * xs_close - close a socket
1228 * @xprt: transport
1229 *
1230 * This is used when all requests are complete; ie, no DRC state remains
1231 * on the server we want to save.
1232 *
1233 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1234 * xs_reset_transport() zeroing the socket from underneath a writer.
1235 */
1236static void xs_close(struct rpc_xprt *xprt)
1237{
1238	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1239
1240	dprintk("RPC:       xs_close xprt %p\n", xprt);
1241
1242	xs_reset_transport(transport);
1243	xprt->reestablish_timeout = 0;
1244}
1245
1246static void xs_inject_disconnect(struct rpc_xprt *xprt)
1247{
1248	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
1249		xprt);
 
1250	xprt_disconnect_done(xprt);
1251}
1252
1253static void xs_xprt_free(struct rpc_xprt *xprt)
1254{
1255	xs_free_peer_addresses(xprt);
1256	xprt_free(xprt);
 
 
1257}
1258
1259/**
1260 * xs_destroy - prepare to shutdown a transport
1261 * @xprt: doomed transport
1262 *
1263 */
1264static void xs_destroy(struct rpc_xprt *xprt)
1265{
1266	struct sock_xprt *transport = container_of(xprt,
1267			struct sock_xprt, xprt);
1268	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
1269
1270	cancel_delayed_work_sync(&transport->connect_worker);
 
1271	xs_close(xprt);
1272	cancel_work_sync(&transport->recv_worker);
1273	cancel_work_sync(&transport->error_worker);
1274	xs_xprt_free(xprt);
1275	module_put(THIS_MODULE);
1276}
1277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1278/**
1279 * xs_udp_data_read_skb - receive callback for UDP sockets
1280 * @xprt: transport
1281 * @sk: socket
1282 * @skb: skbuff
1283 *
 
1284 */
1285static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1286		struct sock *sk,
1287		struct sk_buff *skb)
1288{
1289	struct rpc_task *task;
 
1290	struct rpc_rqst *rovr;
1291	int repsize, copied;
 
1292	u32 _xid;
1293	__be32 *xp;
1294
1295	repsize = skb->len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1296	if (repsize < 4) {
1297		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1298		return;
1299	}
1300
1301	/* Copy the XID from the skb... */
1302	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
 
1303	if (xp == NULL)
1304		return;
1305
1306	/* Look up and lock the request corresponding to the given XID */
1307	spin_lock(&xprt->queue_lock);
1308	rovr = xprt_lookup_rqst(xprt, *xp);
1309	if (!rovr)
1310		goto out_unlock;
1311	xprt_pin_rqst(rovr);
1312	xprt_update_rtt(rovr->rq_task);
1313	spin_unlock(&xprt->queue_lock);
1314	task = rovr->rq_task;
1315
1316	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1317		copied = repsize;
1318
1319	/* Suck it into the iovec, verify checksum if not done by hw. */
1320	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1321		spin_lock(&xprt->queue_lock);
1322		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1323		goto out_unpin;
1324	}
1325
 
 
 
 
1326
1327	spin_lock(&xprt->transport_lock);
1328	xprt_adjust_cwnd(xprt, task, copied);
1329	spin_unlock(&xprt->transport_lock);
1330	spin_lock(&xprt->queue_lock);
1331	xprt_complete_rqst(task, copied);
1332	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1333out_unpin:
1334	xprt_unpin_rqst(rovr);
1335 out_unlock:
1336	spin_unlock(&xprt->queue_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1337}
1338
1339static void xs_udp_data_receive(struct sock_xprt *transport)
 
 
1340{
1341	struct sk_buff *skb;
1342	struct sock *sk;
1343	int err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1344
1345	mutex_lock(&transport->recv_mutex);
1346	sk = transport->inet;
1347	if (sk == NULL)
1348		goto out;
1349	for (;;) {
1350		skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
1351		if (skb == NULL)
1352			break;
1353		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1354		consume_skb(skb);
1355		cond_resched();
1356	}
1357	xs_poll_check_readable(transport);
1358out:
1359	mutex_unlock(&transport->recv_mutex);
1360}
1361
1362static void xs_udp_data_receive_workfn(struct work_struct *work)
 
 
 
 
 
1363{
1364	struct sock_xprt *transport =
1365		container_of(work, struct sock_xprt, recv_worker);
1366	unsigned int pflags = memalloc_nofs_save();
 
 
1367
1368	xs_udp_data_receive(transport);
1369	memalloc_nofs_restore(pflags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1370}
1371
1372/**
1373 * xs_data_ready - "data ready" callback for sockets
1374 * @sk: socket with data to read
1375 *
 
 
 
1376 */
1377static void xs_data_ready(struct sock *sk)
 
1378{
1379	struct rpc_xprt *xprt;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1380
1381	xprt = xprt_from_sock(sk);
1382	if (xprt != NULL) {
1383		struct sock_xprt *transport = container_of(xprt,
1384				struct sock_xprt, xprt);
1385
1386		trace_xs_data_ready(xprt);
1387
1388		transport->old_data_ready(sk);
1389		/* Any data means we had a useful conversation, so
1390		 * then we don't need to delay the next reconnect
1391		 */
1392		if (xprt->reestablish_timeout)
1393			xprt->reestablish_timeout = 0;
1394		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1395			queue_work(xprtiod_workqueue, &transport->recv_worker);
 
1396	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1397}
 
1398
1399/*
1400 * Helper function to force a TCP close if the server is sending
1401 * junk and/or it has put us in CLOSE_WAIT
1402 */
1403static void xs_tcp_force_close(struct rpc_xprt *xprt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1404{
1405	xprt_force_disconnect(xprt);
 
 
 
 
 
 
 
 
 
1406}
1407
1408#if defined(CONFIG_SUNRPC_BACKCHANNEL)
1409static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1410{
1411	return PAGE_SIZE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1412}
1413#endif /* CONFIG_SUNRPC_BACKCHANNEL */
1414
1415/**
1416 * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1417 * @sk: socket whose state has changed
 
1418 *
1419 */
1420static void xs_local_state_change(struct sock *sk)
1421{
1422	struct rpc_xprt *xprt;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1423	struct sock_xprt *transport;
1424
1425	if (!(xprt = xprt_from_sock(sk)))
1426		return;
 
 
 
 
 
 
 
 
 
 
1427	transport = container_of(xprt, struct sock_xprt, xprt);
1428	if (sk->sk_shutdown & SHUTDOWN_MASK) {
1429		clear_bit(XPRT_CONNECTED, &xprt->state);
1430		/* Trigger the socket release */
1431		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1432	}
 
 
 
 
 
 
 
 
 
 
 
1433}
1434
1435/**
1436 * xs_tcp_state_change - callback to handle TCP socket state changes
1437 * @sk: socket whose state has changed
1438 *
1439 */
1440static void xs_tcp_state_change(struct sock *sk)
1441{
1442	struct rpc_xprt *xprt;
1443	struct sock_xprt *transport;
1444
 
1445	if (!(xprt = xprt_from_sock(sk)))
1446		return;
1447	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1448	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1449			sk->sk_state, xprt_connected(xprt),
1450			sock_flag(sk, SOCK_DEAD),
1451			sock_flag(sk, SOCK_ZAPPED),
1452			sk->sk_shutdown);
1453
1454	transport = container_of(xprt, struct sock_xprt, xprt);
1455	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1456	switch (sk->sk_state) {
1457	case TCP_ESTABLISHED:
 
1458		if (!xprt_test_and_set_connected(xprt)) {
1459			xprt->connect_cookie++;
1460			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1461			xprt_clear_connecting(xprt);
1462
1463			xprt->stat.connect_count++;
1464			xprt->stat.connect_time += (long)jiffies -
1465						   xprt->stat.connect_start;
1466			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
 
 
 
1467		}
 
1468		break;
1469	case TCP_FIN_WAIT1:
1470		/* The client initiated a shutdown of the socket */
1471		xprt->connect_cookie++;
1472		xprt->reestablish_timeout = 0;
1473		set_bit(XPRT_CLOSING, &xprt->state);
1474		smp_mb__before_atomic();
1475		clear_bit(XPRT_CONNECTED, &xprt->state);
1476		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1477		smp_mb__after_atomic();
 
1478		break;
1479	case TCP_CLOSE_WAIT:
1480		/* The server initiated a shutdown of the socket */
 
1481		xprt->connect_cookie++;
1482		clear_bit(XPRT_CONNECTED, &xprt->state);
1483		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1484		fallthrough;
1485	case TCP_CLOSING:
1486		/*
1487		 * If the server closed down the connection, make sure that
1488		 * we back off before reconnecting
1489		 */
1490		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1491			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1492		break;
1493	case TCP_LAST_ACK:
1494		set_bit(XPRT_CLOSING, &xprt->state);
1495		smp_mb__before_atomic();
 
1496		clear_bit(XPRT_CONNECTED, &xprt->state);
1497		smp_mb__after_atomic();
1498		break;
1499	case TCP_CLOSE:
1500		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1501					&transport->sock_state))
1502			xprt_clear_connecting(xprt);
1503		clear_bit(XPRT_CLOSING, &xprt->state);
1504		/* Trigger the socket release */
1505		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1506	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1507}
1508
1509static void xs_write_space(struct sock *sk)
1510{
1511	struct sock_xprt *transport;
1512	struct rpc_xprt *xprt;
1513
1514	if (!sk->sk_socket)
1515		return;
1516	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1517
1518	if (unlikely(!(xprt = xprt_from_sock(sk))))
1519		return;
1520	transport = container_of(xprt, struct sock_xprt, xprt);
1521	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
1522		return;
1523	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1524	sk->sk_write_pending--;
1525}
1526
1527/**
1528 * xs_udp_write_space - callback invoked when socket buffer space
1529 *                             becomes available
1530 * @sk: socket whose state has changed
1531 *
1532 * Called when more output buffer space is available for this socket.
1533 * We try not to wake our writers until they can make "significant"
1534 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1535 * with a bunch of small requests.
1536 */
1537static void xs_udp_write_space(struct sock *sk)
1538{
 
 
1539	/* from net/core/sock.c:sock_def_write_space */
1540	if (sock_writeable(sk))
1541		xs_write_space(sk);
 
 
1542}
1543
1544/**
1545 * xs_tcp_write_space - callback invoked when socket buffer space
1546 *                             becomes available
1547 * @sk: socket whose state has changed
1548 *
1549 * Called when more output buffer space is available for this socket.
1550 * We try not to wake our writers until they can make "significant"
1551 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1552 * with a bunch of small requests.
1553 */
1554static void xs_tcp_write_space(struct sock *sk)
1555{
 
 
1556	/* from net/core/stream.c:sk_stream_write_space */
1557	if (sk_stream_is_writeable(sk))
1558		xs_write_space(sk);
 
 
1559}
1560
1561static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1562{
1563	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1564	struct sock *sk = transport->inet;
1565
1566	if (transport->rcvsize) {
1567		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1568		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1569	}
1570	if (transport->sndsize) {
1571		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1572		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1573		sk->sk_write_space(sk);
1574	}
1575}
1576
1577/**
1578 * xs_udp_set_buffer_size - set send and receive limits
1579 * @xprt: generic transport
1580 * @sndsize: requested size of send buffer, in bytes
1581 * @rcvsize: requested size of receive buffer, in bytes
1582 *
1583 * Set socket send and receive buffer size limits.
1584 */
1585static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1586{
1587	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1588
1589	transport->sndsize = 0;
1590	if (sndsize)
1591		transport->sndsize = sndsize + 1024;
1592	transport->rcvsize = 0;
1593	if (rcvsize)
1594		transport->rcvsize = rcvsize + 1024;
1595
1596	xs_udp_do_set_buffer_size(xprt);
1597}
1598
1599/**
1600 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1601 * @xprt: controlling transport
1602 * @task: task that timed out
1603 *
1604 * Adjust the congestion window after a retransmit timeout has occurred.
1605 */
1606static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
1607{
1608	spin_lock(&xprt->transport_lock);
1609	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1610	spin_unlock(&xprt->transport_lock);
1611}
1612
1613static int xs_get_random_port(void)
1614{
1615	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1616	unsigned short range;
1617	unsigned short rand;
1618
1619	if (max < min)
1620		return -EADDRINUSE;
1621	range = max - min + 1;
1622	rand = get_random_u32_below(range);
1623	return rand + min;
1624}
1625
1626static unsigned short xs_sock_getport(struct socket *sock)
1627{
1628	struct sockaddr_storage buf;
1629	unsigned short port = 0;
1630
1631	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
1632		goto out;
1633	switch (buf.ss_family) {
1634	case AF_INET6:
1635		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1636		break;
1637	case AF_INET:
1638		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1639	}
1640out:
1641	return port;
1642}
1643
1644/**
1645 * xs_set_port - reset the port number in the remote endpoint address
1646 * @xprt: generic transport
1647 * @port: new port number
1648 *
1649 */
1650static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1651{
1652	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1653
1654	rpc_set_port(xs_addr(xprt), port);
1655	xs_update_peer_port(xprt);
1656}
1657
1658static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1659{
1660	if (transport->srcport == 0 && transport->xprt.reuseport)
1661		transport->srcport = xs_sock_getport(sock);
1662}
1663
1664static int xs_get_srcport(struct sock_xprt *transport)
1665{
1666	int port = transport->srcport;
1667
1668	if (port == 0 && transport->xprt.resvport)
1669		port = xs_get_random_port();
1670	return port;
1671}
1672
1673static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1674{
1675	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1676	unsigned short ret = 0;
1677	mutex_lock(&sock->recv_mutex);
1678	if (sock->sock)
1679		ret = xs_sock_getport(sock->sock);
1680	mutex_unlock(&sock->recv_mutex);
1681	return ret;
1682}
1683
1684static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1685{
1686	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1687	union {
1688		struct sockaddr sa;
1689		struct sockaddr_storage st;
1690	} saddr;
1691	int ret = -ENOTCONN;
1692
1693	mutex_lock(&sock->recv_mutex);
1694	if (sock->sock) {
1695		ret = kernel_getsockname(sock->sock, &saddr.sa);
1696		if (ret >= 0)
1697			ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1698	}
1699	mutex_unlock(&sock->recv_mutex);
1700	return ret;
1701}
1702
1703static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1704{
1705	if (transport->srcport != 0)
1706		transport->srcport = 0;
1707	if (!transport->xprt.resvport)
1708		return 0;
1709	if (port <= xprt_min_resvport || port > xprt_max_resvport)
1710		return xprt_max_resvport;
1711	return --port;
1712}
1713static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1714{
1715	struct sockaddr_storage myaddr;
1716	int err, nloop = 0;
1717	int port = xs_get_srcport(transport);
1718	unsigned short last;
1719
1720	/*
1721	 * If we are asking for any ephemeral port (i.e. port == 0 &&
1722	 * transport->xprt.resvport == 0), don't bind.  Let the local
1723	 * port selection happen implicitly when the socket is used
1724	 * (for example at connect time).
1725	 *
1726	 * This ensures that we can continue to establish TCP
1727	 * connections even when all local ephemeral ports are already
1728	 * a part of some TCP connection.  This makes no difference
1729	 * for UDP sockets, but also doesn't harm them.
1730	 *
1731	 * If we're asking for any reserved port (i.e. port == 0 &&
1732	 * transport->xprt.resvport == 1) xs_get_srcport above will
1733	 * ensure that port is non-zero and we will bind as needed.
1734	 */
1735	if (port <= 0)
1736		return port;
1737
1738	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1739	do {
1740		rpc_set_port((struct sockaddr *)&myaddr, port);
1741		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1742				transport->xprt.addrlen);
 
 
1743		if (err == 0) {
1744			if (transport->xprt.reuseport)
1745				transport->srcport = port;
1746			break;
1747		}
1748		last = port;
1749		port = xs_next_srcport(transport, port);
1750		if (port > last)
1751			nloop++;
1752	} while (err == -EADDRINUSE && nloop != 2);
1753
1754	if (myaddr.ss_family == AF_INET)
1755		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1756				&((struct sockaddr_in *)&myaddr)->sin_addr,
1757				port, err ? "failed" : "ok", err);
1758	else
1759		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1760				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1761				port, err ? "failed" : "ok", err);
1762	return err;
1763}
1764
1765/*
1766 * We don't support autobind on AF_LOCAL sockets
1767 */
1768static void xs_local_rpcbind(struct rpc_task *task)
1769{
1770	xprt_set_bound(task->tk_xprt);
1771}
1772
1773static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1774{
1775}
1776
1777#ifdef CONFIG_DEBUG_LOCK_ALLOC
1778static struct lock_class_key xs_key[3];
1779static struct lock_class_key xs_slock_key[3];
1780
1781static inline void xs_reclassify_socketu(struct socket *sock)
1782{
1783	struct sock *sk = sock->sk;
1784
 
1785	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1786		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1787}
1788
1789static inline void xs_reclassify_socket4(struct socket *sock)
1790{
1791	struct sock *sk = sock->sk;
1792
 
1793	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1794		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1795}
1796
1797static inline void xs_reclassify_socket6(struct socket *sock)
1798{
1799	struct sock *sk = sock->sk;
1800
 
1801	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1802		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1803}
1804
1805static inline void xs_reclassify_socket(int family, struct socket *sock)
1806{
1807	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
1808		return;
1809
1810	switch (family) {
1811	case AF_LOCAL:
1812		xs_reclassify_socketu(sock);
1813		break;
1814	case AF_INET:
1815		xs_reclassify_socket4(sock);
1816		break;
1817	case AF_INET6:
1818		xs_reclassify_socket6(sock);
1819		break;
1820	}
1821}
1822#else
1823static inline void xs_reclassify_socket(int family, struct socket *sock)
 
 
 
 
 
 
 
 
1824{
1825}
1826#endif
1827
1828static void xs_dummy_setup_socket(struct work_struct *work)
1829{
1830}
 
1831
1832static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1833		struct sock_xprt *transport, int family, int type,
1834		int protocol, bool reuseport)
1835{
1836	struct file *filp;
1837	struct socket *sock;
1838	int err;
1839
1840	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1841	if (err < 0) {
1842		dprintk("RPC:       can't create %d transport socket (%d).\n",
1843				protocol, -err);
1844		goto out;
1845	}
1846	xs_reclassify_socket(family, sock);
1847
1848	if (reuseport)
1849		sock_set_reuseport(sock->sk);
1850
1851	err = xs_bind(transport, sock);
1852	if (err) {
1853		sock_release(sock);
1854		goto out;
1855	}
1856
1857	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1858	if (IS_ERR(filp))
1859		return ERR_CAST(filp);
1860	transport->file = filp;
1861
1862	return sock;
1863out:
1864	return ERR_PTR(err);
1865}
1866
1867static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1868				      struct socket *sock)
1869{
1870	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1871									xprt);
1872
1873	if (!transport->inet) {
1874		struct sock *sk = sock->sk;
1875
1876		lock_sock(sk);
1877
1878		xs_save_old_callbacks(transport, sk);
1879
1880		sk->sk_user_data = xprt;
1881		sk->sk_data_ready = xs_data_ready;
1882		sk->sk_write_space = xs_udp_write_space;
1883		sk->sk_state_change = xs_local_state_change;
1884		sk->sk_error_report = xs_error_report;
1885		sk->sk_use_task_frag = false;
1886
1887		xprt_clear_connected(xprt);
1888
1889		/* Reset to new socket */
1890		transport->sock = sock;
1891		transport->inet = sk;
1892
1893		release_sock(sk);
1894	}
1895
1896	xs_stream_start_connect(transport);
1897
 
1898	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1899}
1900
1901/**
1902 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
 
1903 * @transport: socket transport to connect
 
 
 
1904 */
1905static int xs_local_setup_socket(struct sock_xprt *transport)
1906{
 
 
1907	struct rpc_xprt *xprt = &transport->xprt;
1908	struct file *filp;
1909	struct socket *sock;
1910	int status;
 
 
 
1911
 
1912	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1913					SOCK_STREAM, 0, &sock, 1);
1914	if (status < 0) {
1915		dprintk("RPC:       can't create AF_LOCAL "
1916			"transport socket (%d).\n", -status);
1917		goto out;
1918	}
1919	xs_reclassify_socket(AF_LOCAL, sock);
1920
1921	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1922	if (IS_ERR(filp)) {
1923		status = PTR_ERR(filp);
1924		goto out;
1925	}
1926	transport->file = filp;
1927
1928	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1929			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1930
1931	status = xs_local_finish_connecting(xprt, sock);
1932	trace_rpc_socket_connect(xprt, sock, status);
1933	switch (status) {
1934	case 0:
1935		dprintk("RPC:       xprt %p connected to %s\n",
1936				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1937		xprt->stat.connect_count++;
1938		xprt->stat.connect_time += (long)jiffies -
1939					   xprt->stat.connect_start;
1940		xprt_set_connected(xprt);
1941		break;
1942	case -ENOBUFS:
1943		break;
1944	case -ENOENT:
1945		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1946				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1947		break;
1948	case -ECONNREFUSED:
1949		dprintk("RPC:       xprt %p: connection refused for %s\n",
1950				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1951		break;
1952	default:
1953		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1954				__func__, -status,
1955				xprt->address_strings[RPC_DISPLAY_ADDR]);
1956	}
1957
1958out:
1959	xprt_clear_connecting(xprt);
1960	xprt_wake_pending_tasks(xprt, status);
1961	return status;
1962}
1963
1964static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1965{
1966	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1967	int ret;
1968
1969	if (transport->file)
1970		goto force_disconnect;
1971
1972	if (RPC_IS_ASYNC(task)) {
1973		/*
1974		 * We want the AF_LOCAL connect to be resolved in the
1975		 * filesystem namespace of the process making the rpc
1976		 * call.  Thus we connect synchronously.
1977		 *
1978		 * If we want to support asynchronous AF_LOCAL calls,
1979		 * we'll need to figure out how to pass a namespace to
1980		 * connect.
1981		 */
1982		rpc_task_set_rpc_status(task, -ENOTCONN);
1983		goto out_wake;
1984	}
1985	ret = xs_local_setup_socket(transport);
1986	if (ret && !RPC_IS_SOFTCONN(task))
1987		msleep_interruptible(15000);
1988	return;
1989force_disconnect:
1990	xprt_force_disconnect(xprt);
1991out_wake:
1992	xprt_clear_connecting(xprt);
1993	xprt_wake_pending_tasks(xprt, -ENOTCONN);
1994}
1995
1996#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1997/*
1998 * Note that this should be called with XPRT_LOCKED held, or recv_mutex
1999 * held, or when we otherwise know that we have exclusive access to the
2000 * socket, to guard against races with xs_reset_transport.
2001 */
2002static void xs_set_memalloc(struct rpc_xprt *xprt)
2003{
2004	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2005			xprt);
2006
2007	/*
2008	 * If there's no sock, then we have nothing to set. The
2009	 * reconnecting process will get it for us.
2010	 */
2011	if (!transport->inet)
2012		return;
2013	if (atomic_read(&xprt->swapper))
2014		sk_set_memalloc(transport->inet);
2015}
2016
2017/**
2018 * xs_enable_swap - Tag this transport as being used for swap.
2019 * @xprt: transport to tag
2020 *
2021 * Take a reference to this transport on behalf of the rpc_clnt, and
2022 * optionally mark it for swapping if it wasn't already.
2023 */
2024static int
2025xs_enable_swap(struct rpc_xprt *xprt)
2026{
2027	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2028
2029	mutex_lock(&xs->recv_mutex);
2030	if (atomic_inc_return(&xprt->swapper) == 1 &&
2031	    xs->inet)
2032		sk_set_memalloc(xs->inet);
2033	mutex_unlock(&xs->recv_mutex);
2034	return 0;
2035}
2036
2037/**
2038 * xs_disable_swap - Untag this transport as being used for swap.
2039 * @xprt: transport to tag
2040 *
2041 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2042 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2043 */
2044static void
2045xs_disable_swap(struct rpc_xprt *xprt)
2046{
2047	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2048
2049	mutex_lock(&xs->recv_mutex);
2050	if (atomic_dec_and_test(&xprt->swapper) &&
2051	    xs->inet)
2052		sk_clear_memalloc(xs->inet);
2053	mutex_unlock(&xs->recv_mutex);
2054}
2055#else
2056static void xs_set_memalloc(struct rpc_xprt *xprt)
2057{
2058}
2059
2060static int
2061xs_enable_swap(struct rpc_xprt *xprt)
2062{
2063	return -EINVAL;
2064}
2065
2066static void
2067xs_disable_swap(struct rpc_xprt *xprt)
2068{
2069}
2070#endif
2071
2072static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2073{
2074	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2075
2076	if (!transport->inet) {
2077		struct sock *sk = sock->sk;
2078
2079		lock_sock(sk);
2080
2081		xs_save_old_callbacks(transport, sk);
2082
2083		sk->sk_user_data = xprt;
2084		sk->sk_data_ready = xs_data_ready;
2085		sk->sk_write_space = xs_udp_write_space;
2086		sk->sk_use_task_frag = false;
 
 
2087
2088		xprt_set_connected(xprt);
2089
2090		/* Reset to new socket */
2091		transport->sock = sock;
2092		transport->inet = sk;
2093
2094		xs_set_memalloc(xprt);
2095
2096		release_sock(sk);
2097	}
2098	xs_udp_do_set_buffer_size(xprt);
2099
2100	xprt->stat.connect_start = jiffies;
2101}
2102
2103static void xs_udp_setup_socket(struct work_struct *work)
2104{
2105	struct sock_xprt *transport =
2106		container_of(work, struct sock_xprt, connect_worker.work);
2107	struct rpc_xprt *xprt = &transport->xprt;
2108	struct socket *sock;
2109	int status = -EIO;
2110	unsigned int pflags = current->flags;
2111
2112	if (atomic_read(&xprt->swapper))
2113		current->flags |= PF_MEMALLOC;
 
 
 
2114	sock = xs_create_sock(xprt, transport,
2115			xs_addr(xprt)->sa_family, SOCK_DGRAM,
2116			IPPROTO_UDP, false);
2117	if (IS_ERR(sock))
2118		goto out;
2119
2120	dprintk("RPC:       worker connecting xprt %p via %s to "
2121				"%s (port %s)\n", xprt,
2122			xprt->address_strings[RPC_DISPLAY_PROTO],
2123			xprt->address_strings[RPC_DISPLAY_ADDR],
2124			xprt->address_strings[RPC_DISPLAY_PORT]);
2125
2126	xs_udp_finish_connecting(xprt, sock);
2127	trace_rpc_socket_connect(xprt, sock, 0);
2128	status = 0;
2129out:
2130	xprt_clear_connecting(xprt);
2131	xprt_unlock_connect(xprt, transport);
2132	xprt_wake_pending_tasks(xprt, status);
2133	current_restore_flags(pflags, PF_MEMALLOC);
2134}
2135
2136/**
2137 * xs_tcp_shutdown - gracefully shut down a TCP socket
2138 * @xprt: transport
2139 *
2140 * Initiates a graceful shutdown of the TCP socket by calling the
2141 * equivalent of shutdown(SHUT_RDWR);
2142 */
2143static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2144{
2145	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2146	struct socket *sock = transport->sock;
2147	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
2148
2149	if (sock == NULL)
2150		return;
2151	if (!xprt->reuseport) {
2152		xs_close(xprt);
2153		return;
2154	}
2155	switch (skst) {
2156	case TCP_FIN_WAIT1:
2157	case TCP_FIN_WAIT2:
2158		break;
2159	case TCP_ESTABLISHED:
2160	case TCP_CLOSE_WAIT:
2161		kernel_sock_shutdown(sock, SHUT_RDWR);
2162		trace_rpc_socket_shutdown(xprt, sock);
2163		break;
2164	default:
2165		xs_reset_transport(transport);
2166	}
2167}
2168
2169static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2170		struct socket *sock)
2171{
2172	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2173	unsigned int keepidle;
2174	unsigned int keepcnt;
2175	unsigned int timeo;
2176
2177	spin_lock(&xprt->transport_lock);
2178	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2179	keepcnt = xprt->timeout->to_retries + 1;
2180	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2181		(xprt->timeout->to_retries + 1);
2182	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2183	spin_unlock(&xprt->transport_lock);
2184
2185	/* TCP Keepalive options */
2186	sock_set_keepalive(sock->sk);
2187	tcp_sock_set_keepidle(sock->sk, keepidle);
2188	tcp_sock_set_keepintvl(sock->sk, keepidle);
2189	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2190
2191	/* TCP user timeout (see RFC5482) */
2192	tcp_sock_set_user_timeout(sock->sk, timeo);
2193}
2194
2195static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2196		unsigned long connect_timeout,
2197		unsigned long reconnect_timeout)
2198{
2199	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2200	struct rpc_timeout to;
2201	unsigned long initval;
2202
2203	spin_lock(&xprt->transport_lock);
2204	if (reconnect_timeout < xprt->max_reconnect_timeout)
2205		xprt->max_reconnect_timeout = reconnect_timeout;
2206	if (connect_timeout < xprt->connect_timeout) {
2207		memcpy(&to, xprt->timeout, sizeof(to));
2208		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2209		/* Arbitrary lower limit */
2210		if (initval <  XS_TCP_INIT_REEST_TO << 1)
2211			initval = XS_TCP_INIT_REEST_TO << 1;
2212		to.to_initval = initval;
2213		to.to_maxval = initval;
2214		memcpy(&transport->tcp_timeout, &to,
2215				sizeof(transport->tcp_timeout));
2216		xprt->timeout = &transport->tcp_timeout;
2217		xprt->connect_timeout = connect_timeout;
 
 
 
2218	}
2219	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2220	spin_unlock(&xprt->transport_lock);
2221}
2222
2223static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2224{
2225	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
 
2226
2227	if (!transport->inet) {
2228		struct sock *sk = sock->sk;
2229
2230		/* Avoid temporary address, they are bad for long-lived
2231		 * connections such as NFS mounts.
2232		 * RFC4941, section 3.6 suggests that:
2233		 *    Individual applications, which have specific
2234		 *    knowledge about the normal duration of connections,
2235		 *    MAY override this as appropriate.
2236		 */
2237		if (xs_addr(xprt)->sa_family == PF_INET6) {
2238			ip6_sock_set_addr_preferences(sk,
2239				IPV6_PREFER_SRC_PUBLIC);
2240		}
2241
2242		xs_tcp_set_socket_timeouts(xprt, sock);
2243		tcp_sock_set_nodelay(sk);
2244
2245		lock_sock(sk);
2246
2247		xs_save_old_callbacks(transport, sk);
2248
2249		sk->sk_user_data = xprt;
2250		sk->sk_data_ready = xs_data_ready;
2251		sk->sk_state_change = xs_tcp_state_change;
2252		sk->sk_write_space = xs_tcp_write_space;
2253		sk->sk_error_report = xs_error_report;
2254		sk->sk_use_task_frag = false;
2255
2256		/* socket options */
 
2257		sock_reset_flag(sk, SOCK_LINGER);
 
 
2258
2259		xprt_clear_connected(xprt);
2260
2261		/* Reset to new socket */
2262		transport->sock = sock;
2263		transport->inet = sk;
2264
2265		release_sock(sk);
2266	}
2267
2268	if (!xprt_bound(xprt))
2269		return -ENOTCONN;
2270
2271	xs_set_memalloc(xprt);
2272
2273	xs_stream_start_connect(transport);
2274
2275	/* Tell the socket layer to start connecting... */
2276	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2277	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
 
 
 
 
 
 
 
 
 
 
 
2278}
2279
2280/**
2281 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2282 * @work: queued work item
 
 
2283 *
2284 * Invoked by a work queue tasklet.
2285 */
2286static void xs_tcp_setup_socket(struct work_struct *work)
2287{
2288	struct sock_xprt *transport =
2289		container_of(work, struct sock_xprt, connect_worker.work);
2290	struct socket *sock = transport->sock;
2291	struct rpc_xprt *xprt = &transport->xprt;
2292	int status;
2293	unsigned int pflags = current->flags;
2294
2295	if (atomic_read(&xprt->swapper))
2296		current->flags |= PF_MEMALLOC;
2297
2298	if (xprt_connected(xprt))
2299		goto out;
2300	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
2301			       &transport->sock_state) ||
2302	    !sock) {
2303		xs_reset_transport(transport);
2304		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
2305				      SOCK_STREAM, IPPROTO_TCP, true);
2306		if (IS_ERR(sock)) {
2307			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
2308			goto out;
2309		}
 
 
 
 
 
 
 
 
 
 
2310	}
2311
2312	dprintk("RPC:       worker connecting xprt %p via %s to "
2313				"%s (port %s)\n", xprt,
2314			xprt->address_strings[RPC_DISPLAY_PROTO],
2315			xprt->address_strings[RPC_DISPLAY_ADDR],
2316			xprt->address_strings[RPC_DISPLAY_PORT]);
2317
2318	status = xs_tcp_finish_connecting(xprt, sock);
2319	trace_rpc_socket_connect(xprt, sock, status);
2320	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
2321			xprt, -status, xprt_connected(xprt),
2322			sock->sk->sk_state);
2323	switch (status) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2324	case 0:
2325	case -EINPROGRESS:
2326		/* SYN_SENT! */
2327		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2328		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2329			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2330		fallthrough;
2331	case -EALREADY:
2332		goto out_unlock;
2333	case -EADDRNOTAVAIL:
2334		/* Source port number is unavailable. Try a new one! */
2335		transport->srcport = 0;
2336		status = -EAGAIN;
2337		break;
2338	case -EINVAL:
2339		/* Happens, for instance, if the user specified a link
2340		 * local IPv6 address without a scope-id.
2341		 */
2342	case -ECONNREFUSED:
2343	case -ECONNRESET:
2344	case -ENETDOWN:
2345	case -ENETUNREACH:
2346	case -EHOSTUNREACH:
2347	case -EADDRINUSE:
2348	case -ENOBUFS:
2349		break;
2350	default:
2351		printk("%s: connect returned unhandled error %d\n",
2352			__func__, status);
2353		status = -EAGAIN;
2354	}
2355
2356	/* xs_tcp_force_close() wakes tasks with a fixed error code.
2357	 * We need to wake them first to ensure the correct error code.
2358	 */
2359	xprt_wake_pending_tasks(xprt, status);
2360	xs_tcp_force_close(xprt);
2361out:
2362	xprt_clear_connecting(xprt);
2363out_unlock:
2364	xprt_unlock_connect(xprt, transport);
2365	current_restore_flags(pflags, PF_MEMALLOC);
2366}
2367
2368/**
2369 * xs_connect - connect a socket to a remote endpoint
2370 * @xprt: pointer to transport structure
2371 * @task: address of RPC task that manages state of connect request
2372 *
2373 * TCP: If the remote end dropped the connection, delay reconnecting.
2374 *
2375 * UDP socket connects are synchronous, but we use a work queue anyway
2376 * to guarantee that even unprivileged user processes can set up a
2377 * socket on a privileged port.
2378 *
2379 * If a UDP socket connect fails, the delay behavior here prevents
2380 * retry floods (hard mounts).
2381 */
2382static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2383{
 
2384	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2385	unsigned long delay = 0;
2386
2387	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2388
2389	if (transport->sock != NULL) {
2390		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
2391			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
2392
2393		delay = xprt_reconnect_delay(xprt);
2394		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
2395
2396	} else
 
 
 
 
 
2397		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2398
2399	queue_delayed_work(xprtiod_workqueue,
2400			&transport->connect_worker,
2401			delay);
2402}
2403
2404static void xs_wake_disconnect(struct sock_xprt *transport)
2405{
2406	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2407		xs_tcp_force_close(&transport->xprt);
2408}
2409
2410static void xs_wake_write(struct sock_xprt *transport)
2411{
2412	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2413		xprt_write_space(&transport->xprt);
2414}
2415
2416static void xs_wake_error(struct sock_xprt *transport)
2417{
2418	int sockerr;
2419
2420	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2421		return;
2422	mutex_lock(&transport->recv_mutex);
2423	if (transport->sock == NULL)
2424		goto out;
2425	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2426		goto out;
2427	sockerr = xchg(&transport->xprt_err, 0);
2428	if (sockerr < 0)
2429		xprt_wake_pending_tasks(&transport->xprt, sockerr);
2430out:
2431	mutex_unlock(&transport->recv_mutex);
2432}
2433
2434static void xs_wake_pending(struct sock_xprt *transport)
2435{
2436	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2437		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2438}
2439
2440static void xs_error_handle(struct work_struct *work)
2441{
2442	struct sock_xprt *transport = container_of(work,
2443			struct sock_xprt, error_worker);
2444
2445	xs_wake_disconnect(transport);
2446	xs_wake_write(transport);
2447	xs_wake_error(transport);
2448	xs_wake_pending(transport);
2449}
2450
2451/**
2452 * xs_local_print_stats - display AF_LOCAL socket-specific stats
2453 * @xprt: rpc_xprt struct containing statistics
2454 * @seq: output file
2455 *
2456 */
2457static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2458{
2459	long idle_time = 0;
2460
2461	if (xprt_connected(xprt))
2462		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2463
2464	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2465			"%llu %llu %lu %llu %llu\n",
2466			xprt->stat.bind_count,
2467			xprt->stat.connect_count,
2468			xprt->stat.connect_time / HZ,
2469			idle_time,
2470			xprt->stat.sends,
2471			xprt->stat.recvs,
2472			xprt->stat.bad_xids,
2473			xprt->stat.req_u,
2474			xprt->stat.bklog_u,
2475			xprt->stat.max_slots,
2476			xprt->stat.sending_u,
2477			xprt->stat.pending_u);
2478}
2479
2480/**
2481 * xs_udp_print_stats - display UDP socket-specific stats
2482 * @xprt: rpc_xprt struct containing statistics
2483 * @seq: output file
2484 *
2485 */
2486static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2487{
2488	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2489
2490	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2491			"%lu %llu %llu\n",
2492			transport->srcport,
2493			xprt->stat.bind_count,
2494			xprt->stat.sends,
2495			xprt->stat.recvs,
2496			xprt->stat.bad_xids,
2497			xprt->stat.req_u,
2498			xprt->stat.bklog_u,
2499			xprt->stat.max_slots,
2500			xprt->stat.sending_u,
2501			xprt->stat.pending_u);
2502}
2503
2504/**
2505 * xs_tcp_print_stats - display TCP socket-specific stats
2506 * @xprt: rpc_xprt struct containing statistics
2507 * @seq: output file
2508 *
2509 */
2510static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2511{
2512	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2513	long idle_time = 0;
2514
2515	if (xprt_connected(xprt))
2516		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2517
2518	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2519			"%llu %llu %lu %llu %llu\n",
2520			transport->srcport,
2521			xprt->stat.bind_count,
2522			xprt->stat.connect_count,
2523			xprt->stat.connect_time / HZ,
2524			idle_time,
2525			xprt->stat.sends,
2526			xprt->stat.recvs,
2527			xprt->stat.bad_xids,
2528			xprt->stat.req_u,
2529			xprt->stat.bklog_u,
2530			xprt->stat.max_slots,
2531			xprt->stat.sending_u,
2532			xprt->stat.pending_u);
2533}
2534
2535/*
2536 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2537 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2538 * to use the server side send routines.
2539 */
2540static int bc_malloc(struct rpc_task *task)
2541{
2542	struct rpc_rqst *rqst = task->tk_rqstp;
2543	size_t size = rqst->rq_callsize;
2544	struct page *page;
2545	struct rpc_buffer *buf;
2546
2547	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2548		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2549			  size);
2550		return -EINVAL;
2551	}
2552
2553	page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
2554	if (!page)
2555		return -ENOMEM;
2556
2557	buf = page_address(page);
2558	buf->len = PAGE_SIZE;
2559
2560	rqst->rq_buffer = buf->data;
2561	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
2562	return 0;
2563}
2564
2565/*
2566 * Free the space allocated in the bc_alloc routine
2567 */
2568static void bc_free(struct rpc_task *task)
2569{
2570	void *buffer = task->tk_rqstp->rq_buffer;
2571	struct rpc_buffer *buf;
2572
 
 
 
2573	buf = container_of(buffer, struct rpc_buffer, data);
2574	free_page((unsigned long)buf);
2575}
2576
 
 
 
 
2577static int bc_sendto(struct rpc_rqst *req)
2578{
2579	struct xdr_buf *xdr = &req->rq_snd_buf;
 
 
2580	struct sock_xprt *transport =
2581			container_of(req->rq_xprt, struct sock_xprt, xprt);
2582	struct msghdr msg = {
2583		.msg_flags	= 0,
2584	};
2585	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2586					 (u32)xdr->len);
2587	unsigned int sent = 0;
2588	int err;
 
 
 
 
 
 
 
 
 
2589
2590	req->rq_xtime = ktime_get();
2591	err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2592	if (err < 0)
2593		return err;
2594	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2595	xdr_free_bvec(xdr);
2596	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2597		return -EAGAIN;
2598	return sent;
2599}
2600
2601/**
2602 * bc_send_request - Send a backchannel Call on a TCP socket
2603 * @req: rpc_rqst containing Call message to be sent
2604 *
2605 * xpt_mutex ensures @rqstp's whole message is written to the socket
2606 * without interruption.
2607 *
2608 * Return values:
2609 *   %0 if the message was sent successfully
2610 *   %ENOTCONN if the message was not sent
2611 */
2612static int bc_send_request(struct rpc_rqst *req)
2613{
 
2614	struct svc_xprt	*xprt;
2615	int len;
 
2616
 
2617	/*
2618	 * Get the server socket associated with this callback xprt
2619	 */
2620	xprt = req->rq_xprt->bc_xprt;
 
2621
2622	/*
2623	 * Grab the mutex to serialize data as the connection is shared
2624	 * with the fore channel
2625	 */
2626	mutex_lock(&xprt->xpt_mutex);
 
 
 
 
 
2627	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2628		len = -ENOTCONN;
2629	else
2630		len = bc_sendto(req);
2631	mutex_unlock(&xprt->xpt_mutex);
2632
2633	if (len > 0)
2634		len = 0;
2635
2636	return len;
2637}
2638
2639/*
2640 * The close routine. Since this is client initiated, we do nothing
2641 */
2642
2643static void bc_close(struct rpc_xprt *xprt)
2644{
2645	xprt_disconnect_done(xprt);
2646}
2647
2648/*
2649 * The xprt destroy routine. Again, because this connection is client
2650 * initiated, we do nothing
2651 */
2652
2653static void bc_destroy(struct rpc_xprt *xprt)
2654{
2655	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
2656
2657	xs_xprt_free(xprt);
2658	module_put(THIS_MODULE);
2659}
2660
2661static const struct rpc_xprt_ops xs_local_ops = {
2662	.reserve_xprt		= xprt_reserve_xprt,
2663	.release_xprt		= xprt_release_xprt,
2664	.alloc_slot		= xprt_alloc_slot,
2665	.free_slot		= xprt_free_slot,
2666	.rpcbind		= xs_local_rpcbind,
2667	.set_port		= xs_local_set_port,
2668	.connect		= xs_local_connect,
2669	.buf_alloc		= rpc_malloc,
2670	.buf_free		= rpc_free,
2671	.prepare_request	= xs_stream_prepare_request,
2672	.send_request		= xs_local_send_request,
2673	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2674	.close			= xs_close,
2675	.destroy		= xs_destroy,
2676	.print_stats		= xs_local_print_stats,
2677	.enable_swap		= xs_enable_swap,
2678	.disable_swap		= xs_disable_swap,
2679};
2680
2681static const struct rpc_xprt_ops xs_udp_ops = {
2682	.set_buffer_size	= xs_udp_set_buffer_size,
2683	.reserve_xprt		= xprt_reserve_xprt_cong,
2684	.release_xprt		= xprt_release_xprt_cong,
2685	.alloc_slot		= xprt_alloc_slot,
2686	.free_slot		= xprt_free_slot,
2687	.rpcbind		= rpcb_getport_async,
2688	.set_port		= xs_set_port,
2689	.connect		= xs_connect,
2690	.get_srcaddr		= xs_sock_srcaddr,
2691	.get_srcport		= xs_sock_srcport,
2692	.buf_alloc		= rpc_malloc,
2693	.buf_free		= rpc_free,
2694	.send_request		= xs_udp_send_request,
2695	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
2696	.timer			= xs_udp_timer,
2697	.release_request	= xprt_release_rqst_cong,
2698	.close			= xs_close,
2699	.destroy		= xs_destroy,
2700	.print_stats		= xs_udp_print_stats,
2701	.enable_swap		= xs_enable_swap,
2702	.disable_swap		= xs_disable_swap,
2703	.inject_disconnect	= xs_inject_disconnect,
2704};
2705
2706static const struct rpc_xprt_ops xs_tcp_ops = {
2707	.reserve_xprt		= xprt_reserve_xprt,
2708	.release_xprt		= xprt_release_xprt,
2709	.alloc_slot		= xprt_alloc_slot,
2710	.free_slot		= xprt_free_slot,
2711	.rpcbind		= rpcb_getport_async,
2712	.set_port		= xs_set_port,
2713	.connect		= xs_connect,
2714	.get_srcaddr		= xs_sock_srcaddr,
2715	.get_srcport		= xs_sock_srcport,
2716	.buf_alloc		= rpc_malloc,
2717	.buf_free		= rpc_free,
2718	.prepare_request	= xs_stream_prepare_request,
2719	.send_request		= xs_tcp_send_request,
2720	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2721	.close			= xs_tcp_shutdown,
2722	.destroy		= xs_destroy,
2723	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2724	.print_stats		= xs_tcp_print_stats,
2725	.enable_swap		= xs_enable_swap,
2726	.disable_swap		= xs_disable_swap,
2727	.inject_disconnect	= xs_inject_disconnect,
2728#ifdef CONFIG_SUNRPC_BACKCHANNEL
2729	.bc_setup		= xprt_setup_bc,
2730	.bc_maxpayload		= xs_tcp_bc_maxpayload,
2731	.bc_num_slots		= xprt_bc_max_slots,
2732	.bc_free_rqst		= xprt_free_bc_rqst,
2733	.bc_destroy		= xprt_destroy_bc,
2734#endif
2735};
2736
2737/*
2738 * The rpc_xprt_ops for the server backchannel
2739 */
2740
2741static const struct rpc_xprt_ops bc_tcp_ops = {
2742	.reserve_xprt		= xprt_reserve_xprt,
2743	.release_xprt		= xprt_release_xprt,
2744	.alloc_slot		= xprt_alloc_slot,
2745	.free_slot		= xprt_free_slot,
2746	.buf_alloc		= bc_malloc,
2747	.buf_free		= bc_free,
2748	.send_request		= bc_send_request,
2749	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2750	.close			= bc_close,
2751	.destroy		= bc_destroy,
2752	.print_stats		= xs_tcp_print_stats,
2753	.enable_swap		= xs_enable_swap,
2754	.disable_swap		= xs_disable_swap,
2755	.inject_disconnect	= xs_inject_disconnect,
2756};
2757
2758static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2759{
2760	static const struct sockaddr_in sin = {
2761		.sin_family		= AF_INET,
2762		.sin_addr.s_addr	= htonl(INADDR_ANY),
2763	};
2764	static const struct sockaddr_in6 sin6 = {
2765		.sin6_family		= AF_INET6,
2766		.sin6_addr		= IN6ADDR_ANY_INIT,
2767	};
2768
2769	switch (family) {
2770	case AF_LOCAL:
2771		break;
2772	case AF_INET:
2773		memcpy(sap, &sin, sizeof(sin));
2774		break;
2775	case AF_INET6:
2776		memcpy(sap, &sin6, sizeof(sin6));
2777		break;
2778	default:
2779		dprintk("RPC:       %s: Bad address family\n", __func__);
2780		return -EAFNOSUPPORT;
2781	}
2782	return 0;
2783}
2784
2785static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2786				      unsigned int slot_table_size,
2787				      unsigned int max_slot_table_size)
2788{
2789	struct rpc_xprt *xprt;
2790	struct sock_xprt *new;
2791
2792	if (args->addrlen > sizeof(xprt->addr)) {
2793		dprintk("RPC:       xs_setup_xprt: address too large\n");
2794		return ERR_PTR(-EBADF);
2795	}
2796
2797	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2798			max_slot_table_size);
2799	if (xprt == NULL) {
2800		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
2801				"rpc_xprt\n");
2802		return ERR_PTR(-ENOMEM);
2803	}
2804
2805	new = container_of(xprt, struct sock_xprt, xprt);
2806	mutex_init(&new->recv_mutex);
2807	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2808	xprt->addrlen = args->addrlen;
2809	if (args->srcaddr)
2810		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2811	else {
2812		int err;
2813		err = xs_init_anyaddr(args->dstaddr->sa_family,
2814					(struct sockaddr *)&new->srcaddr);
2815		if (err != 0) {
2816			xprt_free(xprt);
2817			return ERR_PTR(err);
2818		}
2819	}
2820
2821	return xprt;
2822}
2823
2824static const struct rpc_timeout xs_local_default_timeout = {
2825	.to_initval = 10 * HZ,
2826	.to_maxval = 10 * HZ,
2827	.to_retries = 2,
2828};
2829
2830/**
2831 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2832 * @args: rpc transport creation arguments
2833 *
2834 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2835 */
2836static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2837{
2838	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2839	struct sock_xprt *transport;
2840	struct rpc_xprt *xprt;
2841	struct rpc_xprt *ret;
2842
2843	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2844			xprt_max_tcp_slot_table_entries);
2845	if (IS_ERR(xprt))
2846		return xprt;
2847	transport = container_of(xprt, struct sock_xprt, xprt);
2848
2849	xprt->prot = 0;
2850	xprt->xprt_class = &xs_local_transport;
2851	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2852
2853	xprt->bind_timeout = XS_BIND_TO;
2854	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2855	xprt->idle_timeout = XS_IDLE_DISC_TO;
2856
2857	xprt->ops = &xs_local_ops;
2858	xprt->timeout = &xs_local_default_timeout;
2859
2860	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2861	INIT_WORK(&transport->error_worker, xs_error_handle);
2862	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
2863
2864	switch (sun->sun_family) {
2865	case AF_LOCAL:
2866		if (sun->sun_path[0] != '/') {
2867			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2868					sun->sun_path);
2869			ret = ERR_PTR(-EINVAL);
2870			goto out_err;
2871		}
2872		xprt_set_bound(xprt);
 
 
2873		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2874		break;
2875	default:
2876		ret = ERR_PTR(-EAFNOSUPPORT);
2877		goto out_err;
2878	}
2879
2880	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2881			xprt->address_strings[RPC_DISPLAY_ADDR]);
2882
2883	if (try_module_get(THIS_MODULE))
2884		return xprt;
2885	ret = ERR_PTR(-EINVAL);
2886out_err:
2887	xs_xprt_free(xprt);
2888	return ret;
2889}
2890
2891static const struct rpc_timeout xs_udp_default_timeout = {
2892	.to_initval = 5 * HZ,
2893	.to_maxval = 30 * HZ,
2894	.to_increment = 5 * HZ,
2895	.to_retries = 5,
2896};
2897
2898/**
2899 * xs_setup_udp - Set up transport to use a UDP socket
2900 * @args: rpc transport creation arguments
2901 *
2902 */
2903static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2904{
2905	struct sockaddr *addr = args->dstaddr;
2906	struct rpc_xprt *xprt;
2907	struct sock_xprt *transport;
2908	struct rpc_xprt *ret;
2909
2910	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2911			xprt_udp_slot_table_entries);
2912	if (IS_ERR(xprt))
2913		return xprt;
2914	transport = container_of(xprt, struct sock_xprt, xprt);
2915
2916	xprt->prot = IPPROTO_UDP;
2917	xprt->xprt_class = &xs_udp_transport;
2918	/* XXX: header size can vary due to auth type, IPv6, etc. */
2919	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2920
2921	xprt->bind_timeout = XS_BIND_TO;
2922	xprt->reestablish_timeout = XS_UDP_REEST_TO;
2923	xprt->idle_timeout = XS_IDLE_DISC_TO;
2924
2925	xprt->ops = &xs_udp_ops;
2926
2927	xprt->timeout = &xs_udp_default_timeout;
2928
2929	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
2930	INIT_WORK(&transport->error_worker, xs_error_handle);
2931	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2932
2933	switch (addr->sa_family) {
2934	case AF_INET:
2935		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2936			xprt_set_bound(xprt);
2937
 
 
2938		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2939		break;
2940	case AF_INET6:
2941		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2942			xprt_set_bound(xprt);
2943
 
 
2944		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2945		break;
2946	default:
2947		ret = ERR_PTR(-EAFNOSUPPORT);
2948		goto out_err;
2949	}
2950
2951	if (xprt_bound(xprt))
2952		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2953				xprt->address_strings[RPC_DISPLAY_ADDR],
2954				xprt->address_strings[RPC_DISPLAY_PORT],
2955				xprt->address_strings[RPC_DISPLAY_PROTO]);
2956	else
2957		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2958				xprt->address_strings[RPC_DISPLAY_ADDR],
2959				xprt->address_strings[RPC_DISPLAY_PROTO]);
2960
2961	if (try_module_get(THIS_MODULE))
2962		return xprt;
2963	ret = ERR_PTR(-EINVAL);
2964out_err:
2965	xs_xprt_free(xprt);
2966	return ret;
2967}
2968
2969static const struct rpc_timeout xs_tcp_default_timeout = {
2970	.to_initval = 60 * HZ,
2971	.to_maxval = 60 * HZ,
2972	.to_retries = 2,
2973};
2974
2975/**
2976 * xs_setup_tcp - Set up transport to use a TCP socket
2977 * @args: rpc transport creation arguments
2978 *
2979 */
2980static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2981{
2982	struct sockaddr *addr = args->dstaddr;
2983	struct rpc_xprt *xprt;
2984	struct sock_xprt *transport;
2985	struct rpc_xprt *ret;
2986	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2987
2988	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2989		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2990
2991	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2992			max_slot_table_size);
2993	if (IS_ERR(xprt))
2994		return xprt;
2995	transport = container_of(xprt, struct sock_xprt, xprt);
2996
2997	xprt->prot = IPPROTO_TCP;
2998	xprt->xprt_class = &xs_tcp_transport;
2999	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3000
3001	xprt->bind_timeout = XS_BIND_TO;
3002	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3003	xprt->idle_timeout = XS_IDLE_DISC_TO;
3004
3005	xprt->ops = &xs_tcp_ops;
3006	xprt->timeout = &xs_tcp_default_timeout;
3007
3008	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
3009	xprt->connect_timeout = xprt->timeout->to_initval *
3010		(xprt->timeout->to_retries + 1);
3011
3012	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
3013	INIT_WORK(&transport->error_worker, xs_error_handle);
3014	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3015
3016	switch (addr->sa_family) {
3017	case AF_INET:
3018		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3019			xprt_set_bound(xprt);
3020
 
 
3021		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
3022		break;
3023	case AF_INET6:
3024		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3025			xprt_set_bound(xprt);
3026
 
 
3027		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
3028		break;
3029	default:
3030		ret = ERR_PTR(-EAFNOSUPPORT);
3031		goto out_err;
3032	}
3033
3034	if (xprt_bound(xprt))
3035		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3036				xprt->address_strings[RPC_DISPLAY_ADDR],
3037				xprt->address_strings[RPC_DISPLAY_PORT],
3038				xprt->address_strings[RPC_DISPLAY_PROTO]);
3039	else
3040		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3041				xprt->address_strings[RPC_DISPLAY_ADDR],
3042				xprt->address_strings[RPC_DISPLAY_PROTO]);
3043
 
3044	if (try_module_get(THIS_MODULE))
3045		return xprt;
3046	ret = ERR_PTR(-EINVAL);
3047out_err:
3048	xs_xprt_free(xprt);
3049	return ret;
3050}
3051
3052/**
3053 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3054 * @args: rpc transport creation arguments
3055 *
3056 */
3057static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3058{
3059	struct sockaddr *addr = args->dstaddr;
3060	struct rpc_xprt *xprt;
3061	struct sock_xprt *transport;
3062	struct svc_sock *bc_sock;
3063	struct rpc_xprt *ret;
3064
 
 
 
 
 
 
 
 
 
3065	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3066			xprt_tcp_slot_table_entries);
3067	if (IS_ERR(xprt))
3068		return xprt;
3069	transport = container_of(xprt, struct sock_xprt, xprt);
3070
3071	xprt->prot = IPPROTO_TCP;
3072	xprt->xprt_class = &xs_bc_tcp_transport;
3073	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3074	xprt->timeout = &xs_tcp_default_timeout;
3075
3076	/* backchannel */
3077	xprt_set_bound(xprt);
3078	xprt->bind_timeout = 0;
3079	xprt->reestablish_timeout = 0;
3080	xprt->idle_timeout = 0;
3081
3082	xprt->ops = &bc_tcp_ops;
3083
3084	switch (addr->sa_family) {
3085	case AF_INET:
3086		xs_format_peer_addresses(xprt, "tcp",
3087					 RPCBIND_NETID_TCP);
3088		break;
3089	case AF_INET6:
3090		xs_format_peer_addresses(xprt, "tcp",
3091				   RPCBIND_NETID_TCP6);
3092		break;
3093	default:
3094		ret = ERR_PTR(-EAFNOSUPPORT);
3095		goto out_err;
3096	}
3097
3098	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3099			xprt->address_strings[RPC_DISPLAY_ADDR],
3100			xprt->address_strings[RPC_DISPLAY_PORT],
3101			xprt->address_strings[RPC_DISPLAY_PROTO]);
3102
3103	/*
3104	 * Once we've associated a backchannel xprt with a connection,
3105	 * we want to keep it around as long as the connection lasts,
3106	 * in case we need to start using it for a backchannel again;
3107	 * this reference won't be dropped until bc_xprt is destroyed.
 
3108	 */
3109	xprt_get(xprt);
3110	args->bc_xprt->xpt_bc_xprt = xprt;
3111	xprt->bc_xprt = args->bc_xprt;
3112	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3113	transport->sock = bc_sock->sk_sock;
3114	transport->inet = bc_sock->sk_sk;
3115
3116	/*
3117	 * Since we don't want connections for the backchannel, we set
3118	 * the xprt status to connected
3119	 */
3120	xprt_set_connected(xprt);
3121
 
3122	if (try_module_get(THIS_MODULE))
3123		return xprt;
3124
3125	args->bc_xprt->xpt_bc_xprt = NULL;
3126	args->bc_xprt->xpt_bc_xps = NULL;
3127	xprt_put(xprt);
3128	ret = ERR_PTR(-EINVAL);
3129out_err:
3130	xs_xprt_free(xprt);
3131	return ret;
3132}
3133
3134static struct xprt_class	xs_local_transport = {
3135	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3136	.name		= "named UNIX socket",
3137	.owner		= THIS_MODULE,
3138	.ident		= XPRT_TRANSPORT_LOCAL,
3139	.setup		= xs_setup_local,
3140	.netid		= { "" },
3141};
3142
3143static struct xprt_class	xs_udp_transport = {
3144	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3145	.name		= "udp",
3146	.owner		= THIS_MODULE,
3147	.ident		= XPRT_TRANSPORT_UDP,
3148	.setup		= xs_setup_udp,
3149	.netid		= { "udp", "udp6", "" },
3150};
3151
3152static struct xprt_class	xs_tcp_transport = {
3153	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3154	.name		= "tcp",
3155	.owner		= THIS_MODULE,
3156	.ident		= XPRT_TRANSPORT_TCP,
3157	.setup		= xs_setup_tcp,
3158	.netid		= { "tcp", "tcp6", "" },
3159};
3160
3161static struct xprt_class	xs_bc_tcp_transport = {
3162	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3163	.name		= "tcp NFSv4.1 backchannel",
3164	.owner		= THIS_MODULE,
3165	.ident		= XPRT_TRANSPORT_BC_TCP,
3166	.setup		= xs_setup_bc_tcp,
3167	.netid		= { "" },
3168};
3169
3170/**
3171 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3172 *
3173 */
3174int init_socket_xprt(void)
3175{
 
3176	if (!sunrpc_table_header)
3177		sunrpc_table_header = register_sysctl_table(sunrpc_table);
 
3178
3179	xprt_register_transport(&xs_local_transport);
3180	xprt_register_transport(&xs_udp_transport);
3181	xprt_register_transport(&xs_tcp_transport);
3182	xprt_register_transport(&xs_bc_tcp_transport);
3183
3184	return 0;
3185}
3186
3187/**
3188 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3189 *
3190 */
3191void cleanup_socket_xprt(void)
3192{
 
3193	if (sunrpc_table_header) {
3194		unregister_sysctl_table(sunrpc_table_header);
3195		sunrpc_table_header = NULL;
3196	}
 
3197
3198	xprt_unregister_transport(&xs_local_transport);
3199	xprt_unregister_transport(&xs_udp_transport);
3200	xprt_unregister_transport(&xs_tcp_transport);
3201	xprt_unregister_transport(&xs_bc_tcp_transport);
3202}
3203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3204static int param_set_portnr(const char *val, const struct kernel_param *kp)
3205{
3206	return param_set_uint_minmax(val, kp,
3207			RPC_MIN_RESVPORT,
3208			RPC_MAX_RESVPORT);
3209}
3210
3211static const struct kernel_param_ops param_ops_portnr = {
3212	.set = param_set_portnr,
3213	.get = param_get_uint,
3214};
3215
3216#define param_check_portnr(name, p) \
3217	__param_check(name, p, unsigned int);
3218
3219module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3220module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3221
3222static int param_set_slot_table_size(const char *val,
3223				     const struct kernel_param *kp)
3224{
3225	return param_set_uint_minmax(val, kp,
3226			RPC_MIN_SLOT_TABLE,
3227			RPC_MAX_SLOT_TABLE);
3228}
3229
3230static const struct kernel_param_ops param_ops_slot_table_size = {
3231	.set = param_set_slot_table_size,
3232	.get = param_get_uint,
3233};
3234
3235#define param_check_slot_table_size(name, p) \
3236	__param_check(name, p, unsigned int);
3237
3238static int param_set_max_slot_table_size(const char *val,
3239				     const struct kernel_param *kp)
3240{
3241	return param_set_uint_minmax(val, kp,
3242			RPC_MIN_SLOT_TABLE,
3243			RPC_MAX_SLOT_TABLE_LIMIT);
3244}
3245
3246static const struct kernel_param_ops param_ops_max_slot_table_size = {
3247	.set = param_set_max_slot_table_size,
3248	.get = param_get_uint,
3249};
3250
3251#define param_check_max_slot_table_size(name, p) \
3252	__param_check(name, p, unsigned int);
3253
3254module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3255		   slot_table_size, 0644);
3256module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3257		   max_slot_table_size, 0644);
3258module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3259		   slot_table_size, 0644);