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