osd_client.c 76 KB

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