messenger.c 79 KB

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