osd_client.c 55 KB

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