messenger.c 69 KB

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