osd_client.c 67 KB

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