messenger.c 77 KB

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