messenger.c 83 KB

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