messenger.c 83 KB

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