messenger.c 82 KB

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