osd_client.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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 (IS_ERR(req))
  417. return req;
  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. __unregister_linger_request(osdc, req);
  511. __register_request(osdc, req);
  512. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  513. dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
  514. osd->o_osd);
  515. }
  516. }
  517. static void kick_osd_requests(struct ceph_osd_client *osdc,
  518. struct ceph_osd *kickosd)
  519. {
  520. mutex_lock(&osdc->request_mutex);
  521. __kick_osd_requests(osdc, kickosd);
  522. mutex_unlock(&osdc->request_mutex);
  523. }
  524. /*
  525. * If the osd connection drops, we need to resubmit all requests.
  526. */
  527. static void osd_reset(struct ceph_connection *con)
  528. {
  529. struct ceph_osd *osd = con->private;
  530. struct ceph_osd_client *osdc;
  531. if (!osd)
  532. return;
  533. dout("osd_reset osd%d\n", osd->o_osd);
  534. osdc = osd->o_osdc;
  535. down_read(&osdc->map_sem);
  536. kick_osd_requests(osdc, osd);
  537. send_queued(osdc);
  538. up_read(&osdc->map_sem);
  539. }
  540. /*
  541. * Track open sessions with osds.
  542. */
  543. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
  544. {
  545. struct ceph_osd *osd;
  546. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  547. if (!osd)
  548. return NULL;
  549. atomic_set(&osd->o_ref, 1);
  550. osd->o_osdc = osdc;
  551. INIT_LIST_HEAD(&osd->o_requests);
  552. INIT_LIST_HEAD(&osd->o_linger_requests);
  553. INIT_LIST_HEAD(&osd->o_osd_lru);
  554. osd->o_incarnation = 1;
  555. ceph_con_init(osdc->client->msgr, &osd->o_con);
  556. osd->o_con.private = osd;
  557. osd->o_con.ops = &osd_con_ops;
  558. osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
  559. INIT_LIST_HEAD(&osd->o_keepalive_item);
  560. return osd;
  561. }
  562. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  563. {
  564. if (atomic_inc_not_zero(&osd->o_ref)) {
  565. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  566. atomic_read(&osd->o_ref));
  567. return osd;
  568. } else {
  569. dout("get_osd %p FAIL\n", osd);
  570. return NULL;
  571. }
  572. }
  573. static void put_osd(struct ceph_osd *osd)
  574. {
  575. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  576. atomic_read(&osd->o_ref) - 1);
  577. if (atomic_dec_and_test(&osd->o_ref)) {
  578. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  579. if (osd->o_authorizer)
  580. ac->ops->destroy_authorizer(ac, osd->o_authorizer);
  581. kfree(osd);
  582. }
  583. }
  584. /*
  585. * remove an osd from our map
  586. */
  587. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  588. {
  589. dout("__remove_osd %p\n", osd);
  590. BUG_ON(!list_empty(&osd->o_requests));
  591. rb_erase(&osd->o_node, &osdc->osds);
  592. list_del_init(&osd->o_osd_lru);
  593. ceph_con_close(&osd->o_con);
  594. put_osd(osd);
  595. }
  596. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  597. struct ceph_osd *osd)
  598. {
  599. dout("__move_osd_to_lru %p\n", osd);
  600. BUG_ON(!list_empty(&osd->o_osd_lru));
  601. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  602. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  603. }
  604. static void __remove_osd_from_lru(struct ceph_osd *osd)
  605. {
  606. dout("__remove_osd_from_lru %p\n", osd);
  607. if (!list_empty(&osd->o_osd_lru))
  608. list_del_init(&osd->o_osd_lru);
  609. }
  610. static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
  611. {
  612. struct ceph_osd *osd, *nosd;
  613. dout("__remove_old_osds %p\n", osdc);
  614. mutex_lock(&osdc->request_mutex);
  615. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  616. if (!remove_all && time_before(jiffies, osd->lru_ttl))
  617. break;
  618. __remove_osd(osdc, osd);
  619. }
  620. mutex_unlock(&osdc->request_mutex);
  621. }
  622. /*
  623. * reset osd connect
  624. */
  625. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  626. {
  627. struct ceph_osd_request *req;
  628. int ret = 0;
  629. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  630. if (list_empty(&osd->o_requests) &&
  631. list_empty(&osd->o_linger_requests)) {
  632. __remove_osd(osdc, osd);
  633. } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
  634. &osd->o_con.peer_addr,
  635. sizeof(osd->o_con.peer_addr)) == 0 &&
  636. !ceph_con_opened(&osd->o_con)) {
  637. dout(" osd addr hasn't changed and connection never opened,"
  638. " letting msgr retry");
  639. /* touch each r_stamp for handle_timeout()'s benfit */
  640. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  641. req->r_stamp = jiffies;
  642. ret = -EAGAIN;
  643. } else {
  644. ceph_con_close(&osd->o_con);
  645. ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
  646. osd->o_incarnation++;
  647. }
  648. return ret;
  649. }
  650. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  651. {
  652. struct rb_node **p = &osdc->osds.rb_node;
  653. struct rb_node *parent = NULL;
  654. struct ceph_osd *osd = NULL;
  655. while (*p) {
  656. parent = *p;
  657. osd = rb_entry(parent, struct ceph_osd, o_node);
  658. if (new->o_osd < osd->o_osd)
  659. p = &(*p)->rb_left;
  660. else if (new->o_osd > osd->o_osd)
  661. p = &(*p)->rb_right;
  662. else
  663. BUG();
  664. }
  665. rb_link_node(&new->o_node, parent, p);
  666. rb_insert_color(&new->o_node, &osdc->osds);
  667. }
  668. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  669. {
  670. struct ceph_osd *osd;
  671. struct rb_node *n = osdc->osds.rb_node;
  672. while (n) {
  673. osd = rb_entry(n, struct ceph_osd, o_node);
  674. if (o < osd->o_osd)
  675. n = n->rb_left;
  676. else if (o > osd->o_osd)
  677. n = n->rb_right;
  678. else
  679. return osd;
  680. }
  681. return NULL;
  682. }
  683. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  684. {
  685. schedule_delayed_work(&osdc->timeout_work,
  686. osdc->client->options->osd_keepalive_timeout * HZ);
  687. }
  688. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  689. {
  690. cancel_delayed_work(&osdc->timeout_work);
  691. }
  692. /*
  693. * Register request, assign tid. If this is the first request, set up
  694. * the timeout event.
  695. */
  696. static void __register_request(struct ceph_osd_client *osdc,
  697. struct ceph_osd_request *req)
  698. {
  699. req->r_tid = ++osdc->last_tid;
  700. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  701. INIT_LIST_HEAD(&req->r_req_lru_item);
  702. dout("register_request %p tid %lld\n", req, req->r_tid);
  703. __insert_request(osdc, req);
  704. ceph_osdc_get_request(req);
  705. osdc->num_requests++;
  706. if (osdc->num_requests == 1) {
  707. dout(" first request, scheduling timeout\n");
  708. __schedule_osd_timeout(osdc);
  709. }
  710. }
  711. static void register_request(struct ceph_osd_client *osdc,
  712. struct ceph_osd_request *req)
  713. {
  714. mutex_lock(&osdc->request_mutex);
  715. __register_request(osdc, req);
  716. mutex_unlock(&osdc->request_mutex);
  717. }
  718. /*
  719. * called under osdc->request_mutex
  720. */
  721. static void __unregister_request(struct ceph_osd_client *osdc,
  722. struct ceph_osd_request *req)
  723. {
  724. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  725. rb_erase(&req->r_node, &osdc->requests);
  726. osdc->num_requests--;
  727. if (req->r_osd) {
  728. /* make sure the original request isn't in flight. */
  729. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  730. list_del_init(&req->r_osd_item);
  731. if (list_empty(&req->r_osd->o_requests) &&
  732. list_empty(&req->r_osd->o_linger_requests)) {
  733. dout("moving osd to %p lru\n", req->r_osd);
  734. __move_osd_to_lru(osdc, req->r_osd);
  735. }
  736. if (list_empty(&req->r_linger_item))
  737. req->r_osd = NULL;
  738. }
  739. ceph_osdc_put_request(req);
  740. list_del_init(&req->r_req_lru_item);
  741. if (osdc->num_requests == 0) {
  742. dout(" no requests, canceling timeout\n");
  743. __cancel_osd_timeout(osdc);
  744. }
  745. }
  746. /*
  747. * Cancel a previously queued request message
  748. */
  749. static void __cancel_request(struct ceph_osd_request *req)
  750. {
  751. if (req->r_sent && req->r_osd) {
  752. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  753. req->r_sent = 0;
  754. }
  755. }
  756. static void __register_linger_request(struct ceph_osd_client *osdc,
  757. struct ceph_osd_request *req)
  758. {
  759. dout("__register_linger_request %p\n", req);
  760. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  761. list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
  762. }
  763. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  764. struct ceph_osd_request *req)
  765. {
  766. dout("__unregister_linger_request %p\n", req);
  767. if (req->r_osd) {
  768. list_del_init(&req->r_linger_item);
  769. list_del_init(&req->r_linger_osd);
  770. if (list_empty(&req->r_osd->o_requests) &&
  771. list_empty(&req->r_osd->o_linger_requests)) {
  772. dout("moving osd to %p lru\n", req->r_osd);
  773. __move_osd_to_lru(osdc, req->r_osd);
  774. }
  775. if (list_empty(&req->r_osd_item))
  776. req->r_osd = NULL;
  777. }
  778. }
  779. void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
  780. struct ceph_osd_request *req)
  781. {
  782. mutex_lock(&osdc->request_mutex);
  783. if (req->r_linger) {
  784. __unregister_linger_request(osdc, req);
  785. ceph_osdc_put_request(req);
  786. }
  787. mutex_unlock(&osdc->request_mutex);
  788. }
  789. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  790. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  791. struct ceph_osd_request *req)
  792. {
  793. if (!req->r_linger) {
  794. dout("set_request_linger %p\n", req);
  795. req->r_linger = 1;
  796. /*
  797. * caller is now responsible for calling
  798. * unregister_linger_request
  799. */
  800. ceph_osdc_get_request(req);
  801. }
  802. }
  803. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  804. /*
  805. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  806. * (as needed), and set the request r_osd appropriately. If there is
  807. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  808. * (unsent, homeless) or leave on in-flight lru.
  809. *
  810. * Return 0 if unchanged, 1 if changed, or negative on error.
  811. *
  812. * Caller should hold map_sem for read and request_mutex.
  813. */
  814. static int __map_request(struct ceph_osd_client *osdc,
  815. struct ceph_osd_request *req)
  816. {
  817. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  818. struct ceph_pg pgid;
  819. int acting[CEPH_PG_MAX_SIZE];
  820. int o = -1, num = 0;
  821. int err;
  822. dout("map_request %p tid %lld\n", req, req->r_tid);
  823. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  824. &req->r_file_layout, osdc->osdmap);
  825. if (err) {
  826. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  827. return err;
  828. }
  829. pgid = reqhead->layout.ol_pgid;
  830. req->r_pgid = pgid;
  831. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  832. if (err > 0) {
  833. o = acting[0];
  834. num = err;
  835. }
  836. if ((req->r_osd && req->r_osd->o_osd == o &&
  837. req->r_sent >= req->r_osd->o_incarnation &&
  838. req->r_num_pg_osds == num &&
  839. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  840. (req->r_osd == NULL && o == -1))
  841. return 0; /* no change */
  842. dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
  843. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  844. req->r_osd ? req->r_osd->o_osd : -1);
  845. /* record full pg acting set */
  846. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  847. req->r_num_pg_osds = num;
  848. if (req->r_osd) {
  849. __cancel_request(req);
  850. list_del_init(&req->r_osd_item);
  851. req->r_osd = NULL;
  852. }
  853. req->r_osd = __lookup_osd(osdc, o);
  854. if (!req->r_osd && o >= 0) {
  855. err = -ENOMEM;
  856. req->r_osd = create_osd(osdc);
  857. if (!req->r_osd) {
  858. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  859. goto out;
  860. }
  861. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  862. req->r_osd->o_osd = o;
  863. req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
  864. __insert_osd(osdc, req->r_osd);
  865. ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
  866. }
  867. if (req->r_osd) {
  868. __remove_osd_from_lru(req->r_osd);
  869. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  870. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  871. } else {
  872. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  873. }
  874. err = 1; /* osd or pg changed */
  875. out:
  876. return err;
  877. }
  878. /*
  879. * caller should hold map_sem (for read) and request_mutex
  880. */
  881. static int __send_request(struct ceph_osd_client *osdc,
  882. struct ceph_osd_request *req)
  883. {
  884. struct ceph_osd_request_head *reqhead;
  885. dout("send_request %p tid %llu to osd%d flags %d\n",
  886. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  887. reqhead = req->r_request->front.iov_base;
  888. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  889. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  890. reqhead->reassert_version = req->r_reassert_version;
  891. req->r_stamp = jiffies;
  892. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  893. ceph_msg_get(req->r_request); /* send consumes a ref */
  894. ceph_con_send(&req->r_osd->o_con, req->r_request);
  895. req->r_sent = req->r_osd->o_incarnation;
  896. return 0;
  897. }
  898. /*
  899. * Send any requests in the queue (req_unsent).
  900. */
  901. static void send_queued(struct ceph_osd_client *osdc)
  902. {
  903. struct ceph_osd_request *req, *tmp;
  904. dout("send_queued\n");
  905. mutex_lock(&osdc->request_mutex);
  906. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
  907. __send_request(osdc, req);
  908. }
  909. mutex_unlock(&osdc->request_mutex);
  910. }
  911. /*
  912. * Timeout callback, called every N seconds when 1 or more osd
  913. * requests has been active for more than N seconds. When this
  914. * happens, we ping all OSDs with requests who have timed out to
  915. * ensure any communications channel reset is detected. Reset the
  916. * request timeouts another N seconds in the future as we go.
  917. * Reschedule the timeout event another N seconds in future (unless
  918. * there are no open requests).
  919. */
  920. static void handle_timeout(struct work_struct *work)
  921. {
  922. struct ceph_osd_client *osdc =
  923. container_of(work, struct ceph_osd_client, timeout_work.work);
  924. struct ceph_osd_request *req, *last_req = NULL;
  925. struct ceph_osd *osd;
  926. unsigned long timeout = osdc->client->options->osd_timeout * HZ;
  927. unsigned long keepalive =
  928. osdc->client->options->osd_keepalive_timeout * HZ;
  929. unsigned long last_stamp = 0;
  930. struct list_head slow_osds;
  931. dout("timeout\n");
  932. down_read(&osdc->map_sem);
  933. ceph_monc_request_next_osdmap(&osdc->client->monc);
  934. mutex_lock(&osdc->request_mutex);
  935. /*
  936. * reset osds that appear to be _really_ unresponsive. this
  937. * is a failsafe measure.. we really shouldn't be getting to
  938. * this point if the system is working properly. the monitors
  939. * should mark the osd as failed and we should find out about
  940. * it from an updated osd map.
  941. */
  942. while (timeout && !list_empty(&osdc->req_lru)) {
  943. req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
  944. r_req_lru_item);
  945. if (time_before(jiffies, req->r_stamp + timeout))
  946. break;
  947. BUG_ON(req == last_req && req->r_stamp == last_stamp);
  948. last_req = req;
  949. last_stamp = req->r_stamp;
  950. osd = req->r_osd;
  951. BUG_ON(!osd);
  952. pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
  953. req->r_tid, osd->o_osd);
  954. __kick_osd_requests(osdc, osd);
  955. }
  956. /*
  957. * ping osds that are a bit slow. this ensures that if there
  958. * is a break in the TCP connection we will notice, and reopen
  959. * a connection with that osd (from the fault callback).
  960. */
  961. INIT_LIST_HEAD(&slow_osds);
  962. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  963. if (time_before(jiffies, req->r_stamp + keepalive))
  964. break;
  965. osd = req->r_osd;
  966. BUG_ON(!osd);
  967. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  968. req->r_tid, osd->o_osd);
  969. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  970. }
  971. while (!list_empty(&slow_osds)) {
  972. osd = list_entry(slow_osds.next, struct ceph_osd,
  973. o_keepalive_item);
  974. list_del_init(&osd->o_keepalive_item);
  975. ceph_con_keepalive(&osd->o_con);
  976. }
  977. __schedule_osd_timeout(osdc);
  978. mutex_unlock(&osdc->request_mutex);
  979. send_queued(osdc);
  980. up_read(&osdc->map_sem);
  981. }
  982. static void handle_osds_timeout(struct work_struct *work)
  983. {
  984. struct ceph_osd_client *osdc =
  985. container_of(work, struct ceph_osd_client,
  986. osds_timeout_work.work);
  987. unsigned long delay =
  988. osdc->client->options->osd_idle_ttl * HZ >> 2;
  989. dout("osds timeout\n");
  990. down_read(&osdc->map_sem);
  991. remove_old_osds(osdc, 0);
  992. up_read(&osdc->map_sem);
  993. schedule_delayed_work(&osdc->osds_timeout_work,
  994. round_jiffies_relative(delay));
  995. }
  996. /*
  997. * handle osd op reply. either call the callback if it is specified,
  998. * or do the completion to wake up the waiting thread.
  999. */
  1000. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1001. struct ceph_connection *con)
  1002. {
  1003. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  1004. struct ceph_osd_request *req;
  1005. u64 tid;
  1006. int numops, object_len, flags;
  1007. s32 result;
  1008. tid = le64_to_cpu(msg->hdr.tid);
  1009. if (msg->front.iov_len < sizeof(*rhead))
  1010. goto bad;
  1011. numops = le32_to_cpu(rhead->num_ops);
  1012. object_len = le32_to_cpu(rhead->object_len);
  1013. result = le32_to_cpu(rhead->result);
  1014. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  1015. numops * sizeof(struct ceph_osd_op))
  1016. goto bad;
  1017. dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
  1018. /* lookup */
  1019. mutex_lock(&osdc->request_mutex);
  1020. req = __lookup_request(osdc, tid);
  1021. if (req == NULL) {
  1022. dout("handle_reply tid %llu dne\n", tid);
  1023. mutex_unlock(&osdc->request_mutex);
  1024. return;
  1025. }
  1026. ceph_osdc_get_request(req);
  1027. flags = le32_to_cpu(rhead->flags);
  1028. /*
  1029. * if this connection filled our message, drop our reference now, to
  1030. * avoid a (safe but slower) revoke later.
  1031. */
  1032. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  1033. dout(" dropping con_filling_msg ref %p\n", con);
  1034. req->r_con_filling_msg = NULL;
  1035. ceph_con_put(con);
  1036. }
  1037. if (!req->r_got_reply) {
  1038. unsigned bytes;
  1039. req->r_result = le32_to_cpu(rhead->result);
  1040. bytes = le32_to_cpu(msg->hdr.data_len);
  1041. dout("handle_reply result %d bytes %d\n", req->r_result,
  1042. bytes);
  1043. if (req->r_result == 0)
  1044. req->r_result = bytes;
  1045. /* in case this is a write and we need to replay, */
  1046. req->r_reassert_version = rhead->reassert_version;
  1047. req->r_got_reply = 1;
  1048. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1049. dout("handle_reply tid %llu dup ack\n", tid);
  1050. mutex_unlock(&osdc->request_mutex);
  1051. goto done;
  1052. }
  1053. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1054. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1055. __register_linger_request(osdc, req);
  1056. /* either this is a read, or we got the safe response */
  1057. if (result < 0 ||
  1058. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1059. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1060. __unregister_request(osdc, req);
  1061. mutex_unlock(&osdc->request_mutex);
  1062. if (req->r_callback)
  1063. req->r_callback(req, msg);
  1064. else
  1065. complete_all(&req->r_completion);
  1066. if (flags & CEPH_OSD_FLAG_ONDISK) {
  1067. if (req->r_safe_callback)
  1068. req->r_safe_callback(req, msg);
  1069. complete_all(&req->r_safe_completion); /* fsync waiter */
  1070. }
  1071. done:
  1072. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1073. ceph_osdc_put_request(req);
  1074. return;
  1075. bad:
  1076. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  1077. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  1078. (int)sizeof(*rhead));
  1079. ceph_msg_dump(msg);
  1080. }
  1081. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1082. {
  1083. struct rb_node *p, *n;
  1084. for (p = rb_first(&osdc->osds); p; p = n) {
  1085. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1086. n = rb_next(p);
  1087. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1088. memcmp(&osd->o_con.peer_addr,
  1089. ceph_osd_addr(osdc->osdmap,
  1090. osd->o_osd),
  1091. sizeof(struct ceph_entity_addr)) != 0)
  1092. __reset_osd(osdc, osd);
  1093. }
  1094. }
  1095. /*
  1096. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1097. * no osd, request a new map.
  1098. *
  1099. * Caller should hold map_sem for read and request_mutex.
  1100. */
  1101. static void kick_requests(struct ceph_osd_client *osdc)
  1102. {
  1103. struct ceph_osd_request *req, *nreq;
  1104. struct rb_node *p;
  1105. int needmap = 0;
  1106. int err;
  1107. dout("kick_requests\n");
  1108. mutex_lock(&osdc->request_mutex);
  1109. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  1110. req = rb_entry(p, struct ceph_osd_request, r_node);
  1111. err = __map_request(osdc, req);
  1112. if (err < 0)
  1113. continue; /* error */
  1114. if (req->r_osd == NULL) {
  1115. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1116. needmap++; /* request a newer map */
  1117. } else if (err > 0) {
  1118. dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
  1119. req->r_osd ? req->r_osd->o_osd : -1);
  1120. if (!req->r_linger)
  1121. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1122. }
  1123. }
  1124. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1125. r_linger_item) {
  1126. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1127. err = __map_request(osdc, req);
  1128. if (err == 0)
  1129. continue; /* no change and no osd was specified */
  1130. if (err < 0)
  1131. continue; /* hrm! */
  1132. if (req->r_osd == NULL) {
  1133. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1134. needmap++; /* request a newer map */
  1135. continue;
  1136. }
  1137. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1138. req->r_osd ? req->r_osd->o_osd : -1);
  1139. __unregister_linger_request(osdc, req);
  1140. __register_request(osdc, req);
  1141. }
  1142. mutex_unlock(&osdc->request_mutex);
  1143. if (needmap) {
  1144. dout("%d requests for down osds, need new map\n", needmap);
  1145. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1146. }
  1147. }
  1148. /*
  1149. * Process updated osd map.
  1150. *
  1151. * The message contains any number of incremental and full maps, normally
  1152. * indicating some sort of topology change in the cluster. Kick requests
  1153. * off to different OSDs as needed.
  1154. */
  1155. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1156. {
  1157. void *p, *end, *next;
  1158. u32 nr_maps, maplen;
  1159. u32 epoch;
  1160. struct ceph_osdmap *newmap = NULL, *oldmap;
  1161. int err;
  1162. struct ceph_fsid fsid;
  1163. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1164. p = msg->front.iov_base;
  1165. end = p + msg->front.iov_len;
  1166. /* verify fsid */
  1167. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1168. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1169. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1170. return;
  1171. down_write(&osdc->map_sem);
  1172. /* incremental maps */
  1173. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1174. dout(" %d inc maps\n", nr_maps);
  1175. while (nr_maps > 0) {
  1176. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1177. epoch = ceph_decode_32(&p);
  1178. maplen = ceph_decode_32(&p);
  1179. ceph_decode_need(&p, end, maplen, bad);
  1180. next = p + maplen;
  1181. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1182. dout("applying incremental map %u len %d\n",
  1183. epoch, maplen);
  1184. newmap = osdmap_apply_incremental(&p, next,
  1185. osdc->osdmap,
  1186. osdc->client->msgr);
  1187. if (IS_ERR(newmap)) {
  1188. err = PTR_ERR(newmap);
  1189. goto bad;
  1190. }
  1191. BUG_ON(!newmap);
  1192. if (newmap != osdc->osdmap) {
  1193. ceph_osdmap_destroy(osdc->osdmap);
  1194. osdc->osdmap = newmap;
  1195. }
  1196. kick_requests(osdc);
  1197. reset_changed_osds(osdc);
  1198. } else {
  1199. dout("ignoring incremental map %u len %d\n",
  1200. epoch, maplen);
  1201. }
  1202. p = next;
  1203. nr_maps--;
  1204. }
  1205. if (newmap)
  1206. goto done;
  1207. /* full maps */
  1208. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1209. dout(" %d full maps\n", nr_maps);
  1210. while (nr_maps) {
  1211. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1212. epoch = ceph_decode_32(&p);
  1213. maplen = ceph_decode_32(&p);
  1214. ceph_decode_need(&p, end, maplen, bad);
  1215. if (nr_maps > 1) {
  1216. dout("skipping non-latest full map %u len %d\n",
  1217. epoch, maplen);
  1218. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1219. dout("skipping full map %u len %d, "
  1220. "older than our %u\n", epoch, maplen,
  1221. osdc->osdmap->epoch);
  1222. } else {
  1223. dout("taking full map %u len %d\n", epoch, maplen);
  1224. newmap = osdmap_decode(&p, p+maplen);
  1225. if (IS_ERR(newmap)) {
  1226. err = PTR_ERR(newmap);
  1227. goto bad;
  1228. }
  1229. BUG_ON(!newmap);
  1230. oldmap = osdc->osdmap;
  1231. osdc->osdmap = newmap;
  1232. if (oldmap)
  1233. ceph_osdmap_destroy(oldmap);
  1234. kick_requests(osdc);
  1235. }
  1236. p += maplen;
  1237. nr_maps--;
  1238. }
  1239. done:
  1240. downgrade_write(&osdc->map_sem);
  1241. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1242. send_queued(osdc);
  1243. up_read(&osdc->map_sem);
  1244. wake_up_all(&osdc->client->auth_wq);
  1245. return;
  1246. bad:
  1247. pr_err("osdc handle_map corrupt msg\n");
  1248. ceph_msg_dump(msg);
  1249. up_write(&osdc->map_sem);
  1250. return;
  1251. }
  1252. /*
  1253. * watch/notify callback event infrastructure
  1254. *
  1255. * These callbacks are used both for watch and notify operations.
  1256. */
  1257. static void __release_event(struct kref *kref)
  1258. {
  1259. struct ceph_osd_event *event =
  1260. container_of(kref, struct ceph_osd_event, kref);
  1261. dout("__release_event %p\n", event);
  1262. kfree(event);
  1263. }
  1264. static void get_event(struct ceph_osd_event *event)
  1265. {
  1266. kref_get(&event->kref);
  1267. }
  1268. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1269. {
  1270. kref_put(&event->kref, __release_event);
  1271. }
  1272. EXPORT_SYMBOL(ceph_osdc_put_event);
  1273. static void __insert_event(struct ceph_osd_client *osdc,
  1274. struct ceph_osd_event *new)
  1275. {
  1276. struct rb_node **p = &osdc->event_tree.rb_node;
  1277. struct rb_node *parent = NULL;
  1278. struct ceph_osd_event *event = NULL;
  1279. while (*p) {
  1280. parent = *p;
  1281. event = rb_entry(parent, struct ceph_osd_event, node);
  1282. if (new->cookie < event->cookie)
  1283. p = &(*p)->rb_left;
  1284. else if (new->cookie > event->cookie)
  1285. p = &(*p)->rb_right;
  1286. else
  1287. BUG();
  1288. }
  1289. rb_link_node(&new->node, parent, p);
  1290. rb_insert_color(&new->node, &osdc->event_tree);
  1291. }
  1292. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1293. u64 cookie)
  1294. {
  1295. struct rb_node **p = &osdc->event_tree.rb_node;
  1296. struct rb_node *parent = NULL;
  1297. struct ceph_osd_event *event = NULL;
  1298. while (*p) {
  1299. parent = *p;
  1300. event = rb_entry(parent, struct ceph_osd_event, node);
  1301. if (cookie < event->cookie)
  1302. p = &(*p)->rb_left;
  1303. else if (cookie > event->cookie)
  1304. p = &(*p)->rb_right;
  1305. else
  1306. return event;
  1307. }
  1308. return NULL;
  1309. }
  1310. static void __remove_event(struct ceph_osd_event *event)
  1311. {
  1312. struct ceph_osd_client *osdc = event->osdc;
  1313. if (!RB_EMPTY_NODE(&event->node)) {
  1314. dout("__remove_event removed %p\n", event);
  1315. rb_erase(&event->node, &osdc->event_tree);
  1316. ceph_osdc_put_event(event);
  1317. } else {
  1318. dout("__remove_event didn't remove %p\n", event);
  1319. }
  1320. }
  1321. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1322. void (*event_cb)(u64, u64, u8, void *),
  1323. int one_shot, void *data,
  1324. struct ceph_osd_event **pevent)
  1325. {
  1326. struct ceph_osd_event *event;
  1327. event = kmalloc(sizeof(*event), GFP_NOIO);
  1328. if (!event)
  1329. return -ENOMEM;
  1330. dout("create_event %p\n", event);
  1331. event->cb = event_cb;
  1332. event->one_shot = one_shot;
  1333. event->data = data;
  1334. event->osdc = osdc;
  1335. INIT_LIST_HEAD(&event->osd_node);
  1336. kref_init(&event->kref); /* one ref for us */
  1337. kref_get(&event->kref); /* one ref for the caller */
  1338. init_completion(&event->completion);
  1339. spin_lock(&osdc->event_lock);
  1340. event->cookie = ++osdc->event_count;
  1341. __insert_event(osdc, event);
  1342. spin_unlock(&osdc->event_lock);
  1343. *pevent = event;
  1344. return 0;
  1345. }
  1346. EXPORT_SYMBOL(ceph_osdc_create_event);
  1347. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1348. {
  1349. struct ceph_osd_client *osdc = event->osdc;
  1350. dout("cancel_event %p\n", event);
  1351. spin_lock(&osdc->event_lock);
  1352. __remove_event(event);
  1353. spin_unlock(&osdc->event_lock);
  1354. ceph_osdc_put_event(event); /* caller's */
  1355. }
  1356. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1357. static void do_event_work(struct work_struct *work)
  1358. {
  1359. struct ceph_osd_event_work *event_work =
  1360. container_of(work, struct ceph_osd_event_work, work);
  1361. struct ceph_osd_event *event = event_work->event;
  1362. u64 ver = event_work->ver;
  1363. u64 notify_id = event_work->notify_id;
  1364. u8 opcode = event_work->opcode;
  1365. dout("do_event_work completing %p\n", event);
  1366. event->cb(ver, notify_id, opcode, event->data);
  1367. complete(&event->completion);
  1368. dout("do_event_work completed %p\n", event);
  1369. ceph_osdc_put_event(event);
  1370. kfree(event_work);
  1371. }
  1372. /*
  1373. * Process osd watch notifications
  1374. */
  1375. void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1376. {
  1377. void *p, *end;
  1378. u8 proto_ver;
  1379. u64 cookie, ver, notify_id;
  1380. u8 opcode;
  1381. struct ceph_osd_event *event;
  1382. struct ceph_osd_event_work *event_work;
  1383. p = msg->front.iov_base;
  1384. end = p + msg->front.iov_len;
  1385. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1386. ceph_decode_8_safe(&p, end, opcode, bad);
  1387. ceph_decode_64_safe(&p, end, cookie, bad);
  1388. ceph_decode_64_safe(&p, end, ver, bad);
  1389. ceph_decode_64_safe(&p, end, notify_id, bad);
  1390. spin_lock(&osdc->event_lock);
  1391. event = __find_event(osdc, cookie);
  1392. if (event) {
  1393. get_event(event);
  1394. if (event->one_shot)
  1395. __remove_event(event);
  1396. }
  1397. spin_unlock(&osdc->event_lock);
  1398. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1399. cookie, ver, event);
  1400. if (event) {
  1401. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1402. if (!event_work) {
  1403. dout("ERROR: could not allocate event_work\n");
  1404. goto done_err;
  1405. }
  1406. INIT_WORK(&event_work->work, do_event_work);
  1407. event_work->event = event;
  1408. event_work->ver = ver;
  1409. event_work->notify_id = notify_id;
  1410. event_work->opcode = opcode;
  1411. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1412. dout("WARNING: failed to queue notify event work\n");
  1413. goto done_err;
  1414. }
  1415. }
  1416. return;
  1417. done_err:
  1418. complete(&event->completion);
  1419. ceph_osdc_put_event(event);
  1420. return;
  1421. bad:
  1422. pr_err("osdc handle_watch_notify corrupt msg\n");
  1423. return;
  1424. }
  1425. int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
  1426. {
  1427. int err;
  1428. dout("wait_event %p\n", event);
  1429. err = wait_for_completion_interruptible_timeout(&event->completion,
  1430. timeout * HZ);
  1431. ceph_osdc_put_event(event);
  1432. if (err > 0)
  1433. err = 0;
  1434. dout("wait_event %p returns %d\n", event, err);
  1435. return err;
  1436. }
  1437. EXPORT_SYMBOL(ceph_osdc_wait_event);
  1438. /*
  1439. * Register request, send initial attempt.
  1440. */
  1441. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1442. struct ceph_osd_request *req,
  1443. bool nofail)
  1444. {
  1445. int rc = 0;
  1446. req->r_request->pages = req->r_pages;
  1447. req->r_request->nr_pages = req->r_num_pages;
  1448. #ifdef CONFIG_BLOCK
  1449. req->r_request->bio = req->r_bio;
  1450. #endif
  1451. req->r_request->trail = req->r_trail;
  1452. register_request(osdc, req);
  1453. down_read(&osdc->map_sem);
  1454. mutex_lock(&osdc->request_mutex);
  1455. /*
  1456. * a racing kick_requests() may have sent the message for us
  1457. * while we dropped request_mutex above, so only send now if
  1458. * the request still han't been touched yet.
  1459. */
  1460. if (req->r_sent == 0) {
  1461. rc = __map_request(osdc, req);
  1462. if (rc < 0)
  1463. goto out_unlock;
  1464. if (req->r_osd == NULL) {
  1465. dout("send_request %p no up osds in pg\n", req);
  1466. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1467. } else {
  1468. rc = __send_request(osdc, req);
  1469. if (rc) {
  1470. if (nofail) {
  1471. dout("osdc_start_request failed send, "
  1472. " will retry %lld\n", req->r_tid);
  1473. rc = 0;
  1474. } else {
  1475. __unregister_request(osdc, req);
  1476. }
  1477. }
  1478. }
  1479. }
  1480. out_unlock:
  1481. mutex_unlock(&osdc->request_mutex);
  1482. up_read(&osdc->map_sem);
  1483. return rc;
  1484. }
  1485. EXPORT_SYMBOL(ceph_osdc_start_request);
  1486. /*
  1487. * wait for a request to complete
  1488. */
  1489. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1490. struct ceph_osd_request *req)
  1491. {
  1492. int rc;
  1493. rc = wait_for_completion_interruptible(&req->r_completion);
  1494. if (rc < 0) {
  1495. mutex_lock(&osdc->request_mutex);
  1496. __cancel_request(req);
  1497. __unregister_request(osdc, req);
  1498. mutex_unlock(&osdc->request_mutex);
  1499. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1500. return rc;
  1501. }
  1502. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1503. return req->r_result;
  1504. }
  1505. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1506. /*
  1507. * sync - wait for all in-flight requests to flush. avoid starvation.
  1508. */
  1509. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1510. {
  1511. struct ceph_osd_request *req;
  1512. u64 last_tid, next_tid = 0;
  1513. mutex_lock(&osdc->request_mutex);
  1514. last_tid = osdc->last_tid;
  1515. while (1) {
  1516. req = __lookup_request_ge(osdc, next_tid);
  1517. if (!req)
  1518. break;
  1519. if (req->r_tid > last_tid)
  1520. break;
  1521. next_tid = req->r_tid + 1;
  1522. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1523. continue;
  1524. ceph_osdc_get_request(req);
  1525. mutex_unlock(&osdc->request_mutex);
  1526. dout("sync waiting on tid %llu (last is %llu)\n",
  1527. req->r_tid, last_tid);
  1528. wait_for_completion(&req->r_safe_completion);
  1529. mutex_lock(&osdc->request_mutex);
  1530. ceph_osdc_put_request(req);
  1531. }
  1532. mutex_unlock(&osdc->request_mutex);
  1533. dout("sync done (thru tid %llu)\n", last_tid);
  1534. }
  1535. EXPORT_SYMBOL(ceph_osdc_sync);
  1536. /*
  1537. * init, shutdown
  1538. */
  1539. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1540. {
  1541. int err;
  1542. dout("init\n");
  1543. osdc->client = client;
  1544. osdc->osdmap = NULL;
  1545. init_rwsem(&osdc->map_sem);
  1546. init_completion(&osdc->map_waiters);
  1547. osdc->last_requested_map = 0;
  1548. mutex_init(&osdc->request_mutex);
  1549. osdc->last_tid = 0;
  1550. osdc->osds = RB_ROOT;
  1551. INIT_LIST_HEAD(&osdc->osd_lru);
  1552. osdc->requests = RB_ROOT;
  1553. INIT_LIST_HEAD(&osdc->req_lru);
  1554. INIT_LIST_HEAD(&osdc->req_unsent);
  1555. INIT_LIST_HEAD(&osdc->req_notarget);
  1556. INIT_LIST_HEAD(&osdc->req_linger);
  1557. osdc->num_requests = 0;
  1558. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1559. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1560. spin_lock_init(&osdc->event_lock);
  1561. osdc->event_tree = RB_ROOT;
  1562. osdc->event_count = 0;
  1563. schedule_delayed_work(&osdc->osds_timeout_work,
  1564. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1565. err = -ENOMEM;
  1566. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1567. sizeof(struct ceph_osd_request));
  1568. if (!osdc->req_mempool)
  1569. goto out;
  1570. err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
  1571. "osd_op");
  1572. if (err < 0)
  1573. goto out_mempool;
  1574. err = ceph_msgpool_init(&osdc->msgpool_op_reply,
  1575. OSD_OPREPLY_FRONT_LEN, 10, true,
  1576. "osd_op_reply");
  1577. if (err < 0)
  1578. goto out_msgpool;
  1579. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1580. if (IS_ERR(osdc->notify_wq)) {
  1581. err = PTR_ERR(osdc->notify_wq);
  1582. osdc->notify_wq = NULL;
  1583. goto out_msgpool;
  1584. }
  1585. return 0;
  1586. out_msgpool:
  1587. ceph_msgpool_destroy(&osdc->msgpool_op);
  1588. out_mempool:
  1589. mempool_destroy(osdc->req_mempool);
  1590. out:
  1591. return err;
  1592. }
  1593. EXPORT_SYMBOL(ceph_osdc_init);
  1594. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1595. {
  1596. flush_workqueue(osdc->notify_wq);
  1597. destroy_workqueue(osdc->notify_wq);
  1598. cancel_delayed_work_sync(&osdc->timeout_work);
  1599. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1600. if (osdc->osdmap) {
  1601. ceph_osdmap_destroy(osdc->osdmap);
  1602. osdc->osdmap = NULL;
  1603. }
  1604. remove_old_osds(osdc, 1);
  1605. WARN_ON(!RB_EMPTY_ROOT(&osdc->osds));
  1606. mempool_destroy(osdc->req_mempool);
  1607. ceph_msgpool_destroy(&osdc->msgpool_op);
  1608. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1609. }
  1610. EXPORT_SYMBOL(ceph_osdc_stop);
  1611. /*
  1612. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1613. * *plen. Return number of bytes read, or error.
  1614. */
  1615. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1616. struct ceph_vino vino, struct ceph_file_layout *layout,
  1617. u64 off, u64 *plen,
  1618. u32 truncate_seq, u64 truncate_size,
  1619. struct page **pages, int num_pages, int page_align)
  1620. {
  1621. struct ceph_osd_request *req;
  1622. int rc = 0;
  1623. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1624. vino.snap, off, *plen);
  1625. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1626. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1627. NULL, 0, truncate_seq, truncate_size, NULL,
  1628. false, 1, page_align);
  1629. if (!req)
  1630. return -ENOMEM;
  1631. /* it may be a short read due to an object boundary */
  1632. req->r_pages = pages;
  1633. dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
  1634. off, *plen, req->r_num_pages, page_align);
  1635. rc = ceph_osdc_start_request(osdc, req, false);
  1636. if (!rc)
  1637. rc = ceph_osdc_wait_request(osdc, req);
  1638. ceph_osdc_put_request(req);
  1639. dout("readpages result %d\n", rc);
  1640. return rc;
  1641. }
  1642. EXPORT_SYMBOL(ceph_osdc_readpages);
  1643. /*
  1644. * do a synchronous write on N pages
  1645. */
  1646. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1647. struct ceph_file_layout *layout,
  1648. struct ceph_snap_context *snapc,
  1649. u64 off, u64 len,
  1650. u32 truncate_seq, u64 truncate_size,
  1651. struct timespec *mtime,
  1652. struct page **pages, int num_pages,
  1653. int flags, int do_sync, bool nofail)
  1654. {
  1655. struct ceph_osd_request *req;
  1656. int rc = 0;
  1657. int page_align = off & ~PAGE_MASK;
  1658. BUG_ON(vino.snap != CEPH_NOSNAP);
  1659. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1660. CEPH_OSD_OP_WRITE,
  1661. flags | CEPH_OSD_FLAG_ONDISK |
  1662. CEPH_OSD_FLAG_WRITE,
  1663. snapc, do_sync,
  1664. truncate_seq, truncate_size, mtime,
  1665. nofail, 1, page_align);
  1666. if (!req)
  1667. return -ENOMEM;
  1668. /* it may be a short write due to an object boundary */
  1669. req->r_pages = pages;
  1670. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1671. req->r_num_pages);
  1672. rc = ceph_osdc_start_request(osdc, req, nofail);
  1673. if (!rc)
  1674. rc = ceph_osdc_wait_request(osdc, req);
  1675. ceph_osdc_put_request(req);
  1676. if (rc == 0)
  1677. rc = len;
  1678. dout("writepages result %d\n", rc);
  1679. return rc;
  1680. }
  1681. EXPORT_SYMBOL(ceph_osdc_writepages);
  1682. /*
  1683. * handle incoming message
  1684. */
  1685. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1686. {
  1687. struct ceph_osd *osd = con->private;
  1688. struct ceph_osd_client *osdc;
  1689. int type = le16_to_cpu(msg->hdr.type);
  1690. if (!osd)
  1691. goto out;
  1692. osdc = osd->o_osdc;
  1693. switch (type) {
  1694. case CEPH_MSG_OSD_MAP:
  1695. ceph_osdc_handle_map(osdc, msg);
  1696. break;
  1697. case CEPH_MSG_OSD_OPREPLY:
  1698. handle_reply(osdc, msg, con);
  1699. break;
  1700. case CEPH_MSG_WATCH_NOTIFY:
  1701. handle_watch_notify(osdc, msg);
  1702. break;
  1703. default:
  1704. pr_err("received unknown message type %d %s\n", type,
  1705. ceph_msg_type_name(type));
  1706. }
  1707. out:
  1708. ceph_msg_put(msg);
  1709. }
  1710. /*
  1711. * lookup and return message for incoming reply. set up reply message
  1712. * pages.
  1713. */
  1714. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1715. struct ceph_msg_header *hdr,
  1716. int *skip)
  1717. {
  1718. struct ceph_osd *osd = con->private;
  1719. struct ceph_osd_client *osdc = osd->o_osdc;
  1720. struct ceph_msg *m;
  1721. struct ceph_osd_request *req;
  1722. int front = le32_to_cpu(hdr->front_len);
  1723. int data_len = le32_to_cpu(hdr->data_len);
  1724. u64 tid;
  1725. tid = le64_to_cpu(hdr->tid);
  1726. mutex_lock(&osdc->request_mutex);
  1727. req = __lookup_request(osdc, tid);
  1728. if (!req) {
  1729. *skip = 1;
  1730. m = NULL;
  1731. pr_info("get_reply unknown tid %llu from osd%d\n", tid,
  1732. osd->o_osd);
  1733. goto out;
  1734. }
  1735. if (req->r_con_filling_msg) {
  1736. dout("get_reply revoking msg %p from old con %p\n",
  1737. req->r_reply, req->r_con_filling_msg);
  1738. ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
  1739. ceph_con_put(req->r_con_filling_msg);
  1740. req->r_con_filling_msg = NULL;
  1741. }
  1742. if (front > req->r_reply->front.iov_len) {
  1743. pr_warning("get_reply front %d > preallocated %d\n",
  1744. front, (int)req->r_reply->front.iov_len);
  1745. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
  1746. if (!m)
  1747. goto out;
  1748. ceph_msg_put(req->r_reply);
  1749. req->r_reply = m;
  1750. }
  1751. m = ceph_msg_get(req->r_reply);
  1752. if (data_len > 0) {
  1753. int want = calc_pages_for(req->r_page_alignment, data_len);
  1754. if (unlikely(req->r_num_pages < want)) {
  1755. pr_warning("tid %lld reply %d > expected %d pages\n",
  1756. tid, want, m->nr_pages);
  1757. *skip = 1;
  1758. ceph_msg_put(m);
  1759. m = NULL;
  1760. goto out;
  1761. }
  1762. m->pages = req->r_pages;
  1763. m->nr_pages = req->r_num_pages;
  1764. m->page_alignment = req->r_page_alignment;
  1765. #ifdef CONFIG_BLOCK
  1766. m->bio = req->r_bio;
  1767. #endif
  1768. }
  1769. *skip = 0;
  1770. req->r_con_filling_msg = ceph_con_get(con);
  1771. dout("get_reply tid %lld %p\n", tid, m);
  1772. out:
  1773. mutex_unlock(&osdc->request_mutex);
  1774. return m;
  1775. }
  1776. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1777. struct ceph_msg_header *hdr,
  1778. int *skip)
  1779. {
  1780. struct ceph_osd *osd = con->private;
  1781. int type = le16_to_cpu(hdr->type);
  1782. int front = le32_to_cpu(hdr->front_len);
  1783. switch (type) {
  1784. case CEPH_MSG_OSD_MAP:
  1785. case CEPH_MSG_WATCH_NOTIFY:
  1786. return ceph_msg_new(type, front, GFP_NOFS);
  1787. case CEPH_MSG_OSD_OPREPLY:
  1788. return get_reply(con, hdr, skip);
  1789. default:
  1790. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1791. osd->o_osd);
  1792. *skip = 1;
  1793. return NULL;
  1794. }
  1795. }
  1796. /*
  1797. * Wrappers to refcount containing ceph_osd struct
  1798. */
  1799. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1800. {
  1801. struct ceph_osd *osd = con->private;
  1802. if (get_osd(osd))
  1803. return con;
  1804. return NULL;
  1805. }
  1806. static void put_osd_con(struct ceph_connection *con)
  1807. {
  1808. struct ceph_osd *osd = con->private;
  1809. put_osd(osd);
  1810. }
  1811. /*
  1812. * authentication
  1813. */
  1814. static int get_authorizer(struct ceph_connection *con,
  1815. void **buf, int *len, int *proto,
  1816. void **reply_buf, int *reply_len, int force_new)
  1817. {
  1818. struct ceph_osd *o = con->private;
  1819. struct ceph_osd_client *osdc = o->o_osdc;
  1820. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1821. int ret = 0;
  1822. if (force_new && o->o_authorizer) {
  1823. ac->ops->destroy_authorizer(ac, o->o_authorizer);
  1824. o->o_authorizer = NULL;
  1825. }
  1826. if (o->o_authorizer == NULL) {
  1827. ret = ac->ops->create_authorizer(
  1828. ac, CEPH_ENTITY_TYPE_OSD,
  1829. &o->o_authorizer,
  1830. &o->o_authorizer_buf,
  1831. &o->o_authorizer_buf_len,
  1832. &o->o_authorizer_reply_buf,
  1833. &o->o_authorizer_reply_buf_len);
  1834. if (ret)
  1835. return ret;
  1836. }
  1837. *proto = ac->protocol;
  1838. *buf = o->o_authorizer_buf;
  1839. *len = o->o_authorizer_buf_len;
  1840. *reply_buf = o->o_authorizer_reply_buf;
  1841. *reply_len = o->o_authorizer_reply_buf_len;
  1842. return 0;
  1843. }
  1844. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1845. {
  1846. struct ceph_osd *o = con->private;
  1847. struct ceph_osd_client *osdc = o->o_osdc;
  1848. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1849. return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
  1850. }
  1851. static int invalidate_authorizer(struct ceph_connection *con)
  1852. {
  1853. struct ceph_osd *o = con->private;
  1854. struct ceph_osd_client *osdc = o->o_osdc;
  1855. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1856. if (ac->ops->invalidate_authorizer)
  1857. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1858. return ceph_monc_validate_auth(&osdc->client->monc);
  1859. }
  1860. static const struct ceph_connection_operations osd_con_ops = {
  1861. .get = get_osd_con,
  1862. .put = put_osd_con,
  1863. .dispatch = dispatch,
  1864. .get_authorizer = get_authorizer,
  1865. .verify_authorizer_reply = verify_authorizer_reply,
  1866. .invalidate_authorizer = invalidate_authorizer,
  1867. .alloc_msg = alloc_msg,
  1868. .fault = osd_reset,
  1869. };