11d8206b9STom Tucker /* 21d8206b9STom Tucker * linux/net/sunrpc/svc_xprt.c 31d8206b9STom Tucker * 41d8206b9STom Tucker * Author: Tom Tucker <tom@opengridcomputing.com> 51d8206b9STom Tucker */ 61d8206b9STom Tucker 71d8206b9STom Tucker #include <linux/sched.h> 81d8206b9STom Tucker #include <linux/errno.h> 91d8206b9STom Tucker #include <linux/freezer.h> 107086721fSJeff Layton #include <linux/kthread.h> 115a0e3ad6STejun Heo #include <linux/slab.h> 121d8206b9STom Tucker #include <net/sock.h> 13c3d4879eSScott Mayhew #include <linux/sunrpc/addr.h> 141d8206b9STom Tucker #include <linux/sunrpc/stats.h> 151d8206b9STom Tucker #include <linux/sunrpc/svc_xprt.h> 16dcf1a357SH Hartley Sweeten #include <linux/sunrpc/svcsock.h> 1799de8ea9SJ. Bruce Fields #include <linux/sunrpc/xprt.h> 183a9a231dSPaul Gortmaker #include <linux/module.h> 19c3d4879eSScott Mayhew #include <linux/netdevice.h> 20860a0d9eSJeff Layton #include <trace/events/sunrpc.h> 211d8206b9STom Tucker 221d8206b9STom Tucker #define RPCDBG_FACILITY RPCDBG_SVCXPRT 231d8206b9STom Tucker 24ff3ac5c3STrond Myklebust static unsigned int svc_rpc_per_connection_limit __read_mostly; 25ff3ac5c3STrond Myklebust module_param(svc_rpc_per_connection_limit, uint, 0644); 26ff3ac5c3STrond Myklebust 27ff3ac5c3STrond Myklebust 280f0257eaSTom Tucker static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt); 290f0257eaSTom Tucker static int svc_deferred_recv(struct svc_rqst *rqstp); 300f0257eaSTom Tucker static struct cache_deferred_req *svc_defer(struct cache_req *req); 31ff861c4dSKees Cook static void svc_age_temp_xprts(struct timer_list *t); 327710ec36SJ. Bruce Fields static void svc_delete_xprt(struct svc_xprt *xprt); 330f0257eaSTom Tucker 340f0257eaSTom Tucker /* apparently the "standard" is that clients close 350f0257eaSTom Tucker * idle connections after 5 minutes, servers after 360f0257eaSTom Tucker * 6 minutes 370f0257eaSTom Tucker * http://www.connectathon.org/talks96/nfstcp.pdf 380f0257eaSTom Tucker */ 390f0257eaSTom Tucker static int svc_conn_age_period = 6*60; 400f0257eaSTom Tucker 411d8206b9STom Tucker /* List of registered transport classes */ 421d8206b9STom Tucker static DEFINE_SPINLOCK(svc_xprt_class_lock); 431d8206b9STom Tucker static LIST_HEAD(svc_xprt_class_list); 441d8206b9STom Tucker 450f0257eaSTom Tucker /* SMP locking strategy: 460f0257eaSTom Tucker * 470f0257eaSTom Tucker * svc_pool->sp_lock protects most of the fields of that pool. 480f0257eaSTom Tucker * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt. 490f0257eaSTom Tucker * when both need to be taken (rare), svc_serv->sv_lock is first. 503c519914SJeff Layton * The "service mutex" protects svc_serv->sv_nrthread. 510f0257eaSTom Tucker * svc_sock->sk_lock protects the svc_sock->sk_deferred list 520f0257eaSTom Tucker * and the ->sk_info_authunix cache. 530f0257eaSTom Tucker * 540f0257eaSTom Tucker * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being 550f0257eaSTom Tucker * enqueued multiply. During normal transport processing this bit 560f0257eaSTom Tucker * is set by svc_xprt_enqueue and cleared by svc_xprt_received. 570f0257eaSTom Tucker * Providers should not manipulate this bit directly. 580f0257eaSTom Tucker * 590f0257eaSTom Tucker * Some flags can be set to certain values at any time 600f0257eaSTom Tucker * providing that certain rules are followed: 610f0257eaSTom Tucker * 620f0257eaSTom Tucker * XPT_CONN, XPT_DATA: 630f0257eaSTom Tucker * - Can be set or cleared at any time. 640f0257eaSTom Tucker * - After a set, svc_xprt_enqueue must be called to enqueue 650f0257eaSTom Tucker * the transport for processing. 660f0257eaSTom Tucker * - After a clear, the transport must be read/accepted. 670f0257eaSTom Tucker * If this succeeds, it must be set again. 680f0257eaSTom Tucker * XPT_CLOSE: 690f0257eaSTom Tucker * - Can set at any time. It is never cleared. 700f0257eaSTom Tucker * XPT_DEAD: 710f0257eaSTom Tucker * - Can only be set while XPT_BUSY is held which ensures 720f0257eaSTom Tucker * that no other thread will be using the transport or will 730f0257eaSTom Tucker * try to set XPT_DEAD. 740f0257eaSTom Tucker */ 751d8206b9STom Tucker int svc_reg_xprt_class(struct svc_xprt_class *xcl) 761d8206b9STom Tucker { 771d8206b9STom Tucker struct svc_xprt_class *cl; 781d8206b9STom Tucker int res = -EEXIST; 791d8206b9STom Tucker 801d8206b9STom Tucker dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name); 811d8206b9STom Tucker 821d8206b9STom Tucker INIT_LIST_HEAD(&xcl->xcl_list); 831d8206b9STom Tucker spin_lock(&svc_xprt_class_lock); 841d8206b9STom Tucker /* Make sure there isn't already a class with the same name */ 851d8206b9STom Tucker list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) { 861d8206b9STom Tucker if (strcmp(xcl->xcl_name, cl->xcl_name) == 0) 871d8206b9STom Tucker goto out; 881d8206b9STom Tucker } 891d8206b9STom Tucker list_add_tail(&xcl->xcl_list, &svc_xprt_class_list); 901d8206b9STom Tucker res = 0; 911d8206b9STom Tucker out: 921d8206b9STom Tucker spin_unlock(&svc_xprt_class_lock); 931d8206b9STom Tucker return res; 941d8206b9STom Tucker } 951d8206b9STom Tucker EXPORT_SYMBOL_GPL(svc_reg_xprt_class); 961d8206b9STom Tucker 971d8206b9STom Tucker void svc_unreg_xprt_class(struct svc_xprt_class *xcl) 981d8206b9STom Tucker { 991d8206b9STom Tucker dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name); 1001d8206b9STom Tucker spin_lock(&svc_xprt_class_lock); 1011d8206b9STom Tucker list_del_init(&xcl->xcl_list); 1021d8206b9STom Tucker spin_unlock(&svc_xprt_class_lock); 1031d8206b9STom Tucker } 1041d8206b9STom Tucker EXPORT_SYMBOL_GPL(svc_unreg_xprt_class); 1051d8206b9STom Tucker 106dc9a16e4STom Tucker /* 107dc9a16e4STom Tucker * Format the transport list for printing 108dc9a16e4STom Tucker */ 109dc9a16e4STom Tucker int svc_print_xprts(char *buf, int maxlen) 110dc9a16e4STom Tucker { 1118f3a6de3SPavel Emelyanov struct svc_xprt_class *xcl; 112dc9a16e4STom Tucker char tmpstr[80]; 113dc9a16e4STom Tucker int len = 0; 114dc9a16e4STom Tucker buf[0] = '\0'; 115dc9a16e4STom Tucker 116dc9a16e4STom Tucker spin_lock(&svc_xprt_class_lock); 1178f3a6de3SPavel Emelyanov list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) { 118dc9a16e4STom Tucker int slen; 119dc9a16e4STom Tucker 120dc9a16e4STom Tucker sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload); 121dc9a16e4STom Tucker slen = strlen(tmpstr); 122dc9a16e4STom Tucker if (len + slen > maxlen) 123dc9a16e4STom Tucker break; 124dc9a16e4STom Tucker len += slen; 125dc9a16e4STom Tucker strcat(buf, tmpstr); 126dc9a16e4STom Tucker } 127dc9a16e4STom Tucker spin_unlock(&svc_xprt_class_lock); 128dc9a16e4STom Tucker 129dc9a16e4STom Tucker return len; 130dc9a16e4STom Tucker } 131dc9a16e4STom Tucker 132e1b3157fSTom Tucker static void svc_xprt_free(struct kref *kref) 133e1b3157fSTom Tucker { 134e1b3157fSTom Tucker struct svc_xprt *xprt = 135e1b3157fSTom Tucker container_of(kref, struct svc_xprt, xpt_ref); 136e1b3157fSTom Tucker struct module *owner = xprt->xpt_class->xcl_owner; 137e3bfca01SPavel Emelyanov if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) 138e3bfca01SPavel Emelyanov svcauth_unix_info_release(xprt); 1394fb8518bSPavel Emelyanov put_net(xprt->xpt_net); 14099de8ea9SJ. Bruce Fields /* See comment on corresponding get in xs_setup_bc_tcp(): */ 14199de8ea9SJ. Bruce Fields if (xprt->xpt_bc_xprt) 14299de8ea9SJ. Bruce Fields xprt_put(xprt->xpt_bc_xprt); 14339a9beabSJ. Bruce Fields if (xprt->xpt_bc_xps) 14439a9beabSJ. Bruce Fields xprt_switch_put(xprt->xpt_bc_xps); 145e1b3157fSTom Tucker xprt->xpt_ops->xpo_free(xprt); 146e1b3157fSTom Tucker module_put(owner); 147e1b3157fSTom Tucker } 148e1b3157fSTom Tucker 149e1b3157fSTom Tucker void svc_xprt_put(struct svc_xprt *xprt) 150e1b3157fSTom Tucker { 151e1b3157fSTom Tucker kref_put(&xprt->xpt_ref, svc_xprt_free); 152e1b3157fSTom Tucker } 153e1b3157fSTom Tucker EXPORT_SYMBOL_GPL(svc_xprt_put); 154e1b3157fSTom Tucker 1551d8206b9STom Tucker /* 1561d8206b9STom Tucker * Called by transport drivers to initialize the transport independent 1571d8206b9STom Tucker * portion of the transport instance. 1581d8206b9STom Tucker */ 159bd4620ddSStanislav Kinsbursky void svc_xprt_init(struct net *net, struct svc_xprt_class *xcl, 160bd4620ddSStanislav Kinsbursky struct svc_xprt *xprt, struct svc_serv *serv) 1611d8206b9STom Tucker { 1621d8206b9STom Tucker memset(xprt, 0, sizeof(*xprt)); 1631d8206b9STom Tucker xprt->xpt_class = xcl; 1641d8206b9STom Tucker xprt->xpt_ops = xcl->xcl_ops; 165e1b3157fSTom Tucker kref_init(&xprt->xpt_ref); 166bb5cf160STom Tucker xprt->xpt_server = serv; 1677a182083STom Tucker INIT_LIST_HEAD(&xprt->xpt_list); 1687a182083STom Tucker INIT_LIST_HEAD(&xprt->xpt_ready); 1698c7b0172STom Tucker INIT_LIST_HEAD(&xprt->xpt_deferred); 170edc7a894SJ. Bruce Fields INIT_LIST_HEAD(&xprt->xpt_users); 171a50fea26STom Tucker mutex_init(&xprt->xpt_mutex); 172def13d74STom Tucker spin_lock_init(&xprt->xpt_lock); 1734e5caaa5STom Tucker set_bit(XPT_BUSY, &xprt->xpt_flags); 1744cfc7e60SRahul Iyer rpc_init_wait_queue(&xprt->xpt_bc_pending, "xpt_bc_pending"); 175bd4620ddSStanislav Kinsbursky xprt->xpt_net = get_net(net); 1761d8206b9STom Tucker } 1771d8206b9STom Tucker EXPORT_SYMBOL_GPL(svc_xprt_init); 178b700cbb1STom Tucker 1795dd248f6SChuck Lever static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl, 1805dd248f6SChuck Lever struct svc_serv *serv, 18162832c03SPavel Emelyanov struct net *net, 1829652ada3SChuck Lever const int family, 1839652ada3SChuck Lever const unsigned short port, 1849652ada3SChuck Lever int flags) 185b700cbb1STom Tucker { 186b700cbb1STom Tucker struct sockaddr_in sin = { 187b700cbb1STom Tucker .sin_family = AF_INET, 188e6f1cebfSAl Viro .sin_addr.s_addr = htonl(INADDR_ANY), 189b700cbb1STom Tucker .sin_port = htons(port), 190b700cbb1STom Tucker }; 191dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1925dd248f6SChuck Lever struct sockaddr_in6 sin6 = { 1935dd248f6SChuck Lever .sin6_family = AF_INET6, 1945dd248f6SChuck Lever .sin6_addr = IN6ADDR_ANY_INIT, 1955dd248f6SChuck Lever .sin6_port = htons(port), 1965dd248f6SChuck Lever }; 197dfd56b8bSEric Dumazet #endif 1985dd248f6SChuck Lever struct sockaddr *sap; 1995dd248f6SChuck Lever size_t len; 2005dd248f6SChuck Lever 2019652ada3SChuck Lever switch (family) { 2029652ada3SChuck Lever case PF_INET: 2035dd248f6SChuck Lever sap = (struct sockaddr *)&sin; 2045dd248f6SChuck Lever len = sizeof(sin); 2055dd248f6SChuck Lever break; 206dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2079652ada3SChuck Lever case PF_INET6: 2085dd248f6SChuck Lever sap = (struct sockaddr *)&sin6; 2095dd248f6SChuck Lever len = sizeof(sin6); 2105dd248f6SChuck Lever break; 211dfd56b8bSEric Dumazet #endif 2125dd248f6SChuck Lever default: 2135dd248f6SChuck Lever return ERR_PTR(-EAFNOSUPPORT); 2145dd248f6SChuck Lever } 2155dd248f6SChuck Lever 21662832c03SPavel Emelyanov return xcl->xcl_ops->xpo_create(serv, net, sap, len, flags); 2175dd248f6SChuck Lever } 2185dd248f6SChuck Lever 2196741019cSJ. Bruce Fields /* 2206741019cSJ. Bruce Fields * svc_xprt_received conditionally queues the transport for processing 2216741019cSJ. Bruce Fields * by another thread. The caller must hold the XPT_BUSY bit and must 2226741019cSJ. Bruce Fields * not thereafter touch transport data. 2236741019cSJ. Bruce Fields * 2246741019cSJ. Bruce Fields * Note: XPT_DATA only gets cleared when a read-attempt finds no (or 2256741019cSJ. Bruce Fields * insufficient) data. 2266741019cSJ. Bruce Fields */ 2276741019cSJ. Bruce Fields static void svc_xprt_received(struct svc_xprt *xprt) 2286741019cSJ. Bruce Fields { 229acf06a7fSJeff Layton if (!test_bit(XPT_BUSY, &xprt->xpt_flags)) { 230acf06a7fSJeff Layton WARN_ONCE(1, "xprt=0x%p already busy!", xprt); 231ff1fdb9bSWeston Andros Adamson return; 232acf06a7fSJeff Layton } 233acf06a7fSJeff Layton 2346741019cSJ. Bruce Fields /* As soon as we clear busy, the xprt could be closed and 235b9e13cdfSJeff Layton * 'put', so we need a reference to call svc_enqueue_xprt with: 2366741019cSJ. Bruce Fields */ 2376741019cSJ. Bruce Fields svc_xprt_get(xprt); 2380971374eSTrond Myklebust smp_mb__before_atomic(); 2396741019cSJ. Bruce Fields clear_bit(XPT_BUSY, &xprt->xpt_flags); 240b9e13cdfSJeff Layton xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt); 2416741019cSJ. Bruce Fields svc_xprt_put(xprt); 2426741019cSJ. Bruce Fields } 2436741019cSJ. Bruce Fields 24439b55301SJ. Bruce Fields void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new) 24539b55301SJ. Bruce Fields { 24639b55301SJ. Bruce Fields clear_bit(XPT_TEMP, &new->xpt_flags); 24739b55301SJ. Bruce Fields spin_lock_bh(&serv->sv_lock); 24839b55301SJ. Bruce Fields list_add(&new->xpt_list, &serv->sv_permsocks); 24939b55301SJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 25039b55301SJ. Bruce Fields svc_xprt_received(new); 25139b55301SJ. Bruce Fields } 25239b55301SJ. Bruce Fields 253da36e6dbSColin Ian King static int _svc_create_xprt(struct svc_serv *serv, const char *xprt_name, 254fc5d00b0SPavel Emelyanov struct net *net, const int family, 255fc5d00b0SPavel Emelyanov const unsigned short port, int flags) 2565dd248f6SChuck Lever { 2575dd248f6SChuck Lever struct svc_xprt_class *xcl; 2585dd248f6SChuck Lever 259b700cbb1STom Tucker spin_lock(&svc_xprt_class_lock); 260b700cbb1STom Tucker list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) { 261b700cbb1STom Tucker struct svc_xprt *newxprt; 262ed2849d3SNeilBrown unsigned short newport; 2634e5caaa5STom Tucker 2644e5caaa5STom Tucker if (strcmp(xprt_name, xcl->xcl_name)) 2654e5caaa5STom Tucker continue; 2664e5caaa5STom Tucker 2674e5caaa5STom Tucker if (!try_module_get(xcl->xcl_owner)) 2684e5caaa5STom Tucker goto err; 2694e5caaa5STom Tucker 2704e5caaa5STom Tucker spin_unlock(&svc_xprt_class_lock); 27162832c03SPavel Emelyanov newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags); 272b700cbb1STom Tucker if (IS_ERR(newxprt)) { 273b700cbb1STom Tucker module_put(xcl->xcl_owner); 2744e5caaa5STom Tucker return PTR_ERR(newxprt); 275b700cbb1STom Tucker } 27639b55301SJ. Bruce Fields svc_add_new_perm_xprt(serv, newxprt); 277ed2849d3SNeilBrown newport = svc_xprt_local_port(newxprt); 278ed2849d3SNeilBrown return newport; 279b700cbb1STom Tucker } 2804e5caaa5STom Tucker err: 281b700cbb1STom Tucker spin_unlock(&svc_xprt_class_lock); 28268717908SChuck Lever /* This errno is exposed to user space. Provide a reasonable 28368717908SChuck Lever * perror msg for a bad transport. */ 28468717908SChuck Lever return -EPROTONOSUPPORT; 285b700cbb1STom Tucker } 286d96b9c93SJ. Bruce Fields 287d96b9c93SJ. Bruce Fields int svc_create_xprt(struct svc_serv *serv, const char *xprt_name, 288d96b9c93SJ. Bruce Fields struct net *net, const int family, 289d96b9c93SJ. Bruce Fields const unsigned short port, int flags) 290d96b9c93SJ. Bruce Fields { 291d96b9c93SJ. Bruce Fields int err; 292d96b9c93SJ. Bruce Fields 293d96b9c93SJ. Bruce Fields dprintk("svc: creating transport %s[%d]\n", xprt_name, port); 294d96b9c93SJ. Bruce Fields err = _svc_create_xprt(serv, xprt_name, net, family, port, flags); 295d96b9c93SJ. Bruce Fields if (err == -EPROTONOSUPPORT) { 296d96b9c93SJ. Bruce Fields request_module("svc%s", xprt_name); 297d96b9c93SJ. Bruce Fields err = _svc_create_xprt(serv, xprt_name, net, family, port, flags); 298d96b9c93SJ. Bruce Fields } 299d96b9c93SJ. Bruce Fields if (err) 300d96b9c93SJ. Bruce Fields dprintk("svc: transport %s not found, err %d\n", 301d96b9c93SJ. Bruce Fields xprt_name, err); 302d96b9c93SJ. Bruce Fields return err; 303d96b9c93SJ. Bruce Fields } 304b700cbb1STom Tucker EXPORT_SYMBOL_GPL(svc_create_xprt); 3059dbc240fSTom Tucker 3069dbc240fSTom Tucker /* 3079dbc240fSTom Tucker * Copy the local and remote xprt addresses to the rqstp structure 3089dbc240fSTom Tucker */ 3099dbc240fSTom Tucker void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt) 3109dbc240fSTom Tucker { 3119dbc240fSTom Tucker memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen); 3129dbc240fSTom Tucker rqstp->rq_addrlen = xprt->xpt_remotelen; 3139dbc240fSTom Tucker 3149dbc240fSTom Tucker /* 3159dbc240fSTom Tucker * Destination address in request is needed for binding the 3169dbc240fSTom Tucker * source address in RPC replies/callbacks later. 3179dbc240fSTom Tucker */ 318849a1cf1SMi Jinlong memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen); 319849a1cf1SMi Jinlong rqstp->rq_daddrlen = xprt->xpt_locallen; 3209dbc240fSTom Tucker } 3219dbc240fSTom Tucker EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs); 3229dbc240fSTom Tucker 3230f0257eaSTom Tucker /** 3240f0257eaSTom Tucker * svc_print_addr - Format rq_addr field for printing 3250f0257eaSTom Tucker * @rqstp: svc_rqst struct containing address to print 3260f0257eaSTom Tucker * @buf: target buffer for formatted address 3270f0257eaSTom Tucker * @len: length of target buffer 3280f0257eaSTom Tucker * 3290f0257eaSTom Tucker */ 3300f0257eaSTom Tucker char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len) 3310f0257eaSTom Tucker { 3320f0257eaSTom Tucker return __svc_print_addr(svc_addr(rqstp), buf, len); 3330f0257eaSTom Tucker } 3340f0257eaSTom Tucker EXPORT_SYMBOL_GPL(svc_print_addr); 3350f0257eaSTom Tucker 336ff3ac5c3STrond Myklebust static bool svc_xprt_slots_in_range(struct svc_xprt *xprt) 337ff3ac5c3STrond Myklebust { 338ff3ac5c3STrond Myklebust unsigned int limit = svc_rpc_per_connection_limit; 339ff3ac5c3STrond Myklebust int nrqsts = atomic_read(&xprt->xpt_nr_rqsts); 340ff3ac5c3STrond Myklebust 341ff3ac5c3STrond Myklebust return limit == 0 || (nrqsts >= 0 && nrqsts < limit); 342ff3ac5c3STrond Myklebust } 343ff3ac5c3STrond Myklebust 344ff3ac5c3STrond Myklebust static bool svc_xprt_reserve_slot(struct svc_rqst *rqstp, struct svc_xprt *xprt) 345ff3ac5c3STrond Myklebust { 346ff3ac5c3STrond Myklebust if (!test_bit(RQ_DATA, &rqstp->rq_flags)) { 347ff3ac5c3STrond Myklebust if (!svc_xprt_slots_in_range(xprt)) 348ff3ac5c3STrond Myklebust return false; 349ff3ac5c3STrond Myklebust atomic_inc(&xprt->xpt_nr_rqsts); 350ff3ac5c3STrond Myklebust set_bit(RQ_DATA, &rqstp->rq_flags); 351ff3ac5c3STrond Myklebust } 352ff3ac5c3STrond Myklebust return true; 353ff3ac5c3STrond Myklebust } 354ff3ac5c3STrond Myklebust 355ff3ac5c3STrond Myklebust static void svc_xprt_release_slot(struct svc_rqst *rqstp) 356ff3ac5c3STrond Myklebust { 357ff3ac5c3STrond Myklebust struct svc_xprt *xprt = rqstp->rq_xprt; 358ff3ac5c3STrond Myklebust if (test_and_clear_bit(RQ_DATA, &rqstp->rq_flags)) { 359ff3ac5c3STrond Myklebust atomic_dec(&xprt->xpt_nr_rqsts); 360ff3ac5c3STrond Myklebust svc_xprt_enqueue(xprt); 361ff3ac5c3STrond Myklebust } 362ff3ac5c3STrond Myklebust } 363ff3ac5c3STrond Myklebust 3649c335c0bSJ. Bruce Fields static bool svc_xprt_has_something_to_do(struct svc_xprt *xprt) 3659c335c0bSJ. Bruce Fields { 3669c335c0bSJ. Bruce Fields if (xprt->xpt_flags & ((1<<XPT_CONN)|(1<<XPT_CLOSE))) 3679c335c0bSJ. Bruce Fields return true; 36882ea2d76STrond Myklebust if (xprt->xpt_flags & ((1<<XPT_DATA)|(1<<XPT_DEFERRED))) { 369ff3ac5c3STrond Myklebust if (xprt->xpt_ops->xpo_has_wspace(xprt) && 370ff3ac5c3STrond Myklebust svc_xprt_slots_in_range(xprt)) 37182ea2d76STrond Myklebust return true; 37282ea2d76STrond Myklebust trace_svc_xprt_no_write_space(xprt); 37382ea2d76STrond Myklebust return false; 37482ea2d76STrond Myklebust } 3759c335c0bSJ. Bruce Fields return false; 3769c335c0bSJ. Bruce Fields } 3779c335c0bSJ. Bruce Fields 378b9e13cdfSJeff Layton void svc_xprt_do_enqueue(struct svc_xprt *xprt) 3790f0257eaSTom Tucker { 3800f0257eaSTom Tucker struct svc_pool *pool; 38183a712e0SJeff Layton struct svc_rqst *rqstp = NULL; 3820f0257eaSTom Tucker int cpu; 3830f0257eaSTom Tucker 3849c335c0bSJ. Bruce Fields if (!svc_xprt_has_something_to_do(xprt)) 38583a712e0SJeff Layton goto out; 3860f0257eaSTom Tucker 3870f0257eaSTom Tucker /* Mark transport as busy. It will remain in this state until 3880f0257eaSTom Tucker * the provider calls svc_xprt_received. We update XPT_BUSY 3890f0257eaSTom Tucker * atomically because it also guards against trying to enqueue 3900f0257eaSTom Tucker * the transport twice. 3910f0257eaSTom Tucker */ 3920f0257eaSTom Tucker if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) { 3930f0257eaSTom Tucker /* Don't enqueue transport while already enqueued */ 3940f0257eaSTom Tucker dprintk("svc: transport %p busy, not enqueued\n", xprt); 39583a712e0SJeff Layton goto out; 3960f0257eaSTom Tucker } 3970f0257eaSTom Tucker 3980c0746d0STrond Myklebust cpu = get_cpu(); 3990c0746d0STrond Myklebust pool = svc_pool_for_cpu(xprt->xpt_server, cpu); 4000c0746d0STrond Myklebust 401403c7b44SJeff Layton atomic_long_inc(&pool->sp_stats.packets); 4020c0746d0STrond Myklebust 403b1691bc0SJeff Layton dprintk("svc: transport %p put into queue\n", xprt); 404b1691bc0SJeff Layton spin_lock_bh(&pool->sp_lock); 405b1691bc0SJeff Layton list_add_tail(&xprt->xpt_ready, &pool->sp_sockets); 406b1691bc0SJeff Layton pool->sp_stats.sockets_queued++; 4070f0257eaSTom Tucker spin_unlock_bh(&pool->sp_lock); 40822700f3cSTrond Myklebust 40922700f3cSTrond Myklebust /* find a thread for this xprt */ 41022700f3cSTrond Myklebust rcu_read_lock(); 41122700f3cSTrond Myklebust list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { 41222700f3cSTrond Myklebust if (test_and_set_bit(RQ_BUSY, &rqstp->rq_flags)) 41322700f3cSTrond Myklebust continue; 41422700f3cSTrond Myklebust atomic_long_inc(&pool->sp_stats.threads_woken); 41522700f3cSTrond Myklebust wake_up_process(rqstp->rq_task); 41622700f3cSTrond Myklebust goto out_unlock; 417b1691bc0SJeff Layton } 41822700f3cSTrond Myklebust set_bit(SP_CONGESTED, &pool->sp_flags); 41983a712e0SJeff Layton rqstp = NULL; 42022700f3cSTrond Myklebust out_unlock: 42122700f3cSTrond Myklebust rcu_read_unlock(); 422983c6844STrond Myklebust put_cpu(); 42383a712e0SJeff Layton out: 42483a712e0SJeff Layton trace_svc_xprt_do_enqueue(xprt, rqstp); 4250f0257eaSTom Tucker } 426b9e13cdfSJeff Layton EXPORT_SYMBOL_GPL(svc_xprt_do_enqueue); 4270971374eSTrond Myklebust 4280971374eSTrond Myklebust /* 4290971374eSTrond Myklebust * Queue up a transport with data pending. If there are idle nfsd 4300971374eSTrond Myklebust * processes, wake 'em up. 4310971374eSTrond Myklebust * 4320971374eSTrond Myklebust */ 4330971374eSTrond Myklebust void svc_xprt_enqueue(struct svc_xprt *xprt) 4340971374eSTrond Myklebust { 4350971374eSTrond Myklebust if (test_bit(XPT_BUSY, &xprt->xpt_flags)) 4360971374eSTrond Myklebust return; 437b9e13cdfSJeff Layton xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt); 4380971374eSTrond Myklebust } 4390f0257eaSTom Tucker EXPORT_SYMBOL_GPL(svc_xprt_enqueue); 4400f0257eaSTom Tucker 4410f0257eaSTom Tucker /* 442b1691bc0SJeff Layton * Dequeue the first transport, if there is one. 4430f0257eaSTom Tucker */ 4440f0257eaSTom Tucker static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool) 4450f0257eaSTom Tucker { 446b1691bc0SJeff Layton struct svc_xprt *xprt = NULL; 4470f0257eaSTom Tucker 4480f0257eaSTom Tucker if (list_empty(&pool->sp_sockets)) 44983a712e0SJeff Layton goto out; 4500f0257eaSTom Tucker 451b1691bc0SJeff Layton spin_lock_bh(&pool->sp_lock); 452b1691bc0SJeff Layton if (likely(!list_empty(&pool->sp_sockets))) { 453b1691bc0SJeff Layton xprt = list_first_entry(&pool->sp_sockets, 4540f0257eaSTom Tucker struct svc_xprt, xpt_ready); 4550f0257eaSTom Tucker list_del_init(&xprt->xpt_ready); 456b1691bc0SJeff Layton svc_xprt_get(xprt); 4570f0257eaSTom Tucker 4580f0257eaSTom Tucker dprintk("svc: transport %p dequeued, inuse=%d\n", 4592c935bc5SPeter Zijlstra xprt, kref_read(&xprt->xpt_ref)); 460b1691bc0SJeff Layton } 461b1691bc0SJeff Layton spin_unlock_bh(&pool->sp_lock); 46283a712e0SJeff Layton out: 46383a712e0SJeff Layton trace_svc_xprt_dequeue(xprt); 4640f0257eaSTom Tucker return xprt; 4650f0257eaSTom Tucker } 4660f0257eaSTom Tucker 4670f0257eaSTom Tucker /** 4680f0257eaSTom Tucker * svc_reserve - change the space reserved for the reply to a request. 4690f0257eaSTom Tucker * @rqstp: The request in question 4700f0257eaSTom Tucker * @space: new max space to reserve 4710f0257eaSTom Tucker * 4720f0257eaSTom Tucker * Each request reserves some space on the output queue of the transport 4730f0257eaSTom Tucker * to make sure the reply fits. This function reduces that reserved 4740f0257eaSTom Tucker * space to be the amount of space used already, plus @space. 4750f0257eaSTom Tucker * 4760f0257eaSTom Tucker */ 4770f0257eaSTom Tucker void svc_reserve(struct svc_rqst *rqstp, int space) 4780f0257eaSTom Tucker { 4790f0257eaSTom Tucker space += rqstp->rq_res.head[0].iov_len; 4800f0257eaSTom Tucker 4810f0257eaSTom Tucker if (space < rqstp->rq_reserved) { 4820f0257eaSTom Tucker struct svc_xprt *xprt = rqstp->rq_xprt; 4830f0257eaSTom Tucker atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved); 4840f0257eaSTom Tucker rqstp->rq_reserved = space; 4850f0257eaSTom Tucker 4860f0257eaSTom Tucker svc_xprt_enqueue(xprt); 4870f0257eaSTom Tucker } 4880f0257eaSTom Tucker } 48924c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_reserve); 4900f0257eaSTom Tucker 4910f0257eaSTom Tucker static void svc_xprt_release(struct svc_rqst *rqstp) 4920f0257eaSTom Tucker { 4930f0257eaSTom Tucker struct svc_xprt *xprt = rqstp->rq_xprt; 4940f0257eaSTom Tucker 49563a1b156SChuck Lever xprt->xpt_ops->xpo_release_rqst(rqstp); 4960f0257eaSTom Tucker 4972779e3aeSTom Tucker kfree(rqstp->rq_deferred); 4982779e3aeSTom Tucker rqstp->rq_deferred = NULL; 4992779e3aeSTom Tucker 5000f0257eaSTom Tucker svc_free_res_pages(rqstp); 5010f0257eaSTom Tucker rqstp->rq_res.page_len = 0; 5020f0257eaSTom Tucker rqstp->rq_res.page_base = 0; 5030f0257eaSTom Tucker 5040f0257eaSTom Tucker /* Reset response buffer and release 5050f0257eaSTom Tucker * the reservation. 5060f0257eaSTom Tucker * But first, check that enough space was reserved 5070f0257eaSTom Tucker * for the reply, otherwise we have a bug! 5080f0257eaSTom Tucker */ 5090f0257eaSTom Tucker if ((rqstp->rq_res.len) > rqstp->rq_reserved) 5100f0257eaSTom Tucker printk(KERN_ERR "RPC request reserved %d but used %d\n", 5110f0257eaSTom Tucker rqstp->rq_reserved, 5120f0257eaSTom Tucker rqstp->rq_res.len); 5130f0257eaSTom Tucker 5140f0257eaSTom Tucker rqstp->rq_res.head[0].iov_len = 0; 5150f0257eaSTom Tucker svc_reserve(rqstp, 0); 516ff3ac5c3STrond Myklebust svc_xprt_release_slot(rqstp); 5170f0257eaSTom Tucker rqstp->rq_xprt = NULL; 5180f0257eaSTom Tucker svc_xprt_put(xprt); 5190f0257eaSTom Tucker } 5200f0257eaSTom Tucker 5210f0257eaSTom Tucker /* 522ceff739cSJeff Layton * Some svc_serv's will have occasional work to do, even when a xprt is not 523ceff739cSJeff Layton * waiting to be serviced. This function is there to "kick" a task in one of 524ceff739cSJeff Layton * those services so that it can wake up and do that work. Note that we only 525ceff739cSJeff Layton * bother with pool 0 as we don't need to wake up more than one thread for 526ceff739cSJeff Layton * this purpose. 5270f0257eaSTom Tucker */ 5280f0257eaSTom Tucker void svc_wake_up(struct svc_serv *serv) 5290f0257eaSTom Tucker { 5300f0257eaSTom Tucker struct svc_rqst *rqstp; 5310f0257eaSTom Tucker struct svc_pool *pool; 5320f0257eaSTom Tucker 533ceff739cSJeff Layton pool = &serv->sv_pools[0]; 5340f0257eaSTom Tucker 535b1691bc0SJeff Layton rcu_read_lock(); 536b1691bc0SJeff Layton list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { 537b1691bc0SJeff Layton /* skip any that aren't queued */ 538b1691bc0SJeff Layton if (test_bit(RQ_BUSY, &rqstp->rq_flags)) 539b1691bc0SJeff Layton continue; 540b1691bc0SJeff Layton rcu_read_unlock(); 5410f0257eaSTom Tucker dprintk("svc: daemon %p woken up.\n", rqstp); 542983c6844STrond Myklebust wake_up_process(rqstp->rq_task); 54383a712e0SJeff Layton trace_svc_wake_up(rqstp->rq_task->pid); 544b1691bc0SJeff Layton return; 545b1691bc0SJeff Layton } 546b1691bc0SJeff Layton rcu_read_unlock(); 547b1691bc0SJeff Layton 548b1691bc0SJeff Layton /* No free entries available */ 5494d5db3f5SJeff Layton set_bit(SP_TASK_PENDING, &pool->sp_flags); 550b1691bc0SJeff Layton smp_wmb(); 55183a712e0SJeff Layton trace_svc_wake_up(0); 5520f0257eaSTom Tucker } 55324c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_wake_up); 5540f0257eaSTom Tucker 5550f0257eaSTom Tucker int svc_port_is_privileged(struct sockaddr *sin) 5560f0257eaSTom Tucker { 5570f0257eaSTom Tucker switch (sin->sa_family) { 5580f0257eaSTom Tucker case AF_INET: 5590f0257eaSTom Tucker return ntohs(((struct sockaddr_in *)sin)->sin_port) 5600f0257eaSTom Tucker < PROT_SOCK; 5610f0257eaSTom Tucker case AF_INET6: 5620f0257eaSTom Tucker return ntohs(((struct sockaddr_in6 *)sin)->sin6_port) 5630f0257eaSTom Tucker < PROT_SOCK; 5640f0257eaSTom Tucker default: 5650f0257eaSTom Tucker return 0; 5660f0257eaSTom Tucker } 5670f0257eaSTom Tucker } 5680f0257eaSTom Tucker 5690f0257eaSTom Tucker /* 570c9233eb7SJeff Layton * Make sure that we don't have too many active connections. If we have, 571c9233eb7SJeff Layton * something must be dropped. It's not clear what will happen if we allow 572c9233eb7SJeff Layton * "too many" connections, but when dealing with network-facing software, 573c9233eb7SJeff Layton * we have to code defensively. Here we do that by imposing hard limits. 5740f0257eaSTom Tucker * 5750f0257eaSTom Tucker * There's no point in trying to do random drop here for DoS 5760f0257eaSTom Tucker * prevention. The NFS clients does 1 reconnect in 15 seconds. An 5770f0257eaSTom Tucker * attacker can easily beat that. 5780f0257eaSTom Tucker * 5790f0257eaSTom Tucker * The only somewhat efficient mechanism would be if drop old 5800f0257eaSTom Tucker * connections from the same IP first. But right now we don't even 5810f0257eaSTom Tucker * record the client IP in svc_sock. 582c9233eb7SJeff Layton * 583c9233eb7SJeff Layton * single-threaded services that expect a lot of clients will probably 584c9233eb7SJeff Layton * need to set sv_maxconn to override the default value which is based 585c9233eb7SJeff Layton * on the number of threads 5860f0257eaSTom Tucker */ 5870f0257eaSTom Tucker static void svc_check_conn_limits(struct svc_serv *serv) 5880f0257eaSTom Tucker { 589c9233eb7SJeff Layton unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn : 590c9233eb7SJeff Layton (serv->sv_nrthreads+3) * 20; 591c9233eb7SJeff Layton 592c9233eb7SJeff Layton if (serv->sv_tmpcnt > limit) { 5930f0257eaSTom Tucker struct svc_xprt *xprt = NULL; 5940f0257eaSTom Tucker spin_lock_bh(&serv->sv_lock); 5950f0257eaSTom Tucker if (!list_empty(&serv->sv_tempsocks)) { 5960f0257eaSTom Tucker /* Try to help the admin */ 597e87cc472SJoe Perches net_notice_ratelimited("%s: too many open connections, consider increasing the %s\n", 598c9233eb7SJeff Layton serv->sv_name, serv->sv_maxconn ? 599e87cc472SJoe Perches "max number of connections" : 600e87cc472SJoe Perches "number of threads"); 6010f0257eaSTom Tucker /* 6020f0257eaSTom Tucker * Always select the oldest connection. It's not fair, 6030f0257eaSTom Tucker * but so is life 6040f0257eaSTom Tucker */ 6050f0257eaSTom Tucker xprt = list_entry(serv->sv_tempsocks.prev, 6060f0257eaSTom Tucker struct svc_xprt, 6070f0257eaSTom Tucker xpt_list); 6080f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 6090f0257eaSTom Tucker svc_xprt_get(xprt); 6100f0257eaSTom Tucker } 6110f0257eaSTom Tucker spin_unlock_bh(&serv->sv_lock); 6120f0257eaSTom Tucker 6130f0257eaSTom Tucker if (xprt) { 6140f0257eaSTom Tucker svc_xprt_enqueue(xprt); 6150f0257eaSTom Tucker svc_xprt_put(xprt); 6160f0257eaSTom Tucker } 6170f0257eaSTom Tucker } 6180f0257eaSTom Tucker } 6190f0257eaSTom Tucker 620e1d83ee6SRashika Kheria static int svc_alloc_arg(struct svc_rqst *rqstp) 6210f0257eaSTom Tucker { 6220f0257eaSTom Tucker struct svc_serv *serv = rqstp->rq_server; 6230f0257eaSTom Tucker struct xdr_buf *arg; 6246797fa5aSJ. Bruce Fields int pages; 6256797fa5aSJ. Bruce Fields int i; 6260f0257eaSTom Tucker 6270f0257eaSTom Tucker /* now allocate needed pages. If we get a failure, sleep briefly */ 6288c6ae498SChuck Lever pages = (serv->sv_max_mesg + 2 * PAGE_SIZE) >> PAGE_SHIFT; 6298c6ae498SChuck Lever if (pages > RPCSVC_MAXPAGES) { 6308c6ae498SChuck Lever pr_warn_once("svc: warning: pages=%u > RPCSVC_MAXPAGES=%lu\n", 6318c6ae498SChuck Lever pages, RPCSVC_MAXPAGES); 632b25cd058SWeston Andros Adamson /* use as many pages as possible */ 6338c6ae498SChuck Lever pages = RPCSVC_MAXPAGES; 6348c6ae498SChuck Lever } 6350f0257eaSTom Tucker for (i = 0; i < pages ; i++) 6360f0257eaSTom Tucker while (rqstp->rq_pages[i] == NULL) { 6370f0257eaSTom Tucker struct page *p = alloc_page(GFP_KERNEL); 6380f0257eaSTom Tucker if (!p) { 6397b54fe61SJeff Layton set_current_state(TASK_INTERRUPTIBLE); 6407b54fe61SJeff Layton if (signalled() || kthread_should_stop()) { 6417b54fe61SJeff Layton set_current_state(TASK_RUNNING); 6427086721fSJeff Layton return -EINTR; 6437b54fe61SJeff Layton } 6447b54fe61SJeff Layton schedule_timeout(msecs_to_jiffies(500)); 6450f0257eaSTom Tucker } 6460f0257eaSTom Tucker rqstp->rq_pages[i] = p; 6470f0257eaSTom Tucker } 6482825a7f9SJ. Bruce Fields rqstp->rq_page_end = &rqstp->rq_pages[i]; 6490f0257eaSTom Tucker rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */ 6500f0257eaSTom Tucker 6510f0257eaSTom Tucker /* Make arg->head point to first page and arg->pages point to rest */ 6520f0257eaSTom Tucker arg = &rqstp->rq_arg; 6530f0257eaSTom Tucker arg->head[0].iov_base = page_address(rqstp->rq_pages[0]); 6540f0257eaSTom Tucker arg->head[0].iov_len = PAGE_SIZE; 6550f0257eaSTom Tucker arg->pages = rqstp->rq_pages + 1; 6560f0257eaSTom Tucker arg->page_base = 0; 6570f0257eaSTom Tucker /* save at least one page for response */ 6580f0257eaSTom Tucker arg->page_len = (pages-2)*PAGE_SIZE; 6590f0257eaSTom Tucker arg->len = (pages-1)*PAGE_SIZE; 6600f0257eaSTom Tucker arg->tail[0].iov_len = 0; 6616797fa5aSJ. Bruce Fields return 0; 6626797fa5aSJ. Bruce Fields } 6630f0257eaSTom Tucker 664b1691bc0SJeff Layton static bool 665b1691bc0SJeff Layton rqst_should_sleep(struct svc_rqst *rqstp) 666b1691bc0SJeff Layton { 667b1691bc0SJeff Layton struct svc_pool *pool = rqstp->rq_pool; 668b1691bc0SJeff Layton 669b1691bc0SJeff Layton /* did someone call svc_wake_up? */ 670b1691bc0SJeff Layton if (test_and_clear_bit(SP_TASK_PENDING, &pool->sp_flags)) 671b1691bc0SJeff Layton return false; 672b1691bc0SJeff Layton 673b1691bc0SJeff Layton /* was a socket queued? */ 674b1691bc0SJeff Layton if (!list_empty(&pool->sp_sockets)) 675b1691bc0SJeff Layton return false; 676b1691bc0SJeff Layton 677b1691bc0SJeff Layton /* are we shutting down? */ 678b1691bc0SJeff Layton if (signalled() || kthread_should_stop()) 679b1691bc0SJeff Layton return false; 680b1691bc0SJeff Layton 681b1691bc0SJeff Layton /* are we freezing? */ 682b1691bc0SJeff Layton if (freezing(current)) 683b1691bc0SJeff Layton return false; 684b1691bc0SJeff Layton 685b1691bc0SJeff Layton return true; 686b1691bc0SJeff Layton } 687b1691bc0SJeff Layton 688e1d83ee6SRashika Kheria static struct svc_xprt *svc_get_next_xprt(struct svc_rqst *rqstp, long timeout) 6896797fa5aSJ. Bruce Fields { 6906797fa5aSJ. Bruce Fields struct svc_pool *pool = rqstp->rq_pool; 691a4aa8054STrond Myklebust long time_left = 0; 6920f0257eaSTom Tucker 693b1691bc0SJeff Layton /* rq_xprt should be clear on entry */ 694b1691bc0SJeff Layton WARN_ON_ONCE(rqstp->rq_xprt); 695b1691bc0SJeff Layton 69622700f3cSTrond Myklebust rqstp->rq_xprt = svc_xprt_dequeue(pool); 69722700f3cSTrond Myklebust if (rqstp->rq_xprt) 69822700f3cSTrond Myklebust goto out_found; 699b1691bc0SJeff Layton 7000f0257eaSTom Tucker /* 7010f0257eaSTom Tucker * We have to be able to interrupt this wait 7020f0257eaSTom Tucker * to bring down the daemons ... 7030f0257eaSTom Tucker */ 7040f0257eaSTom Tucker set_current_state(TASK_INTERRUPTIBLE); 70522700f3cSTrond Myklebust smp_mb__before_atomic(); 70622700f3cSTrond Myklebust clear_bit(SP_CONGESTED, &pool->sp_flags); 707b1691bc0SJeff Layton clear_bit(RQ_BUSY, &rqstp->rq_flags); 70822700f3cSTrond Myklebust smp_mb__after_atomic(); 7097086721fSJeff Layton 710b1691bc0SJeff Layton if (likely(rqst_should_sleep(rqstp))) 71103cf6c9fSGreg Banks time_left = schedule_timeout(timeout); 712b1691bc0SJeff Layton else 713983c6844STrond Myklebust __set_current_state(TASK_RUNNING); 7140f0257eaSTom Tucker 7150f0257eaSTom Tucker try_to_freeze(); 7160f0257eaSTom Tucker 717b1691bc0SJeff Layton set_bit(RQ_BUSY, &rqstp->rq_flags); 71822700f3cSTrond Myklebust smp_mb__after_atomic(); 71922700f3cSTrond Myklebust rqstp->rq_xprt = svc_xprt_dequeue(pool); 72022700f3cSTrond Myklebust if (rqstp->rq_xprt) 72122700f3cSTrond Myklebust goto out_found; 722106f359cSTrond Myklebust 72303cf6c9fSGreg Banks if (!time_left) 724403c7b44SJeff Layton atomic_long_inc(&pool->sp_stats.threads_timedout); 7250f0257eaSTom Tucker 7267086721fSJeff Layton if (signalled() || kthread_should_stop()) 7276797fa5aSJ. Bruce Fields return ERR_PTR(-EINTR); 7286797fa5aSJ. Bruce Fields return ERR_PTR(-EAGAIN); 72922700f3cSTrond Myklebust out_found: 73022700f3cSTrond Myklebust /* Normally we will wait up to 5 seconds for any required 73122700f3cSTrond Myklebust * cache information to be provided. 73222700f3cSTrond Myklebust */ 73322700f3cSTrond Myklebust if (!test_bit(SP_CONGESTED, &pool->sp_flags)) 73422700f3cSTrond Myklebust rqstp->rq_chandle.thread_wait = 5*HZ; 73522700f3cSTrond Myklebust else 73622700f3cSTrond Myklebust rqstp->rq_chandle.thread_wait = 1*HZ; 73722700f3cSTrond Myklebust return rqstp->rq_xprt; 7380f0257eaSTom Tucker } 7390f0257eaSTom Tucker 740e1d83ee6SRashika Kheria static void svc_add_new_temp_xprt(struct svc_serv *serv, struct svc_xprt *newxpt) 74165b2e665SJ. Bruce Fields { 74265b2e665SJ. Bruce Fields spin_lock_bh(&serv->sv_lock); 74365b2e665SJ. Bruce Fields set_bit(XPT_TEMP, &newxpt->xpt_flags); 74465b2e665SJ. Bruce Fields list_add(&newxpt->xpt_list, &serv->sv_tempsocks); 74565b2e665SJ. Bruce Fields serv->sv_tmpcnt++; 74665b2e665SJ. Bruce Fields if (serv->sv_temptimer.function == NULL) { 74765b2e665SJ. Bruce Fields /* setup timer to age temp transports */ 748841b86f3SKees Cook serv->sv_temptimer.function = svc_age_temp_xprts; 74965b2e665SJ. Bruce Fields mod_timer(&serv->sv_temptimer, 75065b2e665SJ. Bruce Fields jiffies + svc_conn_age_period * HZ); 75165b2e665SJ. Bruce Fields } 75265b2e665SJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 75365b2e665SJ. Bruce Fields svc_xprt_received(newxpt); 75465b2e665SJ. Bruce Fields } 75565b2e665SJ. Bruce Fields 7566797fa5aSJ. Bruce Fields static int svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt) 7576797fa5aSJ. Bruce Fields { 7586797fa5aSJ. Bruce Fields struct svc_serv *serv = rqstp->rq_server; 7596797fa5aSJ. Bruce Fields int len = 0; 7606797fa5aSJ. Bruce Fields 7611b644b6eSJ. Bruce Fields if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) { 7621b644b6eSJ. Bruce Fields dprintk("svc_recv: found XPT_CLOSE\n"); 763546125d1SScott Mayhew if (test_and_clear_bit(XPT_KILL_TEMP, &xprt->xpt_flags)) 764546125d1SScott Mayhew xprt->xpt_ops->xpo_kill_temp_xprt(xprt); 7651b644b6eSJ. Bruce Fields svc_delete_xprt(xprt); 766ca7896cdSJ. Bruce Fields /* Leave XPT_BUSY set on the dead xprt: */ 76783a712e0SJeff Layton goto out; 768ca7896cdSJ. Bruce Fields } 769ca7896cdSJ. Bruce Fields if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) { 7700f0257eaSTom Tucker struct svc_xprt *newxpt; 7710f0257eaSTom Tucker /* 7720f0257eaSTom Tucker * We know this module_get will succeed because the 7730f0257eaSTom Tucker * listener holds a reference too 7740f0257eaSTom Tucker */ 77565b2e665SJ. Bruce Fields __module_get(xprt->xpt_class->xcl_owner); 7760f0257eaSTom Tucker svc_check_conn_limits(xprt->xpt_server); 77765b2e665SJ. Bruce Fields newxpt = xprt->xpt_ops->xpo_accept(xprt); 77865b2e665SJ. Bruce Fields if (newxpt) 77965b2e665SJ. Bruce Fields svc_add_new_temp_xprt(serv, newxpt); 780c789102cSTrond Myklebust else 781c789102cSTrond Myklebust module_put(xprt->xpt_class->xcl_owner); 782ff3ac5c3STrond Myklebust } else if (svc_xprt_reserve_slot(rqstp, xprt)) { 7836797fa5aSJ. Bruce Fields /* XPT_DATA|XPT_DEFERRED case: */ 7840f0257eaSTom Tucker dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n", 7856797fa5aSJ. Bruce Fields rqstp, rqstp->rq_pool->sp_id, xprt, 7862c935bc5SPeter Zijlstra kref_read(&xprt->xpt_ref)); 7870f0257eaSTom Tucker rqstp->rq_deferred = svc_deferred_dequeue(xprt); 788ca7896cdSJ. Bruce Fields if (rqstp->rq_deferred) 7890f0257eaSTom Tucker len = svc_deferred_recv(rqstp); 790ca7896cdSJ. Bruce Fields else 7910f0257eaSTom Tucker len = xprt->xpt_ops->xpo_recvfrom(rqstp); 7920f0257eaSTom Tucker dprintk("svc: got len=%d\n", len); 793d10f27a7SJ. Bruce Fields rqstp->rq_reserved = serv->sv_max_mesg; 794d10f27a7SJ. Bruce Fields atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved); 7950f0257eaSTom Tucker } 7966797fa5aSJ. Bruce Fields /* clear XPT_BUSY: */ 797ca7896cdSJ. Bruce Fields svc_xprt_received(xprt); 79883a712e0SJeff Layton out: 79983a712e0SJeff Layton trace_svc_handle_xprt(xprt, len); 8006797fa5aSJ. Bruce Fields return len; 8016797fa5aSJ. Bruce Fields } 8026797fa5aSJ. Bruce Fields 8036797fa5aSJ. Bruce Fields /* 8046797fa5aSJ. Bruce Fields * Receive the next request on any transport. This code is carefully 8056797fa5aSJ. Bruce Fields * organised not to touch any cachelines in the shared svc_serv 8066797fa5aSJ. Bruce Fields * structure, only cachelines in the local svc_pool. 8076797fa5aSJ. Bruce Fields */ 8086797fa5aSJ. Bruce Fields int svc_recv(struct svc_rqst *rqstp, long timeout) 8096797fa5aSJ. Bruce Fields { 8106797fa5aSJ. Bruce Fields struct svc_xprt *xprt = NULL; 8116797fa5aSJ. Bruce Fields struct svc_serv *serv = rqstp->rq_server; 8126797fa5aSJ. Bruce Fields int len, err; 8136797fa5aSJ. Bruce Fields 8146797fa5aSJ. Bruce Fields dprintk("svc: server %p waiting for data (to = %ld)\n", 8156797fa5aSJ. Bruce Fields rqstp, timeout); 8166797fa5aSJ. Bruce Fields 8176797fa5aSJ. Bruce Fields if (rqstp->rq_xprt) 8186797fa5aSJ. Bruce Fields printk(KERN_ERR 8196797fa5aSJ. Bruce Fields "svc_recv: service %p, transport not NULL!\n", 8206797fa5aSJ. Bruce Fields rqstp); 821983c6844STrond Myklebust 8226797fa5aSJ. Bruce Fields err = svc_alloc_arg(rqstp); 8236797fa5aSJ. Bruce Fields if (err) 824860a0d9eSJeff Layton goto out; 8256797fa5aSJ. Bruce Fields 8266797fa5aSJ. Bruce Fields try_to_freeze(); 8276797fa5aSJ. Bruce Fields cond_resched(); 828860a0d9eSJeff Layton err = -EINTR; 8296797fa5aSJ. Bruce Fields if (signalled() || kthread_should_stop()) 830860a0d9eSJeff Layton goto out; 8316797fa5aSJ. Bruce Fields 8326797fa5aSJ. Bruce Fields xprt = svc_get_next_xprt(rqstp, timeout); 833860a0d9eSJeff Layton if (IS_ERR(xprt)) { 834860a0d9eSJeff Layton err = PTR_ERR(xprt); 835860a0d9eSJeff Layton goto out; 836860a0d9eSJeff Layton } 8376797fa5aSJ. Bruce Fields 8386797fa5aSJ. Bruce Fields len = svc_handle_xprt(rqstp, xprt); 8390f0257eaSTom Tucker 8400f0257eaSTom Tucker /* No data, incomplete (TCP) read, or accept() */ 841860a0d9eSJeff Layton err = -EAGAIN; 8429f9d2ebeSJ. Bruce Fields if (len <= 0) 843860a0d9eSJeff Layton goto out_release; 844ca7896cdSJ. Bruce Fields 8450f0257eaSTom Tucker clear_bit(XPT_OLD, &xprt->xpt_flags); 8460f0257eaSTom Tucker 847989f881eSChuck Lever xprt->xpt_ops->xpo_secure_port(rqstp); 8480f0257eaSTom Tucker rqstp->rq_chandle.defer = svc_defer; 849860a0d9eSJeff Layton rqstp->rq_xid = svc_getu32(&rqstp->rq_arg.head[0]); 8500f0257eaSTom Tucker 8510f0257eaSTom Tucker if (serv->sv_stats) 8520f0257eaSTom Tucker serv->sv_stats->netcnt++; 853860a0d9eSJeff Layton trace_svc_recv(rqstp, len); 8540f0257eaSTom Tucker return len; 855860a0d9eSJeff Layton out_release: 856ca7896cdSJ. Bruce Fields rqstp->rq_res.len = 0; 857ca7896cdSJ. Bruce Fields svc_xprt_release(rqstp); 858860a0d9eSJeff Layton out: 859860a0d9eSJeff Layton trace_svc_recv(rqstp, err); 860860a0d9eSJeff Layton return err; 8610f0257eaSTom Tucker } 86224c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_recv); 8630f0257eaSTom Tucker 8640f0257eaSTom Tucker /* 8650f0257eaSTom Tucker * Drop request 8660f0257eaSTom Tucker */ 8670f0257eaSTom Tucker void svc_drop(struct svc_rqst *rqstp) 8680f0257eaSTom Tucker { 869104f6351STrond Myklebust trace_svc_drop(rqstp); 8700f0257eaSTom Tucker dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt); 8710f0257eaSTom Tucker svc_xprt_release(rqstp); 8720f0257eaSTom Tucker } 87324c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(svc_drop); 8740f0257eaSTom Tucker 8750f0257eaSTom Tucker /* 8760f0257eaSTom Tucker * Return reply to client. 8770f0257eaSTom Tucker */ 8780f0257eaSTom Tucker int svc_send(struct svc_rqst *rqstp) 8790f0257eaSTom Tucker { 8800f0257eaSTom Tucker struct svc_xprt *xprt; 881860a0d9eSJeff Layton int len = -EFAULT; 8820f0257eaSTom Tucker struct xdr_buf *xb; 8830f0257eaSTom Tucker 8840f0257eaSTom Tucker xprt = rqstp->rq_xprt; 8850f0257eaSTom Tucker if (!xprt) 886860a0d9eSJeff Layton goto out; 8870f0257eaSTom Tucker 8880f0257eaSTom Tucker /* release the receive skb before sending the reply */ 88963a1b156SChuck Lever xprt->xpt_ops->xpo_release_rqst(rqstp); 8900f0257eaSTom Tucker 8910f0257eaSTom Tucker /* calculate over-all length */ 8920f0257eaSTom Tucker xb = &rqstp->rq_res; 8930f0257eaSTom Tucker xb->len = xb->head[0].iov_len + 8940f0257eaSTom Tucker xb->page_len + 8950f0257eaSTom Tucker xb->tail[0].iov_len; 8960f0257eaSTom Tucker 8970f0257eaSTom Tucker /* Grab mutex to serialize outgoing data. */ 8980f0257eaSTom Tucker mutex_lock(&xprt->xpt_mutex); 899f06f00a2SJ. Bruce Fields if (test_bit(XPT_DEAD, &xprt->xpt_flags) 900f06f00a2SJ. Bruce Fields || test_bit(XPT_CLOSE, &xprt->xpt_flags)) 9010f0257eaSTom Tucker len = -ENOTCONN; 9020f0257eaSTom Tucker else 9030f0257eaSTom Tucker len = xprt->xpt_ops->xpo_sendto(rqstp); 9040f0257eaSTom Tucker mutex_unlock(&xprt->xpt_mutex); 9054cfc7e60SRahul Iyer rpc_wake_up(&xprt->xpt_bc_pending); 9060f0257eaSTom Tucker svc_xprt_release(rqstp); 9070f0257eaSTom Tucker 9080f0257eaSTom Tucker if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN) 909860a0d9eSJeff Layton len = 0; 910860a0d9eSJeff Layton out: 911860a0d9eSJeff Layton trace_svc_send(rqstp, len); 9120f0257eaSTom Tucker return len; 9130f0257eaSTom Tucker } 9140f0257eaSTom Tucker 9150f0257eaSTom Tucker /* 9160f0257eaSTom Tucker * Timer function to close old temporary transports, using 9170f0257eaSTom Tucker * a mark-and-sweep algorithm. 9180f0257eaSTom Tucker */ 919ff861c4dSKees Cook static void svc_age_temp_xprts(struct timer_list *t) 9200f0257eaSTom Tucker { 921ff861c4dSKees Cook struct svc_serv *serv = from_timer(serv, t, sv_temptimer); 9220f0257eaSTom Tucker struct svc_xprt *xprt; 9230f0257eaSTom Tucker struct list_head *le, *next; 9240f0257eaSTom Tucker 9250f0257eaSTom Tucker dprintk("svc_age_temp_xprts\n"); 9260f0257eaSTom Tucker 9270f0257eaSTom Tucker if (!spin_trylock_bh(&serv->sv_lock)) { 9280f0257eaSTom Tucker /* busy, try again 1 sec later */ 9290f0257eaSTom Tucker dprintk("svc_age_temp_xprts: busy\n"); 9300f0257eaSTom Tucker mod_timer(&serv->sv_temptimer, jiffies + HZ); 9310f0257eaSTom Tucker return; 9320f0257eaSTom Tucker } 9330f0257eaSTom Tucker 9340f0257eaSTom Tucker list_for_each_safe(le, next, &serv->sv_tempsocks) { 9350f0257eaSTom Tucker xprt = list_entry(le, struct svc_xprt, xpt_list); 9360f0257eaSTom Tucker 9370f0257eaSTom Tucker /* First time through, just mark it OLD. Second time 9380f0257eaSTom Tucker * through, close it. */ 9390f0257eaSTom Tucker if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags)) 9400f0257eaSTom Tucker continue; 9412c935bc5SPeter Zijlstra if (kref_read(&xprt->xpt_ref) > 1 || 942f64f9e71SJoe Perches test_bit(XPT_BUSY, &xprt->xpt_flags)) 9430f0257eaSTom Tucker continue; 944e75bafbfSJ. Bruce Fields list_del_init(le); 9450f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 9460f0257eaSTom Tucker dprintk("queuing xprt %p for closing\n", xprt); 9470f0257eaSTom Tucker 9480f0257eaSTom Tucker /* a thread will dequeue and close it soon */ 9490f0257eaSTom Tucker svc_xprt_enqueue(xprt); 9500f0257eaSTom Tucker } 951e75bafbfSJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 9520f0257eaSTom Tucker 9530f0257eaSTom Tucker mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ); 9540f0257eaSTom Tucker } 9550f0257eaSTom Tucker 956c3d4879eSScott Mayhew /* Close temporary transports whose xpt_local matches server_addr immediately 957c3d4879eSScott Mayhew * instead of waiting for them to be picked up by the timer. 958c3d4879eSScott Mayhew * 959c3d4879eSScott Mayhew * This is meant to be called from a notifier_block that runs when an ip 960c3d4879eSScott Mayhew * address is deleted. 961c3d4879eSScott Mayhew */ 962c3d4879eSScott Mayhew void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) 963c3d4879eSScott Mayhew { 964c3d4879eSScott Mayhew struct svc_xprt *xprt; 965c3d4879eSScott Mayhew struct list_head *le, *next; 966c3d4879eSScott Mayhew LIST_HEAD(to_be_closed); 967c3d4879eSScott Mayhew 968c3d4879eSScott Mayhew spin_lock_bh(&serv->sv_lock); 969c3d4879eSScott Mayhew list_for_each_safe(le, next, &serv->sv_tempsocks) { 970c3d4879eSScott Mayhew xprt = list_entry(le, struct svc_xprt, xpt_list); 971c3d4879eSScott Mayhew if (rpc_cmp_addr(server_addr, (struct sockaddr *) 972c3d4879eSScott Mayhew &xprt->xpt_local)) { 973c3d4879eSScott Mayhew dprintk("svc_age_temp_xprts_now: found %p\n", xprt); 974c3d4879eSScott Mayhew list_move(le, &to_be_closed); 975c3d4879eSScott Mayhew } 976c3d4879eSScott Mayhew } 977c3d4879eSScott Mayhew spin_unlock_bh(&serv->sv_lock); 978c3d4879eSScott Mayhew 979c3d4879eSScott Mayhew while (!list_empty(&to_be_closed)) { 980c3d4879eSScott Mayhew le = to_be_closed.next; 981c3d4879eSScott Mayhew list_del_init(le); 982c3d4879eSScott Mayhew xprt = list_entry(le, struct svc_xprt, xpt_list); 983546125d1SScott Mayhew set_bit(XPT_CLOSE, &xprt->xpt_flags); 984546125d1SScott Mayhew set_bit(XPT_KILL_TEMP, &xprt->xpt_flags); 985546125d1SScott Mayhew dprintk("svc_age_temp_xprts_now: queuing xprt %p for closing\n", 986546125d1SScott Mayhew xprt); 987546125d1SScott Mayhew svc_xprt_enqueue(xprt); 988c3d4879eSScott Mayhew } 989c3d4879eSScott Mayhew } 990c3d4879eSScott Mayhew EXPORT_SYMBOL_GPL(svc_age_temp_xprts_now); 991c3d4879eSScott Mayhew 992edc7a894SJ. Bruce Fields static void call_xpt_users(struct svc_xprt *xprt) 993edc7a894SJ. Bruce Fields { 994edc7a894SJ. Bruce Fields struct svc_xpt_user *u; 995edc7a894SJ. Bruce Fields 996edc7a894SJ. Bruce Fields spin_lock(&xprt->xpt_lock); 997edc7a894SJ. Bruce Fields while (!list_empty(&xprt->xpt_users)) { 998edc7a894SJ. Bruce Fields u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list); 999edc7a894SJ. Bruce Fields list_del(&u->list); 1000edc7a894SJ. Bruce Fields u->callback(u); 1001edc7a894SJ. Bruce Fields } 1002edc7a894SJ. Bruce Fields spin_unlock(&xprt->xpt_lock); 1003edc7a894SJ. Bruce Fields } 1004edc7a894SJ. Bruce Fields 10050f0257eaSTom Tucker /* 10060f0257eaSTom Tucker * Remove a dead transport 10070f0257eaSTom Tucker */ 10087710ec36SJ. Bruce Fields static void svc_delete_xprt(struct svc_xprt *xprt) 10090f0257eaSTom Tucker { 10100f0257eaSTom Tucker struct svc_serv *serv = xprt->xpt_server; 101122945e4aSTom Tucker struct svc_deferred_req *dr; 101222945e4aSTom Tucker 101322945e4aSTom Tucker /* Only do this once */ 101422945e4aSTom Tucker if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) 1015ac9303ebSJ. Bruce Fields BUG(); 10160f0257eaSTom Tucker 10170f0257eaSTom Tucker dprintk("svc: svc_delete_xprt(%p)\n", xprt); 10180f0257eaSTom Tucker xprt->xpt_ops->xpo_detach(xprt); 10190f0257eaSTom Tucker 10200f0257eaSTom Tucker spin_lock_bh(&serv->sv_lock); 10210f0257eaSTom Tucker list_del_init(&xprt->xpt_list); 102201047298SWeston Andros Adamson WARN_ON_ONCE(!list_empty(&xprt->xpt_ready)); 10230f0257eaSTom Tucker if (test_bit(XPT_TEMP, &xprt->xpt_flags)) 10240f0257eaSTom Tucker serv->sv_tmpcnt--; 1025788e69e5SJ. Bruce Fields spin_unlock_bh(&serv->sv_lock); 102622945e4aSTom Tucker 1027ab1b18f7SNeil Brown while ((dr = svc_deferred_dequeue(xprt)) != NULL) 102822945e4aSTom Tucker kfree(dr); 102922945e4aSTom Tucker 1030edc7a894SJ. Bruce Fields call_xpt_users(xprt); 103122945e4aSTom Tucker svc_xprt_put(xprt); 10320f0257eaSTom Tucker } 10330f0257eaSTom Tucker 10340f0257eaSTom Tucker void svc_close_xprt(struct svc_xprt *xprt) 10350f0257eaSTom Tucker { 10360f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 10370f0257eaSTom Tucker if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) 10380f0257eaSTom Tucker /* someone else will have to effect the close */ 10390f0257eaSTom Tucker return; 1040b1763316SJ. Bruce Fields /* 1041b1763316SJ. Bruce Fields * We expect svc_close_xprt() to work even when no threads are 1042b1763316SJ. Bruce Fields * running (e.g., while configuring the server before starting 1043b1763316SJ. Bruce Fields * any threads), so if the transport isn't busy, we delete 1044b1763316SJ. Bruce Fields * it ourself: 1045b1763316SJ. Bruce Fields */ 10460f0257eaSTom Tucker svc_delete_xprt(xprt); 10470f0257eaSTom Tucker } 1048a217813fSTom Tucker EXPORT_SYMBOL_GPL(svc_close_xprt); 10490f0257eaSTom Tucker 1050cc630d9fSJ. Bruce Fields static int svc_close_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net) 10510f0257eaSTom Tucker { 10520f0257eaSTom Tucker struct svc_xprt *xprt; 1053cc630d9fSJ. Bruce Fields int ret = 0; 10540f0257eaSTom Tucker 1055719f8bccSJ. Bruce Fields spin_lock(&serv->sv_lock); 1056b4f36f88SJ. Bruce Fields list_for_each_entry(xprt, xprt_list, xpt_list) { 10577b147f1fSStanislav Kinsbursky if (xprt->xpt_net != net) 10587b147f1fSStanislav Kinsbursky continue; 1059cc630d9fSJ. Bruce Fields ret++; 10600f0257eaSTom Tucker set_bit(XPT_CLOSE, &xprt->xpt_flags); 1061cc630d9fSJ. Bruce Fields svc_xprt_enqueue(xprt); 10620f0257eaSTom Tucker } 1063719f8bccSJ. Bruce Fields spin_unlock(&serv->sv_lock); 1064cc630d9fSJ. Bruce Fields return ret; 10650f0257eaSTom Tucker } 10660f0257eaSTom Tucker 1067cc630d9fSJ. Bruce Fields static struct svc_xprt *svc_dequeue_net(struct svc_serv *serv, struct net *net) 10682fefb8a0SJ. Bruce Fields { 1069b4f36f88SJ. Bruce Fields struct svc_pool *pool; 1070b4f36f88SJ. Bruce Fields struct svc_xprt *xprt; 1071b4f36f88SJ. Bruce Fields struct svc_xprt *tmp; 1072b4f36f88SJ. Bruce Fields int i; 1073b4f36f88SJ. Bruce Fields 1074b4f36f88SJ. Bruce Fields for (i = 0; i < serv->sv_nrpools; i++) { 1075b4f36f88SJ. Bruce Fields pool = &serv->sv_pools[i]; 1076b4f36f88SJ. Bruce Fields 1077b4f36f88SJ. Bruce Fields spin_lock_bh(&pool->sp_lock); 10786f513365SStanislav Kinsbursky list_for_each_entry_safe(xprt, tmp, &pool->sp_sockets, xpt_ready) { 10797b147f1fSStanislav Kinsbursky if (xprt->xpt_net != net) 10807b147f1fSStanislav Kinsbursky continue; 1081b4f36f88SJ. Bruce Fields list_del_init(&xprt->xpt_ready); 1082cc630d9fSJ. Bruce Fields spin_unlock_bh(&pool->sp_lock); 1083cc630d9fSJ. Bruce Fields return xprt; 1084b4f36f88SJ. Bruce Fields } 1085b4f36f88SJ. Bruce Fields spin_unlock_bh(&pool->sp_lock); 1086b4f36f88SJ. Bruce Fields } 1087cc630d9fSJ. Bruce Fields return NULL; 10886f513365SStanislav Kinsbursky } 10896f513365SStanislav Kinsbursky 1090cc630d9fSJ. Bruce Fields static void svc_clean_up_xprts(struct svc_serv *serv, struct net *net) 10916f513365SStanislav Kinsbursky { 10926f513365SStanislav Kinsbursky struct svc_xprt *xprt; 10936f513365SStanislav Kinsbursky 1094cc630d9fSJ. Bruce Fields while ((xprt = svc_dequeue_net(serv, net))) { 1095cc630d9fSJ. Bruce Fields set_bit(XPT_CLOSE, &xprt->xpt_flags); 1096719f8bccSJ. Bruce Fields svc_delete_xprt(xprt); 10973a22bf50SStanislav Kinsbursky } 1098cc630d9fSJ. Bruce Fields } 10993a22bf50SStanislav Kinsbursky 1100cc630d9fSJ. Bruce Fields /* 1101cc630d9fSJ. Bruce Fields * Server threads may still be running (especially in the case where the 1102cc630d9fSJ. Bruce Fields * service is still running in other network namespaces). 1103cc630d9fSJ. Bruce Fields * 1104cc630d9fSJ. Bruce Fields * So we shut down sockets the same way we would on a running server, by 1105cc630d9fSJ. Bruce Fields * setting XPT_CLOSE, enqueuing, and letting a thread pick it up to do 1106cc630d9fSJ. Bruce Fields * the close. In the case there are no such other threads, 1107cc630d9fSJ. Bruce Fields * threads running, svc_clean_up_xprts() does a simple version of a 1108cc630d9fSJ. Bruce Fields * server's main event loop, and in the case where there are other 1109cc630d9fSJ. Bruce Fields * threads, we may need to wait a little while and then check again to 1110cc630d9fSJ. Bruce Fields * see if they're done. 1111cc630d9fSJ. Bruce Fields */ 11127b147f1fSStanislav Kinsbursky void svc_close_net(struct svc_serv *serv, struct net *net) 11133a22bf50SStanislav Kinsbursky { 1114cc630d9fSJ. Bruce Fields int delay = 0; 11156f513365SStanislav Kinsbursky 1116cc630d9fSJ. Bruce Fields while (svc_close_list(serv, &serv->sv_permsocks, net) + 1117cc630d9fSJ. Bruce Fields svc_close_list(serv, &serv->sv_tempsocks, net)) { 1118cc630d9fSJ. Bruce Fields 1119cc630d9fSJ. Bruce Fields svc_clean_up_xprts(serv, net); 1120cc630d9fSJ. Bruce Fields msleep(delay++); 1121cc630d9fSJ. Bruce Fields } 11222fefb8a0SJ. Bruce Fields } 11232fefb8a0SJ. Bruce Fields 11240f0257eaSTom Tucker /* 11250f0257eaSTom Tucker * Handle defer and revisit of requests 11260f0257eaSTom Tucker */ 11270f0257eaSTom Tucker 11280f0257eaSTom Tucker static void svc_revisit(struct cache_deferred_req *dreq, int too_many) 11290f0257eaSTom Tucker { 11300f0257eaSTom Tucker struct svc_deferred_req *dr = 11310f0257eaSTom Tucker container_of(dreq, struct svc_deferred_req, handle); 11320f0257eaSTom Tucker struct svc_xprt *xprt = dr->xprt; 11330f0257eaSTom Tucker 113422945e4aSTom Tucker spin_lock(&xprt->xpt_lock); 113522945e4aSTom Tucker set_bit(XPT_DEFERRED, &xprt->xpt_flags); 113622945e4aSTom Tucker if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) { 113722945e4aSTom Tucker spin_unlock(&xprt->xpt_lock); 113822945e4aSTom Tucker dprintk("revisit canceled\n"); 11390f0257eaSTom Tucker svc_xprt_put(xprt); 1140104f6351STrond Myklebust trace_svc_drop_deferred(dr); 11410f0257eaSTom Tucker kfree(dr); 11420f0257eaSTom Tucker return; 11430f0257eaSTom Tucker } 11440f0257eaSTom Tucker dprintk("revisit queued\n"); 11450f0257eaSTom Tucker dr->xprt = NULL; 11460f0257eaSTom Tucker list_add(&dr->handle.recent, &xprt->xpt_deferred); 11470f0257eaSTom Tucker spin_unlock(&xprt->xpt_lock); 11480f0257eaSTom Tucker svc_xprt_enqueue(xprt); 11490f0257eaSTom Tucker svc_xprt_put(xprt); 11500f0257eaSTom Tucker } 11510f0257eaSTom Tucker 1152260c1d12STom Tucker /* 1153260c1d12STom Tucker * Save the request off for later processing. The request buffer looks 1154260c1d12STom Tucker * like this: 1155260c1d12STom Tucker * 1156260c1d12STom Tucker * <xprt-header><rpc-header><rpc-pagelist><rpc-tail> 1157260c1d12STom Tucker * 1158260c1d12STom Tucker * This code can only handle requests that consist of an xprt-header 1159260c1d12STom Tucker * and rpc-header. 1160260c1d12STom Tucker */ 11610f0257eaSTom Tucker static struct cache_deferred_req *svc_defer(struct cache_req *req) 11620f0257eaSTom Tucker { 11630f0257eaSTom Tucker struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle); 11640f0257eaSTom Tucker struct svc_deferred_req *dr; 11650f0257eaSTom Tucker 116630660e04SJeff Layton if (rqstp->rq_arg.page_len || !test_bit(RQ_USEDEFERRAL, &rqstp->rq_flags)) 11670f0257eaSTom Tucker return NULL; /* if more than a page, give up FIXME */ 11680f0257eaSTom Tucker if (rqstp->rq_deferred) { 11690f0257eaSTom Tucker dr = rqstp->rq_deferred; 11700f0257eaSTom Tucker rqstp->rq_deferred = NULL; 11710f0257eaSTom Tucker } else { 1172260c1d12STom Tucker size_t skip; 1173260c1d12STom Tucker size_t size; 11740f0257eaSTom Tucker /* FIXME maybe discard if size too large */ 1175260c1d12STom Tucker size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len; 11760f0257eaSTom Tucker dr = kmalloc(size, GFP_KERNEL); 11770f0257eaSTom Tucker if (dr == NULL) 11780f0257eaSTom Tucker return NULL; 11790f0257eaSTom Tucker 11800f0257eaSTom Tucker dr->handle.owner = rqstp->rq_server; 11810f0257eaSTom Tucker dr->prot = rqstp->rq_prot; 11820f0257eaSTom Tucker memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen); 11830f0257eaSTom Tucker dr->addrlen = rqstp->rq_addrlen; 11840f0257eaSTom Tucker dr->daddr = rqstp->rq_daddr; 11850f0257eaSTom Tucker dr->argslen = rqstp->rq_arg.len >> 2; 1186260c1d12STom Tucker dr->xprt_hlen = rqstp->rq_xprt_hlen; 1187260c1d12STom Tucker 1188260c1d12STom Tucker /* back up head to the start of the buffer and copy */ 1189260c1d12STom Tucker skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; 11900f0257eaSTom Tucker memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip, 11910f0257eaSTom Tucker dr->argslen << 2); 11920f0257eaSTom Tucker } 11930f0257eaSTom Tucker svc_xprt_get(rqstp->rq_xprt); 11940f0257eaSTom Tucker dr->xprt = rqstp->rq_xprt; 119578b65eb3SJeff Layton set_bit(RQ_DROPME, &rqstp->rq_flags); 11960f0257eaSTom Tucker 11970f0257eaSTom Tucker dr->handle.revisit = svc_revisit; 1198104f6351STrond Myklebust trace_svc_defer(rqstp); 11990f0257eaSTom Tucker return &dr->handle; 12000f0257eaSTom Tucker } 12010f0257eaSTom Tucker 12020f0257eaSTom Tucker /* 12030f0257eaSTom Tucker * recv data from a deferred request into an active one 12040f0257eaSTom Tucker */ 12050f0257eaSTom Tucker static int svc_deferred_recv(struct svc_rqst *rqstp) 12060f0257eaSTom Tucker { 12070f0257eaSTom Tucker struct svc_deferred_req *dr = rqstp->rq_deferred; 12080f0257eaSTom Tucker 1209260c1d12STom Tucker /* setup iov_base past transport header */ 1210260c1d12STom Tucker rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2); 1211260c1d12STom Tucker /* The iov_len does not include the transport header bytes */ 1212260c1d12STom Tucker rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen; 12130f0257eaSTom Tucker rqstp->rq_arg.page_len = 0; 1214260c1d12STom Tucker /* The rq_arg.len includes the transport header bytes */ 12150f0257eaSTom Tucker rqstp->rq_arg.len = dr->argslen<<2; 12160f0257eaSTom Tucker rqstp->rq_prot = dr->prot; 12170f0257eaSTom Tucker memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen); 12180f0257eaSTom Tucker rqstp->rq_addrlen = dr->addrlen; 1219260c1d12STom Tucker /* Save off transport header len in case we get deferred again */ 1220260c1d12STom Tucker rqstp->rq_xprt_hlen = dr->xprt_hlen; 12210f0257eaSTom Tucker rqstp->rq_daddr = dr->daddr; 12220f0257eaSTom Tucker rqstp->rq_respages = rqstp->rq_pages; 1223260c1d12STom Tucker return (dr->argslen<<2) - dr->xprt_hlen; 12240f0257eaSTom Tucker } 12250f0257eaSTom Tucker 12260f0257eaSTom Tucker 12270f0257eaSTom Tucker static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt) 12280f0257eaSTom Tucker { 12290f0257eaSTom Tucker struct svc_deferred_req *dr = NULL; 12300f0257eaSTom Tucker 12310f0257eaSTom Tucker if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags)) 12320f0257eaSTom Tucker return NULL; 12330f0257eaSTom Tucker spin_lock(&xprt->xpt_lock); 12340f0257eaSTom Tucker if (!list_empty(&xprt->xpt_deferred)) { 12350f0257eaSTom Tucker dr = list_entry(xprt->xpt_deferred.next, 12360f0257eaSTom Tucker struct svc_deferred_req, 12370f0257eaSTom Tucker handle.recent); 12380f0257eaSTom Tucker list_del_init(&dr->handle.recent); 1239104f6351STrond Myklebust trace_svc_revisit_deferred(dr); 124062bac4afSJ. Bruce Fields } else 124162bac4afSJ. Bruce Fields clear_bit(XPT_DEFERRED, &xprt->xpt_flags); 12420f0257eaSTom Tucker spin_unlock(&xprt->xpt_lock); 12430f0257eaSTom Tucker return dr; 12440f0257eaSTom Tucker } 12457fcb98d5STom Tucker 1246156e6209SChuck Lever /** 1247156e6209SChuck Lever * svc_find_xprt - find an RPC transport instance 1248156e6209SChuck Lever * @serv: pointer to svc_serv to search 1249156e6209SChuck Lever * @xcl_name: C string containing transport's class name 12504cb54ca2SStanislav Kinsbursky * @net: owner net pointer 1251156e6209SChuck Lever * @af: Address family of transport's local address 1252156e6209SChuck Lever * @port: transport's IP port number 1253156e6209SChuck Lever * 12547fcb98d5STom Tucker * Return the transport instance pointer for the endpoint accepting 12557fcb98d5STom Tucker * connections/peer traffic from the specified transport class, 12567fcb98d5STom Tucker * address family and port. 12577fcb98d5STom Tucker * 12587fcb98d5STom Tucker * Specifying 0 for the address family or port is effectively a 12597fcb98d5STom Tucker * wild-card, and will result in matching the first transport in the 12607fcb98d5STom Tucker * service's list that has a matching class name. 12617fcb98d5STom Tucker */ 1262156e6209SChuck Lever struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name, 12634cb54ca2SStanislav Kinsbursky struct net *net, const sa_family_t af, 12644cb54ca2SStanislav Kinsbursky const unsigned short port) 12657fcb98d5STom Tucker { 12667fcb98d5STom Tucker struct svc_xprt *xprt; 12677fcb98d5STom Tucker struct svc_xprt *found = NULL; 12687fcb98d5STom Tucker 12697fcb98d5STom Tucker /* Sanity check the args */ 1270156e6209SChuck Lever if (serv == NULL || xcl_name == NULL) 12717fcb98d5STom Tucker return found; 12727fcb98d5STom Tucker 12737fcb98d5STom Tucker spin_lock_bh(&serv->sv_lock); 12747fcb98d5STom Tucker list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 12754cb54ca2SStanislav Kinsbursky if (xprt->xpt_net != net) 12764cb54ca2SStanislav Kinsbursky continue; 12777fcb98d5STom Tucker if (strcmp(xprt->xpt_class->xcl_name, xcl_name)) 12787fcb98d5STom Tucker continue; 12797fcb98d5STom Tucker if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family) 12807fcb98d5STom Tucker continue; 1281156e6209SChuck Lever if (port != 0 && port != svc_xprt_local_port(xprt)) 12827fcb98d5STom Tucker continue; 12837fcb98d5STom Tucker found = xprt; 1284a217813fSTom Tucker svc_xprt_get(xprt); 12857fcb98d5STom Tucker break; 12867fcb98d5STom Tucker } 12877fcb98d5STom Tucker spin_unlock_bh(&serv->sv_lock); 12887fcb98d5STom Tucker return found; 12897fcb98d5STom Tucker } 12907fcb98d5STom Tucker EXPORT_SYMBOL_GPL(svc_find_xprt); 12919571af18STom Tucker 1292335c54bdSChuck Lever static int svc_one_xprt_name(const struct svc_xprt *xprt, 1293335c54bdSChuck Lever char *pos, int remaining) 1294335c54bdSChuck Lever { 1295335c54bdSChuck Lever int len; 1296335c54bdSChuck Lever 1297335c54bdSChuck Lever len = snprintf(pos, remaining, "%s %u\n", 1298335c54bdSChuck Lever xprt->xpt_class->xcl_name, 1299335c54bdSChuck Lever svc_xprt_local_port(xprt)); 1300335c54bdSChuck Lever if (len >= remaining) 1301335c54bdSChuck Lever return -ENAMETOOLONG; 1302335c54bdSChuck Lever return len; 1303335c54bdSChuck Lever } 1304335c54bdSChuck Lever 1305335c54bdSChuck Lever /** 1306335c54bdSChuck Lever * svc_xprt_names - format a buffer with a list of transport names 1307335c54bdSChuck Lever * @serv: pointer to an RPC service 1308335c54bdSChuck Lever * @buf: pointer to a buffer to be filled in 1309335c54bdSChuck Lever * @buflen: length of buffer to be filled in 1310335c54bdSChuck Lever * 1311335c54bdSChuck Lever * Fills in @buf with a string containing a list of transport names, 1312335c54bdSChuck Lever * each name terminated with '\n'. 1313335c54bdSChuck Lever * 1314335c54bdSChuck Lever * Returns positive length of the filled-in string on success; otherwise 1315335c54bdSChuck Lever * a negative errno value is returned if an error occurs. 13169571af18STom Tucker */ 1317335c54bdSChuck Lever int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen) 13189571af18STom Tucker { 13199571af18STom Tucker struct svc_xprt *xprt; 1320335c54bdSChuck Lever int len, totlen; 1321335c54bdSChuck Lever char *pos; 13229571af18STom Tucker 13239571af18STom Tucker /* Sanity check args */ 13249571af18STom Tucker if (!serv) 13259571af18STom Tucker return 0; 13269571af18STom Tucker 13279571af18STom Tucker spin_lock_bh(&serv->sv_lock); 1328335c54bdSChuck Lever 1329335c54bdSChuck Lever pos = buf; 1330335c54bdSChuck Lever totlen = 0; 13319571af18STom Tucker list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 1332335c54bdSChuck Lever len = svc_one_xprt_name(xprt, pos, buflen - totlen); 1333335c54bdSChuck Lever if (len < 0) { 1334335c54bdSChuck Lever *buf = '\0'; 1335335c54bdSChuck Lever totlen = len; 1336335c54bdSChuck Lever } 1337335c54bdSChuck Lever if (len <= 0) 13389571af18STom Tucker break; 1339335c54bdSChuck Lever 1340335c54bdSChuck Lever pos += len; 13419571af18STom Tucker totlen += len; 13429571af18STom Tucker } 1343335c54bdSChuck Lever 13449571af18STom Tucker spin_unlock_bh(&serv->sv_lock); 13459571af18STom Tucker return totlen; 13469571af18STom Tucker } 13479571af18STom Tucker EXPORT_SYMBOL_GPL(svc_xprt_names); 134803cf6c9fSGreg Banks 134903cf6c9fSGreg Banks 135003cf6c9fSGreg Banks /*----------------------------------------------------------------------------*/ 135103cf6c9fSGreg Banks 135203cf6c9fSGreg Banks static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos) 135303cf6c9fSGreg Banks { 135403cf6c9fSGreg Banks unsigned int pidx = (unsigned int)*pos; 135503cf6c9fSGreg Banks struct svc_serv *serv = m->private; 135603cf6c9fSGreg Banks 135703cf6c9fSGreg Banks dprintk("svc_pool_stats_start, *pidx=%u\n", pidx); 135803cf6c9fSGreg Banks 135903cf6c9fSGreg Banks if (!pidx) 136003cf6c9fSGreg Banks return SEQ_START_TOKEN; 136103cf6c9fSGreg Banks return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]); 136203cf6c9fSGreg Banks } 136303cf6c9fSGreg Banks 136403cf6c9fSGreg Banks static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos) 136503cf6c9fSGreg Banks { 136603cf6c9fSGreg Banks struct svc_pool *pool = p; 136703cf6c9fSGreg Banks struct svc_serv *serv = m->private; 136803cf6c9fSGreg Banks 136903cf6c9fSGreg Banks dprintk("svc_pool_stats_next, *pos=%llu\n", *pos); 137003cf6c9fSGreg Banks 137103cf6c9fSGreg Banks if (p == SEQ_START_TOKEN) { 137203cf6c9fSGreg Banks pool = &serv->sv_pools[0]; 137303cf6c9fSGreg Banks } else { 137403cf6c9fSGreg Banks unsigned int pidx = (pool - &serv->sv_pools[0]); 137503cf6c9fSGreg Banks if (pidx < serv->sv_nrpools-1) 137603cf6c9fSGreg Banks pool = &serv->sv_pools[pidx+1]; 137703cf6c9fSGreg Banks else 137803cf6c9fSGreg Banks pool = NULL; 137903cf6c9fSGreg Banks } 138003cf6c9fSGreg Banks ++*pos; 138103cf6c9fSGreg Banks return pool; 138203cf6c9fSGreg Banks } 138303cf6c9fSGreg Banks 138403cf6c9fSGreg Banks static void svc_pool_stats_stop(struct seq_file *m, void *p) 138503cf6c9fSGreg Banks { 138603cf6c9fSGreg Banks } 138703cf6c9fSGreg Banks 138803cf6c9fSGreg Banks static int svc_pool_stats_show(struct seq_file *m, void *p) 138903cf6c9fSGreg Banks { 139003cf6c9fSGreg Banks struct svc_pool *pool = p; 139103cf6c9fSGreg Banks 139203cf6c9fSGreg Banks if (p == SEQ_START_TOKEN) { 139378c210efSJ. Bruce Fields seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n"); 139403cf6c9fSGreg Banks return 0; 139503cf6c9fSGreg Banks } 139603cf6c9fSGreg Banks 139778c210efSJ. Bruce Fields seq_printf(m, "%u %lu %lu %lu %lu\n", 139803cf6c9fSGreg Banks pool->sp_id, 1399403c7b44SJeff Layton (unsigned long)atomic_long_read(&pool->sp_stats.packets), 140003cf6c9fSGreg Banks pool->sp_stats.sockets_queued, 1401403c7b44SJeff Layton (unsigned long)atomic_long_read(&pool->sp_stats.threads_woken), 1402403c7b44SJeff Layton (unsigned long)atomic_long_read(&pool->sp_stats.threads_timedout)); 140303cf6c9fSGreg Banks 140403cf6c9fSGreg Banks return 0; 140503cf6c9fSGreg Banks } 140603cf6c9fSGreg Banks 140703cf6c9fSGreg Banks static const struct seq_operations svc_pool_stats_seq_ops = { 140803cf6c9fSGreg Banks .start = svc_pool_stats_start, 140903cf6c9fSGreg Banks .next = svc_pool_stats_next, 141003cf6c9fSGreg Banks .stop = svc_pool_stats_stop, 141103cf6c9fSGreg Banks .show = svc_pool_stats_show, 141203cf6c9fSGreg Banks }; 141303cf6c9fSGreg Banks 141403cf6c9fSGreg Banks int svc_pool_stats_open(struct svc_serv *serv, struct file *file) 141503cf6c9fSGreg Banks { 141603cf6c9fSGreg Banks int err; 141703cf6c9fSGreg Banks 141803cf6c9fSGreg Banks err = seq_open(file, &svc_pool_stats_seq_ops); 141903cf6c9fSGreg Banks if (!err) 142003cf6c9fSGreg Banks ((struct seq_file *) file->private_data)->private = serv; 142103cf6c9fSGreg Banks return err; 142203cf6c9fSGreg Banks } 142303cf6c9fSGreg Banks EXPORT_SYMBOL(svc_pool_stats_open); 142403cf6c9fSGreg Banks 142503cf6c9fSGreg Banks /*----------------------------------------------------------------------------*/ 1426