osd_client.c 57 KB

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