osd_client.c 66 KB

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