osd_client.c 68 KB

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