messenger.c 80 KB

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