osd_client.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605
  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. req->r_linger = 0;
  1051. ceph_osdc_put_request(req);
  1052. }
  1053. mutex_unlock(&osdc->request_mutex);
  1054. }
  1055. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  1056. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  1057. struct ceph_osd_request *req)
  1058. {
  1059. if (!req->r_linger) {
  1060. dout("set_request_linger %p\n", req);
  1061. req->r_linger = 1;
  1062. /*
  1063. * caller is now responsible for calling
  1064. * unregister_linger_request
  1065. */
  1066. ceph_osdc_get_request(req);
  1067. }
  1068. }
  1069. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  1070. /*
  1071. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  1072. * (as needed), and set the request r_osd appropriately. If there is
  1073. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  1074. * (unsent, homeless) or leave on in-flight lru.
  1075. *
  1076. * Return 0 if unchanged, 1 if changed, or negative on error.
  1077. *
  1078. * Caller should hold map_sem for read and request_mutex.
  1079. */
  1080. static int __map_request(struct ceph_osd_client *osdc,
  1081. struct ceph_osd_request *req, int force_resend)
  1082. {
  1083. struct ceph_pg pgid;
  1084. int acting[CEPH_PG_MAX_SIZE];
  1085. int o = -1, num = 0;
  1086. int err;
  1087. dout("map_request %p tid %lld\n", req, req->r_tid);
  1088. err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
  1089. ceph_file_layout_pg_pool(req->r_file_layout));
  1090. if (err) {
  1091. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  1092. return err;
  1093. }
  1094. req->r_pgid = pgid;
  1095. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  1096. if (err > 0) {
  1097. o = acting[0];
  1098. num = err;
  1099. }
  1100. if ((!force_resend &&
  1101. req->r_osd && req->r_osd->o_osd == o &&
  1102. req->r_sent >= req->r_osd->o_incarnation &&
  1103. req->r_num_pg_osds == num &&
  1104. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  1105. (req->r_osd == NULL && o == -1))
  1106. return 0; /* no change */
  1107. dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
  1108. req->r_tid, pgid.pool, pgid.seed, o,
  1109. req->r_osd ? req->r_osd->o_osd : -1);
  1110. /* record full pg acting set */
  1111. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  1112. req->r_num_pg_osds = num;
  1113. if (req->r_osd) {
  1114. __cancel_request(req);
  1115. list_del_init(&req->r_osd_item);
  1116. req->r_osd = NULL;
  1117. }
  1118. req->r_osd = __lookup_osd(osdc, o);
  1119. if (!req->r_osd && o >= 0) {
  1120. err = -ENOMEM;
  1121. req->r_osd = create_osd(osdc, o);
  1122. if (!req->r_osd) {
  1123. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  1124. goto out;
  1125. }
  1126. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  1127. __insert_osd(osdc, req->r_osd);
  1128. ceph_con_open(&req->r_osd->o_con,
  1129. CEPH_ENTITY_TYPE_OSD, o,
  1130. &osdc->osdmap->osd_addr[o]);
  1131. }
  1132. if (req->r_osd) {
  1133. __remove_osd_from_lru(req->r_osd);
  1134. list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
  1135. list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
  1136. } else {
  1137. list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
  1138. }
  1139. err = 1; /* osd or pg changed */
  1140. out:
  1141. return err;
  1142. }
  1143. /*
  1144. * caller should hold map_sem (for read) and request_mutex
  1145. */
  1146. static void __send_request(struct ceph_osd_client *osdc,
  1147. struct ceph_osd_request *req)
  1148. {
  1149. void *p;
  1150. dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
  1151. req, req->r_tid, req->r_osd->o_osd, req->r_flags,
  1152. (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
  1153. /* fill in message content that changes each time we send it */
  1154. put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
  1155. put_unaligned_le32(req->r_flags, req->r_request_flags);
  1156. put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
  1157. p = req->r_request_pgid;
  1158. ceph_encode_64(&p, req->r_pgid.pool);
  1159. ceph_encode_32(&p, req->r_pgid.seed);
  1160. put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
  1161. memcpy(req->r_request_reassert_version, &req->r_reassert_version,
  1162. sizeof(req->r_reassert_version));
  1163. req->r_stamp = jiffies;
  1164. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  1165. ceph_msg_get(req->r_request); /* send consumes a ref */
  1166. /* Mark the request unsafe if this is the first timet's being sent. */
  1167. if (!req->r_sent && req->r_unsafe_callback)
  1168. req->r_unsafe_callback(req, true);
  1169. req->r_sent = req->r_osd->o_incarnation;
  1170. ceph_con_send(&req->r_osd->o_con, req->r_request);
  1171. }
  1172. /*
  1173. * Send any requests in the queue (req_unsent).
  1174. */
  1175. static void __send_queued(struct ceph_osd_client *osdc)
  1176. {
  1177. struct ceph_osd_request *req, *tmp;
  1178. dout("__send_queued\n");
  1179. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
  1180. __send_request(osdc, req);
  1181. }
  1182. /*
  1183. * Timeout callback, called every N seconds when 1 or more osd
  1184. * requests has been active for more than N seconds. When this
  1185. * happens, we ping all OSDs with requests who have timed out to
  1186. * ensure any communications channel reset is detected. Reset the
  1187. * request timeouts another N seconds in the future as we go.
  1188. * Reschedule the timeout event another N seconds in future (unless
  1189. * there are no open requests).
  1190. */
  1191. static void handle_timeout(struct work_struct *work)
  1192. {
  1193. struct ceph_osd_client *osdc =
  1194. container_of(work, struct ceph_osd_client, timeout_work.work);
  1195. struct ceph_osd_request *req;
  1196. struct ceph_osd *osd;
  1197. unsigned long keepalive =
  1198. osdc->client->options->osd_keepalive_timeout * HZ;
  1199. struct list_head slow_osds;
  1200. dout("timeout\n");
  1201. down_read(&osdc->map_sem);
  1202. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1203. mutex_lock(&osdc->request_mutex);
  1204. /*
  1205. * ping osds that are a bit slow. this ensures that if there
  1206. * is a break in the TCP connection we will notice, and reopen
  1207. * a connection with that osd (from the fault callback).
  1208. */
  1209. INIT_LIST_HEAD(&slow_osds);
  1210. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  1211. if (time_before(jiffies, req->r_stamp + keepalive))
  1212. break;
  1213. osd = req->r_osd;
  1214. BUG_ON(!osd);
  1215. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  1216. req->r_tid, osd->o_osd);
  1217. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  1218. }
  1219. while (!list_empty(&slow_osds)) {
  1220. osd = list_entry(slow_osds.next, struct ceph_osd,
  1221. o_keepalive_item);
  1222. list_del_init(&osd->o_keepalive_item);
  1223. ceph_con_keepalive(&osd->o_con);
  1224. }
  1225. __schedule_osd_timeout(osdc);
  1226. __send_queued(osdc);
  1227. mutex_unlock(&osdc->request_mutex);
  1228. up_read(&osdc->map_sem);
  1229. }
  1230. static void handle_osds_timeout(struct work_struct *work)
  1231. {
  1232. struct ceph_osd_client *osdc =
  1233. container_of(work, struct ceph_osd_client,
  1234. osds_timeout_work.work);
  1235. unsigned long delay =
  1236. osdc->client->options->osd_idle_ttl * HZ >> 2;
  1237. dout("osds timeout\n");
  1238. down_read(&osdc->map_sem);
  1239. remove_old_osds(osdc);
  1240. up_read(&osdc->map_sem);
  1241. schedule_delayed_work(&osdc->osds_timeout_work,
  1242. round_jiffies_relative(delay));
  1243. }
  1244. static void complete_request(struct ceph_osd_request *req)
  1245. {
  1246. if (req->r_unsafe_callback)
  1247. req->r_unsafe_callback(req, false);
  1248. complete_all(&req->r_safe_completion); /* fsync waiter */
  1249. }
  1250. /*
  1251. * handle osd op reply. either call the callback if it is specified,
  1252. * or do the completion to wake up the waiting thread.
  1253. */
  1254. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1255. struct ceph_connection *con)
  1256. {
  1257. void *p, *end;
  1258. struct ceph_osd_request *req;
  1259. u64 tid;
  1260. int object_len;
  1261. unsigned int numops;
  1262. int payload_len, flags;
  1263. s32 result;
  1264. s32 retry_attempt;
  1265. struct ceph_pg pg;
  1266. int err;
  1267. u32 reassert_epoch;
  1268. u64 reassert_version;
  1269. u32 osdmap_epoch;
  1270. int already_completed;
  1271. u32 bytes;
  1272. unsigned int i;
  1273. tid = le64_to_cpu(msg->hdr.tid);
  1274. dout("handle_reply %p tid %llu\n", msg, tid);
  1275. p = msg->front.iov_base;
  1276. end = p + msg->front.iov_len;
  1277. ceph_decode_need(&p, end, 4, bad);
  1278. object_len = ceph_decode_32(&p);
  1279. ceph_decode_need(&p, end, object_len, bad);
  1280. p += object_len;
  1281. err = ceph_decode_pgid(&p, end, &pg);
  1282. if (err)
  1283. goto bad;
  1284. ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
  1285. flags = ceph_decode_64(&p);
  1286. result = ceph_decode_32(&p);
  1287. reassert_epoch = ceph_decode_32(&p);
  1288. reassert_version = ceph_decode_64(&p);
  1289. osdmap_epoch = ceph_decode_32(&p);
  1290. /* lookup */
  1291. mutex_lock(&osdc->request_mutex);
  1292. req = __lookup_request(osdc, tid);
  1293. if (req == NULL) {
  1294. dout("handle_reply tid %llu dne\n", tid);
  1295. goto bad_mutex;
  1296. }
  1297. ceph_osdc_get_request(req);
  1298. dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
  1299. req, result);
  1300. ceph_decode_need(&p, end, 4, bad);
  1301. numops = ceph_decode_32(&p);
  1302. if (numops > CEPH_OSD_MAX_OP)
  1303. goto bad_put;
  1304. if (numops != req->r_num_ops)
  1305. goto bad_put;
  1306. payload_len = 0;
  1307. ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
  1308. for (i = 0; i < numops; i++) {
  1309. struct ceph_osd_op *op = p;
  1310. int len;
  1311. len = le32_to_cpu(op->payload_len);
  1312. req->r_reply_op_len[i] = len;
  1313. dout(" op %d has %d bytes\n", i, len);
  1314. payload_len += len;
  1315. p += sizeof(*op);
  1316. }
  1317. bytes = le32_to_cpu(msg->hdr.data_len);
  1318. if (payload_len != bytes) {
  1319. pr_warning("sum of op payload lens %d != data_len %d",
  1320. payload_len, bytes);
  1321. goto bad_put;
  1322. }
  1323. ceph_decode_need(&p, end, 4 + numops * 4, bad);
  1324. retry_attempt = ceph_decode_32(&p);
  1325. for (i = 0; i < numops; i++)
  1326. req->r_reply_op_result[i] = ceph_decode_32(&p);
  1327. if (!req->r_got_reply) {
  1328. req->r_result = result;
  1329. dout("handle_reply result %d bytes %d\n", req->r_result,
  1330. bytes);
  1331. if (req->r_result == 0)
  1332. req->r_result = bytes;
  1333. /* in case this is a write and we need to replay, */
  1334. req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
  1335. req->r_reassert_version.version = cpu_to_le64(reassert_version);
  1336. req->r_got_reply = 1;
  1337. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1338. dout("handle_reply tid %llu dup ack\n", tid);
  1339. mutex_unlock(&osdc->request_mutex);
  1340. goto done;
  1341. }
  1342. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1343. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1344. __register_linger_request(osdc, req);
  1345. /* either this is a read, or we got the safe response */
  1346. if (result < 0 ||
  1347. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1348. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1349. __unregister_request(osdc, req);
  1350. already_completed = req->r_completed;
  1351. req->r_completed = 1;
  1352. mutex_unlock(&osdc->request_mutex);
  1353. if (already_completed)
  1354. goto done;
  1355. if (req->r_callback)
  1356. req->r_callback(req, msg);
  1357. else
  1358. complete_all(&req->r_completion);
  1359. if (flags & CEPH_OSD_FLAG_ONDISK)
  1360. complete_request(req);
  1361. done:
  1362. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1363. ceph_osdc_put_request(req);
  1364. return;
  1365. bad_put:
  1366. ceph_osdc_put_request(req);
  1367. bad_mutex:
  1368. mutex_unlock(&osdc->request_mutex);
  1369. bad:
  1370. pr_err("corrupt osd_op_reply got %d %d\n",
  1371. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
  1372. ceph_msg_dump(msg);
  1373. }
  1374. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1375. {
  1376. struct rb_node *p, *n;
  1377. for (p = rb_first(&osdc->osds); p; p = n) {
  1378. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1379. n = rb_next(p);
  1380. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1381. memcmp(&osd->o_con.peer_addr,
  1382. ceph_osd_addr(osdc->osdmap,
  1383. osd->o_osd),
  1384. sizeof(struct ceph_entity_addr)) != 0)
  1385. __reset_osd(osdc, osd);
  1386. }
  1387. }
  1388. /*
  1389. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1390. * no osd, request a new map.
  1391. *
  1392. * Caller should hold map_sem for read.
  1393. */
  1394. static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
  1395. {
  1396. struct ceph_osd_request *req, *nreq;
  1397. struct rb_node *p;
  1398. int needmap = 0;
  1399. int err;
  1400. dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
  1401. mutex_lock(&osdc->request_mutex);
  1402. for (p = rb_first(&osdc->requests); p; ) {
  1403. req = rb_entry(p, struct ceph_osd_request, r_node);
  1404. p = rb_next(p);
  1405. /*
  1406. * For linger requests that have not yet been
  1407. * registered, move them to the linger list; they'll
  1408. * be sent to the osd in the loop below. Unregister
  1409. * the request before re-registering it as a linger
  1410. * request to ensure the __map_request() below
  1411. * will decide it needs to be sent.
  1412. */
  1413. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1414. dout("%p tid %llu restart on osd%d\n",
  1415. req, req->r_tid,
  1416. req->r_osd ? req->r_osd->o_osd : -1);
  1417. __unregister_request(osdc, req);
  1418. __register_linger_request(osdc, req);
  1419. continue;
  1420. }
  1421. err = __map_request(osdc, req, force_resend);
  1422. if (err < 0)
  1423. continue; /* error */
  1424. if (req->r_osd == NULL) {
  1425. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1426. needmap++; /* request a newer map */
  1427. } else if (err > 0) {
  1428. if (!req->r_linger) {
  1429. dout("%p tid %llu requeued on osd%d\n", req,
  1430. req->r_tid,
  1431. req->r_osd ? req->r_osd->o_osd : -1);
  1432. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1433. }
  1434. }
  1435. }
  1436. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1437. r_linger_item) {
  1438. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1439. err = __map_request(osdc, req, force_resend);
  1440. dout("__map_request returned %d\n", err);
  1441. if (err == 0)
  1442. continue; /* no change and no osd was specified */
  1443. if (err < 0)
  1444. continue; /* hrm! */
  1445. if (req->r_osd == NULL) {
  1446. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1447. needmap++; /* request a newer map */
  1448. continue;
  1449. }
  1450. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1451. req->r_osd ? req->r_osd->o_osd : -1);
  1452. __register_request(osdc, req);
  1453. __unregister_linger_request(osdc, req);
  1454. }
  1455. reset_changed_osds(osdc);
  1456. mutex_unlock(&osdc->request_mutex);
  1457. if (needmap) {
  1458. dout("%d requests for down osds, need new map\n", needmap);
  1459. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1460. }
  1461. }
  1462. /*
  1463. * Process updated osd map.
  1464. *
  1465. * The message contains any number of incremental and full maps, normally
  1466. * indicating some sort of topology change in the cluster. Kick requests
  1467. * off to different OSDs as needed.
  1468. */
  1469. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1470. {
  1471. void *p, *end, *next;
  1472. u32 nr_maps, maplen;
  1473. u32 epoch;
  1474. struct ceph_osdmap *newmap = NULL, *oldmap;
  1475. int err;
  1476. struct ceph_fsid fsid;
  1477. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1478. p = msg->front.iov_base;
  1479. end = p + msg->front.iov_len;
  1480. /* verify fsid */
  1481. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1482. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1483. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1484. return;
  1485. down_write(&osdc->map_sem);
  1486. /* incremental maps */
  1487. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1488. dout(" %d inc maps\n", nr_maps);
  1489. while (nr_maps > 0) {
  1490. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1491. epoch = ceph_decode_32(&p);
  1492. maplen = ceph_decode_32(&p);
  1493. ceph_decode_need(&p, end, maplen, bad);
  1494. next = p + maplen;
  1495. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1496. dout("applying incremental map %u len %d\n",
  1497. epoch, maplen);
  1498. newmap = osdmap_apply_incremental(&p, next,
  1499. osdc->osdmap,
  1500. &osdc->client->msgr);
  1501. if (IS_ERR(newmap)) {
  1502. err = PTR_ERR(newmap);
  1503. goto bad;
  1504. }
  1505. BUG_ON(!newmap);
  1506. if (newmap != osdc->osdmap) {
  1507. ceph_osdmap_destroy(osdc->osdmap);
  1508. osdc->osdmap = newmap;
  1509. }
  1510. kick_requests(osdc, 0);
  1511. } else {
  1512. dout("ignoring incremental map %u len %d\n",
  1513. epoch, maplen);
  1514. }
  1515. p = next;
  1516. nr_maps--;
  1517. }
  1518. if (newmap)
  1519. goto done;
  1520. /* full maps */
  1521. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1522. dout(" %d full maps\n", nr_maps);
  1523. while (nr_maps) {
  1524. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1525. epoch = ceph_decode_32(&p);
  1526. maplen = ceph_decode_32(&p);
  1527. ceph_decode_need(&p, end, maplen, bad);
  1528. if (nr_maps > 1) {
  1529. dout("skipping non-latest full map %u len %d\n",
  1530. epoch, maplen);
  1531. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1532. dout("skipping full map %u len %d, "
  1533. "older than our %u\n", epoch, maplen,
  1534. osdc->osdmap->epoch);
  1535. } else {
  1536. int skipped_map = 0;
  1537. dout("taking full map %u len %d\n", epoch, maplen);
  1538. newmap = osdmap_decode(&p, p+maplen);
  1539. if (IS_ERR(newmap)) {
  1540. err = PTR_ERR(newmap);
  1541. goto bad;
  1542. }
  1543. BUG_ON(!newmap);
  1544. oldmap = osdc->osdmap;
  1545. osdc->osdmap = newmap;
  1546. if (oldmap) {
  1547. if (oldmap->epoch + 1 < newmap->epoch)
  1548. skipped_map = 1;
  1549. ceph_osdmap_destroy(oldmap);
  1550. }
  1551. kick_requests(osdc, skipped_map);
  1552. }
  1553. p += maplen;
  1554. nr_maps--;
  1555. }
  1556. done:
  1557. downgrade_write(&osdc->map_sem);
  1558. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1559. /*
  1560. * subscribe to subsequent osdmap updates if full to ensure
  1561. * we find out when we are no longer full and stop returning
  1562. * ENOSPC.
  1563. */
  1564. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  1565. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1566. mutex_lock(&osdc->request_mutex);
  1567. __send_queued(osdc);
  1568. mutex_unlock(&osdc->request_mutex);
  1569. up_read(&osdc->map_sem);
  1570. wake_up_all(&osdc->client->auth_wq);
  1571. return;
  1572. bad:
  1573. pr_err("osdc handle_map corrupt msg\n");
  1574. ceph_msg_dump(msg);
  1575. up_write(&osdc->map_sem);
  1576. return;
  1577. }
  1578. /*
  1579. * watch/notify callback event infrastructure
  1580. *
  1581. * These callbacks are used both for watch and notify operations.
  1582. */
  1583. static void __release_event(struct kref *kref)
  1584. {
  1585. struct ceph_osd_event *event =
  1586. container_of(kref, struct ceph_osd_event, kref);
  1587. dout("__release_event %p\n", event);
  1588. kfree(event);
  1589. }
  1590. static void get_event(struct ceph_osd_event *event)
  1591. {
  1592. kref_get(&event->kref);
  1593. }
  1594. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1595. {
  1596. kref_put(&event->kref, __release_event);
  1597. }
  1598. EXPORT_SYMBOL(ceph_osdc_put_event);
  1599. static void __insert_event(struct ceph_osd_client *osdc,
  1600. struct ceph_osd_event *new)
  1601. {
  1602. struct rb_node **p = &osdc->event_tree.rb_node;
  1603. struct rb_node *parent = NULL;
  1604. struct ceph_osd_event *event = NULL;
  1605. while (*p) {
  1606. parent = *p;
  1607. event = rb_entry(parent, struct ceph_osd_event, node);
  1608. if (new->cookie < event->cookie)
  1609. p = &(*p)->rb_left;
  1610. else if (new->cookie > event->cookie)
  1611. p = &(*p)->rb_right;
  1612. else
  1613. BUG();
  1614. }
  1615. rb_link_node(&new->node, parent, p);
  1616. rb_insert_color(&new->node, &osdc->event_tree);
  1617. }
  1618. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1619. u64 cookie)
  1620. {
  1621. struct rb_node **p = &osdc->event_tree.rb_node;
  1622. struct rb_node *parent = NULL;
  1623. struct ceph_osd_event *event = NULL;
  1624. while (*p) {
  1625. parent = *p;
  1626. event = rb_entry(parent, struct ceph_osd_event, node);
  1627. if (cookie < event->cookie)
  1628. p = &(*p)->rb_left;
  1629. else if (cookie > event->cookie)
  1630. p = &(*p)->rb_right;
  1631. else
  1632. return event;
  1633. }
  1634. return NULL;
  1635. }
  1636. static void __remove_event(struct ceph_osd_event *event)
  1637. {
  1638. struct ceph_osd_client *osdc = event->osdc;
  1639. if (!RB_EMPTY_NODE(&event->node)) {
  1640. dout("__remove_event removed %p\n", event);
  1641. rb_erase(&event->node, &osdc->event_tree);
  1642. ceph_osdc_put_event(event);
  1643. } else {
  1644. dout("__remove_event didn't remove %p\n", event);
  1645. }
  1646. }
  1647. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1648. void (*event_cb)(u64, u64, u8, void *),
  1649. void *data, struct ceph_osd_event **pevent)
  1650. {
  1651. struct ceph_osd_event *event;
  1652. event = kmalloc(sizeof(*event), GFP_NOIO);
  1653. if (!event)
  1654. return -ENOMEM;
  1655. dout("create_event %p\n", event);
  1656. event->cb = event_cb;
  1657. event->one_shot = 0;
  1658. event->data = data;
  1659. event->osdc = osdc;
  1660. INIT_LIST_HEAD(&event->osd_node);
  1661. RB_CLEAR_NODE(&event->node);
  1662. kref_init(&event->kref); /* one ref for us */
  1663. kref_get(&event->kref); /* one ref for the caller */
  1664. spin_lock(&osdc->event_lock);
  1665. event->cookie = ++osdc->event_count;
  1666. __insert_event(osdc, event);
  1667. spin_unlock(&osdc->event_lock);
  1668. *pevent = event;
  1669. return 0;
  1670. }
  1671. EXPORT_SYMBOL(ceph_osdc_create_event);
  1672. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1673. {
  1674. struct ceph_osd_client *osdc = event->osdc;
  1675. dout("cancel_event %p\n", event);
  1676. spin_lock(&osdc->event_lock);
  1677. __remove_event(event);
  1678. spin_unlock(&osdc->event_lock);
  1679. ceph_osdc_put_event(event); /* caller's */
  1680. }
  1681. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1682. static void do_event_work(struct work_struct *work)
  1683. {
  1684. struct ceph_osd_event_work *event_work =
  1685. container_of(work, struct ceph_osd_event_work, work);
  1686. struct ceph_osd_event *event = event_work->event;
  1687. u64 ver = event_work->ver;
  1688. u64 notify_id = event_work->notify_id;
  1689. u8 opcode = event_work->opcode;
  1690. dout("do_event_work completing %p\n", event);
  1691. event->cb(ver, notify_id, opcode, event->data);
  1692. dout("do_event_work completed %p\n", event);
  1693. ceph_osdc_put_event(event);
  1694. kfree(event_work);
  1695. }
  1696. /*
  1697. * Process osd watch notifications
  1698. */
  1699. static void handle_watch_notify(struct ceph_osd_client *osdc,
  1700. struct ceph_msg *msg)
  1701. {
  1702. void *p, *end;
  1703. u8 proto_ver;
  1704. u64 cookie, ver, notify_id;
  1705. u8 opcode;
  1706. struct ceph_osd_event *event;
  1707. struct ceph_osd_event_work *event_work;
  1708. p = msg->front.iov_base;
  1709. end = p + msg->front.iov_len;
  1710. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1711. ceph_decode_8_safe(&p, end, opcode, bad);
  1712. ceph_decode_64_safe(&p, end, cookie, bad);
  1713. ceph_decode_64_safe(&p, end, ver, bad);
  1714. ceph_decode_64_safe(&p, end, notify_id, bad);
  1715. spin_lock(&osdc->event_lock);
  1716. event = __find_event(osdc, cookie);
  1717. if (event) {
  1718. BUG_ON(event->one_shot);
  1719. get_event(event);
  1720. }
  1721. spin_unlock(&osdc->event_lock);
  1722. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1723. cookie, ver, event);
  1724. if (event) {
  1725. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1726. if (!event_work) {
  1727. dout("ERROR: could not allocate event_work\n");
  1728. goto done_err;
  1729. }
  1730. INIT_WORK(&event_work->work, do_event_work);
  1731. event_work->event = event;
  1732. event_work->ver = ver;
  1733. event_work->notify_id = notify_id;
  1734. event_work->opcode = opcode;
  1735. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1736. dout("WARNING: failed to queue notify event work\n");
  1737. goto done_err;
  1738. }
  1739. }
  1740. return;
  1741. done_err:
  1742. ceph_osdc_put_event(event);
  1743. return;
  1744. bad:
  1745. pr_err("osdc handle_watch_notify corrupt msg\n");
  1746. return;
  1747. }
  1748. /*
  1749. * build new request AND message
  1750. *
  1751. */
  1752. void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
  1753. struct ceph_snap_context *snapc, u64 snap_id,
  1754. struct timespec *mtime)
  1755. {
  1756. struct ceph_msg *msg = req->r_request;
  1757. void *p;
  1758. size_t msg_size;
  1759. int flags = req->r_flags;
  1760. u64 data_len;
  1761. unsigned int i;
  1762. req->r_snapid = snap_id;
  1763. req->r_snapc = ceph_get_snap_context(snapc);
  1764. /* encode request */
  1765. msg->hdr.version = cpu_to_le16(4);
  1766. p = msg->front.iov_base;
  1767. ceph_encode_32(&p, 1); /* client_inc is always 1 */
  1768. req->r_request_osdmap_epoch = p;
  1769. p += 4;
  1770. req->r_request_flags = p;
  1771. p += 4;
  1772. if (req->r_flags & CEPH_OSD_FLAG_WRITE)
  1773. ceph_encode_timespec(p, mtime);
  1774. p += sizeof(struct ceph_timespec);
  1775. req->r_request_reassert_version = p;
  1776. p += sizeof(struct ceph_eversion); /* will get filled in */
  1777. /* oloc */
  1778. ceph_encode_8(&p, 4);
  1779. ceph_encode_8(&p, 4);
  1780. ceph_encode_32(&p, 8 + 4 + 4);
  1781. req->r_request_pool = p;
  1782. p += 8;
  1783. ceph_encode_32(&p, -1); /* preferred */
  1784. ceph_encode_32(&p, 0); /* key len */
  1785. ceph_encode_8(&p, 1);
  1786. req->r_request_pgid = p;
  1787. p += 8 + 4;
  1788. ceph_encode_32(&p, -1); /* preferred */
  1789. /* oid */
  1790. ceph_encode_32(&p, req->r_oid_len);
  1791. memcpy(p, req->r_oid, req->r_oid_len);
  1792. dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
  1793. p += req->r_oid_len;
  1794. /* ops--can imply data */
  1795. ceph_encode_16(&p, (u16)req->r_num_ops);
  1796. data_len = 0;
  1797. for (i = 0; i < req->r_num_ops; i++) {
  1798. data_len += osd_req_encode_op(req, p, i);
  1799. p += sizeof(struct ceph_osd_op);
  1800. }
  1801. /* snaps */
  1802. ceph_encode_64(&p, req->r_snapid);
  1803. ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
  1804. ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
  1805. if (req->r_snapc) {
  1806. for (i = 0; i < snapc->num_snaps; i++) {
  1807. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  1808. }
  1809. }
  1810. req->r_request_attempts = p;
  1811. p += 4;
  1812. /* data */
  1813. if (flags & CEPH_OSD_FLAG_WRITE) {
  1814. u16 data_off;
  1815. /*
  1816. * The header "data_off" is a hint to the receiver
  1817. * allowing it to align received data into its
  1818. * buffers such that there's no need to re-copy
  1819. * it before writing it to disk (direct I/O).
  1820. */
  1821. data_off = (u16) (off & 0xffff);
  1822. req->r_request->hdr.data_off = cpu_to_le16(data_off);
  1823. }
  1824. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  1825. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  1826. msg_size = p - msg->front.iov_base;
  1827. msg->front.iov_len = msg_size;
  1828. msg->hdr.front_len = cpu_to_le32(msg_size);
  1829. dout("build_request msg_size was %d\n", (int)msg_size);
  1830. }
  1831. EXPORT_SYMBOL(ceph_osdc_build_request);
  1832. /*
  1833. * Register request, send initial attempt.
  1834. */
  1835. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1836. struct ceph_osd_request *req,
  1837. bool nofail)
  1838. {
  1839. int rc = 0;
  1840. down_read(&osdc->map_sem);
  1841. mutex_lock(&osdc->request_mutex);
  1842. __register_request(osdc, req);
  1843. req->r_sent = 0;
  1844. req->r_got_reply = 0;
  1845. req->r_completed = 0;
  1846. rc = __map_request(osdc, req, 0);
  1847. if (rc < 0) {
  1848. if (nofail) {
  1849. dout("osdc_start_request failed map, "
  1850. " will retry %lld\n", req->r_tid);
  1851. rc = 0;
  1852. }
  1853. goto out_unlock;
  1854. }
  1855. if (req->r_osd == NULL) {
  1856. dout("send_request %p no up osds in pg\n", req);
  1857. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1858. } else {
  1859. __send_queued(osdc);
  1860. }
  1861. rc = 0;
  1862. out_unlock:
  1863. mutex_unlock(&osdc->request_mutex);
  1864. up_read(&osdc->map_sem);
  1865. return rc;
  1866. }
  1867. EXPORT_SYMBOL(ceph_osdc_start_request);
  1868. /*
  1869. * wait for a request to complete
  1870. */
  1871. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1872. struct ceph_osd_request *req)
  1873. {
  1874. int rc;
  1875. rc = wait_for_completion_interruptible(&req->r_completion);
  1876. if (rc < 0) {
  1877. mutex_lock(&osdc->request_mutex);
  1878. __cancel_request(req);
  1879. __unregister_request(osdc, req);
  1880. mutex_unlock(&osdc->request_mutex);
  1881. complete_request(req);
  1882. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1883. return rc;
  1884. }
  1885. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1886. return req->r_result;
  1887. }
  1888. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1889. /*
  1890. * sync - wait for all in-flight requests to flush. avoid starvation.
  1891. */
  1892. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1893. {
  1894. struct ceph_osd_request *req;
  1895. u64 last_tid, next_tid = 0;
  1896. mutex_lock(&osdc->request_mutex);
  1897. last_tid = osdc->last_tid;
  1898. while (1) {
  1899. req = __lookup_request_ge(osdc, next_tid);
  1900. if (!req)
  1901. break;
  1902. if (req->r_tid > last_tid)
  1903. break;
  1904. next_tid = req->r_tid + 1;
  1905. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1906. continue;
  1907. ceph_osdc_get_request(req);
  1908. mutex_unlock(&osdc->request_mutex);
  1909. dout("sync waiting on tid %llu (last is %llu)\n",
  1910. req->r_tid, last_tid);
  1911. wait_for_completion(&req->r_safe_completion);
  1912. mutex_lock(&osdc->request_mutex);
  1913. ceph_osdc_put_request(req);
  1914. }
  1915. mutex_unlock(&osdc->request_mutex);
  1916. dout("sync done (thru tid %llu)\n", last_tid);
  1917. }
  1918. EXPORT_SYMBOL(ceph_osdc_sync);
  1919. /*
  1920. * init, shutdown
  1921. */
  1922. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1923. {
  1924. int err;
  1925. dout("init\n");
  1926. osdc->client = client;
  1927. osdc->osdmap = NULL;
  1928. init_rwsem(&osdc->map_sem);
  1929. init_completion(&osdc->map_waiters);
  1930. osdc->last_requested_map = 0;
  1931. mutex_init(&osdc->request_mutex);
  1932. osdc->last_tid = 0;
  1933. osdc->osds = RB_ROOT;
  1934. INIT_LIST_HEAD(&osdc->osd_lru);
  1935. osdc->requests = RB_ROOT;
  1936. INIT_LIST_HEAD(&osdc->req_lru);
  1937. INIT_LIST_HEAD(&osdc->req_unsent);
  1938. INIT_LIST_HEAD(&osdc->req_notarget);
  1939. INIT_LIST_HEAD(&osdc->req_linger);
  1940. osdc->num_requests = 0;
  1941. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1942. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1943. spin_lock_init(&osdc->event_lock);
  1944. osdc->event_tree = RB_ROOT;
  1945. osdc->event_count = 0;
  1946. schedule_delayed_work(&osdc->osds_timeout_work,
  1947. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1948. err = -ENOMEM;
  1949. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1950. sizeof(struct ceph_osd_request));
  1951. if (!osdc->req_mempool)
  1952. goto out;
  1953. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  1954. OSD_OP_FRONT_LEN, 10, true,
  1955. "osd_op");
  1956. if (err < 0)
  1957. goto out_mempool;
  1958. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  1959. OSD_OPREPLY_FRONT_LEN, 10, true,
  1960. "osd_op_reply");
  1961. if (err < 0)
  1962. goto out_msgpool;
  1963. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1964. if (IS_ERR(osdc->notify_wq)) {
  1965. err = PTR_ERR(osdc->notify_wq);
  1966. osdc->notify_wq = NULL;
  1967. goto out_msgpool;
  1968. }
  1969. return 0;
  1970. out_msgpool:
  1971. ceph_msgpool_destroy(&osdc->msgpool_op);
  1972. out_mempool:
  1973. mempool_destroy(osdc->req_mempool);
  1974. out:
  1975. return err;
  1976. }
  1977. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1978. {
  1979. flush_workqueue(osdc->notify_wq);
  1980. destroy_workqueue(osdc->notify_wq);
  1981. cancel_delayed_work_sync(&osdc->timeout_work);
  1982. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1983. if (osdc->osdmap) {
  1984. ceph_osdmap_destroy(osdc->osdmap);
  1985. osdc->osdmap = NULL;
  1986. }
  1987. remove_all_osds(osdc);
  1988. mempool_destroy(osdc->req_mempool);
  1989. ceph_msgpool_destroy(&osdc->msgpool_op);
  1990. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1991. }
  1992. /*
  1993. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1994. * *plen. Return number of bytes read, or error.
  1995. */
  1996. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1997. struct ceph_vino vino, struct ceph_file_layout *layout,
  1998. u64 off, u64 *plen,
  1999. u32 truncate_seq, u64 truncate_size,
  2000. struct page **pages, int num_pages, int page_align)
  2001. {
  2002. struct ceph_osd_request *req;
  2003. int rc = 0;
  2004. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  2005. vino.snap, off, *plen);
  2006. req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
  2007. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  2008. NULL, truncate_seq, truncate_size,
  2009. false);
  2010. if (IS_ERR(req))
  2011. return PTR_ERR(req);
  2012. /* it may be a short read due to an object boundary */
  2013. osd_req_op_extent_osd_data_pages(req, 0,
  2014. pages, *plen, page_align, false, false);
  2015. dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
  2016. off, *plen, *plen, page_align);
  2017. ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
  2018. rc = ceph_osdc_start_request(osdc, req, false);
  2019. if (!rc)
  2020. rc = ceph_osdc_wait_request(osdc, req);
  2021. ceph_osdc_put_request(req);
  2022. dout("readpages result %d\n", rc);
  2023. return rc;
  2024. }
  2025. EXPORT_SYMBOL(ceph_osdc_readpages);
  2026. /*
  2027. * do a synchronous write on N pages
  2028. */
  2029. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  2030. struct ceph_file_layout *layout,
  2031. struct ceph_snap_context *snapc,
  2032. u64 off, u64 len,
  2033. u32 truncate_seq, u64 truncate_size,
  2034. struct timespec *mtime,
  2035. struct page **pages, int num_pages)
  2036. {
  2037. struct ceph_osd_request *req;
  2038. int rc = 0;
  2039. int page_align = off & ~PAGE_MASK;
  2040. BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
  2041. req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
  2042. CEPH_OSD_OP_WRITE,
  2043. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  2044. snapc, truncate_seq, truncate_size,
  2045. true);
  2046. if (IS_ERR(req))
  2047. return PTR_ERR(req);
  2048. /* it may be a short write due to an object boundary */
  2049. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  2050. false, false);
  2051. dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
  2052. ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
  2053. rc = ceph_osdc_start_request(osdc, req, true);
  2054. if (!rc)
  2055. rc = ceph_osdc_wait_request(osdc, req);
  2056. ceph_osdc_put_request(req);
  2057. if (rc == 0)
  2058. rc = len;
  2059. dout("writepages result %d\n", rc);
  2060. return rc;
  2061. }
  2062. EXPORT_SYMBOL(ceph_osdc_writepages);
  2063. int ceph_osdc_setup(void)
  2064. {
  2065. BUG_ON(ceph_osd_request_cache);
  2066. ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
  2067. sizeof (struct ceph_osd_request),
  2068. __alignof__(struct ceph_osd_request),
  2069. 0, NULL);
  2070. return ceph_osd_request_cache ? 0 : -ENOMEM;
  2071. }
  2072. EXPORT_SYMBOL(ceph_osdc_setup);
  2073. void ceph_osdc_cleanup(void)
  2074. {
  2075. BUG_ON(!ceph_osd_request_cache);
  2076. kmem_cache_destroy(ceph_osd_request_cache);
  2077. ceph_osd_request_cache = NULL;
  2078. }
  2079. EXPORT_SYMBOL(ceph_osdc_cleanup);
  2080. /*
  2081. * handle incoming message
  2082. */
  2083. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  2084. {
  2085. struct ceph_osd *osd = con->private;
  2086. struct ceph_osd_client *osdc;
  2087. int type = le16_to_cpu(msg->hdr.type);
  2088. if (!osd)
  2089. goto out;
  2090. osdc = osd->o_osdc;
  2091. switch (type) {
  2092. case CEPH_MSG_OSD_MAP:
  2093. ceph_osdc_handle_map(osdc, msg);
  2094. break;
  2095. case CEPH_MSG_OSD_OPREPLY:
  2096. handle_reply(osdc, msg, con);
  2097. break;
  2098. case CEPH_MSG_WATCH_NOTIFY:
  2099. handle_watch_notify(osdc, msg);
  2100. break;
  2101. default:
  2102. pr_err("received unknown message type %d %s\n", type,
  2103. ceph_msg_type_name(type));
  2104. }
  2105. out:
  2106. ceph_msg_put(msg);
  2107. }
  2108. /*
  2109. * lookup and return message for incoming reply. set up reply message
  2110. * pages.
  2111. */
  2112. static struct ceph_msg *get_reply(struct ceph_connection *con,
  2113. struct ceph_msg_header *hdr,
  2114. int *skip)
  2115. {
  2116. struct ceph_osd *osd = con->private;
  2117. struct ceph_osd_client *osdc = osd->o_osdc;
  2118. struct ceph_msg *m;
  2119. struct ceph_osd_request *req;
  2120. int front = le32_to_cpu(hdr->front_len);
  2121. int data_len = le32_to_cpu(hdr->data_len);
  2122. u64 tid;
  2123. tid = le64_to_cpu(hdr->tid);
  2124. mutex_lock(&osdc->request_mutex);
  2125. req = __lookup_request(osdc, tid);
  2126. if (!req) {
  2127. *skip = 1;
  2128. m = NULL;
  2129. dout("get_reply unknown tid %llu from osd%d\n", tid,
  2130. osd->o_osd);
  2131. goto out;
  2132. }
  2133. if (req->r_reply->con)
  2134. dout("%s revoking msg %p from old con %p\n", __func__,
  2135. req->r_reply, req->r_reply->con);
  2136. ceph_msg_revoke_incoming(req->r_reply);
  2137. if (front > req->r_reply->front.iov_len) {
  2138. pr_warning("get_reply front %d > preallocated %d\n",
  2139. front, (int)req->r_reply->front.iov_len);
  2140. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
  2141. if (!m)
  2142. goto out;
  2143. ceph_msg_put(req->r_reply);
  2144. req->r_reply = m;
  2145. }
  2146. m = ceph_msg_get(req->r_reply);
  2147. if (data_len > 0) {
  2148. struct ceph_osd_data *osd_data;
  2149. /*
  2150. * XXX This is assuming there is only one op containing
  2151. * XXX page data. Probably OK for reads, but this
  2152. * XXX ought to be done more generally.
  2153. */
  2154. osd_data = osd_req_op_extent_osd_data(req, 0);
  2155. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  2156. if (osd_data->pages &&
  2157. unlikely(osd_data->length < data_len)) {
  2158. pr_warning("tid %lld reply has %d bytes "
  2159. "we had only %llu bytes ready\n",
  2160. tid, data_len, osd_data->length);
  2161. *skip = 1;
  2162. ceph_msg_put(m);
  2163. m = NULL;
  2164. goto out;
  2165. }
  2166. }
  2167. }
  2168. *skip = 0;
  2169. dout("get_reply tid %lld %p\n", tid, m);
  2170. out:
  2171. mutex_unlock(&osdc->request_mutex);
  2172. return m;
  2173. }
  2174. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  2175. struct ceph_msg_header *hdr,
  2176. int *skip)
  2177. {
  2178. struct ceph_osd *osd = con->private;
  2179. int type = le16_to_cpu(hdr->type);
  2180. int front = le32_to_cpu(hdr->front_len);
  2181. *skip = 0;
  2182. switch (type) {
  2183. case CEPH_MSG_OSD_MAP:
  2184. case CEPH_MSG_WATCH_NOTIFY:
  2185. return ceph_msg_new(type, front, GFP_NOFS, false);
  2186. case CEPH_MSG_OSD_OPREPLY:
  2187. return get_reply(con, hdr, skip);
  2188. default:
  2189. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  2190. osd->o_osd);
  2191. *skip = 1;
  2192. return NULL;
  2193. }
  2194. }
  2195. /*
  2196. * Wrappers to refcount containing ceph_osd struct
  2197. */
  2198. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  2199. {
  2200. struct ceph_osd *osd = con->private;
  2201. if (get_osd(osd))
  2202. return con;
  2203. return NULL;
  2204. }
  2205. static void put_osd_con(struct ceph_connection *con)
  2206. {
  2207. struct ceph_osd *osd = con->private;
  2208. put_osd(osd);
  2209. }
  2210. /*
  2211. * authentication
  2212. */
  2213. /*
  2214. * Note: returned pointer is the address of a structure that's
  2215. * managed separately. Caller must *not* attempt to free it.
  2216. */
  2217. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  2218. int *proto, int force_new)
  2219. {
  2220. struct ceph_osd *o = con->private;
  2221. struct ceph_osd_client *osdc = o->o_osdc;
  2222. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2223. struct ceph_auth_handshake *auth = &o->o_auth;
  2224. if (force_new && auth->authorizer) {
  2225. ceph_auth_destroy_authorizer(ac, auth->authorizer);
  2226. auth->authorizer = NULL;
  2227. }
  2228. if (!auth->authorizer) {
  2229. int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2230. auth);
  2231. if (ret)
  2232. return ERR_PTR(ret);
  2233. } else {
  2234. int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2235. auth);
  2236. if (ret)
  2237. return ERR_PTR(ret);
  2238. }
  2239. *proto = ac->protocol;
  2240. return auth;
  2241. }
  2242. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  2243. {
  2244. struct ceph_osd *o = con->private;
  2245. struct ceph_osd_client *osdc = o->o_osdc;
  2246. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2247. return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  2248. }
  2249. static int invalidate_authorizer(struct ceph_connection *con)
  2250. {
  2251. struct ceph_osd *o = con->private;
  2252. struct ceph_osd_client *osdc = o->o_osdc;
  2253. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2254. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  2255. return ceph_monc_validate_auth(&osdc->client->monc);
  2256. }
  2257. static const struct ceph_connection_operations osd_con_ops = {
  2258. .get = get_osd_con,
  2259. .put = put_osd_con,
  2260. .dispatch = dispatch,
  2261. .get_authorizer = get_authorizer,
  2262. .verify_authorizer_reply = verify_authorizer_reply,
  2263. .invalidate_authorizer = invalidate_authorizer,
  2264. .alloc_msg = alloc_msg,
  2265. .fault = osd_reset,
  2266. };