messenger.c 86 KB

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