messenger.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246
  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_cursor *cursor,
  611. size_t length)
  612. {
  613. struct ceph_msg_data *data = cursor->data;
  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_cursor *cursor,
  626. size_t *page_offset,
  627. size_t *length)
  628. {
  629. struct ceph_msg_data *data = cursor->data;
  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 > cursor->resid);
  647. BUG_ON(*page_offset + *length > PAGE_SIZE);
  648. return bio_vec->bv_page;
  649. }
  650. static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
  651. size_t bytes)
  652. {
  653. struct bio *bio;
  654. struct bio_vec *bio_vec;
  655. unsigned int index;
  656. BUG_ON(cursor->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 /* CONFIG_BLOCK */
  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_cursor *cursor,
  692. size_t length)
  693. {
  694. struct ceph_msg_data *data = cursor->data;
  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); /* short reads are OK */
  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. BUG_ON(length > SIZE_MAX - cursor->page_offset);
  707. cursor->last_piece = (size_t)cursor->page_offset + length <= PAGE_SIZE;
  708. }
  709. static struct page *
  710. ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
  711. size_t *page_offset, size_t *length)
  712. {
  713. struct ceph_msg_data *data = cursor->data;
  714. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  715. BUG_ON(cursor->page_index >= cursor->page_count);
  716. BUG_ON(cursor->page_offset >= PAGE_SIZE);
  717. *page_offset = cursor->page_offset;
  718. if (cursor->last_piece)
  719. *length = cursor->resid;
  720. else
  721. *length = PAGE_SIZE - *page_offset;
  722. return data->pages[cursor->page_index];
  723. }
  724. static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
  725. size_t bytes)
  726. {
  727. BUG_ON(cursor->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 = (cursor->page_offset + bytes) & ~PAGE_MASK;
  732. if (!bytes || cursor->page_offset)
  733. return false; /* more bytes to process in the current page */
  734. /* Move on to the next page; offset is already at 0 */
  735. BUG_ON(cursor->page_index >= cursor->page_count);
  736. cursor->page_index++;
  737. cursor->last_piece = cursor->resid <= PAGE_SIZE;
  738. return true;
  739. }
  740. /*
  741. * For a pagelist, a piece is whatever remains to be consumed in the
  742. * first page in the list, or the front of the next page.
  743. */
  744. static void
  745. ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
  746. size_t length)
  747. {
  748. struct ceph_msg_data *data = cursor->data;
  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); /* short reads are OK */
  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 *
  765. ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
  766. size_t *page_offset, size_t *length)
  767. {
  768. struct ceph_msg_data *data = cursor->data;
  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. /* offset of first page in pagelist is always 0 */
  776. *page_offset = cursor->offset & ~PAGE_MASK;
  777. if (cursor->last_piece)
  778. *length = cursor->resid;
  779. else
  780. *length = PAGE_SIZE - *page_offset;
  781. return cursor->page;
  782. }
  783. static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
  784. size_t bytes)
  785. {
  786. struct ceph_msg_data *data = cursor->data;
  787. struct ceph_pagelist *pagelist;
  788. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  789. pagelist = data->pagelist;
  790. BUG_ON(!pagelist);
  791. BUG_ON(cursor->offset + cursor->resid != pagelist->length);
  792. BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
  793. /* Advance the cursor offset */
  794. cursor->resid -= bytes;
  795. cursor->offset += bytes;
  796. /* offset of first page in pagelist is always 0 */
  797. if (!bytes || cursor->offset & ~PAGE_MASK)
  798. return false; /* more bytes to process in the current page */
  799. /* Move on to the next page */
  800. BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
  801. cursor->page = list_entry_next(cursor->page, lru);
  802. cursor->last_piece = cursor->resid <= PAGE_SIZE;
  803. return true;
  804. }
  805. /*
  806. * Message data is handled (sent or received) in pieces, where each
  807. * piece resides on a single page. The network layer might not
  808. * consume an entire piece at once. A data item's cursor keeps
  809. * track of which piece is next to process and how much remains to
  810. * be processed in that piece. It also tracks whether the current
  811. * piece is the last one in the data item.
  812. */
  813. static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
  814. {
  815. struct ceph_msg_data_cursor *cursor = &msg->cursor;
  816. cursor->data = msg->data;
  817. switch (cursor->data->type) {
  818. case CEPH_MSG_DATA_PAGELIST:
  819. ceph_msg_data_pagelist_cursor_init(cursor, length);
  820. break;
  821. case CEPH_MSG_DATA_PAGES:
  822. ceph_msg_data_pages_cursor_init(cursor, length);
  823. break;
  824. #ifdef CONFIG_BLOCK
  825. case CEPH_MSG_DATA_BIO:
  826. ceph_msg_data_bio_cursor_init(cursor, length);
  827. break;
  828. #endif /* CONFIG_BLOCK */
  829. case CEPH_MSG_DATA_NONE:
  830. default:
  831. /* BUG(); */
  832. break;
  833. }
  834. cursor->need_crc = true;
  835. }
  836. /*
  837. * Return the page containing the next piece to process for a given
  838. * data item, and supply the page offset and length of that piece.
  839. * Indicate whether this is the last piece in this data item.
  840. */
  841. static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
  842. size_t *page_offset, size_t *length,
  843. bool *last_piece)
  844. {
  845. struct page *page;
  846. switch (cursor->data->type) {
  847. case CEPH_MSG_DATA_PAGELIST:
  848. page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
  849. break;
  850. case CEPH_MSG_DATA_PAGES:
  851. page = ceph_msg_data_pages_next(cursor, page_offset, length);
  852. break;
  853. #ifdef CONFIG_BLOCK
  854. case CEPH_MSG_DATA_BIO:
  855. page = ceph_msg_data_bio_next(cursor, page_offset, length);
  856. break;
  857. #endif /* CONFIG_BLOCK */
  858. case CEPH_MSG_DATA_NONE:
  859. default:
  860. page = NULL;
  861. break;
  862. }
  863. BUG_ON(!page);
  864. BUG_ON(*page_offset + *length > PAGE_SIZE);
  865. BUG_ON(!*length);
  866. if (last_piece)
  867. *last_piece = cursor->last_piece;
  868. return page;
  869. }
  870. /*
  871. * Returns true if the result moves the cursor on to the next piece
  872. * of the data item.
  873. */
  874. static bool ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
  875. size_t bytes)
  876. {
  877. bool new_piece;
  878. BUG_ON(bytes > cursor->resid);
  879. switch (cursor->data->type) {
  880. case CEPH_MSG_DATA_PAGELIST:
  881. new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
  882. break;
  883. case CEPH_MSG_DATA_PAGES:
  884. new_piece = ceph_msg_data_pages_advance(cursor, bytes);
  885. break;
  886. #ifdef CONFIG_BLOCK
  887. case CEPH_MSG_DATA_BIO:
  888. new_piece = ceph_msg_data_bio_advance(cursor, bytes);
  889. break;
  890. #endif /* CONFIG_BLOCK */
  891. case CEPH_MSG_DATA_NONE:
  892. default:
  893. BUG();
  894. break;
  895. }
  896. cursor->need_crc = new_piece;
  897. return new_piece;
  898. }
  899. static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
  900. {
  901. BUG_ON(!msg);
  902. BUG_ON(!data_len);
  903. /* Initialize data cursor */
  904. ceph_msg_data_cursor_init(msg, (size_t)data_len);
  905. }
  906. /*
  907. * Prepare footer for currently outgoing message, and finish things
  908. * off. Assumes out_kvec* are already valid.. we just add on to the end.
  909. */
  910. static void prepare_write_message_footer(struct ceph_connection *con)
  911. {
  912. struct ceph_msg *m = con->out_msg;
  913. int v = con->out_kvec_left;
  914. m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
  915. dout("prepare_write_message_footer %p\n", con);
  916. con->out_kvec_is_msg = true;
  917. con->out_kvec[v].iov_base = &m->footer;
  918. con->out_kvec[v].iov_len = sizeof(m->footer);
  919. con->out_kvec_bytes += sizeof(m->footer);
  920. con->out_kvec_left++;
  921. con->out_more = m->more_to_follow;
  922. con->out_msg_done = true;
  923. }
  924. /*
  925. * Prepare headers for the next outgoing message.
  926. */
  927. static void prepare_write_message(struct ceph_connection *con)
  928. {
  929. struct ceph_msg *m;
  930. u32 crc;
  931. con_out_kvec_reset(con);
  932. con->out_kvec_is_msg = true;
  933. con->out_msg_done = false;
  934. /* Sneak an ack in there first? If we can get it into the same
  935. * TCP packet that's a good thing. */
  936. if (con->in_seq > con->in_seq_acked) {
  937. con->in_seq_acked = con->in_seq;
  938. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  939. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  940. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  941. &con->out_temp_ack);
  942. }
  943. BUG_ON(list_empty(&con->out_queue));
  944. m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
  945. con->out_msg = m;
  946. BUG_ON(m->con != con);
  947. /* put message on sent list */
  948. ceph_msg_get(m);
  949. list_move_tail(&m->list_head, &con->out_sent);
  950. /*
  951. * only assign outgoing seq # if we haven't sent this message
  952. * yet. if it is requeued, resend with it's original seq.
  953. */
  954. if (m->needs_out_seq) {
  955. m->hdr.seq = cpu_to_le64(++con->out_seq);
  956. m->needs_out_seq = false;
  957. }
  958. WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
  959. dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\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. m->data_length);
  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->data_length) {
  989. prepare_message_data(con->out_msg, m->data_length);
  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 u32 ceph_crc32c_page(u32 crc, struct page *page,
  1156. unsigned int page_offset,
  1157. unsigned int length)
  1158. {
  1159. char *kaddr;
  1160. kaddr = kmap(page);
  1161. BUG_ON(kaddr == NULL);
  1162. crc = crc32c(crc, kaddr + page_offset, length);
  1163. kunmap(page);
  1164. return crc;
  1165. }
  1166. /*
  1167. * Write as much message data payload as we can. If we finish, queue
  1168. * up the footer.
  1169. * 1 -> done, footer is now queued in out_kvec[].
  1170. * 0 -> socket full, but more to do
  1171. * <0 -> error
  1172. */
  1173. static int write_partial_message_data(struct ceph_connection *con)
  1174. {
  1175. struct ceph_msg *msg = con->out_msg;
  1176. struct ceph_msg_data_cursor *cursor = &msg->cursor;
  1177. bool do_datacrc = !con->msgr->nocrc;
  1178. u32 crc;
  1179. dout("%s %p msg %p\n", __func__, con, msg);
  1180. if (WARN_ON(!msg->data))
  1181. return -EINVAL;
  1182. /*
  1183. * Iterate through each page that contains data to be
  1184. * written, and send as much as possible for each.
  1185. *
  1186. * If we are calculating the data crc (the default), we will
  1187. * need to map the page. If we have no pages, they have
  1188. * been revoked, so use the zero page.
  1189. */
  1190. crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
  1191. while (cursor->resid) {
  1192. struct page *page;
  1193. size_t page_offset;
  1194. size_t length;
  1195. bool last_piece;
  1196. bool need_crc;
  1197. int ret;
  1198. page = ceph_msg_data_next(&msg->cursor, &page_offset, &length,
  1199. &last_piece);
  1200. ret = ceph_tcp_sendpage(con->sock, page, page_offset,
  1201. length, last_piece);
  1202. if (ret <= 0) {
  1203. if (do_datacrc)
  1204. msg->footer.data_crc = cpu_to_le32(crc);
  1205. return ret;
  1206. }
  1207. if (do_datacrc && cursor->need_crc)
  1208. crc = ceph_crc32c_page(crc, page, page_offset, length);
  1209. need_crc = ceph_msg_data_advance(&msg->cursor, (size_t)ret);
  1210. }
  1211. dout("%s %p msg %p done\n", __func__, con, msg);
  1212. /* prepare and queue up footer, too */
  1213. if (do_datacrc)
  1214. msg->footer.data_crc = cpu_to_le32(crc);
  1215. else
  1216. msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
  1217. con_out_kvec_reset(con);
  1218. prepare_write_message_footer(con);
  1219. return 1; /* must return > 0 to indicate success */
  1220. }
  1221. /*
  1222. * write some zeros
  1223. */
  1224. static int write_partial_skip(struct ceph_connection *con)
  1225. {
  1226. int ret;
  1227. while (con->out_skip > 0) {
  1228. size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
  1229. ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
  1230. if (ret <= 0)
  1231. goto out;
  1232. con->out_skip -= ret;
  1233. }
  1234. ret = 1;
  1235. out:
  1236. return ret;
  1237. }
  1238. /*
  1239. * Prepare to read connection handshake, or an ack.
  1240. */
  1241. static void prepare_read_banner(struct ceph_connection *con)
  1242. {
  1243. dout("prepare_read_banner %p\n", con);
  1244. con->in_base_pos = 0;
  1245. }
  1246. static void prepare_read_connect(struct ceph_connection *con)
  1247. {
  1248. dout("prepare_read_connect %p\n", con);
  1249. con->in_base_pos = 0;
  1250. }
  1251. static void prepare_read_ack(struct ceph_connection *con)
  1252. {
  1253. dout("prepare_read_ack %p\n", con);
  1254. con->in_base_pos = 0;
  1255. }
  1256. static void prepare_read_seq(struct ceph_connection *con)
  1257. {
  1258. dout("prepare_read_seq %p\n", con);
  1259. con->in_base_pos = 0;
  1260. con->in_tag = CEPH_MSGR_TAG_SEQ;
  1261. }
  1262. static void prepare_read_tag(struct ceph_connection *con)
  1263. {
  1264. dout("prepare_read_tag %p\n", con);
  1265. con->in_base_pos = 0;
  1266. con->in_tag = CEPH_MSGR_TAG_READY;
  1267. }
  1268. /*
  1269. * Prepare to read a message.
  1270. */
  1271. static int prepare_read_message(struct ceph_connection *con)
  1272. {
  1273. dout("prepare_read_message %p\n", con);
  1274. BUG_ON(con->in_msg != NULL);
  1275. con->in_base_pos = 0;
  1276. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  1277. return 0;
  1278. }
  1279. static int read_partial(struct ceph_connection *con,
  1280. int end, int size, void *object)
  1281. {
  1282. while (con->in_base_pos < end) {
  1283. int left = end - con->in_base_pos;
  1284. int have = size - left;
  1285. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  1286. if (ret <= 0)
  1287. return ret;
  1288. con->in_base_pos += ret;
  1289. }
  1290. return 1;
  1291. }
  1292. /*
  1293. * Read all or part of the connect-side handshake on a new connection
  1294. */
  1295. static int read_partial_banner(struct ceph_connection *con)
  1296. {
  1297. int size;
  1298. int end;
  1299. int ret;
  1300. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  1301. /* peer's banner */
  1302. size = strlen(CEPH_BANNER);
  1303. end = size;
  1304. ret = read_partial(con, end, size, con->in_banner);
  1305. if (ret <= 0)
  1306. goto out;
  1307. size = sizeof (con->actual_peer_addr);
  1308. end += size;
  1309. ret = read_partial(con, end, size, &con->actual_peer_addr);
  1310. if (ret <= 0)
  1311. goto out;
  1312. size = sizeof (con->peer_addr_for_me);
  1313. end += size;
  1314. ret = read_partial(con, end, size, &con->peer_addr_for_me);
  1315. if (ret <= 0)
  1316. goto out;
  1317. out:
  1318. return ret;
  1319. }
  1320. static int read_partial_connect(struct ceph_connection *con)
  1321. {
  1322. int size;
  1323. int end;
  1324. int ret;
  1325. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  1326. size = sizeof (con->in_reply);
  1327. end = size;
  1328. ret = read_partial(con, end, size, &con->in_reply);
  1329. if (ret <= 0)
  1330. goto out;
  1331. size = le32_to_cpu(con->in_reply.authorizer_len);
  1332. end += size;
  1333. ret = read_partial(con, end, size, con->auth_reply_buf);
  1334. if (ret <= 0)
  1335. goto out;
  1336. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  1337. con, (int)con->in_reply.tag,
  1338. le32_to_cpu(con->in_reply.connect_seq),
  1339. le32_to_cpu(con->in_reply.global_seq));
  1340. out:
  1341. return ret;
  1342. }
  1343. /*
  1344. * Verify the hello banner looks okay.
  1345. */
  1346. static int verify_hello(struct ceph_connection *con)
  1347. {
  1348. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  1349. pr_err("connect to %s got bad banner\n",
  1350. ceph_pr_addr(&con->peer_addr.in_addr));
  1351. con->error_msg = "protocol error, bad banner";
  1352. return -1;
  1353. }
  1354. return 0;
  1355. }
  1356. static bool addr_is_blank(struct sockaddr_storage *ss)
  1357. {
  1358. switch (ss->ss_family) {
  1359. case AF_INET:
  1360. return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
  1361. case AF_INET6:
  1362. return
  1363. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
  1364. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
  1365. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
  1366. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
  1367. }
  1368. return false;
  1369. }
  1370. static int addr_port(struct sockaddr_storage *ss)
  1371. {
  1372. switch (ss->ss_family) {
  1373. case AF_INET:
  1374. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  1375. case AF_INET6:
  1376. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  1377. }
  1378. return 0;
  1379. }
  1380. static void addr_set_port(struct sockaddr_storage *ss, int p)
  1381. {
  1382. switch (ss->ss_family) {
  1383. case AF_INET:
  1384. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  1385. break;
  1386. case AF_INET6:
  1387. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  1388. break;
  1389. }
  1390. }
  1391. /*
  1392. * Unlike other *_pton function semantics, zero indicates success.
  1393. */
  1394. static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
  1395. char delim, const char **ipend)
  1396. {
  1397. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  1398. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  1399. memset(ss, 0, sizeof(*ss));
  1400. if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
  1401. ss->ss_family = AF_INET;
  1402. return 0;
  1403. }
  1404. if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
  1405. ss->ss_family = AF_INET6;
  1406. return 0;
  1407. }
  1408. return -EINVAL;
  1409. }
  1410. /*
  1411. * Extract hostname string and resolve using kernel DNS facility.
  1412. */
  1413. #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
  1414. static int ceph_dns_resolve_name(const char *name, size_t namelen,
  1415. struct sockaddr_storage *ss, char delim, const char **ipend)
  1416. {
  1417. const char *end, *delim_p;
  1418. char *colon_p, *ip_addr = NULL;
  1419. int ip_len, ret;
  1420. /*
  1421. * The end of the hostname occurs immediately preceding the delimiter or
  1422. * the port marker (':') where the delimiter takes precedence.
  1423. */
  1424. delim_p = memchr(name, delim, namelen);
  1425. colon_p = memchr(name, ':', namelen);
  1426. if (delim_p && colon_p)
  1427. end = delim_p < colon_p ? delim_p : colon_p;
  1428. else if (!delim_p && colon_p)
  1429. end = colon_p;
  1430. else {
  1431. end = delim_p;
  1432. if (!end) /* case: hostname:/ */
  1433. end = name + namelen;
  1434. }
  1435. if (end <= name)
  1436. return -EINVAL;
  1437. /* do dns_resolve upcall */
  1438. ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
  1439. if (ip_len > 0)
  1440. ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
  1441. else
  1442. ret = -ESRCH;
  1443. kfree(ip_addr);
  1444. *ipend = end;
  1445. pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
  1446. ret, ret ? "failed" : ceph_pr_addr(ss));
  1447. return ret;
  1448. }
  1449. #else
  1450. static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
  1451. struct sockaddr_storage *ss, char delim, const char **ipend)
  1452. {
  1453. return -EINVAL;
  1454. }
  1455. #endif
  1456. /*
  1457. * Parse a server name (IP or hostname). If a valid IP address is not found
  1458. * then try to extract a hostname to resolve using userspace DNS upcall.
  1459. */
  1460. static int ceph_parse_server_name(const char *name, size_t namelen,
  1461. struct sockaddr_storage *ss, char delim, const char **ipend)
  1462. {
  1463. int ret;
  1464. ret = ceph_pton(name, namelen, ss, delim, ipend);
  1465. if (ret)
  1466. ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
  1467. return ret;
  1468. }
  1469. /*
  1470. * Parse an ip[:port] list into an addr array. Use the default
  1471. * monitor port if a port isn't specified.
  1472. */
  1473. int ceph_parse_ips(const char *c, const char *end,
  1474. struct ceph_entity_addr *addr,
  1475. int max_count, int *count)
  1476. {
  1477. int i, ret = -EINVAL;
  1478. const char *p = c;
  1479. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  1480. for (i = 0; i < max_count; i++) {
  1481. const char *ipend;
  1482. struct sockaddr_storage *ss = &addr[i].in_addr;
  1483. int port;
  1484. char delim = ',';
  1485. if (*p == '[') {
  1486. delim = ']';
  1487. p++;
  1488. }
  1489. ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
  1490. if (ret)
  1491. goto bad;
  1492. ret = -EINVAL;
  1493. p = ipend;
  1494. if (delim == ']') {
  1495. if (*p != ']') {
  1496. dout("missing matching ']'\n");
  1497. goto bad;
  1498. }
  1499. p++;
  1500. }
  1501. /* port? */
  1502. if (p < end && *p == ':') {
  1503. port = 0;
  1504. p++;
  1505. while (p < end && *p >= '0' && *p <= '9') {
  1506. port = (port * 10) + (*p - '0');
  1507. p++;
  1508. }
  1509. if (port > 65535 || port == 0)
  1510. goto bad;
  1511. } else {
  1512. port = CEPH_MON_PORT;
  1513. }
  1514. addr_set_port(ss, port);
  1515. dout("parse_ips got %s\n", ceph_pr_addr(ss));
  1516. if (p == end)
  1517. break;
  1518. if (*p != ',')
  1519. goto bad;
  1520. p++;
  1521. }
  1522. if (p != end)
  1523. goto bad;
  1524. if (count)
  1525. *count = i + 1;
  1526. return 0;
  1527. bad:
  1528. pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
  1529. return ret;
  1530. }
  1531. EXPORT_SYMBOL(ceph_parse_ips);
  1532. static int process_banner(struct ceph_connection *con)
  1533. {
  1534. dout("process_banner on %p\n", con);
  1535. if (verify_hello(con) < 0)
  1536. return -1;
  1537. ceph_decode_addr(&con->actual_peer_addr);
  1538. ceph_decode_addr(&con->peer_addr_for_me);
  1539. /*
  1540. * Make sure the other end is who we wanted. note that the other
  1541. * end may not yet know their ip address, so if it's 0.0.0.0, give
  1542. * them the benefit of the doubt.
  1543. */
  1544. if (memcmp(&con->peer_addr, &con->actual_peer_addr,
  1545. sizeof(con->peer_addr)) != 0 &&
  1546. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  1547. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  1548. pr_warning("wrong peer, want %s/%d, got %s/%d\n",
  1549. ceph_pr_addr(&con->peer_addr.in_addr),
  1550. (int)le32_to_cpu(con->peer_addr.nonce),
  1551. ceph_pr_addr(&con->actual_peer_addr.in_addr),
  1552. (int)le32_to_cpu(con->actual_peer_addr.nonce));
  1553. con->error_msg = "wrong peer at address";
  1554. return -1;
  1555. }
  1556. /*
  1557. * did we learn our address?
  1558. */
  1559. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  1560. int port = addr_port(&con->msgr->inst.addr.in_addr);
  1561. memcpy(&con->msgr->inst.addr.in_addr,
  1562. &con->peer_addr_for_me.in_addr,
  1563. sizeof(con->peer_addr_for_me.in_addr));
  1564. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  1565. encode_my_addr(con->msgr);
  1566. dout("process_banner learned my addr is %s\n",
  1567. ceph_pr_addr(&con->msgr->inst.addr.in_addr));
  1568. }
  1569. return 0;
  1570. }
  1571. static int process_connect(struct ceph_connection *con)
  1572. {
  1573. u64 sup_feat = con->msgr->supported_features;
  1574. u64 req_feat = con->msgr->required_features;
  1575. u64 server_feat = le64_to_cpu(con->in_reply.features);
  1576. int ret;
  1577. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  1578. switch (con->in_reply.tag) {
  1579. case CEPH_MSGR_TAG_FEATURES:
  1580. pr_err("%s%lld %s feature set mismatch,"
  1581. " my %llx < server's %llx, missing %llx\n",
  1582. ENTITY_NAME(con->peer_name),
  1583. ceph_pr_addr(&con->peer_addr.in_addr),
  1584. sup_feat, server_feat, server_feat & ~sup_feat);
  1585. con->error_msg = "missing required protocol features";
  1586. reset_connection(con);
  1587. return -1;
  1588. case CEPH_MSGR_TAG_BADPROTOVER:
  1589. pr_err("%s%lld %s protocol version mismatch,"
  1590. " my %d != server's %d\n",
  1591. ENTITY_NAME(con->peer_name),
  1592. ceph_pr_addr(&con->peer_addr.in_addr),
  1593. le32_to_cpu(con->out_connect.protocol_version),
  1594. le32_to_cpu(con->in_reply.protocol_version));
  1595. con->error_msg = "protocol version mismatch";
  1596. reset_connection(con);
  1597. return -1;
  1598. case CEPH_MSGR_TAG_BADAUTHORIZER:
  1599. con->auth_retry++;
  1600. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  1601. con->auth_retry);
  1602. if (con->auth_retry == 2) {
  1603. con->error_msg = "connect authorization failure";
  1604. return -1;
  1605. }
  1606. con_out_kvec_reset(con);
  1607. ret = prepare_write_connect(con);
  1608. if (ret < 0)
  1609. return ret;
  1610. prepare_read_connect(con);
  1611. break;
  1612. case CEPH_MSGR_TAG_RESETSESSION:
  1613. /*
  1614. * If we connected with a large connect_seq but the peer
  1615. * has no record of a session with us (no connection, or
  1616. * connect_seq == 0), they will send RESETSESION to indicate
  1617. * that they must have reset their session, and may have
  1618. * dropped messages.
  1619. */
  1620. dout("process_connect got RESET peer seq %u\n",
  1621. le32_to_cpu(con->in_reply.connect_seq));
  1622. pr_err("%s%lld %s connection reset\n",
  1623. ENTITY_NAME(con->peer_name),
  1624. ceph_pr_addr(&con->peer_addr.in_addr));
  1625. reset_connection(con);
  1626. con_out_kvec_reset(con);
  1627. ret = prepare_write_connect(con);
  1628. if (ret < 0)
  1629. return ret;
  1630. prepare_read_connect(con);
  1631. /* Tell ceph about it. */
  1632. mutex_unlock(&con->mutex);
  1633. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  1634. if (con->ops->peer_reset)
  1635. con->ops->peer_reset(con);
  1636. mutex_lock(&con->mutex);
  1637. if (con->state != CON_STATE_NEGOTIATING)
  1638. return -EAGAIN;
  1639. break;
  1640. case CEPH_MSGR_TAG_RETRY_SESSION:
  1641. /*
  1642. * If we sent a smaller connect_seq than the peer has, try
  1643. * again with a larger value.
  1644. */
  1645. dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
  1646. le32_to_cpu(con->out_connect.connect_seq),
  1647. le32_to_cpu(con->in_reply.connect_seq));
  1648. con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
  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. break;
  1655. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1656. /*
  1657. * If we sent a smaller global_seq than the peer has, try
  1658. * again with a larger value.
  1659. */
  1660. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1661. con->peer_global_seq,
  1662. le32_to_cpu(con->in_reply.global_seq));
  1663. get_global_seq(con->msgr,
  1664. le32_to_cpu(con->in_reply.global_seq));
  1665. con_out_kvec_reset(con);
  1666. ret = prepare_write_connect(con);
  1667. if (ret < 0)
  1668. return ret;
  1669. prepare_read_connect(con);
  1670. break;
  1671. case CEPH_MSGR_TAG_SEQ:
  1672. case CEPH_MSGR_TAG_READY:
  1673. if (req_feat & ~server_feat) {
  1674. pr_err("%s%lld %s protocol feature mismatch,"
  1675. " my required %llx > server's %llx, need %llx\n",
  1676. ENTITY_NAME(con->peer_name),
  1677. ceph_pr_addr(&con->peer_addr.in_addr),
  1678. req_feat, server_feat, req_feat & ~server_feat);
  1679. con->error_msg = "missing required protocol features";
  1680. reset_connection(con);
  1681. return -1;
  1682. }
  1683. WARN_ON(con->state != CON_STATE_NEGOTIATING);
  1684. con->state = CON_STATE_OPEN;
  1685. con->auth_retry = 0; /* we authenticated; clear flag */
  1686. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1687. con->connect_seq++;
  1688. con->peer_features = server_feat;
  1689. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1690. con->peer_global_seq,
  1691. le32_to_cpu(con->in_reply.connect_seq),
  1692. con->connect_seq);
  1693. WARN_ON(con->connect_seq !=
  1694. le32_to_cpu(con->in_reply.connect_seq));
  1695. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1696. con_flag_set(con, CON_FLAG_LOSSYTX);
  1697. con->delay = 0; /* reset backoff memory */
  1698. if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
  1699. prepare_write_seq(con);
  1700. prepare_read_seq(con);
  1701. } else {
  1702. prepare_read_tag(con);
  1703. }
  1704. break;
  1705. case CEPH_MSGR_TAG_WAIT:
  1706. /*
  1707. * If there is a connection race (we are opening
  1708. * connections to each other), one of us may just have
  1709. * to WAIT. This shouldn't happen if we are the
  1710. * client.
  1711. */
  1712. pr_err("process_connect got WAIT as client\n");
  1713. con->error_msg = "protocol error, got WAIT as client";
  1714. return -1;
  1715. default:
  1716. pr_err("connect protocol error, will retry\n");
  1717. con->error_msg = "protocol error, garbage tag during connect";
  1718. return -1;
  1719. }
  1720. return 0;
  1721. }
  1722. /*
  1723. * read (part of) an ack
  1724. */
  1725. static int read_partial_ack(struct ceph_connection *con)
  1726. {
  1727. int size = sizeof (con->in_temp_ack);
  1728. int end = size;
  1729. return read_partial(con, end, size, &con->in_temp_ack);
  1730. }
  1731. /*
  1732. * We can finally discard anything that's been acked.
  1733. */
  1734. static void process_ack(struct ceph_connection *con)
  1735. {
  1736. struct ceph_msg *m;
  1737. u64 ack = le64_to_cpu(con->in_temp_ack);
  1738. u64 seq;
  1739. while (!list_empty(&con->out_sent)) {
  1740. m = list_first_entry(&con->out_sent, struct ceph_msg,
  1741. list_head);
  1742. seq = le64_to_cpu(m->hdr.seq);
  1743. if (seq > ack)
  1744. break;
  1745. dout("got ack for seq %llu type %d at %p\n", seq,
  1746. le16_to_cpu(m->hdr.type), m);
  1747. m->ack_stamp = jiffies;
  1748. ceph_msg_remove(m);
  1749. }
  1750. prepare_read_tag(con);
  1751. }
  1752. static int read_partial_message_section(struct ceph_connection *con,
  1753. struct kvec *section,
  1754. unsigned int sec_len, u32 *crc)
  1755. {
  1756. int ret, left;
  1757. BUG_ON(!section);
  1758. while (section->iov_len < sec_len) {
  1759. BUG_ON(section->iov_base == NULL);
  1760. left = sec_len - section->iov_len;
  1761. ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
  1762. section->iov_len, left);
  1763. if (ret <= 0)
  1764. return ret;
  1765. section->iov_len += ret;
  1766. }
  1767. if (section->iov_len == sec_len)
  1768. *crc = crc32c(0, section->iov_base, section->iov_len);
  1769. return 1;
  1770. }
  1771. static int read_partial_msg_data(struct ceph_connection *con)
  1772. {
  1773. struct ceph_msg *msg = con->in_msg;
  1774. struct ceph_msg_data_cursor *cursor = &msg->cursor;
  1775. const bool do_datacrc = !con->msgr->nocrc;
  1776. struct page *page;
  1777. size_t page_offset;
  1778. size_t length;
  1779. u32 crc = 0;
  1780. int ret;
  1781. BUG_ON(!msg);
  1782. if (!msg->data)
  1783. return -EIO;
  1784. if (do_datacrc)
  1785. crc = con->in_data_crc;
  1786. while (cursor->resid) {
  1787. page = ceph_msg_data_next(&msg->cursor, &page_offset, &length,
  1788. NULL);
  1789. ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
  1790. if (ret <= 0) {
  1791. if (do_datacrc)
  1792. con->in_data_crc = crc;
  1793. return ret;
  1794. }
  1795. if (do_datacrc)
  1796. crc = ceph_crc32c_page(crc, page, page_offset, ret);
  1797. (void) ceph_msg_data_advance(&msg->cursor, (size_t)ret);
  1798. }
  1799. if (do_datacrc)
  1800. con->in_data_crc = crc;
  1801. return 1; /* must return > 0 to indicate success */
  1802. }
  1803. /*
  1804. * read (part of) a message.
  1805. */
  1806. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
  1807. static int read_partial_message(struct ceph_connection *con)
  1808. {
  1809. struct ceph_msg *m = con->in_msg;
  1810. int size;
  1811. int end;
  1812. int ret;
  1813. unsigned int front_len, middle_len, data_len;
  1814. bool do_datacrc = !con->msgr->nocrc;
  1815. u64 seq;
  1816. u32 crc;
  1817. dout("read_partial_message con %p msg %p\n", con, m);
  1818. /* header */
  1819. size = sizeof (con->in_hdr);
  1820. end = size;
  1821. ret = read_partial(con, end, size, &con->in_hdr);
  1822. if (ret <= 0)
  1823. return ret;
  1824. crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
  1825. if (cpu_to_le32(crc) != con->in_hdr.crc) {
  1826. pr_err("read_partial_message bad hdr "
  1827. " crc %u != expected %u\n",
  1828. crc, con->in_hdr.crc);
  1829. return -EBADMSG;
  1830. }
  1831. front_len = le32_to_cpu(con->in_hdr.front_len);
  1832. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  1833. return -EIO;
  1834. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1835. if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
  1836. return -EIO;
  1837. data_len = le32_to_cpu(con->in_hdr.data_len);
  1838. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  1839. return -EIO;
  1840. /* verify seq# */
  1841. seq = le64_to_cpu(con->in_hdr.seq);
  1842. if ((s64)seq - (s64)con->in_seq < 1) {
  1843. pr_info("skipping %s%lld %s seq %lld expected %lld\n",
  1844. ENTITY_NAME(con->peer_name),
  1845. ceph_pr_addr(&con->peer_addr.in_addr),
  1846. seq, con->in_seq + 1);
  1847. con->in_base_pos = -front_len - middle_len - data_len -
  1848. sizeof(m->footer);
  1849. con->in_tag = CEPH_MSGR_TAG_READY;
  1850. return 0;
  1851. } else if ((s64)seq - (s64)con->in_seq > 1) {
  1852. pr_err("read_partial_message bad seq %lld expected %lld\n",
  1853. seq, con->in_seq + 1);
  1854. con->error_msg = "bad message sequence # for incoming message";
  1855. return -EBADMSG;
  1856. }
  1857. /* allocate message? */
  1858. if (!con->in_msg) {
  1859. int skip = 0;
  1860. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  1861. front_len, data_len);
  1862. ret = ceph_con_in_msg_alloc(con, &skip);
  1863. if (ret < 0)
  1864. return ret;
  1865. BUG_ON(!con->in_msg ^ skip);
  1866. if (con->in_msg && data_len > con->in_msg->data_length) {
  1867. pr_warning("%s skipping long message (%u > %zd)\n",
  1868. __func__, data_len, con->in_msg->data_length);
  1869. ceph_msg_put(con->in_msg);
  1870. con->in_msg = NULL;
  1871. skip = 1;
  1872. }
  1873. if (skip) {
  1874. /* skip this message */
  1875. dout("alloc_msg said skip message\n");
  1876. con->in_base_pos = -front_len - middle_len - data_len -
  1877. sizeof(m->footer);
  1878. con->in_tag = CEPH_MSGR_TAG_READY;
  1879. con->in_seq++;
  1880. return 0;
  1881. }
  1882. BUG_ON(!con->in_msg);
  1883. BUG_ON(con->in_msg->con != con);
  1884. m = con->in_msg;
  1885. m->front.iov_len = 0; /* haven't read it yet */
  1886. if (m->middle)
  1887. m->middle->vec.iov_len = 0;
  1888. /* prepare for data payload, if any */
  1889. if (data_len)
  1890. prepare_message_data(con->in_msg, data_len);
  1891. }
  1892. /* front */
  1893. ret = read_partial_message_section(con, &m->front, front_len,
  1894. &con->in_front_crc);
  1895. if (ret <= 0)
  1896. return ret;
  1897. /* middle */
  1898. if (m->middle) {
  1899. ret = read_partial_message_section(con, &m->middle->vec,
  1900. middle_len,
  1901. &con->in_middle_crc);
  1902. if (ret <= 0)
  1903. return ret;
  1904. }
  1905. /* (page) data */
  1906. if (data_len) {
  1907. ret = read_partial_msg_data(con);
  1908. if (ret <= 0)
  1909. return ret;
  1910. }
  1911. /* footer */
  1912. size = sizeof (m->footer);
  1913. end += size;
  1914. ret = read_partial(con, end, size, &m->footer);
  1915. if (ret <= 0)
  1916. return ret;
  1917. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  1918. m, front_len, m->footer.front_crc, middle_len,
  1919. m->footer.middle_crc, data_len, m->footer.data_crc);
  1920. /* crc ok? */
  1921. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  1922. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  1923. m, con->in_front_crc, m->footer.front_crc);
  1924. return -EBADMSG;
  1925. }
  1926. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  1927. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  1928. m, con->in_middle_crc, m->footer.middle_crc);
  1929. return -EBADMSG;
  1930. }
  1931. if (do_datacrc &&
  1932. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  1933. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  1934. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  1935. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  1936. return -EBADMSG;
  1937. }
  1938. return 1; /* done! */
  1939. }
  1940. /*
  1941. * Process message. This happens in the worker thread. The callback should
  1942. * be careful not to do anything that waits on other incoming messages or it
  1943. * may deadlock.
  1944. */
  1945. static void process_message(struct ceph_connection *con)
  1946. {
  1947. struct ceph_msg *msg;
  1948. BUG_ON(con->in_msg->con != con);
  1949. con->in_msg->con = NULL;
  1950. msg = con->in_msg;
  1951. con->in_msg = NULL;
  1952. con->ops->put(con);
  1953. /* if first message, set peer_name */
  1954. if (con->peer_name.type == 0)
  1955. con->peer_name = msg->hdr.src;
  1956. con->in_seq++;
  1957. mutex_unlock(&con->mutex);
  1958. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  1959. msg, le64_to_cpu(msg->hdr.seq),
  1960. ENTITY_NAME(msg->hdr.src),
  1961. le16_to_cpu(msg->hdr.type),
  1962. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1963. le32_to_cpu(msg->hdr.front_len),
  1964. le32_to_cpu(msg->hdr.data_len),
  1965. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  1966. con->ops->dispatch(con, msg);
  1967. mutex_lock(&con->mutex);
  1968. }
  1969. /*
  1970. * Write something to the socket. Called in a worker thread when the
  1971. * socket appears to be writeable and we have something ready to send.
  1972. */
  1973. static int try_write(struct ceph_connection *con)
  1974. {
  1975. int ret = 1;
  1976. dout("try_write start %p state %lu\n", con, con->state);
  1977. more:
  1978. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  1979. /* open the socket first? */
  1980. if (con->state == CON_STATE_PREOPEN) {
  1981. BUG_ON(con->sock);
  1982. con->state = CON_STATE_CONNECTING;
  1983. con_out_kvec_reset(con);
  1984. prepare_write_banner(con);
  1985. prepare_read_banner(con);
  1986. BUG_ON(con->in_msg);
  1987. con->in_tag = CEPH_MSGR_TAG_READY;
  1988. dout("try_write initiating connect on %p new state %lu\n",
  1989. con, con->state);
  1990. ret = ceph_tcp_connect(con);
  1991. if (ret < 0) {
  1992. con->error_msg = "connect error";
  1993. goto out;
  1994. }
  1995. }
  1996. more_kvec:
  1997. /* kvec data queued? */
  1998. if (con->out_skip) {
  1999. ret = write_partial_skip(con);
  2000. if (ret <= 0)
  2001. goto out;
  2002. }
  2003. if (con->out_kvec_left) {
  2004. ret = write_partial_kvec(con);
  2005. if (ret <= 0)
  2006. goto out;
  2007. }
  2008. /* msg pages? */
  2009. if (con->out_msg) {
  2010. if (con->out_msg_done) {
  2011. ceph_msg_put(con->out_msg);
  2012. con->out_msg = NULL; /* we're done with this one */
  2013. goto do_next;
  2014. }
  2015. ret = write_partial_message_data(con);
  2016. if (ret == 1)
  2017. goto more_kvec; /* we need to send the footer, too! */
  2018. if (ret == 0)
  2019. goto out;
  2020. if (ret < 0) {
  2021. dout("try_write write_partial_message_data err %d\n",
  2022. ret);
  2023. goto out;
  2024. }
  2025. }
  2026. do_next:
  2027. if (con->state == CON_STATE_OPEN) {
  2028. /* is anything else pending? */
  2029. if (!list_empty(&con->out_queue)) {
  2030. prepare_write_message(con);
  2031. goto more;
  2032. }
  2033. if (con->in_seq > con->in_seq_acked) {
  2034. prepare_write_ack(con);
  2035. goto more;
  2036. }
  2037. if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2038. prepare_write_keepalive(con);
  2039. goto more;
  2040. }
  2041. }
  2042. /* Nothing to do! */
  2043. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2044. dout("try_write nothing else to write.\n");
  2045. ret = 0;
  2046. out:
  2047. dout("try_write done on %p ret %d\n", con, ret);
  2048. return ret;
  2049. }
  2050. /*
  2051. * Read what we can from the socket.
  2052. */
  2053. static int try_read(struct ceph_connection *con)
  2054. {
  2055. int ret = -1;
  2056. more:
  2057. dout("try_read start on %p state %lu\n", con, con->state);
  2058. if (con->state != CON_STATE_CONNECTING &&
  2059. con->state != CON_STATE_NEGOTIATING &&
  2060. con->state != CON_STATE_OPEN)
  2061. return 0;
  2062. BUG_ON(!con->sock);
  2063. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  2064. con->in_base_pos);
  2065. if (con->state == CON_STATE_CONNECTING) {
  2066. dout("try_read connecting\n");
  2067. ret = read_partial_banner(con);
  2068. if (ret <= 0)
  2069. goto out;
  2070. ret = process_banner(con);
  2071. if (ret < 0)
  2072. goto out;
  2073. con->state = CON_STATE_NEGOTIATING;
  2074. /*
  2075. * Received banner is good, exchange connection info.
  2076. * Do not reset out_kvec, as sending our banner raced
  2077. * with receiving peer banner after connect completed.
  2078. */
  2079. ret = prepare_write_connect(con);
  2080. if (ret < 0)
  2081. goto out;
  2082. prepare_read_connect(con);
  2083. /* Send connection info before awaiting response */
  2084. goto out;
  2085. }
  2086. if (con->state == CON_STATE_NEGOTIATING) {
  2087. dout("try_read negotiating\n");
  2088. ret = read_partial_connect(con);
  2089. if (ret <= 0)
  2090. goto out;
  2091. ret = process_connect(con);
  2092. if (ret < 0)
  2093. goto out;
  2094. goto more;
  2095. }
  2096. WARN_ON(con->state != CON_STATE_OPEN);
  2097. if (con->in_base_pos < 0) {
  2098. /*
  2099. * skipping + discarding content.
  2100. *
  2101. * FIXME: there must be a better way to do this!
  2102. */
  2103. static char buf[SKIP_BUF_SIZE];
  2104. int skip = min((int) sizeof (buf), -con->in_base_pos);
  2105. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  2106. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  2107. if (ret <= 0)
  2108. goto out;
  2109. con->in_base_pos += ret;
  2110. if (con->in_base_pos)
  2111. goto more;
  2112. }
  2113. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  2114. /*
  2115. * what's next?
  2116. */
  2117. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  2118. if (ret <= 0)
  2119. goto out;
  2120. dout("try_read got tag %d\n", (int)con->in_tag);
  2121. switch (con->in_tag) {
  2122. case CEPH_MSGR_TAG_MSG:
  2123. prepare_read_message(con);
  2124. break;
  2125. case CEPH_MSGR_TAG_ACK:
  2126. prepare_read_ack(con);
  2127. break;
  2128. case CEPH_MSGR_TAG_CLOSE:
  2129. con_close_socket(con);
  2130. con->state = CON_STATE_CLOSED;
  2131. goto out;
  2132. default:
  2133. goto bad_tag;
  2134. }
  2135. }
  2136. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  2137. ret = read_partial_message(con);
  2138. if (ret <= 0) {
  2139. switch (ret) {
  2140. case -EBADMSG:
  2141. con->error_msg = "bad crc";
  2142. ret = -EIO;
  2143. break;
  2144. case -EIO:
  2145. con->error_msg = "io error";
  2146. break;
  2147. }
  2148. goto out;
  2149. }
  2150. if (con->in_tag == CEPH_MSGR_TAG_READY)
  2151. goto more;
  2152. process_message(con);
  2153. if (con->state == CON_STATE_OPEN)
  2154. prepare_read_tag(con);
  2155. goto more;
  2156. }
  2157. if (con->in_tag == CEPH_MSGR_TAG_ACK ||
  2158. con->in_tag == CEPH_MSGR_TAG_SEQ) {
  2159. /*
  2160. * the final handshake seq exchange is semantically
  2161. * equivalent to an ACK
  2162. */
  2163. ret = read_partial_ack(con);
  2164. if (ret <= 0)
  2165. goto out;
  2166. process_ack(con);
  2167. goto more;
  2168. }
  2169. out:
  2170. dout("try_read done on %p ret %d\n", con, ret);
  2171. return ret;
  2172. bad_tag:
  2173. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  2174. con->error_msg = "protocol error, garbage tag";
  2175. ret = -1;
  2176. goto out;
  2177. }
  2178. /*
  2179. * Atomically queue work on a connection after the specified delay.
  2180. * Bump @con reference to avoid races with connection teardown.
  2181. * Returns 0 if work was queued, or an error code otherwise.
  2182. */
  2183. static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
  2184. {
  2185. if (!con->ops->get(con)) {
  2186. dout("%s %p ref count 0\n", __func__, con);
  2187. return -ENOENT;
  2188. }
  2189. if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
  2190. dout("%s %p - already queued\n", __func__, con);
  2191. con->ops->put(con);
  2192. return -EBUSY;
  2193. }
  2194. dout("%s %p %lu\n", __func__, con, delay);
  2195. return 0;
  2196. }
  2197. static void queue_con(struct ceph_connection *con)
  2198. {
  2199. (void) queue_con_delay(con, 0);
  2200. }
  2201. static bool con_sock_closed(struct ceph_connection *con)
  2202. {
  2203. if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
  2204. return false;
  2205. #define CASE(x) \
  2206. case CON_STATE_ ## x: \
  2207. con->error_msg = "socket closed (con state " #x ")"; \
  2208. break;
  2209. switch (con->state) {
  2210. CASE(CLOSED);
  2211. CASE(PREOPEN);
  2212. CASE(CONNECTING);
  2213. CASE(NEGOTIATING);
  2214. CASE(OPEN);
  2215. CASE(STANDBY);
  2216. default:
  2217. pr_warning("%s con %p unrecognized state %lu\n",
  2218. __func__, con, con->state);
  2219. con->error_msg = "unrecognized con state";
  2220. BUG();
  2221. break;
  2222. }
  2223. #undef CASE
  2224. return true;
  2225. }
  2226. static bool con_backoff(struct ceph_connection *con)
  2227. {
  2228. int ret;
  2229. if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
  2230. return false;
  2231. ret = queue_con_delay(con, round_jiffies_relative(con->delay));
  2232. if (ret) {
  2233. dout("%s: con %p FAILED to back off %lu\n", __func__,
  2234. con, con->delay);
  2235. BUG_ON(ret == -ENOENT);
  2236. con_flag_set(con, CON_FLAG_BACKOFF);
  2237. }
  2238. return true;
  2239. }
  2240. /* Finish fault handling; con->mutex must *not* be held here */
  2241. static void con_fault_finish(struct ceph_connection *con)
  2242. {
  2243. /*
  2244. * in case we faulted due to authentication, invalidate our
  2245. * current tickets so that we can get new ones.
  2246. */
  2247. if (con->auth_retry && con->ops->invalidate_authorizer) {
  2248. dout("calling invalidate_authorizer()\n");
  2249. con->ops->invalidate_authorizer(con);
  2250. }
  2251. if (con->ops->fault)
  2252. con->ops->fault(con);
  2253. }
  2254. /*
  2255. * Do some work on a connection. Drop a connection ref when we're done.
  2256. */
  2257. static void con_work(struct work_struct *work)
  2258. {
  2259. struct ceph_connection *con = container_of(work, struct ceph_connection,
  2260. work.work);
  2261. bool fault;
  2262. mutex_lock(&con->mutex);
  2263. while (true) {
  2264. int ret;
  2265. if ((fault = con_sock_closed(con))) {
  2266. dout("%s: con %p SOCK_CLOSED\n", __func__, con);
  2267. break;
  2268. }
  2269. if (con_backoff(con)) {
  2270. dout("%s: con %p BACKOFF\n", __func__, con);
  2271. break;
  2272. }
  2273. if (con->state == CON_STATE_STANDBY) {
  2274. dout("%s: con %p STANDBY\n", __func__, con);
  2275. break;
  2276. }
  2277. if (con->state == CON_STATE_CLOSED) {
  2278. dout("%s: con %p CLOSED\n", __func__, con);
  2279. BUG_ON(con->sock);
  2280. break;
  2281. }
  2282. if (con->state == CON_STATE_PREOPEN) {
  2283. dout("%s: con %p PREOPEN\n", __func__, con);
  2284. BUG_ON(con->sock);
  2285. }
  2286. ret = try_read(con);
  2287. if (ret < 0) {
  2288. if (ret == -EAGAIN)
  2289. continue;
  2290. con->error_msg = "socket error on read";
  2291. fault = true;
  2292. break;
  2293. }
  2294. ret = try_write(con);
  2295. if (ret < 0) {
  2296. if (ret == -EAGAIN)
  2297. continue;
  2298. con->error_msg = "socket error on write";
  2299. fault = true;
  2300. }
  2301. break; /* If we make it to here, we're done */
  2302. }
  2303. if (fault)
  2304. con_fault(con);
  2305. mutex_unlock(&con->mutex);
  2306. if (fault)
  2307. con_fault_finish(con);
  2308. con->ops->put(con);
  2309. }
  2310. /*
  2311. * Generic error/fault handler. A retry mechanism is used with
  2312. * exponential backoff
  2313. */
  2314. static void con_fault(struct ceph_connection *con)
  2315. {
  2316. pr_warning("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  2317. ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
  2318. dout("fault %p state %lu to peer %s\n",
  2319. con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
  2320. WARN_ON(con->state != CON_STATE_CONNECTING &&
  2321. con->state != CON_STATE_NEGOTIATING &&
  2322. con->state != CON_STATE_OPEN);
  2323. con_close_socket(con);
  2324. if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
  2325. dout("fault on LOSSYTX channel, marking CLOSED\n");
  2326. con->state = CON_STATE_CLOSED;
  2327. return;
  2328. }
  2329. if (con->in_msg) {
  2330. BUG_ON(con->in_msg->con != con);
  2331. con->in_msg->con = NULL;
  2332. ceph_msg_put(con->in_msg);
  2333. con->in_msg = NULL;
  2334. con->ops->put(con);
  2335. }
  2336. /* Requeue anything that hasn't been acked */
  2337. list_splice_init(&con->out_sent, &con->out_queue);
  2338. /* If there are no messages queued or keepalive pending, place
  2339. * the connection in a STANDBY state */
  2340. if (list_empty(&con->out_queue) &&
  2341. !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2342. dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
  2343. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2344. con->state = CON_STATE_STANDBY;
  2345. } else {
  2346. /* retry after a delay. */
  2347. con->state = CON_STATE_PREOPEN;
  2348. if (con->delay == 0)
  2349. con->delay = BASE_DELAY_INTERVAL;
  2350. else if (con->delay < MAX_DELAY_INTERVAL)
  2351. con->delay *= 2;
  2352. con_flag_set(con, CON_FLAG_BACKOFF);
  2353. queue_con(con);
  2354. }
  2355. }
  2356. /*
  2357. * initialize a new messenger instance
  2358. */
  2359. void ceph_messenger_init(struct ceph_messenger *msgr,
  2360. struct ceph_entity_addr *myaddr,
  2361. u32 supported_features,
  2362. u32 required_features,
  2363. bool nocrc)
  2364. {
  2365. msgr->supported_features = supported_features;
  2366. msgr->required_features = required_features;
  2367. spin_lock_init(&msgr->global_seq_lock);
  2368. if (myaddr)
  2369. msgr->inst.addr = *myaddr;
  2370. /* select a random nonce */
  2371. msgr->inst.addr.type = 0;
  2372. get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
  2373. encode_my_addr(msgr);
  2374. msgr->nocrc = nocrc;
  2375. atomic_set(&msgr->stopping, 0);
  2376. dout("%s %p\n", __func__, msgr);
  2377. }
  2378. EXPORT_SYMBOL(ceph_messenger_init);
  2379. static void clear_standby(struct ceph_connection *con)
  2380. {
  2381. /* come back from STANDBY? */
  2382. if (con->state == CON_STATE_STANDBY) {
  2383. dout("clear_standby %p and ++connect_seq\n", con);
  2384. con->state = CON_STATE_PREOPEN;
  2385. con->connect_seq++;
  2386. WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
  2387. WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
  2388. }
  2389. }
  2390. /*
  2391. * Queue up an outgoing message on the given connection.
  2392. */
  2393. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  2394. {
  2395. /* set src+dst */
  2396. msg->hdr.src = con->msgr->inst.name;
  2397. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  2398. msg->needs_out_seq = true;
  2399. mutex_lock(&con->mutex);
  2400. if (con->state == CON_STATE_CLOSED) {
  2401. dout("con_send %p closed, dropping %p\n", con, msg);
  2402. ceph_msg_put(msg);
  2403. mutex_unlock(&con->mutex);
  2404. return;
  2405. }
  2406. BUG_ON(msg->con != NULL);
  2407. msg->con = con->ops->get(con);
  2408. BUG_ON(msg->con == NULL);
  2409. BUG_ON(!list_empty(&msg->list_head));
  2410. list_add_tail(&msg->list_head, &con->out_queue);
  2411. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  2412. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  2413. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2414. le32_to_cpu(msg->hdr.front_len),
  2415. le32_to_cpu(msg->hdr.middle_len),
  2416. le32_to_cpu(msg->hdr.data_len));
  2417. clear_standby(con);
  2418. mutex_unlock(&con->mutex);
  2419. /* if there wasn't anything waiting to send before, queue
  2420. * new work */
  2421. if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2422. queue_con(con);
  2423. }
  2424. EXPORT_SYMBOL(ceph_con_send);
  2425. /*
  2426. * Revoke a message that was previously queued for send
  2427. */
  2428. void ceph_msg_revoke(struct ceph_msg *msg)
  2429. {
  2430. struct ceph_connection *con = msg->con;
  2431. if (!con)
  2432. return; /* Message not in our possession */
  2433. mutex_lock(&con->mutex);
  2434. if (!list_empty(&msg->list_head)) {
  2435. dout("%s %p msg %p - was on queue\n", __func__, con, msg);
  2436. list_del_init(&msg->list_head);
  2437. BUG_ON(msg->con == NULL);
  2438. msg->con->ops->put(msg->con);
  2439. msg->con = NULL;
  2440. msg->hdr.seq = 0;
  2441. ceph_msg_put(msg);
  2442. }
  2443. if (con->out_msg == msg) {
  2444. dout("%s %p msg %p - was sending\n", __func__, con, msg);
  2445. con->out_msg = NULL;
  2446. if (con->out_kvec_is_msg) {
  2447. con->out_skip = con->out_kvec_bytes;
  2448. con->out_kvec_is_msg = false;
  2449. }
  2450. msg->hdr.seq = 0;
  2451. ceph_msg_put(msg);
  2452. }
  2453. mutex_unlock(&con->mutex);
  2454. }
  2455. /*
  2456. * Revoke a message that we may be reading data into
  2457. */
  2458. void ceph_msg_revoke_incoming(struct ceph_msg *msg)
  2459. {
  2460. struct ceph_connection *con;
  2461. BUG_ON(msg == NULL);
  2462. if (!msg->con) {
  2463. dout("%s msg %p null con\n", __func__, msg);
  2464. return; /* Message not in our possession */
  2465. }
  2466. con = msg->con;
  2467. mutex_lock(&con->mutex);
  2468. if (con->in_msg == msg) {
  2469. unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
  2470. unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
  2471. unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
  2472. /* skip rest of message */
  2473. dout("%s %p msg %p revoked\n", __func__, con, msg);
  2474. con->in_base_pos = con->in_base_pos -
  2475. sizeof(struct ceph_msg_header) -
  2476. front_len -
  2477. middle_len -
  2478. data_len -
  2479. sizeof(struct ceph_msg_footer);
  2480. ceph_msg_put(con->in_msg);
  2481. con->in_msg = NULL;
  2482. con->in_tag = CEPH_MSGR_TAG_READY;
  2483. con->in_seq++;
  2484. } else {
  2485. dout("%s %p in_msg %p msg %p no-op\n",
  2486. __func__, con, con->in_msg, msg);
  2487. }
  2488. mutex_unlock(&con->mutex);
  2489. }
  2490. /*
  2491. * Queue a keepalive byte to ensure the tcp connection is alive.
  2492. */
  2493. void ceph_con_keepalive(struct ceph_connection *con)
  2494. {
  2495. dout("con_keepalive %p\n", con);
  2496. mutex_lock(&con->mutex);
  2497. clear_standby(con);
  2498. mutex_unlock(&con->mutex);
  2499. if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
  2500. con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2501. queue_con(con);
  2502. }
  2503. EXPORT_SYMBOL(ceph_con_keepalive);
  2504. static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
  2505. {
  2506. struct ceph_msg_data *data;
  2507. if (WARN_ON(!ceph_msg_data_type_valid(type)))
  2508. return NULL;
  2509. data = kzalloc(sizeof (*data), GFP_NOFS);
  2510. if (data)
  2511. data->type = type;
  2512. return data;
  2513. }
  2514. static void ceph_msg_data_destroy(struct ceph_msg_data *data)
  2515. {
  2516. if (!data)
  2517. return;
  2518. if (data->type == CEPH_MSG_DATA_PAGELIST) {
  2519. ceph_pagelist_release(data->pagelist);
  2520. kfree(data->pagelist);
  2521. }
  2522. kfree(data);
  2523. }
  2524. void ceph_msg_data_set_pages(struct ceph_msg *msg, struct page **pages,
  2525. size_t length, size_t alignment)
  2526. {
  2527. struct ceph_msg_data *data;
  2528. BUG_ON(!pages);
  2529. BUG_ON(!length);
  2530. BUG_ON(msg->data_length);
  2531. BUG_ON(msg->data != NULL);
  2532. data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
  2533. BUG_ON(!data);
  2534. data->pages = pages;
  2535. data->length = length;
  2536. data->alignment = alignment & ~PAGE_MASK;
  2537. msg->data = data;
  2538. msg->data_length = length;
  2539. }
  2540. EXPORT_SYMBOL(ceph_msg_data_set_pages);
  2541. void ceph_msg_data_set_pagelist(struct ceph_msg *msg,
  2542. struct ceph_pagelist *pagelist)
  2543. {
  2544. struct ceph_msg_data *data;
  2545. BUG_ON(!pagelist);
  2546. BUG_ON(!pagelist->length);
  2547. BUG_ON(msg->data_length);
  2548. BUG_ON(msg->data != NULL);
  2549. data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
  2550. BUG_ON(!data);
  2551. data->pagelist = pagelist;
  2552. msg->data = data;
  2553. msg->data_length = pagelist->length;
  2554. }
  2555. EXPORT_SYMBOL(ceph_msg_data_set_pagelist);
  2556. #ifdef CONFIG_BLOCK
  2557. void ceph_msg_data_set_bio(struct ceph_msg *msg, struct bio *bio,
  2558. size_t length)
  2559. {
  2560. struct ceph_msg_data *data;
  2561. BUG_ON(!bio);
  2562. BUG_ON(msg->data_length);
  2563. BUG_ON(msg->data != NULL);
  2564. data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
  2565. BUG_ON(!data);
  2566. data->bio = bio;
  2567. data->bio_length = length;
  2568. msg->data = data;
  2569. msg->data_length = length;
  2570. }
  2571. EXPORT_SYMBOL(ceph_msg_data_set_bio);
  2572. #endif /* CONFIG_BLOCK */
  2573. /*
  2574. * construct a new message with given type, size
  2575. * the new msg has a ref count of 1.
  2576. */
  2577. struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  2578. bool can_fail)
  2579. {
  2580. struct ceph_msg *m;
  2581. m = kzalloc(sizeof(*m), flags);
  2582. if (m == NULL)
  2583. goto out;
  2584. m->hdr.type = cpu_to_le16(type);
  2585. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  2586. m->hdr.front_len = cpu_to_le32(front_len);
  2587. INIT_LIST_HEAD(&m->list_head);
  2588. kref_init(&m->kref);
  2589. /* front */
  2590. m->front_max = front_len;
  2591. if (front_len) {
  2592. if (front_len > PAGE_CACHE_SIZE) {
  2593. m->front.iov_base = __vmalloc(front_len, flags,
  2594. PAGE_KERNEL);
  2595. m->front_is_vmalloc = true;
  2596. } else {
  2597. m->front.iov_base = kmalloc(front_len, flags);
  2598. }
  2599. if (m->front.iov_base == NULL) {
  2600. dout("ceph_msg_new can't allocate %d bytes\n",
  2601. front_len);
  2602. goto out2;
  2603. }
  2604. } else {
  2605. m->front.iov_base = NULL;
  2606. }
  2607. m->front.iov_len = front_len;
  2608. dout("ceph_msg_new %p front %d\n", m, front_len);
  2609. return m;
  2610. out2:
  2611. ceph_msg_put(m);
  2612. out:
  2613. if (!can_fail) {
  2614. pr_err("msg_new can't create type %d front %d\n", type,
  2615. front_len);
  2616. WARN_ON(1);
  2617. } else {
  2618. dout("msg_new can't create type %d front %d\n", type,
  2619. front_len);
  2620. }
  2621. return NULL;
  2622. }
  2623. EXPORT_SYMBOL(ceph_msg_new);
  2624. /*
  2625. * Allocate "middle" portion of a message, if it is needed and wasn't
  2626. * allocated by alloc_msg. This allows us to read a small fixed-size
  2627. * per-type header in the front and then gracefully fail (i.e.,
  2628. * propagate the error to the caller based on info in the front) when
  2629. * the middle is too large.
  2630. */
  2631. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  2632. {
  2633. int type = le16_to_cpu(msg->hdr.type);
  2634. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  2635. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  2636. ceph_msg_type_name(type), middle_len);
  2637. BUG_ON(!middle_len);
  2638. BUG_ON(msg->middle);
  2639. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  2640. if (!msg->middle)
  2641. return -ENOMEM;
  2642. return 0;
  2643. }
  2644. /*
  2645. * Allocate a message for receiving an incoming message on a
  2646. * connection, and save the result in con->in_msg. Uses the
  2647. * connection's private alloc_msg op if available.
  2648. *
  2649. * Returns 0 on success, or a negative error code.
  2650. *
  2651. * On success, if we set *skip = 1:
  2652. * - the next message should be skipped and ignored.
  2653. * - con->in_msg == NULL
  2654. * or if we set *skip = 0:
  2655. * - con->in_msg is non-null.
  2656. * On error (ENOMEM, EAGAIN, ...),
  2657. * - con->in_msg == NULL
  2658. */
  2659. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
  2660. {
  2661. struct ceph_msg_header *hdr = &con->in_hdr;
  2662. int middle_len = le32_to_cpu(hdr->middle_len);
  2663. struct ceph_msg *msg;
  2664. int ret = 0;
  2665. BUG_ON(con->in_msg != NULL);
  2666. BUG_ON(!con->ops->alloc_msg);
  2667. mutex_unlock(&con->mutex);
  2668. msg = con->ops->alloc_msg(con, hdr, skip);
  2669. mutex_lock(&con->mutex);
  2670. if (con->state != CON_STATE_OPEN) {
  2671. if (msg)
  2672. ceph_msg_put(msg);
  2673. return -EAGAIN;
  2674. }
  2675. if (msg) {
  2676. BUG_ON(*skip);
  2677. con->in_msg = msg;
  2678. con->in_msg->con = con->ops->get(con);
  2679. BUG_ON(con->in_msg->con == NULL);
  2680. } else {
  2681. /*
  2682. * Null message pointer means either we should skip
  2683. * this message or we couldn't allocate memory. The
  2684. * former is not an error.
  2685. */
  2686. if (*skip)
  2687. return 0;
  2688. con->error_msg = "error allocating memory for incoming message";
  2689. return -ENOMEM;
  2690. }
  2691. memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
  2692. if (middle_len && !con->in_msg->middle) {
  2693. ret = ceph_alloc_middle(con, con->in_msg);
  2694. if (ret < 0) {
  2695. ceph_msg_put(con->in_msg);
  2696. con->in_msg = NULL;
  2697. }
  2698. }
  2699. return ret;
  2700. }
  2701. /*
  2702. * Free a generically kmalloc'd message.
  2703. */
  2704. void ceph_msg_kfree(struct ceph_msg *m)
  2705. {
  2706. dout("msg_kfree %p\n", m);
  2707. if (m->front_is_vmalloc)
  2708. vfree(m->front.iov_base);
  2709. else
  2710. kfree(m->front.iov_base);
  2711. kfree(m);
  2712. }
  2713. /*
  2714. * Drop a msg ref. Destroy as needed.
  2715. */
  2716. void ceph_msg_last_put(struct kref *kref)
  2717. {
  2718. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  2719. dout("ceph_msg_put last one on %p\n", m);
  2720. WARN_ON(!list_empty(&m->list_head));
  2721. /* drop middle, data, if any */
  2722. if (m->middle) {
  2723. ceph_buffer_put(m->middle);
  2724. m->middle = NULL;
  2725. }
  2726. ceph_msg_data_destroy(m->data);
  2727. m->data = NULL;
  2728. m->data_length = 0;
  2729. if (m->pool)
  2730. ceph_msgpool_put(m->pool, m);
  2731. else
  2732. ceph_msg_kfree(m);
  2733. }
  2734. EXPORT_SYMBOL(ceph_msg_last_put);
  2735. void ceph_msg_dump(struct ceph_msg *msg)
  2736. {
  2737. pr_debug("msg_dump %p (front_max %d length %zd)\n", msg,
  2738. msg->front_max, msg->data->length);
  2739. print_hex_dump(KERN_DEBUG, "header: ",
  2740. DUMP_PREFIX_OFFSET, 16, 1,
  2741. &msg->hdr, sizeof(msg->hdr), true);
  2742. print_hex_dump(KERN_DEBUG, " front: ",
  2743. DUMP_PREFIX_OFFSET, 16, 1,
  2744. msg->front.iov_base, msg->front.iov_len, true);
  2745. if (msg->middle)
  2746. print_hex_dump(KERN_DEBUG, "middle: ",
  2747. DUMP_PREFIX_OFFSET, 16, 1,
  2748. msg->middle->vec.iov_base,
  2749. msg->middle->vec.iov_len, true);
  2750. print_hex_dump(KERN_DEBUG, "footer: ",
  2751. DUMP_PREFIX_OFFSET, 16, 1,
  2752. &msg->footer, sizeof(msg->footer), true);
  2753. }
  2754. EXPORT_SYMBOL(ceph_msg_dump);