osd_client.c 79 KB

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