messenger.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373
  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. static void init_bio_iter(struct bio *bio, struct bio **bio_iter,
  606. unsigned int *bio_seg)
  607. {
  608. if (!bio) {
  609. *bio_iter = NULL;
  610. *bio_seg = 0;
  611. return;
  612. }
  613. *bio_iter = bio;
  614. *bio_seg = (unsigned int) bio->bi_idx;
  615. }
  616. static void iter_bio_next(struct bio **bio_iter, unsigned int *seg)
  617. {
  618. if (*bio_iter == NULL)
  619. return;
  620. BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
  621. (*seg)++;
  622. if (*seg == (*bio_iter)->bi_vcnt)
  623. init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
  624. }
  625. /*
  626. * For a bio data item, a piece is whatever remains of the next
  627. * entry in the current bio iovec, or the first entry in the next
  628. * bio in the list.
  629. */
  630. static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data *data)
  631. {
  632. struct ceph_msg_data_cursor *cursor = &data->cursor;
  633. struct bio *bio;
  634. BUG_ON(data->type != CEPH_MSG_DATA_BIO);
  635. bio = data->bio;
  636. BUG_ON(!bio);
  637. BUG_ON(!bio->bi_vcnt);
  638. /* resid = bio->bi_size */
  639. cursor->bio = bio;
  640. cursor->vector_index = 0;
  641. cursor->vector_offset = 0;
  642. cursor->last_piece = !bio->bi_next && bio->bi_vcnt == 1;
  643. }
  644. static struct page *ceph_msg_data_bio_next(struct ceph_msg_data *data,
  645. size_t *page_offset,
  646. size_t *length)
  647. {
  648. struct ceph_msg_data_cursor *cursor = &data->cursor;
  649. struct bio *bio;
  650. struct bio_vec *bio_vec;
  651. unsigned int index;
  652. BUG_ON(data->type != CEPH_MSG_DATA_BIO);
  653. bio = cursor->bio;
  654. BUG_ON(!bio);
  655. index = cursor->vector_index;
  656. BUG_ON(index >= (unsigned int) bio->bi_vcnt);
  657. bio_vec = &bio->bi_io_vec[index];
  658. BUG_ON(cursor->vector_offset >= bio_vec->bv_len);
  659. *page_offset = (size_t) (bio_vec->bv_offset + cursor->vector_offset);
  660. BUG_ON(*page_offset >= PAGE_SIZE);
  661. *length = (size_t) (bio_vec->bv_len - cursor->vector_offset);
  662. BUG_ON(*length > PAGE_SIZE);
  663. return bio_vec->bv_page;
  664. }
  665. static bool ceph_msg_data_bio_advance(struct ceph_msg_data *data, size_t bytes)
  666. {
  667. struct ceph_msg_data_cursor *cursor = &data->cursor;
  668. struct bio *bio;
  669. struct bio_vec *bio_vec;
  670. unsigned int index;
  671. BUG_ON(data->type != CEPH_MSG_DATA_BIO);
  672. bio = cursor->bio;
  673. BUG_ON(!bio);
  674. index = cursor->vector_index;
  675. BUG_ON(index >= (unsigned int) bio->bi_vcnt);
  676. bio_vec = &bio->bi_io_vec[index];
  677. BUG_ON(cursor->vector_offset + bytes > bio_vec->bv_len);
  678. /* Advance the cursor offset */
  679. cursor->vector_offset += bytes;
  680. if (cursor->vector_offset < bio_vec->bv_len)
  681. return false; /* more bytes to process in this segment */
  682. /* Move on to the next segment, and possibly the next bio */
  683. if (++cursor->vector_index == (unsigned int) bio->bi_vcnt) {
  684. bio = bio->bi_next;
  685. cursor->bio = bio;
  686. cursor->vector_index = 0;
  687. }
  688. cursor->vector_offset = 0;
  689. if (!cursor->last_piece && bio && !bio->bi_next)
  690. if (cursor->vector_index == (unsigned int) bio->bi_vcnt - 1)
  691. cursor->last_piece = true;
  692. return true;
  693. }
  694. #endif
  695. /*
  696. * For a page array, a piece comes from the first page in the array
  697. * that has not already been fully consumed.
  698. */
  699. static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data *data)
  700. {
  701. struct ceph_msg_data_cursor *cursor = &data->cursor;
  702. int page_count;
  703. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  704. BUG_ON(!data->pages);
  705. BUG_ON(!data->length);
  706. page_count = calc_pages_for(data->alignment, (u64)data->length);
  707. BUG_ON(page_count > (int) USHRT_MAX);
  708. cursor->resid = data->length;
  709. cursor->page_offset = data->alignment & ~PAGE_MASK;
  710. cursor->page_index = 0;
  711. cursor->page_count = (unsigned short) page_count;
  712. cursor->last_piece = cursor->page_count == 1;
  713. }
  714. static struct page *ceph_msg_data_pages_next(struct ceph_msg_data *data,
  715. size_t *page_offset,
  716. size_t *length)
  717. {
  718. struct ceph_msg_data_cursor *cursor = &data->cursor;
  719. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  720. BUG_ON(cursor->page_index >= cursor->page_count);
  721. BUG_ON(cursor->page_offset >= PAGE_SIZE);
  722. BUG_ON(!cursor->resid);
  723. *page_offset = cursor->page_offset;
  724. if (cursor->last_piece) {
  725. BUG_ON(*page_offset + cursor->resid > PAGE_SIZE);
  726. *length = cursor->resid;
  727. } else {
  728. *length = PAGE_SIZE - *page_offset;
  729. }
  730. return data->pages[cursor->page_index];
  731. }
  732. static bool ceph_msg_data_pages_advance(struct ceph_msg_data *data,
  733. size_t bytes)
  734. {
  735. struct ceph_msg_data_cursor *cursor = &data->cursor;
  736. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  737. BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
  738. BUG_ON(bytes > cursor->resid);
  739. /* Advance the cursor page offset */
  740. cursor->resid -= bytes;
  741. cursor->page_offset += bytes;
  742. if (!bytes || cursor->page_offset & ~PAGE_MASK)
  743. return false; /* more bytes to process in the current page */
  744. /* Move on to the next page */
  745. BUG_ON(cursor->page_index >= cursor->page_count);
  746. cursor->page_offset = 0;
  747. cursor->page_index++;
  748. cursor->last_piece = cursor->page_index == cursor->page_count - 1;
  749. return true;
  750. }
  751. /*
  752. * For a pagelist, a piece is whatever remains to be consumed in the
  753. * first page in the list, or the front of the next page.
  754. */
  755. static void ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data *data)
  756. {
  757. struct ceph_msg_data_cursor *cursor = &data->cursor;
  758. struct ceph_pagelist *pagelist;
  759. struct page *page;
  760. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  761. pagelist = data->pagelist;
  762. BUG_ON(!pagelist);
  763. if (!pagelist->length)
  764. return; /* pagelist can be assigned but empty */
  765. BUG_ON(list_empty(&pagelist->head));
  766. page = list_first_entry(&pagelist->head, struct page, lru);
  767. cursor->page = page;
  768. cursor->offset = 0;
  769. cursor->last_piece = pagelist->length <= PAGE_SIZE;
  770. }
  771. static struct page *ceph_msg_data_pagelist_next(struct ceph_msg_data *data,
  772. size_t *page_offset,
  773. size_t *length)
  774. {
  775. struct ceph_msg_data_cursor *cursor = &data->cursor;
  776. struct ceph_pagelist *pagelist;
  777. size_t piece_end;
  778. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  779. pagelist = data->pagelist;
  780. BUG_ON(!pagelist);
  781. BUG_ON(!cursor->page);
  782. BUG_ON(cursor->offset >= pagelist->length);
  783. if (cursor->last_piece) {
  784. /* pagelist offset is always 0 */
  785. piece_end = pagelist->length & ~PAGE_MASK;
  786. if (!piece_end)
  787. piece_end = PAGE_SIZE;
  788. } else {
  789. piece_end = PAGE_SIZE;
  790. }
  791. *page_offset = cursor->offset & ~PAGE_MASK;
  792. *length = piece_end - *page_offset;
  793. return data->cursor.page;
  794. }
  795. static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data *data,
  796. size_t bytes)
  797. {
  798. struct ceph_msg_data_cursor *cursor = &data->cursor;
  799. struct ceph_pagelist *pagelist;
  800. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  801. pagelist = data->pagelist;
  802. BUG_ON(!pagelist);
  803. BUG_ON(!cursor->page);
  804. BUG_ON(cursor->offset + bytes > pagelist->length);
  805. BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
  806. /* Advance the cursor offset */
  807. cursor->offset += bytes;
  808. /* pagelist offset is always 0 */
  809. if (!bytes || cursor->offset & ~PAGE_MASK)
  810. return false; /* more bytes to process in the current page */
  811. /* Move on to the next page */
  812. BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
  813. cursor->page = list_entry_next(cursor->page, lru);
  814. /* cursor offset is at page boundary; pagelist offset is always 0 */
  815. if (pagelist->length - cursor->offset <= PAGE_SIZE)
  816. cursor->last_piece = true;
  817. return true;
  818. }
  819. /*
  820. * Message data is handled (sent or received) in pieces, where each
  821. * piece resides on a single page. The network layer might not
  822. * consume an entire piece at once. A data item's cursor keeps
  823. * track of which piece is next to process and how much remains to
  824. * be processed in that piece. It also tracks whether the current
  825. * piece is the last one in the data item.
  826. */
  827. static void ceph_msg_data_cursor_init(struct ceph_msg_data *data)
  828. {
  829. switch (data->type) {
  830. case CEPH_MSG_DATA_PAGELIST:
  831. ceph_msg_data_pagelist_cursor_init(data);
  832. break;
  833. case CEPH_MSG_DATA_PAGES:
  834. ceph_msg_data_pages_cursor_init(data);
  835. break;
  836. #ifdef CONFIG_BLOCK
  837. case CEPH_MSG_DATA_BIO:
  838. ceph_msg_data_bio_cursor_init(data);
  839. break;
  840. #endif /* CONFIG_BLOCK */
  841. case CEPH_MSG_DATA_NONE:
  842. default:
  843. /* BUG(); */
  844. break;
  845. }
  846. }
  847. /*
  848. * Return the page containing the next piece to process for a given
  849. * data item, and supply the page offset and length of that piece.
  850. * Indicate whether this is the last piece in this data item.
  851. */
  852. static struct page *ceph_msg_data_next(struct ceph_msg_data *data,
  853. size_t *page_offset,
  854. size_t *length,
  855. bool *last_piece)
  856. {
  857. struct page *page;
  858. switch (data->type) {
  859. case CEPH_MSG_DATA_PAGELIST:
  860. page = ceph_msg_data_pagelist_next(data, page_offset, length);
  861. break;
  862. case CEPH_MSG_DATA_PAGES:
  863. page = ceph_msg_data_pages_next(data, page_offset, length);
  864. break;
  865. #ifdef CONFIG_BLOCK
  866. case CEPH_MSG_DATA_BIO:
  867. page = ceph_msg_data_bio_next(data, page_offset, length);
  868. break;
  869. #endif /* CONFIG_BLOCK */
  870. case CEPH_MSG_DATA_NONE:
  871. default:
  872. page = NULL;
  873. break;
  874. }
  875. BUG_ON(!page);
  876. BUG_ON(*page_offset + *length > PAGE_SIZE);
  877. BUG_ON(!*length);
  878. if (last_piece)
  879. *last_piece = data->cursor.last_piece;
  880. return page;
  881. }
  882. /*
  883. * Returns true if the result moves the cursor on to the next piece
  884. * of the data item.
  885. */
  886. static bool ceph_msg_data_advance(struct ceph_msg_data *data, size_t bytes)
  887. {
  888. bool new_piece;
  889. switch (data->type) {
  890. case CEPH_MSG_DATA_PAGELIST:
  891. new_piece = ceph_msg_data_pagelist_advance(data, bytes);
  892. break;
  893. case CEPH_MSG_DATA_PAGES:
  894. new_piece = ceph_msg_data_pages_advance(data, bytes);
  895. break;
  896. #ifdef CONFIG_BLOCK
  897. case CEPH_MSG_DATA_BIO:
  898. new_piece = ceph_msg_data_bio_advance(data, bytes);
  899. break;
  900. #endif /* CONFIG_BLOCK */
  901. case CEPH_MSG_DATA_NONE:
  902. default:
  903. BUG();
  904. break;
  905. }
  906. return new_piece;
  907. }
  908. static void prepare_message_data(struct ceph_msg *msg,
  909. struct ceph_msg_pos *msg_pos)
  910. {
  911. BUG_ON(!msg);
  912. BUG_ON(!msg->hdr.data_len);
  913. /* initialize page iterator */
  914. msg_pos->page = 0;
  915. if (ceph_msg_has_pages(msg))
  916. msg_pos->page_pos = msg->p.alignment;
  917. else
  918. msg_pos->page_pos = 0;
  919. #ifdef CONFIG_BLOCK
  920. if (ceph_msg_has_bio(msg))
  921. init_bio_iter(msg->b.bio, &msg->b.bio_iter, &msg->b.bio_seg);
  922. #endif
  923. msg_pos->data_pos = 0;
  924. /* Initialize data cursors */
  925. #ifdef CONFIG_BLOCK
  926. if (ceph_msg_has_bio(msg))
  927. ceph_msg_data_cursor_init(&msg->b);
  928. #endif /* CONFIG_BLOCK */
  929. if (ceph_msg_has_pages(msg))
  930. ceph_msg_data_cursor_init(&msg->p);
  931. if (ceph_msg_has_pagelist(msg))
  932. ceph_msg_data_cursor_init(&msg->l);
  933. msg_pos->did_page_crc = false;
  934. }
  935. /*
  936. * Prepare footer for currently outgoing message, and finish things
  937. * off. Assumes out_kvec* are already valid.. we just add on to the end.
  938. */
  939. static void prepare_write_message_footer(struct ceph_connection *con)
  940. {
  941. struct ceph_msg *m = con->out_msg;
  942. int v = con->out_kvec_left;
  943. m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
  944. dout("prepare_write_message_footer %p\n", con);
  945. con->out_kvec_is_msg = true;
  946. con->out_kvec[v].iov_base = &m->footer;
  947. con->out_kvec[v].iov_len = sizeof(m->footer);
  948. con->out_kvec_bytes += sizeof(m->footer);
  949. con->out_kvec_left++;
  950. con->out_more = m->more_to_follow;
  951. con->out_msg_done = true;
  952. }
  953. /*
  954. * Prepare headers for the next outgoing message.
  955. */
  956. static void prepare_write_message(struct ceph_connection *con)
  957. {
  958. struct ceph_msg *m;
  959. u32 crc;
  960. con_out_kvec_reset(con);
  961. con->out_kvec_is_msg = true;
  962. con->out_msg_done = false;
  963. /* Sneak an ack in there first? If we can get it into the same
  964. * TCP packet that's a good thing. */
  965. if (con->in_seq > con->in_seq_acked) {
  966. con->in_seq_acked = con->in_seq;
  967. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  968. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  969. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  970. &con->out_temp_ack);
  971. }
  972. BUG_ON(list_empty(&con->out_queue));
  973. m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
  974. con->out_msg = m;
  975. BUG_ON(m->con != con);
  976. /* put message on sent list */
  977. ceph_msg_get(m);
  978. list_move_tail(&m->list_head, &con->out_sent);
  979. /*
  980. * only assign outgoing seq # if we haven't sent this message
  981. * yet. if it is requeued, resend with it's original seq.
  982. */
  983. if (m->needs_out_seq) {
  984. m->hdr.seq = cpu_to_le64(++con->out_seq);
  985. m->needs_out_seq = false;
  986. }
  987. dout("prepare_write_message %p seq %lld type %d len %d+%d+%d (%zd)\n",
  988. m, con->out_seq, le16_to_cpu(m->hdr.type),
  989. le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
  990. le32_to_cpu(m->hdr.data_len), m->p.length);
  991. BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
  992. /* tag + hdr + front + middle */
  993. con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
  994. con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
  995. con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
  996. if (m->middle)
  997. con_out_kvec_add(con, m->middle->vec.iov_len,
  998. m->middle->vec.iov_base);
  999. /* fill in crc (except data pages), footer */
  1000. crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
  1001. con->out_msg->hdr.crc = cpu_to_le32(crc);
  1002. con->out_msg->footer.flags = 0;
  1003. crc = crc32c(0, m->front.iov_base, m->front.iov_len);
  1004. con->out_msg->footer.front_crc = cpu_to_le32(crc);
  1005. if (m->middle) {
  1006. crc = crc32c(0, m->middle->vec.iov_base,
  1007. m->middle->vec.iov_len);
  1008. con->out_msg->footer.middle_crc = cpu_to_le32(crc);
  1009. } else
  1010. con->out_msg->footer.middle_crc = 0;
  1011. dout("%s front_crc %u middle_crc %u\n", __func__,
  1012. le32_to_cpu(con->out_msg->footer.front_crc),
  1013. le32_to_cpu(con->out_msg->footer.middle_crc));
  1014. /* is there a data payload? */
  1015. con->out_msg->footer.data_crc = 0;
  1016. if (m->hdr.data_len) {
  1017. prepare_message_data(con->out_msg, &con->out_msg_pos);
  1018. con->out_more = 1; /* data + footer will follow */
  1019. } else {
  1020. /* no, queue up footer too and be done */
  1021. prepare_write_message_footer(con);
  1022. }
  1023. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1024. }
  1025. /*
  1026. * Prepare an ack.
  1027. */
  1028. static void prepare_write_ack(struct ceph_connection *con)
  1029. {
  1030. dout("prepare_write_ack %p %llu -> %llu\n", con,
  1031. con->in_seq_acked, con->in_seq);
  1032. con->in_seq_acked = con->in_seq;
  1033. con_out_kvec_reset(con);
  1034. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  1035. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  1036. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  1037. &con->out_temp_ack);
  1038. con->out_more = 1; /* more will follow.. eventually.. */
  1039. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1040. }
  1041. /*
  1042. * Prepare to share the seq during handshake
  1043. */
  1044. static void prepare_write_seq(struct ceph_connection *con)
  1045. {
  1046. dout("prepare_write_seq %p %llu -> %llu\n", con,
  1047. con->in_seq_acked, con->in_seq);
  1048. con->in_seq_acked = con->in_seq;
  1049. con_out_kvec_reset(con);
  1050. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  1051. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  1052. &con->out_temp_ack);
  1053. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1054. }
  1055. /*
  1056. * Prepare to write keepalive byte.
  1057. */
  1058. static void prepare_write_keepalive(struct ceph_connection *con)
  1059. {
  1060. dout("prepare_write_keepalive %p\n", con);
  1061. con_out_kvec_reset(con);
  1062. con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
  1063. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1064. }
  1065. /*
  1066. * Connection negotiation.
  1067. */
  1068. static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
  1069. int *auth_proto)
  1070. {
  1071. struct ceph_auth_handshake *auth;
  1072. if (!con->ops->get_authorizer) {
  1073. con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
  1074. con->out_connect.authorizer_len = 0;
  1075. return NULL;
  1076. }
  1077. /* Can't hold the mutex while getting authorizer */
  1078. mutex_unlock(&con->mutex);
  1079. auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
  1080. mutex_lock(&con->mutex);
  1081. if (IS_ERR(auth))
  1082. return auth;
  1083. if (con->state != CON_STATE_NEGOTIATING)
  1084. return ERR_PTR(-EAGAIN);
  1085. con->auth_reply_buf = auth->authorizer_reply_buf;
  1086. con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
  1087. return auth;
  1088. }
  1089. /*
  1090. * We connected to a peer and are saying hello.
  1091. */
  1092. static void prepare_write_banner(struct ceph_connection *con)
  1093. {
  1094. con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
  1095. con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
  1096. &con->msgr->my_enc_addr);
  1097. con->out_more = 0;
  1098. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1099. }
  1100. static int prepare_write_connect(struct ceph_connection *con)
  1101. {
  1102. unsigned int global_seq = get_global_seq(con->msgr, 0);
  1103. int proto;
  1104. int auth_proto;
  1105. struct ceph_auth_handshake *auth;
  1106. switch (con->peer_name.type) {
  1107. case CEPH_ENTITY_TYPE_MON:
  1108. proto = CEPH_MONC_PROTOCOL;
  1109. break;
  1110. case CEPH_ENTITY_TYPE_OSD:
  1111. proto = CEPH_OSDC_PROTOCOL;
  1112. break;
  1113. case CEPH_ENTITY_TYPE_MDS:
  1114. proto = CEPH_MDSC_PROTOCOL;
  1115. break;
  1116. default:
  1117. BUG();
  1118. }
  1119. dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
  1120. con->connect_seq, global_seq, proto);
  1121. con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
  1122. con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
  1123. con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
  1124. con->out_connect.global_seq = cpu_to_le32(global_seq);
  1125. con->out_connect.protocol_version = cpu_to_le32(proto);
  1126. con->out_connect.flags = 0;
  1127. auth_proto = CEPH_AUTH_UNKNOWN;
  1128. auth = get_connect_authorizer(con, &auth_proto);
  1129. if (IS_ERR(auth))
  1130. return PTR_ERR(auth);
  1131. con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
  1132. con->out_connect.authorizer_len = auth ?
  1133. cpu_to_le32(auth->authorizer_buf_len) : 0;
  1134. con_out_kvec_add(con, sizeof (con->out_connect),
  1135. &con->out_connect);
  1136. if (auth && auth->authorizer_buf_len)
  1137. con_out_kvec_add(con, auth->authorizer_buf_len,
  1138. auth->authorizer_buf);
  1139. con->out_more = 0;
  1140. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1141. return 0;
  1142. }
  1143. /*
  1144. * write as much of pending kvecs to the socket as we can.
  1145. * 1 -> done
  1146. * 0 -> socket full, but more to do
  1147. * <0 -> error
  1148. */
  1149. static int write_partial_kvec(struct ceph_connection *con)
  1150. {
  1151. int ret;
  1152. dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
  1153. while (con->out_kvec_bytes > 0) {
  1154. ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
  1155. con->out_kvec_left, con->out_kvec_bytes,
  1156. con->out_more);
  1157. if (ret <= 0)
  1158. goto out;
  1159. con->out_kvec_bytes -= ret;
  1160. if (con->out_kvec_bytes == 0)
  1161. break; /* done */
  1162. /* account for full iov entries consumed */
  1163. while (ret >= con->out_kvec_cur->iov_len) {
  1164. BUG_ON(!con->out_kvec_left);
  1165. ret -= con->out_kvec_cur->iov_len;
  1166. con->out_kvec_cur++;
  1167. con->out_kvec_left--;
  1168. }
  1169. /* and for a partially-consumed entry */
  1170. if (ret) {
  1171. con->out_kvec_cur->iov_len -= ret;
  1172. con->out_kvec_cur->iov_base += ret;
  1173. }
  1174. }
  1175. con->out_kvec_left = 0;
  1176. con->out_kvec_is_msg = false;
  1177. ret = 1;
  1178. out:
  1179. dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
  1180. con->out_kvec_bytes, con->out_kvec_left, ret);
  1181. return ret; /* done! */
  1182. }
  1183. static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
  1184. size_t len, size_t sent)
  1185. {
  1186. struct ceph_msg *msg = con->out_msg;
  1187. struct ceph_msg_pos *msg_pos = &con->out_msg_pos;
  1188. bool need_crc = false;
  1189. BUG_ON(!msg);
  1190. BUG_ON(!sent);
  1191. msg_pos->data_pos += sent;
  1192. msg_pos->page_pos += sent;
  1193. if (ceph_msg_has_pages(msg))
  1194. need_crc = ceph_msg_data_advance(&msg->p, sent);
  1195. else if (ceph_msg_has_pagelist(msg))
  1196. need_crc = ceph_msg_data_advance(&msg->l, sent);
  1197. #ifdef CONFIG_BLOCK
  1198. else if (ceph_msg_has_bio(msg))
  1199. need_crc = ceph_msg_data_advance(&msg->b, sent);
  1200. #endif /* CONFIG_BLOCK */
  1201. BUG_ON(need_crc && sent != len);
  1202. if (sent < len)
  1203. return;
  1204. BUG_ON(sent != len);
  1205. msg_pos->page_pos = 0;
  1206. msg_pos->page++;
  1207. msg_pos->did_page_crc = false;
  1208. }
  1209. static void in_msg_pos_next(struct ceph_connection *con, size_t len,
  1210. size_t received)
  1211. {
  1212. struct ceph_msg *msg = con->in_msg;
  1213. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1214. BUG_ON(!msg);
  1215. BUG_ON(!received);
  1216. msg_pos->data_pos += received;
  1217. msg_pos->page_pos += received;
  1218. if (received < len)
  1219. return;
  1220. BUG_ON(received != len);
  1221. msg_pos->page_pos = 0;
  1222. msg_pos->page++;
  1223. #ifdef CONFIG_BLOCK
  1224. if (msg->b.bio)
  1225. iter_bio_next(&msg->b.bio_iter, &msg->b.bio_seg);
  1226. #endif /* CONFIG_BLOCK */
  1227. }
  1228. static u32 ceph_crc32c_page(u32 crc, struct page *page,
  1229. unsigned int page_offset,
  1230. unsigned int length)
  1231. {
  1232. char *kaddr;
  1233. kaddr = kmap(page);
  1234. BUG_ON(kaddr == NULL);
  1235. crc = crc32c(crc, kaddr + page_offset, length);
  1236. kunmap(page);
  1237. return crc;
  1238. }
  1239. /*
  1240. * Write as much message data payload as we can. If we finish, queue
  1241. * up the footer.
  1242. * 1 -> done, footer is now queued in out_kvec[].
  1243. * 0 -> socket full, but more to do
  1244. * <0 -> error
  1245. */
  1246. static int write_partial_message_data(struct ceph_connection *con)
  1247. {
  1248. struct ceph_msg *msg = con->out_msg;
  1249. struct ceph_msg_pos *msg_pos = &con->out_msg_pos;
  1250. unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
  1251. bool do_datacrc = !con->msgr->nocrc;
  1252. int ret;
  1253. dout("%s %p msg %p page %d offset %d\n", __func__,
  1254. con, msg, msg_pos->page, msg_pos->page_pos);
  1255. /*
  1256. * Iterate through each page that contains data to be
  1257. * written, and send as much as possible for each.
  1258. *
  1259. * If we are calculating the data crc (the default), we will
  1260. * need to map the page. If we have no pages, they have
  1261. * been revoked, so use the zero page.
  1262. */
  1263. while (data_len > msg_pos->data_pos) {
  1264. struct page *page;
  1265. size_t page_offset;
  1266. size_t length;
  1267. bool last_piece;
  1268. if (ceph_msg_has_pages(msg)) {
  1269. page = ceph_msg_data_next(&msg->p, &page_offset,
  1270. &length, &last_piece);
  1271. } else if (ceph_msg_has_pagelist(msg)) {
  1272. page = ceph_msg_data_next(&msg->l, &page_offset,
  1273. &length, &last_piece);
  1274. #ifdef CONFIG_BLOCK
  1275. } else if (ceph_msg_has_bio(msg)) {
  1276. page = ceph_msg_data_next(&msg->b, &page_offset,
  1277. &length, &last_piece);
  1278. #endif
  1279. } else {
  1280. size_t resid = data_len - msg_pos->data_pos;
  1281. page = zero_page;
  1282. page_offset = msg_pos->page_pos;
  1283. length = PAGE_SIZE - page_offset;
  1284. length = min(resid, length);
  1285. last_piece = length == resid;
  1286. }
  1287. if (do_datacrc && !msg_pos->did_page_crc) {
  1288. u32 crc = le32_to_cpu(msg->footer.data_crc);
  1289. crc = ceph_crc32c_page(crc, page, page_offset, length);
  1290. msg->footer.data_crc = cpu_to_le32(crc);
  1291. msg_pos->did_page_crc = true;
  1292. }
  1293. ret = ceph_tcp_sendpage(con->sock, page, page_offset,
  1294. length, last_piece);
  1295. if (ret <= 0)
  1296. goto out;
  1297. out_msg_pos_next(con, page, length, (size_t) ret);
  1298. }
  1299. dout("%s %p msg %p done\n", __func__, con, msg);
  1300. /* prepare and queue up footer, too */
  1301. if (!do_datacrc)
  1302. msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
  1303. con_out_kvec_reset(con);
  1304. prepare_write_message_footer(con);
  1305. ret = 1;
  1306. out:
  1307. return ret;
  1308. }
  1309. /*
  1310. * write some zeros
  1311. */
  1312. static int write_partial_skip(struct ceph_connection *con)
  1313. {
  1314. int ret;
  1315. while (con->out_skip > 0) {
  1316. size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
  1317. ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
  1318. if (ret <= 0)
  1319. goto out;
  1320. con->out_skip -= ret;
  1321. }
  1322. ret = 1;
  1323. out:
  1324. return ret;
  1325. }
  1326. /*
  1327. * Prepare to read connection handshake, or an ack.
  1328. */
  1329. static void prepare_read_banner(struct ceph_connection *con)
  1330. {
  1331. dout("prepare_read_banner %p\n", con);
  1332. con->in_base_pos = 0;
  1333. }
  1334. static void prepare_read_connect(struct ceph_connection *con)
  1335. {
  1336. dout("prepare_read_connect %p\n", con);
  1337. con->in_base_pos = 0;
  1338. }
  1339. static void prepare_read_ack(struct ceph_connection *con)
  1340. {
  1341. dout("prepare_read_ack %p\n", con);
  1342. con->in_base_pos = 0;
  1343. }
  1344. static void prepare_read_seq(struct ceph_connection *con)
  1345. {
  1346. dout("prepare_read_seq %p\n", con);
  1347. con->in_base_pos = 0;
  1348. con->in_tag = CEPH_MSGR_TAG_SEQ;
  1349. }
  1350. static void prepare_read_tag(struct ceph_connection *con)
  1351. {
  1352. dout("prepare_read_tag %p\n", con);
  1353. con->in_base_pos = 0;
  1354. con->in_tag = CEPH_MSGR_TAG_READY;
  1355. }
  1356. /*
  1357. * Prepare to read a message.
  1358. */
  1359. static int prepare_read_message(struct ceph_connection *con)
  1360. {
  1361. dout("prepare_read_message %p\n", con);
  1362. BUG_ON(con->in_msg != NULL);
  1363. con->in_base_pos = 0;
  1364. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  1365. return 0;
  1366. }
  1367. static int read_partial(struct ceph_connection *con,
  1368. int end, int size, void *object)
  1369. {
  1370. while (con->in_base_pos < end) {
  1371. int left = end - con->in_base_pos;
  1372. int have = size - left;
  1373. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  1374. if (ret <= 0)
  1375. return ret;
  1376. con->in_base_pos += ret;
  1377. }
  1378. return 1;
  1379. }
  1380. /*
  1381. * Read all or part of the connect-side handshake on a new connection
  1382. */
  1383. static int read_partial_banner(struct ceph_connection *con)
  1384. {
  1385. int size;
  1386. int end;
  1387. int ret;
  1388. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  1389. /* peer's banner */
  1390. size = strlen(CEPH_BANNER);
  1391. end = size;
  1392. ret = read_partial(con, end, size, con->in_banner);
  1393. if (ret <= 0)
  1394. goto out;
  1395. size = sizeof (con->actual_peer_addr);
  1396. end += size;
  1397. ret = read_partial(con, end, size, &con->actual_peer_addr);
  1398. if (ret <= 0)
  1399. goto out;
  1400. size = sizeof (con->peer_addr_for_me);
  1401. end += size;
  1402. ret = read_partial(con, end, size, &con->peer_addr_for_me);
  1403. if (ret <= 0)
  1404. goto out;
  1405. out:
  1406. return ret;
  1407. }
  1408. static int read_partial_connect(struct ceph_connection *con)
  1409. {
  1410. int size;
  1411. int end;
  1412. int ret;
  1413. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  1414. size = sizeof (con->in_reply);
  1415. end = size;
  1416. ret = read_partial(con, end, size, &con->in_reply);
  1417. if (ret <= 0)
  1418. goto out;
  1419. size = le32_to_cpu(con->in_reply.authorizer_len);
  1420. end += size;
  1421. ret = read_partial(con, end, size, con->auth_reply_buf);
  1422. if (ret <= 0)
  1423. goto out;
  1424. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  1425. con, (int)con->in_reply.tag,
  1426. le32_to_cpu(con->in_reply.connect_seq),
  1427. le32_to_cpu(con->in_reply.global_seq));
  1428. out:
  1429. return ret;
  1430. }
  1431. /*
  1432. * Verify the hello banner looks okay.
  1433. */
  1434. static int verify_hello(struct ceph_connection *con)
  1435. {
  1436. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  1437. pr_err("connect to %s got bad banner\n",
  1438. ceph_pr_addr(&con->peer_addr.in_addr));
  1439. con->error_msg = "protocol error, bad banner";
  1440. return -1;
  1441. }
  1442. return 0;
  1443. }
  1444. static bool addr_is_blank(struct sockaddr_storage *ss)
  1445. {
  1446. switch (ss->ss_family) {
  1447. case AF_INET:
  1448. return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
  1449. case AF_INET6:
  1450. return
  1451. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
  1452. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
  1453. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
  1454. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
  1455. }
  1456. return false;
  1457. }
  1458. static int addr_port(struct sockaddr_storage *ss)
  1459. {
  1460. switch (ss->ss_family) {
  1461. case AF_INET:
  1462. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  1463. case AF_INET6:
  1464. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  1465. }
  1466. return 0;
  1467. }
  1468. static void addr_set_port(struct sockaddr_storage *ss, int p)
  1469. {
  1470. switch (ss->ss_family) {
  1471. case AF_INET:
  1472. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  1473. break;
  1474. case AF_INET6:
  1475. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  1476. break;
  1477. }
  1478. }
  1479. /*
  1480. * Unlike other *_pton function semantics, zero indicates success.
  1481. */
  1482. static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
  1483. char delim, const char **ipend)
  1484. {
  1485. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  1486. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  1487. memset(ss, 0, sizeof(*ss));
  1488. if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
  1489. ss->ss_family = AF_INET;
  1490. return 0;
  1491. }
  1492. if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
  1493. ss->ss_family = AF_INET6;
  1494. return 0;
  1495. }
  1496. return -EINVAL;
  1497. }
  1498. /*
  1499. * Extract hostname string and resolve using kernel DNS facility.
  1500. */
  1501. #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
  1502. static int ceph_dns_resolve_name(const char *name, size_t namelen,
  1503. struct sockaddr_storage *ss, char delim, const char **ipend)
  1504. {
  1505. const char *end, *delim_p;
  1506. char *colon_p, *ip_addr = NULL;
  1507. int ip_len, ret;
  1508. /*
  1509. * The end of the hostname occurs immediately preceding the delimiter or
  1510. * the port marker (':') where the delimiter takes precedence.
  1511. */
  1512. delim_p = memchr(name, delim, namelen);
  1513. colon_p = memchr(name, ':', namelen);
  1514. if (delim_p && colon_p)
  1515. end = delim_p < colon_p ? delim_p : colon_p;
  1516. else if (!delim_p && colon_p)
  1517. end = colon_p;
  1518. else {
  1519. end = delim_p;
  1520. if (!end) /* case: hostname:/ */
  1521. end = name + namelen;
  1522. }
  1523. if (end <= name)
  1524. return -EINVAL;
  1525. /* do dns_resolve upcall */
  1526. ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
  1527. if (ip_len > 0)
  1528. ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
  1529. else
  1530. ret = -ESRCH;
  1531. kfree(ip_addr);
  1532. *ipend = end;
  1533. pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
  1534. ret, ret ? "failed" : ceph_pr_addr(ss));
  1535. return ret;
  1536. }
  1537. #else
  1538. static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
  1539. struct sockaddr_storage *ss, char delim, const char **ipend)
  1540. {
  1541. return -EINVAL;
  1542. }
  1543. #endif
  1544. /*
  1545. * Parse a server name (IP or hostname). If a valid IP address is not found
  1546. * then try to extract a hostname to resolve using userspace DNS upcall.
  1547. */
  1548. static int ceph_parse_server_name(const char *name, size_t namelen,
  1549. struct sockaddr_storage *ss, char delim, const char **ipend)
  1550. {
  1551. int ret;
  1552. ret = ceph_pton(name, namelen, ss, delim, ipend);
  1553. if (ret)
  1554. ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
  1555. return ret;
  1556. }
  1557. /*
  1558. * Parse an ip[:port] list into an addr array. Use the default
  1559. * monitor port if a port isn't specified.
  1560. */
  1561. int ceph_parse_ips(const char *c, const char *end,
  1562. struct ceph_entity_addr *addr,
  1563. int max_count, int *count)
  1564. {
  1565. int i, ret = -EINVAL;
  1566. const char *p = c;
  1567. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  1568. for (i = 0; i < max_count; i++) {
  1569. const char *ipend;
  1570. struct sockaddr_storage *ss = &addr[i].in_addr;
  1571. int port;
  1572. char delim = ',';
  1573. if (*p == '[') {
  1574. delim = ']';
  1575. p++;
  1576. }
  1577. ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
  1578. if (ret)
  1579. goto bad;
  1580. ret = -EINVAL;
  1581. p = ipend;
  1582. if (delim == ']') {
  1583. if (*p != ']') {
  1584. dout("missing matching ']'\n");
  1585. goto bad;
  1586. }
  1587. p++;
  1588. }
  1589. /* port? */
  1590. if (p < end && *p == ':') {
  1591. port = 0;
  1592. p++;
  1593. while (p < end && *p >= '0' && *p <= '9') {
  1594. port = (port * 10) + (*p - '0');
  1595. p++;
  1596. }
  1597. if (port > 65535 || port == 0)
  1598. goto bad;
  1599. } else {
  1600. port = CEPH_MON_PORT;
  1601. }
  1602. addr_set_port(ss, port);
  1603. dout("parse_ips got %s\n", ceph_pr_addr(ss));
  1604. if (p == end)
  1605. break;
  1606. if (*p != ',')
  1607. goto bad;
  1608. p++;
  1609. }
  1610. if (p != end)
  1611. goto bad;
  1612. if (count)
  1613. *count = i + 1;
  1614. return 0;
  1615. bad:
  1616. pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
  1617. return ret;
  1618. }
  1619. EXPORT_SYMBOL(ceph_parse_ips);
  1620. static int process_banner(struct ceph_connection *con)
  1621. {
  1622. dout("process_banner on %p\n", con);
  1623. if (verify_hello(con) < 0)
  1624. return -1;
  1625. ceph_decode_addr(&con->actual_peer_addr);
  1626. ceph_decode_addr(&con->peer_addr_for_me);
  1627. /*
  1628. * Make sure the other end is who we wanted. note that the other
  1629. * end may not yet know their ip address, so if it's 0.0.0.0, give
  1630. * them the benefit of the doubt.
  1631. */
  1632. if (memcmp(&con->peer_addr, &con->actual_peer_addr,
  1633. sizeof(con->peer_addr)) != 0 &&
  1634. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  1635. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  1636. pr_warning("wrong peer, want %s/%d, got %s/%d\n",
  1637. ceph_pr_addr(&con->peer_addr.in_addr),
  1638. (int)le32_to_cpu(con->peer_addr.nonce),
  1639. ceph_pr_addr(&con->actual_peer_addr.in_addr),
  1640. (int)le32_to_cpu(con->actual_peer_addr.nonce));
  1641. con->error_msg = "wrong peer at address";
  1642. return -1;
  1643. }
  1644. /*
  1645. * did we learn our address?
  1646. */
  1647. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  1648. int port = addr_port(&con->msgr->inst.addr.in_addr);
  1649. memcpy(&con->msgr->inst.addr.in_addr,
  1650. &con->peer_addr_for_me.in_addr,
  1651. sizeof(con->peer_addr_for_me.in_addr));
  1652. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  1653. encode_my_addr(con->msgr);
  1654. dout("process_banner learned my addr is %s\n",
  1655. ceph_pr_addr(&con->msgr->inst.addr.in_addr));
  1656. }
  1657. return 0;
  1658. }
  1659. static int process_connect(struct ceph_connection *con)
  1660. {
  1661. u64 sup_feat = con->msgr->supported_features;
  1662. u64 req_feat = con->msgr->required_features;
  1663. u64 server_feat = le64_to_cpu(con->in_reply.features);
  1664. int ret;
  1665. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  1666. switch (con->in_reply.tag) {
  1667. case CEPH_MSGR_TAG_FEATURES:
  1668. pr_err("%s%lld %s feature set mismatch,"
  1669. " my %llx < server's %llx, missing %llx\n",
  1670. ENTITY_NAME(con->peer_name),
  1671. ceph_pr_addr(&con->peer_addr.in_addr),
  1672. sup_feat, server_feat, server_feat & ~sup_feat);
  1673. con->error_msg = "missing required protocol features";
  1674. reset_connection(con);
  1675. return -1;
  1676. case CEPH_MSGR_TAG_BADPROTOVER:
  1677. pr_err("%s%lld %s protocol version mismatch,"
  1678. " my %d != server's %d\n",
  1679. ENTITY_NAME(con->peer_name),
  1680. ceph_pr_addr(&con->peer_addr.in_addr),
  1681. le32_to_cpu(con->out_connect.protocol_version),
  1682. le32_to_cpu(con->in_reply.protocol_version));
  1683. con->error_msg = "protocol version mismatch";
  1684. reset_connection(con);
  1685. return -1;
  1686. case CEPH_MSGR_TAG_BADAUTHORIZER:
  1687. con->auth_retry++;
  1688. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  1689. con->auth_retry);
  1690. if (con->auth_retry == 2) {
  1691. con->error_msg = "connect authorization failure";
  1692. return -1;
  1693. }
  1694. con_out_kvec_reset(con);
  1695. ret = prepare_write_connect(con);
  1696. if (ret < 0)
  1697. return ret;
  1698. prepare_read_connect(con);
  1699. break;
  1700. case CEPH_MSGR_TAG_RESETSESSION:
  1701. /*
  1702. * If we connected with a large connect_seq but the peer
  1703. * has no record of a session with us (no connection, or
  1704. * connect_seq == 0), they will send RESETSESION to indicate
  1705. * that they must have reset their session, and may have
  1706. * dropped messages.
  1707. */
  1708. dout("process_connect got RESET peer seq %u\n",
  1709. le32_to_cpu(con->in_reply.connect_seq));
  1710. pr_err("%s%lld %s connection reset\n",
  1711. ENTITY_NAME(con->peer_name),
  1712. ceph_pr_addr(&con->peer_addr.in_addr));
  1713. reset_connection(con);
  1714. con_out_kvec_reset(con);
  1715. ret = prepare_write_connect(con);
  1716. if (ret < 0)
  1717. return ret;
  1718. prepare_read_connect(con);
  1719. /* Tell ceph about it. */
  1720. mutex_unlock(&con->mutex);
  1721. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  1722. if (con->ops->peer_reset)
  1723. con->ops->peer_reset(con);
  1724. mutex_lock(&con->mutex);
  1725. if (con->state != CON_STATE_NEGOTIATING)
  1726. return -EAGAIN;
  1727. break;
  1728. case CEPH_MSGR_TAG_RETRY_SESSION:
  1729. /*
  1730. * If we sent a smaller connect_seq than the peer has, try
  1731. * again with a larger value.
  1732. */
  1733. dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
  1734. le32_to_cpu(con->out_connect.connect_seq),
  1735. le32_to_cpu(con->in_reply.connect_seq));
  1736. con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
  1737. con_out_kvec_reset(con);
  1738. ret = prepare_write_connect(con);
  1739. if (ret < 0)
  1740. return ret;
  1741. prepare_read_connect(con);
  1742. break;
  1743. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1744. /*
  1745. * If we sent a smaller global_seq than the peer has, try
  1746. * again with a larger value.
  1747. */
  1748. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1749. con->peer_global_seq,
  1750. le32_to_cpu(con->in_reply.global_seq));
  1751. get_global_seq(con->msgr,
  1752. le32_to_cpu(con->in_reply.global_seq));
  1753. con_out_kvec_reset(con);
  1754. ret = prepare_write_connect(con);
  1755. if (ret < 0)
  1756. return ret;
  1757. prepare_read_connect(con);
  1758. break;
  1759. case CEPH_MSGR_TAG_SEQ:
  1760. case CEPH_MSGR_TAG_READY:
  1761. if (req_feat & ~server_feat) {
  1762. pr_err("%s%lld %s protocol feature mismatch,"
  1763. " my required %llx > server's %llx, need %llx\n",
  1764. ENTITY_NAME(con->peer_name),
  1765. ceph_pr_addr(&con->peer_addr.in_addr),
  1766. req_feat, server_feat, req_feat & ~server_feat);
  1767. con->error_msg = "missing required protocol features";
  1768. reset_connection(con);
  1769. return -1;
  1770. }
  1771. WARN_ON(con->state != CON_STATE_NEGOTIATING);
  1772. con->state = CON_STATE_OPEN;
  1773. con->auth_retry = 0; /* we authenticated; clear flag */
  1774. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1775. con->connect_seq++;
  1776. con->peer_features = server_feat;
  1777. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1778. con->peer_global_seq,
  1779. le32_to_cpu(con->in_reply.connect_seq),
  1780. con->connect_seq);
  1781. WARN_ON(con->connect_seq !=
  1782. le32_to_cpu(con->in_reply.connect_seq));
  1783. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1784. con_flag_set(con, CON_FLAG_LOSSYTX);
  1785. con->delay = 0; /* reset backoff memory */
  1786. if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
  1787. prepare_write_seq(con);
  1788. prepare_read_seq(con);
  1789. } else {
  1790. prepare_read_tag(con);
  1791. }
  1792. break;
  1793. case CEPH_MSGR_TAG_WAIT:
  1794. /*
  1795. * If there is a connection race (we are opening
  1796. * connections to each other), one of us may just have
  1797. * to WAIT. This shouldn't happen if we are the
  1798. * client.
  1799. */
  1800. pr_err("process_connect got WAIT as client\n");
  1801. con->error_msg = "protocol error, got WAIT as client";
  1802. return -1;
  1803. default:
  1804. pr_err("connect protocol error, will retry\n");
  1805. con->error_msg = "protocol error, garbage tag during connect";
  1806. return -1;
  1807. }
  1808. return 0;
  1809. }
  1810. /*
  1811. * read (part of) an ack
  1812. */
  1813. static int read_partial_ack(struct ceph_connection *con)
  1814. {
  1815. int size = sizeof (con->in_temp_ack);
  1816. int end = size;
  1817. return read_partial(con, end, size, &con->in_temp_ack);
  1818. }
  1819. /*
  1820. * We can finally discard anything that's been acked.
  1821. */
  1822. static void process_ack(struct ceph_connection *con)
  1823. {
  1824. struct ceph_msg *m;
  1825. u64 ack = le64_to_cpu(con->in_temp_ack);
  1826. u64 seq;
  1827. while (!list_empty(&con->out_sent)) {
  1828. m = list_first_entry(&con->out_sent, struct ceph_msg,
  1829. list_head);
  1830. seq = le64_to_cpu(m->hdr.seq);
  1831. if (seq > ack)
  1832. break;
  1833. dout("got ack for seq %llu type %d at %p\n", seq,
  1834. le16_to_cpu(m->hdr.type), m);
  1835. m->ack_stamp = jiffies;
  1836. ceph_msg_remove(m);
  1837. }
  1838. prepare_read_tag(con);
  1839. }
  1840. static int read_partial_message_section(struct ceph_connection *con,
  1841. struct kvec *section,
  1842. unsigned int sec_len, u32 *crc)
  1843. {
  1844. int ret, left;
  1845. BUG_ON(!section);
  1846. while (section->iov_len < sec_len) {
  1847. BUG_ON(section->iov_base == NULL);
  1848. left = sec_len - section->iov_len;
  1849. ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
  1850. section->iov_len, left);
  1851. if (ret <= 0)
  1852. return ret;
  1853. section->iov_len += ret;
  1854. }
  1855. if (section->iov_len == sec_len)
  1856. *crc = crc32c(0, section->iov_base, section->iov_len);
  1857. return 1;
  1858. }
  1859. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
  1860. static int read_partial_message_pages(struct ceph_connection *con,
  1861. struct page **pages,
  1862. unsigned int data_len, bool do_datacrc)
  1863. {
  1864. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1865. struct page *page;
  1866. size_t page_offset;
  1867. size_t length;
  1868. unsigned int left;
  1869. int ret;
  1870. /* (page) data */
  1871. BUG_ON(pages == NULL);
  1872. page = pages[msg_pos->page];
  1873. page_offset = msg_pos->page_pos;
  1874. BUG_ON(msg_pos->data_pos >= data_len);
  1875. left = data_len - msg_pos->data_pos;
  1876. BUG_ON(page_offset >= PAGE_SIZE);
  1877. length = min_t(unsigned int, PAGE_SIZE - page_offset, left);
  1878. ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
  1879. if (ret <= 0)
  1880. return ret;
  1881. if (do_datacrc)
  1882. con->in_data_crc = ceph_crc32c_page(con->in_data_crc, page,
  1883. page_offset, ret);
  1884. in_msg_pos_next(con, length, ret);
  1885. return ret;
  1886. }
  1887. #ifdef CONFIG_BLOCK
  1888. static int read_partial_message_bio(struct ceph_connection *con,
  1889. unsigned int data_len, bool do_datacrc)
  1890. {
  1891. struct ceph_msg *msg = con->in_msg;
  1892. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1893. struct bio_vec *bv;
  1894. struct page *page;
  1895. size_t page_offset;
  1896. size_t length;
  1897. unsigned int left;
  1898. int ret;
  1899. BUG_ON(!msg);
  1900. BUG_ON(!msg->b.bio_iter);
  1901. bv = bio_iovec_idx(msg->b.bio_iter, msg->b.bio_seg);
  1902. page = bv->bv_page;
  1903. page_offset = bv->bv_offset + msg_pos->page_pos;
  1904. BUG_ON(msg_pos->data_pos >= data_len);
  1905. left = data_len - msg_pos->data_pos;
  1906. BUG_ON(msg_pos->page_pos >= bv->bv_len);
  1907. length = min_t(unsigned int, bv->bv_len - msg_pos->page_pos, left);
  1908. ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
  1909. if (ret <= 0)
  1910. return ret;
  1911. if (do_datacrc)
  1912. con->in_data_crc = ceph_crc32c_page(con->in_data_crc, page,
  1913. page_offset, ret);
  1914. in_msg_pos_next(con, length, ret);
  1915. return ret;
  1916. }
  1917. #endif
  1918. static int read_partial_msg_data(struct ceph_connection *con)
  1919. {
  1920. struct ceph_msg *msg = con->in_msg;
  1921. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1922. const bool do_datacrc = !con->msgr->nocrc;
  1923. unsigned int data_len;
  1924. int ret;
  1925. BUG_ON(!msg);
  1926. data_len = le32_to_cpu(con->in_hdr.data_len);
  1927. while (msg_pos->data_pos < data_len) {
  1928. if (ceph_msg_has_pages(msg)) {
  1929. ret = read_partial_message_pages(con, msg->p.pages,
  1930. data_len, do_datacrc);
  1931. if (ret <= 0)
  1932. return ret;
  1933. #ifdef CONFIG_BLOCK
  1934. } else if (ceph_msg_has_bio(msg)) {
  1935. ret = read_partial_message_bio(con,
  1936. data_len, do_datacrc);
  1937. if (ret <= 0)
  1938. return ret;
  1939. #endif
  1940. } else {
  1941. BUG_ON(1);
  1942. }
  1943. }
  1944. return 1; /* must return > 0 to indicate success */
  1945. }
  1946. /*
  1947. * read (part of) a message.
  1948. */
  1949. static int read_partial_message(struct ceph_connection *con)
  1950. {
  1951. struct ceph_msg *m = con->in_msg;
  1952. int size;
  1953. int end;
  1954. int ret;
  1955. unsigned int front_len, middle_len, data_len;
  1956. bool do_datacrc = !con->msgr->nocrc;
  1957. u64 seq;
  1958. u32 crc;
  1959. dout("read_partial_message con %p msg %p\n", con, m);
  1960. /* header */
  1961. size = sizeof (con->in_hdr);
  1962. end = size;
  1963. ret = read_partial(con, end, size, &con->in_hdr);
  1964. if (ret <= 0)
  1965. return ret;
  1966. crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
  1967. if (cpu_to_le32(crc) != con->in_hdr.crc) {
  1968. pr_err("read_partial_message bad hdr "
  1969. " crc %u != expected %u\n",
  1970. crc, con->in_hdr.crc);
  1971. return -EBADMSG;
  1972. }
  1973. front_len = le32_to_cpu(con->in_hdr.front_len);
  1974. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  1975. return -EIO;
  1976. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1977. if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
  1978. return -EIO;
  1979. data_len = le32_to_cpu(con->in_hdr.data_len);
  1980. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  1981. return -EIO;
  1982. /* verify seq# */
  1983. seq = le64_to_cpu(con->in_hdr.seq);
  1984. if ((s64)seq - (s64)con->in_seq < 1) {
  1985. pr_info("skipping %s%lld %s seq %lld expected %lld\n",
  1986. ENTITY_NAME(con->peer_name),
  1987. ceph_pr_addr(&con->peer_addr.in_addr),
  1988. seq, con->in_seq + 1);
  1989. con->in_base_pos = -front_len - middle_len - data_len -
  1990. sizeof(m->footer);
  1991. con->in_tag = CEPH_MSGR_TAG_READY;
  1992. return 0;
  1993. } else if ((s64)seq - (s64)con->in_seq > 1) {
  1994. pr_err("read_partial_message bad seq %lld expected %lld\n",
  1995. seq, con->in_seq + 1);
  1996. con->error_msg = "bad message sequence # for incoming message";
  1997. return -EBADMSG;
  1998. }
  1999. /* allocate message? */
  2000. if (!con->in_msg) {
  2001. int skip = 0;
  2002. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  2003. front_len, data_len);
  2004. ret = ceph_con_in_msg_alloc(con, &skip);
  2005. if (ret < 0)
  2006. return ret;
  2007. if (skip) {
  2008. /* skip this message */
  2009. dout("alloc_msg said skip message\n");
  2010. BUG_ON(con->in_msg);
  2011. con->in_base_pos = -front_len - middle_len - data_len -
  2012. sizeof(m->footer);
  2013. con->in_tag = CEPH_MSGR_TAG_READY;
  2014. con->in_seq++;
  2015. return 0;
  2016. }
  2017. BUG_ON(!con->in_msg);
  2018. BUG_ON(con->in_msg->con != con);
  2019. m = con->in_msg;
  2020. m->front.iov_len = 0; /* haven't read it yet */
  2021. if (m->middle)
  2022. m->middle->vec.iov_len = 0;
  2023. /* prepare for data payload, if any */
  2024. if (data_len)
  2025. prepare_message_data(con->in_msg, &con->in_msg_pos);
  2026. }
  2027. /* front */
  2028. ret = read_partial_message_section(con, &m->front, front_len,
  2029. &con->in_front_crc);
  2030. if (ret <= 0)
  2031. return ret;
  2032. /* middle */
  2033. if (m->middle) {
  2034. ret = read_partial_message_section(con, &m->middle->vec,
  2035. middle_len,
  2036. &con->in_middle_crc);
  2037. if (ret <= 0)
  2038. return ret;
  2039. }
  2040. /* (page) data */
  2041. if (data_len) {
  2042. ret = read_partial_msg_data(con);
  2043. if (ret <= 0)
  2044. return ret;
  2045. }
  2046. /* footer */
  2047. size = sizeof (m->footer);
  2048. end += size;
  2049. ret = read_partial(con, end, size, &m->footer);
  2050. if (ret <= 0)
  2051. return ret;
  2052. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  2053. m, front_len, m->footer.front_crc, middle_len,
  2054. m->footer.middle_crc, data_len, m->footer.data_crc);
  2055. /* crc ok? */
  2056. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  2057. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  2058. m, con->in_front_crc, m->footer.front_crc);
  2059. return -EBADMSG;
  2060. }
  2061. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  2062. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  2063. m, con->in_middle_crc, m->footer.middle_crc);
  2064. return -EBADMSG;
  2065. }
  2066. if (do_datacrc &&
  2067. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  2068. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  2069. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  2070. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  2071. return -EBADMSG;
  2072. }
  2073. return 1; /* done! */
  2074. }
  2075. /*
  2076. * Process message. This happens in the worker thread. The callback should
  2077. * be careful not to do anything that waits on other incoming messages or it
  2078. * may deadlock.
  2079. */
  2080. static void process_message(struct ceph_connection *con)
  2081. {
  2082. struct ceph_msg *msg;
  2083. BUG_ON(con->in_msg->con != con);
  2084. con->in_msg->con = NULL;
  2085. msg = con->in_msg;
  2086. con->in_msg = NULL;
  2087. con->ops->put(con);
  2088. /* if first message, set peer_name */
  2089. if (con->peer_name.type == 0)
  2090. con->peer_name = msg->hdr.src;
  2091. con->in_seq++;
  2092. mutex_unlock(&con->mutex);
  2093. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  2094. msg, le64_to_cpu(msg->hdr.seq),
  2095. ENTITY_NAME(msg->hdr.src),
  2096. le16_to_cpu(msg->hdr.type),
  2097. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2098. le32_to_cpu(msg->hdr.front_len),
  2099. le32_to_cpu(msg->hdr.data_len),
  2100. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  2101. con->ops->dispatch(con, msg);
  2102. mutex_lock(&con->mutex);
  2103. }
  2104. /*
  2105. * Write something to the socket. Called in a worker thread when the
  2106. * socket appears to be writeable and we have something ready to send.
  2107. */
  2108. static int try_write(struct ceph_connection *con)
  2109. {
  2110. int ret = 1;
  2111. dout("try_write start %p state %lu\n", con, con->state);
  2112. more:
  2113. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  2114. /* open the socket first? */
  2115. if (con->state == CON_STATE_PREOPEN) {
  2116. BUG_ON(con->sock);
  2117. con->state = CON_STATE_CONNECTING;
  2118. con_out_kvec_reset(con);
  2119. prepare_write_banner(con);
  2120. prepare_read_banner(con);
  2121. BUG_ON(con->in_msg);
  2122. con->in_tag = CEPH_MSGR_TAG_READY;
  2123. dout("try_write initiating connect on %p new state %lu\n",
  2124. con, con->state);
  2125. ret = ceph_tcp_connect(con);
  2126. if (ret < 0) {
  2127. con->error_msg = "connect error";
  2128. goto out;
  2129. }
  2130. }
  2131. more_kvec:
  2132. /* kvec data queued? */
  2133. if (con->out_skip) {
  2134. ret = write_partial_skip(con);
  2135. if (ret <= 0)
  2136. goto out;
  2137. }
  2138. if (con->out_kvec_left) {
  2139. ret = write_partial_kvec(con);
  2140. if (ret <= 0)
  2141. goto out;
  2142. }
  2143. /* msg pages? */
  2144. if (con->out_msg) {
  2145. if (con->out_msg_done) {
  2146. ceph_msg_put(con->out_msg);
  2147. con->out_msg = NULL; /* we're done with this one */
  2148. goto do_next;
  2149. }
  2150. ret = write_partial_message_data(con);
  2151. if (ret == 1)
  2152. goto more_kvec; /* we need to send the footer, too! */
  2153. if (ret == 0)
  2154. goto out;
  2155. if (ret < 0) {
  2156. dout("try_write write_partial_message_data err %d\n",
  2157. ret);
  2158. goto out;
  2159. }
  2160. }
  2161. do_next:
  2162. if (con->state == CON_STATE_OPEN) {
  2163. /* is anything else pending? */
  2164. if (!list_empty(&con->out_queue)) {
  2165. prepare_write_message(con);
  2166. goto more;
  2167. }
  2168. if (con->in_seq > con->in_seq_acked) {
  2169. prepare_write_ack(con);
  2170. goto more;
  2171. }
  2172. if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2173. prepare_write_keepalive(con);
  2174. goto more;
  2175. }
  2176. }
  2177. /* Nothing to do! */
  2178. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2179. dout("try_write nothing else to write.\n");
  2180. ret = 0;
  2181. out:
  2182. dout("try_write done on %p ret %d\n", con, ret);
  2183. return ret;
  2184. }
  2185. /*
  2186. * Read what we can from the socket.
  2187. */
  2188. static int try_read(struct ceph_connection *con)
  2189. {
  2190. int ret = -1;
  2191. more:
  2192. dout("try_read start on %p state %lu\n", con, con->state);
  2193. if (con->state != CON_STATE_CONNECTING &&
  2194. con->state != CON_STATE_NEGOTIATING &&
  2195. con->state != CON_STATE_OPEN)
  2196. return 0;
  2197. BUG_ON(!con->sock);
  2198. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  2199. con->in_base_pos);
  2200. if (con->state == CON_STATE_CONNECTING) {
  2201. dout("try_read connecting\n");
  2202. ret = read_partial_banner(con);
  2203. if (ret <= 0)
  2204. goto out;
  2205. ret = process_banner(con);
  2206. if (ret < 0)
  2207. goto out;
  2208. con->state = CON_STATE_NEGOTIATING;
  2209. /*
  2210. * Received banner is good, exchange connection info.
  2211. * Do not reset out_kvec, as sending our banner raced
  2212. * with receiving peer banner after connect completed.
  2213. */
  2214. ret = prepare_write_connect(con);
  2215. if (ret < 0)
  2216. goto out;
  2217. prepare_read_connect(con);
  2218. /* Send connection info before awaiting response */
  2219. goto out;
  2220. }
  2221. if (con->state == CON_STATE_NEGOTIATING) {
  2222. dout("try_read negotiating\n");
  2223. ret = read_partial_connect(con);
  2224. if (ret <= 0)
  2225. goto out;
  2226. ret = process_connect(con);
  2227. if (ret < 0)
  2228. goto out;
  2229. goto more;
  2230. }
  2231. WARN_ON(con->state != CON_STATE_OPEN);
  2232. if (con->in_base_pos < 0) {
  2233. /*
  2234. * skipping + discarding content.
  2235. *
  2236. * FIXME: there must be a better way to do this!
  2237. */
  2238. static char buf[SKIP_BUF_SIZE];
  2239. int skip = min((int) sizeof (buf), -con->in_base_pos);
  2240. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  2241. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  2242. if (ret <= 0)
  2243. goto out;
  2244. con->in_base_pos += ret;
  2245. if (con->in_base_pos)
  2246. goto more;
  2247. }
  2248. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  2249. /*
  2250. * what's next?
  2251. */
  2252. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  2253. if (ret <= 0)
  2254. goto out;
  2255. dout("try_read got tag %d\n", (int)con->in_tag);
  2256. switch (con->in_tag) {
  2257. case CEPH_MSGR_TAG_MSG:
  2258. prepare_read_message(con);
  2259. break;
  2260. case CEPH_MSGR_TAG_ACK:
  2261. prepare_read_ack(con);
  2262. break;
  2263. case CEPH_MSGR_TAG_CLOSE:
  2264. con_close_socket(con);
  2265. con->state = CON_STATE_CLOSED;
  2266. goto out;
  2267. default:
  2268. goto bad_tag;
  2269. }
  2270. }
  2271. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  2272. ret = read_partial_message(con);
  2273. if (ret <= 0) {
  2274. switch (ret) {
  2275. case -EBADMSG:
  2276. con->error_msg = "bad crc";
  2277. ret = -EIO;
  2278. break;
  2279. case -EIO:
  2280. con->error_msg = "io error";
  2281. break;
  2282. }
  2283. goto out;
  2284. }
  2285. if (con->in_tag == CEPH_MSGR_TAG_READY)
  2286. goto more;
  2287. process_message(con);
  2288. if (con->state == CON_STATE_OPEN)
  2289. prepare_read_tag(con);
  2290. goto more;
  2291. }
  2292. if (con->in_tag == CEPH_MSGR_TAG_ACK ||
  2293. con->in_tag == CEPH_MSGR_TAG_SEQ) {
  2294. /*
  2295. * the final handshake seq exchange is semantically
  2296. * equivalent to an ACK
  2297. */
  2298. ret = read_partial_ack(con);
  2299. if (ret <= 0)
  2300. goto out;
  2301. process_ack(con);
  2302. goto more;
  2303. }
  2304. out:
  2305. dout("try_read done on %p ret %d\n", con, ret);
  2306. return ret;
  2307. bad_tag:
  2308. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  2309. con->error_msg = "protocol error, garbage tag";
  2310. ret = -1;
  2311. goto out;
  2312. }
  2313. /*
  2314. * Atomically queue work on a connection after the specified delay.
  2315. * Bump @con reference to avoid races with connection teardown.
  2316. * Returns 0 if work was queued, or an error code otherwise.
  2317. */
  2318. static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
  2319. {
  2320. if (!con->ops->get(con)) {
  2321. dout("%s %p ref count 0\n", __func__, con);
  2322. return -ENOENT;
  2323. }
  2324. if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
  2325. dout("%s %p - already queued\n", __func__, con);
  2326. con->ops->put(con);
  2327. return -EBUSY;
  2328. }
  2329. dout("%s %p %lu\n", __func__, con, delay);
  2330. return 0;
  2331. }
  2332. static void queue_con(struct ceph_connection *con)
  2333. {
  2334. (void) queue_con_delay(con, 0);
  2335. }
  2336. static bool con_sock_closed(struct ceph_connection *con)
  2337. {
  2338. if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
  2339. return false;
  2340. #define CASE(x) \
  2341. case CON_STATE_ ## x: \
  2342. con->error_msg = "socket closed (con state " #x ")"; \
  2343. break;
  2344. switch (con->state) {
  2345. CASE(CLOSED);
  2346. CASE(PREOPEN);
  2347. CASE(CONNECTING);
  2348. CASE(NEGOTIATING);
  2349. CASE(OPEN);
  2350. CASE(STANDBY);
  2351. default:
  2352. pr_warning("%s con %p unrecognized state %lu\n",
  2353. __func__, con, con->state);
  2354. con->error_msg = "unrecognized con state";
  2355. BUG();
  2356. break;
  2357. }
  2358. #undef CASE
  2359. return true;
  2360. }
  2361. static bool con_backoff(struct ceph_connection *con)
  2362. {
  2363. int ret;
  2364. if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
  2365. return false;
  2366. ret = queue_con_delay(con, round_jiffies_relative(con->delay));
  2367. if (ret) {
  2368. dout("%s: con %p FAILED to back off %lu\n", __func__,
  2369. con, con->delay);
  2370. BUG_ON(ret == -ENOENT);
  2371. con_flag_set(con, CON_FLAG_BACKOFF);
  2372. }
  2373. return true;
  2374. }
  2375. /* Finish fault handling; con->mutex must *not* be held here */
  2376. static void con_fault_finish(struct ceph_connection *con)
  2377. {
  2378. /*
  2379. * in case we faulted due to authentication, invalidate our
  2380. * current tickets so that we can get new ones.
  2381. */
  2382. if (con->auth_retry && con->ops->invalidate_authorizer) {
  2383. dout("calling invalidate_authorizer()\n");
  2384. con->ops->invalidate_authorizer(con);
  2385. }
  2386. if (con->ops->fault)
  2387. con->ops->fault(con);
  2388. }
  2389. /*
  2390. * Do some work on a connection. Drop a connection ref when we're done.
  2391. */
  2392. static void con_work(struct work_struct *work)
  2393. {
  2394. struct ceph_connection *con = container_of(work, struct ceph_connection,
  2395. work.work);
  2396. bool fault;
  2397. mutex_lock(&con->mutex);
  2398. while (true) {
  2399. int ret;
  2400. if ((fault = con_sock_closed(con))) {
  2401. dout("%s: con %p SOCK_CLOSED\n", __func__, con);
  2402. break;
  2403. }
  2404. if (con_backoff(con)) {
  2405. dout("%s: con %p BACKOFF\n", __func__, con);
  2406. break;
  2407. }
  2408. if (con->state == CON_STATE_STANDBY) {
  2409. dout("%s: con %p STANDBY\n", __func__, con);
  2410. break;
  2411. }
  2412. if (con->state == CON_STATE_CLOSED) {
  2413. dout("%s: con %p CLOSED\n", __func__, con);
  2414. BUG_ON(con->sock);
  2415. break;
  2416. }
  2417. if (con->state == CON_STATE_PREOPEN) {
  2418. dout("%s: con %p PREOPEN\n", __func__, con);
  2419. BUG_ON(con->sock);
  2420. }
  2421. ret = try_read(con);
  2422. if (ret < 0) {
  2423. if (ret == -EAGAIN)
  2424. continue;
  2425. con->error_msg = "socket error on read";
  2426. fault = true;
  2427. break;
  2428. }
  2429. ret = try_write(con);
  2430. if (ret < 0) {
  2431. if (ret == -EAGAIN)
  2432. continue;
  2433. con->error_msg = "socket error on write";
  2434. fault = true;
  2435. }
  2436. break; /* If we make it to here, we're done */
  2437. }
  2438. if (fault)
  2439. con_fault(con);
  2440. mutex_unlock(&con->mutex);
  2441. if (fault)
  2442. con_fault_finish(con);
  2443. con->ops->put(con);
  2444. }
  2445. /*
  2446. * Generic error/fault handler. A retry mechanism is used with
  2447. * exponential backoff
  2448. */
  2449. static void con_fault(struct ceph_connection *con)
  2450. {
  2451. pr_warning("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  2452. ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
  2453. dout("fault %p state %lu to peer %s\n",
  2454. con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
  2455. WARN_ON(con->state != CON_STATE_CONNECTING &&
  2456. con->state != CON_STATE_NEGOTIATING &&
  2457. con->state != CON_STATE_OPEN);
  2458. con_close_socket(con);
  2459. if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
  2460. dout("fault on LOSSYTX channel, marking CLOSED\n");
  2461. con->state = CON_STATE_CLOSED;
  2462. return;
  2463. }
  2464. if (con->in_msg) {
  2465. BUG_ON(con->in_msg->con != con);
  2466. con->in_msg->con = NULL;
  2467. ceph_msg_put(con->in_msg);
  2468. con->in_msg = NULL;
  2469. con->ops->put(con);
  2470. }
  2471. /* Requeue anything that hasn't been acked */
  2472. list_splice_init(&con->out_sent, &con->out_queue);
  2473. /* If there are no messages queued or keepalive pending, place
  2474. * the connection in a STANDBY state */
  2475. if (list_empty(&con->out_queue) &&
  2476. !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2477. dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
  2478. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2479. con->state = CON_STATE_STANDBY;
  2480. } else {
  2481. /* retry after a delay. */
  2482. con->state = CON_STATE_PREOPEN;
  2483. if (con->delay == 0)
  2484. con->delay = BASE_DELAY_INTERVAL;
  2485. else if (con->delay < MAX_DELAY_INTERVAL)
  2486. con->delay *= 2;
  2487. con_flag_set(con, CON_FLAG_BACKOFF);
  2488. queue_con(con);
  2489. }
  2490. }
  2491. /*
  2492. * initialize a new messenger instance
  2493. */
  2494. void ceph_messenger_init(struct ceph_messenger *msgr,
  2495. struct ceph_entity_addr *myaddr,
  2496. u32 supported_features,
  2497. u32 required_features,
  2498. bool nocrc)
  2499. {
  2500. msgr->supported_features = supported_features;
  2501. msgr->required_features = required_features;
  2502. spin_lock_init(&msgr->global_seq_lock);
  2503. if (myaddr)
  2504. msgr->inst.addr = *myaddr;
  2505. /* select a random nonce */
  2506. msgr->inst.addr.type = 0;
  2507. get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
  2508. encode_my_addr(msgr);
  2509. msgr->nocrc = nocrc;
  2510. atomic_set(&msgr->stopping, 0);
  2511. dout("%s %p\n", __func__, msgr);
  2512. }
  2513. EXPORT_SYMBOL(ceph_messenger_init);
  2514. static void clear_standby(struct ceph_connection *con)
  2515. {
  2516. /* come back from STANDBY? */
  2517. if (con->state == CON_STATE_STANDBY) {
  2518. dout("clear_standby %p and ++connect_seq\n", con);
  2519. con->state = CON_STATE_PREOPEN;
  2520. con->connect_seq++;
  2521. WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
  2522. WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
  2523. }
  2524. }
  2525. /*
  2526. * Queue up an outgoing message on the given connection.
  2527. */
  2528. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  2529. {
  2530. /* set src+dst */
  2531. msg->hdr.src = con->msgr->inst.name;
  2532. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  2533. msg->needs_out_seq = true;
  2534. mutex_lock(&con->mutex);
  2535. if (con->state == CON_STATE_CLOSED) {
  2536. dout("con_send %p closed, dropping %p\n", con, msg);
  2537. ceph_msg_put(msg);
  2538. mutex_unlock(&con->mutex);
  2539. return;
  2540. }
  2541. BUG_ON(msg->con != NULL);
  2542. msg->con = con->ops->get(con);
  2543. BUG_ON(msg->con == NULL);
  2544. BUG_ON(!list_empty(&msg->list_head));
  2545. list_add_tail(&msg->list_head, &con->out_queue);
  2546. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  2547. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  2548. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2549. le32_to_cpu(msg->hdr.front_len),
  2550. le32_to_cpu(msg->hdr.middle_len),
  2551. le32_to_cpu(msg->hdr.data_len));
  2552. clear_standby(con);
  2553. mutex_unlock(&con->mutex);
  2554. /* if there wasn't anything waiting to send before, queue
  2555. * new work */
  2556. if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2557. queue_con(con);
  2558. }
  2559. EXPORT_SYMBOL(ceph_con_send);
  2560. /*
  2561. * Revoke a message that was previously queued for send
  2562. */
  2563. void ceph_msg_revoke(struct ceph_msg *msg)
  2564. {
  2565. struct ceph_connection *con = msg->con;
  2566. if (!con)
  2567. return; /* Message not in our possession */
  2568. mutex_lock(&con->mutex);
  2569. if (!list_empty(&msg->list_head)) {
  2570. dout("%s %p msg %p - was on queue\n", __func__, con, msg);
  2571. list_del_init(&msg->list_head);
  2572. BUG_ON(msg->con == NULL);
  2573. msg->con->ops->put(msg->con);
  2574. msg->con = NULL;
  2575. msg->hdr.seq = 0;
  2576. ceph_msg_put(msg);
  2577. }
  2578. if (con->out_msg == msg) {
  2579. dout("%s %p msg %p - was sending\n", __func__, con, msg);
  2580. con->out_msg = NULL;
  2581. if (con->out_kvec_is_msg) {
  2582. con->out_skip = con->out_kvec_bytes;
  2583. con->out_kvec_is_msg = false;
  2584. }
  2585. msg->hdr.seq = 0;
  2586. ceph_msg_put(msg);
  2587. }
  2588. mutex_unlock(&con->mutex);
  2589. }
  2590. /*
  2591. * Revoke a message that we may be reading data into
  2592. */
  2593. void ceph_msg_revoke_incoming(struct ceph_msg *msg)
  2594. {
  2595. struct ceph_connection *con;
  2596. BUG_ON(msg == NULL);
  2597. if (!msg->con) {
  2598. dout("%s msg %p null con\n", __func__, msg);
  2599. return; /* Message not in our possession */
  2600. }
  2601. con = msg->con;
  2602. mutex_lock(&con->mutex);
  2603. if (con->in_msg == msg) {
  2604. unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
  2605. unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
  2606. unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
  2607. /* skip rest of message */
  2608. dout("%s %p msg %p revoked\n", __func__, con, msg);
  2609. con->in_base_pos = con->in_base_pos -
  2610. sizeof(struct ceph_msg_header) -
  2611. front_len -
  2612. middle_len -
  2613. data_len -
  2614. sizeof(struct ceph_msg_footer);
  2615. ceph_msg_put(con->in_msg);
  2616. con->in_msg = NULL;
  2617. con->in_tag = CEPH_MSGR_TAG_READY;
  2618. con->in_seq++;
  2619. } else {
  2620. dout("%s %p in_msg %p msg %p no-op\n",
  2621. __func__, con, con->in_msg, msg);
  2622. }
  2623. mutex_unlock(&con->mutex);
  2624. }
  2625. /*
  2626. * Queue a keepalive byte to ensure the tcp connection is alive.
  2627. */
  2628. void ceph_con_keepalive(struct ceph_connection *con)
  2629. {
  2630. dout("con_keepalive %p\n", con);
  2631. mutex_lock(&con->mutex);
  2632. clear_standby(con);
  2633. mutex_unlock(&con->mutex);
  2634. if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
  2635. con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2636. queue_con(con);
  2637. }
  2638. EXPORT_SYMBOL(ceph_con_keepalive);
  2639. static void ceph_msg_data_init(struct ceph_msg_data *data)
  2640. {
  2641. data->type = CEPH_MSG_DATA_NONE;
  2642. }
  2643. void ceph_msg_data_set_pages(struct ceph_msg *msg, struct page **pages,
  2644. size_t length, size_t alignment)
  2645. {
  2646. BUG_ON(!pages);
  2647. BUG_ON(!length);
  2648. BUG_ON(msg->p.type != CEPH_MSG_DATA_NONE);
  2649. msg->p.type = CEPH_MSG_DATA_PAGES;
  2650. msg->p.pages = pages;
  2651. msg->p.length = length;
  2652. msg->p.alignment = alignment & ~PAGE_MASK;
  2653. }
  2654. EXPORT_SYMBOL(ceph_msg_data_set_pages);
  2655. void ceph_msg_data_set_pagelist(struct ceph_msg *msg,
  2656. struct ceph_pagelist *pagelist)
  2657. {
  2658. BUG_ON(!pagelist);
  2659. BUG_ON(!pagelist->length);
  2660. BUG_ON(msg->l.type != CEPH_MSG_DATA_NONE);
  2661. msg->l.type = CEPH_MSG_DATA_PAGELIST;
  2662. msg->l.pagelist = pagelist;
  2663. }
  2664. EXPORT_SYMBOL(ceph_msg_data_set_pagelist);
  2665. void ceph_msg_data_set_bio(struct ceph_msg *msg, struct bio *bio)
  2666. {
  2667. BUG_ON(!bio);
  2668. BUG_ON(msg->b.type != CEPH_MSG_DATA_NONE);
  2669. msg->b.type = CEPH_MSG_DATA_BIO;
  2670. msg->b.bio = bio;
  2671. }
  2672. EXPORT_SYMBOL(ceph_msg_data_set_bio);
  2673. /*
  2674. * construct a new message with given type, size
  2675. * the new msg has a ref count of 1.
  2676. */
  2677. struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  2678. bool can_fail)
  2679. {
  2680. struct ceph_msg *m;
  2681. m = kzalloc(sizeof(*m), flags);
  2682. if (m == NULL)
  2683. goto out;
  2684. m->hdr.type = cpu_to_le16(type);
  2685. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  2686. m->hdr.front_len = cpu_to_le32(front_len);
  2687. INIT_LIST_HEAD(&m->list_head);
  2688. kref_init(&m->kref);
  2689. ceph_msg_data_init(&m->p);
  2690. ceph_msg_data_init(&m->l);
  2691. ceph_msg_data_init(&m->b);
  2692. /* front */
  2693. m->front_max = front_len;
  2694. if (front_len) {
  2695. if (front_len > PAGE_CACHE_SIZE) {
  2696. m->front.iov_base = __vmalloc(front_len, flags,
  2697. PAGE_KERNEL);
  2698. m->front_is_vmalloc = true;
  2699. } else {
  2700. m->front.iov_base = kmalloc(front_len, flags);
  2701. }
  2702. if (m->front.iov_base == NULL) {
  2703. dout("ceph_msg_new can't allocate %d bytes\n",
  2704. front_len);
  2705. goto out2;
  2706. }
  2707. } else {
  2708. m->front.iov_base = NULL;
  2709. }
  2710. m->front.iov_len = front_len;
  2711. dout("ceph_msg_new %p front %d\n", m, front_len);
  2712. return m;
  2713. out2:
  2714. ceph_msg_put(m);
  2715. out:
  2716. if (!can_fail) {
  2717. pr_err("msg_new can't create type %d front %d\n", type,
  2718. front_len);
  2719. WARN_ON(1);
  2720. } else {
  2721. dout("msg_new can't create type %d front %d\n", type,
  2722. front_len);
  2723. }
  2724. return NULL;
  2725. }
  2726. EXPORT_SYMBOL(ceph_msg_new);
  2727. /*
  2728. * Allocate "middle" portion of a message, if it is needed and wasn't
  2729. * allocated by alloc_msg. This allows us to read a small fixed-size
  2730. * per-type header in the front and then gracefully fail (i.e.,
  2731. * propagate the error to the caller based on info in the front) when
  2732. * the middle is too large.
  2733. */
  2734. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  2735. {
  2736. int type = le16_to_cpu(msg->hdr.type);
  2737. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  2738. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  2739. ceph_msg_type_name(type), middle_len);
  2740. BUG_ON(!middle_len);
  2741. BUG_ON(msg->middle);
  2742. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  2743. if (!msg->middle)
  2744. return -ENOMEM;
  2745. return 0;
  2746. }
  2747. /*
  2748. * Allocate a message for receiving an incoming message on a
  2749. * connection, and save the result in con->in_msg. Uses the
  2750. * connection's private alloc_msg op if available.
  2751. *
  2752. * Returns 0 on success, or a negative error code.
  2753. *
  2754. * On success, if we set *skip = 1:
  2755. * - the next message should be skipped and ignored.
  2756. * - con->in_msg == NULL
  2757. * or if we set *skip = 0:
  2758. * - con->in_msg is non-null.
  2759. * On error (ENOMEM, EAGAIN, ...),
  2760. * - con->in_msg == NULL
  2761. */
  2762. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
  2763. {
  2764. struct ceph_msg_header *hdr = &con->in_hdr;
  2765. int middle_len = le32_to_cpu(hdr->middle_len);
  2766. struct ceph_msg *msg;
  2767. int ret = 0;
  2768. BUG_ON(con->in_msg != NULL);
  2769. BUG_ON(!con->ops->alloc_msg);
  2770. mutex_unlock(&con->mutex);
  2771. msg = con->ops->alloc_msg(con, hdr, skip);
  2772. mutex_lock(&con->mutex);
  2773. if (con->state != CON_STATE_OPEN) {
  2774. if (msg)
  2775. ceph_msg_put(msg);
  2776. return -EAGAIN;
  2777. }
  2778. if (msg) {
  2779. BUG_ON(*skip);
  2780. con->in_msg = msg;
  2781. con->in_msg->con = con->ops->get(con);
  2782. BUG_ON(con->in_msg->con == NULL);
  2783. } else {
  2784. /*
  2785. * Null message pointer means either we should skip
  2786. * this message or we couldn't allocate memory. The
  2787. * former is not an error.
  2788. */
  2789. if (*skip)
  2790. return 0;
  2791. con->error_msg = "error allocating memory for incoming message";
  2792. return -ENOMEM;
  2793. }
  2794. memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
  2795. if (middle_len && !con->in_msg->middle) {
  2796. ret = ceph_alloc_middle(con, con->in_msg);
  2797. if (ret < 0) {
  2798. ceph_msg_put(con->in_msg);
  2799. con->in_msg = NULL;
  2800. }
  2801. }
  2802. return ret;
  2803. }
  2804. /*
  2805. * Free a generically kmalloc'd message.
  2806. */
  2807. void ceph_msg_kfree(struct ceph_msg *m)
  2808. {
  2809. dout("msg_kfree %p\n", m);
  2810. if (m->front_is_vmalloc)
  2811. vfree(m->front.iov_base);
  2812. else
  2813. kfree(m->front.iov_base);
  2814. kfree(m);
  2815. }
  2816. /*
  2817. * Drop a msg ref. Destroy as needed.
  2818. */
  2819. void ceph_msg_last_put(struct kref *kref)
  2820. {
  2821. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  2822. dout("ceph_msg_put last one on %p\n", m);
  2823. WARN_ON(!list_empty(&m->list_head));
  2824. /* drop middle, data, if any */
  2825. if (m->middle) {
  2826. ceph_buffer_put(m->middle);
  2827. m->middle = NULL;
  2828. }
  2829. if (ceph_msg_has_pages(m)) {
  2830. m->p.length = 0;
  2831. m->p.pages = NULL;
  2832. m->p.type = CEPH_OSD_DATA_TYPE_NONE;
  2833. }
  2834. if (ceph_msg_has_pagelist(m)) {
  2835. ceph_pagelist_release(m->l.pagelist);
  2836. kfree(m->l.pagelist);
  2837. m->l.pagelist = NULL;
  2838. m->l.type = CEPH_OSD_DATA_TYPE_NONE;
  2839. }
  2840. if (ceph_msg_has_bio(m)) {
  2841. m->b.bio = NULL;
  2842. m->b.type = CEPH_OSD_DATA_TYPE_NONE;
  2843. }
  2844. if (m->pool)
  2845. ceph_msgpool_put(m->pool, m);
  2846. else
  2847. ceph_msg_kfree(m);
  2848. }
  2849. EXPORT_SYMBOL(ceph_msg_last_put);
  2850. void ceph_msg_dump(struct ceph_msg *msg)
  2851. {
  2852. pr_debug("msg_dump %p (front_max %d length %zd)\n", msg,
  2853. msg->front_max, msg->p.length);
  2854. print_hex_dump(KERN_DEBUG, "header: ",
  2855. DUMP_PREFIX_OFFSET, 16, 1,
  2856. &msg->hdr, sizeof(msg->hdr), true);
  2857. print_hex_dump(KERN_DEBUG, " front: ",
  2858. DUMP_PREFIX_OFFSET, 16, 1,
  2859. msg->front.iov_base, msg->front.iov_len, true);
  2860. if (msg->middle)
  2861. print_hex_dump(KERN_DEBUG, "middle: ",
  2862. DUMP_PREFIX_OFFSET, 16, 1,
  2863. msg->middle->vec.iov_base,
  2864. msg->middle->vec.iov_len, true);
  2865. print_hex_dump(KERN_DEBUG, "footer: ",
  2866. DUMP_PREFIX_OFFSET, 16, 1,
  2867. &msg->footer, sizeof(msg->footer), true);
  2868. }
  2869. EXPORT_SYMBOL(ceph_msg_dump);