messenger.c 79 KB

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