osd_client.c 55 KB

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