osd_client.c 62 KB

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