osd_client.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853
  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. * Timeout callback, called every N seconds when 1 or more osd
  1242. * requests has been active for more than N seconds. When this
  1243. * happens, we ping all OSDs with requests who have timed out to
  1244. * ensure any communications channel reset is detected. Reset the
  1245. * request timeouts another N seconds in the future as we go.
  1246. * Reschedule the timeout event another N seconds in future (unless
  1247. * there are no open requests).
  1248. */
  1249. static void handle_timeout(struct work_struct *work)
  1250. {
  1251. struct ceph_osd_client *osdc =
  1252. container_of(work, struct ceph_osd_client, timeout_work.work);
  1253. struct ceph_osd_request *req;
  1254. struct ceph_osd *osd;
  1255. unsigned long keepalive =
  1256. osdc->client->options->osd_keepalive_timeout * HZ;
  1257. struct list_head slow_osds;
  1258. dout("timeout\n");
  1259. down_read(&osdc->map_sem);
  1260. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1261. mutex_lock(&osdc->request_mutex);
  1262. /*
  1263. * ping osds that are a bit slow. this ensures that if there
  1264. * is a break in the TCP connection we will notice, and reopen
  1265. * a connection with that osd (from the fault callback).
  1266. */
  1267. INIT_LIST_HEAD(&slow_osds);
  1268. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  1269. if (time_before(jiffies, req->r_stamp + keepalive))
  1270. break;
  1271. osd = req->r_osd;
  1272. BUG_ON(!osd);
  1273. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  1274. req->r_tid, osd->o_osd);
  1275. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  1276. }
  1277. while (!list_empty(&slow_osds)) {
  1278. osd = list_entry(slow_osds.next, struct ceph_osd,
  1279. o_keepalive_item);
  1280. list_del_init(&osd->o_keepalive_item);
  1281. ceph_con_keepalive(&osd->o_con);
  1282. }
  1283. __schedule_osd_timeout(osdc);
  1284. __send_queued(osdc);
  1285. mutex_unlock(&osdc->request_mutex);
  1286. up_read(&osdc->map_sem);
  1287. }
  1288. static void handle_osds_timeout(struct work_struct *work)
  1289. {
  1290. struct ceph_osd_client *osdc =
  1291. container_of(work, struct ceph_osd_client,
  1292. osds_timeout_work.work);
  1293. unsigned long delay =
  1294. osdc->client->options->osd_idle_ttl * HZ >> 2;
  1295. dout("osds timeout\n");
  1296. down_read(&osdc->map_sem);
  1297. remove_old_osds(osdc);
  1298. up_read(&osdc->map_sem);
  1299. schedule_delayed_work(&osdc->osds_timeout_work,
  1300. round_jiffies_relative(delay));
  1301. }
  1302. static int ceph_oloc_decode(void **p, void *end,
  1303. struct ceph_object_locator *oloc)
  1304. {
  1305. u8 struct_v, struct_cv;
  1306. u32 len;
  1307. void *struct_end;
  1308. int ret = 0;
  1309. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  1310. struct_v = ceph_decode_8(p);
  1311. struct_cv = ceph_decode_8(p);
  1312. if (struct_v < 3) {
  1313. pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
  1314. struct_v, struct_cv);
  1315. goto e_inval;
  1316. }
  1317. if (struct_cv > 6) {
  1318. pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
  1319. struct_v, struct_cv);
  1320. goto e_inval;
  1321. }
  1322. len = ceph_decode_32(p);
  1323. ceph_decode_need(p, end, len, e_inval);
  1324. struct_end = *p + len;
  1325. oloc->pool = ceph_decode_64(p);
  1326. *p += 4; /* skip preferred */
  1327. len = ceph_decode_32(p);
  1328. if (len > 0) {
  1329. pr_warn("ceph_object_locator::key is set\n");
  1330. goto e_inval;
  1331. }
  1332. if (struct_v >= 5) {
  1333. len = ceph_decode_32(p);
  1334. if (len > 0) {
  1335. pr_warn("ceph_object_locator::nspace is set\n");
  1336. goto e_inval;
  1337. }
  1338. }
  1339. if (struct_v >= 6) {
  1340. s64 hash = ceph_decode_64(p);
  1341. if (hash != -1) {
  1342. pr_warn("ceph_object_locator::hash is set\n");
  1343. goto e_inval;
  1344. }
  1345. }
  1346. /* skip the rest */
  1347. *p = struct_end;
  1348. out:
  1349. return ret;
  1350. e_inval:
  1351. ret = -EINVAL;
  1352. goto out;
  1353. }
  1354. static int ceph_redirect_decode(void **p, void *end,
  1355. struct ceph_request_redirect *redir)
  1356. {
  1357. u8 struct_v, struct_cv;
  1358. u32 len;
  1359. void *struct_end;
  1360. int ret;
  1361. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  1362. struct_v = ceph_decode_8(p);
  1363. struct_cv = ceph_decode_8(p);
  1364. if (struct_cv > 1) {
  1365. pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
  1366. struct_v, struct_cv);
  1367. goto e_inval;
  1368. }
  1369. len = ceph_decode_32(p);
  1370. ceph_decode_need(p, end, len, e_inval);
  1371. struct_end = *p + len;
  1372. ret = ceph_oloc_decode(p, end, &redir->oloc);
  1373. if (ret)
  1374. goto out;
  1375. len = ceph_decode_32(p);
  1376. if (len > 0) {
  1377. pr_warn("ceph_request_redirect::object_name is set\n");
  1378. goto e_inval;
  1379. }
  1380. len = ceph_decode_32(p);
  1381. *p += len; /* skip osd_instructions */
  1382. /* skip the rest */
  1383. *p = struct_end;
  1384. out:
  1385. return ret;
  1386. e_inval:
  1387. ret = -EINVAL;
  1388. goto out;
  1389. }
  1390. static void complete_request(struct ceph_osd_request *req)
  1391. {
  1392. complete_all(&req->r_safe_completion); /* fsync waiter */
  1393. }
  1394. /*
  1395. * handle osd op reply. either call the callback if it is specified,
  1396. * or do the completion to wake up the waiting thread.
  1397. */
  1398. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1399. struct ceph_connection *con)
  1400. {
  1401. void *p, *end;
  1402. struct ceph_osd_request *req;
  1403. struct ceph_request_redirect redir;
  1404. u64 tid;
  1405. int object_len;
  1406. unsigned int numops;
  1407. int payload_len, flags;
  1408. s32 result;
  1409. s32 retry_attempt;
  1410. struct ceph_pg pg;
  1411. int err;
  1412. u32 reassert_epoch;
  1413. u64 reassert_version;
  1414. u32 osdmap_epoch;
  1415. int already_completed;
  1416. u32 bytes;
  1417. unsigned int i;
  1418. tid = le64_to_cpu(msg->hdr.tid);
  1419. dout("handle_reply %p tid %llu\n", msg, tid);
  1420. p = msg->front.iov_base;
  1421. end = p + msg->front.iov_len;
  1422. ceph_decode_need(&p, end, 4, bad);
  1423. object_len = ceph_decode_32(&p);
  1424. ceph_decode_need(&p, end, object_len, bad);
  1425. p += object_len;
  1426. err = ceph_decode_pgid(&p, end, &pg);
  1427. if (err)
  1428. goto bad;
  1429. ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
  1430. flags = ceph_decode_64(&p);
  1431. result = ceph_decode_32(&p);
  1432. reassert_epoch = ceph_decode_32(&p);
  1433. reassert_version = ceph_decode_64(&p);
  1434. osdmap_epoch = ceph_decode_32(&p);
  1435. /* lookup */
  1436. mutex_lock(&osdc->request_mutex);
  1437. req = __lookup_request(osdc, tid);
  1438. if (req == NULL) {
  1439. dout("handle_reply tid %llu dne\n", tid);
  1440. goto bad_mutex;
  1441. }
  1442. ceph_osdc_get_request(req);
  1443. dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
  1444. req, result);
  1445. ceph_decode_need(&p, end, 4, bad_put);
  1446. numops = ceph_decode_32(&p);
  1447. if (numops > CEPH_OSD_MAX_OP)
  1448. goto bad_put;
  1449. if (numops != req->r_num_ops)
  1450. goto bad_put;
  1451. payload_len = 0;
  1452. ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
  1453. for (i = 0; i < numops; i++) {
  1454. struct ceph_osd_op *op = p;
  1455. int len;
  1456. len = le32_to_cpu(op->payload_len);
  1457. req->r_reply_op_len[i] = len;
  1458. dout(" op %d has %d bytes\n", i, len);
  1459. payload_len += len;
  1460. p += sizeof(*op);
  1461. }
  1462. bytes = le32_to_cpu(msg->hdr.data_len);
  1463. if (payload_len != bytes) {
  1464. pr_warning("sum of op payload lens %d != data_len %d",
  1465. payload_len, bytes);
  1466. goto bad_put;
  1467. }
  1468. ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
  1469. retry_attempt = ceph_decode_32(&p);
  1470. for (i = 0; i < numops; i++)
  1471. req->r_reply_op_result[i] = ceph_decode_32(&p);
  1472. if (le16_to_cpu(msg->hdr.version) >= 6) {
  1473. p += 8 + 4; /* skip replay_version */
  1474. p += 8; /* skip user_version */
  1475. err = ceph_redirect_decode(&p, end, &redir);
  1476. if (err)
  1477. goto bad_put;
  1478. } else {
  1479. redir.oloc.pool = -1;
  1480. }
  1481. if (redir.oloc.pool != -1) {
  1482. dout("redirect pool %lld\n", redir.oloc.pool);
  1483. __unregister_request(osdc, req);
  1484. mutex_unlock(&osdc->request_mutex);
  1485. req->r_target_oloc = redir.oloc; /* struct */
  1486. /*
  1487. * Start redirect requests with nofail=true. If
  1488. * mapping fails, request will end up on the notarget
  1489. * list, waiting for the new osdmap (which can take
  1490. * a while), even though the original request mapped
  1491. * successfully. In the future we might want to follow
  1492. * original request's nofail setting here.
  1493. */
  1494. err = ceph_osdc_start_request(osdc, req, true);
  1495. BUG_ON(err);
  1496. goto done;
  1497. }
  1498. already_completed = req->r_got_reply;
  1499. if (!req->r_got_reply) {
  1500. req->r_result = result;
  1501. dout("handle_reply result %d bytes %d\n", req->r_result,
  1502. bytes);
  1503. if (req->r_result == 0)
  1504. req->r_result = bytes;
  1505. /* in case this is a write and we need to replay, */
  1506. req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
  1507. req->r_reassert_version.version = cpu_to_le64(reassert_version);
  1508. req->r_got_reply = 1;
  1509. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1510. dout("handle_reply tid %llu dup ack\n", tid);
  1511. mutex_unlock(&osdc->request_mutex);
  1512. goto done;
  1513. }
  1514. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1515. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1516. __register_linger_request(osdc, req);
  1517. /* either this is a read, or we got the safe response */
  1518. if (result < 0 ||
  1519. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1520. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1521. __unregister_request(osdc, req);
  1522. mutex_unlock(&osdc->request_mutex);
  1523. if (!already_completed) {
  1524. if (req->r_unsafe_callback &&
  1525. result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
  1526. req->r_unsafe_callback(req, true);
  1527. if (req->r_callback)
  1528. req->r_callback(req, msg);
  1529. else
  1530. complete_all(&req->r_completion);
  1531. }
  1532. if (flags & CEPH_OSD_FLAG_ONDISK) {
  1533. if (req->r_unsafe_callback && already_completed)
  1534. req->r_unsafe_callback(req, false);
  1535. complete_request(req);
  1536. }
  1537. done:
  1538. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1539. ceph_osdc_put_request(req);
  1540. return;
  1541. bad_put:
  1542. req->r_result = -EIO;
  1543. __unregister_request(osdc, req);
  1544. if (req->r_callback)
  1545. req->r_callback(req, msg);
  1546. else
  1547. complete_all(&req->r_completion);
  1548. complete_request(req);
  1549. ceph_osdc_put_request(req);
  1550. bad_mutex:
  1551. mutex_unlock(&osdc->request_mutex);
  1552. bad:
  1553. pr_err("corrupt osd_op_reply got %d %d\n",
  1554. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
  1555. ceph_msg_dump(msg);
  1556. }
  1557. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1558. {
  1559. struct rb_node *p, *n;
  1560. for (p = rb_first(&osdc->osds); p; p = n) {
  1561. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1562. n = rb_next(p);
  1563. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1564. memcmp(&osd->o_con.peer_addr,
  1565. ceph_osd_addr(osdc->osdmap,
  1566. osd->o_osd),
  1567. sizeof(struct ceph_entity_addr)) != 0)
  1568. __reset_osd(osdc, osd);
  1569. }
  1570. }
  1571. /*
  1572. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1573. * no osd, request a new map.
  1574. *
  1575. * Caller should hold map_sem for read.
  1576. */
  1577. static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
  1578. bool force_resend_writes)
  1579. {
  1580. struct ceph_osd_request *req, *nreq;
  1581. struct rb_node *p;
  1582. int needmap = 0;
  1583. int err;
  1584. bool force_resend_req;
  1585. dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
  1586. force_resend_writes ? " (force resend writes)" : "");
  1587. mutex_lock(&osdc->request_mutex);
  1588. for (p = rb_first(&osdc->requests); p; ) {
  1589. req = rb_entry(p, struct ceph_osd_request, r_node);
  1590. p = rb_next(p);
  1591. /*
  1592. * For linger requests that have not yet been
  1593. * registered, move them to the linger list; they'll
  1594. * be sent to the osd in the loop below. Unregister
  1595. * the request before re-registering it as a linger
  1596. * request to ensure the __map_request() below
  1597. * will decide it needs to be sent.
  1598. */
  1599. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1600. dout("%p tid %llu restart on osd%d\n",
  1601. req, req->r_tid,
  1602. req->r_osd ? req->r_osd->o_osd : -1);
  1603. ceph_osdc_get_request(req);
  1604. __unregister_request(osdc, req);
  1605. __register_linger_request(osdc, req);
  1606. ceph_osdc_put_request(req);
  1607. continue;
  1608. }
  1609. force_resend_req = force_resend ||
  1610. (force_resend_writes &&
  1611. req->r_flags & CEPH_OSD_FLAG_WRITE);
  1612. err = __map_request(osdc, req, force_resend_req);
  1613. if (err < 0)
  1614. continue; /* error */
  1615. if (req->r_osd == NULL) {
  1616. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1617. needmap++; /* request a newer map */
  1618. } else if (err > 0) {
  1619. if (!req->r_linger) {
  1620. dout("%p tid %llu requeued on osd%d\n", req,
  1621. req->r_tid,
  1622. req->r_osd ? req->r_osd->o_osd : -1);
  1623. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1624. }
  1625. }
  1626. }
  1627. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1628. r_linger_item) {
  1629. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1630. err = __map_request(osdc, req,
  1631. force_resend || force_resend_writes);
  1632. dout("__map_request returned %d\n", err);
  1633. if (err == 0)
  1634. continue; /* no change and no osd was specified */
  1635. if (err < 0)
  1636. continue; /* hrm! */
  1637. if (req->r_osd == NULL) {
  1638. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1639. needmap++; /* request a newer map */
  1640. continue;
  1641. }
  1642. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1643. req->r_osd ? req->r_osd->o_osd : -1);
  1644. __register_request(osdc, req);
  1645. __unregister_linger_request(osdc, req);
  1646. }
  1647. reset_changed_osds(osdc);
  1648. mutex_unlock(&osdc->request_mutex);
  1649. if (needmap) {
  1650. dout("%d requests for down osds, need new map\n", needmap);
  1651. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1652. }
  1653. }
  1654. /*
  1655. * Process updated osd map.
  1656. *
  1657. * The message contains any number of incremental and full maps, normally
  1658. * indicating some sort of topology change in the cluster. Kick requests
  1659. * off to different OSDs as needed.
  1660. */
  1661. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1662. {
  1663. void *p, *end, *next;
  1664. u32 nr_maps, maplen;
  1665. u32 epoch;
  1666. struct ceph_osdmap *newmap = NULL, *oldmap;
  1667. int err;
  1668. struct ceph_fsid fsid;
  1669. bool was_full;
  1670. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1671. p = msg->front.iov_base;
  1672. end = p + msg->front.iov_len;
  1673. /* verify fsid */
  1674. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1675. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1676. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1677. return;
  1678. down_write(&osdc->map_sem);
  1679. was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
  1680. /* incremental maps */
  1681. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1682. dout(" %d inc maps\n", nr_maps);
  1683. while (nr_maps > 0) {
  1684. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1685. epoch = ceph_decode_32(&p);
  1686. maplen = ceph_decode_32(&p);
  1687. ceph_decode_need(&p, end, maplen, bad);
  1688. next = p + maplen;
  1689. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1690. dout("applying incremental map %u len %d\n",
  1691. epoch, maplen);
  1692. newmap = osdmap_apply_incremental(&p, next,
  1693. osdc->osdmap,
  1694. &osdc->client->msgr);
  1695. if (IS_ERR(newmap)) {
  1696. err = PTR_ERR(newmap);
  1697. goto bad;
  1698. }
  1699. BUG_ON(!newmap);
  1700. if (newmap != osdc->osdmap) {
  1701. ceph_osdmap_destroy(osdc->osdmap);
  1702. osdc->osdmap = newmap;
  1703. }
  1704. was_full = was_full ||
  1705. ceph_osdmap_flag(osdc->osdmap,
  1706. CEPH_OSDMAP_FULL);
  1707. kick_requests(osdc, 0, was_full);
  1708. } else {
  1709. dout("ignoring incremental map %u len %d\n",
  1710. epoch, maplen);
  1711. }
  1712. p = next;
  1713. nr_maps--;
  1714. }
  1715. if (newmap)
  1716. goto done;
  1717. /* full maps */
  1718. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1719. dout(" %d full maps\n", nr_maps);
  1720. while (nr_maps) {
  1721. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1722. epoch = ceph_decode_32(&p);
  1723. maplen = ceph_decode_32(&p);
  1724. ceph_decode_need(&p, end, maplen, bad);
  1725. if (nr_maps > 1) {
  1726. dout("skipping non-latest full map %u len %d\n",
  1727. epoch, maplen);
  1728. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1729. dout("skipping full map %u len %d, "
  1730. "older than our %u\n", epoch, maplen,
  1731. osdc->osdmap->epoch);
  1732. } else {
  1733. int skipped_map = 0;
  1734. dout("taking full map %u len %d\n", epoch, maplen);
  1735. newmap = osdmap_decode(&p, p+maplen);
  1736. if (IS_ERR(newmap)) {
  1737. err = PTR_ERR(newmap);
  1738. goto bad;
  1739. }
  1740. BUG_ON(!newmap);
  1741. oldmap = osdc->osdmap;
  1742. osdc->osdmap = newmap;
  1743. if (oldmap) {
  1744. if (oldmap->epoch + 1 < newmap->epoch)
  1745. skipped_map = 1;
  1746. ceph_osdmap_destroy(oldmap);
  1747. }
  1748. was_full = was_full ||
  1749. ceph_osdmap_flag(osdc->osdmap,
  1750. CEPH_OSDMAP_FULL);
  1751. kick_requests(osdc, skipped_map, was_full);
  1752. }
  1753. p += maplen;
  1754. nr_maps--;
  1755. }
  1756. if (!osdc->osdmap)
  1757. goto bad;
  1758. done:
  1759. downgrade_write(&osdc->map_sem);
  1760. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1761. /*
  1762. * subscribe to subsequent osdmap updates if full to ensure
  1763. * we find out when we are no longer full and stop returning
  1764. * ENOSPC.
  1765. */
  1766. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
  1767. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
  1768. ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
  1769. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1770. mutex_lock(&osdc->request_mutex);
  1771. __send_queued(osdc);
  1772. mutex_unlock(&osdc->request_mutex);
  1773. up_read(&osdc->map_sem);
  1774. wake_up_all(&osdc->client->auth_wq);
  1775. return;
  1776. bad:
  1777. pr_err("osdc handle_map corrupt msg\n");
  1778. ceph_msg_dump(msg);
  1779. up_write(&osdc->map_sem);
  1780. return;
  1781. }
  1782. /*
  1783. * watch/notify callback event infrastructure
  1784. *
  1785. * These callbacks are used both for watch and notify operations.
  1786. */
  1787. static void __release_event(struct kref *kref)
  1788. {
  1789. struct ceph_osd_event *event =
  1790. container_of(kref, struct ceph_osd_event, kref);
  1791. dout("__release_event %p\n", event);
  1792. kfree(event);
  1793. }
  1794. static void get_event(struct ceph_osd_event *event)
  1795. {
  1796. kref_get(&event->kref);
  1797. }
  1798. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1799. {
  1800. kref_put(&event->kref, __release_event);
  1801. }
  1802. EXPORT_SYMBOL(ceph_osdc_put_event);
  1803. static void __insert_event(struct ceph_osd_client *osdc,
  1804. struct ceph_osd_event *new)
  1805. {
  1806. struct rb_node **p = &osdc->event_tree.rb_node;
  1807. struct rb_node *parent = NULL;
  1808. struct ceph_osd_event *event = NULL;
  1809. while (*p) {
  1810. parent = *p;
  1811. event = rb_entry(parent, struct ceph_osd_event, node);
  1812. if (new->cookie < event->cookie)
  1813. p = &(*p)->rb_left;
  1814. else if (new->cookie > event->cookie)
  1815. p = &(*p)->rb_right;
  1816. else
  1817. BUG();
  1818. }
  1819. rb_link_node(&new->node, parent, p);
  1820. rb_insert_color(&new->node, &osdc->event_tree);
  1821. }
  1822. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1823. u64 cookie)
  1824. {
  1825. struct rb_node **p = &osdc->event_tree.rb_node;
  1826. struct rb_node *parent = NULL;
  1827. struct ceph_osd_event *event = NULL;
  1828. while (*p) {
  1829. parent = *p;
  1830. event = rb_entry(parent, struct ceph_osd_event, node);
  1831. if (cookie < event->cookie)
  1832. p = &(*p)->rb_left;
  1833. else if (cookie > event->cookie)
  1834. p = &(*p)->rb_right;
  1835. else
  1836. return event;
  1837. }
  1838. return NULL;
  1839. }
  1840. static void __remove_event(struct ceph_osd_event *event)
  1841. {
  1842. struct ceph_osd_client *osdc = event->osdc;
  1843. if (!RB_EMPTY_NODE(&event->node)) {
  1844. dout("__remove_event removed %p\n", event);
  1845. rb_erase(&event->node, &osdc->event_tree);
  1846. ceph_osdc_put_event(event);
  1847. } else {
  1848. dout("__remove_event didn't remove %p\n", event);
  1849. }
  1850. }
  1851. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1852. void (*event_cb)(u64, u64, u8, void *),
  1853. void *data, struct ceph_osd_event **pevent)
  1854. {
  1855. struct ceph_osd_event *event;
  1856. event = kmalloc(sizeof(*event), GFP_NOIO);
  1857. if (!event)
  1858. return -ENOMEM;
  1859. dout("create_event %p\n", event);
  1860. event->cb = event_cb;
  1861. event->one_shot = 0;
  1862. event->data = data;
  1863. event->osdc = osdc;
  1864. INIT_LIST_HEAD(&event->osd_node);
  1865. RB_CLEAR_NODE(&event->node);
  1866. kref_init(&event->kref); /* one ref for us */
  1867. kref_get(&event->kref); /* one ref for the caller */
  1868. spin_lock(&osdc->event_lock);
  1869. event->cookie = ++osdc->event_count;
  1870. __insert_event(osdc, event);
  1871. spin_unlock(&osdc->event_lock);
  1872. *pevent = event;
  1873. return 0;
  1874. }
  1875. EXPORT_SYMBOL(ceph_osdc_create_event);
  1876. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1877. {
  1878. struct ceph_osd_client *osdc = event->osdc;
  1879. dout("cancel_event %p\n", event);
  1880. spin_lock(&osdc->event_lock);
  1881. __remove_event(event);
  1882. spin_unlock(&osdc->event_lock);
  1883. ceph_osdc_put_event(event); /* caller's */
  1884. }
  1885. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1886. static void do_event_work(struct work_struct *work)
  1887. {
  1888. struct ceph_osd_event_work *event_work =
  1889. container_of(work, struct ceph_osd_event_work, work);
  1890. struct ceph_osd_event *event = event_work->event;
  1891. u64 ver = event_work->ver;
  1892. u64 notify_id = event_work->notify_id;
  1893. u8 opcode = event_work->opcode;
  1894. dout("do_event_work completing %p\n", event);
  1895. event->cb(ver, notify_id, opcode, event->data);
  1896. dout("do_event_work completed %p\n", event);
  1897. ceph_osdc_put_event(event);
  1898. kfree(event_work);
  1899. }
  1900. /*
  1901. * Process osd watch notifications
  1902. */
  1903. static void handle_watch_notify(struct ceph_osd_client *osdc,
  1904. struct ceph_msg *msg)
  1905. {
  1906. void *p, *end;
  1907. u8 proto_ver;
  1908. u64 cookie, ver, notify_id;
  1909. u8 opcode;
  1910. struct ceph_osd_event *event;
  1911. struct ceph_osd_event_work *event_work;
  1912. p = msg->front.iov_base;
  1913. end = p + msg->front.iov_len;
  1914. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1915. ceph_decode_8_safe(&p, end, opcode, bad);
  1916. ceph_decode_64_safe(&p, end, cookie, bad);
  1917. ceph_decode_64_safe(&p, end, ver, bad);
  1918. ceph_decode_64_safe(&p, end, notify_id, bad);
  1919. spin_lock(&osdc->event_lock);
  1920. event = __find_event(osdc, cookie);
  1921. if (event) {
  1922. BUG_ON(event->one_shot);
  1923. get_event(event);
  1924. }
  1925. spin_unlock(&osdc->event_lock);
  1926. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1927. cookie, ver, event);
  1928. if (event) {
  1929. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1930. if (!event_work) {
  1931. dout("ERROR: could not allocate event_work\n");
  1932. goto done_err;
  1933. }
  1934. INIT_WORK(&event_work->work, do_event_work);
  1935. event_work->event = event;
  1936. event_work->ver = ver;
  1937. event_work->notify_id = notify_id;
  1938. event_work->opcode = opcode;
  1939. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1940. dout("WARNING: failed to queue notify event work\n");
  1941. goto done_err;
  1942. }
  1943. }
  1944. return;
  1945. done_err:
  1946. ceph_osdc_put_event(event);
  1947. return;
  1948. bad:
  1949. pr_err("osdc handle_watch_notify corrupt msg\n");
  1950. return;
  1951. }
  1952. /*
  1953. * build new request AND message
  1954. *
  1955. */
  1956. void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
  1957. struct ceph_snap_context *snapc, u64 snap_id,
  1958. struct timespec *mtime)
  1959. {
  1960. struct ceph_msg *msg = req->r_request;
  1961. void *p;
  1962. size_t msg_size;
  1963. int flags = req->r_flags;
  1964. u64 data_len;
  1965. unsigned int i;
  1966. req->r_snapid = snap_id;
  1967. req->r_snapc = ceph_get_snap_context(snapc);
  1968. /* encode request */
  1969. msg->hdr.version = cpu_to_le16(4);
  1970. p = msg->front.iov_base;
  1971. ceph_encode_32(&p, 1); /* client_inc is always 1 */
  1972. req->r_request_osdmap_epoch = p;
  1973. p += 4;
  1974. req->r_request_flags = p;
  1975. p += 4;
  1976. if (req->r_flags & CEPH_OSD_FLAG_WRITE)
  1977. ceph_encode_timespec(p, mtime);
  1978. p += sizeof(struct ceph_timespec);
  1979. req->r_request_reassert_version = p;
  1980. p += sizeof(struct ceph_eversion); /* will get filled in */
  1981. /* oloc */
  1982. ceph_encode_8(&p, 4);
  1983. ceph_encode_8(&p, 4);
  1984. ceph_encode_32(&p, 8 + 4 + 4);
  1985. req->r_request_pool = p;
  1986. p += 8;
  1987. ceph_encode_32(&p, -1); /* preferred */
  1988. ceph_encode_32(&p, 0); /* key len */
  1989. ceph_encode_8(&p, 1);
  1990. req->r_request_pgid = p;
  1991. p += 8 + 4;
  1992. ceph_encode_32(&p, -1); /* preferred */
  1993. /* oid */
  1994. ceph_encode_32(&p, req->r_base_oid.name_len);
  1995. memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
  1996. dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
  1997. req->r_base_oid.name, req->r_base_oid.name_len);
  1998. p += req->r_base_oid.name_len;
  1999. /* ops--can imply data */
  2000. ceph_encode_16(&p, (u16)req->r_num_ops);
  2001. data_len = 0;
  2002. for (i = 0; i < req->r_num_ops; i++) {
  2003. data_len += osd_req_encode_op(req, p, i);
  2004. p += sizeof(struct ceph_osd_op);
  2005. }
  2006. /* snaps */
  2007. ceph_encode_64(&p, req->r_snapid);
  2008. ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
  2009. ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
  2010. if (req->r_snapc) {
  2011. for (i = 0; i < snapc->num_snaps; i++) {
  2012. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  2013. }
  2014. }
  2015. req->r_request_attempts = p;
  2016. p += 4;
  2017. /* data */
  2018. if (flags & CEPH_OSD_FLAG_WRITE) {
  2019. u16 data_off;
  2020. /*
  2021. * The header "data_off" is a hint to the receiver
  2022. * allowing it to align received data into its
  2023. * buffers such that there's no need to re-copy
  2024. * it before writing it to disk (direct I/O).
  2025. */
  2026. data_off = (u16) (off & 0xffff);
  2027. req->r_request->hdr.data_off = cpu_to_le16(data_off);
  2028. }
  2029. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  2030. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  2031. msg_size = p - msg->front.iov_base;
  2032. msg->front.iov_len = msg_size;
  2033. msg->hdr.front_len = cpu_to_le32(msg_size);
  2034. dout("build_request msg_size was %d\n", (int)msg_size);
  2035. }
  2036. EXPORT_SYMBOL(ceph_osdc_build_request);
  2037. /*
  2038. * Register request, send initial attempt.
  2039. */
  2040. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  2041. struct ceph_osd_request *req,
  2042. bool nofail)
  2043. {
  2044. int rc = 0;
  2045. down_read(&osdc->map_sem);
  2046. mutex_lock(&osdc->request_mutex);
  2047. __register_request(osdc, req);
  2048. req->r_sent = 0;
  2049. req->r_got_reply = 0;
  2050. rc = __map_request(osdc, req, 0);
  2051. if (rc < 0) {
  2052. if (nofail) {
  2053. dout("osdc_start_request failed map, "
  2054. " will retry %lld\n", req->r_tid);
  2055. rc = 0;
  2056. } else {
  2057. __unregister_request(osdc, req);
  2058. }
  2059. goto out_unlock;
  2060. }
  2061. if (req->r_osd == NULL) {
  2062. dout("send_request %p no up osds in pg\n", req);
  2063. ceph_monc_request_next_osdmap(&osdc->client->monc);
  2064. } else {
  2065. __send_queued(osdc);
  2066. }
  2067. rc = 0;
  2068. out_unlock:
  2069. mutex_unlock(&osdc->request_mutex);
  2070. up_read(&osdc->map_sem);
  2071. return rc;
  2072. }
  2073. EXPORT_SYMBOL(ceph_osdc_start_request);
  2074. /*
  2075. * wait for a request to complete
  2076. */
  2077. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  2078. struct ceph_osd_request *req)
  2079. {
  2080. int rc;
  2081. rc = wait_for_completion_interruptible(&req->r_completion);
  2082. if (rc < 0) {
  2083. mutex_lock(&osdc->request_mutex);
  2084. __cancel_request(req);
  2085. __unregister_request(osdc, req);
  2086. mutex_unlock(&osdc->request_mutex);
  2087. complete_request(req);
  2088. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  2089. return rc;
  2090. }
  2091. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  2092. return req->r_result;
  2093. }
  2094. EXPORT_SYMBOL(ceph_osdc_wait_request);
  2095. /*
  2096. * sync - wait for all in-flight requests to flush. avoid starvation.
  2097. */
  2098. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  2099. {
  2100. struct ceph_osd_request *req;
  2101. u64 last_tid, next_tid = 0;
  2102. mutex_lock(&osdc->request_mutex);
  2103. last_tid = osdc->last_tid;
  2104. while (1) {
  2105. req = __lookup_request_ge(osdc, next_tid);
  2106. if (!req)
  2107. break;
  2108. if (req->r_tid > last_tid)
  2109. break;
  2110. next_tid = req->r_tid + 1;
  2111. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  2112. continue;
  2113. ceph_osdc_get_request(req);
  2114. mutex_unlock(&osdc->request_mutex);
  2115. dout("sync waiting on tid %llu (last is %llu)\n",
  2116. req->r_tid, last_tid);
  2117. wait_for_completion(&req->r_safe_completion);
  2118. mutex_lock(&osdc->request_mutex);
  2119. ceph_osdc_put_request(req);
  2120. }
  2121. mutex_unlock(&osdc->request_mutex);
  2122. dout("sync done (thru tid %llu)\n", last_tid);
  2123. }
  2124. EXPORT_SYMBOL(ceph_osdc_sync);
  2125. /*
  2126. * Call all pending notify callbacks - for use after a watch is
  2127. * unregistered, to make sure no more callbacks for it will be invoked
  2128. */
  2129. extern void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
  2130. {
  2131. flush_workqueue(osdc->notify_wq);
  2132. }
  2133. EXPORT_SYMBOL(ceph_osdc_flush_notifies);
  2134. /*
  2135. * init, shutdown
  2136. */
  2137. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  2138. {
  2139. int err;
  2140. dout("init\n");
  2141. osdc->client = client;
  2142. osdc->osdmap = NULL;
  2143. init_rwsem(&osdc->map_sem);
  2144. init_completion(&osdc->map_waiters);
  2145. osdc->last_requested_map = 0;
  2146. mutex_init(&osdc->request_mutex);
  2147. osdc->last_tid = 0;
  2148. osdc->osds = RB_ROOT;
  2149. INIT_LIST_HEAD(&osdc->osd_lru);
  2150. osdc->requests = RB_ROOT;
  2151. INIT_LIST_HEAD(&osdc->req_lru);
  2152. INIT_LIST_HEAD(&osdc->req_unsent);
  2153. INIT_LIST_HEAD(&osdc->req_notarget);
  2154. INIT_LIST_HEAD(&osdc->req_linger);
  2155. osdc->num_requests = 0;
  2156. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  2157. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  2158. spin_lock_init(&osdc->event_lock);
  2159. osdc->event_tree = RB_ROOT;
  2160. osdc->event_count = 0;
  2161. schedule_delayed_work(&osdc->osds_timeout_work,
  2162. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  2163. err = -ENOMEM;
  2164. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  2165. sizeof(struct ceph_osd_request));
  2166. if (!osdc->req_mempool)
  2167. goto out;
  2168. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  2169. OSD_OP_FRONT_LEN, 10, true,
  2170. "osd_op");
  2171. if (err < 0)
  2172. goto out_mempool;
  2173. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  2174. OSD_OPREPLY_FRONT_LEN, 10, true,
  2175. "osd_op_reply");
  2176. if (err < 0)
  2177. goto out_msgpool;
  2178. err = -ENOMEM;
  2179. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  2180. if (!osdc->notify_wq)
  2181. goto out_msgpool;
  2182. return 0;
  2183. out_msgpool:
  2184. ceph_msgpool_destroy(&osdc->msgpool_op);
  2185. out_mempool:
  2186. mempool_destroy(osdc->req_mempool);
  2187. out:
  2188. return err;
  2189. }
  2190. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  2191. {
  2192. flush_workqueue(osdc->notify_wq);
  2193. destroy_workqueue(osdc->notify_wq);
  2194. cancel_delayed_work_sync(&osdc->timeout_work);
  2195. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  2196. if (osdc->osdmap) {
  2197. ceph_osdmap_destroy(osdc->osdmap);
  2198. osdc->osdmap = NULL;
  2199. }
  2200. remove_all_osds(osdc);
  2201. mempool_destroy(osdc->req_mempool);
  2202. ceph_msgpool_destroy(&osdc->msgpool_op);
  2203. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  2204. }
  2205. /*
  2206. * Read some contiguous pages. If we cross a stripe boundary, shorten
  2207. * *plen. Return number of bytes read, or error.
  2208. */
  2209. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  2210. struct ceph_vino vino, struct ceph_file_layout *layout,
  2211. u64 off, u64 *plen,
  2212. u32 truncate_seq, u64 truncate_size,
  2213. struct page **pages, int num_pages, int page_align)
  2214. {
  2215. struct ceph_osd_request *req;
  2216. int rc = 0;
  2217. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  2218. vino.snap, off, *plen);
  2219. req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
  2220. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  2221. NULL, truncate_seq, truncate_size,
  2222. false);
  2223. if (IS_ERR(req))
  2224. return PTR_ERR(req);
  2225. /* it may be a short read due to an object boundary */
  2226. osd_req_op_extent_osd_data_pages(req, 0,
  2227. pages, *plen, page_align, false, false);
  2228. dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
  2229. off, *plen, *plen, page_align);
  2230. ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
  2231. rc = ceph_osdc_start_request(osdc, req, false);
  2232. if (!rc)
  2233. rc = ceph_osdc_wait_request(osdc, req);
  2234. ceph_osdc_put_request(req);
  2235. dout("readpages result %d\n", rc);
  2236. return rc;
  2237. }
  2238. EXPORT_SYMBOL(ceph_osdc_readpages);
  2239. /*
  2240. * do a synchronous write on N pages
  2241. */
  2242. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  2243. struct ceph_file_layout *layout,
  2244. struct ceph_snap_context *snapc,
  2245. u64 off, u64 len,
  2246. u32 truncate_seq, u64 truncate_size,
  2247. struct timespec *mtime,
  2248. struct page **pages, int num_pages)
  2249. {
  2250. struct ceph_osd_request *req;
  2251. int rc = 0;
  2252. int page_align = off & ~PAGE_MASK;
  2253. BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
  2254. req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
  2255. CEPH_OSD_OP_WRITE,
  2256. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  2257. snapc, truncate_seq, truncate_size,
  2258. true);
  2259. if (IS_ERR(req))
  2260. return PTR_ERR(req);
  2261. /* it may be a short write due to an object boundary */
  2262. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  2263. false, false);
  2264. dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
  2265. ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
  2266. rc = ceph_osdc_start_request(osdc, req, true);
  2267. if (!rc)
  2268. rc = ceph_osdc_wait_request(osdc, req);
  2269. ceph_osdc_put_request(req);
  2270. if (rc == 0)
  2271. rc = len;
  2272. dout("writepages result %d\n", rc);
  2273. return rc;
  2274. }
  2275. EXPORT_SYMBOL(ceph_osdc_writepages);
  2276. int ceph_osdc_setup(void)
  2277. {
  2278. BUG_ON(ceph_osd_request_cache);
  2279. ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
  2280. sizeof (struct ceph_osd_request),
  2281. __alignof__(struct ceph_osd_request),
  2282. 0, NULL);
  2283. return ceph_osd_request_cache ? 0 : -ENOMEM;
  2284. }
  2285. EXPORT_SYMBOL(ceph_osdc_setup);
  2286. void ceph_osdc_cleanup(void)
  2287. {
  2288. BUG_ON(!ceph_osd_request_cache);
  2289. kmem_cache_destroy(ceph_osd_request_cache);
  2290. ceph_osd_request_cache = NULL;
  2291. }
  2292. EXPORT_SYMBOL(ceph_osdc_cleanup);
  2293. /*
  2294. * handle incoming message
  2295. */
  2296. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  2297. {
  2298. struct ceph_osd *osd = con->private;
  2299. struct ceph_osd_client *osdc;
  2300. int type = le16_to_cpu(msg->hdr.type);
  2301. if (!osd)
  2302. goto out;
  2303. osdc = osd->o_osdc;
  2304. switch (type) {
  2305. case CEPH_MSG_OSD_MAP:
  2306. ceph_osdc_handle_map(osdc, msg);
  2307. break;
  2308. case CEPH_MSG_OSD_OPREPLY:
  2309. handle_reply(osdc, msg, con);
  2310. break;
  2311. case CEPH_MSG_WATCH_NOTIFY:
  2312. handle_watch_notify(osdc, msg);
  2313. break;
  2314. default:
  2315. pr_err("received unknown message type %d %s\n", type,
  2316. ceph_msg_type_name(type));
  2317. }
  2318. out:
  2319. ceph_msg_put(msg);
  2320. }
  2321. /*
  2322. * lookup and return message for incoming reply. set up reply message
  2323. * pages.
  2324. */
  2325. static struct ceph_msg *get_reply(struct ceph_connection *con,
  2326. struct ceph_msg_header *hdr,
  2327. int *skip)
  2328. {
  2329. struct ceph_osd *osd = con->private;
  2330. struct ceph_osd_client *osdc = osd->o_osdc;
  2331. struct ceph_msg *m;
  2332. struct ceph_osd_request *req;
  2333. int front_len = le32_to_cpu(hdr->front_len);
  2334. int data_len = le32_to_cpu(hdr->data_len);
  2335. u64 tid;
  2336. tid = le64_to_cpu(hdr->tid);
  2337. mutex_lock(&osdc->request_mutex);
  2338. req = __lookup_request(osdc, tid);
  2339. if (!req) {
  2340. *skip = 1;
  2341. m = NULL;
  2342. dout("get_reply unknown tid %llu from osd%d\n", tid,
  2343. osd->o_osd);
  2344. goto out;
  2345. }
  2346. if (req->r_reply->con)
  2347. dout("%s revoking msg %p from old con %p\n", __func__,
  2348. req->r_reply, req->r_reply->con);
  2349. ceph_msg_revoke_incoming(req->r_reply);
  2350. if (front_len > req->r_reply->front_alloc_len) {
  2351. pr_warning("get_reply front %d > preallocated %d (%u#%llu)\n",
  2352. front_len, req->r_reply->front_alloc_len,
  2353. (unsigned int)con->peer_name.type,
  2354. le64_to_cpu(con->peer_name.num));
  2355. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
  2356. false);
  2357. if (!m)
  2358. goto out;
  2359. ceph_msg_put(req->r_reply);
  2360. req->r_reply = m;
  2361. }
  2362. m = ceph_msg_get(req->r_reply);
  2363. if (data_len > 0) {
  2364. struct ceph_osd_data *osd_data;
  2365. /*
  2366. * XXX This is assuming there is only one op containing
  2367. * XXX page data. Probably OK for reads, but this
  2368. * XXX ought to be done more generally.
  2369. */
  2370. osd_data = osd_req_op_extent_osd_data(req, 0);
  2371. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  2372. if (osd_data->pages &&
  2373. unlikely(osd_data->length < data_len)) {
  2374. pr_warning("tid %lld reply has %d bytes "
  2375. "we had only %llu bytes ready\n",
  2376. tid, data_len, osd_data->length);
  2377. *skip = 1;
  2378. ceph_msg_put(m);
  2379. m = NULL;
  2380. goto out;
  2381. }
  2382. }
  2383. }
  2384. *skip = 0;
  2385. dout("get_reply tid %lld %p\n", tid, m);
  2386. out:
  2387. mutex_unlock(&osdc->request_mutex);
  2388. return m;
  2389. }
  2390. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  2391. struct ceph_msg_header *hdr,
  2392. int *skip)
  2393. {
  2394. struct ceph_osd *osd = con->private;
  2395. int type = le16_to_cpu(hdr->type);
  2396. int front = le32_to_cpu(hdr->front_len);
  2397. *skip = 0;
  2398. switch (type) {
  2399. case CEPH_MSG_OSD_MAP:
  2400. case CEPH_MSG_WATCH_NOTIFY:
  2401. return ceph_msg_new(type, front, GFP_NOFS, false);
  2402. case CEPH_MSG_OSD_OPREPLY:
  2403. return get_reply(con, hdr, skip);
  2404. default:
  2405. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  2406. osd->o_osd);
  2407. *skip = 1;
  2408. return NULL;
  2409. }
  2410. }
  2411. /*
  2412. * Wrappers to refcount containing ceph_osd struct
  2413. */
  2414. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  2415. {
  2416. struct ceph_osd *osd = con->private;
  2417. if (get_osd(osd))
  2418. return con;
  2419. return NULL;
  2420. }
  2421. static void put_osd_con(struct ceph_connection *con)
  2422. {
  2423. struct ceph_osd *osd = con->private;
  2424. put_osd(osd);
  2425. }
  2426. /*
  2427. * authentication
  2428. */
  2429. /*
  2430. * Note: returned pointer is the address of a structure that's
  2431. * managed separately. Caller must *not* attempt to free it.
  2432. */
  2433. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  2434. int *proto, int force_new)
  2435. {
  2436. struct ceph_osd *o = con->private;
  2437. struct ceph_osd_client *osdc = o->o_osdc;
  2438. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2439. struct ceph_auth_handshake *auth = &o->o_auth;
  2440. if (force_new && auth->authorizer) {
  2441. ceph_auth_destroy_authorizer(ac, auth->authorizer);
  2442. auth->authorizer = NULL;
  2443. }
  2444. if (!auth->authorizer) {
  2445. int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2446. auth);
  2447. if (ret)
  2448. return ERR_PTR(ret);
  2449. } else {
  2450. int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2451. auth);
  2452. if (ret)
  2453. return ERR_PTR(ret);
  2454. }
  2455. *proto = ac->protocol;
  2456. return auth;
  2457. }
  2458. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  2459. {
  2460. struct ceph_osd *o = con->private;
  2461. struct ceph_osd_client *osdc = o->o_osdc;
  2462. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2463. return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  2464. }
  2465. static int invalidate_authorizer(struct ceph_connection *con)
  2466. {
  2467. struct ceph_osd *o = con->private;
  2468. struct ceph_osd_client *osdc = o->o_osdc;
  2469. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2470. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  2471. return ceph_monc_validate_auth(&osdc->client->monc);
  2472. }
  2473. static const struct ceph_connection_operations osd_con_ops = {
  2474. .get = get_osd_con,
  2475. .put = put_osd_con,
  2476. .dispatch = dispatch,
  2477. .get_authorizer = get_authorizer,
  2478. .verify_authorizer_reply = verify_authorizer_reply,
  2479. .invalidate_authorizer = invalidate_authorizer,
  2480. .alloc_msg = alloc_msg,
  2481. .fault = osd_reset,
  2482. };