osd_client.c 55 KB

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