messenger.c 69 KB

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