osd_client.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/err.h>
  4. #include <linux/highmem.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #ifdef CONFIG_BLOCK
  10. #include <linux/bio.h>
  11. #endif
  12. #include <linux/ceph/libceph.h>
  13. #include <linux/ceph/osd_client.h>
  14. #include <linux/ceph/messenger.h>
  15. #include <linux/ceph/decode.h>
  16. #include <linux/ceph/auth.h>
  17. #include <linux/ceph/pagelist.h>
  18. #define OSD_OP_FRONT_LEN 4096
  19. #define OSD_OPREPLY_FRONT_LEN 512
  20. static struct kmem_cache *ceph_osd_request_cache;
  21. static const struct ceph_connection_operations osd_con_ops;
  22. static void __send_queued(struct ceph_osd_client *osdc);
  23. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  24. static void __register_request(struct ceph_osd_client *osdc,
  25. struct ceph_osd_request *req);
  26. static void __unregister_request(struct ceph_osd_client *osdc,
  27. struct ceph_osd_request *req);
  28. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  29. struct ceph_osd_request *req);
  30. static void __enqueue_request(struct ceph_osd_request *req);
  31. static void __send_request(struct ceph_osd_client *osdc,
  32. struct ceph_osd_request *req);
  33. /*
  34. * Implement client access to distributed object storage cluster.
  35. *
  36. * All data objects are stored within a cluster/cloud of OSDs, or
  37. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  38. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  39. * remote daemons serving up and coordinating consistent and safe
  40. * access to storage.
  41. *
  42. * Cluster membership and the mapping of data objects onto storage devices
  43. * are described by the osd map.
  44. *
  45. * We keep track of pending OSD requests (read, write), resubmit
  46. * requests to different OSDs when the cluster topology/data layout
  47. * change, or retry the affected requests when the communications
  48. * channel with an OSD is reset.
  49. */
  50. /*
  51. * calculate the mapping of a file extent onto an object, and fill out the
  52. * request accordingly. shorten extent as necessary if it crosses an
  53. * object boundary.
  54. *
  55. * fill osd op in request message.
  56. */
  57. static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
  58. u64 *objnum, u64 *objoff, u64 *objlen)
  59. {
  60. u64 orig_len = *plen;
  61. int r;
  62. /* object extent? */
  63. r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
  64. objoff, objlen);
  65. if (r < 0)
  66. return r;
  67. if (*objlen < orig_len) {
  68. *plen = *objlen;
  69. dout(" skipping last %llu, final file extent %llu~%llu\n",
  70. orig_len - *plen, off, *plen);
  71. }
  72. dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
  73. return 0;
  74. }
  75. static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
  76. {
  77. memset(osd_data, 0, sizeof (*osd_data));
  78. osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
  79. }
  80. static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
  81. struct page **pages, u64 length, u32 alignment,
  82. bool pages_from_pool, bool own_pages)
  83. {
  84. osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
  85. osd_data->pages = pages;
  86. osd_data->length = length;
  87. osd_data->alignment = alignment;
  88. osd_data->pages_from_pool = pages_from_pool;
  89. osd_data->own_pages = own_pages;
  90. }
  91. static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
  92. struct ceph_pagelist *pagelist)
  93. {
  94. osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
  95. osd_data->pagelist = pagelist;
  96. }
  97. #ifdef CONFIG_BLOCK
  98. static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
  99. struct bio *bio, size_t bio_length)
  100. {
  101. osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
  102. osd_data->bio = bio;
  103. osd_data->bio_length = bio_length;
  104. }
  105. #endif /* CONFIG_BLOCK */
  106. #define osd_req_op_data(oreq, whch, typ, fld) \
  107. ({ \
  108. struct ceph_osd_request *__oreq = (oreq); \
  109. unsigned int __whch = (whch); \
  110. BUG_ON(__whch >= __oreq->r_num_ops); \
  111. &__oreq->r_ops[__whch].typ.fld; \
  112. })
  113. static struct ceph_osd_data *
  114. osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
  115. {
  116. BUG_ON(which >= osd_req->r_num_ops);
  117. return &osd_req->r_ops[which].raw_data_in;
  118. }
  119. struct ceph_osd_data *
  120. osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
  121. unsigned int which)
  122. {
  123. return osd_req_op_data(osd_req, which, extent, osd_data);
  124. }
  125. EXPORT_SYMBOL(osd_req_op_extent_osd_data);
  126. struct ceph_osd_data *
  127. osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
  128. unsigned int which)
  129. {
  130. return osd_req_op_data(osd_req, which, cls, response_data);
  131. }
  132. EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
  133. void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
  134. unsigned int which, struct page **pages,
  135. u64 length, u32 alignment,
  136. bool pages_from_pool, bool own_pages)
  137. {
  138. struct ceph_osd_data *osd_data;
  139. osd_data = osd_req_op_raw_data_in(osd_req, which);
  140. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  141. pages_from_pool, own_pages);
  142. }
  143. EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
  144. void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
  145. unsigned int which, struct page **pages,
  146. u64 length, u32 alignment,
  147. bool pages_from_pool, bool own_pages)
  148. {
  149. struct ceph_osd_data *osd_data;
  150. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  151. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  152. pages_from_pool, own_pages);
  153. }
  154. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
  155. void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
  156. unsigned int which, struct ceph_pagelist *pagelist)
  157. {
  158. struct ceph_osd_data *osd_data;
  159. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  160. ceph_osd_data_pagelist_init(osd_data, pagelist);
  161. }
  162. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
  163. #ifdef CONFIG_BLOCK
  164. void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
  165. unsigned int which, struct bio *bio, size_t bio_length)
  166. {
  167. struct ceph_osd_data *osd_data;
  168. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  169. ceph_osd_data_bio_init(osd_data, bio, bio_length);
  170. }
  171. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
  172. #endif /* CONFIG_BLOCK */
  173. static void osd_req_op_cls_request_info_pagelist(
  174. struct ceph_osd_request *osd_req,
  175. unsigned int which, struct ceph_pagelist *pagelist)
  176. {
  177. struct ceph_osd_data *osd_data;
  178. osd_data = osd_req_op_data(osd_req, which, cls, request_info);
  179. ceph_osd_data_pagelist_init(osd_data, pagelist);
  180. }
  181. void osd_req_op_cls_request_data_pagelist(
  182. struct ceph_osd_request *osd_req,
  183. unsigned int which, struct ceph_pagelist *pagelist)
  184. {
  185. struct ceph_osd_data *osd_data;
  186. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  187. ceph_osd_data_pagelist_init(osd_data, pagelist);
  188. }
  189. EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
  190. void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
  191. unsigned int which, struct page **pages, u64 length,
  192. u32 alignment, bool pages_from_pool, bool own_pages)
  193. {
  194. struct ceph_osd_data *osd_data;
  195. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  196. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  197. pages_from_pool, own_pages);
  198. }
  199. EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
  200. void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
  201. unsigned int which, struct page **pages, u64 length,
  202. u32 alignment, bool pages_from_pool, bool own_pages)
  203. {
  204. struct ceph_osd_data *osd_data;
  205. osd_data = osd_req_op_data(osd_req, which, cls, response_data);
  206. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  207. pages_from_pool, own_pages);
  208. }
  209. EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
  210. static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
  211. {
  212. switch (osd_data->type) {
  213. case CEPH_OSD_DATA_TYPE_NONE:
  214. return 0;
  215. case CEPH_OSD_DATA_TYPE_PAGES:
  216. return osd_data->length;
  217. case CEPH_OSD_DATA_TYPE_PAGELIST:
  218. return (u64)osd_data->pagelist->length;
  219. #ifdef CONFIG_BLOCK
  220. case CEPH_OSD_DATA_TYPE_BIO:
  221. return (u64)osd_data->bio_length;
  222. #endif /* CONFIG_BLOCK */
  223. default:
  224. WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
  225. return 0;
  226. }
  227. }
  228. static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
  229. {
  230. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
  231. int num_pages;
  232. num_pages = calc_pages_for((u64)osd_data->alignment,
  233. (u64)osd_data->length);
  234. ceph_release_page_vector(osd_data->pages, num_pages);
  235. }
  236. ceph_osd_data_init(osd_data);
  237. }
  238. static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
  239. unsigned int which)
  240. {
  241. struct ceph_osd_req_op *op;
  242. BUG_ON(which >= osd_req->r_num_ops);
  243. op = &osd_req->r_ops[which];
  244. switch (op->op) {
  245. case CEPH_OSD_OP_READ:
  246. case CEPH_OSD_OP_WRITE:
  247. case CEPH_OSD_OP_WRITEFULL:
  248. ceph_osd_data_release(&op->extent.osd_data);
  249. break;
  250. case CEPH_OSD_OP_CALL:
  251. ceph_osd_data_release(&op->cls.request_info);
  252. ceph_osd_data_release(&op->cls.request_data);
  253. ceph_osd_data_release(&op->cls.response_data);
  254. break;
  255. case CEPH_OSD_OP_SETXATTR:
  256. case CEPH_OSD_OP_CMPXATTR:
  257. ceph_osd_data_release(&op->xattr.osd_data);
  258. break;
  259. case CEPH_OSD_OP_STAT:
  260. ceph_osd_data_release(&op->raw_data_in);
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. /*
  267. * requests
  268. */
  269. static void ceph_osdc_release_request(struct kref *kref)
  270. {
  271. struct ceph_osd_request *req = container_of(kref,
  272. struct ceph_osd_request, r_kref);
  273. unsigned int which;
  274. dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
  275. req->r_request, req->r_reply);
  276. WARN_ON(!RB_EMPTY_NODE(&req->r_node));
  277. WARN_ON(!list_empty(&req->r_req_lru_item));
  278. WARN_ON(!list_empty(&req->r_osd_item));
  279. WARN_ON(!list_empty(&req->r_linger_item));
  280. WARN_ON(!list_empty(&req->r_linger_osd_item));
  281. WARN_ON(req->r_osd);
  282. if (req->r_request)
  283. ceph_msg_put(req->r_request);
  284. if (req->r_reply) {
  285. ceph_msg_revoke_incoming(req->r_reply);
  286. ceph_msg_put(req->r_reply);
  287. }
  288. for (which = 0; which < req->r_num_ops; which++)
  289. osd_req_op_data_release(req, which);
  290. ceph_put_snap_context(req->r_snapc);
  291. if (req->r_mempool)
  292. mempool_free(req, req->r_osdc->req_mempool);
  293. else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
  294. kmem_cache_free(ceph_osd_request_cache, req);
  295. else
  296. kfree(req);
  297. }
  298. void ceph_osdc_get_request(struct ceph_osd_request *req)
  299. {
  300. dout("%s %p (was %d)\n", __func__, req,
  301. atomic_read(&req->r_kref.refcount));
  302. kref_get(&req->r_kref);
  303. }
  304. EXPORT_SYMBOL(ceph_osdc_get_request);
  305. void ceph_osdc_put_request(struct ceph_osd_request *req)
  306. {
  307. dout("%s %p (was %d)\n", __func__, req,
  308. atomic_read(&req->r_kref.refcount));
  309. kref_put(&req->r_kref, ceph_osdc_release_request);
  310. }
  311. EXPORT_SYMBOL(ceph_osdc_put_request);
  312. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  313. struct ceph_snap_context *snapc,
  314. unsigned int num_ops,
  315. bool use_mempool,
  316. gfp_t gfp_flags)
  317. {
  318. struct ceph_osd_request *req;
  319. struct ceph_msg *msg;
  320. size_t msg_size;
  321. if (use_mempool) {
  322. BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
  323. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  324. } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
  325. req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
  326. } else {
  327. BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
  328. req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
  329. gfp_flags);
  330. }
  331. if (unlikely(!req))
  332. return NULL;
  333. /* req only, each op is zeroed in _osd_req_op_init() */
  334. memset(req, 0, sizeof(*req));
  335. req->r_osdc = osdc;
  336. req->r_mempool = use_mempool;
  337. req->r_num_ops = num_ops;
  338. kref_init(&req->r_kref);
  339. init_completion(&req->r_completion);
  340. init_completion(&req->r_safe_completion);
  341. RB_CLEAR_NODE(&req->r_node);
  342. INIT_LIST_HEAD(&req->r_unsafe_item);
  343. INIT_LIST_HEAD(&req->r_linger_item);
  344. INIT_LIST_HEAD(&req->r_linger_osd_item);
  345. INIT_LIST_HEAD(&req->r_req_lru_item);
  346. INIT_LIST_HEAD(&req->r_osd_item);
  347. req->r_base_oloc.pool = -1;
  348. req->r_target_oloc.pool = -1;
  349. msg_size = OSD_OPREPLY_FRONT_LEN;
  350. if (num_ops > CEPH_OSD_SLAB_OPS) {
  351. /* ceph_osd_op and rval */
  352. msg_size += (num_ops - CEPH_OSD_SLAB_OPS) *
  353. (sizeof(struct ceph_osd_op) + 4);
  354. }
  355. /* create reply message */
  356. if (use_mempool)
  357. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  358. else
  359. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size,
  360. gfp_flags, true);
  361. if (!msg) {
  362. ceph_osdc_put_request(req);
  363. return NULL;
  364. }
  365. req->r_reply = msg;
  366. msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
  367. msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
  368. msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
  369. msg_size += 1 + 8 + 4 + 4; /* pgid */
  370. msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
  371. msg_size += 2 + num_ops * sizeof(struct ceph_osd_op);
  372. msg_size += 8; /* snapid */
  373. msg_size += 8; /* snap_seq */
  374. msg_size += 4 + 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
  375. msg_size += 4; /* retry_attempt */
  376. /* create request message; allow space for oid */
  377. if (use_mempool)
  378. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  379. else
  380. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
  381. if (!msg) {
  382. ceph_osdc_put_request(req);
  383. return NULL;
  384. }
  385. memset(msg->front.iov_base, 0, msg->front.iov_len);
  386. req->r_request = msg;
  387. return req;
  388. }
  389. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  390. static bool osd_req_opcode_valid(u16 opcode)
  391. {
  392. switch (opcode) {
  393. #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
  394. __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
  395. #undef GENERATE_CASE
  396. default:
  397. return false;
  398. }
  399. }
  400. /*
  401. * This is an osd op init function for opcodes that have no data or
  402. * other information associated with them. It also serves as a
  403. * common init routine for all the other init functions, below.
  404. */
  405. static struct ceph_osd_req_op *
  406. _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
  407. u16 opcode, u32 flags)
  408. {
  409. struct ceph_osd_req_op *op;
  410. BUG_ON(which >= osd_req->r_num_ops);
  411. BUG_ON(!osd_req_opcode_valid(opcode));
  412. op = &osd_req->r_ops[which];
  413. memset(op, 0, sizeof (*op));
  414. op->op = opcode;
  415. op->flags = flags;
  416. return op;
  417. }
  418. void osd_req_op_init(struct ceph_osd_request *osd_req,
  419. unsigned int which, u16 opcode, u32 flags)
  420. {
  421. (void)_osd_req_op_init(osd_req, which, opcode, flags);
  422. }
  423. EXPORT_SYMBOL(osd_req_op_init);
  424. void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
  425. unsigned int which, u16 opcode,
  426. u64 offset, u64 length,
  427. u64 truncate_size, u32 truncate_seq)
  428. {
  429. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  430. opcode, 0);
  431. size_t payload_len = 0;
  432. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  433. opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
  434. opcode != CEPH_OSD_OP_TRUNCATE);
  435. op->extent.offset = offset;
  436. op->extent.length = length;
  437. op->extent.truncate_size = truncate_size;
  438. op->extent.truncate_seq = truncate_seq;
  439. if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
  440. payload_len += length;
  441. op->indata_len = payload_len;
  442. }
  443. EXPORT_SYMBOL(osd_req_op_extent_init);
  444. void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
  445. unsigned int which, u64 length)
  446. {
  447. struct ceph_osd_req_op *op;
  448. u64 previous;
  449. BUG_ON(which >= osd_req->r_num_ops);
  450. op = &osd_req->r_ops[which];
  451. previous = op->extent.length;
  452. if (length == previous)
  453. return; /* Nothing to do */
  454. BUG_ON(length > previous);
  455. op->extent.length = length;
  456. op->indata_len -= previous - length;
  457. }
  458. EXPORT_SYMBOL(osd_req_op_extent_update);
  459. void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
  460. unsigned int which, u64 offset_inc)
  461. {
  462. struct ceph_osd_req_op *op, *prev_op;
  463. BUG_ON(which + 1 >= osd_req->r_num_ops);
  464. prev_op = &osd_req->r_ops[which];
  465. op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
  466. /* dup previous one */
  467. op->indata_len = prev_op->indata_len;
  468. op->outdata_len = prev_op->outdata_len;
  469. op->extent = prev_op->extent;
  470. /* adjust offset */
  471. op->extent.offset += offset_inc;
  472. op->extent.length -= offset_inc;
  473. if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
  474. op->indata_len -= offset_inc;
  475. }
  476. EXPORT_SYMBOL(osd_req_op_extent_dup_last);
  477. void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
  478. u16 opcode, const char *class, const char *method)
  479. {
  480. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  481. opcode, 0);
  482. struct ceph_pagelist *pagelist;
  483. size_t payload_len = 0;
  484. size_t size;
  485. BUG_ON(opcode != CEPH_OSD_OP_CALL);
  486. pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
  487. BUG_ON(!pagelist);
  488. ceph_pagelist_init(pagelist);
  489. op->cls.class_name = class;
  490. size = strlen(class);
  491. BUG_ON(size > (size_t) U8_MAX);
  492. op->cls.class_len = size;
  493. ceph_pagelist_append(pagelist, class, size);
  494. payload_len += size;
  495. op->cls.method_name = method;
  496. size = strlen(method);
  497. BUG_ON(size > (size_t) U8_MAX);
  498. op->cls.method_len = size;
  499. ceph_pagelist_append(pagelist, method, size);
  500. payload_len += size;
  501. osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
  502. op->cls.argc = 0; /* currently unused */
  503. op->indata_len = payload_len;
  504. }
  505. EXPORT_SYMBOL(osd_req_op_cls_init);
  506. int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
  507. u16 opcode, const char *name, const void *value,
  508. size_t size, u8 cmp_op, u8 cmp_mode)
  509. {
  510. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  511. opcode, 0);
  512. struct ceph_pagelist *pagelist;
  513. size_t payload_len;
  514. BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
  515. pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
  516. if (!pagelist)
  517. return -ENOMEM;
  518. ceph_pagelist_init(pagelist);
  519. payload_len = strlen(name);
  520. op->xattr.name_len = payload_len;
  521. ceph_pagelist_append(pagelist, name, payload_len);
  522. op->xattr.value_len = size;
  523. ceph_pagelist_append(pagelist, value, size);
  524. payload_len += size;
  525. op->xattr.cmp_op = cmp_op;
  526. op->xattr.cmp_mode = cmp_mode;
  527. ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
  528. op->indata_len = payload_len;
  529. return 0;
  530. }
  531. EXPORT_SYMBOL(osd_req_op_xattr_init);
  532. void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
  533. unsigned int which, u16 opcode,
  534. u64 cookie, u64 version, int flag)
  535. {
  536. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  537. opcode, 0);
  538. BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
  539. op->watch.cookie = cookie;
  540. op->watch.ver = version;
  541. if (opcode == CEPH_OSD_OP_WATCH && flag)
  542. op->watch.flag = (u8)1;
  543. }
  544. EXPORT_SYMBOL(osd_req_op_watch_init);
  545. void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
  546. unsigned int which,
  547. u64 expected_object_size,
  548. u64 expected_write_size)
  549. {
  550. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
  551. CEPH_OSD_OP_SETALLOCHINT,
  552. 0);
  553. op->alloc_hint.expected_object_size = expected_object_size;
  554. op->alloc_hint.expected_write_size = expected_write_size;
  555. /*
  556. * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
  557. * not worth a feature bit. Set FAILOK per-op flag to make
  558. * sure older osds don't trip over an unsupported opcode.
  559. */
  560. op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
  561. }
  562. EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
  563. static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
  564. struct ceph_osd_data *osd_data)
  565. {
  566. u64 length = ceph_osd_data_length(osd_data);
  567. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  568. BUG_ON(length > (u64) SIZE_MAX);
  569. if (length)
  570. ceph_msg_data_add_pages(msg, osd_data->pages,
  571. length, osd_data->alignment);
  572. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  573. BUG_ON(!length);
  574. ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
  575. #ifdef CONFIG_BLOCK
  576. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
  577. ceph_msg_data_add_bio(msg, osd_data->bio, length);
  578. #endif
  579. } else {
  580. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
  581. }
  582. }
  583. static u64 osd_req_encode_op(struct ceph_osd_request *req,
  584. struct ceph_osd_op *dst, unsigned int which)
  585. {
  586. struct ceph_osd_req_op *src;
  587. struct ceph_osd_data *osd_data;
  588. u64 request_data_len = 0;
  589. u64 data_length;
  590. BUG_ON(which >= req->r_num_ops);
  591. src = &req->r_ops[which];
  592. if (WARN_ON(!osd_req_opcode_valid(src->op))) {
  593. pr_err("unrecognized osd opcode %d\n", src->op);
  594. return 0;
  595. }
  596. switch (src->op) {
  597. case CEPH_OSD_OP_STAT:
  598. osd_data = &src->raw_data_in;
  599. ceph_osdc_msg_data_add(req->r_reply, osd_data);
  600. break;
  601. case CEPH_OSD_OP_READ:
  602. case CEPH_OSD_OP_WRITE:
  603. case CEPH_OSD_OP_WRITEFULL:
  604. case CEPH_OSD_OP_ZERO:
  605. case CEPH_OSD_OP_TRUNCATE:
  606. if (src->op == CEPH_OSD_OP_WRITE ||
  607. src->op == CEPH_OSD_OP_WRITEFULL)
  608. request_data_len = src->extent.length;
  609. dst->extent.offset = cpu_to_le64(src->extent.offset);
  610. dst->extent.length = cpu_to_le64(src->extent.length);
  611. dst->extent.truncate_size =
  612. cpu_to_le64(src->extent.truncate_size);
  613. dst->extent.truncate_seq =
  614. cpu_to_le32(src->extent.truncate_seq);
  615. osd_data = &src->extent.osd_data;
  616. if (src->op == CEPH_OSD_OP_WRITE ||
  617. src->op == CEPH_OSD_OP_WRITEFULL)
  618. ceph_osdc_msg_data_add(req->r_request, osd_data);
  619. else
  620. ceph_osdc_msg_data_add(req->r_reply, osd_data);
  621. break;
  622. case CEPH_OSD_OP_CALL:
  623. dst->cls.class_len = src->cls.class_len;
  624. dst->cls.method_len = src->cls.method_len;
  625. osd_data = &src->cls.request_info;
  626. ceph_osdc_msg_data_add(req->r_request, osd_data);
  627. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
  628. request_data_len = osd_data->pagelist->length;
  629. osd_data = &src->cls.request_data;
  630. data_length = ceph_osd_data_length(osd_data);
  631. if (data_length) {
  632. BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
  633. dst->cls.indata_len = cpu_to_le32(data_length);
  634. ceph_osdc_msg_data_add(req->r_request, osd_data);
  635. src->indata_len += data_length;
  636. request_data_len += data_length;
  637. }
  638. osd_data = &src->cls.response_data;
  639. ceph_osdc_msg_data_add(req->r_reply, osd_data);
  640. break;
  641. case CEPH_OSD_OP_STARTSYNC:
  642. break;
  643. case CEPH_OSD_OP_NOTIFY_ACK:
  644. case CEPH_OSD_OP_WATCH:
  645. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  646. dst->watch.ver = cpu_to_le64(src->watch.ver);
  647. dst->watch.flag = src->watch.flag;
  648. break;
  649. case CEPH_OSD_OP_SETALLOCHINT:
  650. dst->alloc_hint.expected_object_size =
  651. cpu_to_le64(src->alloc_hint.expected_object_size);
  652. dst->alloc_hint.expected_write_size =
  653. cpu_to_le64(src->alloc_hint.expected_write_size);
  654. break;
  655. case CEPH_OSD_OP_SETXATTR:
  656. case CEPH_OSD_OP_CMPXATTR:
  657. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  658. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  659. dst->xattr.cmp_op = src->xattr.cmp_op;
  660. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  661. osd_data = &src->xattr.osd_data;
  662. ceph_osdc_msg_data_add(req->r_request, osd_data);
  663. request_data_len = osd_data->pagelist->length;
  664. break;
  665. case CEPH_OSD_OP_CREATE:
  666. case CEPH_OSD_OP_DELETE:
  667. break;
  668. default:
  669. pr_err("unsupported osd opcode %s\n",
  670. ceph_osd_op_name(src->op));
  671. WARN_ON(1);
  672. return 0;
  673. }
  674. dst->op = cpu_to_le16(src->op);
  675. dst->flags = cpu_to_le32(src->flags);
  676. dst->payload_len = cpu_to_le32(src->indata_len);
  677. return request_data_len;
  678. }
  679. /*
  680. * build new request AND message, calculate layout, and adjust file
  681. * extent as needed.
  682. *
  683. * if the file was recently truncated, we include information about its
  684. * old and new size so that the object can be updated appropriately. (we
  685. * avoid synchronously deleting truncated objects because it's slow.)
  686. *
  687. * if @do_sync, include a 'startsync' command so that the osd will flush
  688. * data quickly.
  689. */
  690. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  691. struct ceph_file_layout *layout,
  692. struct ceph_vino vino,
  693. u64 off, u64 *plen,
  694. unsigned int which, int num_ops,
  695. int opcode, int flags,
  696. struct ceph_snap_context *snapc,
  697. u32 truncate_seq,
  698. u64 truncate_size,
  699. bool use_mempool)
  700. {
  701. struct ceph_osd_request *req;
  702. u64 objnum = 0;
  703. u64 objoff = 0;
  704. u64 objlen = 0;
  705. int r;
  706. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  707. opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
  708. opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
  709. req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
  710. GFP_NOFS);
  711. if (!req)
  712. return ERR_PTR(-ENOMEM);
  713. req->r_flags = flags;
  714. /* calculate max write size */
  715. r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
  716. if (r < 0) {
  717. ceph_osdc_put_request(req);
  718. return ERR_PTR(r);
  719. }
  720. if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
  721. osd_req_op_init(req, which, opcode, 0);
  722. } else {
  723. u32 object_size = le32_to_cpu(layout->fl_object_size);
  724. u32 object_base = off - objoff;
  725. if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
  726. if (truncate_size <= object_base) {
  727. truncate_size = 0;
  728. } else {
  729. truncate_size -= object_base;
  730. if (truncate_size > object_size)
  731. truncate_size = object_size;
  732. }
  733. }
  734. osd_req_op_extent_init(req, which, opcode, objoff, objlen,
  735. truncate_size, truncate_seq);
  736. }
  737. req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
  738. snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
  739. "%llx.%08llx", vino.ino, objnum);
  740. req->r_base_oid.name_len = strlen(req->r_base_oid.name);
  741. return req;
  742. }
  743. EXPORT_SYMBOL(ceph_osdc_new_request);
  744. /*
  745. * We keep osd requests in an rbtree, sorted by ->r_tid.
  746. */
  747. static void __insert_request(struct ceph_osd_client *osdc,
  748. struct ceph_osd_request *new)
  749. {
  750. struct rb_node **p = &osdc->requests.rb_node;
  751. struct rb_node *parent = NULL;
  752. struct ceph_osd_request *req = NULL;
  753. while (*p) {
  754. parent = *p;
  755. req = rb_entry(parent, struct ceph_osd_request, r_node);
  756. if (new->r_tid < req->r_tid)
  757. p = &(*p)->rb_left;
  758. else if (new->r_tid > req->r_tid)
  759. p = &(*p)->rb_right;
  760. else
  761. BUG();
  762. }
  763. rb_link_node(&new->r_node, parent, p);
  764. rb_insert_color(&new->r_node, &osdc->requests);
  765. }
  766. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  767. u64 tid)
  768. {
  769. struct ceph_osd_request *req;
  770. struct rb_node *n = osdc->requests.rb_node;
  771. while (n) {
  772. req = rb_entry(n, struct ceph_osd_request, r_node);
  773. if (tid < req->r_tid)
  774. n = n->rb_left;
  775. else if (tid > req->r_tid)
  776. n = n->rb_right;
  777. else
  778. return req;
  779. }
  780. return NULL;
  781. }
  782. static struct ceph_osd_request *
  783. __lookup_request_ge(struct ceph_osd_client *osdc,
  784. u64 tid)
  785. {
  786. struct ceph_osd_request *req;
  787. struct rb_node *n = osdc->requests.rb_node;
  788. while (n) {
  789. req = rb_entry(n, struct ceph_osd_request, r_node);
  790. if (tid < req->r_tid) {
  791. if (!n->rb_left)
  792. return req;
  793. n = n->rb_left;
  794. } else if (tid > req->r_tid) {
  795. n = n->rb_right;
  796. } else {
  797. return req;
  798. }
  799. }
  800. return NULL;
  801. }
  802. static void __kick_linger_request(struct ceph_osd_request *req)
  803. {
  804. struct ceph_osd_client *osdc = req->r_osdc;
  805. struct ceph_osd *osd = req->r_osd;
  806. /*
  807. * Linger requests need to be resent with a new tid to avoid
  808. * the dup op detection logic on the OSDs. Achieve this with
  809. * a re-register dance instead of open-coding.
  810. */
  811. ceph_osdc_get_request(req);
  812. if (!list_empty(&req->r_linger_item))
  813. __unregister_linger_request(osdc, req);
  814. else
  815. __unregister_request(osdc, req);
  816. __register_request(osdc, req);
  817. ceph_osdc_put_request(req);
  818. /*
  819. * Unless request has been registered as both normal and
  820. * lingering, __unregister{,_linger}_request clears r_osd.
  821. * However, here we need to preserve r_osd to make sure we
  822. * requeue on the same OSD.
  823. */
  824. WARN_ON(req->r_osd || !osd);
  825. req->r_osd = osd;
  826. dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
  827. __enqueue_request(req);
  828. }
  829. /*
  830. * Resubmit requests pending on the given osd.
  831. */
  832. static void __kick_osd_requests(struct ceph_osd_client *osdc,
  833. struct ceph_osd *osd)
  834. {
  835. struct ceph_osd_request *req, *nreq;
  836. LIST_HEAD(resend);
  837. LIST_HEAD(resend_linger);
  838. int err;
  839. dout("%s osd%d\n", __func__, osd->o_osd);
  840. err = __reset_osd(osdc, osd);
  841. if (err)
  842. return;
  843. /*
  844. * Build up a list of requests to resend by traversing the
  845. * osd's list of requests. Requests for a given object are
  846. * sent in tid order, and that is also the order they're
  847. * kept on this list. Therefore all requests that are in
  848. * flight will be found first, followed by all requests that
  849. * have not yet been sent. And to resend requests while
  850. * preserving this order we will want to put any sent
  851. * requests back on the front of the osd client's unsent
  852. * list.
  853. *
  854. * So we build a separate ordered list of already-sent
  855. * requests for the affected osd and splice it onto the
  856. * front of the osd client's unsent list. Once we've seen a
  857. * request that has not yet been sent we're done. Those
  858. * requests are already sitting right where they belong.
  859. */
  860. list_for_each_entry(req, &osd->o_requests, r_osd_item) {
  861. if (!req->r_sent)
  862. break;
  863. if (!req->r_linger) {
  864. dout("%s requeueing %p tid %llu\n", __func__, req,
  865. req->r_tid);
  866. list_move_tail(&req->r_req_lru_item, &resend);
  867. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  868. } else {
  869. list_move_tail(&req->r_req_lru_item, &resend_linger);
  870. }
  871. }
  872. list_splice(&resend, &osdc->req_unsent);
  873. /*
  874. * Both registered and not yet registered linger requests are
  875. * enqueued with a new tid on the same OSD. We add/move them
  876. * to req_unsent/o_requests at the end to keep things in tid
  877. * order.
  878. */
  879. list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
  880. r_linger_osd_item) {
  881. WARN_ON(!list_empty(&req->r_req_lru_item));
  882. __kick_linger_request(req);
  883. }
  884. list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
  885. __kick_linger_request(req);
  886. }
  887. /*
  888. * If the osd connection drops, we need to resubmit all requests.
  889. */
  890. static void osd_reset(struct ceph_connection *con)
  891. {
  892. struct ceph_osd *osd = con->private;
  893. struct ceph_osd_client *osdc;
  894. if (!osd)
  895. return;
  896. dout("osd_reset osd%d\n", osd->o_osd);
  897. osdc = osd->o_osdc;
  898. down_read(&osdc->map_sem);
  899. mutex_lock(&osdc->request_mutex);
  900. __kick_osd_requests(osdc, osd);
  901. __send_queued(osdc);
  902. mutex_unlock(&osdc->request_mutex);
  903. up_read(&osdc->map_sem);
  904. }
  905. /*
  906. * Track open sessions with osds.
  907. */
  908. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  909. {
  910. struct ceph_osd *osd;
  911. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  912. if (!osd)
  913. return NULL;
  914. atomic_set(&osd->o_ref, 1);
  915. osd->o_osdc = osdc;
  916. osd->o_osd = onum;
  917. RB_CLEAR_NODE(&osd->o_node);
  918. INIT_LIST_HEAD(&osd->o_requests);
  919. INIT_LIST_HEAD(&osd->o_linger_requests);
  920. INIT_LIST_HEAD(&osd->o_osd_lru);
  921. osd->o_incarnation = 1;
  922. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  923. INIT_LIST_HEAD(&osd->o_keepalive_item);
  924. return osd;
  925. }
  926. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  927. {
  928. if (atomic_inc_not_zero(&osd->o_ref)) {
  929. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  930. atomic_read(&osd->o_ref));
  931. return osd;
  932. } else {
  933. dout("get_osd %p FAIL\n", osd);
  934. return NULL;
  935. }
  936. }
  937. static void put_osd(struct ceph_osd *osd)
  938. {
  939. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  940. atomic_read(&osd->o_ref) - 1);
  941. if (atomic_dec_and_test(&osd->o_ref)) {
  942. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  943. if (osd->o_auth.authorizer)
  944. ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
  945. kfree(osd);
  946. }
  947. }
  948. /*
  949. * remove an osd from our map
  950. */
  951. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  952. {
  953. dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
  954. WARN_ON(!list_empty(&osd->o_requests));
  955. WARN_ON(!list_empty(&osd->o_linger_requests));
  956. list_del_init(&osd->o_osd_lru);
  957. rb_erase(&osd->o_node, &osdc->osds);
  958. RB_CLEAR_NODE(&osd->o_node);
  959. }
  960. static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  961. {
  962. dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
  963. if (!RB_EMPTY_NODE(&osd->o_node)) {
  964. ceph_con_close(&osd->o_con);
  965. __remove_osd(osdc, osd);
  966. put_osd(osd);
  967. }
  968. }
  969. static void remove_all_osds(struct ceph_osd_client *osdc)
  970. {
  971. dout("%s %p\n", __func__, osdc);
  972. mutex_lock(&osdc->request_mutex);
  973. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  974. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  975. struct ceph_osd, o_node);
  976. remove_osd(osdc, osd);
  977. }
  978. mutex_unlock(&osdc->request_mutex);
  979. }
  980. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  981. struct ceph_osd *osd)
  982. {
  983. dout("%s %p\n", __func__, osd);
  984. BUG_ON(!list_empty(&osd->o_osd_lru));
  985. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  986. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
  987. }
  988. static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
  989. struct ceph_osd *osd)
  990. {
  991. dout("%s %p\n", __func__, osd);
  992. if (list_empty(&osd->o_requests) &&
  993. list_empty(&osd->o_linger_requests))
  994. __move_osd_to_lru(osdc, osd);
  995. }
  996. static void __remove_osd_from_lru(struct ceph_osd *osd)
  997. {
  998. dout("__remove_osd_from_lru %p\n", osd);
  999. if (!list_empty(&osd->o_osd_lru))
  1000. list_del_init(&osd->o_osd_lru);
  1001. }
  1002. static void remove_old_osds(struct ceph_osd_client *osdc)
  1003. {
  1004. struct ceph_osd *osd, *nosd;
  1005. dout("__remove_old_osds %p\n", osdc);
  1006. mutex_lock(&osdc->request_mutex);
  1007. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  1008. if (time_before(jiffies, osd->lru_ttl))
  1009. break;
  1010. remove_osd(osdc, osd);
  1011. }
  1012. mutex_unlock(&osdc->request_mutex);
  1013. }
  1014. /*
  1015. * reset osd connect
  1016. */
  1017. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  1018. {
  1019. struct ceph_entity_addr *peer_addr;
  1020. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  1021. if (list_empty(&osd->o_requests) &&
  1022. list_empty(&osd->o_linger_requests)) {
  1023. remove_osd(osdc, osd);
  1024. return -ENODEV;
  1025. }
  1026. peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
  1027. if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
  1028. !ceph_con_opened(&osd->o_con)) {
  1029. struct ceph_osd_request *req;
  1030. dout("osd addr hasn't changed and connection never opened, "
  1031. "letting msgr retry\n");
  1032. /* touch each r_stamp for handle_timeout()'s benfit */
  1033. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  1034. req->r_stamp = jiffies;
  1035. return -EAGAIN;
  1036. }
  1037. ceph_con_close(&osd->o_con);
  1038. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
  1039. osd->o_incarnation++;
  1040. return 0;
  1041. }
  1042. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  1043. {
  1044. struct rb_node **p = &osdc->osds.rb_node;
  1045. struct rb_node *parent = NULL;
  1046. struct ceph_osd *osd = NULL;
  1047. dout("__insert_osd %p osd%d\n", new, new->o_osd);
  1048. while (*p) {
  1049. parent = *p;
  1050. osd = rb_entry(parent, struct ceph_osd, o_node);
  1051. if (new->o_osd < osd->o_osd)
  1052. p = &(*p)->rb_left;
  1053. else if (new->o_osd > osd->o_osd)
  1054. p = &(*p)->rb_right;
  1055. else
  1056. BUG();
  1057. }
  1058. rb_link_node(&new->o_node, parent, p);
  1059. rb_insert_color(&new->o_node, &osdc->osds);
  1060. }
  1061. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  1062. {
  1063. struct ceph_osd *osd;
  1064. struct rb_node *n = osdc->osds.rb_node;
  1065. while (n) {
  1066. osd = rb_entry(n, struct ceph_osd, o_node);
  1067. if (o < osd->o_osd)
  1068. n = n->rb_left;
  1069. else if (o > osd->o_osd)
  1070. n = n->rb_right;
  1071. else
  1072. return osd;
  1073. }
  1074. return NULL;
  1075. }
  1076. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  1077. {
  1078. schedule_delayed_work(&osdc->timeout_work,
  1079. osdc->client->options->osd_keepalive_timeout);
  1080. }
  1081. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  1082. {
  1083. cancel_delayed_work(&osdc->timeout_work);
  1084. }
  1085. /*
  1086. * Register request, assign tid. If this is the first request, set up
  1087. * the timeout event.
  1088. */
  1089. static void __register_request(struct ceph_osd_client *osdc,
  1090. struct ceph_osd_request *req)
  1091. {
  1092. req->r_tid = ++osdc->last_tid;
  1093. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  1094. dout("__register_request %p tid %lld\n", req, req->r_tid);
  1095. __insert_request(osdc, req);
  1096. ceph_osdc_get_request(req);
  1097. osdc->num_requests++;
  1098. if (osdc->num_requests == 1) {
  1099. dout(" first request, scheduling timeout\n");
  1100. __schedule_osd_timeout(osdc);
  1101. }
  1102. }
  1103. /*
  1104. * called under osdc->request_mutex
  1105. */
  1106. static void __unregister_request(struct ceph_osd_client *osdc,
  1107. struct ceph_osd_request *req)
  1108. {
  1109. if (RB_EMPTY_NODE(&req->r_node)) {
  1110. dout("__unregister_request %p tid %lld not registered\n",
  1111. req, req->r_tid);
  1112. return;
  1113. }
  1114. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  1115. rb_erase(&req->r_node, &osdc->requests);
  1116. RB_CLEAR_NODE(&req->r_node);
  1117. osdc->num_requests--;
  1118. if (req->r_osd) {
  1119. /* make sure the original request isn't in flight. */
  1120. ceph_msg_revoke(req->r_request);
  1121. list_del_init(&req->r_osd_item);
  1122. maybe_move_osd_to_lru(osdc, req->r_osd);
  1123. if (list_empty(&req->r_linger_osd_item))
  1124. req->r_osd = NULL;
  1125. }
  1126. list_del_init(&req->r_req_lru_item);
  1127. ceph_osdc_put_request(req);
  1128. if (osdc->num_requests == 0) {
  1129. dout(" no requests, canceling timeout\n");
  1130. __cancel_osd_timeout(osdc);
  1131. }
  1132. }
  1133. /*
  1134. * Cancel a previously queued request message
  1135. */
  1136. static void __cancel_request(struct ceph_osd_request *req)
  1137. {
  1138. if (req->r_sent && req->r_osd) {
  1139. ceph_msg_revoke(req->r_request);
  1140. req->r_sent = 0;
  1141. }
  1142. }
  1143. static void __register_linger_request(struct ceph_osd_client *osdc,
  1144. struct ceph_osd_request *req)
  1145. {
  1146. dout("%s %p tid %llu\n", __func__, req, req->r_tid);
  1147. WARN_ON(!req->r_linger);
  1148. ceph_osdc_get_request(req);
  1149. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  1150. if (req->r_osd)
  1151. list_add_tail(&req->r_linger_osd_item,
  1152. &req->r_osd->o_linger_requests);
  1153. }
  1154. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  1155. struct ceph_osd_request *req)
  1156. {
  1157. WARN_ON(!req->r_linger);
  1158. if (list_empty(&req->r_linger_item)) {
  1159. dout("%s %p tid %llu not registered\n", __func__, req,
  1160. req->r_tid);
  1161. return;
  1162. }
  1163. dout("%s %p tid %llu\n", __func__, req, req->r_tid);
  1164. list_del_init(&req->r_linger_item);
  1165. if (req->r_osd) {
  1166. list_del_init(&req->r_linger_osd_item);
  1167. maybe_move_osd_to_lru(osdc, req->r_osd);
  1168. if (list_empty(&req->r_osd_item))
  1169. req->r_osd = NULL;
  1170. }
  1171. ceph_osdc_put_request(req);
  1172. }
  1173. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  1174. struct ceph_osd_request *req)
  1175. {
  1176. if (!req->r_linger) {
  1177. dout("set_request_linger %p\n", req);
  1178. req->r_linger = 1;
  1179. }
  1180. }
  1181. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  1182. /*
  1183. * Returns whether a request should be blocked from being sent
  1184. * based on the current osdmap and osd_client settings.
  1185. *
  1186. * Caller should hold map_sem for read.
  1187. */
  1188. static bool __req_should_be_paused(struct ceph_osd_client *osdc,
  1189. struct ceph_osd_request *req)
  1190. {
  1191. bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
  1192. bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
  1193. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
  1194. return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
  1195. (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
  1196. }
  1197. /*
  1198. * Calculate mapping of a request to a PG. Takes tiering into account.
  1199. */
  1200. static int __calc_request_pg(struct ceph_osdmap *osdmap,
  1201. struct ceph_osd_request *req,
  1202. struct ceph_pg *pg_out)
  1203. {
  1204. bool need_check_tiering;
  1205. need_check_tiering = false;
  1206. if (req->r_target_oloc.pool == -1) {
  1207. req->r_target_oloc = req->r_base_oloc; /* struct */
  1208. need_check_tiering = true;
  1209. }
  1210. if (req->r_target_oid.name_len == 0) {
  1211. ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
  1212. need_check_tiering = true;
  1213. }
  1214. if (need_check_tiering &&
  1215. (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
  1216. struct ceph_pg_pool_info *pi;
  1217. pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
  1218. if (pi) {
  1219. if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
  1220. pi->read_tier >= 0)
  1221. req->r_target_oloc.pool = pi->read_tier;
  1222. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  1223. pi->write_tier >= 0)
  1224. req->r_target_oloc.pool = pi->write_tier;
  1225. }
  1226. /* !pi is caught in ceph_oloc_oid_to_pg() */
  1227. }
  1228. return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
  1229. &req->r_target_oid, pg_out);
  1230. }
  1231. static void __enqueue_request(struct ceph_osd_request *req)
  1232. {
  1233. struct ceph_osd_client *osdc = req->r_osdc;
  1234. dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
  1235. req->r_osd ? req->r_osd->o_osd : -1);
  1236. if (req->r_osd) {
  1237. __remove_osd_from_lru(req->r_osd);
  1238. list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
  1239. list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
  1240. } else {
  1241. list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
  1242. }
  1243. }
  1244. /*
  1245. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  1246. * (as needed), and set the request r_osd appropriately. If there is
  1247. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  1248. * (unsent, homeless) or leave on in-flight lru.
  1249. *
  1250. * Return 0 if unchanged, 1 if changed, or negative on error.
  1251. *
  1252. * Caller should hold map_sem for read and request_mutex.
  1253. */
  1254. static int __map_request(struct ceph_osd_client *osdc,
  1255. struct ceph_osd_request *req, int force_resend)
  1256. {
  1257. struct ceph_pg pgid;
  1258. int acting[CEPH_PG_MAX_SIZE];
  1259. int num, o;
  1260. int err;
  1261. bool was_paused;
  1262. dout("map_request %p tid %lld\n", req, req->r_tid);
  1263. err = __calc_request_pg(osdc->osdmap, req, &pgid);
  1264. if (err) {
  1265. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  1266. return err;
  1267. }
  1268. req->r_pgid = pgid;
  1269. num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
  1270. if (num < 0)
  1271. num = 0;
  1272. was_paused = req->r_paused;
  1273. req->r_paused = __req_should_be_paused(osdc, req);
  1274. if (was_paused && !req->r_paused)
  1275. force_resend = 1;
  1276. if ((!force_resend &&
  1277. req->r_osd && req->r_osd->o_osd == o &&
  1278. req->r_sent >= req->r_osd->o_incarnation &&
  1279. req->r_num_pg_osds == num &&
  1280. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  1281. (req->r_osd == NULL && o == -1) ||
  1282. req->r_paused)
  1283. return 0; /* no change */
  1284. dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
  1285. req->r_tid, pgid.pool, pgid.seed, o,
  1286. req->r_osd ? req->r_osd->o_osd : -1);
  1287. /* record full pg acting set */
  1288. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  1289. req->r_num_pg_osds = num;
  1290. if (req->r_osd) {
  1291. __cancel_request(req);
  1292. list_del_init(&req->r_osd_item);
  1293. list_del_init(&req->r_linger_osd_item);
  1294. req->r_osd = NULL;
  1295. }
  1296. req->r_osd = __lookup_osd(osdc, o);
  1297. if (!req->r_osd && o >= 0) {
  1298. err = -ENOMEM;
  1299. req->r_osd = create_osd(osdc, o);
  1300. if (!req->r_osd) {
  1301. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  1302. goto out;
  1303. }
  1304. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  1305. __insert_osd(osdc, req->r_osd);
  1306. ceph_con_open(&req->r_osd->o_con,
  1307. CEPH_ENTITY_TYPE_OSD, o,
  1308. &osdc->osdmap->osd_addr[o]);
  1309. }
  1310. __enqueue_request(req);
  1311. err = 1; /* osd or pg changed */
  1312. out:
  1313. return err;
  1314. }
  1315. /*
  1316. * caller should hold map_sem (for read) and request_mutex
  1317. */
  1318. static void __send_request(struct ceph_osd_client *osdc,
  1319. struct ceph_osd_request *req)
  1320. {
  1321. void *p;
  1322. dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
  1323. req, req->r_tid, req->r_osd->o_osd, req->r_flags,
  1324. (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
  1325. /* fill in message content that changes each time we send it */
  1326. put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
  1327. put_unaligned_le32(req->r_flags, req->r_request_flags);
  1328. put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
  1329. p = req->r_request_pgid;
  1330. ceph_encode_64(&p, req->r_pgid.pool);
  1331. ceph_encode_32(&p, req->r_pgid.seed);
  1332. put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
  1333. memcpy(req->r_request_reassert_version, &req->r_reassert_version,
  1334. sizeof(req->r_reassert_version));
  1335. req->r_stamp = jiffies;
  1336. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  1337. ceph_msg_get(req->r_request); /* send consumes a ref */
  1338. req->r_sent = req->r_osd->o_incarnation;
  1339. ceph_con_send(&req->r_osd->o_con, req->r_request);
  1340. }
  1341. /*
  1342. * Send any requests in the queue (req_unsent).
  1343. */
  1344. static void __send_queued(struct ceph_osd_client *osdc)
  1345. {
  1346. struct ceph_osd_request *req, *tmp;
  1347. dout("__send_queued\n");
  1348. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
  1349. __send_request(osdc, req);
  1350. }
  1351. /*
  1352. * Caller should hold map_sem for read and request_mutex.
  1353. */
  1354. static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1355. struct ceph_osd_request *req,
  1356. bool nofail)
  1357. {
  1358. int rc;
  1359. __register_request(osdc, req);
  1360. req->r_sent = 0;
  1361. req->r_got_reply = 0;
  1362. rc = __map_request(osdc, req, 0);
  1363. if (rc < 0) {
  1364. if (nofail) {
  1365. dout("osdc_start_request failed map, "
  1366. " will retry %lld\n", req->r_tid);
  1367. rc = 0;
  1368. } else {
  1369. __unregister_request(osdc, req);
  1370. }
  1371. return rc;
  1372. }
  1373. if (req->r_osd == NULL) {
  1374. dout("send_request %p no up osds in pg\n", req);
  1375. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1376. } else {
  1377. __send_queued(osdc);
  1378. }
  1379. return 0;
  1380. }
  1381. /*
  1382. * Timeout callback, called every N seconds when 1 or more osd
  1383. * requests has been active for more than N seconds. When this
  1384. * happens, we ping all OSDs with requests who have timed out to
  1385. * ensure any communications channel reset is detected. Reset the
  1386. * request timeouts another N seconds in the future as we go.
  1387. * Reschedule the timeout event another N seconds in future (unless
  1388. * there are no open requests).
  1389. */
  1390. static void handle_timeout(struct work_struct *work)
  1391. {
  1392. struct ceph_osd_client *osdc =
  1393. container_of(work, struct ceph_osd_client, timeout_work.work);
  1394. struct ceph_options *opts = osdc->client->options;
  1395. struct ceph_osd_request *req;
  1396. struct ceph_osd *osd;
  1397. struct list_head slow_osds;
  1398. dout("timeout\n");
  1399. down_read(&osdc->map_sem);
  1400. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1401. mutex_lock(&osdc->request_mutex);
  1402. /*
  1403. * ping osds that are a bit slow. this ensures that if there
  1404. * is a break in the TCP connection we will notice, and reopen
  1405. * a connection with that osd (from the fault callback).
  1406. */
  1407. INIT_LIST_HEAD(&slow_osds);
  1408. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  1409. if (time_before(jiffies,
  1410. req->r_stamp + opts->osd_keepalive_timeout))
  1411. break;
  1412. osd = req->r_osd;
  1413. BUG_ON(!osd);
  1414. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  1415. req->r_tid, osd->o_osd);
  1416. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  1417. }
  1418. while (!list_empty(&slow_osds)) {
  1419. osd = list_entry(slow_osds.next, struct ceph_osd,
  1420. o_keepalive_item);
  1421. list_del_init(&osd->o_keepalive_item);
  1422. ceph_con_keepalive(&osd->o_con);
  1423. }
  1424. __schedule_osd_timeout(osdc);
  1425. __send_queued(osdc);
  1426. mutex_unlock(&osdc->request_mutex);
  1427. up_read(&osdc->map_sem);
  1428. }
  1429. static void handle_osds_timeout(struct work_struct *work)
  1430. {
  1431. struct ceph_osd_client *osdc =
  1432. container_of(work, struct ceph_osd_client,
  1433. osds_timeout_work.work);
  1434. unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
  1435. dout("osds timeout\n");
  1436. down_read(&osdc->map_sem);
  1437. remove_old_osds(osdc);
  1438. up_read(&osdc->map_sem);
  1439. schedule_delayed_work(&osdc->osds_timeout_work,
  1440. round_jiffies_relative(delay));
  1441. }
  1442. static int ceph_oloc_decode(void **p, void *end,
  1443. struct ceph_object_locator *oloc)
  1444. {
  1445. u8 struct_v, struct_cv;
  1446. u32 len;
  1447. void *struct_end;
  1448. int ret = 0;
  1449. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  1450. struct_v = ceph_decode_8(p);
  1451. struct_cv = ceph_decode_8(p);
  1452. if (struct_v < 3) {
  1453. pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
  1454. struct_v, struct_cv);
  1455. goto e_inval;
  1456. }
  1457. if (struct_cv > 6) {
  1458. pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
  1459. struct_v, struct_cv);
  1460. goto e_inval;
  1461. }
  1462. len = ceph_decode_32(p);
  1463. ceph_decode_need(p, end, len, e_inval);
  1464. struct_end = *p + len;
  1465. oloc->pool = ceph_decode_64(p);
  1466. *p += 4; /* skip preferred */
  1467. len = ceph_decode_32(p);
  1468. if (len > 0) {
  1469. pr_warn("ceph_object_locator::key is set\n");
  1470. goto e_inval;
  1471. }
  1472. if (struct_v >= 5) {
  1473. len = ceph_decode_32(p);
  1474. if (len > 0) {
  1475. pr_warn("ceph_object_locator::nspace is set\n");
  1476. goto e_inval;
  1477. }
  1478. }
  1479. if (struct_v >= 6) {
  1480. s64 hash = ceph_decode_64(p);
  1481. if (hash != -1) {
  1482. pr_warn("ceph_object_locator::hash is set\n");
  1483. goto e_inval;
  1484. }
  1485. }
  1486. /* skip the rest */
  1487. *p = struct_end;
  1488. out:
  1489. return ret;
  1490. e_inval:
  1491. ret = -EINVAL;
  1492. goto out;
  1493. }
  1494. static int ceph_redirect_decode(void **p, void *end,
  1495. struct ceph_request_redirect *redir)
  1496. {
  1497. u8 struct_v, struct_cv;
  1498. u32 len;
  1499. void *struct_end;
  1500. int ret;
  1501. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  1502. struct_v = ceph_decode_8(p);
  1503. struct_cv = ceph_decode_8(p);
  1504. if (struct_cv > 1) {
  1505. pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
  1506. struct_v, struct_cv);
  1507. goto e_inval;
  1508. }
  1509. len = ceph_decode_32(p);
  1510. ceph_decode_need(p, end, len, e_inval);
  1511. struct_end = *p + len;
  1512. ret = ceph_oloc_decode(p, end, &redir->oloc);
  1513. if (ret)
  1514. goto out;
  1515. len = ceph_decode_32(p);
  1516. if (len > 0) {
  1517. pr_warn("ceph_request_redirect::object_name is set\n");
  1518. goto e_inval;
  1519. }
  1520. len = ceph_decode_32(p);
  1521. *p += len; /* skip osd_instructions */
  1522. /* skip the rest */
  1523. *p = struct_end;
  1524. out:
  1525. return ret;
  1526. e_inval:
  1527. ret = -EINVAL;
  1528. goto out;
  1529. }
  1530. static void complete_request(struct ceph_osd_request *req)
  1531. {
  1532. complete_all(&req->r_safe_completion); /* fsync waiter */
  1533. }
  1534. /*
  1535. * handle osd op reply. either call the callback if it is specified,
  1536. * or do the completion to wake up the waiting thread.
  1537. */
  1538. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1539. {
  1540. void *p, *end;
  1541. struct ceph_osd_request *req;
  1542. struct ceph_request_redirect redir;
  1543. u64 tid;
  1544. int object_len;
  1545. unsigned int numops;
  1546. int payload_len, flags;
  1547. s32 result;
  1548. s32 retry_attempt;
  1549. struct ceph_pg pg;
  1550. int err;
  1551. u32 reassert_epoch;
  1552. u64 reassert_version;
  1553. u32 osdmap_epoch;
  1554. int already_completed;
  1555. u32 bytes;
  1556. u8 decode_redir;
  1557. unsigned int i;
  1558. tid = le64_to_cpu(msg->hdr.tid);
  1559. dout("handle_reply %p tid %llu\n", msg, tid);
  1560. p = msg->front.iov_base;
  1561. end = p + msg->front.iov_len;
  1562. ceph_decode_need(&p, end, 4, bad);
  1563. object_len = ceph_decode_32(&p);
  1564. ceph_decode_need(&p, end, object_len, bad);
  1565. p += object_len;
  1566. err = ceph_decode_pgid(&p, end, &pg);
  1567. if (err)
  1568. goto bad;
  1569. ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
  1570. flags = ceph_decode_64(&p);
  1571. result = ceph_decode_32(&p);
  1572. reassert_epoch = ceph_decode_32(&p);
  1573. reassert_version = ceph_decode_64(&p);
  1574. osdmap_epoch = ceph_decode_32(&p);
  1575. /* lookup */
  1576. down_read(&osdc->map_sem);
  1577. mutex_lock(&osdc->request_mutex);
  1578. req = __lookup_request(osdc, tid);
  1579. if (req == NULL) {
  1580. dout("handle_reply tid %llu dne\n", tid);
  1581. goto bad_mutex;
  1582. }
  1583. ceph_osdc_get_request(req);
  1584. dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
  1585. req, result);
  1586. ceph_decode_need(&p, end, 4, bad_put);
  1587. numops = ceph_decode_32(&p);
  1588. if (numops > CEPH_OSD_MAX_OPS)
  1589. goto bad_put;
  1590. if (numops != req->r_num_ops)
  1591. goto bad_put;
  1592. payload_len = 0;
  1593. ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
  1594. for (i = 0; i < numops; i++) {
  1595. struct ceph_osd_op *op = p;
  1596. int len;
  1597. len = le32_to_cpu(op->payload_len);
  1598. req->r_ops[i].outdata_len = len;
  1599. dout(" op %d has %d bytes\n", i, len);
  1600. payload_len += len;
  1601. p += sizeof(*op);
  1602. }
  1603. bytes = le32_to_cpu(msg->hdr.data_len);
  1604. if (payload_len != bytes) {
  1605. pr_warn("sum of op payload lens %d != data_len %d\n",
  1606. payload_len, bytes);
  1607. goto bad_put;
  1608. }
  1609. ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
  1610. retry_attempt = ceph_decode_32(&p);
  1611. for (i = 0; i < numops; i++)
  1612. req->r_ops[i].rval = ceph_decode_32(&p);
  1613. if (le16_to_cpu(msg->hdr.version) >= 6) {
  1614. p += 8 + 4; /* skip replay_version */
  1615. p += 8; /* skip user_version */
  1616. if (le16_to_cpu(msg->hdr.version) >= 7)
  1617. ceph_decode_8_safe(&p, end, decode_redir, bad_put);
  1618. else
  1619. decode_redir = 1;
  1620. } else {
  1621. decode_redir = 0;
  1622. }
  1623. if (decode_redir) {
  1624. err = ceph_redirect_decode(&p, end, &redir);
  1625. if (err)
  1626. goto bad_put;
  1627. } else {
  1628. redir.oloc.pool = -1;
  1629. }
  1630. if (redir.oloc.pool != -1) {
  1631. dout("redirect pool %lld\n", redir.oloc.pool);
  1632. __unregister_request(osdc, req);
  1633. req->r_target_oloc = redir.oloc; /* struct */
  1634. /*
  1635. * Start redirect requests with nofail=true. If
  1636. * mapping fails, request will end up on the notarget
  1637. * list, waiting for the new osdmap (which can take
  1638. * a while), even though the original request mapped
  1639. * successfully. In the future we might want to follow
  1640. * original request's nofail setting here.
  1641. */
  1642. err = __ceph_osdc_start_request(osdc, req, true);
  1643. BUG_ON(err);
  1644. goto out_unlock;
  1645. }
  1646. already_completed = req->r_got_reply;
  1647. if (!req->r_got_reply) {
  1648. req->r_result = result;
  1649. dout("handle_reply result %d bytes %d\n", req->r_result,
  1650. bytes);
  1651. if (req->r_result == 0)
  1652. req->r_result = bytes;
  1653. /* in case this is a write and we need to replay, */
  1654. req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
  1655. req->r_reassert_version.version = cpu_to_le64(reassert_version);
  1656. req->r_got_reply = 1;
  1657. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1658. dout("handle_reply tid %llu dup ack\n", tid);
  1659. goto out_unlock;
  1660. }
  1661. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1662. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1663. __register_linger_request(osdc, req);
  1664. /* either this is a read, or we got the safe response */
  1665. if (result < 0 ||
  1666. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1667. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1668. __unregister_request(osdc, req);
  1669. mutex_unlock(&osdc->request_mutex);
  1670. up_read(&osdc->map_sem);
  1671. if (!already_completed) {
  1672. if (req->r_unsafe_callback &&
  1673. result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
  1674. req->r_unsafe_callback(req, true);
  1675. if (req->r_callback)
  1676. req->r_callback(req, msg);
  1677. else
  1678. complete_all(&req->r_completion);
  1679. }
  1680. if (flags & CEPH_OSD_FLAG_ONDISK) {
  1681. if (req->r_unsafe_callback && already_completed)
  1682. req->r_unsafe_callback(req, false);
  1683. complete_request(req);
  1684. }
  1685. out:
  1686. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1687. ceph_osdc_put_request(req);
  1688. return;
  1689. out_unlock:
  1690. mutex_unlock(&osdc->request_mutex);
  1691. up_read(&osdc->map_sem);
  1692. goto out;
  1693. bad_put:
  1694. req->r_result = -EIO;
  1695. __unregister_request(osdc, req);
  1696. if (req->r_callback)
  1697. req->r_callback(req, msg);
  1698. else
  1699. complete_all(&req->r_completion);
  1700. complete_request(req);
  1701. ceph_osdc_put_request(req);
  1702. bad_mutex:
  1703. mutex_unlock(&osdc->request_mutex);
  1704. up_read(&osdc->map_sem);
  1705. bad:
  1706. pr_err("corrupt osd_op_reply got %d %d\n",
  1707. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
  1708. ceph_msg_dump(msg);
  1709. }
  1710. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1711. {
  1712. struct rb_node *p, *n;
  1713. dout("%s %p\n", __func__, osdc);
  1714. for (p = rb_first(&osdc->osds); p; p = n) {
  1715. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1716. n = rb_next(p);
  1717. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1718. memcmp(&osd->o_con.peer_addr,
  1719. ceph_osd_addr(osdc->osdmap,
  1720. osd->o_osd),
  1721. sizeof(struct ceph_entity_addr)) != 0)
  1722. __reset_osd(osdc, osd);
  1723. }
  1724. }
  1725. /*
  1726. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1727. * no osd, request a new map.
  1728. *
  1729. * Caller should hold map_sem for read.
  1730. */
  1731. static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
  1732. bool force_resend_writes)
  1733. {
  1734. struct ceph_osd_request *req, *nreq;
  1735. struct rb_node *p;
  1736. int needmap = 0;
  1737. int err;
  1738. bool force_resend_req;
  1739. dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
  1740. force_resend_writes ? " (force resend writes)" : "");
  1741. mutex_lock(&osdc->request_mutex);
  1742. for (p = rb_first(&osdc->requests); p; ) {
  1743. req = rb_entry(p, struct ceph_osd_request, r_node);
  1744. p = rb_next(p);
  1745. /*
  1746. * For linger requests that have not yet been
  1747. * registered, move them to the linger list; they'll
  1748. * be sent to the osd in the loop below. Unregister
  1749. * the request before re-registering it as a linger
  1750. * request to ensure the __map_request() below
  1751. * will decide it needs to be sent.
  1752. */
  1753. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1754. dout("%p tid %llu restart on osd%d\n",
  1755. req, req->r_tid,
  1756. req->r_osd ? req->r_osd->o_osd : -1);
  1757. ceph_osdc_get_request(req);
  1758. __unregister_request(osdc, req);
  1759. __register_linger_request(osdc, req);
  1760. ceph_osdc_put_request(req);
  1761. continue;
  1762. }
  1763. force_resend_req = force_resend ||
  1764. (force_resend_writes &&
  1765. req->r_flags & CEPH_OSD_FLAG_WRITE);
  1766. err = __map_request(osdc, req, force_resend_req);
  1767. if (err < 0)
  1768. continue; /* error */
  1769. if (req->r_osd == NULL) {
  1770. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1771. needmap++; /* request a newer map */
  1772. } else if (err > 0) {
  1773. if (!req->r_linger) {
  1774. dout("%p tid %llu requeued on osd%d\n", req,
  1775. req->r_tid,
  1776. req->r_osd ? req->r_osd->o_osd : -1);
  1777. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1778. }
  1779. }
  1780. }
  1781. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1782. r_linger_item) {
  1783. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1784. err = __map_request(osdc, req,
  1785. force_resend || force_resend_writes);
  1786. dout("__map_request returned %d\n", err);
  1787. if (err < 0)
  1788. continue; /* hrm! */
  1789. if (req->r_osd == NULL || err > 0) {
  1790. if (req->r_osd == NULL) {
  1791. dout("lingering %p tid %llu maps to no osd\n",
  1792. req, req->r_tid);
  1793. /*
  1794. * A homeless lingering request makes
  1795. * no sense, as it's job is to keep
  1796. * a particular OSD connection open.
  1797. * Request a newer map and kick the
  1798. * request, knowing that it won't be
  1799. * resent until we actually get a map
  1800. * that can tell us where to send it.
  1801. */
  1802. needmap++;
  1803. }
  1804. dout("kicking lingering %p tid %llu osd%d\n", req,
  1805. req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
  1806. __register_request(osdc, req);
  1807. __unregister_linger_request(osdc, req);
  1808. }
  1809. }
  1810. reset_changed_osds(osdc);
  1811. mutex_unlock(&osdc->request_mutex);
  1812. if (needmap) {
  1813. dout("%d requests for down osds, need new map\n", needmap);
  1814. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1815. }
  1816. }
  1817. /*
  1818. * Process updated osd map.
  1819. *
  1820. * The message contains any number of incremental and full maps, normally
  1821. * indicating some sort of topology change in the cluster. Kick requests
  1822. * off to different OSDs as needed.
  1823. */
  1824. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1825. {
  1826. void *p, *end, *next;
  1827. u32 nr_maps, maplen;
  1828. u32 epoch;
  1829. struct ceph_osdmap *newmap = NULL, *oldmap;
  1830. int err;
  1831. struct ceph_fsid fsid;
  1832. bool was_full;
  1833. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1834. p = msg->front.iov_base;
  1835. end = p + msg->front.iov_len;
  1836. /* verify fsid */
  1837. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1838. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1839. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1840. return;
  1841. down_write(&osdc->map_sem);
  1842. was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
  1843. /* incremental maps */
  1844. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1845. dout(" %d inc maps\n", nr_maps);
  1846. while (nr_maps > 0) {
  1847. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1848. epoch = ceph_decode_32(&p);
  1849. maplen = ceph_decode_32(&p);
  1850. ceph_decode_need(&p, end, maplen, bad);
  1851. next = p + maplen;
  1852. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1853. dout("applying incremental map %u len %d\n",
  1854. epoch, maplen);
  1855. newmap = osdmap_apply_incremental(&p, next,
  1856. osdc->osdmap,
  1857. &osdc->client->msgr);
  1858. if (IS_ERR(newmap)) {
  1859. err = PTR_ERR(newmap);
  1860. goto bad;
  1861. }
  1862. BUG_ON(!newmap);
  1863. if (newmap != osdc->osdmap) {
  1864. ceph_osdmap_destroy(osdc->osdmap);
  1865. osdc->osdmap = newmap;
  1866. }
  1867. was_full = was_full ||
  1868. ceph_osdmap_flag(osdc->osdmap,
  1869. CEPH_OSDMAP_FULL);
  1870. kick_requests(osdc, 0, was_full);
  1871. } else {
  1872. dout("ignoring incremental map %u len %d\n",
  1873. epoch, maplen);
  1874. }
  1875. p = next;
  1876. nr_maps--;
  1877. }
  1878. if (newmap)
  1879. goto done;
  1880. /* full maps */
  1881. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1882. dout(" %d full maps\n", nr_maps);
  1883. while (nr_maps) {
  1884. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1885. epoch = ceph_decode_32(&p);
  1886. maplen = ceph_decode_32(&p);
  1887. ceph_decode_need(&p, end, maplen, bad);
  1888. if (nr_maps > 1) {
  1889. dout("skipping non-latest full map %u len %d\n",
  1890. epoch, maplen);
  1891. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1892. dout("skipping full map %u len %d, "
  1893. "older than our %u\n", epoch, maplen,
  1894. osdc->osdmap->epoch);
  1895. } else {
  1896. int skipped_map = 0;
  1897. dout("taking full map %u len %d\n", epoch, maplen);
  1898. newmap = ceph_osdmap_decode(&p, p+maplen);
  1899. if (IS_ERR(newmap)) {
  1900. err = PTR_ERR(newmap);
  1901. goto bad;
  1902. }
  1903. BUG_ON(!newmap);
  1904. oldmap = osdc->osdmap;
  1905. osdc->osdmap = newmap;
  1906. if (oldmap) {
  1907. if (oldmap->epoch + 1 < newmap->epoch)
  1908. skipped_map = 1;
  1909. ceph_osdmap_destroy(oldmap);
  1910. }
  1911. was_full = was_full ||
  1912. ceph_osdmap_flag(osdc->osdmap,
  1913. CEPH_OSDMAP_FULL);
  1914. kick_requests(osdc, skipped_map, was_full);
  1915. }
  1916. p += maplen;
  1917. nr_maps--;
  1918. }
  1919. if (!osdc->osdmap)
  1920. goto bad;
  1921. done:
  1922. downgrade_write(&osdc->map_sem);
  1923. ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
  1924. osdc->osdmap->epoch);
  1925. /*
  1926. * subscribe to subsequent osdmap updates if full to ensure
  1927. * we find out when we are no longer full and stop returning
  1928. * ENOSPC.
  1929. */
  1930. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
  1931. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
  1932. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
  1933. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1934. mutex_lock(&osdc->request_mutex);
  1935. __send_queued(osdc);
  1936. mutex_unlock(&osdc->request_mutex);
  1937. up_read(&osdc->map_sem);
  1938. wake_up_all(&osdc->client->auth_wq);
  1939. return;
  1940. bad:
  1941. pr_err("osdc handle_map corrupt msg\n");
  1942. ceph_msg_dump(msg);
  1943. up_write(&osdc->map_sem);
  1944. }
  1945. /*
  1946. * watch/notify callback event infrastructure
  1947. *
  1948. * These callbacks are used both for watch and notify operations.
  1949. */
  1950. static void __release_event(struct kref *kref)
  1951. {
  1952. struct ceph_osd_event *event =
  1953. container_of(kref, struct ceph_osd_event, kref);
  1954. dout("__release_event %p\n", event);
  1955. kfree(event);
  1956. }
  1957. static void get_event(struct ceph_osd_event *event)
  1958. {
  1959. kref_get(&event->kref);
  1960. }
  1961. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1962. {
  1963. kref_put(&event->kref, __release_event);
  1964. }
  1965. EXPORT_SYMBOL(ceph_osdc_put_event);
  1966. static void __insert_event(struct ceph_osd_client *osdc,
  1967. struct ceph_osd_event *new)
  1968. {
  1969. struct rb_node **p = &osdc->event_tree.rb_node;
  1970. struct rb_node *parent = NULL;
  1971. struct ceph_osd_event *event = NULL;
  1972. while (*p) {
  1973. parent = *p;
  1974. event = rb_entry(parent, struct ceph_osd_event, node);
  1975. if (new->cookie < event->cookie)
  1976. p = &(*p)->rb_left;
  1977. else if (new->cookie > event->cookie)
  1978. p = &(*p)->rb_right;
  1979. else
  1980. BUG();
  1981. }
  1982. rb_link_node(&new->node, parent, p);
  1983. rb_insert_color(&new->node, &osdc->event_tree);
  1984. }
  1985. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1986. u64 cookie)
  1987. {
  1988. struct rb_node **p = &osdc->event_tree.rb_node;
  1989. struct rb_node *parent = NULL;
  1990. struct ceph_osd_event *event = NULL;
  1991. while (*p) {
  1992. parent = *p;
  1993. event = rb_entry(parent, struct ceph_osd_event, node);
  1994. if (cookie < event->cookie)
  1995. p = &(*p)->rb_left;
  1996. else if (cookie > event->cookie)
  1997. p = &(*p)->rb_right;
  1998. else
  1999. return event;
  2000. }
  2001. return NULL;
  2002. }
  2003. static void __remove_event(struct ceph_osd_event *event)
  2004. {
  2005. struct ceph_osd_client *osdc = event->osdc;
  2006. if (!RB_EMPTY_NODE(&event->node)) {
  2007. dout("__remove_event removed %p\n", event);
  2008. rb_erase(&event->node, &osdc->event_tree);
  2009. ceph_osdc_put_event(event);
  2010. } else {
  2011. dout("__remove_event didn't remove %p\n", event);
  2012. }
  2013. }
  2014. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  2015. void (*event_cb)(u64, u64, u8, void *),
  2016. void *data, struct ceph_osd_event **pevent)
  2017. {
  2018. struct ceph_osd_event *event;
  2019. event = kmalloc(sizeof(*event), GFP_NOIO);
  2020. if (!event)
  2021. return -ENOMEM;
  2022. dout("create_event %p\n", event);
  2023. event->cb = event_cb;
  2024. event->one_shot = 0;
  2025. event->data = data;
  2026. event->osdc = osdc;
  2027. INIT_LIST_HEAD(&event->osd_node);
  2028. RB_CLEAR_NODE(&event->node);
  2029. kref_init(&event->kref); /* one ref for us */
  2030. kref_get(&event->kref); /* one ref for the caller */
  2031. spin_lock(&osdc->event_lock);
  2032. event->cookie = ++osdc->event_count;
  2033. __insert_event(osdc, event);
  2034. spin_unlock(&osdc->event_lock);
  2035. *pevent = event;
  2036. return 0;
  2037. }
  2038. EXPORT_SYMBOL(ceph_osdc_create_event);
  2039. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  2040. {
  2041. struct ceph_osd_client *osdc = event->osdc;
  2042. dout("cancel_event %p\n", event);
  2043. spin_lock(&osdc->event_lock);
  2044. __remove_event(event);
  2045. spin_unlock(&osdc->event_lock);
  2046. ceph_osdc_put_event(event); /* caller's */
  2047. }
  2048. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  2049. static void do_event_work(struct work_struct *work)
  2050. {
  2051. struct ceph_osd_event_work *event_work =
  2052. container_of(work, struct ceph_osd_event_work, work);
  2053. struct ceph_osd_event *event = event_work->event;
  2054. u64 ver = event_work->ver;
  2055. u64 notify_id = event_work->notify_id;
  2056. u8 opcode = event_work->opcode;
  2057. dout("do_event_work completing %p\n", event);
  2058. event->cb(ver, notify_id, opcode, event->data);
  2059. dout("do_event_work completed %p\n", event);
  2060. ceph_osdc_put_event(event);
  2061. kfree(event_work);
  2062. }
  2063. /*
  2064. * Process osd watch notifications
  2065. */
  2066. static void handle_watch_notify(struct ceph_osd_client *osdc,
  2067. struct ceph_msg *msg)
  2068. {
  2069. void *p, *end;
  2070. u8 proto_ver;
  2071. u64 cookie, ver, notify_id;
  2072. u8 opcode;
  2073. struct ceph_osd_event *event;
  2074. struct ceph_osd_event_work *event_work;
  2075. p = msg->front.iov_base;
  2076. end = p + msg->front.iov_len;
  2077. ceph_decode_8_safe(&p, end, proto_ver, bad);
  2078. ceph_decode_8_safe(&p, end, opcode, bad);
  2079. ceph_decode_64_safe(&p, end, cookie, bad);
  2080. ceph_decode_64_safe(&p, end, ver, bad);
  2081. ceph_decode_64_safe(&p, end, notify_id, bad);
  2082. spin_lock(&osdc->event_lock);
  2083. event = __find_event(osdc, cookie);
  2084. if (event) {
  2085. BUG_ON(event->one_shot);
  2086. get_event(event);
  2087. }
  2088. spin_unlock(&osdc->event_lock);
  2089. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  2090. cookie, ver, event);
  2091. if (event) {
  2092. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  2093. if (!event_work) {
  2094. pr_err("couldn't allocate event_work\n");
  2095. ceph_osdc_put_event(event);
  2096. return;
  2097. }
  2098. INIT_WORK(&event_work->work, do_event_work);
  2099. event_work->event = event;
  2100. event_work->ver = ver;
  2101. event_work->notify_id = notify_id;
  2102. event_work->opcode = opcode;
  2103. queue_work(osdc->notify_wq, &event_work->work);
  2104. }
  2105. return;
  2106. bad:
  2107. pr_err("osdc handle_watch_notify corrupt msg\n");
  2108. }
  2109. /*
  2110. * build new request AND message
  2111. *
  2112. */
  2113. void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
  2114. struct ceph_snap_context *snapc, u64 snap_id,
  2115. struct timespec *mtime)
  2116. {
  2117. struct ceph_msg *msg = req->r_request;
  2118. void *p;
  2119. size_t msg_size;
  2120. int flags = req->r_flags;
  2121. u64 data_len;
  2122. unsigned int i;
  2123. req->r_snapid = snap_id;
  2124. req->r_snapc = ceph_get_snap_context(snapc);
  2125. /* encode request */
  2126. msg->hdr.version = cpu_to_le16(4);
  2127. p = msg->front.iov_base;
  2128. ceph_encode_32(&p, 1); /* client_inc is always 1 */
  2129. req->r_request_osdmap_epoch = p;
  2130. p += 4;
  2131. req->r_request_flags = p;
  2132. p += 4;
  2133. if (req->r_flags & CEPH_OSD_FLAG_WRITE)
  2134. ceph_encode_timespec(p, mtime);
  2135. p += sizeof(struct ceph_timespec);
  2136. req->r_request_reassert_version = p;
  2137. p += sizeof(struct ceph_eversion); /* will get filled in */
  2138. /* oloc */
  2139. ceph_encode_8(&p, 4);
  2140. ceph_encode_8(&p, 4);
  2141. ceph_encode_32(&p, 8 + 4 + 4);
  2142. req->r_request_pool = p;
  2143. p += 8;
  2144. ceph_encode_32(&p, -1); /* preferred */
  2145. ceph_encode_32(&p, 0); /* key len */
  2146. ceph_encode_8(&p, 1);
  2147. req->r_request_pgid = p;
  2148. p += 8 + 4;
  2149. ceph_encode_32(&p, -1); /* preferred */
  2150. /* oid */
  2151. ceph_encode_32(&p, req->r_base_oid.name_len);
  2152. memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
  2153. dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
  2154. req->r_base_oid.name, req->r_base_oid.name_len);
  2155. p += req->r_base_oid.name_len;
  2156. /* ops--can imply data */
  2157. ceph_encode_16(&p, (u16)req->r_num_ops);
  2158. data_len = 0;
  2159. for (i = 0; i < req->r_num_ops; i++) {
  2160. data_len += osd_req_encode_op(req, p, i);
  2161. p += sizeof(struct ceph_osd_op);
  2162. }
  2163. /* snaps */
  2164. ceph_encode_64(&p, req->r_snapid);
  2165. ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
  2166. ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
  2167. if (req->r_snapc) {
  2168. for (i = 0; i < snapc->num_snaps; i++) {
  2169. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  2170. }
  2171. }
  2172. req->r_request_attempts = p;
  2173. p += 4;
  2174. /* data */
  2175. if (flags & CEPH_OSD_FLAG_WRITE) {
  2176. u16 data_off;
  2177. /*
  2178. * The header "data_off" is a hint to the receiver
  2179. * allowing it to align received data into its
  2180. * buffers such that there's no need to re-copy
  2181. * it before writing it to disk (direct I/O).
  2182. */
  2183. data_off = (u16) (off & 0xffff);
  2184. req->r_request->hdr.data_off = cpu_to_le16(data_off);
  2185. }
  2186. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  2187. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  2188. msg_size = p - msg->front.iov_base;
  2189. msg->front.iov_len = msg_size;
  2190. msg->hdr.front_len = cpu_to_le32(msg_size);
  2191. dout("build_request msg_size was %d\n", (int)msg_size);
  2192. }
  2193. EXPORT_SYMBOL(ceph_osdc_build_request);
  2194. /*
  2195. * Register request, send initial attempt.
  2196. */
  2197. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  2198. struct ceph_osd_request *req,
  2199. bool nofail)
  2200. {
  2201. int rc;
  2202. down_read(&osdc->map_sem);
  2203. mutex_lock(&osdc->request_mutex);
  2204. rc = __ceph_osdc_start_request(osdc, req, nofail);
  2205. mutex_unlock(&osdc->request_mutex);
  2206. up_read(&osdc->map_sem);
  2207. return rc;
  2208. }
  2209. EXPORT_SYMBOL(ceph_osdc_start_request);
  2210. /*
  2211. * Unregister a registered request. The request is not completed (i.e.
  2212. * no callbacks or wakeups) - higher layers are supposed to know what
  2213. * they are canceling.
  2214. */
  2215. void ceph_osdc_cancel_request(struct ceph_osd_request *req)
  2216. {
  2217. struct ceph_osd_client *osdc = req->r_osdc;
  2218. mutex_lock(&osdc->request_mutex);
  2219. if (req->r_linger)
  2220. __unregister_linger_request(osdc, req);
  2221. __unregister_request(osdc, req);
  2222. mutex_unlock(&osdc->request_mutex);
  2223. dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
  2224. }
  2225. EXPORT_SYMBOL(ceph_osdc_cancel_request);
  2226. /*
  2227. * wait for a request to complete
  2228. */
  2229. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  2230. struct ceph_osd_request *req)
  2231. {
  2232. int rc;
  2233. dout("%s %p tid %llu\n", __func__, req, req->r_tid);
  2234. rc = wait_for_completion_interruptible(&req->r_completion);
  2235. if (rc < 0) {
  2236. dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
  2237. ceph_osdc_cancel_request(req);
  2238. complete_request(req);
  2239. return rc;
  2240. }
  2241. dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
  2242. req->r_result);
  2243. return req->r_result;
  2244. }
  2245. EXPORT_SYMBOL(ceph_osdc_wait_request);
  2246. /*
  2247. * sync - wait for all in-flight requests to flush. avoid starvation.
  2248. */
  2249. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  2250. {
  2251. struct ceph_osd_request *req;
  2252. u64 last_tid, next_tid = 0;
  2253. mutex_lock(&osdc->request_mutex);
  2254. last_tid = osdc->last_tid;
  2255. while (1) {
  2256. req = __lookup_request_ge(osdc, next_tid);
  2257. if (!req)
  2258. break;
  2259. if (req->r_tid > last_tid)
  2260. break;
  2261. next_tid = req->r_tid + 1;
  2262. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  2263. continue;
  2264. ceph_osdc_get_request(req);
  2265. mutex_unlock(&osdc->request_mutex);
  2266. dout("sync waiting on tid %llu (last is %llu)\n",
  2267. req->r_tid, last_tid);
  2268. wait_for_completion(&req->r_safe_completion);
  2269. mutex_lock(&osdc->request_mutex);
  2270. ceph_osdc_put_request(req);
  2271. }
  2272. mutex_unlock(&osdc->request_mutex);
  2273. dout("sync done (thru tid %llu)\n", last_tid);
  2274. }
  2275. EXPORT_SYMBOL(ceph_osdc_sync);
  2276. /*
  2277. * Call all pending notify callbacks - for use after a watch is
  2278. * unregistered, to make sure no more callbacks for it will be invoked
  2279. */
  2280. void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
  2281. {
  2282. flush_workqueue(osdc->notify_wq);
  2283. }
  2284. EXPORT_SYMBOL(ceph_osdc_flush_notifies);
  2285. /*
  2286. * init, shutdown
  2287. */
  2288. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  2289. {
  2290. int err;
  2291. dout("init\n");
  2292. osdc->client = client;
  2293. osdc->osdmap = NULL;
  2294. init_rwsem(&osdc->map_sem);
  2295. init_completion(&osdc->map_waiters);
  2296. osdc->last_requested_map = 0;
  2297. mutex_init(&osdc->request_mutex);
  2298. osdc->last_tid = 0;
  2299. osdc->osds = RB_ROOT;
  2300. INIT_LIST_HEAD(&osdc->osd_lru);
  2301. osdc->requests = RB_ROOT;
  2302. INIT_LIST_HEAD(&osdc->req_lru);
  2303. INIT_LIST_HEAD(&osdc->req_unsent);
  2304. INIT_LIST_HEAD(&osdc->req_notarget);
  2305. INIT_LIST_HEAD(&osdc->req_linger);
  2306. osdc->num_requests = 0;
  2307. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  2308. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  2309. spin_lock_init(&osdc->event_lock);
  2310. osdc->event_tree = RB_ROOT;
  2311. osdc->event_count = 0;
  2312. schedule_delayed_work(&osdc->osds_timeout_work,
  2313. round_jiffies_relative(osdc->client->options->osd_idle_ttl));
  2314. err = -ENOMEM;
  2315. osdc->req_mempool = mempool_create_slab_pool(10,
  2316. ceph_osd_request_cache);
  2317. if (!osdc->req_mempool)
  2318. goto out;
  2319. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  2320. OSD_OP_FRONT_LEN, 10, true,
  2321. "osd_op");
  2322. if (err < 0)
  2323. goto out_mempool;
  2324. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  2325. OSD_OPREPLY_FRONT_LEN, 10, true,
  2326. "osd_op_reply");
  2327. if (err < 0)
  2328. goto out_msgpool;
  2329. err = -ENOMEM;
  2330. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  2331. if (!osdc->notify_wq)
  2332. goto out_msgpool_reply;
  2333. return 0;
  2334. out_msgpool_reply:
  2335. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  2336. out_msgpool:
  2337. ceph_msgpool_destroy(&osdc->msgpool_op);
  2338. out_mempool:
  2339. mempool_destroy(osdc->req_mempool);
  2340. out:
  2341. return err;
  2342. }
  2343. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  2344. {
  2345. flush_workqueue(osdc->notify_wq);
  2346. destroy_workqueue(osdc->notify_wq);
  2347. cancel_delayed_work_sync(&osdc->timeout_work);
  2348. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  2349. if (osdc->osdmap) {
  2350. ceph_osdmap_destroy(osdc->osdmap);
  2351. osdc->osdmap = NULL;
  2352. }
  2353. remove_all_osds(osdc);
  2354. mempool_destroy(osdc->req_mempool);
  2355. ceph_msgpool_destroy(&osdc->msgpool_op);
  2356. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  2357. }
  2358. /*
  2359. * Read some contiguous pages. If we cross a stripe boundary, shorten
  2360. * *plen. Return number of bytes read, or error.
  2361. */
  2362. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  2363. struct ceph_vino vino, struct ceph_file_layout *layout,
  2364. u64 off, u64 *plen,
  2365. u32 truncate_seq, u64 truncate_size,
  2366. struct page **pages, int num_pages, int page_align)
  2367. {
  2368. struct ceph_osd_request *req;
  2369. int rc = 0;
  2370. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  2371. vino.snap, off, *plen);
  2372. req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
  2373. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  2374. NULL, truncate_seq, truncate_size,
  2375. false);
  2376. if (IS_ERR(req))
  2377. return PTR_ERR(req);
  2378. /* it may be a short read due to an object boundary */
  2379. osd_req_op_extent_osd_data_pages(req, 0,
  2380. pages, *plen, page_align, false, false);
  2381. dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
  2382. off, *plen, *plen, page_align);
  2383. ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
  2384. rc = ceph_osdc_start_request(osdc, req, false);
  2385. if (!rc)
  2386. rc = ceph_osdc_wait_request(osdc, req);
  2387. ceph_osdc_put_request(req);
  2388. dout("readpages result %d\n", rc);
  2389. return rc;
  2390. }
  2391. EXPORT_SYMBOL(ceph_osdc_readpages);
  2392. /*
  2393. * do a synchronous write on N pages
  2394. */
  2395. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  2396. struct ceph_file_layout *layout,
  2397. struct ceph_snap_context *snapc,
  2398. u64 off, u64 len,
  2399. u32 truncate_seq, u64 truncate_size,
  2400. struct timespec *mtime,
  2401. struct page **pages, int num_pages)
  2402. {
  2403. struct ceph_osd_request *req;
  2404. int rc = 0;
  2405. int page_align = off & ~PAGE_MASK;
  2406. BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
  2407. req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
  2408. CEPH_OSD_OP_WRITE,
  2409. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  2410. snapc, truncate_seq, truncate_size,
  2411. true);
  2412. if (IS_ERR(req))
  2413. return PTR_ERR(req);
  2414. /* it may be a short write due to an object boundary */
  2415. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  2416. false, false);
  2417. dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
  2418. ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
  2419. rc = ceph_osdc_start_request(osdc, req, true);
  2420. if (!rc)
  2421. rc = ceph_osdc_wait_request(osdc, req);
  2422. ceph_osdc_put_request(req);
  2423. if (rc == 0)
  2424. rc = len;
  2425. dout("writepages result %d\n", rc);
  2426. return rc;
  2427. }
  2428. EXPORT_SYMBOL(ceph_osdc_writepages);
  2429. int ceph_osdc_setup(void)
  2430. {
  2431. size_t size = sizeof(struct ceph_osd_request) +
  2432. CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
  2433. BUG_ON(ceph_osd_request_cache);
  2434. ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
  2435. 0, 0, NULL);
  2436. return ceph_osd_request_cache ? 0 : -ENOMEM;
  2437. }
  2438. EXPORT_SYMBOL(ceph_osdc_setup);
  2439. void ceph_osdc_cleanup(void)
  2440. {
  2441. BUG_ON(!ceph_osd_request_cache);
  2442. kmem_cache_destroy(ceph_osd_request_cache);
  2443. ceph_osd_request_cache = NULL;
  2444. }
  2445. EXPORT_SYMBOL(ceph_osdc_cleanup);
  2446. /*
  2447. * handle incoming message
  2448. */
  2449. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  2450. {
  2451. struct ceph_osd *osd = con->private;
  2452. struct ceph_osd_client *osdc;
  2453. int type = le16_to_cpu(msg->hdr.type);
  2454. if (!osd)
  2455. goto out;
  2456. osdc = osd->o_osdc;
  2457. switch (type) {
  2458. case CEPH_MSG_OSD_MAP:
  2459. ceph_osdc_handle_map(osdc, msg);
  2460. break;
  2461. case CEPH_MSG_OSD_OPREPLY:
  2462. handle_reply(osdc, msg);
  2463. break;
  2464. case CEPH_MSG_WATCH_NOTIFY:
  2465. handle_watch_notify(osdc, msg);
  2466. break;
  2467. default:
  2468. pr_err("received unknown message type %d %s\n", type,
  2469. ceph_msg_type_name(type));
  2470. }
  2471. out:
  2472. ceph_msg_put(msg);
  2473. }
  2474. /*
  2475. * Lookup and return message for incoming reply. Don't try to do
  2476. * anything about a larger than preallocated data portion of the
  2477. * message at the moment - for now, just skip the message.
  2478. */
  2479. static struct ceph_msg *get_reply(struct ceph_connection *con,
  2480. struct ceph_msg_header *hdr,
  2481. int *skip)
  2482. {
  2483. struct ceph_osd *osd = con->private;
  2484. struct ceph_osd_client *osdc = osd->o_osdc;
  2485. struct ceph_msg *m;
  2486. struct ceph_osd_request *req;
  2487. int front_len = le32_to_cpu(hdr->front_len);
  2488. int data_len = le32_to_cpu(hdr->data_len);
  2489. u64 tid;
  2490. tid = le64_to_cpu(hdr->tid);
  2491. mutex_lock(&osdc->request_mutex);
  2492. req = __lookup_request(osdc, tid);
  2493. if (!req) {
  2494. dout("%s osd%d tid %llu unknown, skipping\n", __func__,
  2495. osd->o_osd, tid);
  2496. m = NULL;
  2497. *skip = 1;
  2498. goto out;
  2499. }
  2500. ceph_msg_revoke_incoming(req->r_reply);
  2501. if (front_len > req->r_reply->front_alloc_len) {
  2502. pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
  2503. __func__, osd->o_osd, req->r_tid, front_len,
  2504. req->r_reply->front_alloc_len);
  2505. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
  2506. false);
  2507. if (!m)
  2508. goto out;
  2509. ceph_msg_put(req->r_reply);
  2510. req->r_reply = m;
  2511. }
  2512. if (data_len > req->r_reply->data_length) {
  2513. pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
  2514. __func__, osd->o_osd, req->r_tid, data_len,
  2515. req->r_reply->data_length);
  2516. m = NULL;
  2517. *skip = 1;
  2518. goto out;
  2519. }
  2520. m = ceph_msg_get(req->r_reply);
  2521. dout("get_reply tid %lld %p\n", tid, m);
  2522. out:
  2523. mutex_unlock(&osdc->request_mutex);
  2524. return m;
  2525. }
  2526. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  2527. struct ceph_msg_header *hdr,
  2528. int *skip)
  2529. {
  2530. struct ceph_osd *osd = con->private;
  2531. int type = le16_to_cpu(hdr->type);
  2532. int front = le32_to_cpu(hdr->front_len);
  2533. *skip = 0;
  2534. switch (type) {
  2535. case CEPH_MSG_OSD_MAP:
  2536. case CEPH_MSG_WATCH_NOTIFY:
  2537. return ceph_msg_new(type, front, GFP_NOFS, false);
  2538. case CEPH_MSG_OSD_OPREPLY:
  2539. return get_reply(con, hdr, skip);
  2540. default:
  2541. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  2542. osd->o_osd);
  2543. *skip = 1;
  2544. return NULL;
  2545. }
  2546. }
  2547. /*
  2548. * Wrappers to refcount containing ceph_osd struct
  2549. */
  2550. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  2551. {
  2552. struct ceph_osd *osd = con->private;
  2553. if (get_osd(osd))
  2554. return con;
  2555. return NULL;
  2556. }
  2557. static void put_osd_con(struct ceph_connection *con)
  2558. {
  2559. struct ceph_osd *osd = con->private;
  2560. put_osd(osd);
  2561. }
  2562. /*
  2563. * authentication
  2564. */
  2565. /*
  2566. * Note: returned pointer is the address of a structure that's
  2567. * managed separately. Caller must *not* attempt to free it.
  2568. */
  2569. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  2570. int *proto, int force_new)
  2571. {
  2572. struct ceph_osd *o = con->private;
  2573. struct ceph_osd_client *osdc = o->o_osdc;
  2574. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2575. struct ceph_auth_handshake *auth = &o->o_auth;
  2576. if (force_new && auth->authorizer) {
  2577. ceph_auth_destroy_authorizer(ac, auth->authorizer);
  2578. auth->authorizer = NULL;
  2579. }
  2580. if (!auth->authorizer) {
  2581. int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2582. auth);
  2583. if (ret)
  2584. return ERR_PTR(ret);
  2585. } else {
  2586. int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2587. auth);
  2588. if (ret)
  2589. return ERR_PTR(ret);
  2590. }
  2591. *proto = ac->protocol;
  2592. return auth;
  2593. }
  2594. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  2595. {
  2596. struct ceph_osd *o = con->private;
  2597. struct ceph_osd_client *osdc = o->o_osdc;
  2598. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2599. return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  2600. }
  2601. static int invalidate_authorizer(struct ceph_connection *con)
  2602. {
  2603. struct ceph_osd *o = con->private;
  2604. struct ceph_osd_client *osdc = o->o_osdc;
  2605. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2606. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  2607. return ceph_monc_validate_auth(&osdc->client->monc);
  2608. }
  2609. static int osd_sign_message(struct ceph_msg *msg)
  2610. {
  2611. struct ceph_osd *o = msg->con->private;
  2612. struct ceph_auth_handshake *auth = &o->o_auth;
  2613. return ceph_auth_sign_message(auth, msg);
  2614. }
  2615. static int osd_check_message_signature(struct ceph_msg *msg)
  2616. {
  2617. struct ceph_osd *o = msg->con->private;
  2618. struct ceph_auth_handshake *auth = &o->o_auth;
  2619. return ceph_auth_check_message_signature(auth, msg);
  2620. }
  2621. static const struct ceph_connection_operations osd_con_ops = {
  2622. .get = get_osd_con,
  2623. .put = put_osd_con,
  2624. .dispatch = dispatch,
  2625. .get_authorizer = get_authorizer,
  2626. .verify_authorizer_reply = verify_authorizer_reply,
  2627. .invalidate_authorizer = invalidate_authorizer,
  2628. .alloc_msg = alloc_msg,
  2629. .sign_message = osd_sign_message,
  2630. .check_message_signature = osd_check_message_signature,
  2631. .fault = osd_reset,
  2632. };