messenger.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296
  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_pages(msg))
  906. msg_pos->page_pos = msg->p.alignment;
  907. else
  908. msg_pos->page_pos = 0;
  909. msg_pos->data_pos = 0;
  910. /* Initialize data cursors */
  911. #ifdef CONFIG_BLOCK
  912. if (ceph_msg_has_bio(msg))
  913. ceph_msg_data_cursor_init(&msg->b, data_len);
  914. #endif /* CONFIG_BLOCK */
  915. if (ceph_msg_has_pages(msg))
  916. ceph_msg_data_cursor_init(&msg->p, data_len);
  917. if (ceph_msg_has_pagelist(msg))
  918. ceph_msg_data_cursor_init(&msg->l, data_len);
  919. msg_pos->did_page_crc = false;
  920. }
  921. /*
  922. * Prepare footer for currently outgoing message, and finish things
  923. * off. Assumes out_kvec* are already valid.. we just add on to the end.
  924. */
  925. static void prepare_write_message_footer(struct ceph_connection *con)
  926. {
  927. struct ceph_msg *m = con->out_msg;
  928. int v = con->out_kvec_left;
  929. m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
  930. dout("prepare_write_message_footer %p\n", con);
  931. con->out_kvec_is_msg = true;
  932. con->out_kvec[v].iov_base = &m->footer;
  933. con->out_kvec[v].iov_len = sizeof(m->footer);
  934. con->out_kvec_bytes += sizeof(m->footer);
  935. con->out_kvec_left++;
  936. con->out_more = m->more_to_follow;
  937. con->out_msg_done = true;
  938. }
  939. /*
  940. * Prepare headers for the next outgoing message.
  941. */
  942. static void prepare_write_message(struct ceph_connection *con)
  943. {
  944. struct ceph_msg *m;
  945. u32 crc;
  946. con_out_kvec_reset(con);
  947. con->out_kvec_is_msg = true;
  948. con->out_msg_done = false;
  949. /* Sneak an ack in there first? If we can get it into the same
  950. * TCP packet that's a good thing. */
  951. if (con->in_seq > con->in_seq_acked) {
  952. con->in_seq_acked = con->in_seq;
  953. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  954. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  955. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  956. &con->out_temp_ack);
  957. }
  958. BUG_ON(list_empty(&con->out_queue));
  959. m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
  960. con->out_msg = m;
  961. BUG_ON(m->con != con);
  962. /* put message on sent list */
  963. ceph_msg_get(m);
  964. list_move_tail(&m->list_head, &con->out_sent);
  965. /*
  966. * only assign outgoing seq # if we haven't sent this message
  967. * yet. if it is requeued, resend with it's original seq.
  968. */
  969. if (m->needs_out_seq) {
  970. m->hdr.seq = cpu_to_le64(++con->out_seq);
  971. m->needs_out_seq = false;
  972. }
  973. dout("prepare_write_message %p seq %lld type %d len %d+%d+%d (%zd)\n",
  974. m, con->out_seq, le16_to_cpu(m->hdr.type),
  975. le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
  976. le32_to_cpu(m->hdr.data_len), m->p.length);
  977. BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
  978. /* tag + hdr + front + middle */
  979. con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
  980. con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
  981. con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
  982. if (m->middle)
  983. con_out_kvec_add(con, m->middle->vec.iov_len,
  984. m->middle->vec.iov_base);
  985. /* fill in crc (except data pages), footer */
  986. crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
  987. con->out_msg->hdr.crc = cpu_to_le32(crc);
  988. con->out_msg->footer.flags = 0;
  989. crc = crc32c(0, m->front.iov_base, m->front.iov_len);
  990. con->out_msg->footer.front_crc = cpu_to_le32(crc);
  991. if (m->middle) {
  992. crc = crc32c(0, m->middle->vec.iov_base,
  993. m->middle->vec.iov_len);
  994. con->out_msg->footer.middle_crc = cpu_to_le32(crc);
  995. } else
  996. con->out_msg->footer.middle_crc = 0;
  997. dout("%s front_crc %u middle_crc %u\n", __func__,
  998. le32_to_cpu(con->out_msg->footer.front_crc),
  999. le32_to_cpu(con->out_msg->footer.middle_crc));
  1000. /* is there a data payload? */
  1001. con->out_msg->footer.data_crc = 0;
  1002. if (m->hdr.data_len) {
  1003. prepare_message_data(con->out_msg, &con->out_msg_pos);
  1004. con->out_more = 1; /* data + footer will follow */
  1005. } else {
  1006. /* no, queue up footer too and be done */
  1007. prepare_write_message_footer(con);
  1008. }
  1009. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1010. }
  1011. /*
  1012. * Prepare an ack.
  1013. */
  1014. static void prepare_write_ack(struct ceph_connection *con)
  1015. {
  1016. dout("prepare_write_ack %p %llu -> %llu\n", con,
  1017. con->in_seq_acked, con->in_seq);
  1018. con->in_seq_acked = con->in_seq;
  1019. con_out_kvec_reset(con);
  1020. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  1021. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  1022. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  1023. &con->out_temp_ack);
  1024. con->out_more = 1; /* more will follow.. eventually.. */
  1025. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1026. }
  1027. /*
  1028. * Prepare to share the seq during handshake
  1029. */
  1030. static void prepare_write_seq(struct ceph_connection *con)
  1031. {
  1032. dout("prepare_write_seq %p %llu -> %llu\n", con,
  1033. con->in_seq_acked, con->in_seq);
  1034. con->in_seq_acked = con->in_seq;
  1035. con_out_kvec_reset(con);
  1036. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  1037. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  1038. &con->out_temp_ack);
  1039. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1040. }
  1041. /*
  1042. * Prepare to write keepalive byte.
  1043. */
  1044. static void prepare_write_keepalive(struct ceph_connection *con)
  1045. {
  1046. dout("prepare_write_keepalive %p\n", con);
  1047. con_out_kvec_reset(con);
  1048. con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
  1049. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1050. }
  1051. /*
  1052. * Connection negotiation.
  1053. */
  1054. static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
  1055. int *auth_proto)
  1056. {
  1057. struct ceph_auth_handshake *auth;
  1058. if (!con->ops->get_authorizer) {
  1059. con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
  1060. con->out_connect.authorizer_len = 0;
  1061. return NULL;
  1062. }
  1063. /* Can't hold the mutex while getting authorizer */
  1064. mutex_unlock(&con->mutex);
  1065. auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
  1066. mutex_lock(&con->mutex);
  1067. if (IS_ERR(auth))
  1068. return auth;
  1069. if (con->state != CON_STATE_NEGOTIATING)
  1070. return ERR_PTR(-EAGAIN);
  1071. con->auth_reply_buf = auth->authorizer_reply_buf;
  1072. con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
  1073. return auth;
  1074. }
  1075. /*
  1076. * We connected to a peer and are saying hello.
  1077. */
  1078. static void prepare_write_banner(struct ceph_connection *con)
  1079. {
  1080. con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
  1081. con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
  1082. &con->msgr->my_enc_addr);
  1083. con->out_more = 0;
  1084. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1085. }
  1086. static int prepare_write_connect(struct ceph_connection *con)
  1087. {
  1088. unsigned int global_seq = get_global_seq(con->msgr, 0);
  1089. int proto;
  1090. int auth_proto;
  1091. struct ceph_auth_handshake *auth;
  1092. switch (con->peer_name.type) {
  1093. case CEPH_ENTITY_TYPE_MON:
  1094. proto = CEPH_MONC_PROTOCOL;
  1095. break;
  1096. case CEPH_ENTITY_TYPE_OSD:
  1097. proto = CEPH_OSDC_PROTOCOL;
  1098. break;
  1099. case CEPH_ENTITY_TYPE_MDS:
  1100. proto = CEPH_MDSC_PROTOCOL;
  1101. break;
  1102. default:
  1103. BUG();
  1104. }
  1105. dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
  1106. con->connect_seq, global_seq, proto);
  1107. con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
  1108. con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
  1109. con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
  1110. con->out_connect.global_seq = cpu_to_le32(global_seq);
  1111. con->out_connect.protocol_version = cpu_to_le32(proto);
  1112. con->out_connect.flags = 0;
  1113. auth_proto = CEPH_AUTH_UNKNOWN;
  1114. auth = get_connect_authorizer(con, &auth_proto);
  1115. if (IS_ERR(auth))
  1116. return PTR_ERR(auth);
  1117. con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
  1118. con->out_connect.authorizer_len = auth ?
  1119. cpu_to_le32(auth->authorizer_buf_len) : 0;
  1120. con_out_kvec_add(con, sizeof (con->out_connect),
  1121. &con->out_connect);
  1122. if (auth && auth->authorizer_buf_len)
  1123. con_out_kvec_add(con, auth->authorizer_buf_len,
  1124. auth->authorizer_buf);
  1125. con->out_more = 0;
  1126. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1127. return 0;
  1128. }
  1129. /*
  1130. * write as much of pending kvecs to the socket as we can.
  1131. * 1 -> done
  1132. * 0 -> socket full, but more to do
  1133. * <0 -> error
  1134. */
  1135. static int write_partial_kvec(struct ceph_connection *con)
  1136. {
  1137. int ret;
  1138. dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
  1139. while (con->out_kvec_bytes > 0) {
  1140. ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
  1141. con->out_kvec_left, con->out_kvec_bytes,
  1142. con->out_more);
  1143. if (ret <= 0)
  1144. goto out;
  1145. con->out_kvec_bytes -= ret;
  1146. if (con->out_kvec_bytes == 0)
  1147. break; /* done */
  1148. /* account for full iov entries consumed */
  1149. while (ret >= con->out_kvec_cur->iov_len) {
  1150. BUG_ON(!con->out_kvec_left);
  1151. ret -= con->out_kvec_cur->iov_len;
  1152. con->out_kvec_cur++;
  1153. con->out_kvec_left--;
  1154. }
  1155. /* and for a partially-consumed entry */
  1156. if (ret) {
  1157. con->out_kvec_cur->iov_len -= ret;
  1158. con->out_kvec_cur->iov_base += ret;
  1159. }
  1160. }
  1161. con->out_kvec_left = 0;
  1162. con->out_kvec_is_msg = false;
  1163. ret = 1;
  1164. out:
  1165. dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
  1166. con->out_kvec_bytes, con->out_kvec_left, ret);
  1167. return ret; /* done! */
  1168. }
  1169. static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
  1170. size_t len, size_t sent)
  1171. {
  1172. struct ceph_msg *msg = con->out_msg;
  1173. struct ceph_msg_pos *msg_pos = &con->out_msg_pos;
  1174. bool need_crc = false;
  1175. BUG_ON(!msg);
  1176. BUG_ON(!sent);
  1177. msg_pos->data_pos += sent;
  1178. msg_pos->page_pos += sent;
  1179. if (ceph_msg_has_pages(msg))
  1180. need_crc = ceph_msg_data_advance(&msg->p, sent);
  1181. else if (ceph_msg_has_pagelist(msg))
  1182. need_crc = ceph_msg_data_advance(&msg->l, sent);
  1183. #ifdef CONFIG_BLOCK
  1184. else if (ceph_msg_has_bio(msg))
  1185. need_crc = ceph_msg_data_advance(&msg->b, sent);
  1186. #endif /* CONFIG_BLOCK */
  1187. BUG_ON(need_crc && sent != len);
  1188. if (sent < len)
  1189. return;
  1190. BUG_ON(sent != len);
  1191. msg_pos->page_pos = 0;
  1192. msg_pos->page++;
  1193. msg_pos->did_page_crc = false;
  1194. }
  1195. static void in_msg_pos_next(struct ceph_connection *con, size_t len,
  1196. size_t received)
  1197. {
  1198. struct ceph_msg *msg = con->in_msg;
  1199. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1200. BUG_ON(!msg);
  1201. BUG_ON(!received);
  1202. msg_pos->data_pos += received;
  1203. msg_pos->page_pos += received;
  1204. if (ceph_msg_has_pages(msg))
  1205. (void) ceph_msg_data_advance(&msg->p, received);
  1206. #ifdef CONFIG_BLOCK
  1207. else if (ceph_msg_has_bio(msg))
  1208. (void) ceph_msg_data_advance(&msg->b, received);
  1209. #endif /* CONFIG_BLOCK */
  1210. if (received < len)
  1211. return;
  1212. BUG_ON(received != len);
  1213. msg_pos->page_pos = 0;
  1214. msg_pos->page++;
  1215. }
  1216. static u32 ceph_crc32c_page(u32 crc, struct page *page,
  1217. unsigned int page_offset,
  1218. unsigned int length)
  1219. {
  1220. char *kaddr;
  1221. kaddr = kmap(page);
  1222. BUG_ON(kaddr == NULL);
  1223. crc = crc32c(crc, kaddr + page_offset, length);
  1224. kunmap(page);
  1225. return crc;
  1226. }
  1227. /*
  1228. * Write as much message data payload as we can. If we finish, queue
  1229. * up the footer.
  1230. * 1 -> done, footer is now queued in out_kvec[].
  1231. * 0 -> socket full, but more to do
  1232. * <0 -> error
  1233. */
  1234. static int write_partial_message_data(struct ceph_connection *con)
  1235. {
  1236. struct ceph_msg *msg = con->out_msg;
  1237. struct ceph_msg_pos *msg_pos = &con->out_msg_pos;
  1238. unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
  1239. bool do_datacrc = !con->msgr->nocrc;
  1240. int ret;
  1241. dout("%s %p msg %p page %d offset %d\n", __func__,
  1242. con, msg, msg_pos->page, msg_pos->page_pos);
  1243. /*
  1244. * Iterate through each page that contains data to be
  1245. * written, and send as much as possible for each.
  1246. *
  1247. * If we are calculating the data crc (the default), we will
  1248. * need to map the page. If we have no pages, they have
  1249. * been revoked, so use the zero page.
  1250. */
  1251. while (data_len > msg_pos->data_pos) {
  1252. struct page *page;
  1253. size_t page_offset;
  1254. size_t length;
  1255. bool last_piece;
  1256. if (ceph_msg_has_pages(msg)) {
  1257. page = ceph_msg_data_next(&msg->p, &page_offset,
  1258. &length, &last_piece);
  1259. } else if (ceph_msg_has_pagelist(msg)) {
  1260. page = ceph_msg_data_next(&msg->l, &page_offset,
  1261. &length, &last_piece);
  1262. #ifdef CONFIG_BLOCK
  1263. } else if (ceph_msg_has_bio(msg)) {
  1264. page = ceph_msg_data_next(&msg->b, &page_offset,
  1265. &length, &last_piece);
  1266. #endif
  1267. } else {
  1268. WARN(1, "con %p data_len %u but no outbound data\n",
  1269. con, data_len);
  1270. ret = -EINVAL;
  1271. goto out;
  1272. }
  1273. if (do_datacrc && !msg_pos->did_page_crc) {
  1274. u32 crc = le32_to_cpu(msg->footer.data_crc);
  1275. crc = ceph_crc32c_page(crc, page, page_offset, length);
  1276. msg->footer.data_crc = cpu_to_le32(crc);
  1277. msg_pos->did_page_crc = true;
  1278. }
  1279. ret = ceph_tcp_sendpage(con->sock, page, page_offset,
  1280. length, last_piece);
  1281. if (ret <= 0)
  1282. goto out;
  1283. out_msg_pos_next(con, page, length, (size_t) ret);
  1284. }
  1285. dout("%s %p msg %p done\n", __func__, con, msg);
  1286. /* prepare and queue up footer, too */
  1287. if (!do_datacrc)
  1288. msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
  1289. con_out_kvec_reset(con);
  1290. prepare_write_message_footer(con);
  1291. ret = 1;
  1292. out:
  1293. return ret;
  1294. }
  1295. /*
  1296. * write some zeros
  1297. */
  1298. static int write_partial_skip(struct ceph_connection *con)
  1299. {
  1300. int ret;
  1301. while (con->out_skip > 0) {
  1302. size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
  1303. ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
  1304. if (ret <= 0)
  1305. goto out;
  1306. con->out_skip -= ret;
  1307. }
  1308. ret = 1;
  1309. out:
  1310. return ret;
  1311. }
  1312. /*
  1313. * Prepare to read connection handshake, or an ack.
  1314. */
  1315. static void prepare_read_banner(struct ceph_connection *con)
  1316. {
  1317. dout("prepare_read_banner %p\n", con);
  1318. con->in_base_pos = 0;
  1319. }
  1320. static void prepare_read_connect(struct ceph_connection *con)
  1321. {
  1322. dout("prepare_read_connect %p\n", con);
  1323. con->in_base_pos = 0;
  1324. }
  1325. static void prepare_read_ack(struct ceph_connection *con)
  1326. {
  1327. dout("prepare_read_ack %p\n", con);
  1328. con->in_base_pos = 0;
  1329. }
  1330. static void prepare_read_seq(struct ceph_connection *con)
  1331. {
  1332. dout("prepare_read_seq %p\n", con);
  1333. con->in_base_pos = 0;
  1334. con->in_tag = CEPH_MSGR_TAG_SEQ;
  1335. }
  1336. static void prepare_read_tag(struct ceph_connection *con)
  1337. {
  1338. dout("prepare_read_tag %p\n", con);
  1339. con->in_base_pos = 0;
  1340. con->in_tag = CEPH_MSGR_TAG_READY;
  1341. }
  1342. /*
  1343. * Prepare to read a message.
  1344. */
  1345. static int prepare_read_message(struct ceph_connection *con)
  1346. {
  1347. dout("prepare_read_message %p\n", con);
  1348. BUG_ON(con->in_msg != NULL);
  1349. con->in_base_pos = 0;
  1350. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  1351. return 0;
  1352. }
  1353. static int read_partial(struct ceph_connection *con,
  1354. int end, int size, void *object)
  1355. {
  1356. while (con->in_base_pos < end) {
  1357. int left = end - con->in_base_pos;
  1358. int have = size - left;
  1359. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  1360. if (ret <= 0)
  1361. return ret;
  1362. con->in_base_pos += ret;
  1363. }
  1364. return 1;
  1365. }
  1366. /*
  1367. * Read all or part of the connect-side handshake on a new connection
  1368. */
  1369. static int read_partial_banner(struct ceph_connection *con)
  1370. {
  1371. int size;
  1372. int end;
  1373. int ret;
  1374. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  1375. /* peer's banner */
  1376. size = strlen(CEPH_BANNER);
  1377. end = size;
  1378. ret = read_partial(con, end, size, con->in_banner);
  1379. if (ret <= 0)
  1380. goto out;
  1381. size = sizeof (con->actual_peer_addr);
  1382. end += size;
  1383. ret = read_partial(con, end, size, &con->actual_peer_addr);
  1384. if (ret <= 0)
  1385. goto out;
  1386. size = sizeof (con->peer_addr_for_me);
  1387. end += size;
  1388. ret = read_partial(con, end, size, &con->peer_addr_for_me);
  1389. if (ret <= 0)
  1390. goto out;
  1391. out:
  1392. return ret;
  1393. }
  1394. static int read_partial_connect(struct ceph_connection *con)
  1395. {
  1396. int size;
  1397. int end;
  1398. int ret;
  1399. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  1400. size = sizeof (con->in_reply);
  1401. end = size;
  1402. ret = read_partial(con, end, size, &con->in_reply);
  1403. if (ret <= 0)
  1404. goto out;
  1405. size = le32_to_cpu(con->in_reply.authorizer_len);
  1406. end += size;
  1407. ret = read_partial(con, end, size, con->auth_reply_buf);
  1408. if (ret <= 0)
  1409. goto out;
  1410. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  1411. con, (int)con->in_reply.tag,
  1412. le32_to_cpu(con->in_reply.connect_seq),
  1413. le32_to_cpu(con->in_reply.global_seq));
  1414. out:
  1415. return ret;
  1416. }
  1417. /*
  1418. * Verify the hello banner looks okay.
  1419. */
  1420. static int verify_hello(struct ceph_connection *con)
  1421. {
  1422. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  1423. pr_err("connect to %s got bad banner\n",
  1424. ceph_pr_addr(&con->peer_addr.in_addr));
  1425. con->error_msg = "protocol error, bad banner";
  1426. return -1;
  1427. }
  1428. return 0;
  1429. }
  1430. static bool addr_is_blank(struct sockaddr_storage *ss)
  1431. {
  1432. switch (ss->ss_family) {
  1433. case AF_INET:
  1434. return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
  1435. case AF_INET6:
  1436. return
  1437. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
  1438. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
  1439. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
  1440. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
  1441. }
  1442. return false;
  1443. }
  1444. static int addr_port(struct sockaddr_storage *ss)
  1445. {
  1446. switch (ss->ss_family) {
  1447. case AF_INET:
  1448. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  1449. case AF_INET6:
  1450. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  1451. }
  1452. return 0;
  1453. }
  1454. static void addr_set_port(struct sockaddr_storage *ss, int p)
  1455. {
  1456. switch (ss->ss_family) {
  1457. case AF_INET:
  1458. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  1459. break;
  1460. case AF_INET6:
  1461. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  1462. break;
  1463. }
  1464. }
  1465. /*
  1466. * Unlike other *_pton function semantics, zero indicates success.
  1467. */
  1468. static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
  1469. char delim, const char **ipend)
  1470. {
  1471. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  1472. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  1473. memset(ss, 0, sizeof(*ss));
  1474. if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
  1475. ss->ss_family = AF_INET;
  1476. return 0;
  1477. }
  1478. if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
  1479. ss->ss_family = AF_INET6;
  1480. return 0;
  1481. }
  1482. return -EINVAL;
  1483. }
  1484. /*
  1485. * Extract hostname string and resolve using kernel DNS facility.
  1486. */
  1487. #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
  1488. static int ceph_dns_resolve_name(const char *name, size_t namelen,
  1489. struct sockaddr_storage *ss, char delim, const char **ipend)
  1490. {
  1491. const char *end, *delim_p;
  1492. char *colon_p, *ip_addr = NULL;
  1493. int ip_len, ret;
  1494. /*
  1495. * The end of the hostname occurs immediately preceding the delimiter or
  1496. * the port marker (':') where the delimiter takes precedence.
  1497. */
  1498. delim_p = memchr(name, delim, namelen);
  1499. colon_p = memchr(name, ':', namelen);
  1500. if (delim_p && colon_p)
  1501. end = delim_p < colon_p ? delim_p : colon_p;
  1502. else if (!delim_p && colon_p)
  1503. end = colon_p;
  1504. else {
  1505. end = delim_p;
  1506. if (!end) /* case: hostname:/ */
  1507. end = name + namelen;
  1508. }
  1509. if (end <= name)
  1510. return -EINVAL;
  1511. /* do dns_resolve upcall */
  1512. ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
  1513. if (ip_len > 0)
  1514. ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
  1515. else
  1516. ret = -ESRCH;
  1517. kfree(ip_addr);
  1518. *ipend = end;
  1519. pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
  1520. ret, ret ? "failed" : ceph_pr_addr(ss));
  1521. return ret;
  1522. }
  1523. #else
  1524. static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
  1525. struct sockaddr_storage *ss, char delim, const char **ipend)
  1526. {
  1527. return -EINVAL;
  1528. }
  1529. #endif
  1530. /*
  1531. * Parse a server name (IP or hostname). If a valid IP address is not found
  1532. * then try to extract a hostname to resolve using userspace DNS upcall.
  1533. */
  1534. static int ceph_parse_server_name(const char *name, size_t namelen,
  1535. struct sockaddr_storage *ss, char delim, const char **ipend)
  1536. {
  1537. int ret;
  1538. ret = ceph_pton(name, namelen, ss, delim, ipend);
  1539. if (ret)
  1540. ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
  1541. return ret;
  1542. }
  1543. /*
  1544. * Parse an ip[:port] list into an addr array. Use the default
  1545. * monitor port if a port isn't specified.
  1546. */
  1547. int ceph_parse_ips(const char *c, const char *end,
  1548. struct ceph_entity_addr *addr,
  1549. int max_count, int *count)
  1550. {
  1551. int i, ret = -EINVAL;
  1552. const char *p = c;
  1553. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  1554. for (i = 0; i < max_count; i++) {
  1555. const char *ipend;
  1556. struct sockaddr_storage *ss = &addr[i].in_addr;
  1557. int port;
  1558. char delim = ',';
  1559. if (*p == '[') {
  1560. delim = ']';
  1561. p++;
  1562. }
  1563. ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
  1564. if (ret)
  1565. goto bad;
  1566. ret = -EINVAL;
  1567. p = ipend;
  1568. if (delim == ']') {
  1569. if (*p != ']') {
  1570. dout("missing matching ']'\n");
  1571. goto bad;
  1572. }
  1573. p++;
  1574. }
  1575. /* port? */
  1576. if (p < end && *p == ':') {
  1577. port = 0;
  1578. p++;
  1579. while (p < end && *p >= '0' && *p <= '9') {
  1580. port = (port * 10) + (*p - '0');
  1581. p++;
  1582. }
  1583. if (port > 65535 || port == 0)
  1584. goto bad;
  1585. } else {
  1586. port = CEPH_MON_PORT;
  1587. }
  1588. addr_set_port(ss, port);
  1589. dout("parse_ips got %s\n", ceph_pr_addr(ss));
  1590. if (p == end)
  1591. break;
  1592. if (*p != ',')
  1593. goto bad;
  1594. p++;
  1595. }
  1596. if (p != end)
  1597. goto bad;
  1598. if (count)
  1599. *count = i + 1;
  1600. return 0;
  1601. bad:
  1602. pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
  1603. return ret;
  1604. }
  1605. EXPORT_SYMBOL(ceph_parse_ips);
  1606. static int process_banner(struct ceph_connection *con)
  1607. {
  1608. dout("process_banner on %p\n", con);
  1609. if (verify_hello(con) < 0)
  1610. return -1;
  1611. ceph_decode_addr(&con->actual_peer_addr);
  1612. ceph_decode_addr(&con->peer_addr_for_me);
  1613. /*
  1614. * Make sure the other end is who we wanted. note that the other
  1615. * end may not yet know their ip address, so if it's 0.0.0.0, give
  1616. * them the benefit of the doubt.
  1617. */
  1618. if (memcmp(&con->peer_addr, &con->actual_peer_addr,
  1619. sizeof(con->peer_addr)) != 0 &&
  1620. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  1621. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  1622. pr_warning("wrong peer, want %s/%d, got %s/%d\n",
  1623. ceph_pr_addr(&con->peer_addr.in_addr),
  1624. (int)le32_to_cpu(con->peer_addr.nonce),
  1625. ceph_pr_addr(&con->actual_peer_addr.in_addr),
  1626. (int)le32_to_cpu(con->actual_peer_addr.nonce));
  1627. con->error_msg = "wrong peer at address";
  1628. return -1;
  1629. }
  1630. /*
  1631. * did we learn our address?
  1632. */
  1633. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  1634. int port = addr_port(&con->msgr->inst.addr.in_addr);
  1635. memcpy(&con->msgr->inst.addr.in_addr,
  1636. &con->peer_addr_for_me.in_addr,
  1637. sizeof(con->peer_addr_for_me.in_addr));
  1638. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  1639. encode_my_addr(con->msgr);
  1640. dout("process_banner learned my addr is %s\n",
  1641. ceph_pr_addr(&con->msgr->inst.addr.in_addr));
  1642. }
  1643. return 0;
  1644. }
  1645. static int process_connect(struct ceph_connection *con)
  1646. {
  1647. u64 sup_feat = con->msgr->supported_features;
  1648. u64 req_feat = con->msgr->required_features;
  1649. u64 server_feat = le64_to_cpu(con->in_reply.features);
  1650. int ret;
  1651. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  1652. switch (con->in_reply.tag) {
  1653. case CEPH_MSGR_TAG_FEATURES:
  1654. pr_err("%s%lld %s feature set mismatch,"
  1655. " my %llx < server's %llx, missing %llx\n",
  1656. ENTITY_NAME(con->peer_name),
  1657. ceph_pr_addr(&con->peer_addr.in_addr),
  1658. sup_feat, server_feat, server_feat & ~sup_feat);
  1659. con->error_msg = "missing required protocol features";
  1660. reset_connection(con);
  1661. return -1;
  1662. case CEPH_MSGR_TAG_BADPROTOVER:
  1663. pr_err("%s%lld %s protocol version mismatch,"
  1664. " my %d != server's %d\n",
  1665. ENTITY_NAME(con->peer_name),
  1666. ceph_pr_addr(&con->peer_addr.in_addr),
  1667. le32_to_cpu(con->out_connect.protocol_version),
  1668. le32_to_cpu(con->in_reply.protocol_version));
  1669. con->error_msg = "protocol version mismatch";
  1670. reset_connection(con);
  1671. return -1;
  1672. case CEPH_MSGR_TAG_BADAUTHORIZER:
  1673. con->auth_retry++;
  1674. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  1675. con->auth_retry);
  1676. if (con->auth_retry == 2) {
  1677. con->error_msg = "connect authorization failure";
  1678. return -1;
  1679. }
  1680. con_out_kvec_reset(con);
  1681. ret = prepare_write_connect(con);
  1682. if (ret < 0)
  1683. return ret;
  1684. prepare_read_connect(con);
  1685. break;
  1686. case CEPH_MSGR_TAG_RESETSESSION:
  1687. /*
  1688. * If we connected with a large connect_seq but the peer
  1689. * has no record of a session with us (no connection, or
  1690. * connect_seq == 0), they will send RESETSESION to indicate
  1691. * that they must have reset their session, and may have
  1692. * dropped messages.
  1693. */
  1694. dout("process_connect got RESET peer seq %u\n",
  1695. le32_to_cpu(con->in_reply.connect_seq));
  1696. pr_err("%s%lld %s connection reset\n",
  1697. ENTITY_NAME(con->peer_name),
  1698. ceph_pr_addr(&con->peer_addr.in_addr));
  1699. reset_connection(con);
  1700. con_out_kvec_reset(con);
  1701. ret = prepare_write_connect(con);
  1702. if (ret < 0)
  1703. return ret;
  1704. prepare_read_connect(con);
  1705. /* Tell ceph about it. */
  1706. mutex_unlock(&con->mutex);
  1707. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  1708. if (con->ops->peer_reset)
  1709. con->ops->peer_reset(con);
  1710. mutex_lock(&con->mutex);
  1711. if (con->state != CON_STATE_NEGOTIATING)
  1712. return -EAGAIN;
  1713. break;
  1714. case CEPH_MSGR_TAG_RETRY_SESSION:
  1715. /*
  1716. * If we sent a smaller connect_seq than the peer has, try
  1717. * again with a larger value.
  1718. */
  1719. dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
  1720. le32_to_cpu(con->out_connect.connect_seq),
  1721. le32_to_cpu(con->in_reply.connect_seq));
  1722. con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
  1723. con_out_kvec_reset(con);
  1724. ret = prepare_write_connect(con);
  1725. if (ret < 0)
  1726. return ret;
  1727. prepare_read_connect(con);
  1728. break;
  1729. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1730. /*
  1731. * If we sent a smaller global_seq than the peer has, try
  1732. * again with a larger value.
  1733. */
  1734. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1735. con->peer_global_seq,
  1736. le32_to_cpu(con->in_reply.global_seq));
  1737. get_global_seq(con->msgr,
  1738. le32_to_cpu(con->in_reply.global_seq));
  1739. con_out_kvec_reset(con);
  1740. ret = prepare_write_connect(con);
  1741. if (ret < 0)
  1742. return ret;
  1743. prepare_read_connect(con);
  1744. break;
  1745. case CEPH_MSGR_TAG_SEQ:
  1746. case CEPH_MSGR_TAG_READY:
  1747. if (req_feat & ~server_feat) {
  1748. pr_err("%s%lld %s protocol feature mismatch,"
  1749. " my required %llx > server's %llx, need %llx\n",
  1750. ENTITY_NAME(con->peer_name),
  1751. ceph_pr_addr(&con->peer_addr.in_addr),
  1752. req_feat, server_feat, req_feat & ~server_feat);
  1753. con->error_msg = "missing required protocol features";
  1754. reset_connection(con);
  1755. return -1;
  1756. }
  1757. WARN_ON(con->state != CON_STATE_NEGOTIATING);
  1758. con->state = CON_STATE_OPEN;
  1759. con->auth_retry = 0; /* we authenticated; clear flag */
  1760. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1761. con->connect_seq++;
  1762. con->peer_features = server_feat;
  1763. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1764. con->peer_global_seq,
  1765. le32_to_cpu(con->in_reply.connect_seq),
  1766. con->connect_seq);
  1767. WARN_ON(con->connect_seq !=
  1768. le32_to_cpu(con->in_reply.connect_seq));
  1769. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1770. con_flag_set(con, CON_FLAG_LOSSYTX);
  1771. con->delay = 0; /* reset backoff memory */
  1772. if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
  1773. prepare_write_seq(con);
  1774. prepare_read_seq(con);
  1775. } else {
  1776. prepare_read_tag(con);
  1777. }
  1778. break;
  1779. case CEPH_MSGR_TAG_WAIT:
  1780. /*
  1781. * If there is a connection race (we are opening
  1782. * connections to each other), one of us may just have
  1783. * to WAIT. This shouldn't happen if we are the
  1784. * client.
  1785. */
  1786. pr_err("process_connect got WAIT as client\n");
  1787. con->error_msg = "protocol error, got WAIT as client";
  1788. return -1;
  1789. default:
  1790. pr_err("connect protocol error, will retry\n");
  1791. con->error_msg = "protocol error, garbage tag during connect";
  1792. return -1;
  1793. }
  1794. return 0;
  1795. }
  1796. /*
  1797. * read (part of) an ack
  1798. */
  1799. static int read_partial_ack(struct ceph_connection *con)
  1800. {
  1801. int size = sizeof (con->in_temp_ack);
  1802. int end = size;
  1803. return read_partial(con, end, size, &con->in_temp_ack);
  1804. }
  1805. /*
  1806. * We can finally discard anything that's been acked.
  1807. */
  1808. static void process_ack(struct ceph_connection *con)
  1809. {
  1810. struct ceph_msg *m;
  1811. u64 ack = le64_to_cpu(con->in_temp_ack);
  1812. u64 seq;
  1813. while (!list_empty(&con->out_sent)) {
  1814. m = list_first_entry(&con->out_sent, struct ceph_msg,
  1815. list_head);
  1816. seq = le64_to_cpu(m->hdr.seq);
  1817. if (seq > ack)
  1818. break;
  1819. dout("got ack for seq %llu type %d at %p\n", seq,
  1820. le16_to_cpu(m->hdr.type), m);
  1821. m->ack_stamp = jiffies;
  1822. ceph_msg_remove(m);
  1823. }
  1824. prepare_read_tag(con);
  1825. }
  1826. static int read_partial_message_section(struct ceph_connection *con,
  1827. struct kvec *section,
  1828. unsigned int sec_len, u32 *crc)
  1829. {
  1830. int ret, left;
  1831. BUG_ON(!section);
  1832. while (section->iov_len < sec_len) {
  1833. BUG_ON(section->iov_base == NULL);
  1834. left = sec_len - section->iov_len;
  1835. ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
  1836. section->iov_len, left);
  1837. if (ret <= 0)
  1838. return ret;
  1839. section->iov_len += ret;
  1840. }
  1841. if (section->iov_len == sec_len)
  1842. *crc = crc32c(0, section->iov_base, section->iov_len);
  1843. return 1;
  1844. }
  1845. static int read_partial_msg_data(struct ceph_connection *con)
  1846. {
  1847. struct ceph_msg *msg = con->in_msg;
  1848. struct ceph_msg_pos *msg_pos = &con->in_msg_pos;
  1849. const bool do_datacrc = !con->msgr->nocrc;
  1850. unsigned int data_len;
  1851. struct page *page;
  1852. size_t page_offset;
  1853. size_t length;
  1854. int ret;
  1855. BUG_ON(!msg);
  1856. data_len = le32_to_cpu(con->in_hdr.data_len);
  1857. while (msg_pos->data_pos < data_len) {
  1858. if (ceph_msg_has_pages(msg)) {
  1859. page = ceph_msg_data_next(&msg->p, &page_offset,
  1860. &length, NULL);
  1861. #ifdef CONFIG_BLOCK
  1862. } else if (ceph_msg_has_bio(msg)) {
  1863. page = ceph_msg_data_next(&msg->b, &page_offset,
  1864. &length, NULL);
  1865. #endif
  1866. } else {
  1867. BUG_ON(1);
  1868. }
  1869. ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
  1870. if (ret <= 0)
  1871. return ret;
  1872. if (do_datacrc)
  1873. con->in_data_crc = ceph_crc32c_page(con->in_data_crc,
  1874. page, page_offset, ret);
  1875. in_msg_pos_next(con, length, ret);
  1876. }
  1877. return 1; /* must return > 0 to indicate success */
  1878. }
  1879. /*
  1880. * read (part of) a message.
  1881. */
  1882. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
  1883. static int read_partial_message(struct ceph_connection *con)
  1884. {
  1885. struct ceph_msg *m = con->in_msg;
  1886. int size;
  1887. int end;
  1888. int ret;
  1889. unsigned int front_len, middle_len, data_len;
  1890. bool do_datacrc = !con->msgr->nocrc;
  1891. u64 seq;
  1892. u32 crc;
  1893. dout("read_partial_message con %p msg %p\n", con, m);
  1894. /* header */
  1895. size = sizeof (con->in_hdr);
  1896. end = size;
  1897. ret = read_partial(con, end, size, &con->in_hdr);
  1898. if (ret <= 0)
  1899. return ret;
  1900. crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
  1901. if (cpu_to_le32(crc) != con->in_hdr.crc) {
  1902. pr_err("read_partial_message bad hdr "
  1903. " crc %u != expected %u\n",
  1904. crc, con->in_hdr.crc);
  1905. return -EBADMSG;
  1906. }
  1907. front_len = le32_to_cpu(con->in_hdr.front_len);
  1908. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  1909. return -EIO;
  1910. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1911. if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
  1912. return -EIO;
  1913. data_len = le32_to_cpu(con->in_hdr.data_len);
  1914. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  1915. return -EIO;
  1916. /* verify seq# */
  1917. seq = le64_to_cpu(con->in_hdr.seq);
  1918. if ((s64)seq - (s64)con->in_seq < 1) {
  1919. pr_info("skipping %s%lld %s seq %lld expected %lld\n",
  1920. ENTITY_NAME(con->peer_name),
  1921. ceph_pr_addr(&con->peer_addr.in_addr),
  1922. seq, con->in_seq + 1);
  1923. con->in_base_pos = -front_len - middle_len - data_len -
  1924. sizeof(m->footer);
  1925. con->in_tag = CEPH_MSGR_TAG_READY;
  1926. return 0;
  1927. } else if ((s64)seq - (s64)con->in_seq > 1) {
  1928. pr_err("read_partial_message bad seq %lld expected %lld\n",
  1929. seq, con->in_seq + 1);
  1930. con->error_msg = "bad message sequence # for incoming message";
  1931. return -EBADMSG;
  1932. }
  1933. /* allocate message? */
  1934. if (!con->in_msg) {
  1935. int skip = 0;
  1936. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  1937. front_len, data_len);
  1938. ret = ceph_con_in_msg_alloc(con, &skip);
  1939. if (ret < 0)
  1940. return ret;
  1941. if (skip) {
  1942. /* skip this message */
  1943. dout("alloc_msg said skip message\n");
  1944. BUG_ON(con->in_msg);
  1945. con->in_base_pos = -front_len - middle_len - data_len -
  1946. sizeof(m->footer);
  1947. con->in_tag = CEPH_MSGR_TAG_READY;
  1948. con->in_seq++;
  1949. return 0;
  1950. }
  1951. BUG_ON(!con->in_msg);
  1952. BUG_ON(con->in_msg->con != con);
  1953. m = con->in_msg;
  1954. m->front.iov_len = 0; /* haven't read it yet */
  1955. if (m->middle)
  1956. m->middle->vec.iov_len = 0;
  1957. /* prepare for data payload, if any */
  1958. if (data_len)
  1959. prepare_message_data(con->in_msg, &con->in_msg_pos);
  1960. }
  1961. /* front */
  1962. ret = read_partial_message_section(con, &m->front, front_len,
  1963. &con->in_front_crc);
  1964. if (ret <= 0)
  1965. return ret;
  1966. /* middle */
  1967. if (m->middle) {
  1968. ret = read_partial_message_section(con, &m->middle->vec,
  1969. middle_len,
  1970. &con->in_middle_crc);
  1971. if (ret <= 0)
  1972. return ret;
  1973. }
  1974. /* (page) data */
  1975. if (data_len) {
  1976. ret = read_partial_msg_data(con);
  1977. if (ret <= 0)
  1978. return ret;
  1979. }
  1980. /* footer */
  1981. size = sizeof (m->footer);
  1982. end += size;
  1983. ret = read_partial(con, end, size, &m->footer);
  1984. if (ret <= 0)
  1985. return ret;
  1986. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  1987. m, front_len, m->footer.front_crc, middle_len,
  1988. m->footer.middle_crc, data_len, m->footer.data_crc);
  1989. /* crc ok? */
  1990. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  1991. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  1992. m, con->in_front_crc, m->footer.front_crc);
  1993. return -EBADMSG;
  1994. }
  1995. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  1996. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  1997. m, con->in_middle_crc, m->footer.middle_crc);
  1998. return -EBADMSG;
  1999. }
  2000. if (do_datacrc &&
  2001. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  2002. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  2003. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  2004. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  2005. return -EBADMSG;
  2006. }
  2007. return 1; /* done! */
  2008. }
  2009. /*
  2010. * Process message. This happens in the worker thread. The callback should
  2011. * be careful not to do anything that waits on other incoming messages or it
  2012. * may deadlock.
  2013. */
  2014. static void process_message(struct ceph_connection *con)
  2015. {
  2016. struct ceph_msg *msg;
  2017. BUG_ON(con->in_msg->con != con);
  2018. con->in_msg->con = NULL;
  2019. msg = con->in_msg;
  2020. con->in_msg = NULL;
  2021. con->ops->put(con);
  2022. /* if first message, set peer_name */
  2023. if (con->peer_name.type == 0)
  2024. con->peer_name = msg->hdr.src;
  2025. con->in_seq++;
  2026. mutex_unlock(&con->mutex);
  2027. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  2028. msg, le64_to_cpu(msg->hdr.seq),
  2029. ENTITY_NAME(msg->hdr.src),
  2030. le16_to_cpu(msg->hdr.type),
  2031. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2032. le32_to_cpu(msg->hdr.front_len),
  2033. le32_to_cpu(msg->hdr.data_len),
  2034. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  2035. con->ops->dispatch(con, msg);
  2036. mutex_lock(&con->mutex);
  2037. }
  2038. /*
  2039. * Write something to the socket. Called in a worker thread when the
  2040. * socket appears to be writeable and we have something ready to send.
  2041. */
  2042. static int try_write(struct ceph_connection *con)
  2043. {
  2044. int ret = 1;
  2045. dout("try_write start %p state %lu\n", con, con->state);
  2046. more:
  2047. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  2048. /* open the socket first? */
  2049. if (con->state == CON_STATE_PREOPEN) {
  2050. BUG_ON(con->sock);
  2051. con->state = CON_STATE_CONNECTING;
  2052. con_out_kvec_reset(con);
  2053. prepare_write_banner(con);
  2054. prepare_read_banner(con);
  2055. BUG_ON(con->in_msg);
  2056. con->in_tag = CEPH_MSGR_TAG_READY;
  2057. dout("try_write initiating connect on %p new state %lu\n",
  2058. con, con->state);
  2059. ret = ceph_tcp_connect(con);
  2060. if (ret < 0) {
  2061. con->error_msg = "connect error";
  2062. goto out;
  2063. }
  2064. }
  2065. more_kvec:
  2066. /* kvec data queued? */
  2067. if (con->out_skip) {
  2068. ret = write_partial_skip(con);
  2069. if (ret <= 0)
  2070. goto out;
  2071. }
  2072. if (con->out_kvec_left) {
  2073. ret = write_partial_kvec(con);
  2074. if (ret <= 0)
  2075. goto out;
  2076. }
  2077. /* msg pages? */
  2078. if (con->out_msg) {
  2079. if (con->out_msg_done) {
  2080. ceph_msg_put(con->out_msg);
  2081. con->out_msg = NULL; /* we're done with this one */
  2082. goto do_next;
  2083. }
  2084. ret = write_partial_message_data(con);
  2085. if (ret == 1)
  2086. goto more_kvec; /* we need to send the footer, too! */
  2087. if (ret == 0)
  2088. goto out;
  2089. if (ret < 0) {
  2090. dout("try_write write_partial_message_data err %d\n",
  2091. ret);
  2092. goto out;
  2093. }
  2094. }
  2095. do_next:
  2096. if (con->state == CON_STATE_OPEN) {
  2097. /* is anything else pending? */
  2098. if (!list_empty(&con->out_queue)) {
  2099. prepare_write_message(con);
  2100. goto more;
  2101. }
  2102. if (con->in_seq > con->in_seq_acked) {
  2103. prepare_write_ack(con);
  2104. goto more;
  2105. }
  2106. if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2107. prepare_write_keepalive(con);
  2108. goto more;
  2109. }
  2110. }
  2111. /* Nothing to do! */
  2112. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2113. dout("try_write nothing else to write.\n");
  2114. ret = 0;
  2115. out:
  2116. dout("try_write done on %p ret %d\n", con, ret);
  2117. return ret;
  2118. }
  2119. /*
  2120. * Read what we can from the socket.
  2121. */
  2122. static int try_read(struct ceph_connection *con)
  2123. {
  2124. int ret = -1;
  2125. more:
  2126. dout("try_read start on %p state %lu\n", con, con->state);
  2127. if (con->state != CON_STATE_CONNECTING &&
  2128. con->state != CON_STATE_NEGOTIATING &&
  2129. con->state != CON_STATE_OPEN)
  2130. return 0;
  2131. BUG_ON(!con->sock);
  2132. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  2133. con->in_base_pos);
  2134. if (con->state == CON_STATE_CONNECTING) {
  2135. dout("try_read connecting\n");
  2136. ret = read_partial_banner(con);
  2137. if (ret <= 0)
  2138. goto out;
  2139. ret = process_banner(con);
  2140. if (ret < 0)
  2141. goto out;
  2142. con->state = CON_STATE_NEGOTIATING;
  2143. /*
  2144. * Received banner is good, exchange connection info.
  2145. * Do not reset out_kvec, as sending our banner raced
  2146. * with receiving peer banner after connect completed.
  2147. */
  2148. ret = prepare_write_connect(con);
  2149. if (ret < 0)
  2150. goto out;
  2151. prepare_read_connect(con);
  2152. /* Send connection info before awaiting response */
  2153. goto out;
  2154. }
  2155. if (con->state == CON_STATE_NEGOTIATING) {
  2156. dout("try_read negotiating\n");
  2157. ret = read_partial_connect(con);
  2158. if (ret <= 0)
  2159. goto out;
  2160. ret = process_connect(con);
  2161. if (ret < 0)
  2162. goto out;
  2163. goto more;
  2164. }
  2165. WARN_ON(con->state != CON_STATE_OPEN);
  2166. if (con->in_base_pos < 0) {
  2167. /*
  2168. * skipping + discarding content.
  2169. *
  2170. * FIXME: there must be a better way to do this!
  2171. */
  2172. static char buf[SKIP_BUF_SIZE];
  2173. int skip = min((int) sizeof (buf), -con->in_base_pos);
  2174. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  2175. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  2176. if (ret <= 0)
  2177. goto out;
  2178. con->in_base_pos += ret;
  2179. if (con->in_base_pos)
  2180. goto more;
  2181. }
  2182. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  2183. /*
  2184. * what's next?
  2185. */
  2186. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  2187. if (ret <= 0)
  2188. goto out;
  2189. dout("try_read got tag %d\n", (int)con->in_tag);
  2190. switch (con->in_tag) {
  2191. case CEPH_MSGR_TAG_MSG:
  2192. prepare_read_message(con);
  2193. break;
  2194. case CEPH_MSGR_TAG_ACK:
  2195. prepare_read_ack(con);
  2196. break;
  2197. case CEPH_MSGR_TAG_CLOSE:
  2198. con_close_socket(con);
  2199. con->state = CON_STATE_CLOSED;
  2200. goto out;
  2201. default:
  2202. goto bad_tag;
  2203. }
  2204. }
  2205. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  2206. ret = read_partial_message(con);
  2207. if (ret <= 0) {
  2208. switch (ret) {
  2209. case -EBADMSG:
  2210. con->error_msg = "bad crc";
  2211. ret = -EIO;
  2212. break;
  2213. case -EIO:
  2214. con->error_msg = "io error";
  2215. break;
  2216. }
  2217. goto out;
  2218. }
  2219. if (con->in_tag == CEPH_MSGR_TAG_READY)
  2220. goto more;
  2221. process_message(con);
  2222. if (con->state == CON_STATE_OPEN)
  2223. prepare_read_tag(con);
  2224. goto more;
  2225. }
  2226. if (con->in_tag == CEPH_MSGR_TAG_ACK ||
  2227. con->in_tag == CEPH_MSGR_TAG_SEQ) {
  2228. /*
  2229. * the final handshake seq exchange is semantically
  2230. * equivalent to an ACK
  2231. */
  2232. ret = read_partial_ack(con);
  2233. if (ret <= 0)
  2234. goto out;
  2235. process_ack(con);
  2236. goto more;
  2237. }
  2238. out:
  2239. dout("try_read done on %p ret %d\n", con, ret);
  2240. return ret;
  2241. bad_tag:
  2242. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  2243. con->error_msg = "protocol error, garbage tag";
  2244. ret = -1;
  2245. goto out;
  2246. }
  2247. /*
  2248. * Atomically queue work on a connection after the specified delay.
  2249. * Bump @con reference to avoid races with connection teardown.
  2250. * Returns 0 if work was queued, or an error code otherwise.
  2251. */
  2252. static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
  2253. {
  2254. if (!con->ops->get(con)) {
  2255. dout("%s %p ref count 0\n", __func__, con);
  2256. return -ENOENT;
  2257. }
  2258. if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
  2259. dout("%s %p - already queued\n", __func__, con);
  2260. con->ops->put(con);
  2261. return -EBUSY;
  2262. }
  2263. dout("%s %p %lu\n", __func__, con, delay);
  2264. return 0;
  2265. }
  2266. static void queue_con(struct ceph_connection *con)
  2267. {
  2268. (void) queue_con_delay(con, 0);
  2269. }
  2270. static bool con_sock_closed(struct ceph_connection *con)
  2271. {
  2272. if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
  2273. return false;
  2274. #define CASE(x) \
  2275. case CON_STATE_ ## x: \
  2276. con->error_msg = "socket closed (con state " #x ")"; \
  2277. break;
  2278. switch (con->state) {
  2279. CASE(CLOSED);
  2280. CASE(PREOPEN);
  2281. CASE(CONNECTING);
  2282. CASE(NEGOTIATING);
  2283. CASE(OPEN);
  2284. CASE(STANDBY);
  2285. default:
  2286. pr_warning("%s con %p unrecognized state %lu\n",
  2287. __func__, con, con->state);
  2288. con->error_msg = "unrecognized con state";
  2289. BUG();
  2290. break;
  2291. }
  2292. #undef CASE
  2293. return true;
  2294. }
  2295. static bool con_backoff(struct ceph_connection *con)
  2296. {
  2297. int ret;
  2298. if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
  2299. return false;
  2300. ret = queue_con_delay(con, round_jiffies_relative(con->delay));
  2301. if (ret) {
  2302. dout("%s: con %p FAILED to back off %lu\n", __func__,
  2303. con, con->delay);
  2304. BUG_ON(ret == -ENOENT);
  2305. con_flag_set(con, CON_FLAG_BACKOFF);
  2306. }
  2307. return true;
  2308. }
  2309. /* Finish fault handling; con->mutex must *not* be held here */
  2310. static void con_fault_finish(struct ceph_connection *con)
  2311. {
  2312. /*
  2313. * in case we faulted due to authentication, invalidate our
  2314. * current tickets so that we can get new ones.
  2315. */
  2316. if (con->auth_retry && con->ops->invalidate_authorizer) {
  2317. dout("calling invalidate_authorizer()\n");
  2318. con->ops->invalidate_authorizer(con);
  2319. }
  2320. if (con->ops->fault)
  2321. con->ops->fault(con);
  2322. }
  2323. /*
  2324. * Do some work on a connection. Drop a connection ref when we're done.
  2325. */
  2326. static void con_work(struct work_struct *work)
  2327. {
  2328. struct ceph_connection *con = container_of(work, struct ceph_connection,
  2329. work.work);
  2330. bool fault;
  2331. mutex_lock(&con->mutex);
  2332. while (true) {
  2333. int ret;
  2334. if ((fault = con_sock_closed(con))) {
  2335. dout("%s: con %p SOCK_CLOSED\n", __func__, con);
  2336. break;
  2337. }
  2338. if (con_backoff(con)) {
  2339. dout("%s: con %p BACKOFF\n", __func__, con);
  2340. break;
  2341. }
  2342. if (con->state == CON_STATE_STANDBY) {
  2343. dout("%s: con %p STANDBY\n", __func__, con);
  2344. break;
  2345. }
  2346. if (con->state == CON_STATE_CLOSED) {
  2347. dout("%s: con %p CLOSED\n", __func__, con);
  2348. BUG_ON(con->sock);
  2349. break;
  2350. }
  2351. if (con->state == CON_STATE_PREOPEN) {
  2352. dout("%s: con %p PREOPEN\n", __func__, con);
  2353. BUG_ON(con->sock);
  2354. }
  2355. ret = try_read(con);
  2356. if (ret < 0) {
  2357. if (ret == -EAGAIN)
  2358. continue;
  2359. con->error_msg = "socket error on read";
  2360. fault = true;
  2361. break;
  2362. }
  2363. ret = try_write(con);
  2364. if (ret < 0) {
  2365. if (ret == -EAGAIN)
  2366. continue;
  2367. con->error_msg = "socket error on write";
  2368. fault = true;
  2369. }
  2370. break; /* If we make it to here, we're done */
  2371. }
  2372. if (fault)
  2373. con_fault(con);
  2374. mutex_unlock(&con->mutex);
  2375. if (fault)
  2376. con_fault_finish(con);
  2377. con->ops->put(con);
  2378. }
  2379. /*
  2380. * Generic error/fault handler. A retry mechanism is used with
  2381. * exponential backoff
  2382. */
  2383. static void con_fault(struct ceph_connection *con)
  2384. {
  2385. pr_warning("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  2386. ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
  2387. dout("fault %p state %lu to peer %s\n",
  2388. con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
  2389. WARN_ON(con->state != CON_STATE_CONNECTING &&
  2390. con->state != CON_STATE_NEGOTIATING &&
  2391. con->state != CON_STATE_OPEN);
  2392. con_close_socket(con);
  2393. if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
  2394. dout("fault on LOSSYTX channel, marking CLOSED\n");
  2395. con->state = CON_STATE_CLOSED;
  2396. return;
  2397. }
  2398. if (con->in_msg) {
  2399. BUG_ON(con->in_msg->con != con);
  2400. con->in_msg->con = NULL;
  2401. ceph_msg_put(con->in_msg);
  2402. con->in_msg = NULL;
  2403. con->ops->put(con);
  2404. }
  2405. /* Requeue anything that hasn't been acked */
  2406. list_splice_init(&con->out_sent, &con->out_queue);
  2407. /* If there are no messages queued or keepalive pending, place
  2408. * the connection in a STANDBY state */
  2409. if (list_empty(&con->out_queue) &&
  2410. !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2411. dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
  2412. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2413. con->state = CON_STATE_STANDBY;
  2414. } else {
  2415. /* retry after a delay. */
  2416. con->state = CON_STATE_PREOPEN;
  2417. if (con->delay == 0)
  2418. con->delay = BASE_DELAY_INTERVAL;
  2419. else if (con->delay < MAX_DELAY_INTERVAL)
  2420. con->delay *= 2;
  2421. con_flag_set(con, CON_FLAG_BACKOFF);
  2422. queue_con(con);
  2423. }
  2424. }
  2425. /*
  2426. * initialize a new messenger instance
  2427. */
  2428. void ceph_messenger_init(struct ceph_messenger *msgr,
  2429. struct ceph_entity_addr *myaddr,
  2430. u32 supported_features,
  2431. u32 required_features,
  2432. bool nocrc)
  2433. {
  2434. msgr->supported_features = supported_features;
  2435. msgr->required_features = required_features;
  2436. spin_lock_init(&msgr->global_seq_lock);
  2437. if (myaddr)
  2438. msgr->inst.addr = *myaddr;
  2439. /* select a random nonce */
  2440. msgr->inst.addr.type = 0;
  2441. get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
  2442. encode_my_addr(msgr);
  2443. msgr->nocrc = nocrc;
  2444. atomic_set(&msgr->stopping, 0);
  2445. dout("%s %p\n", __func__, msgr);
  2446. }
  2447. EXPORT_SYMBOL(ceph_messenger_init);
  2448. static void clear_standby(struct ceph_connection *con)
  2449. {
  2450. /* come back from STANDBY? */
  2451. if (con->state == CON_STATE_STANDBY) {
  2452. dout("clear_standby %p and ++connect_seq\n", con);
  2453. con->state = CON_STATE_PREOPEN;
  2454. con->connect_seq++;
  2455. WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
  2456. WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
  2457. }
  2458. }
  2459. /*
  2460. * Queue up an outgoing message on the given connection.
  2461. */
  2462. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  2463. {
  2464. /* set src+dst */
  2465. msg->hdr.src = con->msgr->inst.name;
  2466. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  2467. msg->needs_out_seq = true;
  2468. mutex_lock(&con->mutex);
  2469. if (con->state == CON_STATE_CLOSED) {
  2470. dout("con_send %p closed, dropping %p\n", con, msg);
  2471. ceph_msg_put(msg);
  2472. mutex_unlock(&con->mutex);
  2473. return;
  2474. }
  2475. BUG_ON(msg->con != NULL);
  2476. msg->con = con->ops->get(con);
  2477. BUG_ON(msg->con == NULL);
  2478. BUG_ON(!list_empty(&msg->list_head));
  2479. list_add_tail(&msg->list_head, &con->out_queue);
  2480. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  2481. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  2482. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2483. le32_to_cpu(msg->hdr.front_len),
  2484. le32_to_cpu(msg->hdr.middle_len),
  2485. le32_to_cpu(msg->hdr.data_len));
  2486. clear_standby(con);
  2487. mutex_unlock(&con->mutex);
  2488. /* if there wasn't anything waiting to send before, queue
  2489. * new work */
  2490. if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2491. queue_con(con);
  2492. }
  2493. EXPORT_SYMBOL(ceph_con_send);
  2494. /*
  2495. * Revoke a message that was previously queued for send
  2496. */
  2497. void ceph_msg_revoke(struct ceph_msg *msg)
  2498. {
  2499. struct ceph_connection *con = msg->con;
  2500. if (!con)
  2501. return; /* Message not in our possession */
  2502. mutex_lock(&con->mutex);
  2503. if (!list_empty(&msg->list_head)) {
  2504. dout("%s %p msg %p - was on queue\n", __func__, con, msg);
  2505. list_del_init(&msg->list_head);
  2506. BUG_ON(msg->con == NULL);
  2507. msg->con->ops->put(msg->con);
  2508. msg->con = NULL;
  2509. msg->hdr.seq = 0;
  2510. ceph_msg_put(msg);
  2511. }
  2512. if (con->out_msg == msg) {
  2513. dout("%s %p msg %p - was sending\n", __func__, con, msg);
  2514. con->out_msg = NULL;
  2515. if (con->out_kvec_is_msg) {
  2516. con->out_skip = con->out_kvec_bytes;
  2517. con->out_kvec_is_msg = false;
  2518. }
  2519. msg->hdr.seq = 0;
  2520. ceph_msg_put(msg);
  2521. }
  2522. mutex_unlock(&con->mutex);
  2523. }
  2524. /*
  2525. * Revoke a message that we may be reading data into
  2526. */
  2527. void ceph_msg_revoke_incoming(struct ceph_msg *msg)
  2528. {
  2529. struct ceph_connection *con;
  2530. BUG_ON(msg == NULL);
  2531. if (!msg->con) {
  2532. dout("%s msg %p null con\n", __func__, msg);
  2533. return; /* Message not in our possession */
  2534. }
  2535. con = msg->con;
  2536. mutex_lock(&con->mutex);
  2537. if (con->in_msg == msg) {
  2538. unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
  2539. unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
  2540. unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
  2541. /* skip rest of message */
  2542. dout("%s %p msg %p revoked\n", __func__, con, msg);
  2543. con->in_base_pos = con->in_base_pos -
  2544. sizeof(struct ceph_msg_header) -
  2545. front_len -
  2546. middle_len -
  2547. data_len -
  2548. sizeof(struct ceph_msg_footer);
  2549. ceph_msg_put(con->in_msg);
  2550. con->in_msg = NULL;
  2551. con->in_tag = CEPH_MSGR_TAG_READY;
  2552. con->in_seq++;
  2553. } else {
  2554. dout("%s %p in_msg %p msg %p no-op\n",
  2555. __func__, con, con->in_msg, msg);
  2556. }
  2557. mutex_unlock(&con->mutex);
  2558. }
  2559. /*
  2560. * Queue a keepalive byte to ensure the tcp connection is alive.
  2561. */
  2562. void ceph_con_keepalive(struct ceph_connection *con)
  2563. {
  2564. dout("con_keepalive %p\n", con);
  2565. mutex_lock(&con->mutex);
  2566. clear_standby(con);
  2567. mutex_unlock(&con->mutex);
  2568. if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
  2569. con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2570. queue_con(con);
  2571. }
  2572. EXPORT_SYMBOL(ceph_con_keepalive);
  2573. static void ceph_msg_data_init(struct ceph_msg_data *data)
  2574. {
  2575. data->type = CEPH_MSG_DATA_NONE;
  2576. }
  2577. void ceph_msg_data_set_pages(struct ceph_msg *msg, struct page **pages,
  2578. size_t length, size_t alignment)
  2579. {
  2580. BUG_ON(!pages);
  2581. BUG_ON(!length);
  2582. BUG_ON(msg->p.type != CEPH_MSG_DATA_NONE);
  2583. msg->p.type = CEPH_MSG_DATA_PAGES;
  2584. msg->p.pages = pages;
  2585. msg->p.length = length;
  2586. msg->p.alignment = alignment & ~PAGE_MASK;
  2587. }
  2588. EXPORT_SYMBOL(ceph_msg_data_set_pages);
  2589. void ceph_msg_data_set_pagelist(struct ceph_msg *msg,
  2590. struct ceph_pagelist *pagelist)
  2591. {
  2592. BUG_ON(!pagelist);
  2593. BUG_ON(!pagelist->length);
  2594. BUG_ON(msg->l.type != CEPH_MSG_DATA_NONE);
  2595. msg->l.type = CEPH_MSG_DATA_PAGELIST;
  2596. msg->l.pagelist = pagelist;
  2597. }
  2598. EXPORT_SYMBOL(ceph_msg_data_set_pagelist);
  2599. void ceph_msg_data_set_bio(struct ceph_msg *msg, struct bio *bio)
  2600. {
  2601. BUG_ON(!bio);
  2602. BUG_ON(msg->b.type != CEPH_MSG_DATA_NONE);
  2603. msg->b.type = CEPH_MSG_DATA_BIO;
  2604. msg->b.bio = bio;
  2605. }
  2606. EXPORT_SYMBOL(ceph_msg_data_set_bio);
  2607. /*
  2608. * construct a new message with given type, size
  2609. * the new msg has a ref count of 1.
  2610. */
  2611. struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  2612. bool can_fail)
  2613. {
  2614. struct ceph_msg *m;
  2615. m = kzalloc(sizeof(*m), flags);
  2616. if (m == NULL)
  2617. goto out;
  2618. m->hdr.type = cpu_to_le16(type);
  2619. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  2620. m->hdr.front_len = cpu_to_le32(front_len);
  2621. INIT_LIST_HEAD(&m->list_head);
  2622. kref_init(&m->kref);
  2623. ceph_msg_data_init(&m->p);
  2624. ceph_msg_data_init(&m->l);
  2625. ceph_msg_data_init(&m->b);
  2626. /* front */
  2627. m->front_max = front_len;
  2628. if (front_len) {
  2629. if (front_len > PAGE_CACHE_SIZE) {
  2630. m->front.iov_base = __vmalloc(front_len, flags,
  2631. PAGE_KERNEL);
  2632. m->front_is_vmalloc = true;
  2633. } else {
  2634. m->front.iov_base = kmalloc(front_len, flags);
  2635. }
  2636. if (m->front.iov_base == NULL) {
  2637. dout("ceph_msg_new can't allocate %d bytes\n",
  2638. front_len);
  2639. goto out2;
  2640. }
  2641. } else {
  2642. m->front.iov_base = NULL;
  2643. }
  2644. m->front.iov_len = front_len;
  2645. dout("ceph_msg_new %p front %d\n", m, front_len);
  2646. return m;
  2647. out2:
  2648. ceph_msg_put(m);
  2649. out:
  2650. if (!can_fail) {
  2651. pr_err("msg_new can't create type %d front %d\n", type,
  2652. front_len);
  2653. WARN_ON(1);
  2654. } else {
  2655. dout("msg_new can't create type %d front %d\n", type,
  2656. front_len);
  2657. }
  2658. return NULL;
  2659. }
  2660. EXPORT_SYMBOL(ceph_msg_new);
  2661. /*
  2662. * Allocate "middle" portion of a message, if it is needed and wasn't
  2663. * allocated by alloc_msg. This allows us to read a small fixed-size
  2664. * per-type header in the front and then gracefully fail (i.e.,
  2665. * propagate the error to the caller based on info in the front) when
  2666. * the middle is too large.
  2667. */
  2668. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  2669. {
  2670. int type = le16_to_cpu(msg->hdr.type);
  2671. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  2672. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  2673. ceph_msg_type_name(type), middle_len);
  2674. BUG_ON(!middle_len);
  2675. BUG_ON(msg->middle);
  2676. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  2677. if (!msg->middle)
  2678. return -ENOMEM;
  2679. return 0;
  2680. }
  2681. /*
  2682. * Allocate a message for receiving an incoming message on a
  2683. * connection, and save the result in con->in_msg. Uses the
  2684. * connection's private alloc_msg op if available.
  2685. *
  2686. * Returns 0 on success, or a negative error code.
  2687. *
  2688. * On success, if we set *skip = 1:
  2689. * - the next message should be skipped and ignored.
  2690. * - con->in_msg == NULL
  2691. * or if we set *skip = 0:
  2692. * - con->in_msg is non-null.
  2693. * On error (ENOMEM, EAGAIN, ...),
  2694. * - con->in_msg == NULL
  2695. */
  2696. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
  2697. {
  2698. struct ceph_msg_header *hdr = &con->in_hdr;
  2699. int middle_len = le32_to_cpu(hdr->middle_len);
  2700. struct ceph_msg *msg;
  2701. int ret = 0;
  2702. BUG_ON(con->in_msg != NULL);
  2703. BUG_ON(!con->ops->alloc_msg);
  2704. mutex_unlock(&con->mutex);
  2705. msg = con->ops->alloc_msg(con, hdr, skip);
  2706. mutex_lock(&con->mutex);
  2707. if (con->state != CON_STATE_OPEN) {
  2708. if (msg)
  2709. ceph_msg_put(msg);
  2710. return -EAGAIN;
  2711. }
  2712. if (msg) {
  2713. BUG_ON(*skip);
  2714. con->in_msg = msg;
  2715. con->in_msg->con = con->ops->get(con);
  2716. BUG_ON(con->in_msg->con == NULL);
  2717. } else {
  2718. /*
  2719. * Null message pointer means either we should skip
  2720. * this message or we couldn't allocate memory. The
  2721. * former is not an error.
  2722. */
  2723. if (*skip)
  2724. return 0;
  2725. con->error_msg = "error allocating memory for incoming message";
  2726. return -ENOMEM;
  2727. }
  2728. memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
  2729. if (middle_len && !con->in_msg->middle) {
  2730. ret = ceph_alloc_middle(con, con->in_msg);
  2731. if (ret < 0) {
  2732. ceph_msg_put(con->in_msg);
  2733. con->in_msg = NULL;
  2734. }
  2735. }
  2736. return ret;
  2737. }
  2738. /*
  2739. * Free a generically kmalloc'd message.
  2740. */
  2741. void ceph_msg_kfree(struct ceph_msg *m)
  2742. {
  2743. dout("msg_kfree %p\n", m);
  2744. if (m->front_is_vmalloc)
  2745. vfree(m->front.iov_base);
  2746. else
  2747. kfree(m->front.iov_base);
  2748. kfree(m);
  2749. }
  2750. /*
  2751. * Drop a msg ref. Destroy as needed.
  2752. */
  2753. void ceph_msg_last_put(struct kref *kref)
  2754. {
  2755. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  2756. dout("ceph_msg_put last one on %p\n", m);
  2757. WARN_ON(!list_empty(&m->list_head));
  2758. /* drop middle, data, if any */
  2759. if (m->middle) {
  2760. ceph_buffer_put(m->middle);
  2761. m->middle = NULL;
  2762. }
  2763. if (ceph_msg_has_pages(m)) {
  2764. m->p.length = 0;
  2765. m->p.pages = NULL;
  2766. m->p.type = CEPH_OSD_DATA_TYPE_NONE;
  2767. }
  2768. if (ceph_msg_has_pagelist(m)) {
  2769. ceph_pagelist_release(m->l.pagelist);
  2770. kfree(m->l.pagelist);
  2771. m->l.pagelist = NULL;
  2772. m->l.type = CEPH_OSD_DATA_TYPE_NONE;
  2773. }
  2774. if (ceph_msg_has_bio(m)) {
  2775. m->b.bio = NULL;
  2776. m->b.type = CEPH_OSD_DATA_TYPE_NONE;
  2777. }
  2778. if (m->pool)
  2779. ceph_msgpool_put(m->pool, m);
  2780. else
  2781. ceph_msg_kfree(m);
  2782. }
  2783. EXPORT_SYMBOL(ceph_msg_last_put);
  2784. void ceph_msg_dump(struct ceph_msg *msg)
  2785. {
  2786. pr_debug("msg_dump %p (front_max %d length %zd)\n", msg,
  2787. msg->front_max, msg->p.length);
  2788. print_hex_dump(KERN_DEBUG, "header: ",
  2789. DUMP_PREFIX_OFFSET, 16, 1,
  2790. &msg->hdr, sizeof(msg->hdr), true);
  2791. print_hex_dump(KERN_DEBUG, " front: ",
  2792. DUMP_PREFIX_OFFSET, 16, 1,
  2793. msg->front.iov_base, msg->front.iov_len, true);
  2794. if (msg->middle)
  2795. print_hex_dump(KERN_DEBUG, "middle: ",
  2796. DUMP_PREFIX_OFFSET, 16, 1,
  2797. msg->middle->vec.iov_base,
  2798. msg->middle->vec.iov_len, true);
  2799. print_hex_dump(KERN_DEBUG, "footer: ",
  2800. DUMP_PREFIX_OFFSET, 16, 1,
  2801. &msg->footer, sizeof(msg->footer), true);
  2802. }
  2803. EXPORT_SYMBOL(ceph_msg_dump);