messenger.c 84 KB

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