osd_client.c 58 KB

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