messenger.c 71 KB

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