messenger.c 81 KB

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