osd_client.c 78 KB

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