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