messenger.c 83 KB

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