osd_client.c 78 KB

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