osd_client.c 75 KB

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