messenger.c 81 KB

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