messenger.c 61 KB

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