messenger.c 82 KB

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