messenger.c 64 KB

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