svc_xprt.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*
  2. * linux/net/sunrpc/svc_xprt.c
  3. *
  4. * Author: Tom Tucker <tom@opengridcomputing.com>
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/errno.h>
  8. #include <linux/freezer.h>
  9. #include <linux/kthread.h>
  10. #include <linux/slab.h>
  11. #include <net/sock.h>
  12. #include <linux/sunrpc/stats.h>
  13. #include <linux/sunrpc/svc_xprt.h>
  14. #include <linux/sunrpc/svcsock.h>
  15. #include <linux/sunrpc/xprt.h>
  16. #include <linux/module.h>
  17. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  18. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
  19. static int svc_deferred_recv(struct svc_rqst *rqstp);
  20. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  21. static void svc_age_temp_xprts(unsigned long closure);
  22. static void svc_delete_xprt(struct svc_xprt *xprt);
  23. static void svc_xprt_do_enqueue(struct svc_xprt *xprt);
  24. /* apparently the "standard" is that clients close
  25. * idle connections after 5 minutes, servers after
  26. * 6 minutes
  27. * http://www.connectathon.org/talks96/nfstcp.pdf
  28. */
  29. static int svc_conn_age_period = 6*60;
  30. /* List of registered transport classes */
  31. static DEFINE_SPINLOCK(svc_xprt_class_lock);
  32. static LIST_HEAD(svc_xprt_class_list);
  33. /* SMP locking strategy:
  34. *
  35. * svc_pool->sp_lock protects most of the fields of that pool.
  36. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  37. * when both need to be taken (rare), svc_serv->sv_lock is first.
  38. * BKL protects svc_serv->sv_nrthread.
  39. * svc_sock->sk_lock protects the svc_sock->sk_deferred list
  40. * and the ->sk_info_authunix cache.
  41. *
  42. * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
  43. * enqueued multiply. During normal transport processing this bit
  44. * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
  45. * Providers should not manipulate this bit directly.
  46. *
  47. * Some flags can be set to certain values at any time
  48. * providing that certain rules are followed:
  49. *
  50. * XPT_CONN, XPT_DATA:
  51. * - Can be set or cleared at any time.
  52. * - After a set, svc_xprt_enqueue must be called to enqueue
  53. * the transport for processing.
  54. * - After a clear, the transport must be read/accepted.
  55. * If this succeeds, it must be set again.
  56. * XPT_CLOSE:
  57. * - Can set at any time. It is never cleared.
  58. * XPT_DEAD:
  59. * - Can only be set while XPT_BUSY is held which ensures
  60. * that no other thread will be using the transport or will
  61. * try to set XPT_DEAD.
  62. */
  63. int svc_reg_xprt_class(struct svc_xprt_class *xcl)
  64. {
  65. struct svc_xprt_class *cl;
  66. int res = -EEXIST;
  67. dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
  68. INIT_LIST_HEAD(&xcl->xcl_list);
  69. spin_lock(&svc_xprt_class_lock);
  70. /* Make sure there isn't already a class with the same name */
  71. list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
  72. if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
  73. goto out;
  74. }
  75. list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
  76. res = 0;
  77. out:
  78. spin_unlock(&svc_xprt_class_lock);
  79. return res;
  80. }
  81. EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
  82. void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
  83. {
  84. dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
  85. spin_lock(&svc_xprt_class_lock);
  86. list_del_init(&xcl->xcl_list);
  87. spin_unlock(&svc_xprt_class_lock);
  88. }
  89. EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
  90. /*
  91. * Format the transport list for printing
  92. */
  93. int svc_print_xprts(char *buf, int maxlen)
  94. {
  95. struct svc_xprt_class *xcl;
  96. char tmpstr[80];
  97. int len = 0;
  98. buf[0] = '\0';
  99. spin_lock(&svc_xprt_class_lock);
  100. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  101. int slen;
  102. sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
  103. slen = strlen(tmpstr);
  104. if (len + slen > maxlen)
  105. break;
  106. len += slen;
  107. strcat(buf, tmpstr);
  108. }
  109. spin_unlock(&svc_xprt_class_lock);
  110. return len;
  111. }
  112. static void svc_xprt_free(struct kref *kref)
  113. {
  114. struct svc_xprt *xprt =
  115. container_of(kref, struct svc_xprt, xpt_ref);
  116. struct module *owner = xprt->xpt_class->xcl_owner;
  117. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags))
  118. svcauth_unix_info_release(xprt);
  119. put_net(xprt->xpt_net);
  120. /* See comment on corresponding get in xs_setup_bc_tcp(): */
  121. if (xprt->xpt_bc_xprt)
  122. xprt_put(xprt->xpt_bc_xprt);
  123. xprt->xpt_ops->xpo_free(xprt);
  124. module_put(owner);
  125. }
  126. void svc_xprt_put(struct svc_xprt *xprt)
  127. {
  128. kref_put(&xprt->xpt_ref, svc_xprt_free);
  129. }
  130. EXPORT_SYMBOL_GPL(svc_xprt_put);
  131. /*
  132. * Called by transport drivers to initialize the transport independent
  133. * portion of the transport instance.
  134. */
  135. void svc_xprt_init(struct net *net, struct svc_xprt_class *xcl,
  136. struct svc_xprt *xprt, struct svc_serv *serv)
  137. {
  138. memset(xprt, 0, sizeof(*xprt));
  139. xprt->xpt_class = xcl;
  140. xprt->xpt_ops = xcl->xcl_ops;
  141. kref_init(&xprt->xpt_ref);
  142. xprt->xpt_server = serv;
  143. INIT_LIST_HEAD(&xprt->xpt_list);
  144. INIT_LIST_HEAD(&xprt->xpt_ready);
  145. INIT_LIST_HEAD(&xprt->xpt_deferred);
  146. INIT_LIST_HEAD(&xprt->xpt_users);
  147. mutex_init(&xprt->xpt_mutex);
  148. spin_lock_init(&xprt->xpt_lock);
  149. set_bit(XPT_BUSY, &xprt->xpt_flags);
  150. rpc_init_wait_queue(&xprt->xpt_bc_pending, "xpt_bc_pending");
  151. xprt->xpt_net = get_net(net);
  152. }
  153. EXPORT_SYMBOL_GPL(svc_xprt_init);
  154. static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
  155. struct svc_serv *serv,
  156. struct net *net,
  157. const int family,
  158. const unsigned short port,
  159. int flags)
  160. {
  161. struct sockaddr_in sin = {
  162. .sin_family = AF_INET,
  163. .sin_addr.s_addr = htonl(INADDR_ANY),
  164. .sin_port = htons(port),
  165. };
  166. #if IS_ENABLED(CONFIG_IPV6)
  167. struct sockaddr_in6 sin6 = {
  168. .sin6_family = AF_INET6,
  169. .sin6_addr = IN6ADDR_ANY_INIT,
  170. .sin6_port = htons(port),
  171. };
  172. #endif
  173. struct sockaddr *sap;
  174. size_t len;
  175. switch (family) {
  176. case PF_INET:
  177. sap = (struct sockaddr *)&sin;
  178. len = sizeof(sin);
  179. break;
  180. #if IS_ENABLED(CONFIG_IPV6)
  181. case PF_INET6:
  182. sap = (struct sockaddr *)&sin6;
  183. len = sizeof(sin6);
  184. break;
  185. #endif
  186. default:
  187. return ERR_PTR(-EAFNOSUPPORT);
  188. }
  189. return xcl->xcl_ops->xpo_create(serv, net, sap, len, flags);
  190. }
  191. /*
  192. * svc_xprt_received conditionally queues the transport for processing
  193. * by another thread. The caller must hold the XPT_BUSY bit and must
  194. * not thereafter touch transport data.
  195. *
  196. * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
  197. * insufficient) data.
  198. */
  199. static void svc_xprt_received(struct svc_xprt *xprt)
  200. {
  201. WARN_ON_ONCE(!test_bit(XPT_BUSY, &xprt->xpt_flags));
  202. if (!test_bit(XPT_BUSY, &xprt->xpt_flags))
  203. return;
  204. /* As soon as we clear busy, the xprt could be closed and
  205. * 'put', so we need a reference to call svc_xprt_do_enqueue with:
  206. */
  207. svc_xprt_get(xprt);
  208. smp_mb__before_atomic();
  209. clear_bit(XPT_BUSY, &xprt->xpt_flags);
  210. svc_xprt_do_enqueue(xprt);
  211. svc_xprt_put(xprt);
  212. }
  213. void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new)
  214. {
  215. clear_bit(XPT_TEMP, &new->xpt_flags);
  216. spin_lock_bh(&serv->sv_lock);
  217. list_add(&new->xpt_list, &serv->sv_permsocks);
  218. spin_unlock_bh(&serv->sv_lock);
  219. svc_xprt_received(new);
  220. }
  221. int svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
  222. struct net *net, const int family,
  223. const unsigned short port, int flags)
  224. {
  225. struct svc_xprt_class *xcl;
  226. dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
  227. spin_lock(&svc_xprt_class_lock);
  228. list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
  229. struct svc_xprt *newxprt;
  230. unsigned short newport;
  231. if (strcmp(xprt_name, xcl->xcl_name))
  232. continue;
  233. if (!try_module_get(xcl->xcl_owner))
  234. goto err;
  235. spin_unlock(&svc_xprt_class_lock);
  236. newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags);
  237. if (IS_ERR(newxprt)) {
  238. module_put(xcl->xcl_owner);
  239. return PTR_ERR(newxprt);
  240. }
  241. svc_add_new_perm_xprt(serv, newxprt);
  242. newport = svc_xprt_local_port(newxprt);
  243. return newport;
  244. }
  245. err:
  246. spin_unlock(&svc_xprt_class_lock);
  247. dprintk("svc: transport %s not found\n", xprt_name);
  248. /* This errno is exposed to user space. Provide a reasonable
  249. * perror msg for a bad transport. */
  250. return -EPROTONOSUPPORT;
  251. }
  252. EXPORT_SYMBOL_GPL(svc_create_xprt);
  253. /*
  254. * Copy the local and remote xprt addresses to the rqstp structure
  255. */
  256. void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  257. {
  258. memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
  259. rqstp->rq_addrlen = xprt->xpt_remotelen;
  260. /*
  261. * Destination address in request is needed for binding the
  262. * source address in RPC replies/callbacks later.
  263. */
  264. memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen);
  265. rqstp->rq_daddrlen = xprt->xpt_locallen;
  266. }
  267. EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
  268. /**
  269. * svc_print_addr - Format rq_addr field for printing
  270. * @rqstp: svc_rqst struct containing address to print
  271. * @buf: target buffer for formatted address
  272. * @len: length of target buffer
  273. *
  274. */
  275. char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
  276. {
  277. return __svc_print_addr(svc_addr(rqstp), buf, len);
  278. }
  279. EXPORT_SYMBOL_GPL(svc_print_addr);
  280. /*
  281. * Queue up an idle server thread. Must have pool->sp_lock held.
  282. * Note: this is really a stack rather than a queue, so that we only
  283. * use as many different threads as we need, and the rest don't pollute
  284. * the cache.
  285. */
  286. static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
  287. {
  288. list_add(&rqstp->rq_list, &pool->sp_threads);
  289. }
  290. /*
  291. * Dequeue an nfsd thread. Must have pool->sp_lock held.
  292. */
  293. static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
  294. {
  295. list_del(&rqstp->rq_list);
  296. }
  297. static bool svc_xprt_has_something_to_do(struct svc_xprt *xprt)
  298. {
  299. if (xprt->xpt_flags & ((1<<XPT_CONN)|(1<<XPT_CLOSE)))
  300. return true;
  301. if (xprt->xpt_flags & ((1<<XPT_DATA)|(1<<XPT_DEFERRED)))
  302. return xprt->xpt_ops->xpo_has_wspace(xprt);
  303. return false;
  304. }
  305. static void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  306. {
  307. struct svc_pool *pool;
  308. struct svc_rqst *rqstp;
  309. int cpu;
  310. if (!svc_xprt_has_something_to_do(xprt))
  311. return;
  312. cpu = get_cpu();
  313. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  314. put_cpu();
  315. spin_lock_bh(&pool->sp_lock);
  316. if (!list_empty(&pool->sp_threads) &&
  317. !list_empty(&pool->sp_sockets))
  318. printk(KERN_ERR
  319. "svc_xprt_enqueue: "
  320. "threads and transports both waiting??\n");
  321. pool->sp_stats.packets++;
  322. /* Mark transport as busy. It will remain in this state until
  323. * the provider calls svc_xprt_received. We update XPT_BUSY
  324. * atomically because it also guards against trying to enqueue
  325. * the transport twice.
  326. */
  327. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
  328. /* Don't enqueue transport while already enqueued */
  329. dprintk("svc: transport %p busy, not enqueued\n", xprt);
  330. goto out_unlock;
  331. }
  332. if (!list_empty(&pool->sp_threads)) {
  333. rqstp = list_entry(pool->sp_threads.next,
  334. struct svc_rqst,
  335. rq_list);
  336. dprintk("svc: transport %p served by daemon %p\n",
  337. xprt, rqstp);
  338. svc_thread_dequeue(pool, rqstp);
  339. if (rqstp->rq_xprt)
  340. printk(KERN_ERR
  341. "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
  342. rqstp, rqstp->rq_xprt);
  343. rqstp->rq_xprt = xprt;
  344. svc_xprt_get(xprt);
  345. pool->sp_stats.threads_woken++;
  346. wake_up(&rqstp->rq_wait);
  347. } else {
  348. dprintk("svc: transport %p put into queue\n", xprt);
  349. list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
  350. pool->sp_stats.sockets_queued++;
  351. }
  352. out_unlock:
  353. spin_unlock_bh(&pool->sp_lock);
  354. }
  355. /*
  356. * Queue up a transport with data pending. If there are idle nfsd
  357. * processes, wake 'em up.
  358. *
  359. */
  360. void svc_xprt_enqueue(struct svc_xprt *xprt)
  361. {
  362. if (test_bit(XPT_BUSY, &xprt->xpt_flags))
  363. return;
  364. svc_xprt_do_enqueue(xprt);
  365. }
  366. EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
  367. /*
  368. * Dequeue the first transport. Must be called with the pool->sp_lock held.
  369. */
  370. static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
  371. {
  372. struct svc_xprt *xprt;
  373. if (list_empty(&pool->sp_sockets))
  374. return NULL;
  375. xprt = list_entry(pool->sp_sockets.next,
  376. struct svc_xprt, xpt_ready);
  377. list_del_init(&xprt->xpt_ready);
  378. dprintk("svc: transport %p dequeued, inuse=%d\n",
  379. xprt, atomic_read(&xprt->xpt_ref.refcount));
  380. return xprt;
  381. }
  382. /**
  383. * svc_reserve - change the space reserved for the reply to a request.
  384. * @rqstp: The request in question
  385. * @space: new max space to reserve
  386. *
  387. * Each request reserves some space on the output queue of the transport
  388. * to make sure the reply fits. This function reduces that reserved
  389. * space to be the amount of space used already, plus @space.
  390. *
  391. */
  392. void svc_reserve(struct svc_rqst *rqstp, int space)
  393. {
  394. space += rqstp->rq_res.head[0].iov_len;
  395. if (space < rqstp->rq_reserved) {
  396. struct svc_xprt *xprt = rqstp->rq_xprt;
  397. atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
  398. rqstp->rq_reserved = space;
  399. if (xprt->xpt_ops->xpo_adjust_wspace)
  400. xprt->xpt_ops->xpo_adjust_wspace(xprt);
  401. svc_xprt_enqueue(xprt);
  402. }
  403. }
  404. EXPORT_SYMBOL_GPL(svc_reserve);
  405. static void svc_xprt_release(struct svc_rqst *rqstp)
  406. {
  407. struct svc_xprt *xprt = rqstp->rq_xprt;
  408. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  409. kfree(rqstp->rq_deferred);
  410. rqstp->rq_deferred = NULL;
  411. svc_free_res_pages(rqstp);
  412. rqstp->rq_res.page_len = 0;
  413. rqstp->rq_res.page_base = 0;
  414. /* Reset response buffer and release
  415. * the reservation.
  416. * But first, check that enough space was reserved
  417. * for the reply, otherwise we have a bug!
  418. */
  419. if ((rqstp->rq_res.len) > rqstp->rq_reserved)
  420. printk(KERN_ERR "RPC request reserved %d but used %d\n",
  421. rqstp->rq_reserved,
  422. rqstp->rq_res.len);
  423. rqstp->rq_res.head[0].iov_len = 0;
  424. svc_reserve(rqstp, 0);
  425. rqstp->rq_xprt = NULL;
  426. svc_xprt_put(xprt);
  427. }
  428. /*
  429. * External function to wake up a server waiting for data
  430. * This really only makes sense for services like lockd
  431. * which have exactly one thread anyway.
  432. */
  433. void svc_wake_up(struct svc_serv *serv)
  434. {
  435. struct svc_rqst *rqstp;
  436. unsigned int i;
  437. struct svc_pool *pool;
  438. for (i = 0; i < serv->sv_nrpools; i++) {
  439. pool = &serv->sv_pools[i];
  440. spin_lock_bh(&pool->sp_lock);
  441. if (!list_empty(&pool->sp_threads)) {
  442. rqstp = list_entry(pool->sp_threads.next,
  443. struct svc_rqst,
  444. rq_list);
  445. dprintk("svc: daemon %p woken up.\n", rqstp);
  446. /*
  447. svc_thread_dequeue(pool, rqstp);
  448. rqstp->rq_xprt = NULL;
  449. */
  450. wake_up(&rqstp->rq_wait);
  451. } else
  452. pool->sp_task_pending = 1;
  453. spin_unlock_bh(&pool->sp_lock);
  454. }
  455. }
  456. EXPORT_SYMBOL_GPL(svc_wake_up);
  457. int svc_port_is_privileged(struct sockaddr *sin)
  458. {
  459. switch (sin->sa_family) {
  460. case AF_INET:
  461. return ntohs(((struct sockaddr_in *)sin)->sin_port)
  462. < PROT_SOCK;
  463. case AF_INET6:
  464. return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
  465. < PROT_SOCK;
  466. default:
  467. return 0;
  468. }
  469. }
  470. /*
  471. * Make sure that we don't have too many active connections. If we have,
  472. * something must be dropped. It's not clear what will happen if we allow
  473. * "too many" connections, but when dealing with network-facing software,
  474. * we have to code defensively. Here we do that by imposing hard limits.
  475. *
  476. * There's no point in trying to do random drop here for DoS
  477. * prevention. The NFS clients does 1 reconnect in 15 seconds. An
  478. * attacker can easily beat that.
  479. *
  480. * The only somewhat efficient mechanism would be if drop old
  481. * connections from the same IP first. But right now we don't even
  482. * record the client IP in svc_sock.
  483. *
  484. * single-threaded services that expect a lot of clients will probably
  485. * need to set sv_maxconn to override the default value which is based
  486. * on the number of threads
  487. */
  488. static void svc_check_conn_limits(struct svc_serv *serv)
  489. {
  490. unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn :
  491. (serv->sv_nrthreads+3) * 20;
  492. if (serv->sv_tmpcnt > limit) {
  493. struct svc_xprt *xprt = NULL;
  494. spin_lock_bh(&serv->sv_lock);
  495. if (!list_empty(&serv->sv_tempsocks)) {
  496. /* Try to help the admin */
  497. net_notice_ratelimited("%s: too many open connections, consider increasing the %s\n",
  498. serv->sv_name, serv->sv_maxconn ?
  499. "max number of connections" :
  500. "number of threads");
  501. /*
  502. * Always select the oldest connection. It's not fair,
  503. * but so is life
  504. */
  505. xprt = list_entry(serv->sv_tempsocks.prev,
  506. struct svc_xprt,
  507. xpt_list);
  508. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  509. svc_xprt_get(xprt);
  510. }
  511. spin_unlock_bh(&serv->sv_lock);
  512. if (xprt) {
  513. svc_xprt_enqueue(xprt);
  514. svc_xprt_put(xprt);
  515. }
  516. }
  517. }
  518. static int svc_alloc_arg(struct svc_rqst *rqstp)
  519. {
  520. struct svc_serv *serv = rqstp->rq_server;
  521. struct xdr_buf *arg;
  522. int pages;
  523. int i;
  524. /* now allocate needed pages. If we get a failure, sleep briefly */
  525. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  526. WARN_ON_ONCE(pages >= RPCSVC_MAXPAGES);
  527. if (pages >= RPCSVC_MAXPAGES)
  528. /* use as many pages as possible */
  529. pages = RPCSVC_MAXPAGES - 1;
  530. for (i = 0; i < pages ; i++)
  531. while (rqstp->rq_pages[i] == NULL) {
  532. struct page *p = alloc_page(GFP_KERNEL);
  533. if (!p) {
  534. set_current_state(TASK_INTERRUPTIBLE);
  535. if (signalled() || kthread_should_stop()) {
  536. set_current_state(TASK_RUNNING);
  537. return -EINTR;
  538. }
  539. schedule_timeout(msecs_to_jiffies(500));
  540. }
  541. rqstp->rq_pages[i] = p;
  542. }
  543. rqstp->rq_page_end = &rqstp->rq_pages[i];
  544. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  545. /* Make arg->head point to first page and arg->pages point to rest */
  546. arg = &rqstp->rq_arg;
  547. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  548. arg->head[0].iov_len = PAGE_SIZE;
  549. arg->pages = rqstp->rq_pages + 1;
  550. arg->page_base = 0;
  551. /* save at least one page for response */
  552. arg->page_len = (pages-2)*PAGE_SIZE;
  553. arg->len = (pages-1)*PAGE_SIZE;
  554. arg->tail[0].iov_len = 0;
  555. return 0;
  556. }
  557. static struct svc_xprt *svc_get_next_xprt(struct svc_rqst *rqstp, long timeout)
  558. {
  559. struct svc_xprt *xprt;
  560. struct svc_pool *pool = rqstp->rq_pool;
  561. DECLARE_WAITQUEUE(wait, current);
  562. long time_left;
  563. /* Normally we will wait up to 5 seconds for any required
  564. * cache information to be provided.
  565. */
  566. rqstp->rq_chandle.thread_wait = 5*HZ;
  567. spin_lock_bh(&pool->sp_lock);
  568. xprt = svc_xprt_dequeue(pool);
  569. if (xprt) {
  570. rqstp->rq_xprt = xprt;
  571. svc_xprt_get(xprt);
  572. /* As there is a shortage of threads and this request
  573. * had to be queued, don't allow the thread to wait so
  574. * long for cache updates.
  575. */
  576. rqstp->rq_chandle.thread_wait = 1*HZ;
  577. pool->sp_task_pending = 0;
  578. } else {
  579. if (pool->sp_task_pending) {
  580. pool->sp_task_pending = 0;
  581. spin_unlock_bh(&pool->sp_lock);
  582. return ERR_PTR(-EAGAIN);
  583. }
  584. /* No data pending. Go to sleep */
  585. svc_thread_enqueue(pool, rqstp);
  586. /*
  587. * We have to be able to interrupt this wait
  588. * to bring down the daemons ...
  589. */
  590. set_current_state(TASK_INTERRUPTIBLE);
  591. /*
  592. * checking kthread_should_stop() here allows us to avoid
  593. * locking and signalling when stopping kthreads that call
  594. * svc_recv. If the thread has already been woken up, then
  595. * we can exit here without sleeping. If not, then it
  596. * it'll be woken up quickly during the schedule_timeout
  597. */
  598. if (kthread_should_stop()) {
  599. set_current_state(TASK_RUNNING);
  600. spin_unlock_bh(&pool->sp_lock);
  601. return ERR_PTR(-EINTR);
  602. }
  603. add_wait_queue(&rqstp->rq_wait, &wait);
  604. spin_unlock_bh(&pool->sp_lock);
  605. time_left = schedule_timeout(timeout);
  606. try_to_freeze();
  607. spin_lock_bh(&pool->sp_lock);
  608. remove_wait_queue(&rqstp->rq_wait, &wait);
  609. if (!time_left)
  610. pool->sp_stats.threads_timedout++;
  611. xprt = rqstp->rq_xprt;
  612. if (!xprt) {
  613. svc_thread_dequeue(pool, rqstp);
  614. spin_unlock_bh(&pool->sp_lock);
  615. dprintk("svc: server %p, no data yet\n", rqstp);
  616. if (signalled() || kthread_should_stop())
  617. return ERR_PTR(-EINTR);
  618. else
  619. return ERR_PTR(-EAGAIN);
  620. }
  621. }
  622. spin_unlock_bh(&pool->sp_lock);
  623. return xprt;
  624. }
  625. static void svc_add_new_temp_xprt(struct svc_serv *serv, struct svc_xprt *newxpt)
  626. {
  627. spin_lock_bh(&serv->sv_lock);
  628. set_bit(XPT_TEMP, &newxpt->xpt_flags);
  629. list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
  630. serv->sv_tmpcnt++;
  631. if (serv->sv_temptimer.function == NULL) {
  632. /* setup timer to age temp transports */
  633. setup_timer(&serv->sv_temptimer, svc_age_temp_xprts,
  634. (unsigned long)serv);
  635. mod_timer(&serv->sv_temptimer,
  636. jiffies + svc_conn_age_period * HZ);
  637. }
  638. spin_unlock_bh(&serv->sv_lock);
  639. svc_xprt_received(newxpt);
  640. }
  641. static int svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt)
  642. {
  643. struct svc_serv *serv = rqstp->rq_server;
  644. int len = 0;
  645. if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
  646. dprintk("svc_recv: found XPT_CLOSE\n");
  647. svc_delete_xprt(xprt);
  648. /* Leave XPT_BUSY set on the dead xprt: */
  649. return 0;
  650. }
  651. if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
  652. struct svc_xprt *newxpt;
  653. /*
  654. * We know this module_get will succeed because the
  655. * listener holds a reference too
  656. */
  657. __module_get(xprt->xpt_class->xcl_owner);
  658. svc_check_conn_limits(xprt->xpt_server);
  659. newxpt = xprt->xpt_ops->xpo_accept(xprt);
  660. if (newxpt)
  661. svc_add_new_temp_xprt(serv, newxpt);
  662. else
  663. module_put(xprt->xpt_class->xcl_owner);
  664. } else if (xprt->xpt_ops->xpo_has_wspace(xprt)) {
  665. /* XPT_DATA|XPT_DEFERRED case: */
  666. dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
  667. rqstp, rqstp->rq_pool->sp_id, xprt,
  668. atomic_read(&xprt->xpt_ref.refcount));
  669. rqstp->rq_deferred = svc_deferred_dequeue(xprt);
  670. if (rqstp->rq_deferred)
  671. len = svc_deferred_recv(rqstp);
  672. else
  673. len = xprt->xpt_ops->xpo_recvfrom(rqstp);
  674. dprintk("svc: got len=%d\n", len);
  675. rqstp->rq_reserved = serv->sv_max_mesg;
  676. atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
  677. }
  678. /* clear XPT_BUSY: */
  679. svc_xprt_received(xprt);
  680. return len;
  681. }
  682. /*
  683. * Receive the next request on any transport. This code is carefully
  684. * organised not to touch any cachelines in the shared svc_serv
  685. * structure, only cachelines in the local svc_pool.
  686. */
  687. int svc_recv(struct svc_rqst *rqstp, long timeout)
  688. {
  689. struct svc_xprt *xprt = NULL;
  690. struct svc_serv *serv = rqstp->rq_server;
  691. int len, err;
  692. dprintk("svc: server %p waiting for data (to = %ld)\n",
  693. rqstp, timeout);
  694. if (rqstp->rq_xprt)
  695. printk(KERN_ERR
  696. "svc_recv: service %p, transport not NULL!\n",
  697. rqstp);
  698. if (waitqueue_active(&rqstp->rq_wait))
  699. printk(KERN_ERR
  700. "svc_recv: service %p, wait queue active!\n",
  701. rqstp);
  702. err = svc_alloc_arg(rqstp);
  703. if (err)
  704. return err;
  705. try_to_freeze();
  706. cond_resched();
  707. if (signalled() || kthread_should_stop())
  708. return -EINTR;
  709. xprt = svc_get_next_xprt(rqstp, timeout);
  710. if (IS_ERR(xprt))
  711. return PTR_ERR(xprt);
  712. len = svc_handle_xprt(rqstp, xprt);
  713. /* No data, incomplete (TCP) read, or accept() */
  714. if (len <= 0)
  715. goto out;
  716. clear_bit(XPT_OLD, &xprt->xpt_flags);
  717. rqstp->rq_secure = xprt->xpt_ops->xpo_secure_port(rqstp);
  718. rqstp->rq_chandle.defer = svc_defer;
  719. if (serv->sv_stats)
  720. serv->sv_stats->netcnt++;
  721. return len;
  722. out:
  723. rqstp->rq_res.len = 0;
  724. svc_xprt_release(rqstp);
  725. return -EAGAIN;
  726. }
  727. EXPORT_SYMBOL_GPL(svc_recv);
  728. /*
  729. * Drop request
  730. */
  731. void svc_drop(struct svc_rqst *rqstp)
  732. {
  733. dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
  734. svc_xprt_release(rqstp);
  735. }
  736. EXPORT_SYMBOL_GPL(svc_drop);
  737. /*
  738. * Return reply to client.
  739. */
  740. int svc_send(struct svc_rqst *rqstp)
  741. {
  742. struct svc_xprt *xprt;
  743. int len;
  744. struct xdr_buf *xb;
  745. xprt = rqstp->rq_xprt;
  746. if (!xprt)
  747. return -EFAULT;
  748. /* release the receive skb before sending the reply */
  749. rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
  750. /* calculate over-all length */
  751. xb = &rqstp->rq_res;
  752. xb->len = xb->head[0].iov_len +
  753. xb->page_len +
  754. xb->tail[0].iov_len;
  755. /* Grab mutex to serialize outgoing data. */
  756. mutex_lock(&xprt->xpt_mutex);
  757. if (test_bit(XPT_DEAD, &xprt->xpt_flags)
  758. || test_bit(XPT_CLOSE, &xprt->xpt_flags))
  759. len = -ENOTCONN;
  760. else
  761. len = xprt->xpt_ops->xpo_sendto(rqstp);
  762. mutex_unlock(&xprt->xpt_mutex);
  763. rpc_wake_up(&xprt->xpt_bc_pending);
  764. svc_xprt_release(rqstp);
  765. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  766. return 0;
  767. return len;
  768. }
  769. /*
  770. * Timer function to close old temporary transports, using
  771. * a mark-and-sweep algorithm.
  772. */
  773. static void svc_age_temp_xprts(unsigned long closure)
  774. {
  775. struct svc_serv *serv = (struct svc_serv *)closure;
  776. struct svc_xprt *xprt;
  777. struct list_head *le, *next;
  778. dprintk("svc_age_temp_xprts\n");
  779. if (!spin_trylock_bh(&serv->sv_lock)) {
  780. /* busy, try again 1 sec later */
  781. dprintk("svc_age_temp_xprts: busy\n");
  782. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  783. return;
  784. }
  785. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  786. xprt = list_entry(le, struct svc_xprt, xpt_list);
  787. /* First time through, just mark it OLD. Second time
  788. * through, close it. */
  789. if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
  790. continue;
  791. if (atomic_read(&xprt->xpt_ref.refcount) > 1 ||
  792. test_bit(XPT_BUSY, &xprt->xpt_flags))
  793. continue;
  794. list_del_init(le);
  795. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  796. set_bit(XPT_DETACHED, &xprt->xpt_flags);
  797. dprintk("queuing xprt %p for closing\n", xprt);
  798. /* a thread will dequeue and close it soon */
  799. svc_xprt_enqueue(xprt);
  800. }
  801. spin_unlock_bh(&serv->sv_lock);
  802. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  803. }
  804. static void call_xpt_users(struct svc_xprt *xprt)
  805. {
  806. struct svc_xpt_user *u;
  807. spin_lock(&xprt->xpt_lock);
  808. while (!list_empty(&xprt->xpt_users)) {
  809. u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list);
  810. list_del(&u->list);
  811. u->callback(u);
  812. }
  813. spin_unlock(&xprt->xpt_lock);
  814. }
  815. /*
  816. * Remove a dead transport
  817. */
  818. static void svc_delete_xprt(struct svc_xprt *xprt)
  819. {
  820. struct svc_serv *serv = xprt->xpt_server;
  821. struct svc_deferred_req *dr;
  822. /* Only do this once */
  823. if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
  824. BUG();
  825. dprintk("svc: svc_delete_xprt(%p)\n", xprt);
  826. xprt->xpt_ops->xpo_detach(xprt);
  827. spin_lock_bh(&serv->sv_lock);
  828. if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
  829. list_del_init(&xprt->xpt_list);
  830. WARN_ON_ONCE(!list_empty(&xprt->xpt_ready));
  831. if (test_bit(XPT_TEMP, &xprt->xpt_flags))
  832. serv->sv_tmpcnt--;
  833. spin_unlock_bh(&serv->sv_lock);
  834. while ((dr = svc_deferred_dequeue(xprt)) != NULL)
  835. kfree(dr);
  836. call_xpt_users(xprt);
  837. svc_xprt_put(xprt);
  838. }
  839. void svc_close_xprt(struct svc_xprt *xprt)
  840. {
  841. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  842. if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
  843. /* someone else will have to effect the close */
  844. return;
  845. /*
  846. * We expect svc_close_xprt() to work even when no threads are
  847. * running (e.g., while configuring the server before starting
  848. * any threads), so if the transport isn't busy, we delete
  849. * it ourself:
  850. */
  851. svc_delete_xprt(xprt);
  852. }
  853. EXPORT_SYMBOL_GPL(svc_close_xprt);
  854. static int svc_close_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net)
  855. {
  856. struct svc_xprt *xprt;
  857. int ret = 0;
  858. spin_lock(&serv->sv_lock);
  859. list_for_each_entry(xprt, xprt_list, xpt_list) {
  860. if (xprt->xpt_net != net)
  861. continue;
  862. ret++;
  863. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  864. svc_xprt_enqueue(xprt);
  865. }
  866. spin_unlock(&serv->sv_lock);
  867. return ret;
  868. }
  869. static struct svc_xprt *svc_dequeue_net(struct svc_serv *serv, struct net *net)
  870. {
  871. struct svc_pool *pool;
  872. struct svc_xprt *xprt;
  873. struct svc_xprt *tmp;
  874. int i;
  875. for (i = 0; i < serv->sv_nrpools; i++) {
  876. pool = &serv->sv_pools[i];
  877. spin_lock_bh(&pool->sp_lock);
  878. list_for_each_entry_safe(xprt, tmp, &pool->sp_sockets, xpt_ready) {
  879. if (xprt->xpt_net != net)
  880. continue;
  881. list_del_init(&xprt->xpt_ready);
  882. spin_unlock_bh(&pool->sp_lock);
  883. return xprt;
  884. }
  885. spin_unlock_bh(&pool->sp_lock);
  886. }
  887. return NULL;
  888. }
  889. static void svc_clean_up_xprts(struct svc_serv *serv, struct net *net)
  890. {
  891. struct svc_xprt *xprt;
  892. while ((xprt = svc_dequeue_net(serv, net))) {
  893. set_bit(XPT_CLOSE, &xprt->xpt_flags);
  894. svc_delete_xprt(xprt);
  895. }
  896. }
  897. /*
  898. * Server threads may still be running (especially in the case where the
  899. * service is still running in other network namespaces).
  900. *
  901. * So we shut down sockets the same way we would on a running server, by
  902. * setting XPT_CLOSE, enqueuing, and letting a thread pick it up to do
  903. * the close. In the case there are no such other threads,
  904. * threads running, svc_clean_up_xprts() does a simple version of a
  905. * server's main event loop, and in the case where there are other
  906. * threads, we may need to wait a little while and then check again to
  907. * see if they're done.
  908. */
  909. void svc_close_net(struct svc_serv *serv, struct net *net)
  910. {
  911. int delay = 0;
  912. while (svc_close_list(serv, &serv->sv_permsocks, net) +
  913. svc_close_list(serv, &serv->sv_tempsocks, net)) {
  914. svc_clean_up_xprts(serv, net);
  915. msleep(delay++);
  916. }
  917. }
  918. /*
  919. * Handle defer and revisit of requests
  920. */
  921. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  922. {
  923. struct svc_deferred_req *dr =
  924. container_of(dreq, struct svc_deferred_req, handle);
  925. struct svc_xprt *xprt = dr->xprt;
  926. spin_lock(&xprt->xpt_lock);
  927. set_bit(XPT_DEFERRED, &xprt->xpt_flags);
  928. if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
  929. spin_unlock(&xprt->xpt_lock);
  930. dprintk("revisit canceled\n");
  931. svc_xprt_put(xprt);
  932. kfree(dr);
  933. return;
  934. }
  935. dprintk("revisit queued\n");
  936. dr->xprt = NULL;
  937. list_add(&dr->handle.recent, &xprt->xpt_deferred);
  938. spin_unlock(&xprt->xpt_lock);
  939. svc_xprt_enqueue(xprt);
  940. svc_xprt_put(xprt);
  941. }
  942. /*
  943. * Save the request off for later processing. The request buffer looks
  944. * like this:
  945. *
  946. * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
  947. *
  948. * This code can only handle requests that consist of an xprt-header
  949. * and rpc-header.
  950. */
  951. static struct cache_deferred_req *svc_defer(struct cache_req *req)
  952. {
  953. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  954. struct svc_deferred_req *dr;
  955. if (rqstp->rq_arg.page_len || !rqstp->rq_usedeferral)
  956. return NULL; /* if more than a page, give up FIXME */
  957. if (rqstp->rq_deferred) {
  958. dr = rqstp->rq_deferred;
  959. rqstp->rq_deferred = NULL;
  960. } else {
  961. size_t skip;
  962. size_t size;
  963. /* FIXME maybe discard if size too large */
  964. size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
  965. dr = kmalloc(size, GFP_KERNEL);
  966. if (dr == NULL)
  967. return NULL;
  968. dr->handle.owner = rqstp->rq_server;
  969. dr->prot = rqstp->rq_prot;
  970. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  971. dr->addrlen = rqstp->rq_addrlen;
  972. dr->daddr = rqstp->rq_daddr;
  973. dr->argslen = rqstp->rq_arg.len >> 2;
  974. dr->xprt_hlen = rqstp->rq_xprt_hlen;
  975. /* back up head to the start of the buffer and copy */
  976. skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  977. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
  978. dr->argslen << 2);
  979. }
  980. svc_xprt_get(rqstp->rq_xprt);
  981. dr->xprt = rqstp->rq_xprt;
  982. rqstp->rq_dropme = true;
  983. dr->handle.revisit = svc_revisit;
  984. return &dr->handle;
  985. }
  986. /*
  987. * recv data from a deferred request into an active one
  988. */
  989. static int svc_deferred_recv(struct svc_rqst *rqstp)
  990. {
  991. struct svc_deferred_req *dr = rqstp->rq_deferred;
  992. /* setup iov_base past transport header */
  993. rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
  994. /* The iov_len does not include the transport header bytes */
  995. rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
  996. rqstp->rq_arg.page_len = 0;
  997. /* The rq_arg.len includes the transport header bytes */
  998. rqstp->rq_arg.len = dr->argslen<<2;
  999. rqstp->rq_prot = dr->prot;
  1000. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  1001. rqstp->rq_addrlen = dr->addrlen;
  1002. /* Save off transport header len in case we get deferred again */
  1003. rqstp->rq_xprt_hlen = dr->xprt_hlen;
  1004. rqstp->rq_daddr = dr->daddr;
  1005. rqstp->rq_respages = rqstp->rq_pages;
  1006. return (dr->argslen<<2) - dr->xprt_hlen;
  1007. }
  1008. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
  1009. {
  1010. struct svc_deferred_req *dr = NULL;
  1011. if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
  1012. return NULL;
  1013. spin_lock(&xprt->xpt_lock);
  1014. if (!list_empty(&xprt->xpt_deferred)) {
  1015. dr = list_entry(xprt->xpt_deferred.next,
  1016. struct svc_deferred_req,
  1017. handle.recent);
  1018. list_del_init(&dr->handle.recent);
  1019. } else
  1020. clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
  1021. spin_unlock(&xprt->xpt_lock);
  1022. return dr;
  1023. }
  1024. /**
  1025. * svc_find_xprt - find an RPC transport instance
  1026. * @serv: pointer to svc_serv to search
  1027. * @xcl_name: C string containing transport's class name
  1028. * @net: owner net pointer
  1029. * @af: Address family of transport's local address
  1030. * @port: transport's IP port number
  1031. *
  1032. * Return the transport instance pointer for the endpoint accepting
  1033. * connections/peer traffic from the specified transport class,
  1034. * address family and port.
  1035. *
  1036. * Specifying 0 for the address family or port is effectively a
  1037. * wild-card, and will result in matching the first transport in the
  1038. * service's list that has a matching class name.
  1039. */
  1040. struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
  1041. struct net *net, const sa_family_t af,
  1042. const unsigned short port)
  1043. {
  1044. struct svc_xprt *xprt;
  1045. struct svc_xprt *found = NULL;
  1046. /* Sanity check the args */
  1047. if (serv == NULL || xcl_name == NULL)
  1048. return found;
  1049. spin_lock_bh(&serv->sv_lock);
  1050. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1051. if (xprt->xpt_net != net)
  1052. continue;
  1053. if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
  1054. continue;
  1055. if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
  1056. continue;
  1057. if (port != 0 && port != svc_xprt_local_port(xprt))
  1058. continue;
  1059. found = xprt;
  1060. svc_xprt_get(xprt);
  1061. break;
  1062. }
  1063. spin_unlock_bh(&serv->sv_lock);
  1064. return found;
  1065. }
  1066. EXPORT_SYMBOL_GPL(svc_find_xprt);
  1067. static int svc_one_xprt_name(const struct svc_xprt *xprt,
  1068. char *pos, int remaining)
  1069. {
  1070. int len;
  1071. len = snprintf(pos, remaining, "%s %u\n",
  1072. xprt->xpt_class->xcl_name,
  1073. svc_xprt_local_port(xprt));
  1074. if (len >= remaining)
  1075. return -ENAMETOOLONG;
  1076. return len;
  1077. }
  1078. /**
  1079. * svc_xprt_names - format a buffer with a list of transport names
  1080. * @serv: pointer to an RPC service
  1081. * @buf: pointer to a buffer to be filled in
  1082. * @buflen: length of buffer to be filled in
  1083. *
  1084. * Fills in @buf with a string containing a list of transport names,
  1085. * each name terminated with '\n'.
  1086. *
  1087. * Returns positive length of the filled-in string on success; otherwise
  1088. * a negative errno value is returned if an error occurs.
  1089. */
  1090. int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
  1091. {
  1092. struct svc_xprt *xprt;
  1093. int len, totlen;
  1094. char *pos;
  1095. /* Sanity check args */
  1096. if (!serv)
  1097. return 0;
  1098. spin_lock_bh(&serv->sv_lock);
  1099. pos = buf;
  1100. totlen = 0;
  1101. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1102. len = svc_one_xprt_name(xprt, pos, buflen - totlen);
  1103. if (len < 0) {
  1104. *buf = '\0';
  1105. totlen = len;
  1106. }
  1107. if (len <= 0)
  1108. break;
  1109. pos += len;
  1110. totlen += len;
  1111. }
  1112. spin_unlock_bh(&serv->sv_lock);
  1113. return totlen;
  1114. }
  1115. EXPORT_SYMBOL_GPL(svc_xprt_names);
  1116. /*----------------------------------------------------------------------------*/
  1117. static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
  1118. {
  1119. unsigned int pidx = (unsigned int)*pos;
  1120. struct svc_serv *serv = m->private;
  1121. dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
  1122. if (!pidx)
  1123. return SEQ_START_TOKEN;
  1124. return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]);
  1125. }
  1126. static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
  1127. {
  1128. struct svc_pool *pool = p;
  1129. struct svc_serv *serv = m->private;
  1130. dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
  1131. if (p == SEQ_START_TOKEN) {
  1132. pool = &serv->sv_pools[0];
  1133. } else {
  1134. unsigned int pidx = (pool - &serv->sv_pools[0]);
  1135. if (pidx < serv->sv_nrpools-1)
  1136. pool = &serv->sv_pools[pidx+1];
  1137. else
  1138. pool = NULL;
  1139. }
  1140. ++*pos;
  1141. return pool;
  1142. }
  1143. static void svc_pool_stats_stop(struct seq_file *m, void *p)
  1144. {
  1145. }
  1146. static int svc_pool_stats_show(struct seq_file *m, void *p)
  1147. {
  1148. struct svc_pool *pool = p;
  1149. if (p == SEQ_START_TOKEN) {
  1150. seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n");
  1151. return 0;
  1152. }
  1153. seq_printf(m, "%u %lu %lu %lu %lu\n",
  1154. pool->sp_id,
  1155. pool->sp_stats.packets,
  1156. pool->sp_stats.sockets_queued,
  1157. pool->sp_stats.threads_woken,
  1158. pool->sp_stats.threads_timedout);
  1159. return 0;
  1160. }
  1161. static const struct seq_operations svc_pool_stats_seq_ops = {
  1162. .start = svc_pool_stats_start,
  1163. .next = svc_pool_stats_next,
  1164. .stop = svc_pool_stats_stop,
  1165. .show = svc_pool_stats_show,
  1166. };
  1167. int svc_pool_stats_open(struct svc_serv *serv, struct file *file)
  1168. {
  1169. int err;
  1170. err = seq_open(file, &svc_pool_stats_seq_ops);
  1171. if (!err)
  1172. ((struct seq_file *) file->private_data)->private = serv;
  1173. return err;
  1174. }
  1175. EXPORT_SYMBOL(svc_pool_stats_open);
  1176. /*----------------------------------------------------------------------------*/