osd_client.c 79 KB

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