messenger.c 80 KB

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