osd_client.c 77 KB

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