osd_client.c 67 KB

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