messenger.c 81 KB

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