svc_xprt.c 37 KB

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