svc_xprt.c 38 KB

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