osd_client.c 78 KB

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