osd_client.c 76 KB

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