1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21d8206b9STom Tucker /* 31d8206b9STom Tucker * linux/net/sunrpc/svc_xprt.c 41d8206b9STom Tucker * 51d8206b9STom Tucker * Author: Tom Tucker <tom@opengridcomputing.com> 61d8206b9STom Tucker */ 71d8206b9STom Tucker 81d8206b9STom Tucker #include <linux/sched.h> 91d8206b9STom Tucker #include <linux/errno.h> 101d8206b9STom Tucker #include <linux/freezer.h> 117086721fSJeff Layton #include <linux/kthread.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 131d8206b9STom Tucker #include <net/sock.h> 14c3d4879eSScott Mayhew #include <linux/sunrpc/addr.h> 151d8206b9STom Tucker #include <linux/sunrpc/stats.h> 161d8206b9STom Tucker #include <linux/sunrpc/svc_xprt.h> 17dcf1a357SH Hartley Sweeten #include <linux/sunrpc/svcsock.h> 1899de8ea9SJ. Bruce Fields #include <linux/sunrpc/xprt.h> 193a9a231dSPaul Gortmaker #include <linux/module.h> 20c3d4879eSScott Mayhew #include <linux/netdevice.h> 21860a0d9eSJeff Layton #include <trace/events/sunrpc.h> 221d8206b9STom Tucker 231d8206b9STom Tucker #define RPCDBG_FACILITY RPCDBG_SVCXPRT 241d8206b9STom Tucker 25ff3ac5c3STrond Myklebust static unsigned int svc_rpc_per_connection_limit __read_mostly; 26ff3ac5c3STrond Myklebust module_param(svc_rpc_per_connection_limit, uint, 0644); 27ff3ac5c3STrond Myklebust 28ff3ac5c3STrond Myklebust 290f0257eaSTom Tucker static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt); 300f0257eaSTom Tucker static int svc_deferred_recv(struct svc_rqst *rqstp); 310f0257eaSTom Tucker static struct cache_deferred_req *svc_defer(struct cache_req *req); 32ff861c4dSKees Cook static void svc_age_temp_xprts(struct timer_list *t); 337710ec36SJ. Bruce Fields static void svc_delete_xprt(struct svc_xprt *xprt); 340f0257eaSTom Tucker 350f0257eaSTom Tucker /* apparently the "standard" is that clients close 360f0257eaSTom Tucker * idle connections after 5 minutes, servers after 370f0257eaSTom Tucker * 6 minutes 38855c9e76SJ. Bruce Fields * http://nfsv4bat.org/Documents/ConnectAThon/1996/nfstcp.pdf 390f0257eaSTom Tucker */ 400f0257eaSTom Tucker static int svc_conn_age_period = 6*60; 410f0257eaSTom Tucker 421d8206b9STom Tucker /* List of registered transport classes */ 431d8206b9STom Tucker static DEFINE_SPINLOCK(svc_xprt_class_lock); 441d8206b9STom Tucker static LIST_HEAD(svc_xprt_class_list); 451d8206b9STom Tucker 460f0257eaSTom Tucker /* SMP locking strategy: 470f0257eaSTom Tucker * 480f0257eaSTom Tucker * svc_pool->sp_lock protects most of the fields of that pool. 490f0257eaSTom Tucker * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt. 500f0257eaSTom Tucker * when both need to be taken (rare), svc_serv->sv_lock is first. 513c519914SJeff Layton * The "service mutex" protects svc_serv->sv_nrthread. 520f0257eaSTom Tucker * svc_sock->sk_lock protects the svc_sock->sk_deferred list 530f0257eaSTom Tucker * and the ->sk_info_authunix cache. 540f0257eaSTom Tucker * 550f0257eaSTom Tucker * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being 560f0257eaSTom Tucker * enqueued multiply. During normal transport processing this bit 570f0257eaSTom Tucker * is set by svc_xprt_enqueue and cleared by svc_xprt_received. 580f0257eaSTom Tucker * Providers should not manipulate this bit directly. 590f0257eaSTom Tucker * 600f0257eaSTom Tucker * Some flags can be set to certain values at any time 610f0257eaSTom Tucker * providing that certain rules are followed: 620f0257eaSTom Tucker * 630f0257eaSTom Tucker * XPT_CONN, XPT_DATA: 640f0257eaSTom Tucker * - Can be set or cleared at any time. 650f0257eaSTom Tucker * - After a set, svc_xprt_enqueue must be called to enqueue 660f0257eaSTom Tucker * the transport for processing. 670f0257eaSTom Tucker * - After a clear, the transport must be read/accepted. 680f0257eaSTom Tucker * If this succeeds, it must be set again. 690f0257eaSTom Tucker * XPT_CLOSE: 700f0257eaSTom Tucker * - Can set at any time. It is never cleared. 710f0257eaSTom Tucker * XPT_DEAD: 720f0257eaSTom Tucker * - Can only be set while XPT_BUSY is held which ensures 730f0257eaSTom Tucker * that no other thread will be using the transport or will 740f0257eaSTom Tucker * try to set XPT_DEAD. 750f0257eaSTom Tucker */ 761d8206b9STom Tucker int svc_reg_xprt_class(struct svc_xprt_class *xcl) 771d8206b9STom Tucker { 781d8206b9STom Tucker struct svc_xprt_class *cl; 791d8206b9STom Tucker int res = -EEXIST; 801d8206b9STom Tucker 811d8206b9STom Tucker dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name); 821d8206b9STom Tucker 831d8206b9STom Tucker INIT_LIST_HEAD(&xcl->xcl_list); 841d8206b9STom Tucker spin_lock(&svc_xprt_class_lock); 851d8206b9STom Tucker /* Make sure there isn't already a class with the same name */ 861d8206b9STom Tucker list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) { 871d8206b9STom Tucker if (strcmp(xcl->xcl_name, cl->xcl_name) == 0) 881d8206b9STom Tucker goto out; 891d8206b9STom Tucker } 901d8206b9STom Tucker list_add_tail(&xcl->xcl_list, &svc_xprt_class_list); 911d8206b9STom Tucker res = 0; 921d8206b9STom Tucker out: 931d8206b9STom Tucker spin_unlock(&svc_xprt_class_lock); 941d8206b9STom Tucker return res; 951d8206b9STom Tucker } 961d8206b9STom Tucker EXPORT_SYMBOL_GPL(svc_reg_xprt_class); 971d8206b9STom Tucker 981d8206b9STom Tucker void svc_unreg_xprt_class(struct svc_xprt_class *xcl) 991d8206b9STom Tucker { 1001d8206b9STom Tucker dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name); 1011d8206b9STom Tucker spin_lock(&svc_xprt_class_lock); 1021d8206b9STom Tucker list_del_init(&xcl->xcl_list); 1031d8206b9STom Tucker spin_unlock(&svc_xprt_class_lock); 1041d8206b9STom Tucker } 1051d8206b9STom Tucker EXPORT_SYMBOL_GPL(svc_unreg_xprt_class); 1061d8206b9STom Tucker 107b25b60d7SChristophe JAILLET /** 108b25b60d7SChristophe JAILLET * svc_print_xprts - Format the transport list for printing 109b25b60d7SChristophe JAILLET * @buf: target buffer for formatted address 110b25b60d7SChristophe JAILLET * @maxlen: length of target buffer 111b25b60d7SChristophe JAILLET * 112b25b60d7SChristophe JAILLET * Fills in @buf with a string containing a list of transport names, each name 113b25b60d7SChristophe JAILLET * terminated with '\n'. If the buffer is too small, some entries may be 114b25b60d7SChristophe JAILLET * missing, but it is guaranteed that all lines in the output buffer are 115b25b60d7SChristophe JAILLET * complete. 116b25b60d7SChristophe JAILLET * 117b25b60d7SChristophe JAILLET * Returns positive length of the filled-in string. 118dc9a16e4STom Tucker */ 119dc9a16e4STom Tucker int svc_print_xprts(char *buf, int maxlen) 120dc9a16e4STom Tucker { 1218f3a6de3SPavel Emelyanov struct svc_xprt_class *xcl; 122dc9a16e4STom Tucker char tmpstr[80]; 123dc9a16e4STom Tucker int len = 0; 124dc9a16e4STom Tucker buf[0] = '\0'; 125dc9a16e4STom Tucker 126dc9a16e4STom Tucker spin_lock(&svc_xprt_class_lock); 1278f3a6de3SPavel Emelyanov list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) { 128dc9a16e4STom Tucker int slen; 129dc9a16e4STom Tucker 130b25b60d7SChristophe JAILLET slen = snprintf(tmpstr, sizeof(tmpstr), "%s %d\n", 131b25b60d7SChristophe JAILLET xcl->xcl_name, xcl->xcl_max_payload); 132b25b60d7SChristophe JAILLET if (slen >= sizeof(tmpstr) || len + slen >= maxlen) 133dc9a16e4STom Tucker break; 134dc9a16e4STom Tucker len += slen; 135dc9a16e4STom Tucker strcat(buf, tmpstr); 136dc9a16e4STom Tucker } 137dc9a16e4STom Tucker spin_unlock(&svc_xprt_class_lock); 138dc9a16e4STom Tucker 139dc9a16e4STom Tucker return len; 140dc9a16e4STom Tucker } 141dc9a16e4STom Tucker 142e1b3157fSTom Tucker static void svc_xprt_free(struct kref *kref) 143e1b3157fSTom Tucker { 144e1b3157fSTom Tucker struct svc_xprt *xprt = 145e1b3157fSTom Tucker container_of(kref, struct svc_xprt, xpt_ref); 146e1b3157fSTom Tucker struct module *owner = xprt->xpt_class->xcl_owner; 147e3bfca01SPavel Emelyanov if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) 148e3bfca01SPavel Emelyanov svcauth_unix_info_release(xprt); 1494df493a2STrond Myklebust put_cred(xprt->xpt_cred); 1504fb8518bSPavel Emelyanov put_net(xprt->xpt_net); 15199de8ea9SJ. Bruce Fields /* See comment on corresponding get in xs_setup_bc_tcp(): */ 15299de8ea9SJ. Bruce Fields if (xprt->xpt_bc_xprt) 15399de8ea9SJ. Bruce Fields xprt_put(xprt->xpt_bc_xprt); 15439a9beabSJ. Bruce Fields if (xprt->xpt_bc_xps) 15539a9beabSJ. Bruce Fields xprt_switch_put(xprt->xpt_bc_xps); 15611bbb0f7SChuck Lever trace_svc_xprt_free(xprt); 157e1b3157fSTom Tucker xprt->xpt_ops->xpo_free(xprt); 158e1b3157fSTom Tucker module_put(owner); 159e1b3157fSTom Tucker } 160e1b3157fSTom Tucker 161e1b3157fSTom Tucker void svc_xprt_put(struct svc_xprt *xprt) 162e1b3157fSTom Tucker { 163e1b3157fSTom Tucker kref_put(&xprt->xpt_ref, svc_xprt_free); 164e1b3157fSTom Tucker } 165e1b3157fSTom Tucker EXPORT_SYMBOL_GPL(svc_xprt_put); 166e1b3157fSTom Tucker 1671d8206b9STom Tucker /* 1681d8206b9STom Tucker * Called by transport drivers to initialize the transport independent 1691d8206b9STom Tucker * portion of the transport instance. 1701d8206b9STom Tucker */ 171bd4620ddSStanislav Kinsbursky void svc_xprt_init(struct net *net, struct svc_xprt_class *xcl, 172bd4620ddSStanislav Kinsbursky struct svc_xprt *xprt, struct svc_serv *serv) 1731d8206b9STom Tucker { 1741d8206b9STom Tucker memset(xprt, 0, sizeof(*xprt)); 1751d8206b9STom Tucker xprt->xpt_class = xcl; 1761d8206b9STom Tucker xprt->xpt_ops = xcl->xcl_ops; 177e1b3157fSTom Tucker kref_init(&xprt->xpt_ref); 178bb5cf160STom Tucker xprt->xpt_server = serv; 1797a182083STom Tucker INIT_LIST_HEAD(&xprt->xpt_list); 1807a182083STom Tucker INIT_LIST_HEAD(&xprt->xpt_ready); 1818c7b0172STom Tucker INIT_LIST_HEAD(&xprt->xpt_deferred); 182edc7a894SJ. Bruce Fields INIT_LIST_HEAD(&xprt->xpt_users); 183a50fea26STom Tucker mutex_init(&xprt->xpt_mutex); 184def13d74STom Tucker spin_lock_init(&xprt->xpt_lock); 1854e5caaa5STom Tucker set_bit(XPT_BUSY, &xprt->xpt_flags); 186bd4620ddSStanislav Kinsbursky xprt->xpt_net = get_net(net); 187ece200ddSChuck Lever strcpy(xprt->xpt_remotebuf, "uninitialized"); 1881d8206b9STom Tucker } 1891d8206b9STom Tucker EXPORT_SYMBOL_GPL(svc_xprt_init); 190b700cbb1STom Tucker 1915dd248f6SChuck Lever static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl, 1925dd248f6SChuck Lever struct svc_serv *serv, 19362832c03SPavel Emelyanov struct net *net, 1949652ada3SChuck Lever const int family, 1959652ada3SChuck Lever const unsigned short port, 1969652ada3SChuck Lever int flags) 197b700cbb1STom Tucker { 198b700cbb1STom Tucker struct sockaddr_in sin = { 199b700cbb1STom Tucker .sin_family = AF_INET, 200e6f1cebfSAl Viro .sin_addr.s_addr = htonl(INADDR_ANY), 201b700cbb1STom Tucker .sin_port = htons(port), 202b700cbb1STom Tucker }; 203dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2045dd248f6SChuck Lever struct sockaddr_in6 sin6 = { 2055dd248f6SChuck Lever .sin6_family = AF_INET6, 2065dd248f6SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 2075dd248f6SChuck Lever .sin6_port = htons(port), 2085dd248f6SChuck Lever }; 209dfd56b8bSEric Dumazet #endif 2104b8f380eSChuck Lever struct svc_xprt *xprt; 2115dd248f6SChuck Lever struct sockaddr *sap; 2125dd248f6SChuck Lever size_t len; 2135dd248f6SChuck Lever 2149652ada3SChuck Lever switch (family) { 2159652ada3SChuck Lever case PF_INET: 2165dd248f6SChuck Lever sap = (struct sockaddr *)&sin; 2175dd248f6SChuck Lever len = sizeof(sin); 2185dd248f6SChuck Lever break; 219dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2209652ada3SChuck Lever case PF_INET6: 2215dd248f6SChuck Lever sap = (struct sockaddr *)&sin6; 2225dd248f6SChuck Lever len = sizeof(sin6); 2235dd248f6SChuck Lever break; 224dfd56b8bSEric Dumazet #endif 2255dd248f6SChuck Lever default: 2265dd248f6SChuck Lever return ERR_PTR(-EAFNOSUPPORT); 2275dd248f6SChuck Lever } 2285dd248f6SChuck Lever 2294b8f380eSChuck Lever xprt = xcl->xcl_ops->xpo_create(serv, net, sap, len, flags); 2304b8f380eSChuck Lever if (IS_ERR(xprt)) 2314b8f380eSChuck Lever trace_svc_xprt_create_err(serv->sv_program->pg_name, 2324b8f380eSChuck Lever xcl->xcl_name, sap, xprt); 2334b8f380eSChuck Lever return xprt; 2345dd248f6SChuck Lever } 2355dd248f6SChuck Lever 2366741019cSJ. Bruce Fields /* 2376741019cSJ. Bruce Fields * svc_xprt_received conditionally queues the transport for processing 2386741019cSJ. Bruce Fields * by another thread. The caller must hold the XPT_BUSY bit and must 2396741019cSJ. Bruce Fields * not thereafter touch transport data. 2406741019cSJ. Bruce Fields * 2416741019cSJ. Bruce Fields * Note: XPT_DATA only gets cleared when a read-attempt finds no (or 2426741019cSJ. Bruce Fields * insufficient) data. 2436741019cSJ. Bruce Fields */ 2446741019cSJ. Bruce Fields static void svc_xprt_received(struct svc_xprt *xprt) 2456741019cSJ. Bruce Fields { 246acf06a7fSJeff Layton if (!test_bit(XPT_BUSY, &xprt->xpt_flags)) { 247acf06a7fSJeff Layton WARN_ONCE(1, "xprt=0x%p already busy!", xprt); 248ff1fdb9bSWeston Andros Adamson return; 249acf06a7fSJeff Layton } 250acf06a7fSJeff Layton 2516741019cSJ. Bruce Fields /* As soon as we clear busy, the xprt could be closed and 252b9e13cdfSJeff Layton * 'put', so we need a reference to call svc_enqueue_xprt with: 2536741019cSJ. Bruce Fields */ 2546741019cSJ. Bruce Fields svc_xprt_get(xprt); 2550971374eSTrond Myklebust smp_mb__before_atomic(); 2566741019cSJ. Bruce Fields clear_bit(XPT_BUSY, &xprt->xpt_flags); 257b9e13cdfSJeff Layton xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt); 2586741019cSJ. Bruce Fields svc_xprt_put(xprt); 2596741019cSJ. Bruce Fields } 2606741019cSJ. Bruce Fields 26139b55301SJ. Bruce Fields void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new) 26239b55301SJ. Bruce Fields { 26339b55301SJ. Bruce Fields clear_bit(XPT_TEMP, &new->xpt_flags); 26439b55301SJ. Bruce Fields spin_lock_bh(&serv->sv_lock); 26539b55301SJ. Bruce Fields list_add(&new->xpt_list, &serv->sv_permsocks); 26639b55301SJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 26739b55301SJ. Bruce Fields svc_xprt_received(new); 26839b55301SJ. Bruce Fields } 26939b55301SJ. Bruce Fields 270da36e6dbSColin Ian King static int _svc_create_xprt(struct svc_serv *serv, const char *xprt_name, 271fc5d00b0SPavel Emelyanov struct net *net, const int family, 2724df493a2STrond Myklebust const unsigned short port, int flags, 2734df493a2STrond Myklebust const struct cred *cred) 2745dd248f6SChuck Lever { 2755dd248f6SChuck Lever struct svc_xprt_class *xcl; 2765dd248f6SChuck Lever 277b700cbb1STom Tucker spin_lock(&svc_xprt_class_lock); 278b700cbb1STom Tucker list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) { 279b700cbb1STom Tucker struct svc_xprt *newxprt; 280ed2849d3SNeilBrown unsigned short newport; 2814e5caaa5STom Tucker 2824e5caaa5STom Tucker if (strcmp(xprt_name, xcl->xcl_name)) 2834e5caaa5STom Tucker continue; 2844e5caaa5STom Tucker 2854e5caaa5STom Tucker if (!try_module_get(xcl->xcl_owner)) 2864e5caaa5STom Tucker goto err; 2874e5caaa5STom Tucker 2884e5caaa5STom Tucker spin_unlock(&svc_xprt_class_lock); 28962832c03SPavel Emelyanov newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags); 290b700cbb1STom Tucker if (IS_ERR(newxprt)) { 291b700cbb1STom Tucker module_put(xcl->xcl_owner); 2924e5caaa5STom Tucker return PTR_ERR(newxprt); 293b700cbb1STom Tucker } 2944df493a2STrond Myklebust newxprt->xpt_cred = get_cred(cred); 29539b55301SJ. Bruce Fields svc_add_new_perm_xprt(serv, newxprt); 296ed2849d3SNeilBrown newport = svc_xprt_local_port(newxprt); 297ed2849d3SNeilBrown return newport; 298b700cbb1STom Tucker } 2994e5caaa5STom Tucker err: 300b700cbb1STom Tucker spin_unlock(&svc_xprt_class_lock); 30168717908SChuck Lever /* This errno is exposed to user space. Provide a reasonable 30268717908SChuck Lever * perror msg for a bad transport. */ 30368717908SChuck Lever return -EPROTONOSUPPORT; 304b700cbb1STom Tucker } 305d96b9c93SJ. Bruce Fields 306d96b9c93SJ. Bruce Fields int svc_create_xprt(struct svc_serv *serv, const char *xprt_name, 307d96b9c93SJ. Bruce Fields struct net *net, const int family, 3084df493a2STrond Myklebust const unsigned short port, int flags, 3094df493a2STrond Myklebust const struct cred *cred) 310d96b9c93SJ. Bruce Fields { 311d96b9c93SJ. Bruce Fields int err; 312d96b9c93SJ. Bruce Fields 3134df493a2STrond Myklebust err = _svc_create_xprt(serv, xprt_name, net, family, port, flags, cred); 314d96b9c93SJ. Bruce Fields if (err == -EPROTONOSUPPORT) { 315d96b9c93SJ. Bruce Fields request_module("svc%s", xprt_name); 3164df493a2STrond Myklebust err = _svc_create_xprt(serv, xprt_name, net, family, port, flags, cred); 317d96b9c93SJ. Bruce Fields } 318d96b9c93SJ. Bruce Fields return err; 319d96b9c93SJ. Bruce Fields } 320b700cbb1STom Tucker EXPORT_SYMBOL_GPL(svc_create_xprt); 3219dbc240fSTom Tucker 3229dbc240fSTom Tucker /* 3239dbc240fSTom Tucker * Copy the local and remote xprt addresses to the rqstp structure 3249dbc240fSTom Tucker */ 3259dbc240fSTom Tucker void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt) 3269dbc240fSTom Tucker { 3279dbc240fSTom Tucker memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen); 3289dbc240fSTom Tucker rqstp->rq_addrlen = xprt->xpt_remotelen; 3299dbc240fSTom Tucker 3309dbc240fSTom Tucker /* 3319dbc240fSTom Tucker * Destination address in request is needed for binding the 3329dbc240fSTom Tucker * source address in RPC replies/callbacks later. 3339dbc240fSTom Tucker */ 334849a1cf1SMi Jinlong memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen); 335849a1cf1SMi Jinlong rqstp->rq_daddrlen = xprt->xpt_locallen; 3369dbc240fSTom Tucker } 3379dbc240fSTom Tucker EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs); 3389dbc240fSTom Tucker 3390f0257eaSTom Tucker /** 3400f0257eaSTom Tucker * svc_print_addr - Format rq_addr field for printing 3410f0257eaSTom Tucker * @rqstp: svc_rqst struct containing address to print 3420f0257eaSTom Tucker * @buf: target buffer for formatted address 3430f0257eaSTom Tucker * @len: length of target buffer 3440f0257eaSTom Tucker * 3450f0257eaSTom Tucker */ 3460f0257eaSTom Tucker char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len) 3470f0257eaSTom Tucker { 3480f0257eaSTom Tucker return __svc_print_addr(svc_addr(rqstp), buf, len); 3490f0257eaSTom Tucker } 3500f0257eaSTom Tucker EXPORT_SYMBOL_GPL(svc_print_addr); 3510f0257eaSTom Tucker 352ff3ac5c3STrond Myklebust static bool svc_xprt_slots_in_range(struct svc_xprt *xprt) 353ff3ac5c3STrond Myklebust { 354ff3ac5c3STrond Myklebust unsigned int limit = svc_rpc_per_connection_limit; 355ff3ac5c3STrond Myklebust int nrqsts = atomic_read(&xprt->xpt_nr_rqsts); 356ff3ac5c3STrond Myklebust 357ff3ac5c3STrond Myklebust return limit == 0 || (nrqsts >= 0 && nrqsts < limit); 358ff3ac5c3STrond Myklebust } 359ff3ac5c3STrond Myklebust 360ff3ac5c3STrond Myklebust static bool svc_xprt_reserve_slot(struct svc_rqst *rqstp, struct svc_xprt *xprt) 361ff3ac5c3STrond Myklebust { 362ff3ac5c3STrond Myklebust if (!test_bit(RQ_DATA, &rqstp->rq_flags)) { 363ff3ac5c3STrond Myklebust if (!svc_xprt_slots_in_range(xprt)) 364ff3ac5c3STrond Myklebust return false; 365ff3ac5c3STrond Myklebust atomic_inc(&xprt->xpt_nr_rqsts); 366ff3ac5c3STrond Myklebust set_bit(RQ_DATA, &rqstp->rq_flags); 367ff3ac5c3STrond Myklebust } 368ff3ac5c3STrond Myklebust return true; 369ff3ac5c3STrond Myklebust } 370ff3ac5c3STrond Myklebust 371ff3ac5c3STrond Myklebust static void svc_xprt_release_slot(struct svc_rqst *rqstp) 372ff3ac5c3STrond Myklebust { 373ff3ac5c3STrond Myklebust struct svc_xprt *xprt = rqstp->rq_xprt; 374ff3ac5c3STrond Myklebust if (test_and_clear_bit(RQ_DATA, &rqstp->rq_flags)) { 375ff3ac5c3STrond Myklebust atomic_dec(&xprt->xpt_nr_rqsts); 37695503d29SJ. Bruce Fields smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */ 377ff3ac5c3STrond Myklebust svc_xprt_enqueue(xprt); 378ff3ac5c3STrond Myklebust } 379ff3ac5c3STrond Myklebust } 380ff3ac5c3STrond Myklebust 38166c898caSJ. Bruce Fields static bool svc_xprt_ready(struct svc_xprt *xprt) 3829c335c0bSJ. Bruce Fields { 3831602a7b7STrond Myklebust unsigned long xpt_flags; 3841602a7b7STrond Myklebust 38595503d29SJ. Bruce Fields /* 38695503d29SJ. Bruce Fields * If another cpu has recently updated xpt_flags, 38795503d29SJ. Bruce Fields * sk_sock->flags, xpt_reserved, or xpt_nr_rqsts, we need to 38895503d29SJ. Bruce Fields * know about it; otherwise it's possible that both that cpu and 38995503d29SJ. Bruce Fields * this one could call svc_xprt_enqueue() without either 39095503d29SJ. Bruce Fields * svc_xprt_enqueue() recognizing that the conditions below 39195503d29SJ. Bruce Fields * are satisfied, and we could stall indefinitely: 39295503d29SJ. Bruce Fields */ 39395503d29SJ. Bruce Fields smp_rmb(); 3941602a7b7STrond Myklebust xpt_flags = READ_ONCE(xprt->xpt_flags); 3951602a7b7STrond Myklebust 3961602a7b7STrond Myklebust if (xpt_flags & (BIT(XPT_CONN) | BIT(XPT_CLOSE))) 3979c335c0bSJ. Bruce Fields return true; 3981602a7b7STrond Myklebust if (xpt_flags & (BIT(XPT_DATA) | BIT(XPT_DEFERRED))) { 399ff3ac5c3STrond Myklebust if (xprt->xpt_ops->xpo_has_wspace(xprt) && 400ff3ac5c3STrond Myklebust svc_xprt_slots_in_range(xprt)) 40182ea2d76STrond Myklebust return true; 40282ea2d76STrond Myklebust trace_svc_xprt_no_write_space(xprt); 40382ea2d76STrond Myklebust return false; 40482ea2d76STrond Myklebust } 4059c335c0bSJ. Bruce Fields return false; 4069c335c0bSJ. Bruce Fields } 4079c335c0bSJ. Bruce Fields 408b9e13cdfSJeff Layton void svc_xprt_do_enqueue(struct svc_xprt *xprt) 4090f0257eaSTom Tucker { 4100f0257eaSTom Tucker struct svc_pool *pool; 41183a712e0SJeff Layton struct svc_rqst *rqstp = NULL; 4120f0257eaSTom Tucker int cpu; 4130f0257eaSTom Tucker 41466c898caSJ. Bruce Fields if (!svc_xprt_ready(xprt)) 4157dbb53baSChuck Lever return; 4160f0257eaSTom Tucker 4170f0257eaSTom Tucker /* Mark transport as busy. It will remain in this state until 4180f0257eaSTom Tucker * the provider calls svc_xprt_received. We update XPT_BUSY 4190f0257eaSTom Tucker * atomically because it also guards against trying to enqueue 4200f0257eaSTom Tucker * the transport twice. 4210f0257eaSTom Tucker */ 4227dbb53baSChuck Lever if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) 4237dbb53baSChuck Lever return; 4240f0257eaSTom Tucker 4250c0746d0STrond Myklebust cpu = get_cpu(); 4260c0746d0STrond Myklebust pool = svc_pool_for_cpu(xprt->xpt_server, cpu); 4270c0746d0STrond Myklebust 428403c7b44SJeff Layton atomic_long_inc(&pool->sp_stats.packets); 4290c0746d0STrond Myklebust 430b1691bc0SJeff Layton spin_lock_bh(&pool->sp_lock); 431b1691bc0SJeff Layton list_add_tail(&xprt->xpt_ready, &pool->sp_sockets); 432b1691bc0SJeff Layton pool->sp_stats.sockets_queued++; 4330f0257eaSTom Tucker spin_unlock_bh(&pool->sp_lock); 43422700f3cSTrond Myklebust 43522700f3cSTrond Myklebust /* find a thread for this xprt */ 43622700f3cSTrond Myklebust rcu_read_lock(); 43722700f3cSTrond Myklebust list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { 43822700f3cSTrond Myklebust if (test_and_set_bit(RQ_BUSY, &rqstp->rq_flags)) 43922700f3cSTrond Myklebust continue; 44022700f3cSTrond Myklebust atomic_long_inc(&pool->sp_stats.threads_woken); 44155f5088cSChuck Lever rqstp->rq_qtime = ktime_get(); 44222700f3cSTrond Myklebust wake_up_process(rqstp->rq_task); 44322700f3cSTrond Myklebust goto out_unlock; 444b1691bc0SJeff Layton } 44522700f3cSTrond Myklebust set_bit(SP_CONGESTED, &pool->sp_flags); 44683a712e0SJeff Layton rqstp = NULL; 44722700f3cSTrond Myklebust out_unlock: 44822700f3cSTrond Myklebust rcu_read_unlock(); 449983c6844STrond Myklebust put_cpu(); 45083a712e0SJeff Layton trace_svc_xprt_do_enqueue(xprt, rqstp); 4510f0257eaSTom Tucker } 452b9e13cdfSJeff Layton EXPORT_SYMBOL_GPL(svc_xprt_do_enqueue); 4530971374eSTrond Myklebust 4540971374eSTrond Myklebust /* 4550971374eSTrond Myklebust * Queue up a transport with data pending. If there are idle nfsd 4560971374eSTrond Myklebust * processes, wake 'em up. 4570971374eSTrond Myklebust * 4580971374eSTrond Myklebust */ 4590971374eSTrond Myklebust void svc_xprt_enqueue(struct svc_xprt *xprt) 4600971374eSTrond Myklebust { 4610971374eSTrond Myklebust if (test_bit(XPT_BUSY, &xprt->xpt_flags)) 4620971374eSTrond Myklebust return; 463b9e13cdfSJeff Layton xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt); 4640971374eSTrond Myklebust } 4650f0257eaSTom Tucker EXPORT_SYMBOL_GPL(svc_xprt_enqueue); 4660f0257eaSTom Tucker 4670f0257eaSTom Tucker /* 468b1691bc0SJeff Layton * Dequeue the first transport, if there is one. 4690f0257eaSTom Tucker */ 4700f0257eaSTom Tucker static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool) 4710f0257eaSTom Tucker { 472b1691bc0SJeff Layton struct svc_xprt *xprt = NULL; 4730f0257eaSTom Tucker 4740f0257eaSTom Tucker if (list_empty(&pool->sp_sockets)) 47583a712e0SJeff Layton goto out; 4760f0257eaSTom Tucker 477b1691bc0SJeff Layton spin_lock_bh(&pool->sp_lock); 478b1691bc0SJeff Layton if (likely(!list_empty(&pool->sp_sockets))) { 479b1691bc0SJeff Layton xprt = list_first_entry(&pool->sp_sockets, 4800f0257eaSTom Tucker struct svc_xprt, xpt_ready); 4810f0257eaSTom Tucker list_del_init(&xprt->xpt_ready); 482b1691bc0SJeff Layton svc_xprt_get(xprt); 483b1691bc0SJeff Layton } 484b1691bc0SJeff Layton spin_unlock_bh(&pool->sp_lock); 48583a712e0SJeff Layton out: 4860f0257eaSTom Tucker return xprt; 4870f0257eaSTom Tucker } 4880f0257eaSTom Tucker 4890f0257eaSTom Tucker /** 4900f0257eaSTom Tucker * svc_reserve - change the space reserved for the reply to a request. 4910f0257eaSTom Tucker * @rqstp: The request in question 4920f0257eaSTom Tucker * @space: new max space to reserve 4930f0257eaSTom Tucker * 4940f0257eaSTom Tucker * Each request reserves some space on the output queue of the transport 4950f0257eaSTom Tucker * to make sure the reply fits. This function reduces that reserved 4960f0257eaSTom Tucker * space to be the amount of space used already, plus @space. 4970f0257eaSTom Tucker * 4980f0257eaSTom Tucker */ 4990f0257eaSTom Tucker void svc_reserve(struct svc_rqst *rqstp, int space) 5000f0257eaSTom Tucker { 501d4b09acfSVasily Averin struct svc_xprt *xprt = rqstp->rq_xprt; 502d4b09acfSVasily Averin 5030f0257eaSTom Tucker space += rqstp->rq_res.head[0].iov_len; 5040f0257eaSTom Tucker 505d4b09acfSVasily Averin if (xprt && space < rqstp->rq_reserved) { 5060f0257eaSTom Tucker atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved); 5070f0257eaSTom Tucker rqstp->rq_reserved = space; 50895503d29SJ. Bruce Fields smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */ 5090f0257eaSTom Tucker svc_xprt_enqueue(xprt); 5100f0257eaSTom Tucker } 5110f0257eaSTom Tucker } 51224c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_reserve); 5130f0257eaSTom Tucker 5140f0257eaSTom Tucker static void svc_xprt_release(struct svc_rqst *rqstp) 5150f0257eaSTom Tucker { 5160f0257eaSTom Tucker struct svc_xprt *xprt = rqstp->rq_xprt; 5170f0257eaSTom Tucker 51863a1b156SChuck Lever xprt->xpt_ops->xpo_release_rqst(rqstp); 5190f0257eaSTom Tucker 5202779e3aeSTom Tucker kfree(rqstp->rq_deferred); 5212779e3aeSTom Tucker rqstp->rq_deferred = NULL; 5222779e3aeSTom Tucker 5230f0257eaSTom Tucker svc_free_res_pages(rqstp); 5240f0257eaSTom Tucker rqstp->rq_res.page_len = 0; 5250f0257eaSTom Tucker rqstp->rq_res.page_base = 0; 5260f0257eaSTom Tucker 5270f0257eaSTom Tucker /* Reset response buffer and release 5280f0257eaSTom Tucker * the reservation. 5290f0257eaSTom Tucker * But first, check that enough space was reserved 5300f0257eaSTom Tucker * for the reply, otherwise we have a bug! 5310f0257eaSTom Tucker */ 5320f0257eaSTom Tucker if ((rqstp->rq_res.len) > rqstp->rq_reserved) 5330f0257eaSTom Tucker printk(KERN_ERR "RPC request reserved %d but used %d\n", 5340f0257eaSTom Tucker rqstp->rq_reserved, 5350f0257eaSTom Tucker rqstp->rq_res.len); 5360f0257eaSTom Tucker 5370f0257eaSTom Tucker rqstp->rq_res.head[0].iov_len = 0; 5380f0257eaSTom Tucker svc_reserve(rqstp, 0); 539ff3ac5c3STrond Myklebust svc_xprt_release_slot(rqstp); 5400f0257eaSTom Tucker rqstp->rq_xprt = NULL; 5410f0257eaSTom Tucker svc_xprt_put(xprt); 5420f0257eaSTom Tucker } 5430f0257eaSTom Tucker 5440f0257eaSTom Tucker /* 545ceff739cSJeff Layton * Some svc_serv's will have occasional work to do, even when a xprt is not 546ceff739cSJeff Layton * waiting to be serviced. This function is there to "kick" a task in one of 547ceff739cSJeff Layton * those services so that it can wake up and do that work. Note that we only 548ceff739cSJeff Layton * bother with pool 0 as we don't need to wake up more than one thread for 549ceff739cSJeff Layton * this purpose. 5500f0257eaSTom Tucker */ 5510f0257eaSTom Tucker void svc_wake_up(struct svc_serv *serv) 5520f0257eaSTom Tucker { 5530f0257eaSTom Tucker struct svc_rqst *rqstp; 5540f0257eaSTom Tucker struct svc_pool *pool; 5550f0257eaSTom Tucker 556ceff739cSJeff Layton pool = &serv->sv_pools[0]; 5570f0257eaSTom Tucker 558b1691bc0SJeff Layton rcu_read_lock(); 559b1691bc0SJeff Layton list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { 560b1691bc0SJeff Layton /* skip any that aren't queued */ 561b1691bc0SJeff Layton if (test_bit(RQ_BUSY, &rqstp->rq_flags)) 562b1691bc0SJeff Layton continue; 563b1691bc0SJeff Layton rcu_read_unlock(); 564983c6844STrond Myklebust wake_up_process(rqstp->rq_task); 56583a712e0SJeff Layton trace_svc_wake_up(rqstp->rq_task->pid); 566b1691bc0SJeff Layton return; 567b1691bc0SJeff Layton } 568b1691bc0SJeff Layton rcu_read_unlock(); 569b1691bc0SJeff Layton 570b1691bc0SJeff Layton /* No free entries available */ 5714d5db3f5SJeff Layton set_bit(SP_TASK_PENDING, &pool->sp_flags); 572b1691bc0SJeff Layton smp_wmb(); 57383a712e0SJeff Layton trace_svc_wake_up(0); 5740f0257eaSTom Tucker } 57524c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_wake_up); 5760f0257eaSTom Tucker 5770f0257eaSTom Tucker int svc_port_is_privileged(struct sockaddr *sin) 5780f0257eaSTom Tucker { 5790f0257eaSTom Tucker switch (sin->sa_family) { 5800f0257eaSTom Tucker case AF_INET: 5810f0257eaSTom Tucker return ntohs(((struct sockaddr_in *)sin)->sin_port) 5820f0257eaSTom Tucker < PROT_SOCK; 5830f0257eaSTom Tucker case AF_INET6: 5840f0257eaSTom Tucker return ntohs(((struct sockaddr_in6 *)sin)->sin6_port) 5850f0257eaSTom Tucker < PROT_SOCK; 5860f0257eaSTom Tucker default: 5870f0257eaSTom Tucker return 0; 5880f0257eaSTom Tucker } 5890f0257eaSTom Tucker } 5900f0257eaSTom Tucker 5910f0257eaSTom Tucker /* 592c9233eb7SJeff Layton * Make sure that we don't have too many active connections. If we have, 593c9233eb7SJeff Layton * something must be dropped. It's not clear what will happen if we allow 594c9233eb7SJeff Layton * "too many" connections, but when dealing with network-facing software, 595c9233eb7SJeff Layton * we have to code defensively. Here we do that by imposing hard limits. 5960f0257eaSTom Tucker * 5970f0257eaSTom Tucker * There's no point in trying to do random drop here for DoS 5980f0257eaSTom Tucker * prevention. The NFS clients does 1 reconnect in 15 seconds. An 5990f0257eaSTom Tucker * attacker can easily beat that. 6000f0257eaSTom Tucker * 6010f0257eaSTom Tucker * The only somewhat efficient mechanism would be if drop old 6020f0257eaSTom Tucker * connections from the same IP first. But right now we don't even 6030f0257eaSTom Tucker * record the client IP in svc_sock. 604c9233eb7SJeff Layton * 605c9233eb7SJeff Layton * single-threaded services that expect a lot of clients will probably 606c9233eb7SJeff Layton * need to set sv_maxconn to override the default value which is based 607c9233eb7SJeff Layton * on the number of threads 6080f0257eaSTom Tucker */ 6090f0257eaSTom Tucker static void svc_check_conn_limits(struct svc_serv *serv) 6100f0257eaSTom Tucker { 611c9233eb7SJeff Layton unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn : 612c9233eb7SJeff Layton (serv->sv_nrthreads+3) * 20; 613c9233eb7SJeff Layton 614c9233eb7SJeff Layton if (serv->sv_tmpcnt > limit) { 6150f0257eaSTom Tucker struct svc_xprt *xprt = NULL; 6160f0257eaSTom Tucker spin_lock_bh(&serv->sv_lock); 6170f0257eaSTom Tucker if (!list_empty(&serv->sv_tempsocks)) { 6180f0257eaSTom Tucker /* Try to help the admin */ 619e87cc472SJoe Perches net_notice_ratelimited("%s: too many open connections, consider increasing the %s\n", 620c9233eb7SJeff Layton serv->sv_name, serv->sv_maxconn ? 621e87cc472SJoe Perches "max number of connections" : 622e87cc472SJoe Perches "number of threads"); 6230f0257eaSTom Tucker /* 6240f0257eaSTom Tucker * Always select the oldest connection. It's not fair, 6250f0257eaSTom Tucker * but so is life 6260f0257eaSTom Tucker */ 6270f0257eaSTom Tucker xprt = list_entry(serv->sv_tempsocks.prev, 6280f0257eaSTom Tucker struct svc_xprt, 6290f0257eaSTom Tucker xpt_list); 6300f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 6310f0257eaSTom Tucker svc_xprt_get(xprt); 6320f0257eaSTom Tucker } 6330f0257eaSTom Tucker spin_unlock_bh(&serv->sv_lock); 6340f0257eaSTom Tucker 6350f0257eaSTom Tucker if (xprt) { 6360f0257eaSTom Tucker svc_xprt_enqueue(xprt); 6370f0257eaSTom Tucker svc_xprt_put(xprt); 6380f0257eaSTom Tucker } 6390f0257eaSTom Tucker } 6400f0257eaSTom Tucker } 6410f0257eaSTom Tucker 642e1d83ee6SRashika Kheria static int svc_alloc_arg(struct svc_rqst *rqstp) 6430f0257eaSTom Tucker { 6440f0257eaSTom Tucker struct svc_serv *serv = rqstp->rq_server; 6450f0257eaSTom Tucker struct xdr_buf *arg; 6466797fa5aSJ. Bruce Fields int pages; 6476797fa5aSJ. Bruce Fields int i; 6480f0257eaSTom Tucker 6490f0257eaSTom Tucker /* now allocate needed pages. If we get a failure, sleep briefly */ 6508c6ae498SChuck Lever pages = (serv->sv_max_mesg + 2 * PAGE_SIZE) >> PAGE_SHIFT; 6518c6ae498SChuck Lever if (pages > RPCSVC_MAXPAGES) { 6528c6ae498SChuck Lever pr_warn_once("svc: warning: pages=%u > RPCSVC_MAXPAGES=%lu\n", 6538c6ae498SChuck Lever pages, RPCSVC_MAXPAGES); 654b25cd058SWeston Andros Adamson /* use as many pages as possible */ 6558c6ae498SChuck Lever pages = RPCSVC_MAXPAGES; 6568c6ae498SChuck Lever } 6570f0257eaSTom Tucker for (i = 0; i < pages ; i++) 6580f0257eaSTom Tucker while (rqstp->rq_pages[i] == NULL) { 6590f0257eaSTom Tucker struct page *p = alloc_page(GFP_KERNEL); 6600f0257eaSTom Tucker if (!p) { 6617b54fe61SJeff Layton set_current_state(TASK_INTERRUPTIBLE); 6627b54fe61SJeff Layton if (signalled() || kthread_should_stop()) { 6637b54fe61SJeff Layton set_current_state(TASK_RUNNING); 6647086721fSJeff Layton return -EINTR; 6657b54fe61SJeff Layton } 6667b54fe61SJeff Layton schedule_timeout(msecs_to_jiffies(500)); 6670f0257eaSTom Tucker } 6680f0257eaSTom Tucker rqstp->rq_pages[i] = p; 6690f0257eaSTom Tucker } 6702825a7f9SJ. Bruce Fields rqstp->rq_page_end = &rqstp->rq_pages[i]; 6710f0257eaSTom Tucker rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */ 6720f0257eaSTom Tucker 6730f0257eaSTom Tucker /* Make arg->head point to first page and arg->pages point to rest */ 6740f0257eaSTom Tucker arg = &rqstp->rq_arg; 6750f0257eaSTom Tucker arg->head[0].iov_base = page_address(rqstp->rq_pages[0]); 6760f0257eaSTom Tucker arg->head[0].iov_len = PAGE_SIZE; 6770f0257eaSTom Tucker arg->pages = rqstp->rq_pages + 1; 6780f0257eaSTom Tucker arg->page_base = 0; 6790f0257eaSTom Tucker /* save at least one page for response */ 6800f0257eaSTom Tucker arg->page_len = (pages-2)*PAGE_SIZE; 6810f0257eaSTom Tucker arg->len = (pages-1)*PAGE_SIZE; 6820f0257eaSTom Tucker arg->tail[0].iov_len = 0; 6836797fa5aSJ. Bruce Fields return 0; 6846797fa5aSJ. Bruce Fields } 6850f0257eaSTom Tucker 686b1691bc0SJeff Layton static bool 687b1691bc0SJeff Layton rqst_should_sleep(struct svc_rqst *rqstp) 688b1691bc0SJeff Layton { 689b1691bc0SJeff Layton struct svc_pool *pool = rqstp->rq_pool; 690b1691bc0SJeff Layton 691b1691bc0SJeff Layton /* did someone call svc_wake_up? */ 692b1691bc0SJeff Layton if (test_and_clear_bit(SP_TASK_PENDING, &pool->sp_flags)) 693b1691bc0SJeff Layton return false; 694b1691bc0SJeff Layton 695b1691bc0SJeff Layton /* was a socket queued? */ 696b1691bc0SJeff Layton if (!list_empty(&pool->sp_sockets)) 697b1691bc0SJeff Layton return false; 698b1691bc0SJeff Layton 699b1691bc0SJeff Layton /* are we shutting down? */ 700b1691bc0SJeff Layton if (signalled() || kthread_should_stop()) 701b1691bc0SJeff Layton return false; 702b1691bc0SJeff Layton 703b1691bc0SJeff Layton /* are we freezing? */ 704b1691bc0SJeff Layton if (freezing(current)) 705b1691bc0SJeff Layton return false; 706b1691bc0SJeff Layton 707b1691bc0SJeff Layton return true; 708b1691bc0SJeff Layton } 709b1691bc0SJeff Layton 710e1d83ee6SRashika Kheria static struct svc_xprt *svc_get_next_xprt(struct svc_rqst *rqstp, long timeout) 7116797fa5aSJ. Bruce Fields { 7126797fa5aSJ. Bruce Fields struct svc_pool *pool = rqstp->rq_pool; 713a4aa8054STrond Myklebust long time_left = 0; 7140f0257eaSTom Tucker 715b1691bc0SJeff Layton /* rq_xprt should be clear on entry */ 716b1691bc0SJeff Layton WARN_ON_ONCE(rqstp->rq_xprt); 717b1691bc0SJeff Layton 71822700f3cSTrond Myklebust rqstp->rq_xprt = svc_xprt_dequeue(pool); 71922700f3cSTrond Myklebust if (rqstp->rq_xprt) 72022700f3cSTrond Myklebust goto out_found; 721b1691bc0SJeff Layton 7220f0257eaSTom Tucker /* 7230f0257eaSTom Tucker * We have to be able to interrupt this wait 7240f0257eaSTom Tucker * to bring down the daemons ... 7250f0257eaSTom Tucker */ 7260f0257eaSTom Tucker set_current_state(TASK_INTERRUPTIBLE); 72722700f3cSTrond Myklebust smp_mb__before_atomic(); 72822700f3cSTrond Myklebust clear_bit(SP_CONGESTED, &pool->sp_flags); 729b1691bc0SJeff Layton clear_bit(RQ_BUSY, &rqstp->rq_flags); 73022700f3cSTrond Myklebust smp_mb__after_atomic(); 7317086721fSJeff Layton 732b1691bc0SJeff Layton if (likely(rqst_should_sleep(rqstp))) 73303cf6c9fSGreg Banks time_left = schedule_timeout(timeout); 734b1691bc0SJeff Layton else 735983c6844STrond Myklebust __set_current_state(TASK_RUNNING); 7360f0257eaSTom Tucker 7370f0257eaSTom Tucker try_to_freeze(); 7380f0257eaSTom Tucker 739b1691bc0SJeff Layton set_bit(RQ_BUSY, &rqstp->rq_flags); 74022700f3cSTrond Myklebust smp_mb__after_atomic(); 74122700f3cSTrond Myklebust rqstp->rq_xprt = svc_xprt_dequeue(pool); 74222700f3cSTrond Myklebust if (rqstp->rq_xprt) 74322700f3cSTrond Myklebust goto out_found; 744106f359cSTrond Myklebust 74503cf6c9fSGreg Banks if (!time_left) 746403c7b44SJeff Layton atomic_long_inc(&pool->sp_stats.threads_timedout); 7470f0257eaSTom Tucker 7487086721fSJeff Layton if (signalled() || kthread_should_stop()) 7496797fa5aSJ. Bruce Fields return ERR_PTR(-EINTR); 7506797fa5aSJ. Bruce Fields return ERR_PTR(-EAGAIN); 75122700f3cSTrond Myklebust out_found: 75222700f3cSTrond Myklebust /* Normally we will wait up to 5 seconds for any required 75322700f3cSTrond Myklebust * cache information to be provided. 75422700f3cSTrond Myklebust */ 75522700f3cSTrond Myklebust if (!test_bit(SP_CONGESTED, &pool->sp_flags)) 75622700f3cSTrond Myklebust rqstp->rq_chandle.thread_wait = 5*HZ; 75722700f3cSTrond Myklebust else 75822700f3cSTrond Myklebust rqstp->rq_chandle.thread_wait = 1*HZ; 75955f5088cSChuck Lever trace_svc_xprt_dequeue(rqstp); 76022700f3cSTrond Myklebust return rqstp->rq_xprt; 7610f0257eaSTom Tucker } 7620f0257eaSTom Tucker 763e1d83ee6SRashika Kheria static void svc_add_new_temp_xprt(struct svc_serv *serv, struct svc_xprt *newxpt) 76465b2e665SJ. Bruce Fields { 76565b2e665SJ. Bruce Fields spin_lock_bh(&serv->sv_lock); 76665b2e665SJ. Bruce Fields set_bit(XPT_TEMP, &newxpt->xpt_flags); 76765b2e665SJ. Bruce Fields list_add(&newxpt->xpt_list, &serv->sv_tempsocks); 76865b2e665SJ. Bruce Fields serv->sv_tmpcnt++; 76965b2e665SJ. Bruce Fields if (serv->sv_temptimer.function == NULL) { 77065b2e665SJ. Bruce Fields /* setup timer to age temp transports */ 771841b86f3SKees Cook serv->sv_temptimer.function = svc_age_temp_xprts; 77265b2e665SJ. Bruce Fields mod_timer(&serv->sv_temptimer, 77365b2e665SJ. Bruce Fields jiffies + svc_conn_age_period * HZ); 77465b2e665SJ. Bruce Fields } 77565b2e665SJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 77665b2e665SJ. Bruce Fields svc_xprt_received(newxpt); 77765b2e665SJ. Bruce Fields } 77865b2e665SJ. Bruce Fields 7796797fa5aSJ. Bruce Fields static int svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt) 7806797fa5aSJ. Bruce Fields { 7816797fa5aSJ. Bruce Fields struct svc_serv *serv = rqstp->rq_server; 7826797fa5aSJ. Bruce Fields int len = 0; 7836797fa5aSJ. Bruce Fields 7841b644b6eSJ. Bruce Fields if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) { 785546125d1SScott Mayhew if (test_and_clear_bit(XPT_KILL_TEMP, &xprt->xpt_flags)) 786546125d1SScott Mayhew xprt->xpt_ops->xpo_kill_temp_xprt(xprt); 7871b644b6eSJ. Bruce Fields svc_delete_xprt(xprt); 788ca7896cdSJ. Bruce Fields /* Leave XPT_BUSY set on the dead xprt: */ 78983a712e0SJeff Layton goto out; 790ca7896cdSJ. Bruce Fields } 791ca7896cdSJ. Bruce Fields if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) { 7920f0257eaSTom Tucker struct svc_xprt *newxpt; 7930f0257eaSTom Tucker /* 7940f0257eaSTom Tucker * We know this module_get will succeed because the 7950f0257eaSTom Tucker * listener holds a reference too 7960f0257eaSTom Tucker */ 79765b2e665SJ. Bruce Fields __module_get(xprt->xpt_class->xcl_owner); 7980f0257eaSTom Tucker svc_check_conn_limits(xprt->xpt_server); 79965b2e665SJ. Bruce Fields newxpt = xprt->xpt_ops->xpo_accept(xprt); 8001237d354STrond Myklebust if (newxpt) { 8011237d354STrond Myklebust newxpt->xpt_cred = get_cred(xprt->xpt_cred); 80265b2e665SJ. Bruce Fields svc_add_new_temp_xprt(serv, newxpt); 80311bbb0f7SChuck Lever trace_svc_xprt_accept(newxpt, serv->sv_name); 8041237d354STrond Myklebust } else 805c789102cSTrond Myklebust module_put(xprt->xpt_class->xcl_owner); 806ff3ac5c3STrond Myklebust } else if (svc_xprt_reserve_slot(rqstp, xprt)) { 8076797fa5aSJ. Bruce Fields /* XPT_DATA|XPT_DEFERRED case: */ 8080f0257eaSTom Tucker dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n", 8096797fa5aSJ. Bruce Fields rqstp, rqstp->rq_pool->sp_id, xprt, 8102c935bc5SPeter Zijlstra kref_read(&xprt->xpt_ref)); 8110f0257eaSTom Tucker rqstp->rq_deferred = svc_deferred_dequeue(xprt); 812ca7896cdSJ. Bruce Fields if (rqstp->rq_deferred) 8130f0257eaSTom Tucker len = svc_deferred_recv(rqstp); 814ca7896cdSJ. Bruce Fields else 8150f0257eaSTom Tucker len = xprt->xpt_ops->xpo_recvfrom(rqstp); 816b20dfc3fSChuck Lever if (len > 0) 817b20dfc3fSChuck Lever trace_svc_recvfrom(&rqstp->rq_arg); 818aaba72cdSChuck Lever rqstp->rq_stime = ktime_get(); 819d10f27a7SJ. Bruce Fields rqstp->rq_reserved = serv->sv_max_mesg; 820d10f27a7SJ. Bruce Fields atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved); 8210f0257eaSTom Tucker } 8226797fa5aSJ. Bruce Fields /* clear XPT_BUSY: */ 823ca7896cdSJ. Bruce Fields svc_xprt_received(xprt); 82483a712e0SJeff Layton out: 82583a712e0SJeff Layton trace_svc_handle_xprt(xprt, len); 8266797fa5aSJ. Bruce Fields return len; 8276797fa5aSJ. Bruce Fields } 8286797fa5aSJ. Bruce Fields 8296797fa5aSJ. Bruce Fields /* 8306797fa5aSJ. Bruce Fields * Receive the next request on any transport. This code is carefully 8316797fa5aSJ. Bruce Fields * organised not to touch any cachelines in the shared svc_serv 8326797fa5aSJ. Bruce Fields * structure, only cachelines in the local svc_pool. 8336797fa5aSJ. Bruce Fields */ 8346797fa5aSJ. Bruce Fields int svc_recv(struct svc_rqst *rqstp, long timeout) 8356797fa5aSJ. Bruce Fields { 8366797fa5aSJ. Bruce Fields struct svc_xprt *xprt = NULL; 8376797fa5aSJ. Bruce Fields struct svc_serv *serv = rqstp->rq_server; 8386797fa5aSJ. Bruce Fields int len, err; 8396797fa5aSJ. Bruce Fields 8406797fa5aSJ. Bruce Fields err = svc_alloc_arg(rqstp); 8416797fa5aSJ. Bruce Fields if (err) 842860a0d9eSJeff Layton goto out; 8436797fa5aSJ. Bruce Fields 8446797fa5aSJ. Bruce Fields try_to_freeze(); 8456797fa5aSJ. Bruce Fields cond_resched(); 846860a0d9eSJeff Layton err = -EINTR; 8476797fa5aSJ. Bruce Fields if (signalled() || kthread_should_stop()) 848860a0d9eSJeff Layton goto out; 8496797fa5aSJ. Bruce Fields 8506797fa5aSJ. Bruce Fields xprt = svc_get_next_xprt(rqstp, timeout); 851860a0d9eSJeff Layton if (IS_ERR(xprt)) { 852860a0d9eSJeff Layton err = PTR_ERR(xprt); 853860a0d9eSJeff Layton goto out; 854860a0d9eSJeff Layton } 8556797fa5aSJ. Bruce Fields 8566797fa5aSJ. Bruce Fields len = svc_handle_xprt(rqstp, xprt); 8570f0257eaSTom Tucker 8580f0257eaSTom Tucker /* No data, incomplete (TCP) read, or accept() */ 859860a0d9eSJeff Layton err = -EAGAIN; 8609f9d2ebeSJ. Bruce Fields if (len <= 0) 861860a0d9eSJeff Layton goto out_release; 862ca7896cdSJ. Bruce Fields 8630f0257eaSTom Tucker clear_bit(XPT_OLD, &xprt->xpt_flags); 8640f0257eaSTom Tucker 865989f881eSChuck Lever xprt->xpt_ops->xpo_secure_port(rqstp); 8660f0257eaSTom Tucker rqstp->rq_chandle.defer = svc_defer; 867860a0d9eSJeff Layton rqstp->rq_xid = svc_getu32(&rqstp->rq_arg.head[0]); 8680f0257eaSTom Tucker 8690f0257eaSTom Tucker if (serv->sv_stats) 8700f0257eaSTom Tucker serv->sv_stats->netcnt++; 871860a0d9eSJeff Layton trace_svc_recv(rqstp, len); 8720f0257eaSTom Tucker return len; 873860a0d9eSJeff Layton out_release: 874ca7896cdSJ. Bruce Fields rqstp->rq_res.len = 0; 875ca7896cdSJ. Bruce Fields svc_xprt_release(rqstp); 876860a0d9eSJeff Layton out: 877860a0d9eSJeff Layton return err; 8780f0257eaSTom Tucker } 87924c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_recv); 8800f0257eaSTom Tucker 8810f0257eaSTom Tucker /* 8820f0257eaSTom Tucker * Drop request 8830f0257eaSTom Tucker */ 8840f0257eaSTom Tucker void svc_drop(struct svc_rqst *rqstp) 8850f0257eaSTom Tucker { 886104f6351STrond Myklebust trace_svc_drop(rqstp); 8870f0257eaSTom Tucker svc_xprt_release(rqstp); 8880f0257eaSTom Tucker } 88924c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_drop); 8900f0257eaSTom Tucker 8910f0257eaSTom Tucker /* 8920f0257eaSTom Tucker * Return reply to client. 8930f0257eaSTom Tucker */ 8940f0257eaSTom Tucker int svc_send(struct svc_rqst *rqstp) 8950f0257eaSTom Tucker { 8960f0257eaSTom Tucker struct svc_xprt *xprt; 897860a0d9eSJeff Layton int len = -EFAULT; 8980f0257eaSTom Tucker struct xdr_buf *xb; 8990f0257eaSTom Tucker 9000f0257eaSTom Tucker xprt = rqstp->rq_xprt; 9010f0257eaSTom Tucker if (!xprt) 902860a0d9eSJeff Layton goto out; 9030f0257eaSTom Tucker 9040f0257eaSTom Tucker /* calculate over-all length */ 9050f0257eaSTom Tucker xb = &rqstp->rq_res; 9060f0257eaSTom Tucker xb->len = xb->head[0].iov_len + 9070f0257eaSTom Tucker xb->page_len + 9080f0257eaSTom Tucker xb->tail[0].iov_len; 909b20dfc3fSChuck Lever trace_svc_sendto(xb); 910aaba72cdSChuck Lever trace_svc_stats_latency(rqstp); 911ca4faf54SChuck Lever 9120f0257eaSTom Tucker len = xprt->xpt_ops->xpo_sendto(rqstp); 913ca4faf54SChuck Lever 914ece200ddSChuck Lever trace_svc_send(rqstp, len); 9150f0257eaSTom Tucker svc_xprt_release(rqstp); 9160f0257eaSTom Tucker 9170f0257eaSTom Tucker if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN) 918860a0d9eSJeff Layton len = 0; 919860a0d9eSJeff Layton out: 9200f0257eaSTom Tucker return len; 9210f0257eaSTom Tucker } 9220f0257eaSTom Tucker 9230f0257eaSTom Tucker /* 9240f0257eaSTom Tucker * Timer function to close old temporary transports, using 9250f0257eaSTom Tucker * a mark-and-sweep algorithm. 9260f0257eaSTom Tucker */ 927ff861c4dSKees Cook static void svc_age_temp_xprts(struct timer_list *t) 9280f0257eaSTom Tucker { 929ff861c4dSKees Cook struct svc_serv *serv = from_timer(serv, t, sv_temptimer); 9300f0257eaSTom Tucker struct svc_xprt *xprt; 9310f0257eaSTom Tucker struct list_head *le, *next; 9320f0257eaSTom Tucker 9330f0257eaSTom Tucker dprintk("svc_age_temp_xprts\n"); 9340f0257eaSTom Tucker 9350f0257eaSTom Tucker if (!spin_trylock_bh(&serv->sv_lock)) { 9360f0257eaSTom Tucker /* busy, try again 1 sec later */ 9370f0257eaSTom Tucker dprintk("svc_age_temp_xprts: busy\n"); 9380f0257eaSTom Tucker mod_timer(&serv->sv_temptimer, jiffies + HZ); 9390f0257eaSTom Tucker return; 9400f0257eaSTom Tucker } 9410f0257eaSTom Tucker 9420f0257eaSTom Tucker list_for_each_safe(le, next, &serv->sv_tempsocks) { 9430f0257eaSTom Tucker xprt = list_entry(le, struct svc_xprt, xpt_list); 9440f0257eaSTom Tucker 9450f0257eaSTom Tucker /* First time through, just mark it OLD. Second time 9460f0257eaSTom Tucker * through, close it. */ 9470f0257eaSTom Tucker if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags)) 9480f0257eaSTom Tucker continue; 9492c935bc5SPeter Zijlstra if (kref_read(&xprt->xpt_ref) > 1 || 950f64f9e71SJoe Perches test_bit(XPT_BUSY, &xprt->xpt_flags)) 9510f0257eaSTom Tucker continue; 952e75bafbfSJ. Bruce Fields list_del_init(le); 9530f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 9540f0257eaSTom Tucker dprintk("queuing xprt %p for closing\n", xprt); 9550f0257eaSTom Tucker 9560f0257eaSTom Tucker /* a thread will dequeue and close it soon */ 9570f0257eaSTom Tucker svc_xprt_enqueue(xprt); 9580f0257eaSTom Tucker } 959e75bafbfSJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 9600f0257eaSTom Tucker 9610f0257eaSTom Tucker mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ); 9620f0257eaSTom Tucker } 9630f0257eaSTom Tucker 964c3d4879eSScott Mayhew /* Close temporary transports whose xpt_local matches server_addr immediately 965c3d4879eSScott Mayhew * instead of waiting for them to be picked up by the timer. 966c3d4879eSScott Mayhew * 967c3d4879eSScott Mayhew * This is meant to be called from a notifier_block that runs when an ip 968c3d4879eSScott Mayhew * address is deleted. 969c3d4879eSScott Mayhew */ 970c3d4879eSScott Mayhew void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) 971c3d4879eSScott Mayhew { 972c3d4879eSScott Mayhew struct svc_xprt *xprt; 973c3d4879eSScott Mayhew struct list_head *le, *next; 974c3d4879eSScott Mayhew LIST_HEAD(to_be_closed); 975c3d4879eSScott Mayhew 976c3d4879eSScott Mayhew spin_lock_bh(&serv->sv_lock); 977c3d4879eSScott Mayhew list_for_each_safe(le, next, &serv->sv_tempsocks) { 978c3d4879eSScott Mayhew xprt = list_entry(le, struct svc_xprt, xpt_list); 979c3d4879eSScott Mayhew if (rpc_cmp_addr(server_addr, (struct sockaddr *) 980c3d4879eSScott Mayhew &xprt->xpt_local)) { 981c3d4879eSScott Mayhew dprintk("svc_age_temp_xprts_now: found %p\n", xprt); 982c3d4879eSScott Mayhew list_move(le, &to_be_closed); 983c3d4879eSScott Mayhew } 984c3d4879eSScott Mayhew } 985c3d4879eSScott Mayhew spin_unlock_bh(&serv->sv_lock); 986c3d4879eSScott Mayhew 987c3d4879eSScott Mayhew while (!list_empty(&to_be_closed)) { 988c3d4879eSScott Mayhew le = to_be_closed.next; 989c3d4879eSScott Mayhew list_del_init(le); 990c3d4879eSScott Mayhew xprt = list_entry(le, struct svc_xprt, xpt_list); 991546125d1SScott Mayhew set_bit(XPT_CLOSE, &xprt->xpt_flags); 992546125d1SScott Mayhew set_bit(XPT_KILL_TEMP, &xprt->xpt_flags); 993546125d1SScott Mayhew dprintk("svc_age_temp_xprts_now: queuing xprt %p for closing\n", 994546125d1SScott Mayhew xprt); 995546125d1SScott Mayhew svc_xprt_enqueue(xprt); 996c3d4879eSScott Mayhew } 997c3d4879eSScott Mayhew } 998c3d4879eSScott Mayhew EXPORT_SYMBOL_GPL(svc_age_temp_xprts_now); 999c3d4879eSScott Mayhew 1000edc7a894SJ. Bruce Fields static void call_xpt_users(struct svc_xprt *xprt) 1001edc7a894SJ. Bruce Fields { 1002edc7a894SJ. Bruce Fields struct svc_xpt_user *u; 1003edc7a894SJ. Bruce Fields 1004edc7a894SJ. Bruce Fields spin_lock(&xprt->xpt_lock); 1005edc7a894SJ. Bruce Fields while (!list_empty(&xprt->xpt_users)) { 1006edc7a894SJ. Bruce Fields u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list); 1007bb6ad557STrond Myklebust list_del_init(&u->list); 1008edc7a894SJ. Bruce Fields u->callback(u); 1009edc7a894SJ. Bruce Fields } 1010edc7a894SJ. Bruce Fields spin_unlock(&xprt->xpt_lock); 1011edc7a894SJ. Bruce Fields } 1012edc7a894SJ. Bruce Fields 10130f0257eaSTom Tucker /* 10140f0257eaSTom Tucker * Remove a dead transport 10150f0257eaSTom Tucker */ 10167710ec36SJ. Bruce Fields static void svc_delete_xprt(struct svc_xprt *xprt) 10170f0257eaSTom Tucker { 10180f0257eaSTom Tucker struct svc_serv *serv = xprt->xpt_server; 101922945e4aSTom Tucker struct svc_deferred_req *dr; 102022945e4aSTom Tucker 102122945e4aSTom Tucker if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) 102211bbb0f7SChuck Lever return; 10230f0257eaSTom Tucker 102411bbb0f7SChuck Lever trace_svc_xprt_detach(xprt); 10250f0257eaSTom Tucker xprt->xpt_ops->xpo_detach(xprt); 10266221f1d9SChuck Lever if (xprt->xpt_bc_xprt) 10276221f1d9SChuck Lever xprt->xpt_bc_xprt->ops->close(xprt->xpt_bc_xprt); 10280f0257eaSTom Tucker 10290f0257eaSTom Tucker spin_lock_bh(&serv->sv_lock); 10300f0257eaSTom Tucker list_del_init(&xprt->xpt_list); 103101047298SWeston Andros Adamson WARN_ON_ONCE(!list_empty(&xprt->xpt_ready)); 10320f0257eaSTom Tucker if (test_bit(XPT_TEMP, &xprt->xpt_flags)) 10330f0257eaSTom Tucker serv->sv_tmpcnt--; 1034788e69e5SJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 103522945e4aSTom Tucker 1036ab1b18f7SNeil Brown while ((dr = svc_deferred_dequeue(xprt)) != NULL) 103722945e4aSTom Tucker kfree(dr); 103822945e4aSTom Tucker 1039edc7a894SJ. Bruce Fields call_xpt_users(xprt); 104022945e4aSTom Tucker svc_xprt_put(xprt); 10410f0257eaSTom Tucker } 10420f0257eaSTom Tucker 10430f0257eaSTom Tucker void svc_close_xprt(struct svc_xprt *xprt) 10440f0257eaSTom Tucker { 104511bbb0f7SChuck Lever trace_svc_xprt_close(xprt); 10460f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 10470f0257eaSTom Tucker if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) 10480f0257eaSTom Tucker /* someone else will have to effect the close */ 10490f0257eaSTom Tucker return; 1050b1763316SJ. Bruce Fields /* 1051b1763316SJ. Bruce Fields * We expect svc_close_xprt() to work even when no threads are 1052b1763316SJ. Bruce Fields * running (e.g., while configuring the server before starting 1053b1763316SJ. Bruce Fields * any threads), so if the transport isn't busy, we delete 1054b1763316SJ. Bruce Fields * it ourself: 1055b1763316SJ. Bruce Fields */ 10560f0257eaSTom Tucker svc_delete_xprt(xprt); 10570f0257eaSTom Tucker } 1058a217813fSTom Tucker EXPORT_SYMBOL_GPL(svc_close_xprt); 10590f0257eaSTom Tucker 1060cc630d9fSJ. Bruce Fields static int svc_close_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net) 10610f0257eaSTom Tucker { 10620f0257eaSTom Tucker struct svc_xprt *xprt; 1063cc630d9fSJ. Bruce Fields int ret = 0; 10640f0257eaSTom Tucker 1065719f8bccSJ. Bruce Fields spin_lock(&serv->sv_lock); 1066b4f36f88SJ. Bruce Fields list_for_each_entry(xprt, xprt_list, xpt_list) { 10677b147f1fSStanislav Kinsbursky if (xprt->xpt_net != net) 10687b147f1fSStanislav Kinsbursky continue; 1069cc630d9fSJ. Bruce Fields ret++; 10700f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 1071cc630d9fSJ. Bruce Fields svc_xprt_enqueue(xprt); 10720f0257eaSTom Tucker } 1073719f8bccSJ. Bruce Fields spin_unlock(&serv->sv_lock); 1074cc630d9fSJ. Bruce Fields return ret; 10750f0257eaSTom Tucker } 10760f0257eaSTom Tucker 1077cc630d9fSJ. Bruce Fields static struct svc_xprt *svc_dequeue_net(struct svc_serv *serv, struct net *net) 10782fefb8a0SJ. Bruce Fields { 1079b4f36f88SJ. Bruce Fields struct svc_pool *pool; 1080b4f36f88SJ. Bruce Fields struct svc_xprt *xprt; 1081b4f36f88SJ. Bruce Fields struct svc_xprt *tmp; 1082b4f36f88SJ. Bruce Fields int i; 1083b4f36f88SJ. Bruce Fields 1084b4f36f88SJ. Bruce Fields for (i = 0; i < serv->sv_nrpools; i++) { 1085b4f36f88SJ. Bruce Fields pool = &serv->sv_pools[i]; 1086b4f36f88SJ. Bruce Fields 1087b4f36f88SJ. Bruce Fields spin_lock_bh(&pool->sp_lock); 10886f513365SStanislav Kinsbursky list_for_each_entry_safe(xprt, tmp, &pool->sp_sockets, xpt_ready) { 10897b147f1fSStanislav Kinsbursky if (xprt->xpt_net != net) 10907b147f1fSStanislav Kinsbursky continue; 1091b4f36f88SJ. Bruce Fields list_del_init(&xprt->xpt_ready); 1092cc630d9fSJ. Bruce Fields spin_unlock_bh(&pool->sp_lock); 1093cc630d9fSJ. Bruce Fields return xprt; 1094b4f36f88SJ. Bruce Fields } 1095b4f36f88SJ. Bruce Fields spin_unlock_bh(&pool->sp_lock); 1096b4f36f88SJ. Bruce Fields } 1097cc630d9fSJ. Bruce Fields return NULL; 10986f513365SStanislav Kinsbursky } 10996f513365SStanislav Kinsbursky 1100cc630d9fSJ. Bruce Fields static void svc_clean_up_xprts(struct svc_serv *serv, struct net *net) 11016f513365SStanislav Kinsbursky { 11026f513365SStanislav Kinsbursky struct svc_xprt *xprt; 11036f513365SStanislav Kinsbursky 1104cc630d9fSJ. Bruce Fields while ((xprt = svc_dequeue_net(serv, net))) { 1105cc630d9fSJ. Bruce Fields set_bit(XPT_CLOSE, &xprt->xpt_flags); 1106719f8bccSJ. Bruce Fields svc_delete_xprt(xprt); 11073a22bf50SStanislav Kinsbursky } 1108cc630d9fSJ. Bruce Fields } 11093a22bf50SStanislav Kinsbursky 1110cc630d9fSJ. Bruce Fields /* 1111cc630d9fSJ. Bruce Fields * Server threads may still be running (especially in the case where the 1112cc630d9fSJ. Bruce Fields * service is still running in other network namespaces). 1113cc630d9fSJ. Bruce Fields * 1114cc630d9fSJ. Bruce Fields * So we shut down sockets the same way we would on a running server, by 1115cc630d9fSJ. Bruce Fields * setting XPT_CLOSE, enqueuing, and letting a thread pick it up to do 1116cc630d9fSJ. Bruce Fields * the close. In the case there are no such other threads, 1117cc630d9fSJ. Bruce Fields * threads running, svc_clean_up_xprts() does a simple version of a 1118cc630d9fSJ. Bruce Fields * server's main event loop, and in the case where there are other 1119cc630d9fSJ. Bruce Fields * threads, we may need to wait a little while and then check again to 1120cc630d9fSJ. Bruce Fields * see if they're done. 1121cc630d9fSJ. Bruce Fields */ 11227b147f1fSStanislav Kinsbursky void svc_close_net(struct svc_serv *serv, struct net *net) 11233a22bf50SStanislav Kinsbursky { 1124cc630d9fSJ. Bruce Fields int delay = 0; 11256f513365SStanislav Kinsbursky 1126cc630d9fSJ. Bruce Fields while (svc_close_list(serv, &serv->sv_permsocks, net) + 1127cc630d9fSJ. Bruce Fields svc_close_list(serv, &serv->sv_tempsocks, net)) { 1128cc630d9fSJ. Bruce Fields 1129cc630d9fSJ. Bruce Fields svc_clean_up_xprts(serv, net); 1130cc630d9fSJ. Bruce Fields msleep(delay++); 1131cc630d9fSJ. Bruce Fields } 11322fefb8a0SJ. Bruce Fields } 11332fefb8a0SJ. Bruce Fields 11340f0257eaSTom Tucker /* 11350f0257eaSTom Tucker * Handle defer and revisit of requests 11360f0257eaSTom Tucker */ 11370f0257eaSTom Tucker 11380f0257eaSTom Tucker static void svc_revisit(struct cache_deferred_req *dreq, int too_many) 11390f0257eaSTom Tucker { 11400f0257eaSTom Tucker struct svc_deferred_req *dr = 11410f0257eaSTom Tucker container_of(dreq, struct svc_deferred_req, handle); 11420f0257eaSTom Tucker struct svc_xprt *xprt = dr->xprt; 11430f0257eaSTom Tucker 114422945e4aSTom Tucker spin_lock(&xprt->xpt_lock); 114522945e4aSTom Tucker set_bit(XPT_DEFERRED, &xprt->xpt_flags); 114622945e4aSTom Tucker if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) { 114722945e4aSTom Tucker spin_unlock(&xprt->xpt_lock); 11488954c5c2SChuck Lever trace_svc_defer_drop(dr); 11490f0257eaSTom Tucker svc_xprt_put(xprt); 11500f0257eaSTom Tucker kfree(dr); 11510f0257eaSTom Tucker return; 11520f0257eaSTom Tucker } 11530f0257eaSTom Tucker dr->xprt = NULL; 11540f0257eaSTom Tucker list_add(&dr->handle.recent, &xprt->xpt_deferred); 11550f0257eaSTom Tucker spin_unlock(&xprt->xpt_lock); 11568954c5c2SChuck Lever trace_svc_defer_queue(dr); 11570f0257eaSTom Tucker svc_xprt_enqueue(xprt); 11580f0257eaSTom Tucker svc_xprt_put(xprt); 11590f0257eaSTom Tucker } 11600f0257eaSTom Tucker 1161260c1d12STom Tucker /* 1162260c1d12STom Tucker * Save the request off for later processing. The request buffer looks 1163260c1d12STom Tucker * like this: 1164260c1d12STom Tucker * 1165260c1d12STom Tucker * <xprt-header><rpc-header><rpc-pagelist><rpc-tail> 1166260c1d12STom Tucker * 1167260c1d12STom Tucker * This code can only handle requests that consist of an xprt-header 1168260c1d12STom Tucker * and rpc-header. 1169260c1d12STom Tucker */ 11700f0257eaSTom Tucker static struct cache_deferred_req *svc_defer(struct cache_req *req) 11710f0257eaSTom Tucker { 11720f0257eaSTom Tucker struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle); 11730f0257eaSTom Tucker struct svc_deferred_req *dr; 11740f0257eaSTom Tucker 117530660e04SJeff Layton if (rqstp->rq_arg.page_len || !test_bit(RQ_USEDEFERRAL, &rqstp->rq_flags)) 11760f0257eaSTom Tucker return NULL; /* if more than a page, give up FIXME */ 11770f0257eaSTom Tucker if (rqstp->rq_deferred) { 11780f0257eaSTom Tucker dr = rqstp->rq_deferred; 11790f0257eaSTom Tucker rqstp->rq_deferred = NULL; 11800f0257eaSTom Tucker } else { 1181260c1d12STom Tucker size_t skip; 1182260c1d12STom Tucker size_t size; 11830f0257eaSTom Tucker /* FIXME maybe discard if size too large */ 1184260c1d12STom Tucker size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len; 11850f0257eaSTom Tucker dr = kmalloc(size, GFP_KERNEL); 11860f0257eaSTom Tucker if (dr == NULL) 11870f0257eaSTom Tucker return NULL; 11880f0257eaSTom Tucker 11890f0257eaSTom Tucker dr->handle.owner = rqstp->rq_server; 11900f0257eaSTom Tucker dr->prot = rqstp->rq_prot; 11910f0257eaSTom Tucker memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen); 11920f0257eaSTom Tucker dr->addrlen = rqstp->rq_addrlen; 11930f0257eaSTom Tucker dr->daddr = rqstp->rq_daddr; 11940f0257eaSTom Tucker dr->argslen = rqstp->rq_arg.len >> 2; 1195260c1d12STom Tucker dr->xprt_hlen = rqstp->rq_xprt_hlen; 1196260c1d12STom Tucker 1197260c1d12STom Tucker /* back up head to the start of the buffer and copy */ 1198260c1d12STom Tucker skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; 11990f0257eaSTom Tucker memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip, 12000f0257eaSTom Tucker dr->argslen << 2); 12010f0257eaSTom Tucker } 12028954c5c2SChuck Lever trace_svc_defer(rqstp); 12030f0257eaSTom Tucker svc_xprt_get(rqstp->rq_xprt); 12040f0257eaSTom Tucker dr->xprt = rqstp->rq_xprt; 120578b65eb3SJeff Layton set_bit(RQ_DROPME, &rqstp->rq_flags); 12060f0257eaSTom Tucker 12070f0257eaSTom Tucker dr->handle.revisit = svc_revisit; 12080f0257eaSTom Tucker return &dr->handle; 12090f0257eaSTom Tucker } 12100f0257eaSTom Tucker 12110f0257eaSTom Tucker /* 12120f0257eaSTom Tucker * recv data from a deferred request into an active one 12130f0257eaSTom Tucker */ 12148954c5c2SChuck Lever static noinline int svc_deferred_recv(struct svc_rqst *rqstp) 12150f0257eaSTom Tucker { 12160f0257eaSTom Tucker struct svc_deferred_req *dr = rqstp->rq_deferred; 12170f0257eaSTom Tucker 12188954c5c2SChuck Lever trace_svc_defer_recv(dr); 12198954c5c2SChuck Lever 1220260c1d12STom Tucker /* setup iov_base past transport header */ 1221260c1d12STom Tucker rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2); 1222260c1d12STom Tucker /* The iov_len does not include the transport header bytes */ 1223260c1d12STom Tucker rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen; 12240f0257eaSTom Tucker rqstp->rq_arg.page_len = 0; 1225260c1d12STom Tucker /* The rq_arg.len includes the transport header bytes */ 12260f0257eaSTom Tucker rqstp->rq_arg.len = dr->argslen<<2; 12270f0257eaSTom Tucker rqstp->rq_prot = dr->prot; 12280f0257eaSTom Tucker memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen); 12290f0257eaSTom Tucker rqstp->rq_addrlen = dr->addrlen; 1230260c1d12STom Tucker /* Save off transport header len in case we get deferred again */ 1231260c1d12STom Tucker rqstp->rq_xprt_hlen = dr->xprt_hlen; 12320f0257eaSTom Tucker rqstp->rq_daddr = dr->daddr; 12330f0257eaSTom Tucker rqstp->rq_respages = rqstp->rq_pages; 1234260c1d12STom Tucker return (dr->argslen<<2) - dr->xprt_hlen; 12350f0257eaSTom Tucker } 12360f0257eaSTom Tucker 12370f0257eaSTom Tucker 12380f0257eaSTom Tucker static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt) 12390f0257eaSTom Tucker { 12400f0257eaSTom Tucker struct svc_deferred_req *dr = NULL; 12410f0257eaSTom Tucker 12420f0257eaSTom Tucker if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags)) 12430f0257eaSTom Tucker return NULL; 12440f0257eaSTom Tucker spin_lock(&xprt->xpt_lock); 12450f0257eaSTom Tucker if (!list_empty(&xprt->xpt_deferred)) { 12460f0257eaSTom Tucker dr = list_entry(xprt->xpt_deferred.next, 12470f0257eaSTom Tucker struct svc_deferred_req, 12480f0257eaSTom Tucker handle.recent); 12490f0257eaSTom Tucker list_del_init(&dr->handle.recent); 125062bac4afSJ. Bruce Fields } else 125162bac4afSJ. Bruce Fields clear_bit(XPT_DEFERRED, &xprt->xpt_flags); 12520f0257eaSTom Tucker spin_unlock(&xprt->xpt_lock); 12530f0257eaSTom Tucker return dr; 12540f0257eaSTom Tucker } 12557fcb98d5STom Tucker 1256156e6209SChuck Lever /** 1257156e6209SChuck Lever * svc_find_xprt - find an RPC transport instance 1258156e6209SChuck Lever * @serv: pointer to svc_serv to search 1259156e6209SChuck Lever * @xcl_name: C string containing transport's class name 12604cb54ca2SStanislav Kinsbursky * @net: owner net pointer 1261156e6209SChuck Lever * @af: Address family of transport's local address 1262156e6209SChuck Lever * @port: transport's IP port number 1263156e6209SChuck Lever * 12647fcb98d5STom Tucker * Return the transport instance pointer for the endpoint accepting 12657fcb98d5STom Tucker * connections/peer traffic from the specified transport class, 12667fcb98d5STom Tucker * address family and port. 12677fcb98d5STom Tucker * 12687fcb98d5STom Tucker * Specifying 0 for the address family or port is effectively a 12697fcb98d5STom Tucker * wild-card, and will result in matching the first transport in the 12707fcb98d5STom Tucker * service's list that has a matching class name. 12717fcb98d5STom Tucker */ 1272156e6209SChuck Lever struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name, 12734cb54ca2SStanislav Kinsbursky struct net *net, const sa_family_t af, 12744cb54ca2SStanislav Kinsbursky const unsigned short port) 12757fcb98d5STom Tucker { 12767fcb98d5STom Tucker struct svc_xprt *xprt; 12777fcb98d5STom Tucker struct svc_xprt *found = NULL; 12787fcb98d5STom Tucker 12797fcb98d5STom Tucker /* Sanity check the args */ 1280156e6209SChuck Lever if (serv == NULL || xcl_name == NULL) 12817fcb98d5STom Tucker return found; 12827fcb98d5STom Tucker 12837fcb98d5STom Tucker spin_lock_bh(&serv->sv_lock); 12847fcb98d5STom Tucker list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 12854cb54ca2SStanislav Kinsbursky if (xprt->xpt_net != net) 12864cb54ca2SStanislav Kinsbursky continue; 12877fcb98d5STom Tucker if (strcmp(xprt->xpt_class->xcl_name, xcl_name)) 12887fcb98d5STom Tucker continue; 12897fcb98d5STom Tucker if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family) 12907fcb98d5STom Tucker continue; 1291156e6209SChuck Lever if (port != 0 && port != svc_xprt_local_port(xprt)) 12927fcb98d5STom Tucker continue; 12937fcb98d5STom Tucker found = xprt; 1294a217813fSTom Tucker svc_xprt_get(xprt); 12957fcb98d5STom Tucker break; 12967fcb98d5STom Tucker } 12977fcb98d5STom Tucker spin_unlock_bh(&serv->sv_lock); 12987fcb98d5STom Tucker return found; 12997fcb98d5STom Tucker } 13007fcb98d5STom Tucker EXPORT_SYMBOL_GPL(svc_find_xprt); 13019571af18STom Tucker 1302335c54bdSChuck Lever static int svc_one_xprt_name(const struct svc_xprt *xprt, 1303335c54bdSChuck Lever char *pos, int remaining) 1304335c54bdSChuck Lever { 1305335c54bdSChuck Lever int len; 1306335c54bdSChuck Lever 1307335c54bdSChuck Lever len = snprintf(pos, remaining, "%s %u\n", 1308335c54bdSChuck Lever xprt->xpt_class->xcl_name, 1309335c54bdSChuck Lever svc_xprt_local_port(xprt)); 1310335c54bdSChuck Lever if (len >= remaining) 1311335c54bdSChuck Lever return -ENAMETOOLONG; 1312335c54bdSChuck Lever return len; 1313335c54bdSChuck Lever } 1314335c54bdSChuck Lever 1315335c54bdSChuck Lever /** 1316335c54bdSChuck Lever * svc_xprt_names - format a buffer with a list of transport names 1317335c54bdSChuck Lever * @serv: pointer to an RPC service 1318335c54bdSChuck Lever * @buf: pointer to a buffer to be filled in 1319335c54bdSChuck Lever * @buflen: length of buffer to be filled in 1320335c54bdSChuck Lever * 1321335c54bdSChuck Lever * Fills in @buf with a string containing a list of transport names, 1322335c54bdSChuck Lever * each name terminated with '\n'. 1323335c54bdSChuck Lever * 1324335c54bdSChuck Lever * Returns positive length of the filled-in string on success; otherwise 1325335c54bdSChuck Lever * a negative errno value is returned if an error occurs. 13269571af18STom Tucker */ 1327335c54bdSChuck Lever int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen) 13289571af18STom Tucker { 13299571af18STom Tucker struct svc_xprt *xprt; 1330335c54bdSChuck Lever int len, totlen; 1331335c54bdSChuck Lever char *pos; 13329571af18STom Tucker 13339571af18STom Tucker /* Sanity check args */ 13349571af18STom Tucker if (!serv) 13359571af18STom Tucker return 0; 13369571af18STom Tucker 13379571af18STom Tucker spin_lock_bh(&serv->sv_lock); 1338335c54bdSChuck Lever 1339335c54bdSChuck Lever pos = buf; 1340335c54bdSChuck Lever totlen = 0; 13419571af18STom Tucker list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 1342335c54bdSChuck Lever len = svc_one_xprt_name(xprt, pos, buflen - totlen); 1343335c54bdSChuck Lever if (len < 0) { 1344335c54bdSChuck Lever *buf = '\0'; 1345335c54bdSChuck Lever totlen = len; 1346335c54bdSChuck Lever } 1347335c54bdSChuck Lever if (len <= 0) 13489571af18STom Tucker break; 1349335c54bdSChuck Lever 1350335c54bdSChuck Lever pos += len; 13519571af18STom Tucker totlen += len; 13529571af18STom Tucker } 1353335c54bdSChuck Lever 13549571af18STom Tucker spin_unlock_bh(&serv->sv_lock); 13559571af18STom Tucker return totlen; 13569571af18STom Tucker } 13579571af18STom Tucker EXPORT_SYMBOL_GPL(svc_xprt_names); 135803cf6c9fSGreg Banks 135903cf6c9fSGreg Banks 136003cf6c9fSGreg Banks /*----------------------------------------------------------------------------*/ 136103cf6c9fSGreg Banks 136203cf6c9fSGreg Banks static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos) 136303cf6c9fSGreg Banks { 136403cf6c9fSGreg Banks unsigned int pidx = (unsigned int)*pos; 136503cf6c9fSGreg Banks struct svc_serv *serv = m->private; 136603cf6c9fSGreg Banks 136703cf6c9fSGreg Banks dprintk("svc_pool_stats_start, *pidx=%u\n", pidx); 136803cf6c9fSGreg Banks 136903cf6c9fSGreg Banks if (!pidx) 137003cf6c9fSGreg Banks return SEQ_START_TOKEN; 137103cf6c9fSGreg Banks return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]); 137203cf6c9fSGreg Banks } 137303cf6c9fSGreg Banks 137403cf6c9fSGreg Banks static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos) 137503cf6c9fSGreg Banks { 137603cf6c9fSGreg Banks struct svc_pool *pool = p; 137703cf6c9fSGreg Banks struct svc_serv *serv = m->private; 137803cf6c9fSGreg Banks 137903cf6c9fSGreg Banks dprintk("svc_pool_stats_next, *pos=%llu\n", *pos); 138003cf6c9fSGreg Banks 138103cf6c9fSGreg Banks if (p == SEQ_START_TOKEN) { 138203cf6c9fSGreg Banks pool = &serv->sv_pools[0]; 138303cf6c9fSGreg Banks } else { 138403cf6c9fSGreg Banks unsigned int pidx = (pool - &serv->sv_pools[0]); 138503cf6c9fSGreg Banks if (pidx < serv->sv_nrpools-1) 138603cf6c9fSGreg Banks pool = &serv->sv_pools[pidx+1]; 138703cf6c9fSGreg Banks else 138803cf6c9fSGreg Banks pool = NULL; 138903cf6c9fSGreg Banks } 139003cf6c9fSGreg Banks ++*pos; 139103cf6c9fSGreg Banks return pool; 139203cf6c9fSGreg Banks } 139303cf6c9fSGreg Banks 139403cf6c9fSGreg Banks static void svc_pool_stats_stop(struct seq_file *m, void *p) 139503cf6c9fSGreg Banks { 139603cf6c9fSGreg Banks } 139703cf6c9fSGreg Banks 139803cf6c9fSGreg Banks static int svc_pool_stats_show(struct seq_file *m, void *p) 139903cf6c9fSGreg Banks { 140003cf6c9fSGreg Banks struct svc_pool *pool = p; 140103cf6c9fSGreg Banks 140203cf6c9fSGreg Banks if (p == SEQ_START_TOKEN) { 140378c210efSJ. Bruce Fields seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n"); 140403cf6c9fSGreg Banks return 0; 140503cf6c9fSGreg Banks } 140603cf6c9fSGreg Banks 140778c210efSJ. Bruce Fields seq_printf(m, "%u %lu %lu %lu %lu\n", 140803cf6c9fSGreg Banks pool->sp_id, 1409403c7b44SJeff Layton (unsigned long)atomic_long_read(&pool->sp_stats.packets), 141003cf6c9fSGreg Banks pool->sp_stats.sockets_queued, 1411403c7b44SJeff Layton (unsigned long)atomic_long_read(&pool->sp_stats.threads_woken), 1412403c7b44SJeff Layton (unsigned long)atomic_long_read(&pool->sp_stats.threads_timedout)); 141303cf6c9fSGreg Banks 141403cf6c9fSGreg Banks return 0; 141503cf6c9fSGreg Banks } 141603cf6c9fSGreg Banks 141703cf6c9fSGreg Banks static const struct seq_operations svc_pool_stats_seq_ops = { 141803cf6c9fSGreg Banks .start = svc_pool_stats_start, 141903cf6c9fSGreg Banks .next = svc_pool_stats_next, 142003cf6c9fSGreg Banks .stop = svc_pool_stats_stop, 142103cf6c9fSGreg Banks .show = svc_pool_stats_show, 142203cf6c9fSGreg Banks }; 142303cf6c9fSGreg Banks 142403cf6c9fSGreg Banks int svc_pool_stats_open(struct svc_serv *serv, struct file *file) 142503cf6c9fSGreg Banks { 142603cf6c9fSGreg Banks int err; 142703cf6c9fSGreg Banks 142803cf6c9fSGreg Banks err = seq_open(file, &svc_pool_stats_seq_ops); 142903cf6c9fSGreg Banks if (!err) 143003cf6c9fSGreg Banks ((struct seq_file *) file->private_data)->private = serv; 143103cf6c9fSGreg Banks return err; 143203cf6c9fSGreg Banks } 143303cf6c9fSGreg Banks EXPORT_SYMBOL(svc_pool_stats_open); 143403cf6c9fSGreg Banks 143503cf6c9fSGreg Banks /*----------------------------------------------------------------------------*/ 1436