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