messenger.c 63 KB

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