osd_client.c 58 KB

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