messenger.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/crc32c.h>
  4. #include <linux/ctype.h>
  5. #include <linux/highmem.h>
  6. #include <linux/inet.h>
  7. #include <linux/kthread.h>
  8. #include <linux/net.h>
  9. #include <linux/nsproxy.h>
  10. #include <linux/sched/mm.h>
  11. #include <linux/slab.h>
  12. #include <linux/socket.h>
  13. #include <linux/string.h>
  14. #ifdef CONFIG_BLOCK
  15. #include <linux/bio.h>
  16. #endif /* CONFIG_BLOCK */
  17. #include <linux/dns_resolver.h>
  18. #include <net/tcp.h>
  19. #include <trace/events/sock.h>
  20. #include <linux/ceph/ceph_features.h>
  21. #include <linux/ceph/libceph.h>
  22. #include <linux/ceph/messenger.h>
  23. #include <linux/ceph/decode.h>
  24. #include <linux/ceph/pagelist.h>
  25. #include <linux/export.h>
  26. /*
  27. * Ceph uses the messenger to exchange ceph_msg messages with other
  28. * hosts in the system. The messenger provides ordered and reliable
  29. * delivery. We tolerate TCP disconnects by reconnecting (with
  30. * exponential backoff) in the case of a fault (disconnection, bad
  31. * crc, protocol error). Acks allow sent messages to be discarded by
  32. * the sender.
  33. */
  34. /*
  35. * We track the state of the socket on a given connection using
  36. * values defined below. The transition to a new socket state is
  37. * handled by a function which verifies we aren't coming from an
  38. * unexpected state.
  39. *
  40. * --------
  41. * | NEW* | transient initial state
  42. * --------
  43. * | con_sock_state_init()
  44. * v
  45. * ----------
  46. * | CLOSED | initialized, but no socket (and no
  47. * ---------- TCP connection)
  48. * ^ \
  49. * | \ con_sock_state_connecting()
  50. * | ----------------------
  51. * | \
  52. * + con_sock_state_closed() \
  53. * |+--------------------------- \
  54. * | \ \ \
  55. * | ----------- \ \
  56. * | | CLOSING | socket event; \ \
  57. * | ----------- await close \ \
  58. * | ^ \ |
  59. * | | \ |
  60. * | + con_sock_state_closing() \ |
  61. * | / \ | |
  62. * | / --------------- | |
  63. * | / \ v v
  64. * | / --------------
  65. * | / -----------------| CONNECTING | socket created, TCP
  66. * | | / -------------- connect initiated
  67. * | | | con_sock_state_connected()
  68. * | | v
  69. * -------------
  70. * | CONNECTED | TCP connection established
  71. * -------------
  72. *
  73. * State values for ceph_connection->sock_state; NEW is assumed to be 0.
  74. */
  75. #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
  76. #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
  77. #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
  78. #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
  79. #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
  80. static bool con_flag_valid(unsigned long con_flag)
  81. {
  82. switch (con_flag) {
  83. case CEPH_CON_F_LOSSYTX:
  84. case CEPH_CON_F_KEEPALIVE_PENDING:
  85. case CEPH_CON_F_WRITE_PENDING:
  86. case CEPH_CON_F_SOCK_CLOSED:
  87. case CEPH_CON_F_BACKOFF:
  88. return true;
  89. default:
  90. return false;
  91. }
  92. }
  93. void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
  94. {
  95. BUG_ON(!con_flag_valid(con_flag));
  96. clear_bit(con_flag, &con->flags);
  97. }
  98. void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag)
  99. {
  100. BUG_ON(!con_flag_valid(con_flag));
  101. set_bit(con_flag, &con->flags);
  102. }
  103. bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag)
  104. {
  105. BUG_ON(!con_flag_valid(con_flag));
  106. return test_bit(con_flag, &con->flags);
  107. }
  108. bool ceph_con_flag_test_and_clear(struct ceph_connection *con,
  109. unsigned long con_flag)
  110. {
  111. BUG_ON(!con_flag_valid(con_flag));
  112. return test_and_clear_bit(con_flag, &con->flags);
  113. }
  114. bool ceph_con_flag_test_and_set(struct ceph_connection *con,
  115. unsigned long con_flag)
  116. {
  117. BUG_ON(!con_flag_valid(con_flag));
  118. return test_and_set_bit(con_flag, &con->flags);
  119. }
  120. /* Slab caches for frequently-allocated structures */
  121. static struct kmem_cache *ceph_msg_cache;
  122. #ifdef CONFIG_LOCKDEP
  123. static struct lock_class_key socket_class;
  124. #endif
  125. static void queue_con(struct ceph_connection *con);
  126. static void cancel_con(struct ceph_connection *con);
  127. static void ceph_con_workfn(struct work_struct *);
  128. static void con_fault(struct ceph_connection *con);
  129. /*
  130. * Nicely render a sockaddr as a string. An array of formatted
  131. * strings is used, to approximate reentrancy.
  132. */
  133. #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
  134. #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
  135. #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
  136. #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
  137. static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
  138. static atomic_t addr_str_seq = ATOMIC_INIT(0);
  139. struct page *ceph_zero_page; /* used in certain error cases */
  140. const char *ceph_pr_addr(const struct ceph_entity_addr *addr)
  141. {
  142. int i;
  143. char *s;
  144. struct sockaddr_storage ss = addr->in_addr; /* align */
  145. struct sockaddr_in *in4 = (struct sockaddr_in *)&ss;
  146. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&ss;
  147. i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
  148. s = addr_str[i];
  149. switch (ss.ss_family) {
  150. case AF_INET:
  151. snprintf(s, MAX_ADDR_STR_LEN, "(%d)%pI4:%hu",
  152. le32_to_cpu(addr->type), &in4->sin_addr,
  153. ntohs(in4->sin_port));
  154. break;
  155. case AF_INET6:
  156. snprintf(s, MAX_ADDR_STR_LEN, "(%d)[%pI6c]:%hu",
  157. le32_to_cpu(addr->type), &in6->sin6_addr,
  158. ntohs(in6->sin6_port));
  159. break;
  160. default:
  161. snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
  162. ss.ss_family);
  163. }
  164. return s;
  165. }
  166. EXPORT_SYMBOL(ceph_pr_addr);
  167. void ceph_encode_my_addr(struct ceph_messenger *msgr)
  168. {
  169. if (!ceph_msgr2(from_msgr(msgr))) {
  170. memcpy(&msgr->my_enc_addr, &msgr->inst.addr,
  171. sizeof(msgr->my_enc_addr));
  172. ceph_encode_banner_addr(&msgr->my_enc_addr);
  173. }
  174. }
  175. /*
  176. * work queue for all reading and writing to/from the socket.
  177. */
  178. static struct workqueue_struct *ceph_msgr_wq;
  179. static int ceph_msgr_slab_init(void)
  180. {
  181. BUG_ON(ceph_msg_cache);
  182. ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
  183. if (!ceph_msg_cache)
  184. return -ENOMEM;
  185. return 0;
  186. }
  187. static void ceph_msgr_slab_exit(void)
  188. {
  189. BUG_ON(!ceph_msg_cache);
  190. kmem_cache_destroy(ceph_msg_cache);
  191. ceph_msg_cache = NULL;
  192. }
  193. static void _ceph_msgr_exit(void)
  194. {
  195. if (ceph_msgr_wq) {
  196. destroy_workqueue(ceph_msgr_wq);
  197. ceph_msgr_wq = NULL;
  198. }
  199. BUG_ON(!ceph_zero_page);
  200. put_page(ceph_zero_page);
  201. ceph_zero_page = NULL;
  202. ceph_msgr_slab_exit();
  203. }
  204. int __init ceph_msgr_init(void)
  205. {
  206. if (ceph_msgr_slab_init())
  207. return -ENOMEM;
  208. BUG_ON(ceph_zero_page);
  209. ceph_zero_page = ZERO_PAGE(0);
  210. get_page(ceph_zero_page);
  211. /*
  212. * The number of active work items is limited by the number of
  213. * connections, so leave @max_active at default.
  214. */
  215. ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
  216. if (ceph_msgr_wq)
  217. return 0;
  218. pr_err("msgr_init failed to create workqueue\n");
  219. _ceph_msgr_exit();
  220. return -ENOMEM;
  221. }
  222. void ceph_msgr_exit(void)
  223. {
  224. BUG_ON(ceph_msgr_wq == NULL);
  225. _ceph_msgr_exit();
  226. }
  227. void ceph_msgr_flush(void)
  228. {
  229. flush_workqueue(ceph_msgr_wq);
  230. }
  231. EXPORT_SYMBOL(ceph_msgr_flush);
  232. /* Connection socket state transition functions */
  233. static void con_sock_state_init(struct ceph_connection *con)
  234. {
  235. int old_state;
  236. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
  237. if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
  238. printk("%s: unexpected old state %d\n", __func__, old_state);
  239. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  240. CON_SOCK_STATE_CLOSED);
  241. }
  242. static void con_sock_state_connecting(struct ceph_connection *con)
  243. {
  244. int old_state;
  245. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
  246. if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
  247. printk("%s: unexpected old state %d\n", __func__, old_state);
  248. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  249. CON_SOCK_STATE_CONNECTING);
  250. }
  251. static void con_sock_state_connected(struct ceph_connection *con)
  252. {
  253. int old_state;
  254. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
  255. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
  256. printk("%s: unexpected old state %d\n", __func__, old_state);
  257. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  258. CON_SOCK_STATE_CONNECTED);
  259. }
  260. static void con_sock_state_closing(struct ceph_connection *con)
  261. {
  262. int old_state;
  263. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
  264. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
  265. old_state != CON_SOCK_STATE_CONNECTED &&
  266. old_state != CON_SOCK_STATE_CLOSING))
  267. printk("%s: unexpected old state %d\n", __func__, old_state);
  268. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  269. CON_SOCK_STATE_CLOSING);
  270. }
  271. static void con_sock_state_closed(struct ceph_connection *con)
  272. {
  273. int old_state;
  274. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
  275. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
  276. old_state != CON_SOCK_STATE_CLOSING &&
  277. old_state != CON_SOCK_STATE_CONNECTING &&
  278. old_state != CON_SOCK_STATE_CLOSED))
  279. printk("%s: unexpected old state %d\n", __func__, old_state);
  280. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  281. CON_SOCK_STATE_CLOSED);
  282. }
  283. /*
  284. * socket callback functions
  285. */
  286. /* data available on socket, or listen socket received a connect */
  287. static void ceph_sock_data_ready(struct sock *sk)
  288. {
  289. struct ceph_connection *con = sk->sk_user_data;
  290. trace_sk_data_ready(sk);
  291. if (atomic_read(&con->msgr->stopping)) {
  292. return;
  293. }
  294. if (sk->sk_state != TCP_CLOSE_WAIT) {
  295. dout("%s %p state = %d, queueing work\n", __func__,
  296. con, con->state);
  297. queue_con(con);
  298. }
  299. }
  300. /* socket has buffer space for writing */
  301. static void ceph_sock_write_space(struct sock *sk)
  302. {
  303. struct ceph_connection *con = sk->sk_user_data;
  304. /* only queue to workqueue if there is data we want to write,
  305. * and there is sufficient space in the socket buffer to accept
  306. * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
  307. * doesn't get called again until try_write() fills the socket
  308. * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
  309. * and net/core/stream.c:sk_stream_write_space().
  310. */
  311. if (ceph_con_flag_test(con, CEPH_CON_F_WRITE_PENDING)) {
  312. if (sk_stream_is_writeable(sk)) {
  313. dout("%s %p queueing write work\n", __func__, con);
  314. clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  315. queue_con(con);
  316. }
  317. } else {
  318. dout("%s %p nothing to write\n", __func__, con);
  319. }
  320. }
  321. /* socket's state has changed */
  322. static void ceph_sock_state_change(struct sock *sk)
  323. {
  324. struct ceph_connection *con = sk->sk_user_data;
  325. dout("%s %p state = %d sk_state = %u\n", __func__,
  326. con, con->state, sk->sk_state);
  327. switch (sk->sk_state) {
  328. case TCP_CLOSE:
  329. dout("%s TCP_CLOSE\n", __func__);
  330. fallthrough;
  331. case TCP_CLOSE_WAIT:
  332. dout("%s TCP_CLOSE_WAIT\n", __func__);
  333. con_sock_state_closing(con);
  334. ceph_con_flag_set(con, CEPH_CON_F_SOCK_CLOSED);
  335. queue_con(con);
  336. break;
  337. case TCP_ESTABLISHED:
  338. dout("%s TCP_ESTABLISHED\n", __func__);
  339. con_sock_state_connected(con);
  340. queue_con(con);
  341. break;
  342. default: /* Everything else is uninteresting */
  343. break;
  344. }
  345. }
  346. /*
  347. * set up socket callbacks
  348. */
  349. static void set_sock_callbacks(struct socket *sock,
  350. struct ceph_connection *con)
  351. {
  352. struct sock *sk = sock->sk;
  353. sk->sk_user_data = con;
  354. sk->sk_data_ready = ceph_sock_data_ready;
  355. sk->sk_write_space = ceph_sock_write_space;
  356. sk->sk_state_change = ceph_sock_state_change;
  357. }
  358. /*
  359. * socket helpers
  360. */
  361. /*
  362. * initiate connection to a remote socket.
  363. */
  364. int ceph_tcp_connect(struct ceph_connection *con)
  365. {
  366. struct sockaddr_storage ss = con->peer_addr.in_addr; /* align */
  367. struct socket *sock;
  368. unsigned int noio_flag;
  369. int ret;
  370. dout("%s con %p peer_addr %s\n", __func__, con,
  371. ceph_pr_addr(&con->peer_addr));
  372. BUG_ON(con->sock);
  373. /* sock_create_kern() allocates with GFP_KERNEL */
  374. noio_flag = memalloc_noio_save();
  375. ret = sock_create_kern(read_pnet(&con->msgr->net), ss.ss_family,
  376. SOCK_STREAM, IPPROTO_TCP, &sock);
  377. memalloc_noio_restore(noio_flag);
  378. if (ret)
  379. return ret;
  380. sock->sk->sk_allocation = GFP_NOFS;
  381. sock->sk->sk_use_task_frag = false;
  382. #ifdef CONFIG_LOCKDEP
  383. lockdep_set_class(&sock->sk->sk_lock, &socket_class);
  384. #endif
  385. set_sock_callbacks(sock, con);
  386. con_sock_state_connecting(con);
  387. ret = kernel_connect(sock, (struct sockaddr *)&ss, sizeof(ss),
  388. O_NONBLOCK);
  389. if (ret == -EINPROGRESS) {
  390. dout("connect %s EINPROGRESS sk_state = %u\n",
  391. ceph_pr_addr(&con->peer_addr),
  392. sock->sk->sk_state);
  393. } else if (ret < 0) {
  394. pr_err("connect %s error %d\n",
  395. ceph_pr_addr(&con->peer_addr), ret);
  396. sock_release(sock);
  397. return ret;
  398. }
  399. if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY))
  400. tcp_sock_set_nodelay(sock->sk);
  401. con->sock = sock;
  402. return 0;
  403. }
  404. /*
  405. * Shutdown/close the socket for the given connection.
  406. */
  407. int ceph_con_close_socket(struct ceph_connection *con)
  408. {
  409. int rc = 0;
  410. dout("%s con %p sock %p\n", __func__, con, con->sock);
  411. if (con->sock) {
  412. rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
  413. sock_release(con->sock);
  414. con->sock = NULL;
  415. }
  416. /*
  417. * Forcibly clear the SOCK_CLOSED flag. It gets set
  418. * independent of the connection mutex, and we could have
  419. * received a socket close event before we had the chance to
  420. * shut the socket down.
  421. */
  422. ceph_con_flag_clear(con, CEPH_CON_F_SOCK_CLOSED);
  423. con_sock_state_closed(con);
  424. return rc;
  425. }
  426. static void ceph_con_reset_protocol(struct ceph_connection *con)
  427. {
  428. dout("%s con %p\n", __func__, con);
  429. ceph_con_close_socket(con);
  430. if (con->in_msg) {
  431. WARN_ON(con->in_msg->con != con);
  432. ceph_msg_put(con->in_msg);
  433. con->in_msg = NULL;
  434. }
  435. if (con->out_msg) {
  436. WARN_ON(con->out_msg->con != con);
  437. ceph_msg_put(con->out_msg);
  438. con->out_msg = NULL;
  439. }
  440. if (con->bounce_page) {
  441. __free_page(con->bounce_page);
  442. con->bounce_page = NULL;
  443. }
  444. if (ceph_msgr2(from_msgr(con->msgr)))
  445. ceph_con_v2_reset_protocol(con);
  446. else
  447. ceph_con_v1_reset_protocol(con);
  448. }
  449. /*
  450. * Reset a connection. Discard all incoming and outgoing messages
  451. * and clear *_seq state.
  452. */
  453. static void ceph_msg_remove(struct ceph_msg *msg)
  454. {
  455. list_del_init(&msg->list_head);
  456. ceph_msg_put(msg);
  457. }
  458. static void ceph_msg_remove_list(struct list_head *head)
  459. {
  460. while (!list_empty(head)) {
  461. struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
  462. list_head);
  463. ceph_msg_remove(msg);
  464. }
  465. }
  466. void ceph_con_reset_session(struct ceph_connection *con)
  467. {
  468. dout("%s con %p\n", __func__, con);
  469. WARN_ON(con->in_msg);
  470. WARN_ON(con->out_msg);
  471. ceph_msg_remove_list(&con->out_queue);
  472. ceph_msg_remove_list(&con->out_sent);
  473. con->out_seq = 0;
  474. con->in_seq = 0;
  475. con->in_seq_acked = 0;
  476. if (ceph_msgr2(from_msgr(con->msgr)))
  477. ceph_con_v2_reset_session(con);
  478. else
  479. ceph_con_v1_reset_session(con);
  480. }
  481. /*
  482. * mark a peer down. drop any open connections.
  483. */
  484. void ceph_con_close(struct ceph_connection *con)
  485. {
  486. mutex_lock(&con->mutex);
  487. dout("con_close %p peer %s\n", con, ceph_pr_addr(&con->peer_addr));
  488. con->state = CEPH_CON_S_CLOSED;
  489. ceph_con_flag_clear(con, CEPH_CON_F_LOSSYTX); /* so we retry next
  490. connect */
  491. ceph_con_flag_clear(con, CEPH_CON_F_KEEPALIVE_PENDING);
  492. ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
  493. ceph_con_flag_clear(con, CEPH_CON_F_BACKOFF);
  494. ceph_con_reset_protocol(con);
  495. ceph_con_reset_session(con);
  496. cancel_con(con);
  497. mutex_unlock(&con->mutex);
  498. }
  499. EXPORT_SYMBOL(ceph_con_close);
  500. /*
  501. * Reopen a closed connection, with a new peer address.
  502. */
  503. void ceph_con_open(struct ceph_connection *con,
  504. __u8 entity_type, __u64 entity_num,
  505. struct ceph_entity_addr *addr)
  506. {
  507. mutex_lock(&con->mutex);
  508. dout("con_open %p %s\n", con, ceph_pr_addr(addr));
  509. WARN_ON(con->state != CEPH_CON_S_CLOSED);
  510. con->state = CEPH_CON_S_PREOPEN;
  511. con->peer_name.type = (__u8) entity_type;
  512. con->peer_name.num = cpu_to_le64(entity_num);
  513. memcpy(&con->peer_addr, addr, sizeof(*addr));
  514. con->delay = 0; /* reset backoff memory */
  515. mutex_unlock(&con->mutex);
  516. queue_con(con);
  517. }
  518. EXPORT_SYMBOL(ceph_con_open);
  519. /*
  520. * return true if this connection ever successfully opened
  521. */
  522. bool ceph_con_opened(struct ceph_connection *con)
  523. {
  524. if (ceph_msgr2(from_msgr(con->msgr)))
  525. return ceph_con_v2_opened(con);
  526. return ceph_con_v1_opened(con);
  527. }
  528. /*
  529. * initialize a new connection.
  530. */
  531. void ceph_con_init(struct ceph_connection *con, void *private,
  532. const struct ceph_connection_operations *ops,
  533. struct ceph_messenger *msgr)
  534. {
  535. dout("con_init %p\n", con);
  536. memset(con, 0, sizeof(*con));
  537. con->private = private;
  538. con->ops = ops;
  539. con->msgr = msgr;
  540. con_sock_state_init(con);
  541. mutex_init(&con->mutex);
  542. INIT_LIST_HEAD(&con->out_queue);
  543. INIT_LIST_HEAD(&con->out_sent);
  544. INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
  545. con->state = CEPH_CON_S_CLOSED;
  546. }
  547. EXPORT_SYMBOL(ceph_con_init);
  548. /*
  549. * We maintain a global counter to order connection attempts. Get
  550. * a unique seq greater than @gt.
  551. */
  552. u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt)
  553. {
  554. u32 ret;
  555. spin_lock(&msgr->global_seq_lock);
  556. if (msgr->global_seq < gt)
  557. msgr->global_seq = gt;
  558. ret = ++msgr->global_seq;
  559. spin_unlock(&msgr->global_seq_lock);
  560. return ret;
  561. }
  562. /*
  563. * Discard messages that have been acked by the server.
  564. */
  565. void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq)
  566. {
  567. struct ceph_msg *msg;
  568. u64 seq;
  569. dout("%s con %p ack_seq %llu\n", __func__, con, ack_seq);
  570. while (!list_empty(&con->out_sent)) {
  571. msg = list_first_entry(&con->out_sent, struct ceph_msg,
  572. list_head);
  573. WARN_ON(msg->needs_out_seq);
  574. seq = le64_to_cpu(msg->hdr.seq);
  575. if (seq > ack_seq)
  576. break;
  577. dout("%s con %p discarding msg %p seq %llu\n", __func__, con,
  578. msg, seq);
  579. ceph_msg_remove(msg);
  580. }
  581. }
  582. /*
  583. * Discard messages that have been requeued in con_fault(), up to
  584. * reconnect_seq. This avoids gratuitously resending messages that
  585. * the server had received and handled prior to reconnect.
  586. */
  587. void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq)
  588. {
  589. struct ceph_msg *msg;
  590. u64 seq;
  591. dout("%s con %p reconnect_seq %llu\n", __func__, con, reconnect_seq);
  592. while (!list_empty(&con->out_queue)) {
  593. msg = list_first_entry(&con->out_queue, struct ceph_msg,
  594. list_head);
  595. if (msg->needs_out_seq)
  596. break;
  597. seq = le64_to_cpu(msg->hdr.seq);
  598. if (seq > reconnect_seq)
  599. break;
  600. dout("%s con %p discarding msg %p seq %llu\n", __func__, con,
  601. msg, seq);
  602. ceph_msg_remove(msg);
  603. }
  604. }
  605. #ifdef CONFIG_BLOCK
  606. /*
  607. * For a bio data item, a piece is whatever remains of the next
  608. * entry in the current bio iovec, or the first entry in the next
  609. * bio in the list.
  610. */
  611. static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
  612. size_t length)
  613. {
  614. struct ceph_msg_data *data = cursor->data;
  615. struct ceph_bio_iter *it = &cursor->bio_iter;
  616. cursor->resid = min_t(size_t, length, data->bio_length);
  617. *it = data->bio_pos;
  618. if (cursor->resid < it->iter.bi_size)
  619. it->iter.bi_size = cursor->resid;
  620. BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
  621. }
  622. static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
  623. size_t *page_offset,
  624. size_t *length)
  625. {
  626. struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
  627. cursor->bio_iter.iter);
  628. *page_offset = bv.bv_offset;
  629. *length = bv.bv_len;
  630. return bv.bv_page;
  631. }
  632. static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
  633. size_t bytes)
  634. {
  635. struct ceph_bio_iter *it = &cursor->bio_iter;
  636. struct page *page = bio_iter_page(it->bio, it->iter);
  637. BUG_ON(bytes > cursor->resid);
  638. BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
  639. cursor->resid -= bytes;
  640. bio_advance_iter(it->bio, &it->iter, bytes);
  641. if (!cursor->resid)
  642. return false; /* no more data */
  643. if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done &&
  644. page == bio_iter_page(it->bio, it->iter)))
  645. return false; /* more bytes to process in this segment */
  646. if (!it->iter.bi_size) {
  647. it->bio = it->bio->bi_next;
  648. it->iter = it->bio->bi_iter;
  649. if (cursor->resid < it->iter.bi_size)
  650. it->iter.bi_size = cursor->resid;
  651. }
  652. BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
  653. return true;
  654. }
  655. #endif /* CONFIG_BLOCK */
  656. static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
  657. size_t length)
  658. {
  659. struct ceph_msg_data *data = cursor->data;
  660. struct bio_vec *bvecs = data->bvec_pos.bvecs;
  661. cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
  662. cursor->bvec_iter = data->bvec_pos.iter;
  663. cursor->bvec_iter.bi_size = cursor->resid;
  664. BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
  665. }
  666. static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
  667. size_t *page_offset,
  668. size_t *length)
  669. {
  670. struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
  671. cursor->bvec_iter);
  672. *page_offset = bv.bv_offset;
  673. *length = bv.bv_len;
  674. return bv.bv_page;
  675. }
  676. static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
  677. size_t bytes)
  678. {
  679. struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
  680. struct page *page = bvec_iter_page(bvecs, cursor->bvec_iter);
  681. BUG_ON(bytes > cursor->resid);
  682. BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
  683. cursor->resid -= bytes;
  684. bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
  685. if (!cursor->resid)
  686. return false; /* no more data */
  687. if (!bytes || (cursor->bvec_iter.bi_bvec_done &&
  688. page == bvec_iter_page(bvecs, cursor->bvec_iter)))
  689. return false; /* more bytes to process in this segment */
  690. BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
  691. return true;
  692. }
  693. /*
  694. * For a page array, a piece comes from the first page in the array
  695. * that has not already been fully consumed.
  696. */
  697. static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
  698. size_t length)
  699. {
  700. struct ceph_msg_data *data = cursor->data;
  701. int page_count;
  702. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  703. BUG_ON(!data->pages);
  704. BUG_ON(!data->length);
  705. cursor->resid = min(length, data->length);
  706. page_count = calc_pages_for(data->alignment, (u64)data->length);
  707. cursor->page_offset = data->alignment & ~PAGE_MASK;
  708. cursor->page_index = 0;
  709. BUG_ON(page_count > (int)USHRT_MAX);
  710. cursor->page_count = (unsigned short)page_count;
  711. BUG_ON(length > SIZE_MAX - cursor->page_offset);
  712. }
  713. static struct page *
  714. ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
  715. size_t *page_offset, size_t *length)
  716. {
  717. struct ceph_msg_data *data = cursor->data;
  718. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  719. BUG_ON(cursor->page_index >= cursor->page_count);
  720. BUG_ON(cursor->page_offset >= PAGE_SIZE);
  721. *page_offset = cursor->page_offset;
  722. *length = min_t(size_t, cursor->resid, PAGE_SIZE - *page_offset);
  723. return data->pages[cursor->page_index];
  724. }
  725. static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
  726. size_t bytes)
  727. {
  728. BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
  729. BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
  730. /* Advance the cursor page offset */
  731. cursor->resid -= bytes;
  732. cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
  733. if (!bytes || cursor->page_offset)
  734. return false; /* more bytes to process in the current page */
  735. if (!cursor->resid)
  736. return false; /* no more data */
  737. /* Move on to the next page; offset is already at 0 */
  738. BUG_ON(cursor->page_index >= cursor->page_count);
  739. cursor->page_index++;
  740. return true;
  741. }
  742. /*
  743. * For a pagelist, a piece is whatever remains to be consumed in the
  744. * first page in the list, or the front of the next page.
  745. */
  746. static void
  747. ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
  748. size_t length)
  749. {
  750. struct ceph_msg_data *data = cursor->data;
  751. struct ceph_pagelist *pagelist;
  752. struct page *page;
  753. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  754. pagelist = data->pagelist;
  755. BUG_ON(!pagelist);
  756. if (!length)
  757. return; /* pagelist can be assigned but empty */
  758. BUG_ON(list_empty(&pagelist->head));
  759. page = list_first_entry(&pagelist->head, struct page, lru);
  760. cursor->resid = min(length, pagelist->length);
  761. cursor->page = page;
  762. cursor->offset = 0;
  763. }
  764. static struct page *
  765. ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
  766. size_t *page_offset, size_t *length)
  767. {
  768. struct ceph_msg_data *data = cursor->data;
  769. struct ceph_pagelist *pagelist;
  770. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  771. pagelist = data->pagelist;
  772. BUG_ON(!pagelist);
  773. BUG_ON(!cursor->page);
  774. BUG_ON(cursor->offset + cursor->resid != pagelist->length);
  775. /* offset of first page in pagelist is always 0 */
  776. *page_offset = cursor->offset & ~PAGE_MASK;
  777. *length = min_t(size_t, cursor->resid, PAGE_SIZE - *page_offset);
  778. return cursor->page;
  779. }
  780. static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
  781. size_t bytes)
  782. {
  783. struct ceph_msg_data *data = cursor->data;
  784. struct ceph_pagelist *pagelist;
  785. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  786. pagelist = data->pagelist;
  787. BUG_ON(!pagelist);
  788. BUG_ON(cursor->offset + cursor->resid != pagelist->length);
  789. BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
  790. /* Advance the cursor offset */
  791. cursor->resid -= bytes;
  792. cursor->offset += bytes;
  793. /* offset of first page in pagelist is always 0 */
  794. if (!bytes || cursor->offset & ~PAGE_MASK)
  795. return false; /* more bytes to process in the current page */
  796. if (!cursor->resid)
  797. return false; /* no more data */
  798. /* Move on to the next page */
  799. BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
  800. cursor->page = list_next_entry(cursor->page, lru);
  801. return true;
  802. }
  803. static void ceph_msg_data_iter_cursor_init(struct ceph_msg_data_cursor *cursor,
  804. size_t length)
  805. {
  806. struct ceph_msg_data *data = cursor->data;
  807. cursor->iov_iter = data->iter;
  808. cursor->lastlen = 0;
  809. iov_iter_truncate(&cursor->iov_iter, length);
  810. cursor->resid = iov_iter_count(&cursor->iov_iter);
  811. }
  812. static struct page *ceph_msg_data_iter_next(struct ceph_msg_data_cursor *cursor,
  813. size_t *page_offset, size_t *length)
  814. {
  815. struct page *page;
  816. ssize_t len;
  817. if (cursor->lastlen)
  818. iov_iter_revert(&cursor->iov_iter, cursor->lastlen);
  819. len = iov_iter_get_pages2(&cursor->iov_iter, &page, PAGE_SIZE,
  820. 1, page_offset);
  821. BUG_ON(len < 0);
  822. cursor->lastlen = len;
  823. /*
  824. * FIXME: The assumption is that the pages represented by the iov_iter
  825. * are pinned, with the references held by the upper-level
  826. * callers, or by virtue of being under writeback. Eventually,
  827. * we'll get an iov_iter_get_pages2 variant that doesn't take
  828. * page refs. Until then, just put the page ref.
  829. */
  830. VM_BUG_ON_PAGE(!PageWriteback(page) && page_count(page) < 2, page);
  831. put_page(page);
  832. *length = min_t(size_t, len, cursor->resid);
  833. return page;
  834. }
  835. static bool ceph_msg_data_iter_advance(struct ceph_msg_data_cursor *cursor,
  836. size_t bytes)
  837. {
  838. BUG_ON(bytes > cursor->resid);
  839. cursor->resid -= bytes;
  840. if (bytes < cursor->lastlen) {
  841. cursor->lastlen -= bytes;
  842. } else {
  843. iov_iter_advance(&cursor->iov_iter, bytes - cursor->lastlen);
  844. cursor->lastlen = 0;
  845. }
  846. return cursor->resid;
  847. }
  848. /*
  849. * Message data is handled (sent or received) in pieces, where each
  850. * piece resides on a single page. The network layer might not
  851. * consume an entire piece at once. A data item's cursor keeps
  852. * track of which piece is next to process and how much remains to
  853. * be processed in that piece. It also tracks whether the current
  854. * piece is the last one in the data item.
  855. */
  856. static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
  857. {
  858. size_t length = cursor->total_resid;
  859. switch (cursor->data->type) {
  860. case CEPH_MSG_DATA_PAGELIST:
  861. ceph_msg_data_pagelist_cursor_init(cursor, length);
  862. break;
  863. case CEPH_MSG_DATA_PAGES:
  864. ceph_msg_data_pages_cursor_init(cursor, length);
  865. break;
  866. #ifdef CONFIG_BLOCK
  867. case CEPH_MSG_DATA_BIO:
  868. ceph_msg_data_bio_cursor_init(cursor, length);
  869. break;
  870. #endif /* CONFIG_BLOCK */
  871. case CEPH_MSG_DATA_BVECS:
  872. ceph_msg_data_bvecs_cursor_init(cursor, length);
  873. break;
  874. case CEPH_MSG_DATA_ITER:
  875. ceph_msg_data_iter_cursor_init(cursor, length);
  876. break;
  877. case CEPH_MSG_DATA_NONE:
  878. default:
  879. /* BUG(); */
  880. break;
  881. }
  882. cursor->need_crc = true;
  883. }
  884. void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
  885. struct ceph_msg *msg, size_t length)
  886. {
  887. BUG_ON(!length);
  888. BUG_ON(length > msg->data_length);
  889. BUG_ON(!msg->num_data_items);
  890. cursor->total_resid = length;
  891. cursor->data = msg->data;
  892. cursor->sr_resid = 0;
  893. __ceph_msg_data_cursor_init(cursor);
  894. }
  895. /*
  896. * Return the page containing the next piece to process for a given
  897. * data item, and supply the page offset and length of that piece.
  898. * Indicate whether this is the last piece in this data item.
  899. */
  900. struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
  901. size_t *page_offset, size_t *length)
  902. {
  903. struct page *page;
  904. switch (cursor->data->type) {
  905. case CEPH_MSG_DATA_PAGELIST:
  906. page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
  907. break;
  908. case CEPH_MSG_DATA_PAGES:
  909. page = ceph_msg_data_pages_next(cursor, page_offset, length);
  910. break;
  911. #ifdef CONFIG_BLOCK
  912. case CEPH_MSG_DATA_BIO:
  913. page = ceph_msg_data_bio_next(cursor, page_offset, length);
  914. break;
  915. #endif /* CONFIG_BLOCK */
  916. case CEPH_MSG_DATA_BVECS:
  917. page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
  918. break;
  919. case CEPH_MSG_DATA_ITER:
  920. page = ceph_msg_data_iter_next(cursor, page_offset, length);
  921. break;
  922. case CEPH_MSG_DATA_NONE:
  923. default:
  924. page = NULL;
  925. break;
  926. }
  927. BUG_ON(!page);
  928. BUG_ON(*page_offset + *length > PAGE_SIZE);
  929. BUG_ON(!*length);
  930. BUG_ON(*length > cursor->resid);
  931. return page;
  932. }
  933. /*
  934. * Returns true if the result moves the cursor on to the next piece
  935. * of the data item.
  936. */
  937. void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes)
  938. {
  939. bool new_piece;
  940. BUG_ON(bytes > cursor->resid);
  941. switch (cursor->data->type) {
  942. case CEPH_MSG_DATA_PAGELIST:
  943. new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
  944. break;
  945. case CEPH_MSG_DATA_PAGES:
  946. new_piece = ceph_msg_data_pages_advance(cursor, bytes);
  947. break;
  948. #ifdef CONFIG_BLOCK
  949. case CEPH_MSG_DATA_BIO:
  950. new_piece = ceph_msg_data_bio_advance(cursor, bytes);
  951. break;
  952. #endif /* CONFIG_BLOCK */
  953. case CEPH_MSG_DATA_BVECS:
  954. new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
  955. break;
  956. case CEPH_MSG_DATA_ITER:
  957. new_piece = ceph_msg_data_iter_advance(cursor, bytes);
  958. break;
  959. case CEPH_MSG_DATA_NONE:
  960. default:
  961. BUG();
  962. break;
  963. }
  964. cursor->total_resid -= bytes;
  965. if (!cursor->resid && cursor->total_resid) {
  966. cursor->data++;
  967. __ceph_msg_data_cursor_init(cursor);
  968. new_piece = true;
  969. }
  970. cursor->need_crc = new_piece;
  971. }
  972. u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset,
  973. unsigned int length)
  974. {
  975. char *kaddr;
  976. kaddr = kmap(page);
  977. BUG_ON(kaddr == NULL);
  978. crc = crc32c(crc, kaddr + page_offset, length);
  979. kunmap(page);
  980. return crc;
  981. }
  982. bool ceph_addr_is_blank(const struct ceph_entity_addr *addr)
  983. {
  984. struct sockaddr_storage ss = addr->in_addr; /* align */
  985. struct in_addr *addr4 = &((struct sockaddr_in *)&ss)->sin_addr;
  986. struct in6_addr *addr6 = &((struct sockaddr_in6 *)&ss)->sin6_addr;
  987. switch (ss.ss_family) {
  988. case AF_INET:
  989. return addr4->s_addr == htonl(INADDR_ANY);
  990. case AF_INET6:
  991. return ipv6_addr_any(addr6);
  992. default:
  993. return true;
  994. }
  995. }
  996. EXPORT_SYMBOL(ceph_addr_is_blank);
  997. int ceph_addr_port(const struct ceph_entity_addr *addr)
  998. {
  999. switch (get_unaligned(&addr->in_addr.ss_family)) {
  1000. case AF_INET:
  1001. return ntohs(get_unaligned(&((struct sockaddr_in *)&addr->in_addr)->sin_port));
  1002. case AF_INET6:
  1003. return ntohs(get_unaligned(&((struct sockaddr_in6 *)&addr->in_addr)->sin6_port));
  1004. }
  1005. return 0;
  1006. }
  1007. void ceph_addr_set_port(struct ceph_entity_addr *addr, int p)
  1008. {
  1009. switch (get_unaligned(&addr->in_addr.ss_family)) {
  1010. case AF_INET:
  1011. put_unaligned(htons(p), &((struct sockaddr_in *)&addr->in_addr)->sin_port);
  1012. break;
  1013. case AF_INET6:
  1014. put_unaligned(htons(p), &((struct sockaddr_in6 *)&addr->in_addr)->sin6_port);
  1015. break;
  1016. }
  1017. }
  1018. /*
  1019. * Unlike other *_pton function semantics, zero indicates success.
  1020. */
  1021. static int ceph_pton(const char *str, size_t len, struct ceph_entity_addr *addr,
  1022. char delim, const char **ipend)
  1023. {
  1024. memset(&addr->in_addr, 0, sizeof(addr->in_addr));
  1025. if (in4_pton(str, len, (u8 *)&((struct sockaddr_in *)&addr->in_addr)->sin_addr.s_addr, delim, ipend)) {
  1026. put_unaligned(AF_INET, &addr->in_addr.ss_family);
  1027. return 0;
  1028. }
  1029. if (in6_pton(str, len, (u8 *)&((struct sockaddr_in6 *)&addr->in_addr)->sin6_addr.s6_addr, delim, ipend)) {
  1030. put_unaligned(AF_INET6, &addr->in_addr.ss_family);
  1031. return 0;
  1032. }
  1033. return -EINVAL;
  1034. }
  1035. /*
  1036. * Extract hostname string and resolve using kernel DNS facility.
  1037. */
  1038. #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
  1039. static int ceph_dns_resolve_name(const char *name, size_t namelen,
  1040. struct ceph_entity_addr *addr, char delim, const char **ipend)
  1041. {
  1042. const char *end, *delim_p;
  1043. char *colon_p, *ip_addr = NULL;
  1044. int ip_len, ret;
  1045. /*
  1046. * The end of the hostname occurs immediately preceding the delimiter or
  1047. * the port marker (':') where the delimiter takes precedence.
  1048. */
  1049. delim_p = memchr(name, delim, namelen);
  1050. colon_p = memchr(name, ':', namelen);
  1051. if (delim_p && colon_p)
  1052. end = min(delim_p, colon_p);
  1053. else if (!delim_p && colon_p)
  1054. end = colon_p;
  1055. else {
  1056. end = delim_p;
  1057. if (!end) /* case: hostname:/ */
  1058. end = name + namelen;
  1059. }
  1060. if (end <= name)
  1061. return -EINVAL;
  1062. /* do dns_resolve upcall */
  1063. ip_len = dns_query(current->nsproxy->net_ns,
  1064. NULL, name, end - name, NULL, &ip_addr, NULL, false);
  1065. if (ip_len > 0)
  1066. ret = ceph_pton(ip_addr, ip_len, addr, -1, NULL);
  1067. else
  1068. ret = -ESRCH;
  1069. kfree(ip_addr);
  1070. *ipend = end;
  1071. pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
  1072. ret, ret ? "failed" : ceph_pr_addr(addr));
  1073. return ret;
  1074. }
  1075. #else
  1076. static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
  1077. struct ceph_entity_addr *addr, char delim, const char **ipend)
  1078. {
  1079. return -EINVAL;
  1080. }
  1081. #endif
  1082. /*
  1083. * Parse a server name (IP or hostname). If a valid IP address is not found
  1084. * then try to extract a hostname to resolve using userspace DNS upcall.
  1085. */
  1086. static int ceph_parse_server_name(const char *name, size_t namelen,
  1087. struct ceph_entity_addr *addr, char delim, const char **ipend)
  1088. {
  1089. int ret;
  1090. ret = ceph_pton(name, namelen, addr, delim, ipend);
  1091. if (ret)
  1092. ret = ceph_dns_resolve_name(name, namelen, addr, delim, ipend);
  1093. return ret;
  1094. }
  1095. /*
  1096. * Parse an ip[:port] list into an addr array. Use the default
  1097. * monitor port if a port isn't specified.
  1098. */
  1099. int ceph_parse_ips(const char *c, const char *end,
  1100. struct ceph_entity_addr *addr,
  1101. int max_count, int *count, char delim)
  1102. {
  1103. int i, ret = -EINVAL;
  1104. const char *p = c;
  1105. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  1106. for (i = 0; i < max_count; i++) {
  1107. char cur_delim = delim;
  1108. const char *ipend;
  1109. int port;
  1110. if (*p == '[') {
  1111. cur_delim = ']';
  1112. p++;
  1113. }
  1114. ret = ceph_parse_server_name(p, end - p, &addr[i], cur_delim,
  1115. &ipend);
  1116. if (ret)
  1117. goto bad;
  1118. ret = -EINVAL;
  1119. p = ipend;
  1120. if (cur_delim == ']') {
  1121. if (*p != ']') {
  1122. dout("missing matching ']'\n");
  1123. goto bad;
  1124. }
  1125. p++;
  1126. }
  1127. /* port? */
  1128. if (p < end && *p == ':') {
  1129. port = 0;
  1130. p++;
  1131. while (p < end && *p >= '0' && *p <= '9') {
  1132. port = (port * 10) + (*p - '0');
  1133. p++;
  1134. }
  1135. if (port == 0)
  1136. port = CEPH_MON_PORT;
  1137. else if (port > 65535)
  1138. goto bad;
  1139. } else {
  1140. port = CEPH_MON_PORT;
  1141. }
  1142. ceph_addr_set_port(&addr[i], port);
  1143. /*
  1144. * We want the type to be set according to ms_mode
  1145. * option, but options are normally parsed after mon
  1146. * addresses. Rather than complicating parsing, set
  1147. * to LEGACY and override in build_initial_monmap()
  1148. * for mon addresses and ceph_messenger_init() for
  1149. * ip option.
  1150. */
  1151. addr[i].type = CEPH_ENTITY_ADDR_TYPE_LEGACY;
  1152. addr[i].nonce = 0;
  1153. dout("%s got %s\n", __func__, ceph_pr_addr(&addr[i]));
  1154. if (p == end)
  1155. break;
  1156. if (*p != delim)
  1157. goto bad;
  1158. p++;
  1159. }
  1160. if (p != end)
  1161. goto bad;
  1162. if (count)
  1163. *count = i + 1;
  1164. return 0;
  1165. bad:
  1166. return ret;
  1167. }
  1168. /*
  1169. * Process message. This happens in the worker thread. The callback should
  1170. * be careful not to do anything that waits on other incoming messages or it
  1171. * may deadlock.
  1172. */
  1173. void ceph_con_process_message(struct ceph_connection *con)
  1174. {
  1175. struct ceph_msg *msg = con->in_msg;
  1176. BUG_ON(con->in_msg->con != con);
  1177. con->in_msg = NULL;
  1178. /* if first message, set peer_name */
  1179. if (con->peer_name.type == 0)
  1180. con->peer_name = msg->hdr.src;
  1181. con->in_seq++;
  1182. mutex_unlock(&con->mutex);
  1183. dout("===== %p %llu from %s%lld %d=%s len %d+%d+%d (%u %u %u) =====\n",
  1184. msg, le64_to_cpu(msg->hdr.seq),
  1185. ENTITY_NAME(msg->hdr.src),
  1186. le16_to_cpu(msg->hdr.type),
  1187. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1188. le32_to_cpu(msg->hdr.front_len),
  1189. le32_to_cpu(msg->hdr.middle_len),
  1190. le32_to_cpu(msg->hdr.data_len),
  1191. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  1192. con->ops->dispatch(con, msg);
  1193. mutex_lock(&con->mutex);
  1194. }
  1195. /*
  1196. * Atomically queue work on a connection after the specified delay.
  1197. * Bump @con reference to avoid races with connection teardown.
  1198. * Returns 0 if work was queued, or an error code otherwise.
  1199. */
  1200. static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
  1201. {
  1202. if (!con->ops->get(con)) {
  1203. dout("%s %p ref count 0\n", __func__, con);
  1204. return -ENOENT;
  1205. }
  1206. if (delay >= HZ)
  1207. delay = round_jiffies_relative(delay);
  1208. dout("%s %p %lu\n", __func__, con, delay);
  1209. if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
  1210. dout("%s %p - already queued\n", __func__, con);
  1211. con->ops->put(con);
  1212. return -EBUSY;
  1213. }
  1214. return 0;
  1215. }
  1216. static void queue_con(struct ceph_connection *con)
  1217. {
  1218. (void) queue_con_delay(con, 0);
  1219. }
  1220. static void cancel_con(struct ceph_connection *con)
  1221. {
  1222. if (cancel_delayed_work(&con->work)) {
  1223. dout("%s %p\n", __func__, con);
  1224. con->ops->put(con);
  1225. }
  1226. }
  1227. static bool con_sock_closed(struct ceph_connection *con)
  1228. {
  1229. if (!ceph_con_flag_test_and_clear(con, CEPH_CON_F_SOCK_CLOSED))
  1230. return false;
  1231. #define CASE(x) \
  1232. case CEPH_CON_S_ ## x: \
  1233. con->error_msg = "socket closed (con state " #x ")"; \
  1234. break;
  1235. switch (con->state) {
  1236. CASE(CLOSED);
  1237. CASE(PREOPEN);
  1238. CASE(V1_BANNER);
  1239. CASE(V1_CONNECT_MSG);
  1240. CASE(V2_BANNER_PREFIX);
  1241. CASE(V2_BANNER_PAYLOAD);
  1242. CASE(V2_HELLO);
  1243. CASE(V2_AUTH);
  1244. CASE(V2_AUTH_SIGNATURE);
  1245. CASE(V2_SESSION_CONNECT);
  1246. CASE(V2_SESSION_RECONNECT);
  1247. CASE(OPEN);
  1248. CASE(STANDBY);
  1249. default:
  1250. BUG();
  1251. }
  1252. #undef CASE
  1253. return true;
  1254. }
  1255. static bool con_backoff(struct ceph_connection *con)
  1256. {
  1257. int ret;
  1258. if (!ceph_con_flag_test_and_clear(con, CEPH_CON_F_BACKOFF))
  1259. return false;
  1260. ret = queue_con_delay(con, con->delay);
  1261. if (ret) {
  1262. dout("%s: con %p FAILED to back off %lu\n", __func__,
  1263. con, con->delay);
  1264. BUG_ON(ret == -ENOENT);
  1265. ceph_con_flag_set(con, CEPH_CON_F_BACKOFF);
  1266. }
  1267. return true;
  1268. }
  1269. /* Finish fault handling; con->mutex must *not* be held here */
  1270. static void con_fault_finish(struct ceph_connection *con)
  1271. {
  1272. dout("%s %p\n", __func__, con);
  1273. /*
  1274. * in case we faulted due to authentication, invalidate our
  1275. * current tickets so that we can get new ones.
  1276. */
  1277. if (!ceph_msgr2(from_msgr(con->msgr)) && con->v1.auth_retry) {
  1278. dout("auth_retry %d, invalidating\n", con->v1.auth_retry);
  1279. if (con->ops->invalidate_authorizer)
  1280. con->ops->invalidate_authorizer(con);
  1281. con->v1.auth_retry = 0;
  1282. }
  1283. if (con->ops->fault)
  1284. con->ops->fault(con);
  1285. }
  1286. /*
  1287. * Do some work on a connection. Drop a connection ref when we're done.
  1288. */
  1289. static void ceph_con_workfn(struct work_struct *work)
  1290. {
  1291. struct ceph_connection *con = container_of(work, struct ceph_connection,
  1292. work.work);
  1293. bool fault;
  1294. mutex_lock(&con->mutex);
  1295. while (true) {
  1296. int ret;
  1297. if ((fault = con_sock_closed(con))) {
  1298. dout("%s: con %p SOCK_CLOSED\n", __func__, con);
  1299. break;
  1300. }
  1301. if (con_backoff(con)) {
  1302. dout("%s: con %p BACKOFF\n", __func__, con);
  1303. break;
  1304. }
  1305. if (con->state == CEPH_CON_S_STANDBY) {
  1306. dout("%s: con %p STANDBY\n", __func__, con);
  1307. break;
  1308. }
  1309. if (con->state == CEPH_CON_S_CLOSED) {
  1310. dout("%s: con %p CLOSED\n", __func__, con);
  1311. BUG_ON(con->sock);
  1312. break;
  1313. }
  1314. if (con->state == CEPH_CON_S_PREOPEN) {
  1315. dout("%s: con %p PREOPEN\n", __func__, con);
  1316. BUG_ON(con->sock);
  1317. }
  1318. if (ceph_msgr2(from_msgr(con->msgr)))
  1319. ret = ceph_con_v2_try_read(con);
  1320. else
  1321. ret = ceph_con_v1_try_read(con);
  1322. if (ret < 0) {
  1323. if (ret == -EAGAIN)
  1324. continue;
  1325. if (!con->error_msg)
  1326. con->error_msg = "socket error on read";
  1327. fault = true;
  1328. break;
  1329. }
  1330. if (ceph_msgr2(from_msgr(con->msgr)))
  1331. ret = ceph_con_v2_try_write(con);
  1332. else
  1333. ret = ceph_con_v1_try_write(con);
  1334. if (ret < 0) {
  1335. if (ret == -EAGAIN)
  1336. continue;
  1337. if (!con->error_msg)
  1338. con->error_msg = "socket error on write";
  1339. fault = true;
  1340. }
  1341. break; /* If we make it to here, we're done */
  1342. }
  1343. if (fault)
  1344. con_fault(con);
  1345. mutex_unlock(&con->mutex);
  1346. if (fault)
  1347. con_fault_finish(con);
  1348. con->ops->put(con);
  1349. }
  1350. /*
  1351. * Generic error/fault handler. A retry mechanism is used with
  1352. * exponential backoff
  1353. */
  1354. static void con_fault(struct ceph_connection *con)
  1355. {
  1356. dout("fault %p state %d to peer %s\n",
  1357. con, con->state, ceph_pr_addr(&con->peer_addr));
  1358. pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  1359. ceph_pr_addr(&con->peer_addr), con->error_msg);
  1360. con->error_msg = NULL;
  1361. WARN_ON(con->state == CEPH_CON_S_STANDBY ||
  1362. con->state == CEPH_CON_S_CLOSED);
  1363. ceph_con_reset_protocol(con);
  1364. if (ceph_con_flag_test(con, CEPH_CON_F_LOSSYTX)) {
  1365. dout("fault on LOSSYTX channel, marking CLOSED\n");
  1366. con->state = CEPH_CON_S_CLOSED;
  1367. return;
  1368. }
  1369. /* Requeue anything that hasn't been acked */
  1370. list_splice_init(&con->out_sent, &con->out_queue);
  1371. /* If there are no messages queued or keepalive pending, place
  1372. * the connection in a STANDBY state */
  1373. if (list_empty(&con->out_queue) &&
  1374. !ceph_con_flag_test(con, CEPH_CON_F_KEEPALIVE_PENDING)) {
  1375. dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
  1376. ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
  1377. con->state = CEPH_CON_S_STANDBY;
  1378. } else {
  1379. /* retry after a delay. */
  1380. con->state = CEPH_CON_S_PREOPEN;
  1381. if (!con->delay) {
  1382. con->delay = BASE_DELAY_INTERVAL;
  1383. } else if (con->delay < MAX_DELAY_INTERVAL) {
  1384. con->delay *= 2;
  1385. if (con->delay > MAX_DELAY_INTERVAL)
  1386. con->delay = MAX_DELAY_INTERVAL;
  1387. }
  1388. ceph_con_flag_set(con, CEPH_CON_F_BACKOFF);
  1389. queue_con(con);
  1390. }
  1391. }
  1392. void ceph_messenger_reset_nonce(struct ceph_messenger *msgr)
  1393. {
  1394. u32 nonce = le32_to_cpu(msgr->inst.addr.nonce) + 1000000;
  1395. msgr->inst.addr.nonce = cpu_to_le32(nonce);
  1396. ceph_encode_my_addr(msgr);
  1397. }
  1398. /*
  1399. * initialize a new messenger instance
  1400. */
  1401. void ceph_messenger_init(struct ceph_messenger *msgr,
  1402. struct ceph_entity_addr *myaddr)
  1403. {
  1404. spin_lock_init(&msgr->global_seq_lock);
  1405. if (myaddr) {
  1406. memcpy(&msgr->inst.addr.in_addr, &myaddr->in_addr,
  1407. sizeof(msgr->inst.addr.in_addr));
  1408. ceph_addr_set_port(&msgr->inst.addr, 0);
  1409. }
  1410. /*
  1411. * Since nautilus, clients are identified using type ANY.
  1412. * For msgr1, ceph_encode_banner_addr() munges it to NONE.
  1413. */
  1414. msgr->inst.addr.type = CEPH_ENTITY_ADDR_TYPE_ANY;
  1415. /* generate a random non-zero nonce */
  1416. do {
  1417. get_random_bytes(&msgr->inst.addr.nonce,
  1418. sizeof(msgr->inst.addr.nonce));
  1419. } while (!msgr->inst.addr.nonce);
  1420. ceph_encode_my_addr(msgr);
  1421. atomic_set(&msgr->stopping, 0);
  1422. write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
  1423. dout("%s %p\n", __func__, msgr);
  1424. }
  1425. void ceph_messenger_fini(struct ceph_messenger *msgr)
  1426. {
  1427. put_net(read_pnet(&msgr->net));
  1428. }
  1429. static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
  1430. {
  1431. if (msg->con)
  1432. msg->con->ops->put(msg->con);
  1433. msg->con = con ? con->ops->get(con) : NULL;
  1434. BUG_ON(msg->con != con);
  1435. }
  1436. static void clear_standby(struct ceph_connection *con)
  1437. {
  1438. /* come back from STANDBY? */
  1439. if (con->state == CEPH_CON_S_STANDBY) {
  1440. dout("clear_standby %p\n", con);
  1441. con->state = CEPH_CON_S_PREOPEN;
  1442. if (!ceph_msgr2(from_msgr(con->msgr)))
  1443. con->v1.connect_seq++;
  1444. WARN_ON(ceph_con_flag_test(con, CEPH_CON_F_WRITE_PENDING));
  1445. WARN_ON(ceph_con_flag_test(con, CEPH_CON_F_KEEPALIVE_PENDING));
  1446. }
  1447. }
  1448. /*
  1449. * Queue up an outgoing message on the given connection.
  1450. *
  1451. * Consumes a ref on @msg.
  1452. */
  1453. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  1454. {
  1455. /* set src+dst */
  1456. msg->hdr.src = con->msgr->inst.name;
  1457. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  1458. msg->needs_out_seq = true;
  1459. mutex_lock(&con->mutex);
  1460. if (con->state == CEPH_CON_S_CLOSED) {
  1461. dout("con_send %p closed, dropping %p\n", con, msg);
  1462. ceph_msg_put(msg);
  1463. mutex_unlock(&con->mutex);
  1464. return;
  1465. }
  1466. msg_con_set(msg, con);
  1467. BUG_ON(!list_empty(&msg->list_head));
  1468. list_add_tail(&msg->list_head, &con->out_queue);
  1469. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  1470. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  1471. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1472. le32_to_cpu(msg->hdr.front_len),
  1473. le32_to_cpu(msg->hdr.middle_len),
  1474. le32_to_cpu(msg->hdr.data_len));
  1475. clear_standby(con);
  1476. mutex_unlock(&con->mutex);
  1477. /* if there wasn't anything waiting to send before, queue
  1478. * new work */
  1479. if (!ceph_con_flag_test_and_set(con, CEPH_CON_F_WRITE_PENDING))
  1480. queue_con(con);
  1481. }
  1482. EXPORT_SYMBOL(ceph_con_send);
  1483. /*
  1484. * Revoke a message that was previously queued for send
  1485. */
  1486. void ceph_msg_revoke(struct ceph_msg *msg)
  1487. {
  1488. struct ceph_connection *con = msg->con;
  1489. if (!con) {
  1490. dout("%s msg %p null con\n", __func__, msg);
  1491. return; /* Message not in our possession */
  1492. }
  1493. mutex_lock(&con->mutex);
  1494. if (list_empty(&msg->list_head)) {
  1495. WARN_ON(con->out_msg == msg);
  1496. dout("%s con %p msg %p not linked\n", __func__, con, msg);
  1497. mutex_unlock(&con->mutex);
  1498. return;
  1499. }
  1500. dout("%s con %p msg %p was linked\n", __func__, con, msg);
  1501. msg->hdr.seq = 0;
  1502. ceph_msg_remove(msg);
  1503. if (con->out_msg == msg) {
  1504. WARN_ON(con->state != CEPH_CON_S_OPEN);
  1505. dout("%s con %p msg %p was sending\n", __func__, con, msg);
  1506. if (ceph_msgr2(from_msgr(con->msgr)))
  1507. ceph_con_v2_revoke(con);
  1508. else
  1509. ceph_con_v1_revoke(con);
  1510. ceph_msg_put(con->out_msg);
  1511. con->out_msg = NULL;
  1512. } else {
  1513. dout("%s con %p msg %p not current, out_msg %p\n", __func__,
  1514. con, msg, con->out_msg);
  1515. }
  1516. mutex_unlock(&con->mutex);
  1517. }
  1518. /*
  1519. * Revoke a message that we may be reading data into
  1520. */
  1521. void ceph_msg_revoke_incoming(struct ceph_msg *msg)
  1522. {
  1523. struct ceph_connection *con = msg->con;
  1524. if (!con) {
  1525. dout("%s msg %p null con\n", __func__, msg);
  1526. return; /* Message not in our possession */
  1527. }
  1528. mutex_lock(&con->mutex);
  1529. if (con->in_msg == msg) {
  1530. WARN_ON(con->state != CEPH_CON_S_OPEN);
  1531. dout("%s con %p msg %p was recving\n", __func__, con, msg);
  1532. if (ceph_msgr2(from_msgr(con->msgr)))
  1533. ceph_con_v2_revoke_incoming(con);
  1534. else
  1535. ceph_con_v1_revoke_incoming(con);
  1536. ceph_msg_put(con->in_msg);
  1537. con->in_msg = NULL;
  1538. } else {
  1539. dout("%s con %p msg %p not current, in_msg %p\n", __func__,
  1540. con, msg, con->in_msg);
  1541. }
  1542. mutex_unlock(&con->mutex);
  1543. }
  1544. /*
  1545. * Queue a keepalive byte to ensure the tcp connection is alive.
  1546. */
  1547. void ceph_con_keepalive(struct ceph_connection *con)
  1548. {
  1549. dout("con_keepalive %p\n", con);
  1550. mutex_lock(&con->mutex);
  1551. clear_standby(con);
  1552. ceph_con_flag_set(con, CEPH_CON_F_KEEPALIVE_PENDING);
  1553. mutex_unlock(&con->mutex);
  1554. if (!ceph_con_flag_test_and_set(con, CEPH_CON_F_WRITE_PENDING))
  1555. queue_con(con);
  1556. }
  1557. EXPORT_SYMBOL(ceph_con_keepalive);
  1558. bool ceph_con_keepalive_expired(struct ceph_connection *con,
  1559. unsigned long interval)
  1560. {
  1561. if (interval > 0 &&
  1562. (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
  1563. struct timespec64 now;
  1564. struct timespec64 ts;
  1565. ktime_get_real_ts64(&now);
  1566. jiffies_to_timespec64(interval, &ts);
  1567. ts = timespec64_add(con->last_keepalive_ack, ts);
  1568. return timespec64_compare(&now, &ts) >= 0;
  1569. }
  1570. return false;
  1571. }
  1572. static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg)
  1573. {
  1574. BUG_ON(msg->num_data_items >= msg->max_data_items);
  1575. return &msg->data[msg->num_data_items++];
  1576. }
  1577. static void ceph_msg_data_destroy(struct ceph_msg_data *data)
  1578. {
  1579. if (data->type == CEPH_MSG_DATA_PAGES && data->own_pages) {
  1580. int num_pages = calc_pages_for(data->alignment, data->length);
  1581. ceph_release_page_vector(data->pages, num_pages);
  1582. } else if (data->type == CEPH_MSG_DATA_PAGELIST) {
  1583. ceph_pagelist_release(data->pagelist);
  1584. }
  1585. }
  1586. void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
  1587. size_t length, size_t alignment, bool own_pages)
  1588. {
  1589. struct ceph_msg_data *data;
  1590. BUG_ON(!pages);
  1591. BUG_ON(!length);
  1592. data = ceph_msg_data_add(msg);
  1593. data->type = CEPH_MSG_DATA_PAGES;
  1594. data->pages = pages;
  1595. data->length = length;
  1596. data->alignment = alignment & ~PAGE_MASK;
  1597. data->own_pages = own_pages;
  1598. msg->data_length += length;
  1599. }
  1600. EXPORT_SYMBOL(ceph_msg_data_add_pages);
  1601. void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
  1602. struct ceph_pagelist *pagelist)
  1603. {
  1604. struct ceph_msg_data *data;
  1605. BUG_ON(!pagelist);
  1606. BUG_ON(!pagelist->length);
  1607. data = ceph_msg_data_add(msg);
  1608. data->type = CEPH_MSG_DATA_PAGELIST;
  1609. refcount_inc(&pagelist->refcnt);
  1610. data->pagelist = pagelist;
  1611. msg->data_length += pagelist->length;
  1612. }
  1613. EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
  1614. #ifdef CONFIG_BLOCK
  1615. void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
  1616. u32 length)
  1617. {
  1618. struct ceph_msg_data *data;
  1619. data = ceph_msg_data_add(msg);
  1620. data->type = CEPH_MSG_DATA_BIO;
  1621. data->bio_pos = *bio_pos;
  1622. data->bio_length = length;
  1623. msg->data_length += length;
  1624. }
  1625. EXPORT_SYMBOL(ceph_msg_data_add_bio);
  1626. #endif /* CONFIG_BLOCK */
  1627. void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
  1628. struct ceph_bvec_iter *bvec_pos)
  1629. {
  1630. struct ceph_msg_data *data;
  1631. data = ceph_msg_data_add(msg);
  1632. data->type = CEPH_MSG_DATA_BVECS;
  1633. data->bvec_pos = *bvec_pos;
  1634. msg->data_length += bvec_pos->iter.bi_size;
  1635. }
  1636. EXPORT_SYMBOL(ceph_msg_data_add_bvecs);
  1637. void ceph_msg_data_add_iter(struct ceph_msg *msg,
  1638. struct iov_iter *iter)
  1639. {
  1640. struct ceph_msg_data *data;
  1641. data = ceph_msg_data_add(msg);
  1642. data->type = CEPH_MSG_DATA_ITER;
  1643. data->iter = *iter;
  1644. msg->data_length += iov_iter_count(&data->iter);
  1645. }
  1646. /*
  1647. * construct a new message with given type, size
  1648. * the new msg has a ref count of 1.
  1649. */
  1650. struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
  1651. gfp_t flags, bool can_fail)
  1652. {
  1653. struct ceph_msg *m;
  1654. m = kmem_cache_zalloc(ceph_msg_cache, flags);
  1655. if (m == NULL)
  1656. goto out;
  1657. m->hdr.type = cpu_to_le16(type);
  1658. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  1659. m->hdr.front_len = cpu_to_le32(front_len);
  1660. INIT_LIST_HEAD(&m->list_head);
  1661. kref_init(&m->kref);
  1662. /* front */
  1663. if (front_len) {
  1664. m->front.iov_base = kvmalloc(front_len, flags);
  1665. if (m->front.iov_base == NULL) {
  1666. dout("ceph_msg_new can't allocate %d bytes\n",
  1667. front_len);
  1668. goto out2;
  1669. }
  1670. } else {
  1671. m->front.iov_base = NULL;
  1672. }
  1673. m->front_alloc_len = m->front.iov_len = front_len;
  1674. if (max_data_items) {
  1675. m->data = kmalloc_array(max_data_items, sizeof(*m->data),
  1676. flags);
  1677. if (!m->data)
  1678. goto out2;
  1679. m->max_data_items = max_data_items;
  1680. }
  1681. dout("ceph_msg_new %p front %d\n", m, front_len);
  1682. return m;
  1683. out2:
  1684. ceph_msg_put(m);
  1685. out:
  1686. if (!can_fail) {
  1687. pr_err("msg_new can't create type %d front %d\n", type,
  1688. front_len);
  1689. WARN_ON(1);
  1690. } else {
  1691. dout("msg_new can't create type %d front %d\n", type,
  1692. front_len);
  1693. }
  1694. return NULL;
  1695. }
  1696. EXPORT_SYMBOL(ceph_msg_new2);
  1697. struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  1698. bool can_fail)
  1699. {
  1700. return ceph_msg_new2(type, front_len, 0, flags, can_fail);
  1701. }
  1702. EXPORT_SYMBOL(ceph_msg_new);
  1703. /*
  1704. * Allocate "middle" portion of a message, if it is needed and wasn't
  1705. * allocated by alloc_msg. This allows us to read a small fixed-size
  1706. * per-type header in the front and then gracefully fail (i.e.,
  1707. * propagate the error to the caller based on info in the front) when
  1708. * the middle is too large.
  1709. */
  1710. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  1711. {
  1712. int type = le16_to_cpu(msg->hdr.type);
  1713. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  1714. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  1715. ceph_msg_type_name(type), middle_len);
  1716. BUG_ON(!middle_len);
  1717. BUG_ON(msg->middle);
  1718. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  1719. if (!msg->middle)
  1720. return -ENOMEM;
  1721. return 0;
  1722. }
  1723. /*
  1724. * Allocate a message for receiving an incoming message on a
  1725. * connection, and save the result in con->in_msg. Uses the
  1726. * connection's private alloc_msg op if available.
  1727. *
  1728. * Returns 0 on success, or a negative error code.
  1729. *
  1730. * On success, if we set *skip = 1:
  1731. * - the next message should be skipped and ignored.
  1732. * - con->in_msg == NULL
  1733. * or if we set *skip = 0:
  1734. * - con->in_msg is non-null.
  1735. * On error (ENOMEM, EAGAIN, ...),
  1736. * - con->in_msg == NULL
  1737. */
  1738. int ceph_con_in_msg_alloc(struct ceph_connection *con,
  1739. struct ceph_msg_header *hdr, int *skip)
  1740. {
  1741. int middle_len = le32_to_cpu(hdr->middle_len);
  1742. struct ceph_msg *msg;
  1743. int ret = 0;
  1744. BUG_ON(con->in_msg != NULL);
  1745. BUG_ON(!con->ops->alloc_msg);
  1746. mutex_unlock(&con->mutex);
  1747. msg = con->ops->alloc_msg(con, hdr, skip);
  1748. mutex_lock(&con->mutex);
  1749. if (con->state != CEPH_CON_S_OPEN) {
  1750. if (msg)
  1751. ceph_msg_put(msg);
  1752. return -EAGAIN;
  1753. }
  1754. if (msg) {
  1755. BUG_ON(*skip);
  1756. msg_con_set(msg, con);
  1757. con->in_msg = msg;
  1758. } else {
  1759. /*
  1760. * Null message pointer means either we should skip
  1761. * this message or we couldn't allocate memory. The
  1762. * former is not an error.
  1763. */
  1764. if (*skip)
  1765. return 0;
  1766. con->error_msg = "error allocating memory for incoming message";
  1767. return -ENOMEM;
  1768. }
  1769. memcpy(&con->in_msg->hdr, hdr, sizeof(*hdr));
  1770. if (middle_len && !con->in_msg->middle) {
  1771. ret = ceph_alloc_middle(con, con->in_msg);
  1772. if (ret < 0) {
  1773. ceph_msg_put(con->in_msg);
  1774. con->in_msg = NULL;
  1775. }
  1776. }
  1777. return ret;
  1778. }
  1779. void ceph_con_get_out_msg(struct ceph_connection *con)
  1780. {
  1781. struct ceph_msg *msg;
  1782. BUG_ON(list_empty(&con->out_queue));
  1783. msg = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
  1784. WARN_ON(msg->con != con);
  1785. /*
  1786. * Put the message on "sent" list using a ref from ceph_con_send().
  1787. * It is put when the message is acked or revoked.
  1788. */
  1789. list_move_tail(&msg->list_head, &con->out_sent);
  1790. /*
  1791. * Only assign outgoing seq # if we haven't sent this message
  1792. * yet. If it is requeued, resend with it's original seq.
  1793. */
  1794. if (msg->needs_out_seq) {
  1795. msg->hdr.seq = cpu_to_le64(++con->out_seq);
  1796. msg->needs_out_seq = false;
  1797. if (con->ops->reencode_message)
  1798. con->ops->reencode_message(msg);
  1799. }
  1800. /*
  1801. * Get a ref for out_msg. It is put when we are done sending the
  1802. * message or in case of a fault.
  1803. */
  1804. WARN_ON(con->out_msg);
  1805. con->out_msg = ceph_msg_get(msg);
  1806. }
  1807. /*
  1808. * Free a generically kmalloc'd message.
  1809. */
  1810. static void ceph_msg_free(struct ceph_msg *m)
  1811. {
  1812. dout("%s %p\n", __func__, m);
  1813. kvfree(m->front.iov_base);
  1814. kfree(m->data);
  1815. kmem_cache_free(ceph_msg_cache, m);
  1816. }
  1817. static void ceph_msg_release(struct kref *kref)
  1818. {
  1819. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  1820. int i;
  1821. dout("%s %p\n", __func__, m);
  1822. WARN_ON(!list_empty(&m->list_head));
  1823. msg_con_set(m, NULL);
  1824. /* drop middle, data, if any */
  1825. if (m->middle) {
  1826. ceph_buffer_put(m->middle);
  1827. m->middle = NULL;
  1828. }
  1829. for (i = 0; i < m->num_data_items; i++)
  1830. ceph_msg_data_destroy(&m->data[i]);
  1831. if (m->pool)
  1832. ceph_msgpool_put(m->pool, m);
  1833. else
  1834. ceph_msg_free(m);
  1835. }
  1836. struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
  1837. {
  1838. dout("%s %p (was %d)\n", __func__, msg,
  1839. kref_read(&msg->kref));
  1840. kref_get(&msg->kref);
  1841. return msg;
  1842. }
  1843. EXPORT_SYMBOL(ceph_msg_get);
  1844. void ceph_msg_put(struct ceph_msg *msg)
  1845. {
  1846. dout("%s %p (was %d)\n", __func__, msg,
  1847. kref_read(&msg->kref));
  1848. kref_put(&msg->kref, ceph_msg_release);
  1849. }
  1850. EXPORT_SYMBOL(ceph_msg_put);
  1851. void ceph_msg_dump(struct ceph_msg *msg)
  1852. {
  1853. pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
  1854. msg->front_alloc_len, msg->data_length);
  1855. print_hex_dump(KERN_DEBUG, "header: ",
  1856. DUMP_PREFIX_OFFSET, 16, 1,
  1857. &msg->hdr, sizeof(msg->hdr), true);
  1858. print_hex_dump(KERN_DEBUG, " front: ",
  1859. DUMP_PREFIX_OFFSET, 16, 1,
  1860. msg->front.iov_base, msg->front.iov_len, true);
  1861. if (msg->middle)
  1862. print_hex_dump(KERN_DEBUG, "middle: ",
  1863. DUMP_PREFIX_OFFSET, 16, 1,
  1864. msg->middle->vec.iov_base,
  1865. msg->middle->vec.iov_len, true);
  1866. print_hex_dump(KERN_DEBUG, "footer: ",
  1867. DUMP_PREFIX_OFFSET, 16, 1,
  1868. &msg->footer, sizeof(msg->footer), true);
  1869. }
  1870. EXPORT_SYMBOL(ceph_msg_dump);