messenger.c 69 KB

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