messenger.c 83 KB

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