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