svc_xprt.c 33 KB

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