osd_client.c 75 KB

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