osd_client.c 69 KB

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