osd_client.c 56 KB

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