messenger.c 86 KB

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