messenger.c 61 KB

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