messenger.c 83 KB

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