messenger.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/crc32c.h>
  3. #include <linux/ctype.h>
  4. #include <linux/highmem.h>
  5. #include <linux/inet.h>
  6. #include <linux/kthread.h>
  7. #include <linux/net.h>
  8. #include <linux/slab.h>
  9. #include <linux/socket.h>
  10. #include <linux/string.h>
  11. #include <linux/bio.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/dns_resolver.h>
  14. #include <net/tcp.h>
  15. #include <linux/ceph/libceph.h>
  16. #include <linux/ceph/messenger.h>
  17. #include <linux/ceph/decode.h>
  18. #include <linux/ceph/pagelist.h>
  19. #include <linux/export.h>
  20. /*
  21. * Ceph uses the messenger to exchange ceph_msg messages with other
  22. * hosts in the system. The messenger provides ordered and reliable
  23. * delivery. We tolerate TCP disconnects by reconnecting (with
  24. * exponential backoff) in the case of a fault (disconnection, bad
  25. * crc, protocol error). Acks allow sent messages to be discarded by
  26. * the sender.
  27. */
  28. /* static tag bytes (protocol control messages) */
  29. static char tag_msg = CEPH_MSGR_TAG_MSG;
  30. static char tag_ack = CEPH_MSGR_TAG_ACK;
  31. static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
  32. #ifdef CONFIG_LOCKDEP
  33. static struct lock_class_key socket_class;
  34. #endif
  35. /*
  36. * When skipping (ignoring) a block of input we read it into a "skip
  37. * buffer," which is this many bytes in size.
  38. */
  39. #define SKIP_BUF_SIZE 1024
  40. static void queue_con(struct ceph_connection *con);
  41. static void con_work(struct work_struct *);
  42. static void ceph_fault(struct ceph_connection *con);
  43. /*
  44. * Nicely render a sockaddr as a string. An array of formatted
  45. * strings is used, to approximate reentrancy.
  46. */
  47. #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
  48. #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
  49. #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
  50. #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
  51. static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
  52. static atomic_t addr_str_seq = ATOMIC_INIT(0);
  53. static struct page *zero_page; /* used in certain error cases */
  54. const char *ceph_pr_addr(const struct sockaddr_storage *ss)
  55. {
  56. int i;
  57. char *s;
  58. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  59. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  60. i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
  61. s = addr_str[i];
  62. switch (ss->ss_family) {
  63. case AF_INET:
  64. snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
  65. ntohs(in4->sin_port));
  66. break;
  67. case AF_INET6:
  68. snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
  69. ntohs(in6->sin6_port));
  70. break;
  71. default:
  72. snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
  73. ss->ss_family);
  74. }
  75. return s;
  76. }
  77. EXPORT_SYMBOL(ceph_pr_addr);
  78. static void encode_my_addr(struct ceph_messenger *msgr)
  79. {
  80. memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
  81. ceph_encode_addr(&msgr->my_enc_addr);
  82. }
  83. /*
  84. * work queue for all reading and writing to/from the socket.
  85. */
  86. static struct workqueue_struct *ceph_msgr_wq;
  87. void _ceph_msgr_exit(void)
  88. {
  89. if (ceph_msgr_wq) {
  90. destroy_workqueue(ceph_msgr_wq);
  91. ceph_msgr_wq = NULL;
  92. }
  93. BUG_ON(zero_page == NULL);
  94. kunmap(zero_page);
  95. page_cache_release(zero_page);
  96. zero_page = NULL;
  97. }
  98. int ceph_msgr_init(void)
  99. {
  100. BUG_ON(zero_page != NULL);
  101. zero_page = ZERO_PAGE(0);
  102. page_cache_get(zero_page);
  103. ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
  104. if (ceph_msgr_wq)
  105. return 0;
  106. pr_err("msgr_init failed to create workqueue\n");
  107. _ceph_msgr_exit();
  108. return -ENOMEM;
  109. }
  110. EXPORT_SYMBOL(ceph_msgr_init);
  111. void ceph_msgr_exit(void)
  112. {
  113. BUG_ON(ceph_msgr_wq == NULL);
  114. _ceph_msgr_exit();
  115. }
  116. EXPORT_SYMBOL(ceph_msgr_exit);
  117. void ceph_msgr_flush(void)
  118. {
  119. flush_workqueue(ceph_msgr_wq);
  120. }
  121. EXPORT_SYMBOL(ceph_msgr_flush);
  122. /*
  123. * socket callback functions
  124. */
  125. /* data available on socket, or listen socket received a connect */
  126. static void ceph_data_ready(struct sock *sk, int count_unused)
  127. {
  128. struct ceph_connection *con = sk->sk_user_data;
  129. if (sk->sk_state != TCP_CLOSE_WAIT) {
  130. dout("ceph_data_ready on %p state = %lu, queueing work\n",
  131. con, con->state);
  132. queue_con(con);
  133. }
  134. }
  135. /* socket has buffer space for writing */
  136. static void ceph_write_space(struct sock *sk)
  137. {
  138. struct ceph_connection *con = sk->sk_user_data;
  139. /* only queue to workqueue if there is data we want to write,
  140. * and there is sufficient space in the socket buffer to accept
  141. * more data. clear SOCK_NOSPACE so that ceph_write_space()
  142. * doesn't get called again until try_write() fills the socket
  143. * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
  144. * and net/core/stream.c:sk_stream_write_space().
  145. */
  146. if (test_bit(WRITE_PENDING, &con->state)) {
  147. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
  148. dout("ceph_write_space %p queueing write work\n", con);
  149. clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  150. queue_con(con);
  151. }
  152. } else {
  153. dout("ceph_write_space %p nothing to write\n", con);
  154. }
  155. }
  156. /* socket's state has changed */
  157. static void ceph_state_change(struct sock *sk)
  158. {
  159. struct ceph_connection *con = sk->sk_user_data;
  160. dout("ceph_state_change %p state = %lu sk_state = %u\n",
  161. con, con->state, sk->sk_state);
  162. if (test_bit(CLOSED, &con->state))
  163. return;
  164. switch (sk->sk_state) {
  165. case TCP_CLOSE:
  166. dout("ceph_state_change TCP_CLOSE\n");
  167. case TCP_CLOSE_WAIT:
  168. dout("ceph_state_change TCP_CLOSE_WAIT\n");
  169. if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
  170. if (test_bit(CONNECTING, &con->state))
  171. con->error_msg = "connection failed";
  172. else
  173. con->error_msg = "socket closed";
  174. queue_con(con);
  175. }
  176. break;
  177. case TCP_ESTABLISHED:
  178. dout("ceph_state_change TCP_ESTABLISHED\n");
  179. queue_con(con);
  180. break;
  181. default: /* Everything else is uninteresting */
  182. break;
  183. }
  184. }
  185. /*
  186. * set up socket callbacks
  187. */
  188. static void set_sock_callbacks(struct socket *sock,
  189. struct ceph_connection *con)
  190. {
  191. struct sock *sk = sock->sk;
  192. sk->sk_user_data = con;
  193. sk->sk_data_ready = ceph_data_ready;
  194. sk->sk_write_space = ceph_write_space;
  195. sk->sk_state_change = ceph_state_change;
  196. }
  197. /*
  198. * socket helpers
  199. */
  200. /*
  201. * initiate connection to a remote socket.
  202. */
  203. static int ceph_tcp_connect(struct ceph_connection *con)
  204. {
  205. struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
  206. struct socket *sock;
  207. int ret;
  208. BUG_ON(con->sock);
  209. ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
  210. IPPROTO_TCP, &sock);
  211. if (ret)
  212. return ret;
  213. sock->sk->sk_allocation = GFP_NOFS;
  214. #ifdef CONFIG_LOCKDEP
  215. lockdep_set_class(&sock->sk->sk_lock, &socket_class);
  216. #endif
  217. set_sock_callbacks(sock, con);
  218. dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
  219. ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
  220. O_NONBLOCK);
  221. if (ret == -EINPROGRESS) {
  222. dout("connect %s EINPROGRESS sk_state = %u\n",
  223. ceph_pr_addr(&con->peer_addr.in_addr),
  224. sock->sk->sk_state);
  225. } else if (ret < 0) {
  226. pr_err("connect %s error %d\n",
  227. ceph_pr_addr(&con->peer_addr.in_addr), ret);
  228. sock_release(sock);
  229. con->error_msg = "connect error";
  230. return ret;
  231. }
  232. con->sock = sock;
  233. return 0;
  234. }
  235. static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
  236. {
  237. struct kvec iov = {buf, len};
  238. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  239. int r;
  240. r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
  241. if (r == -EAGAIN)
  242. r = 0;
  243. return r;
  244. }
  245. /*
  246. * write something. @more is true if caller will be sending more data
  247. * shortly.
  248. */
  249. static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
  250. size_t kvlen, size_t len, int more)
  251. {
  252. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  253. int r;
  254. if (more)
  255. msg.msg_flags |= MSG_MORE;
  256. else
  257. msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
  258. r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
  259. if (r == -EAGAIN)
  260. r = 0;
  261. return r;
  262. }
  263. static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
  264. int offset, size_t size, int more)
  265. {
  266. int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
  267. int ret;
  268. ret = kernel_sendpage(sock, page, offset, size, flags);
  269. if (ret == -EAGAIN)
  270. ret = 0;
  271. return ret;
  272. }
  273. /*
  274. * Shutdown/close the socket for the given connection.
  275. */
  276. static int con_close_socket(struct ceph_connection *con)
  277. {
  278. int rc;
  279. dout("con_close_socket on %p sock %p\n", con, con->sock);
  280. if (!con->sock)
  281. return 0;
  282. set_bit(SOCK_CLOSED, &con->state);
  283. rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
  284. sock_release(con->sock);
  285. con->sock = NULL;
  286. clear_bit(SOCK_CLOSED, &con->state);
  287. return rc;
  288. }
  289. /*
  290. * Reset a connection. Discard all incoming and outgoing messages
  291. * and clear *_seq state.
  292. */
  293. static void ceph_msg_remove(struct ceph_msg *msg)
  294. {
  295. list_del_init(&msg->list_head);
  296. ceph_msg_put(msg);
  297. }
  298. static void ceph_msg_remove_list(struct list_head *head)
  299. {
  300. while (!list_empty(head)) {
  301. struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
  302. list_head);
  303. ceph_msg_remove(msg);
  304. }
  305. }
  306. static void reset_connection(struct ceph_connection *con)
  307. {
  308. /* reset connection, out_queue, msg_ and connect_seq */
  309. /* discard existing out_queue and msg_seq */
  310. ceph_msg_remove_list(&con->out_queue);
  311. ceph_msg_remove_list(&con->out_sent);
  312. if (con->in_msg) {
  313. ceph_msg_put(con->in_msg);
  314. con->in_msg = NULL;
  315. }
  316. con->connect_seq = 0;
  317. con->out_seq = 0;
  318. if (con->out_msg) {
  319. ceph_msg_put(con->out_msg);
  320. con->out_msg = NULL;
  321. }
  322. con->in_seq = 0;
  323. con->in_seq_acked = 0;
  324. }
  325. /*
  326. * mark a peer down. drop any open connections.
  327. */
  328. void ceph_con_close(struct ceph_connection *con)
  329. {
  330. dout("con_close %p peer %s\n", con,
  331. ceph_pr_addr(&con->peer_addr.in_addr));
  332. set_bit(CLOSED, &con->state); /* in case there's queued work */
  333. clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
  334. clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
  335. clear_bit(KEEPALIVE_PENDING, &con->state);
  336. clear_bit(WRITE_PENDING, &con->state);
  337. mutex_lock(&con->mutex);
  338. reset_connection(con);
  339. con->peer_global_seq = 0;
  340. cancel_delayed_work(&con->work);
  341. mutex_unlock(&con->mutex);
  342. queue_con(con);
  343. }
  344. EXPORT_SYMBOL(ceph_con_close);
  345. /*
  346. * Reopen a closed connection, with a new peer address.
  347. */
  348. void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
  349. {
  350. dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
  351. set_bit(OPENING, &con->state);
  352. clear_bit(CLOSED, &con->state);
  353. memcpy(&con->peer_addr, addr, sizeof(*addr));
  354. con->delay = 0; /* reset backoff memory */
  355. queue_con(con);
  356. }
  357. EXPORT_SYMBOL(ceph_con_open);
  358. /*
  359. * return true if this connection ever successfully opened
  360. */
  361. bool ceph_con_opened(struct ceph_connection *con)
  362. {
  363. return con->connect_seq > 0;
  364. }
  365. /*
  366. * generic get/put
  367. */
  368. struct ceph_connection *ceph_con_get(struct ceph_connection *con)
  369. {
  370. int nref = __atomic_add_unless(&con->nref, 1, 0);
  371. dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
  372. return nref ? con : NULL;
  373. }
  374. void ceph_con_put(struct ceph_connection *con)
  375. {
  376. int nref = atomic_dec_return(&con->nref);
  377. BUG_ON(nref < 0);
  378. if (nref == 0) {
  379. BUG_ON(con->sock);
  380. kfree(con);
  381. }
  382. dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
  383. }
  384. /*
  385. * initialize a new connection.
  386. */
  387. void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
  388. {
  389. dout("con_init %p\n", con);
  390. memset(con, 0, sizeof(*con));
  391. atomic_set(&con->nref, 1);
  392. con->msgr = msgr;
  393. mutex_init(&con->mutex);
  394. INIT_LIST_HEAD(&con->out_queue);
  395. INIT_LIST_HEAD(&con->out_sent);
  396. INIT_DELAYED_WORK(&con->work, con_work);
  397. }
  398. EXPORT_SYMBOL(ceph_con_init);
  399. /*
  400. * We maintain a global counter to order connection attempts. Get
  401. * a unique seq greater than @gt.
  402. */
  403. static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
  404. {
  405. u32 ret;
  406. spin_lock(&msgr->global_seq_lock);
  407. if (msgr->global_seq < gt)
  408. msgr->global_seq = gt;
  409. ret = ++msgr->global_seq;
  410. spin_unlock(&msgr->global_seq_lock);
  411. return ret;
  412. }
  413. static void ceph_con_out_kvec_reset(struct ceph_connection *con)
  414. {
  415. con->out_kvec_left = 0;
  416. con->out_kvec_bytes = 0;
  417. con->out_kvec_cur = &con->out_kvec[0];
  418. }
  419. static void ceph_con_out_kvec_add(struct ceph_connection *con,
  420. size_t size, void *data)
  421. {
  422. int index;
  423. index = con->out_kvec_left;
  424. BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
  425. con->out_kvec[index].iov_len = size;
  426. con->out_kvec[index].iov_base = data;
  427. con->out_kvec_left++;
  428. con->out_kvec_bytes += size;
  429. }
  430. /*
  431. * Prepare footer for currently outgoing message, and finish things
  432. * off. Assumes out_kvec* are already valid.. we just add on to the end.
  433. */
  434. static void prepare_write_message_footer(struct ceph_connection *con)
  435. {
  436. struct ceph_msg *m = con->out_msg;
  437. int v = con->out_kvec_left;
  438. dout("prepare_write_message_footer %p\n", con);
  439. con->out_kvec_is_msg = true;
  440. con->out_kvec[v].iov_base = &m->footer;
  441. con->out_kvec[v].iov_len = sizeof(m->footer);
  442. con->out_kvec_bytes += sizeof(m->footer);
  443. con->out_kvec_left++;
  444. con->out_more = m->more_to_follow;
  445. con->out_msg_done = true;
  446. }
  447. /*
  448. * Prepare headers for the next outgoing message.
  449. */
  450. static void prepare_write_message(struct ceph_connection *con)
  451. {
  452. struct ceph_msg *m;
  453. u32 crc;
  454. ceph_con_out_kvec_reset(con);
  455. con->out_kvec_is_msg = true;
  456. con->out_msg_done = false;
  457. /* Sneak an ack in there first? If we can get it into the same
  458. * TCP packet that's a good thing. */
  459. if (con->in_seq > con->in_seq_acked) {
  460. con->in_seq_acked = con->in_seq;
  461. ceph_con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  462. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  463. ceph_con_out_kvec_add(con, sizeof (con->out_temp_ack),
  464. &con->out_temp_ack);
  465. }
  466. m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
  467. con->out_msg = m;
  468. /* put message on sent list */
  469. ceph_msg_get(m);
  470. list_move_tail(&m->list_head, &con->out_sent);
  471. /*
  472. * only assign outgoing seq # if we haven't sent this message
  473. * yet. if it is requeued, resend with it's original seq.
  474. */
  475. if (m->needs_out_seq) {
  476. m->hdr.seq = cpu_to_le64(++con->out_seq);
  477. m->needs_out_seq = false;
  478. }
  479. dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
  480. m, con->out_seq, le16_to_cpu(m->hdr.type),
  481. le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
  482. le32_to_cpu(m->hdr.data_len),
  483. m->nr_pages);
  484. BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
  485. /* tag + hdr + front + middle */
  486. ceph_con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
  487. ceph_con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
  488. ceph_con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
  489. if (m->middle)
  490. ceph_con_out_kvec_add(con, m->middle->vec.iov_len,
  491. m->middle->vec.iov_base);
  492. /* fill in crc (except data pages), footer */
  493. crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
  494. con->out_msg->hdr.crc = cpu_to_le32(crc);
  495. con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
  496. crc = crc32c(0, m->front.iov_base, m->front.iov_len);
  497. con->out_msg->footer.front_crc = cpu_to_le32(crc);
  498. if (m->middle) {
  499. crc = crc32c(0, m->middle->vec.iov_base,
  500. m->middle->vec.iov_len);
  501. con->out_msg->footer.middle_crc = cpu_to_le32(crc);
  502. } else
  503. con->out_msg->footer.middle_crc = 0;
  504. con->out_msg->footer.data_crc = 0;
  505. dout("prepare_write_message front_crc %u data_crc %u\n",
  506. le32_to_cpu(con->out_msg->footer.front_crc),
  507. le32_to_cpu(con->out_msg->footer.middle_crc));
  508. /* is there a data payload? */
  509. if (le32_to_cpu(m->hdr.data_len) > 0) {
  510. /* initialize page iterator */
  511. con->out_msg_pos.page = 0;
  512. if (m->pages)
  513. con->out_msg_pos.page_pos = m->page_alignment;
  514. else
  515. con->out_msg_pos.page_pos = 0;
  516. con->out_msg_pos.data_pos = 0;
  517. con->out_msg_pos.did_page_crc = false;
  518. con->out_more = 1; /* data + footer will follow */
  519. } else {
  520. /* no, queue up footer too and be done */
  521. prepare_write_message_footer(con);
  522. }
  523. set_bit(WRITE_PENDING, &con->state);
  524. }
  525. /*
  526. * Prepare an ack.
  527. */
  528. static void prepare_write_ack(struct ceph_connection *con)
  529. {
  530. dout("prepare_write_ack %p %llu -> %llu\n", con,
  531. con->in_seq_acked, con->in_seq);
  532. con->in_seq_acked = con->in_seq;
  533. ceph_con_out_kvec_reset(con);
  534. ceph_con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  535. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  536. ceph_con_out_kvec_add(con, sizeof (con->out_temp_ack),
  537. &con->out_temp_ack);
  538. con->out_more = 1; /* more will follow.. eventually.. */
  539. set_bit(WRITE_PENDING, &con->state);
  540. }
  541. /*
  542. * Prepare to write keepalive byte.
  543. */
  544. static void prepare_write_keepalive(struct ceph_connection *con)
  545. {
  546. dout("prepare_write_keepalive %p\n", con);
  547. ceph_con_out_kvec_reset(con);
  548. ceph_con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
  549. set_bit(WRITE_PENDING, &con->state);
  550. }
  551. /*
  552. * Connection negotiation.
  553. */
  554. static int prepare_connect_authorizer(struct ceph_connection *con)
  555. {
  556. void *auth_buf;
  557. int auth_len;
  558. int auth_protocol;
  559. if (!con->ops->get_authorizer) {
  560. con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
  561. con->out_connect.authorizer_len = 0;
  562. return 0;
  563. }
  564. /* Can't hold the mutex while getting authorizer */
  565. mutex_unlock(&con->mutex);
  566. auth_buf = NULL;
  567. auth_len = 0;
  568. auth_protocol = CEPH_AUTH_UNKNOWN;
  569. con->ops->get_authorizer(con, &auth_buf, &auth_len, &auth_protocol,
  570. &con->auth_reply_buf, &con->auth_reply_buf_len,
  571. con->auth_retry);
  572. mutex_lock(&con->mutex);
  573. if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->state))
  574. return -EAGAIN;
  575. con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
  576. con->out_connect.authorizer_len = cpu_to_le32(auth_len);
  577. if (auth_len)
  578. ceph_con_out_kvec_add(con, auth_len, auth_buf);
  579. return 0;
  580. }
  581. /*
  582. * We connected to a peer and are saying hello.
  583. */
  584. static void prepare_write_banner(struct ceph_connection *con)
  585. {
  586. ceph_con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
  587. ceph_con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
  588. &con->msgr->my_enc_addr);
  589. con->out_more = 0;
  590. set_bit(WRITE_PENDING, &con->state);
  591. }
  592. static int prepare_write_connect(struct ceph_connection *con)
  593. {
  594. unsigned global_seq = get_global_seq(con->msgr, 0);
  595. int proto;
  596. int ret;
  597. switch (con->peer_name.type) {
  598. case CEPH_ENTITY_TYPE_MON:
  599. proto = CEPH_MONC_PROTOCOL;
  600. break;
  601. case CEPH_ENTITY_TYPE_OSD:
  602. proto = CEPH_OSDC_PROTOCOL;
  603. break;
  604. case CEPH_ENTITY_TYPE_MDS:
  605. proto = CEPH_MDSC_PROTOCOL;
  606. break;
  607. default:
  608. BUG();
  609. }
  610. dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
  611. con->connect_seq, global_seq, proto);
  612. con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
  613. con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
  614. con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
  615. con->out_connect.global_seq = cpu_to_le32(global_seq);
  616. con->out_connect.protocol_version = cpu_to_le32(proto);
  617. con->out_connect.flags = 0;
  618. ceph_con_out_kvec_add(con, sizeof (con->out_connect), &con->out_connect);
  619. ret = prepare_connect_authorizer(con);
  620. if (ret)
  621. return ret;
  622. con->out_more = 0;
  623. set_bit(WRITE_PENDING, &con->state);
  624. return 0;
  625. }
  626. /*
  627. * write as much of pending kvecs to the socket as we can.
  628. * 1 -> done
  629. * 0 -> socket full, but more to do
  630. * <0 -> error
  631. */
  632. static int write_partial_kvec(struct ceph_connection *con)
  633. {
  634. int ret;
  635. dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
  636. while (con->out_kvec_bytes > 0) {
  637. ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
  638. con->out_kvec_left, con->out_kvec_bytes,
  639. con->out_more);
  640. if (ret <= 0)
  641. goto out;
  642. con->out_kvec_bytes -= ret;
  643. if (con->out_kvec_bytes == 0)
  644. break; /* done */
  645. /* account for full iov entries consumed */
  646. while (ret >= con->out_kvec_cur->iov_len) {
  647. BUG_ON(!con->out_kvec_left);
  648. ret -= con->out_kvec_cur->iov_len;
  649. con->out_kvec_cur++;
  650. con->out_kvec_left--;
  651. }
  652. /* and for a partially-consumed entry */
  653. if (ret) {
  654. con->out_kvec_cur->iov_len -= ret;
  655. con->out_kvec_cur->iov_base += ret;
  656. }
  657. }
  658. con->out_kvec_left = 0;
  659. con->out_kvec_is_msg = false;
  660. ret = 1;
  661. out:
  662. dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
  663. con->out_kvec_bytes, con->out_kvec_left, ret);
  664. return ret; /* done! */
  665. }
  666. #ifdef CONFIG_BLOCK
  667. static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
  668. {
  669. if (!bio) {
  670. *iter = NULL;
  671. *seg = 0;
  672. return;
  673. }
  674. *iter = bio;
  675. *seg = bio->bi_idx;
  676. }
  677. static void iter_bio_next(struct bio **bio_iter, int *seg)
  678. {
  679. if (*bio_iter == NULL)
  680. return;
  681. BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
  682. (*seg)++;
  683. if (*seg == (*bio_iter)->bi_vcnt)
  684. init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
  685. }
  686. #endif
  687. /*
  688. * Write as much message data payload as we can. If we finish, queue
  689. * up the footer.
  690. * 1 -> done, footer is now queued in out_kvec[].
  691. * 0 -> socket full, but more to do
  692. * <0 -> error
  693. */
  694. static int write_partial_msg_pages(struct ceph_connection *con)
  695. {
  696. struct ceph_msg *msg = con->out_msg;
  697. unsigned data_len = le32_to_cpu(msg->hdr.data_len);
  698. size_t len;
  699. bool do_datacrc = !con->msgr->nocrc;
  700. int ret;
  701. int total_max_write;
  702. int in_trail = 0;
  703. size_t trail_len = (msg->trail ? msg->trail->length : 0);
  704. dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
  705. con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
  706. con->out_msg_pos.page_pos);
  707. #ifdef CONFIG_BLOCK
  708. if (msg->bio && !msg->bio_iter)
  709. init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
  710. #endif
  711. while (data_len > con->out_msg_pos.data_pos) {
  712. struct page *page = NULL;
  713. int max_write = PAGE_SIZE;
  714. int bio_offset = 0;
  715. total_max_write = data_len - trail_len -
  716. con->out_msg_pos.data_pos;
  717. /*
  718. * if we are calculating the data crc (the default), we need
  719. * to map the page. if our pages[] has been revoked, use the
  720. * zero page.
  721. */
  722. /* have we reached the trail part of the data? */
  723. if (con->out_msg_pos.data_pos >= data_len - trail_len) {
  724. in_trail = 1;
  725. total_max_write = data_len - con->out_msg_pos.data_pos;
  726. page = list_first_entry(&msg->trail->head,
  727. struct page, lru);
  728. max_write = PAGE_SIZE;
  729. } else if (msg->pages) {
  730. page = msg->pages[con->out_msg_pos.page];
  731. } else if (msg->pagelist) {
  732. page = list_first_entry(&msg->pagelist->head,
  733. struct page, lru);
  734. #ifdef CONFIG_BLOCK
  735. } else if (msg->bio) {
  736. struct bio_vec *bv;
  737. bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
  738. page = bv->bv_page;
  739. bio_offset = bv->bv_offset;
  740. max_write = bv->bv_len;
  741. #endif
  742. } else {
  743. page = zero_page;
  744. }
  745. len = min_t(int, max_write - con->out_msg_pos.page_pos,
  746. total_max_write);
  747. if (do_datacrc && !con->out_msg_pos.did_page_crc) {
  748. void *base;
  749. u32 crc;
  750. u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
  751. char *kaddr;
  752. kaddr = kmap(page);
  753. BUG_ON(kaddr == NULL);
  754. base = kaddr + con->out_msg_pos.page_pos + bio_offset;
  755. crc = crc32c(tmpcrc, base, len);
  756. con->out_msg->footer.data_crc = cpu_to_le32(crc);
  757. con->out_msg_pos.did_page_crc = true;
  758. }
  759. ret = ceph_tcp_sendpage(con->sock, page,
  760. con->out_msg_pos.page_pos + bio_offset,
  761. len, 1);
  762. if (do_datacrc)
  763. kunmap(page);
  764. if (ret <= 0)
  765. goto out;
  766. con->out_msg_pos.data_pos += ret;
  767. con->out_msg_pos.page_pos += ret;
  768. if (ret == len) {
  769. con->out_msg_pos.page_pos = 0;
  770. con->out_msg_pos.page++;
  771. con->out_msg_pos.did_page_crc = false;
  772. if (in_trail)
  773. list_move_tail(&page->lru,
  774. &msg->trail->head);
  775. else if (msg->pagelist)
  776. list_move_tail(&page->lru,
  777. &msg->pagelist->head);
  778. #ifdef CONFIG_BLOCK
  779. else if (msg->bio)
  780. iter_bio_next(&msg->bio_iter, &msg->bio_seg);
  781. #endif
  782. }
  783. }
  784. dout("write_partial_msg_pages %p msg %p done\n", con, msg);
  785. /* prepare and queue up footer, too */
  786. if (!do_datacrc)
  787. con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
  788. ceph_con_out_kvec_reset(con);
  789. prepare_write_message_footer(con);
  790. ret = 1;
  791. out:
  792. return ret;
  793. }
  794. /*
  795. * write some zeros
  796. */
  797. static int write_partial_skip(struct ceph_connection *con)
  798. {
  799. int ret;
  800. while (con->out_skip > 0) {
  801. size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
  802. ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
  803. if (ret <= 0)
  804. goto out;
  805. con->out_skip -= ret;
  806. }
  807. ret = 1;
  808. out:
  809. return ret;
  810. }
  811. /*
  812. * Prepare to read connection handshake, or an ack.
  813. */
  814. static void prepare_read_banner(struct ceph_connection *con)
  815. {
  816. dout("prepare_read_banner %p\n", con);
  817. con->in_base_pos = 0;
  818. }
  819. static void prepare_read_connect(struct ceph_connection *con)
  820. {
  821. dout("prepare_read_connect %p\n", con);
  822. con->in_base_pos = 0;
  823. }
  824. static void prepare_read_ack(struct ceph_connection *con)
  825. {
  826. dout("prepare_read_ack %p\n", con);
  827. con->in_base_pos = 0;
  828. }
  829. static void prepare_read_tag(struct ceph_connection *con)
  830. {
  831. dout("prepare_read_tag %p\n", con);
  832. con->in_base_pos = 0;
  833. con->in_tag = CEPH_MSGR_TAG_READY;
  834. }
  835. /*
  836. * Prepare to read a message.
  837. */
  838. static int prepare_read_message(struct ceph_connection *con)
  839. {
  840. dout("prepare_read_message %p\n", con);
  841. BUG_ON(con->in_msg != NULL);
  842. con->in_base_pos = 0;
  843. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  844. return 0;
  845. }
  846. static int read_partial(struct ceph_connection *con,
  847. int end, int size, void *object)
  848. {
  849. while (con->in_base_pos < end) {
  850. int left = end - con->in_base_pos;
  851. int have = size - left;
  852. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  853. if (ret <= 0)
  854. return ret;
  855. con->in_base_pos += ret;
  856. }
  857. return 1;
  858. }
  859. /*
  860. * Read all or part of the connect-side handshake on a new connection
  861. */
  862. static int read_partial_banner(struct ceph_connection *con)
  863. {
  864. int size;
  865. int end;
  866. int ret;
  867. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  868. /* peer's banner */
  869. size = strlen(CEPH_BANNER);
  870. end = size;
  871. ret = read_partial(con, end, size, con->in_banner);
  872. if (ret <= 0)
  873. goto out;
  874. size = sizeof (con->actual_peer_addr);
  875. end += size;
  876. ret = read_partial(con, end, size, &con->actual_peer_addr);
  877. if (ret <= 0)
  878. goto out;
  879. size = sizeof (con->peer_addr_for_me);
  880. end += size;
  881. ret = read_partial(con, end, size, &con->peer_addr_for_me);
  882. if (ret <= 0)
  883. goto out;
  884. out:
  885. return ret;
  886. }
  887. static int read_partial_connect(struct ceph_connection *con)
  888. {
  889. int size;
  890. int end;
  891. int ret;
  892. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  893. size = sizeof (con->in_reply);
  894. end = size;
  895. ret = read_partial(con, end, size, &con->in_reply);
  896. if (ret <= 0)
  897. goto out;
  898. size = le32_to_cpu(con->in_reply.authorizer_len);
  899. end += size;
  900. ret = read_partial(con, end, size, con->auth_reply_buf);
  901. if (ret <= 0)
  902. goto out;
  903. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  904. con, (int)con->in_reply.tag,
  905. le32_to_cpu(con->in_reply.connect_seq),
  906. le32_to_cpu(con->in_reply.global_seq));
  907. out:
  908. return ret;
  909. }
  910. /*
  911. * Verify the hello banner looks okay.
  912. */
  913. static int verify_hello(struct ceph_connection *con)
  914. {
  915. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  916. pr_err("connect to %s got bad banner\n",
  917. ceph_pr_addr(&con->peer_addr.in_addr));
  918. con->error_msg = "protocol error, bad banner";
  919. return -1;
  920. }
  921. return 0;
  922. }
  923. static bool addr_is_blank(struct sockaddr_storage *ss)
  924. {
  925. switch (ss->ss_family) {
  926. case AF_INET:
  927. return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
  928. case AF_INET6:
  929. return
  930. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
  931. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
  932. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
  933. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
  934. }
  935. return false;
  936. }
  937. static int addr_port(struct sockaddr_storage *ss)
  938. {
  939. switch (ss->ss_family) {
  940. case AF_INET:
  941. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  942. case AF_INET6:
  943. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  944. }
  945. return 0;
  946. }
  947. static void addr_set_port(struct sockaddr_storage *ss, int p)
  948. {
  949. switch (ss->ss_family) {
  950. case AF_INET:
  951. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  952. break;
  953. case AF_INET6:
  954. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  955. break;
  956. }
  957. }
  958. /*
  959. * Unlike other *_pton function semantics, zero indicates success.
  960. */
  961. static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
  962. char delim, const char **ipend)
  963. {
  964. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  965. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  966. memset(ss, 0, sizeof(*ss));
  967. if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
  968. ss->ss_family = AF_INET;
  969. return 0;
  970. }
  971. if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
  972. ss->ss_family = AF_INET6;
  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 sockaddr_storage *ss, 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(NULL, name, end - name, NULL, &ip_addr, NULL);
  1006. if (ip_len > 0)
  1007. ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
  1008. else
  1009. ret = -ESRCH;
  1010. kfree(ip_addr);
  1011. *ipend = end;
  1012. pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
  1013. ret, ret ? "failed" : ceph_pr_addr(ss));
  1014. return ret;
  1015. }
  1016. #else
  1017. static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
  1018. struct sockaddr_storage *ss, char delim, const char **ipend)
  1019. {
  1020. return -EINVAL;
  1021. }
  1022. #endif
  1023. /*
  1024. * Parse a server name (IP or hostname). If a valid IP address is not found
  1025. * then try to extract a hostname to resolve using userspace DNS upcall.
  1026. */
  1027. static int ceph_parse_server_name(const char *name, size_t namelen,
  1028. struct sockaddr_storage *ss, char delim, const char **ipend)
  1029. {
  1030. int ret;
  1031. ret = ceph_pton(name, namelen, ss, delim, ipend);
  1032. if (ret)
  1033. ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
  1034. return ret;
  1035. }
  1036. /*
  1037. * Parse an ip[:port] list into an addr array. Use the default
  1038. * monitor port if a port isn't specified.
  1039. */
  1040. int ceph_parse_ips(const char *c, const char *end,
  1041. struct ceph_entity_addr *addr,
  1042. int max_count, int *count)
  1043. {
  1044. int i, ret = -EINVAL;
  1045. const char *p = c;
  1046. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  1047. for (i = 0; i < max_count; i++) {
  1048. const char *ipend;
  1049. struct sockaddr_storage *ss = &addr[i].in_addr;
  1050. int port;
  1051. char delim = ',';
  1052. if (*p == '[') {
  1053. delim = ']';
  1054. p++;
  1055. }
  1056. ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
  1057. if (ret)
  1058. goto bad;
  1059. ret = -EINVAL;
  1060. p = ipend;
  1061. if (delim == ']') {
  1062. if (*p != ']') {
  1063. dout("missing matching ']'\n");
  1064. goto bad;
  1065. }
  1066. p++;
  1067. }
  1068. /* port? */
  1069. if (p < end && *p == ':') {
  1070. port = 0;
  1071. p++;
  1072. while (p < end && *p >= '0' && *p <= '9') {
  1073. port = (port * 10) + (*p - '0');
  1074. p++;
  1075. }
  1076. if (port > 65535 || port == 0)
  1077. goto bad;
  1078. } else {
  1079. port = CEPH_MON_PORT;
  1080. }
  1081. addr_set_port(ss, port);
  1082. dout("parse_ips got %s\n", ceph_pr_addr(ss));
  1083. if (p == end)
  1084. break;
  1085. if (*p != ',')
  1086. goto bad;
  1087. p++;
  1088. }
  1089. if (p != end)
  1090. goto bad;
  1091. if (count)
  1092. *count = i + 1;
  1093. return 0;
  1094. bad:
  1095. pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
  1096. return ret;
  1097. }
  1098. EXPORT_SYMBOL(ceph_parse_ips);
  1099. static int process_banner(struct ceph_connection *con)
  1100. {
  1101. dout("process_banner on %p\n", con);
  1102. if (verify_hello(con) < 0)
  1103. return -1;
  1104. ceph_decode_addr(&con->actual_peer_addr);
  1105. ceph_decode_addr(&con->peer_addr_for_me);
  1106. /*
  1107. * Make sure the other end is who we wanted. note that the other
  1108. * end may not yet know their ip address, so if it's 0.0.0.0, give
  1109. * them the benefit of the doubt.
  1110. */
  1111. if (memcmp(&con->peer_addr, &con->actual_peer_addr,
  1112. sizeof(con->peer_addr)) != 0 &&
  1113. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  1114. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  1115. pr_warning("wrong peer, want %s/%d, got %s/%d\n",
  1116. ceph_pr_addr(&con->peer_addr.in_addr),
  1117. (int)le32_to_cpu(con->peer_addr.nonce),
  1118. ceph_pr_addr(&con->actual_peer_addr.in_addr),
  1119. (int)le32_to_cpu(con->actual_peer_addr.nonce));
  1120. con->error_msg = "wrong peer at address";
  1121. return -1;
  1122. }
  1123. /*
  1124. * did we learn our address?
  1125. */
  1126. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  1127. int port = addr_port(&con->msgr->inst.addr.in_addr);
  1128. memcpy(&con->msgr->inst.addr.in_addr,
  1129. &con->peer_addr_for_me.in_addr,
  1130. sizeof(con->peer_addr_for_me.in_addr));
  1131. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  1132. encode_my_addr(con->msgr);
  1133. dout("process_banner learned my addr is %s\n",
  1134. ceph_pr_addr(&con->msgr->inst.addr.in_addr));
  1135. }
  1136. set_bit(NEGOTIATING, &con->state);
  1137. prepare_read_connect(con);
  1138. return 0;
  1139. }
  1140. static void fail_protocol(struct ceph_connection *con)
  1141. {
  1142. reset_connection(con);
  1143. set_bit(CLOSED, &con->state); /* in case there's queued work */
  1144. mutex_unlock(&con->mutex);
  1145. if (con->ops->bad_proto)
  1146. con->ops->bad_proto(con);
  1147. mutex_lock(&con->mutex);
  1148. }
  1149. static int process_connect(struct ceph_connection *con)
  1150. {
  1151. u64 sup_feat = con->msgr->supported_features;
  1152. u64 req_feat = con->msgr->required_features;
  1153. u64 server_feat = le64_to_cpu(con->in_reply.features);
  1154. int ret;
  1155. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  1156. switch (con->in_reply.tag) {
  1157. case CEPH_MSGR_TAG_FEATURES:
  1158. pr_err("%s%lld %s feature set mismatch,"
  1159. " my %llx < server's %llx, missing %llx\n",
  1160. ENTITY_NAME(con->peer_name),
  1161. ceph_pr_addr(&con->peer_addr.in_addr),
  1162. sup_feat, server_feat, server_feat & ~sup_feat);
  1163. con->error_msg = "missing required protocol features";
  1164. fail_protocol(con);
  1165. return -1;
  1166. case CEPH_MSGR_TAG_BADPROTOVER:
  1167. pr_err("%s%lld %s protocol version mismatch,"
  1168. " my %d != server's %d\n",
  1169. ENTITY_NAME(con->peer_name),
  1170. ceph_pr_addr(&con->peer_addr.in_addr),
  1171. le32_to_cpu(con->out_connect.protocol_version),
  1172. le32_to_cpu(con->in_reply.protocol_version));
  1173. con->error_msg = "protocol version mismatch";
  1174. fail_protocol(con);
  1175. return -1;
  1176. case CEPH_MSGR_TAG_BADAUTHORIZER:
  1177. con->auth_retry++;
  1178. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  1179. con->auth_retry);
  1180. if (con->auth_retry == 2) {
  1181. con->error_msg = "connect authorization failure";
  1182. return -1;
  1183. }
  1184. con->auth_retry = 1;
  1185. ceph_con_out_kvec_reset(con);
  1186. ret = prepare_write_connect(con);
  1187. if (ret < 0)
  1188. return ret;
  1189. prepare_read_connect(con);
  1190. break;
  1191. case CEPH_MSGR_TAG_RESETSESSION:
  1192. /*
  1193. * If we connected with a large connect_seq but the peer
  1194. * has no record of a session with us (no connection, or
  1195. * connect_seq == 0), they will send RESETSESION to indicate
  1196. * that they must have reset their session, and may have
  1197. * dropped messages.
  1198. */
  1199. dout("process_connect got RESET peer seq %u\n",
  1200. le32_to_cpu(con->in_connect.connect_seq));
  1201. pr_err("%s%lld %s connection reset\n",
  1202. ENTITY_NAME(con->peer_name),
  1203. ceph_pr_addr(&con->peer_addr.in_addr));
  1204. reset_connection(con);
  1205. ceph_con_out_kvec_reset(con);
  1206. ret = prepare_write_connect(con);
  1207. if (ret < 0)
  1208. return ret;
  1209. prepare_read_connect(con);
  1210. /* Tell ceph about it. */
  1211. mutex_unlock(&con->mutex);
  1212. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  1213. if (con->ops->peer_reset)
  1214. con->ops->peer_reset(con);
  1215. mutex_lock(&con->mutex);
  1216. if (test_bit(CLOSED, &con->state) ||
  1217. test_bit(OPENING, &con->state))
  1218. return -EAGAIN;
  1219. break;
  1220. case CEPH_MSGR_TAG_RETRY_SESSION:
  1221. /*
  1222. * If we sent a smaller connect_seq than the peer has, try
  1223. * again with a larger value.
  1224. */
  1225. dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
  1226. le32_to_cpu(con->out_connect.connect_seq),
  1227. le32_to_cpu(con->in_connect.connect_seq));
  1228. con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
  1229. ceph_con_out_kvec_reset(con);
  1230. ret = prepare_write_connect(con);
  1231. if (ret < 0)
  1232. return ret;
  1233. prepare_read_connect(con);
  1234. break;
  1235. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1236. /*
  1237. * If we sent a smaller global_seq than the peer has, try
  1238. * again with a larger value.
  1239. */
  1240. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1241. con->peer_global_seq,
  1242. le32_to_cpu(con->in_connect.global_seq));
  1243. get_global_seq(con->msgr,
  1244. le32_to_cpu(con->in_connect.global_seq));
  1245. ceph_con_out_kvec_reset(con);
  1246. ret = prepare_write_connect(con);
  1247. if (ret < 0)
  1248. return ret;
  1249. prepare_read_connect(con);
  1250. break;
  1251. case CEPH_MSGR_TAG_READY:
  1252. if (req_feat & ~server_feat) {
  1253. pr_err("%s%lld %s protocol feature mismatch,"
  1254. " my required %llx > server's %llx, need %llx\n",
  1255. ENTITY_NAME(con->peer_name),
  1256. ceph_pr_addr(&con->peer_addr.in_addr),
  1257. req_feat, server_feat, req_feat & ~server_feat);
  1258. con->error_msg = "missing required protocol features";
  1259. fail_protocol(con);
  1260. return -1;
  1261. }
  1262. clear_bit(CONNECTING, &con->state);
  1263. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1264. con->connect_seq++;
  1265. con->peer_features = server_feat;
  1266. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1267. con->peer_global_seq,
  1268. le32_to_cpu(con->in_reply.connect_seq),
  1269. con->connect_seq);
  1270. WARN_ON(con->connect_seq !=
  1271. le32_to_cpu(con->in_reply.connect_seq));
  1272. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1273. set_bit(LOSSYTX, &con->state);
  1274. prepare_read_tag(con);
  1275. break;
  1276. case CEPH_MSGR_TAG_WAIT:
  1277. /*
  1278. * If there is a connection race (we are opening
  1279. * connections to each other), one of us may just have
  1280. * to WAIT. This shouldn't happen if we are the
  1281. * client.
  1282. */
  1283. pr_err("process_connect got WAIT as client\n");
  1284. con->error_msg = "protocol error, got WAIT as client";
  1285. return -1;
  1286. default:
  1287. pr_err("connect protocol error, will retry\n");
  1288. con->error_msg = "protocol error, garbage tag during connect";
  1289. return -1;
  1290. }
  1291. return 0;
  1292. }
  1293. /*
  1294. * read (part of) an ack
  1295. */
  1296. static int read_partial_ack(struct ceph_connection *con)
  1297. {
  1298. int size = sizeof (con->in_temp_ack);
  1299. int end = size;
  1300. return read_partial(con, end, size, &con->in_temp_ack);
  1301. }
  1302. /*
  1303. * We can finally discard anything that's been acked.
  1304. */
  1305. static void process_ack(struct ceph_connection *con)
  1306. {
  1307. struct ceph_msg *m;
  1308. u64 ack = le64_to_cpu(con->in_temp_ack);
  1309. u64 seq;
  1310. while (!list_empty(&con->out_sent)) {
  1311. m = list_first_entry(&con->out_sent, struct ceph_msg,
  1312. list_head);
  1313. seq = le64_to_cpu(m->hdr.seq);
  1314. if (seq > ack)
  1315. break;
  1316. dout("got ack for seq %llu type %d at %p\n", seq,
  1317. le16_to_cpu(m->hdr.type), m);
  1318. m->ack_stamp = jiffies;
  1319. ceph_msg_remove(m);
  1320. }
  1321. prepare_read_tag(con);
  1322. }
  1323. static int read_partial_message_section(struct ceph_connection *con,
  1324. struct kvec *section,
  1325. unsigned int sec_len, u32 *crc)
  1326. {
  1327. int ret, left;
  1328. BUG_ON(!section);
  1329. while (section->iov_len < sec_len) {
  1330. BUG_ON(section->iov_base == NULL);
  1331. left = sec_len - section->iov_len;
  1332. ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
  1333. section->iov_len, left);
  1334. if (ret <= 0)
  1335. return ret;
  1336. section->iov_len += ret;
  1337. }
  1338. if (section->iov_len == sec_len)
  1339. *crc = crc32c(0, section->iov_base, section->iov_len);
  1340. return 1;
  1341. }
  1342. static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
  1343. struct ceph_msg_header *hdr,
  1344. int *skip);
  1345. static int read_partial_message_pages(struct ceph_connection *con,
  1346. struct page **pages,
  1347. unsigned data_len, bool do_datacrc)
  1348. {
  1349. void *p;
  1350. int ret;
  1351. int left;
  1352. left = min((int)(data_len - con->in_msg_pos.data_pos),
  1353. (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
  1354. /* (page) data */
  1355. BUG_ON(pages == NULL);
  1356. p = kmap(pages[con->in_msg_pos.page]);
  1357. ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
  1358. left);
  1359. if (ret > 0 && do_datacrc)
  1360. con->in_data_crc =
  1361. crc32c(con->in_data_crc,
  1362. p + con->in_msg_pos.page_pos, ret);
  1363. kunmap(pages[con->in_msg_pos.page]);
  1364. if (ret <= 0)
  1365. return ret;
  1366. con->in_msg_pos.data_pos += ret;
  1367. con->in_msg_pos.page_pos += ret;
  1368. if (con->in_msg_pos.page_pos == PAGE_SIZE) {
  1369. con->in_msg_pos.page_pos = 0;
  1370. con->in_msg_pos.page++;
  1371. }
  1372. return ret;
  1373. }
  1374. #ifdef CONFIG_BLOCK
  1375. static int read_partial_message_bio(struct ceph_connection *con,
  1376. struct bio **bio_iter, int *bio_seg,
  1377. unsigned data_len, bool do_datacrc)
  1378. {
  1379. struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
  1380. void *p;
  1381. int ret, left;
  1382. if (IS_ERR(bv))
  1383. return PTR_ERR(bv);
  1384. left = min((int)(data_len - con->in_msg_pos.data_pos),
  1385. (int)(bv->bv_len - con->in_msg_pos.page_pos));
  1386. p = kmap(bv->bv_page) + bv->bv_offset;
  1387. ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
  1388. left);
  1389. if (ret > 0 && do_datacrc)
  1390. con->in_data_crc =
  1391. crc32c(con->in_data_crc,
  1392. p + con->in_msg_pos.page_pos, ret);
  1393. kunmap(bv->bv_page);
  1394. if (ret <= 0)
  1395. return ret;
  1396. con->in_msg_pos.data_pos += ret;
  1397. con->in_msg_pos.page_pos += ret;
  1398. if (con->in_msg_pos.page_pos == bv->bv_len) {
  1399. con->in_msg_pos.page_pos = 0;
  1400. iter_bio_next(bio_iter, bio_seg);
  1401. }
  1402. return ret;
  1403. }
  1404. #endif
  1405. /*
  1406. * read (part of) a message.
  1407. */
  1408. static int read_partial_message(struct ceph_connection *con)
  1409. {
  1410. struct ceph_msg *m = con->in_msg;
  1411. int size;
  1412. int end;
  1413. int ret;
  1414. unsigned front_len, middle_len, data_len;
  1415. bool do_datacrc = !con->msgr->nocrc;
  1416. int skip;
  1417. u64 seq;
  1418. u32 crc;
  1419. dout("read_partial_message con %p msg %p\n", con, m);
  1420. /* header */
  1421. size = sizeof (con->in_hdr);
  1422. end = size;
  1423. ret = read_partial(con, end, size, &con->in_hdr);
  1424. if (ret <= 0)
  1425. return ret;
  1426. crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
  1427. if (cpu_to_le32(crc) != con->in_hdr.crc) {
  1428. pr_err("read_partial_message bad hdr "
  1429. " crc %u != expected %u\n",
  1430. crc, con->in_hdr.crc);
  1431. return -EBADMSG;
  1432. }
  1433. front_len = le32_to_cpu(con->in_hdr.front_len);
  1434. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  1435. return -EIO;
  1436. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1437. if (middle_len > CEPH_MSG_MAX_DATA_LEN)
  1438. return -EIO;
  1439. data_len = le32_to_cpu(con->in_hdr.data_len);
  1440. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  1441. return -EIO;
  1442. /* verify seq# */
  1443. seq = le64_to_cpu(con->in_hdr.seq);
  1444. if ((s64)seq - (s64)con->in_seq < 1) {
  1445. pr_info("skipping %s%lld %s seq %lld expected %lld\n",
  1446. ENTITY_NAME(con->peer_name),
  1447. ceph_pr_addr(&con->peer_addr.in_addr),
  1448. seq, con->in_seq + 1);
  1449. con->in_base_pos = -front_len - middle_len - data_len -
  1450. sizeof(m->footer);
  1451. con->in_tag = CEPH_MSGR_TAG_READY;
  1452. return 0;
  1453. } else if ((s64)seq - (s64)con->in_seq > 1) {
  1454. pr_err("read_partial_message bad seq %lld expected %lld\n",
  1455. seq, con->in_seq + 1);
  1456. con->error_msg = "bad message sequence # for incoming message";
  1457. return -EBADMSG;
  1458. }
  1459. /* allocate message? */
  1460. if (!con->in_msg) {
  1461. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  1462. con->in_hdr.front_len, con->in_hdr.data_len);
  1463. skip = 0;
  1464. con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
  1465. if (skip) {
  1466. /* skip this message */
  1467. dout("alloc_msg said skip message\n");
  1468. BUG_ON(con->in_msg);
  1469. con->in_base_pos = -front_len - middle_len - data_len -
  1470. sizeof(m->footer);
  1471. con->in_tag = CEPH_MSGR_TAG_READY;
  1472. con->in_seq++;
  1473. return 0;
  1474. }
  1475. if (!con->in_msg) {
  1476. con->error_msg =
  1477. "error allocating memory for incoming message";
  1478. return -ENOMEM;
  1479. }
  1480. m = con->in_msg;
  1481. m->front.iov_len = 0; /* haven't read it yet */
  1482. if (m->middle)
  1483. m->middle->vec.iov_len = 0;
  1484. con->in_msg_pos.page = 0;
  1485. if (m->pages)
  1486. con->in_msg_pos.page_pos = m->page_alignment;
  1487. else
  1488. con->in_msg_pos.page_pos = 0;
  1489. con->in_msg_pos.data_pos = 0;
  1490. }
  1491. /* front */
  1492. ret = read_partial_message_section(con, &m->front, front_len,
  1493. &con->in_front_crc);
  1494. if (ret <= 0)
  1495. return ret;
  1496. /* middle */
  1497. if (m->middle) {
  1498. ret = read_partial_message_section(con, &m->middle->vec,
  1499. middle_len,
  1500. &con->in_middle_crc);
  1501. if (ret <= 0)
  1502. return ret;
  1503. }
  1504. #ifdef CONFIG_BLOCK
  1505. if (m->bio && !m->bio_iter)
  1506. init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
  1507. #endif
  1508. /* (page) data */
  1509. while (con->in_msg_pos.data_pos < data_len) {
  1510. if (m->pages) {
  1511. ret = read_partial_message_pages(con, m->pages,
  1512. data_len, do_datacrc);
  1513. if (ret <= 0)
  1514. return ret;
  1515. #ifdef CONFIG_BLOCK
  1516. } else if (m->bio) {
  1517. ret = read_partial_message_bio(con,
  1518. &m->bio_iter, &m->bio_seg,
  1519. data_len, do_datacrc);
  1520. if (ret <= 0)
  1521. return ret;
  1522. #endif
  1523. } else {
  1524. BUG_ON(1);
  1525. }
  1526. }
  1527. /* footer */
  1528. size = sizeof (m->footer);
  1529. end += size;
  1530. ret = read_partial(con, end, size, &m->footer);
  1531. if (ret <= 0)
  1532. return ret;
  1533. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  1534. m, front_len, m->footer.front_crc, middle_len,
  1535. m->footer.middle_crc, data_len, m->footer.data_crc);
  1536. /* crc ok? */
  1537. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  1538. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  1539. m, con->in_front_crc, m->footer.front_crc);
  1540. return -EBADMSG;
  1541. }
  1542. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  1543. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  1544. m, con->in_middle_crc, m->footer.middle_crc);
  1545. return -EBADMSG;
  1546. }
  1547. if (do_datacrc &&
  1548. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  1549. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  1550. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  1551. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  1552. return -EBADMSG;
  1553. }
  1554. return 1; /* done! */
  1555. }
  1556. /*
  1557. * Process message. This happens in the worker thread. The callback should
  1558. * be careful not to do anything that waits on other incoming messages or it
  1559. * may deadlock.
  1560. */
  1561. static void process_message(struct ceph_connection *con)
  1562. {
  1563. struct ceph_msg *msg;
  1564. msg = con->in_msg;
  1565. con->in_msg = NULL;
  1566. /* if first message, set peer_name */
  1567. if (con->peer_name.type == 0)
  1568. con->peer_name = msg->hdr.src;
  1569. con->in_seq++;
  1570. mutex_unlock(&con->mutex);
  1571. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  1572. msg, le64_to_cpu(msg->hdr.seq),
  1573. ENTITY_NAME(msg->hdr.src),
  1574. le16_to_cpu(msg->hdr.type),
  1575. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1576. le32_to_cpu(msg->hdr.front_len),
  1577. le32_to_cpu(msg->hdr.data_len),
  1578. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  1579. con->ops->dispatch(con, msg);
  1580. mutex_lock(&con->mutex);
  1581. prepare_read_tag(con);
  1582. }
  1583. /*
  1584. * Write something to the socket. Called in a worker thread when the
  1585. * socket appears to be writeable and we have something ready to send.
  1586. */
  1587. static int try_write(struct ceph_connection *con)
  1588. {
  1589. int ret = 1;
  1590. dout("try_write start %p state %lu nref %d\n", con, con->state,
  1591. atomic_read(&con->nref));
  1592. more:
  1593. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  1594. /* open the socket first? */
  1595. if (con->sock == NULL) {
  1596. ceph_con_out_kvec_reset(con);
  1597. prepare_write_banner(con);
  1598. ret = prepare_write_connect(con);
  1599. if (ret < 0)
  1600. goto out;
  1601. prepare_read_banner(con);
  1602. set_bit(CONNECTING, &con->state);
  1603. clear_bit(NEGOTIATING, &con->state);
  1604. BUG_ON(con->in_msg);
  1605. con->in_tag = CEPH_MSGR_TAG_READY;
  1606. dout("try_write initiating connect on %p new state %lu\n",
  1607. con, con->state);
  1608. ret = ceph_tcp_connect(con);
  1609. if (ret < 0) {
  1610. con->error_msg = "connect error";
  1611. goto out;
  1612. }
  1613. }
  1614. more_kvec:
  1615. /* kvec data queued? */
  1616. if (con->out_skip) {
  1617. ret = write_partial_skip(con);
  1618. if (ret <= 0)
  1619. goto out;
  1620. }
  1621. if (con->out_kvec_left) {
  1622. ret = write_partial_kvec(con);
  1623. if (ret <= 0)
  1624. goto out;
  1625. }
  1626. /* msg pages? */
  1627. if (con->out_msg) {
  1628. if (con->out_msg_done) {
  1629. ceph_msg_put(con->out_msg);
  1630. con->out_msg = NULL; /* we're done with this one */
  1631. goto do_next;
  1632. }
  1633. ret = write_partial_msg_pages(con);
  1634. if (ret == 1)
  1635. goto more_kvec; /* we need to send the footer, too! */
  1636. if (ret == 0)
  1637. goto out;
  1638. if (ret < 0) {
  1639. dout("try_write write_partial_msg_pages err %d\n",
  1640. ret);
  1641. goto out;
  1642. }
  1643. }
  1644. do_next:
  1645. if (!test_bit(CONNECTING, &con->state)) {
  1646. /* is anything else pending? */
  1647. if (!list_empty(&con->out_queue)) {
  1648. prepare_write_message(con);
  1649. goto more;
  1650. }
  1651. if (con->in_seq > con->in_seq_acked) {
  1652. prepare_write_ack(con);
  1653. goto more;
  1654. }
  1655. if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
  1656. prepare_write_keepalive(con);
  1657. goto more;
  1658. }
  1659. }
  1660. /* Nothing to do! */
  1661. clear_bit(WRITE_PENDING, &con->state);
  1662. dout("try_write nothing else to write.\n");
  1663. ret = 0;
  1664. out:
  1665. dout("try_write done on %p ret %d\n", con, ret);
  1666. return ret;
  1667. }
  1668. /*
  1669. * Read what we can from the socket.
  1670. */
  1671. static int try_read(struct ceph_connection *con)
  1672. {
  1673. int ret = -1;
  1674. if (!con->sock)
  1675. return 0;
  1676. if (test_bit(STANDBY, &con->state))
  1677. return 0;
  1678. dout("try_read start on %p\n", con);
  1679. more:
  1680. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  1681. con->in_base_pos);
  1682. /*
  1683. * process_connect and process_message drop and re-take
  1684. * con->mutex. make sure we handle a racing close or reopen.
  1685. */
  1686. if (test_bit(CLOSED, &con->state) ||
  1687. test_bit(OPENING, &con->state)) {
  1688. ret = -EAGAIN;
  1689. goto out;
  1690. }
  1691. if (test_bit(CONNECTING, &con->state)) {
  1692. if (!test_bit(NEGOTIATING, &con->state)) {
  1693. dout("try_read connecting\n");
  1694. ret = read_partial_banner(con);
  1695. if (ret <= 0)
  1696. goto out;
  1697. ret = process_banner(con);
  1698. if (ret < 0)
  1699. goto out;
  1700. }
  1701. ret = read_partial_connect(con);
  1702. if (ret <= 0)
  1703. goto out;
  1704. ret = process_connect(con);
  1705. if (ret < 0)
  1706. goto out;
  1707. goto more;
  1708. }
  1709. if (con->in_base_pos < 0) {
  1710. /*
  1711. * skipping + discarding content.
  1712. *
  1713. * FIXME: there must be a better way to do this!
  1714. */
  1715. static char buf[SKIP_BUF_SIZE];
  1716. int skip = min((int) sizeof (buf), -con->in_base_pos);
  1717. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  1718. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  1719. if (ret <= 0)
  1720. goto out;
  1721. con->in_base_pos += ret;
  1722. if (con->in_base_pos)
  1723. goto more;
  1724. }
  1725. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  1726. /*
  1727. * what's next?
  1728. */
  1729. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  1730. if (ret <= 0)
  1731. goto out;
  1732. dout("try_read got tag %d\n", (int)con->in_tag);
  1733. switch (con->in_tag) {
  1734. case CEPH_MSGR_TAG_MSG:
  1735. prepare_read_message(con);
  1736. break;
  1737. case CEPH_MSGR_TAG_ACK:
  1738. prepare_read_ack(con);
  1739. break;
  1740. case CEPH_MSGR_TAG_CLOSE:
  1741. set_bit(CLOSED, &con->state); /* fixme */
  1742. goto out;
  1743. default:
  1744. goto bad_tag;
  1745. }
  1746. }
  1747. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  1748. ret = read_partial_message(con);
  1749. if (ret <= 0) {
  1750. switch (ret) {
  1751. case -EBADMSG:
  1752. con->error_msg = "bad crc";
  1753. ret = -EIO;
  1754. break;
  1755. case -EIO:
  1756. con->error_msg = "io error";
  1757. break;
  1758. }
  1759. goto out;
  1760. }
  1761. if (con->in_tag == CEPH_MSGR_TAG_READY)
  1762. goto more;
  1763. process_message(con);
  1764. goto more;
  1765. }
  1766. if (con->in_tag == CEPH_MSGR_TAG_ACK) {
  1767. ret = read_partial_ack(con);
  1768. if (ret <= 0)
  1769. goto out;
  1770. process_ack(con);
  1771. goto more;
  1772. }
  1773. out:
  1774. dout("try_read done on %p ret %d\n", con, ret);
  1775. return ret;
  1776. bad_tag:
  1777. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  1778. con->error_msg = "protocol error, garbage tag";
  1779. ret = -1;
  1780. goto out;
  1781. }
  1782. /*
  1783. * Atomically queue work on a connection. Bump @con reference to
  1784. * avoid races with connection teardown.
  1785. */
  1786. static void queue_con(struct ceph_connection *con)
  1787. {
  1788. if (test_bit(DEAD, &con->state)) {
  1789. dout("queue_con %p ignoring: DEAD\n",
  1790. con);
  1791. return;
  1792. }
  1793. if (!con->ops->get(con)) {
  1794. dout("queue_con %p ref count 0\n", con);
  1795. return;
  1796. }
  1797. if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
  1798. dout("queue_con %p - already queued\n", con);
  1799. con->ops->put(con);
  1800. } else {
  1801. dout("queue_con %p\n", con);
  1802. }
  1803. }
  1804. /*
  1805. * Do some work on a connection. Drop a connection ref when we're done.
  1806. */
  1807. static void con_work(struct work_struct *work)
  1808. {
  1809. struct ceph_connection *con = container_of(work, struct ceph_connection,
  1810. work.work);
  1811. int ret;
  1812. mutex_lock(&con->mutex);
  1813. restart:
  1814. if (test_and_clear_bit(BACKOFF, &con->state)) {
  1815. dout("con_work %p backing off\n", con);
  1816. if (queue_delayed_work(ceph_msgr_wq, &con->work,
  1817. round_jiffies_relative(con->delay))) {
  1818. dout("con_work %p backoff %lu\n", con, con->delay);
  1819. mutex_unlock(&con->mutex);
  1820. return;
  1821. } else {
  1822. con->ops->put(con);
  1823. dout("con_work %p FAILED to back off %lu\n", con,
  1824. con->delay);
  1825. }
  1826. }
  1827. if (test_bit(STANDBY, &con->state)) {
  1828. dout("con_work %p STANDBY\n", con);
  1829. goto done;
  1830. }
  1831. if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
  1832. dout("con_work CLOSED\n");
  1833. con_close_socket(con);
  1834. goto done;
  1835. }
  1836. if (test_and_clear_bit(OPENING, &con->state)) {
  1837. /* reopen w/ new peer */
  1838. dout("con_work OPENING\n");
  1839. con_close_socket(con);
  1840. }
  1841. if (test_and_clear_bit(SOCK_CLOSED, &con->state))
  1842. goto fault;
  1843. ret = try_read(con);
  1844. if (ret == -EAGAIN)
  1845. goto restart;
  1846. if (ret < 0)
  1847. goto fault;
  1848. ret = try_write(con);
  1849. if (ret == -EAGAIN)
  1850. goto restart;
  1851. if (ret < 0)
  1852. goto fault;
  1853. done:
  1854. mutex_unlock(&con->mutex);
  1855. done_unlocked:
  1856. con->ops->put(con);
  1857. return;
  1858. fault:
  1859. mutex_unlock(&con->mutex);
  1860. ceph_fault(con); /* error/fault path */
  1861. goto done_unlocked;
  1862. }
  1863. /*
  1864. * Generic error/fault handler. A retry mechanism is used with
  1865. * exponential backoff
  1866. */
  1867. static void ceph_fault(struct ceph_connection *con)
  1868. {
  1869. pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  1870. ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
  1871. dout("fault %p state %lu to peer %s\n",
  1872. con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
  1873. if (test_bit(LOSSYTX, &con->state)) {
  1874. dout("fault on LOSSYTX channel\n");
  1875. goto out;
  1876. }
  1877. mutex_lock(&con->mutex);
  1878. if (test_bit(CLOSED, &con->state))
  1879. goto out_unlock;
  1880. con_close_socket(con);
  1881. if (con->in_msg) {
  1882. ceph_msg_put(con->in_msg);
  1883. con->in_msg = NULL;
  1884. }
  1885. /* Requeue anything that hasn't been acked */
  1886. list_splice_init(&con->out_sent, &con->out_queue);
  1887. /* If there are no messages queued or keepalive pending, place
  1888. * the connection in a STANDBY state */
  1889. if (list_empty(&con->out_queue) &&
  1890. !test_bit(KEEPALIVE_PENDING, &con->state)) {
  1891. dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
  1892. clear_bit(WRITE_PENDING, &con->state);
  1893. set_bit(STANDBY, &con->state);
  1894. } else {
  1895. /* retry after a delay. */
  1896. if (con->delay == 0)
  1897. con->delay = BASE_DELAY_INTERVAL;
  1898. else if (con->delay < MAX_DELAY_INTERVAL)
  1899. con->delay *= 2;
  1900. con->ops->get(con);
  1901. if (queue_delayed_work(ceph_msgr_wq, &con->work,
  1902. round_jiffies_relative(con->delay))) {
  1903. dout("fault queued %p delay %lu\n", con, con->delay);
  1904. } else {
  1905. con->ops->put(con);
  1906. dout("fault failed to queue %p delay %lu, backoff\n",
  1907. con, con->delay);
  1908. /*
  1909. * In many cases we see a socket state change
  1910. * while con_work is running and end up
  1911. * queuing (non-delayed) work, such that we
  1912. * can't backoff with a delay. Set a flag so
  1913. * that when con_work restarts we schedule the
  1914. * delay then.
  1915. */
  1916. set_bit(BACKOFF, &con->state);
  1917. }
  1918. }
  1919. out_unlock:
  1920. mutex_unlock(&con->mutex);
  1921. out:
  1922. /*
  1923. * in case we faulted due to authentication, invalidate our
  1924. * current tickets so that we can get new ones.
  1925. */
  1926. if (con->auth_retry && con->ops->invalidate_authorizer) {
  1927. dout("calling invalidate_authorizer()\n");
  1928. con->ops->invalidate_authorizer(con);
  1929. }
  1930. if (con->ops->fault)
  1931. con->ops->fault(con);
  1932. }
  1933. /*
  1934. * create a new messenger instance
  1935. */
  1936. struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr,
  1937. u32 supported_features,
  1938. u32 required_features)
  1939. {
  1940. struct ceph_messenger *msgr;
  1941. msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
  1942. if (msgr == NULL)
  1943. return ERR_PTR(-ENOMEM);
  1944. msgr->supported_features = supported_features;
  1945. msgr->required_features = required_features;
  1946. spin_lock_init(&msgr->global_seq_lock);
  1947. if (myaddr)
  1948. msgr->inst.addr = *myaddr;
  1949. /* select a random nonce */
  1950. msgr->inst.addr.type = 0;
  1951. get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
  1952. encode_my_addr(msgr);
  1953. dout("messenger_create %p\n", msgr);
  1954. return msgr;
  1955. }
  1956. EXPORT_SYMBOL(ceph_messenger_create);
  1957. void ceph_messenger_destroy(struct ceph_messenger *msgr)
  1958. {
  1959. dout("destroy %p\n", msgr);
  1960. kfree(msgr);
  1961. dout("destroyed messenger %p\n", msgr);
  1962. }
  1963. EXPORT_SYMBOL(ceph_messenger_destroy);
  1964. static void clear_standby(struct ceph_connection *con)
  1965. {
  1966. /* come back from STANDBY? */
  1967. if (test_and_clear_bit(STANDBY, &con->state)) {
  1968. mutex_lock(&con->mutex);
  1969. dout("clear_standby %p and ++connect_seq\n", con);
  1970. con->connect_seq++;
  1971. WARN_ON(test_bit(WRITE_PENDING, &con->state));
  1972. WARN_ON(test_bit(KEEPALIVE_PENDING, &con->state));
  1973. mutex_unlock(&con->mutex);
  1974. }
  1975. }
  1976. /*
  1977. * Queue up an outgoing message on the given connection.
  1978. */
  1979. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  1980. {
  1981. if (test_bit(CLOSED, &con->state)) {
  1982. dout("con_send %p closed, dropping %p\n", con, msg);
  1983. ceph_msg_put(msg);
  1984. return;
  1985. }
  1986. /* set src+dst */
  1987. msg->hdr.src = con->msgr->inst.name;
  1988. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  1989. msg->needs_out_seq = true;
  1990. /* queue */
  1991. mutex_lock(&con->mutex);
  1992. BUG_ON(!list_empty(&msg->list_head));
  1993. list_add_tail(&msg->list_head, &con->out_queue);
  1994. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  1995. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  1996. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1997. le32_to_cpu(msg->hdr.front_len),
  1998. le32_to_cpu(msg->hdr.middle_len),
  1999. le32_to_cpu(msg->hdr.data_len));
  2000. mutex_unlock(&con->mutex);
  2001. /* if there wasn't anything waiting to send before, queue
  2002. * new work */
  2003. clear_standby(con);
  2004. if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
  2005. queue_con(con);
  2006. }
  2007. EXPORT_SYMBOL(ceph_con_send);
  2008. /*
  2009. * Revoke a message that was previously queued for send
  2010. */
  2011. void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
  2012. {
  2013. mutex_lock(&con->mutex);
  2014. if (!list_empty(&msg->list_head)) {
  2015. dout("con_revoke %p msg %p - was on queue\n", con, msg);
  2016. list_del_init(&msg->list_head);
  2017. ceph_msg_put(msg);
  2018. msg->hdr.seq = 0;
  2019. }
  2020. if (con->out_msg == msg) {
  2021. dout("con_revoke %p msg %p - was sending\n", con, msg);
  2022. con->out_msg = NULL;
  2023. if (con->out_kvec_is_msg) {
  2024. con->out_skip = con->out_kvec_bytes;
  2025. con->out_kvec_is_msg = false;
  2026. }
  2027. ceph_msg_put(msg);
  2028. msg->hdr.seq = 0;
  2029. }
  2030. mutex_unlock(&con->mutex);
  2031. }
  2032. /*
  2033. * Revoke a message that we may be reading data into
  2034. */
  2035. void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
  2036. {
  2037. mutex_lock(&con->mutex);
  2038. if (con->in_msg && con->in_msg == msg) {
  2039. unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
  2040. unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
  2041. unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
  2042. /* skip rest of message */
  2043. dout("con_revoke_pages %p msg %p revoked\n", con, msg);
  2044. con->in_base_pos = con->in_base_pos -
  2045. sizeof(struct ceph_msg_header) -
  2046. front_len -
  2047. middle_len -
  2048. data_len -
  2049. sizeof(struct ceph_msg_footer);
  2050. ceph_msg_put(con->in_msg);
  2051. con->in_msg = NULL;
  2052. con->in_tag = CEPH_MSGR_TAG_READY;
  2053. con->in_seq++;
  2054. } else {
  2055. dout("con_revoke_pages %p msg %p pages %p no-op\n",
  2056. con, con->in_msg, msg);
  2057. }
  2058. mutex_unlock(&con->mutex);
  2059. }
  2060. /*
  2061. * Queue a keepalive byte to ensure the tcp connection is alive.
  2062. */
  2063. void ceph_con_keepalive(struct ceph_connection *con)
  2064. {
  2065. dout("con_keepalive %p\n", con);
  2066. clear_standby(con);
  2067. if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
  2068. test_and_set_bit(WRITE_PENDING, &con->state) == 0)
  2069. queue_con(con);
  2070. }
  2071. EXPORT_SYMBOL(ceph_con_keepalive);
  2072. /*
  2073. * construct a new message with given type, size
  2074. * the new msg has a ref count of 1.
  2075. */
  2076. struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  2077. bool can_fail)
  2078. {
  2079. struct ceph_msg *m;
  2080. m = kmalloc(sizeof(*m), flags);
  2081. if (m == NULL)
  2082. goto out;
  2083. kref_init(&m->kref);
  2084. INIT_LIST_HEAD(&m->list_head);
  2085. m->hdr.tid = 0;
  2086. m->hdr.type = cpu_to_le16(type);
  2087. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  2088. m->hdr.version = 0;
  2089. m->hdr.front_len = cpu_to_le32(front_len);
  2090. m->hdr.middle_len = 0;
  2091. m->hdr.data_len = 0;
  2092. m->hdr.data_off = 0;
  2093. m->hdr.reserved = 0;
  2094. m->footer.front_crc = 0;
  2095. m->footer.middle_crc = 0;
  2096. m->footer.data_crc = 0;
  2097. m->footer.flags = 0;
  2098. m->front_max = front_len;
  2099. m->front_is_vmalloc = false;
  2100. m->more_to_follow = false;
  2101. m->ack_stamp = 0;
  2102. m->pool = NULL;
  2103. /* middle */
  2104. m->middle = NULL;
  2105. /* data */
  2106. m->nr_pages = 0;
  2107. m->page_alignment = 0;
  2108. m->pages = NULL;
  2109. m->pagelist = NULL;
  2110. m->bio = NULL;
  2111. m->bio_iter = NULL;
  2112. m->bio_seg = 0;
  2113. m->trail = NULL;
  2114. /* front */
  2115. if (front_len) {
  2116. if (front_len > PAGE_CACHE_SIZE) {
  2117. m->front.iov_base = __vmalloc(front_len, flags,
  2118. PAGE_KERNEL);
  2119. m->front_is_vmalloc = true;
  2120. } else {
  2121. m->front.iov_base = kmalloc(front_len, flags);
  2122. }
  2123. if (m->front.iov_base == NULL) {
  2124. dout("ceph_msg_new can't allocate %d bytes\n",
  2125. front_len);
  2126. goto out2;
  2127. }
  2128. } else {
  2129. m->front.iov_base = NULL;
  2130. }
  2131. m->front.iov_len = front_len;
  2132. dout("ceph_msg_new %p front %d\n", m, front_len);
  2133. return m;
  2134. out2:
  2135. ceph_msg_put(m);
  2136. out:
  2137. if (!can_fail) {
  2138. pr_err("msg_new can't create type %d front %d\n", type,
  2139. front_len);
  2140. WARN_ON(1);
  2141. } else {
  2142. dout("msg_new can't create type %d front %d\n", type,
  2143. front_len);
  2144. }
  2145. return NULL;
  2146. }
  2147. EXPORT_SYMBOL(ceph_msg_new);
  2148. /*
  2149. * Allocate "middle" portion of a message, if it is needed and wasn't
  2150. * allocated by alloc_msg. This allows us to read a small fixed-size
  2151. * per-type header in the front and then gracefully fail (i.e.,
  2152. * propagate the error to the caller based on info in the front) when
  2153. * the middle is too large.
  2154. */
  2155. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  2156. {
  2157. int type = le16_to_cpu(msg->hdr.type);
  2158. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  2159. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  2160. ceph_msg_type_name(type), middle_len);
  2161. BUG_ON(!middle_len);
  2162. BUG_ON(msg->middle);
  2163. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  2164. if (!msg->middle)
  2165. return -ENOMEM;
  2166. return 0;
  2167. }
  2168. /*
  2169. * Generic message allocator, for incoming messages.
  2170. */
  2171. static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
  2172. struct ceph_msg_header *hdr,
  2173. int *skip)
  2174. {
  2175. int type = le16_to_cpu(hdr->type);
  2176. int front_len = le32_to_cpu(hdr->front_len);
  2177. int middle_len = le32_to_cpu(hdr->middle_len);
  2178. struct ceph_msg *msg = NULL;
  2179. int ret;
  2180. if (con->ops->alloc_msg) {
  2181. mutex_unlock(&con->mutex);
  2182. msg = con->ops->alloc_msg(con, hdr, skip);
  2183. mutex_lock(&con->mutex);
  2184. if (!msg || *skip)
  2185. return NULL;
  2186. }
  2187. if (!msg) {
  2188. *skip = 0;
  2189. msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
  2190. if (!msg) {
  2191. pr_err("unable to allocate msg type %d len %d\n",
  2192. type, front_len);
  2193. return NULL;
  2194. }
  2195. msg->page_alignment = le16_to_cpu(hdr->data_off);
  2196. }
  2197. memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
  2198. if (middle_len && !msg->middle) {
  2199. ret = ceph_alloc_middle(con, msg);
  2200. if (ret < 0) {
  2201. ceph_msg_put(msg);
  2202. return NULL;
  2203. }
  2204. }
  2205. return msg;
  2206. }
  2207. /*
  2208. * Free a generically kmalloc'd message.
  2209. */
  2210. void ceph_msg_kfree(struct ceph_msg *m)
  2211. {
  2212. dout("msg_kfree %p\n", m);
  2213. if (m->front_is_vmalloc)
  2214. vfree(m->front.iov_base);
  2215. else
  2216. kfree(m->front.iov_base);
  2217. kfree(m);
  2218. }
  2219. /*
  2220. * Drop a msg ref. Destroy as needed.
  2221. */
  2222. void ceph_msg_last_put(struct kref *kref)
  2223. {
  2224. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  2225. dout("ceph_msg_put last one on %p\n", m);
  2226. WARN_ON(!list_empty(&m->list_head));
  2227. /* drop middle, data, if any */
  2228. if (m->middle) {
  2229. ceph_buffer_put(m->middle);
  2230. m->middle = NULL;
  2231. }
  2232. m->nr_pages = 0;
  2233. m->pages = NULL;
  2234. if (m->pagelist) {
  2235. ceph_pagelist_release(m->pagelist);
  2236. kfree(m->pagelist);
  2237. m->pagelist = NULL;
  2238. }
  2239. m->trail = NULL;
  2240. if (m->pool)
  2241. ceph_msgpool_put(m->pool, m);
  2242. else
  2243. ceph_msg_kfree(m);
  2244. }
  2245. EXPORT_SYMBOL(ceph_msg_last_put);
  2246. void ceph_msg_dump(struct ceph_msg *msg)
  2247. {
  2248. pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
  2249. msg->front_max, msg->nr_pages);
  2250. print_hex_dump(KERN_DEBUG, "header: ",
  2251. DUMP_PREFIX_OFFSET, 16, 1,
  2252. &msg->hdr, sizeof(msg->hdr), true);
  2253. print_hex_dump(KERN_DEBUG, " front: ",
  2254. DUMP_PREFIX_OFFSET, 16, 1,
  2255. msg->front.iov_base, msg->front.iov_len, true);
  2256. if (msg->middle)
  2257. print_hex_dump(KERN_DEBUG, "middle: ",
  2258. DUMP_PREFIX_OFFSET, 16, 1,
  2259. msg->middle->vec.iov_base,
  2260. msg->middle->vec.iov_len, true);
  2261. print_hex_dump(KERN_DEBUG, "footer: ",
  2262. DUMP_PREFIX_OFFSET, 16, 1,
  2263. &msg->footer, sizeof(msg->footer), true);
  2264. }
  2265. EXPORT_SYMBOL(ceph_msg_dump);