messenger.c 64 KB

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