messenger.c 53 KB

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