messenger.c 69 KB

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