messenger.c 83 KB

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