messenger.c 84 KB

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