osd_client.c 57 KB

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