messenger.c 71 KB

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