1 2 #include <linux/ceph/ceph_debug.h> 3 4 #include <linux/module.h> 5 #include <linux/err.h> 6 #include <linux/highmem.h> 7 #include <linux/mm.h> 8 #include <linux/pagemap.h> 9 #include <linux/slab.h> 10 #include <linux/uaccess.h> 11 #ifdef CONFIG_BLOCK 12 #include <linux/bio.h> 13 #endif 14 15 #include <linux/ceph/libceph.h> 16 #include <linux/ceph/osd_client.h> 17 #include <linux/ceph/messenger.h> 18 #include <linux/ceph/decode.h> 19 #include <linux/ceph/auth.h> 20 #include <linux/ceph/pagelist.h> 21 22 #define OSD_OP_FRONT_LEN 4096 23 #define OSD_OPREPLY_FRONT_LEN 512 24 25 static const struct ceph_connection_operations osd_con_ops; 26 27 static void __send_queued(struct ceph_osd_client *osdc); 28 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd); 29 static void __register_request(struct ceph_osd_client *osdc, 30 struct ceph_osd_request *req); 31 static void __unregister_linger_request(struct ceph_osd_client *osdc, 32 struct ceph_osd_request *req); 33 static void __send_request(struct ceph_osd_client *osdc, 34 struct ceph_osd_request *req); 35 36 /* 37 * Implement client access to distributed object storage cluster. 38 * 39 * All data objects are stored within a cluster/cloud of OSDs, or 40 * "object storage devices." (Note that Ceph OSDs have _nothing_ to 41 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply 42 * remote daemons serving up and coordinating consistent and safe 43 * access to storage. 44 * 45 * Cluster membership and the mapping of data objects onto storage devices 46 * are described by the osd map. 47 * 48 * We keep track of pending OSD requests (read, write), resubmit 49 * requests to different OSDs when the cluster topology/data layout 50 * change, or retry the affected requests when the communications 51 * channel with an OSD is reset. 52 */ 53 54 /* 55 * calculate the mapping of a file extent onto an object, and fill out the 56 * request accordingly. shorten extent as necessary if it crosses an 57 * object boundary. 58 * 59 * fill osd op in request message. 60 */ 61 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen, 62 u64 *objnum, u64 *objoff, u64 *objlen) 63 { 64 u64 orig_len = *plen; 65 int r; 66 67 /* object extent? */ 68 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum, 69 objoff, objlen); 70 if (r < 0) 71 return r; 72 if (*objlen < orig_len) { 73 *plen = *objlen; 74 dout(" skipping last %llu, final file extent %llu~%llu\n", 75 orig_len - *plen, off, *plen); 76 } 77 78 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen); 79 80 return 0; 81 } 82 83 static void ceph_osd_data_init(struct ceph_osd_data *osd_data) 84 { 85 memset(osd_data, 0, sizeof (*osd_data)); 86 osd_data->type = CEPH_OSD_DATA_TYPE_NONE; 87 } 88 89 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data, 90 struct page **pages, u64 length, u32 alignment, 91 bool pages_from_pool, bool own_pages) 92 { 93 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES; 94 osd_data->pages = pages; 95 osd_data->length = length; 96 osd_data->alignment = alignment; 97 osd_data->pages_from_pool = pages_from_pool; 98 osd_data->own_pages = own_pages; 99 } 100 101 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data, 102 struct ceph_pagelist *pagelist) 103 { 104 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST; 105 osd_data->pagelist = pagelist; 106 } 107 108 #ifdef CONFIG_BLOCK 109 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data, 110 struct bio *bio, size_t bio_length) 111 { 112 osd_data->type = CEPH_OSD_DATA_TYPE_BIO; 113 osd_data->bio = bio; 114 osd_data->bio_length = bio_length; 115 } 116 #endif /* CONFIG_BLOCK */ 117 118 struct ceph_osd_data * 119 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req, 120 unsigned int which, bool write_request) 121 { 122 BUG_ON(which >= osd_req->r_num_ops); 123 124 return &osd_req->r_ops[which].extent.osd_data; 125 } 126 EXPORT_SYMBOL(osd_req_op_extent_osd_data); 127 128 struct ceph_osd_data * 129 osd_req_op_cls_request_info(struct ceph_osd_request *osd_req, 130 unsigned int which) 131 { 132 BUG_ON(which >= osd_req->r_num_ops); 133 134 return &osd_req->r_ops[which].cls.request_info; 135 } 136 EXPORT_SYMBOL(osd_req_op_cls_request_info); /* ??? */ 137 138 struct ceph_osd_data * 139 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req, 140 unsigned int which) 141 { 142 BUG_ON(which >= osd_req->r_num_ops); 143 144 return &osd_req->r_ops[which].cls.response_data; 145 } 146 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */ 147 148 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req, 149 unsigned int which, bool write_request, 150 struct page **pages, u64 length, u32 alignment, 151 bool pages_from_pool, bool own_pages) 152 { 153 struct ceph_osd_data *osd_data; 154 155 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request); 156 ceph_osd_data_pages_init(osd_data, pages, length, alignment, 157 pages_from_pool, own_pages); 158 } 159 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages); 160 161 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req, 162 unsigned int which, bool write_request, 163 struct ceph_pagelist *pagelist) 164 { 165 struct ceph_osd_data *osd_data; 166 167 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request); 168 ceph_osd_data_pagelist_init(osd_data, pagelist); 169 } 170 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist); 171 172 #ifdef CONFIG_BLOCK 173 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req, 174 unsigned int which, bool write_request, 175 struct bio *bio, size_t bio_length) 176 { 177 struct ceph_osd_data *osd_data; 178 179 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request); 180 ceph_osd_data_bio_init(osd_data, bio, bio_length); 181 } 182 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio); 183 #endif /* CONFIG_BLOCK */ 184 185 static void osd_req_op_cls_request_info_pagelist( 186 struct ceph_osd_request *osd_req, 187 unsigned int which, struct ceph_pagelist *pagelist) 188 { 189 struct ceph_osd_data *osd_data; 190 191 osd_data = osd_req_op_cls_request_info(osd_req, which); 192 ceph_osd_data_pagelist_init(osd_data, pagelist); 193 } 194 195 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req, 196 unsigned int which, struct page **pages, u64 length, 197 u32 alignment, bool pages_from_pool, bool own_pages) 198 { 199 struct ceph_osd_data *osd_data; 200 201 osd_data = osd_req_op_cls_response_data(osd_req, which); 202 ceph_osd_data_pages_init(osd_data, pages, length, alignment, 203 pages_from_pool, own_pages); 204 } 205 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages); 206 207 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data) 208 { 209 switch (osd_data->type) { 210 case CEPH_OSD_DATA_TYPE_NONE: 211 return 0; 212 case CEPH_OSD_DATA_TYPE_PAGES: 213 return osd_data->length; 214 case CEPH_OSD_DATA_TYPE_PAGELIST: 215 return (u64)osd_data->pagelist->length; 216 #ifdef CONFIG_BLOCK 217 case CEPH_OSD_DATA_TYPE_BIO: 218 return (u64)osd_data->bio_length; 219 #endif /* CONFIG_BLOCK */ 220 default: 221 WARN(true, "unrecognized data type %d\n", (int)osd_data->type); 222 return 0; 223 } 224 } 225 226 227 static void ceph_osd_data_release(struct ceph_osd_data *osd_data) 228 { 229 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) { 230 int num_pages; 231 232 num_pages = calc_pages_for((u64)osd_data->alignment, 233 (u64)osd_data->length); 234 ceph_release_page_vector(osd_data->pages, num_pages); 235 } 236 ceph_osd_data_init(osd_data); 237 } 238 239 static void osd_req_op_data_release(struct ceph_osd_request *osd_req, 240 unsigned int which) 241 { 242 struct ceph_osd_req_op *op; 243 244 BUG_ON(which >= osd_req->r_num_ops); 245 op = &osd_req->r_ops[which]; 246 247 switch (op->op) { 248 case CEPH_OSD_OP_READ: 249 case CEPH_OSD_OP_WRITE: 250 ceph_osd_data_release(&op->extent.osd_data); 251 break; 252 case CEPH_OSD_OP_CALL: 253 ceph_osd_data_release(&op->cls.request_info); 254 ceph_osd_data_release(&op->cls.response_data); 255 break; 256 default: 257 break; 258 } 259 } 260 261 /* 262 * requests 263 */ 264 void ceph_osdc_release_request(struct kref *kref) 265 { 266 struct ceph_osd_request *req; 267 unsigned int which; 268 269 req = container_of(kref, struct ceph_osd_request, r_kref); 270 if (req->r_request) 271 ceph_msg_put(req->r_request); 272 if (req->r_reply) { 273 ceph_msg_revoke_incoming(req->r_reply); 274 ceph_msg_put(req->r_reply); 275 } 276 277 for (which = 0; which < req->r_num_ops; which++) 278 osd_req_op_data_release(req, which); 279 280 ceph_put_snap_context(req->r_snapc); 281 if (req->r_mempool) 282 mempool_free(req, req->r_osdc->req_mempool); 283 else 284 kfree(req); 285 } 286 EXPORT_SYMBOL(ceph_osdc_release_request); 287 288 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc, 289 struct ceph_snap_context *snapc, 290 unsigned int num_ops, 291 bool use_mempool, 292 gfp_t gfp_flags) 293 { 294 struct ceph_osd_request *req; 295 struct ceph_msg *msg; 296 size_t msg_size; 297 298 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX); 299 BUG_ON(num_ops > CEPH_OSD_MAX_OP); 300 301 msg_size = 4 + 4 + 8 + 8 + 4+8; 302 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */ 303 msg_size += 1 + 8 + 4 + 4; /* pg_t */ 304 msg_size += 4 + MAX_OBJ_NAME_SIZE; 305 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op); 306 msg_size += 8; /* snapid */ 307 msg_size += 8; /* snap_seq */ 308 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */ 309 msg_size += 4; 310 311 if (use_mempool) { 312 req = mempool_alloc(osdc->req_mempool, gfp_flags); 313 memset(req, 0, sizeof(*req)); 314 } else { 315 req = kzalloc(sizeof(*req), gfp_flags); 316 } 317 if (req == NULL) 318 return NULL; 319 320 req->r_osdc = osdc; 321 req->r_mempool = use_mempool; 322 req->r_num_ops = num_ops; 323 324 kref_init(&req->r_kref); 325 init_completion(&req->r_completion); 326 init_completion(&req->r_safe_completion); 327 RB_CLEAR_NODE(&req->r_node); 328 INIT_LIST_HEAD(&req->r_unsafe_item); 329 INIT_LIST_HEAD(&req->r_linger_item); 330 INIT_LIST_HEAD(&req->r_linger_osd); 331 INIT_LIST_HEAD(&req->r_req_lru_item); 332 INIT_LIST_HEAD(&req->r_osd_item); 333 334 /* create reply message */ 335 if (use_mempool) 336 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0); 337 else 338 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, 339 OSD_OPREPLY_FRONT_LEN, gfp_flags, true); 340 if (!msg) { 341 ceph_osdc_put_request(req); 342 return NULL; 343 } 344 req->r_reply = msg; 345 346 /* create request message; allow space for oid */ 347 if (use_mempool) 348 msg = ceph_msgpool_get(&osdc->msgpool_op, 0); 349 else 350 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true); 351 if (!msg) { 352 ceph_osdc_put_request(req); 353 return NULL; 354 } 355 356 memset(msg->front.iov_base, 0, msg->front.iov_len); 357 358 req->r_request = msg; 359 360 return req; 361 } 362 EXPORT_SYMBOL(ceph_osdc_alloc_request); 363 364 static bool osd_req_opcode_valid(u16 opcode) 365 { 366 switch (opcode) { 367 case CEPH_OSD_OP_READ: 368 case CEPH_OSD_OP_STAT: 369 case CEPH_OSD_OP_MAPEXT: 370 case CEPH_OSD_OP_MASKTRUNC: 371 case CEPH_OSD_OP_SPARSE_READ: 372 case CEPH_OSD_OP_NOTIFY: 373 case CEPH_OSD_OP_NOTIFY_ACK: 374 case CEPH_OSD_OP_ASSERT_VER: 375 case CEPH_OSD_OP_WRITE: 376 case CEPH_OSD_OP_WRITEFULL: 377 case CEPH_OSD_OP_TRUNCATE: 378 case CEPH_OSD_OP_ZERO: 379 case CEPH_OSD_OP_DELETE: 380 case CEPH_OSD_OP_APPEND: 381 case CEPH_OSD_OP_STARTSYNC: 382 case CEPH_OSD_OP_SETTRUNC: 383 case CEPH_OSD_OP_TRIMTRUNC: 384 case CEPH_OSD_OP_TMAPUP: 385 case CEPH_OSD_OP_TMAPPUT: 386 case CEPH_OSD_OP_TMAPGET: 387 case CEPH_OSD_OP_CREATE: 388 case CEPH_OSD_OP_ROLLBACK: 389 case CEPH_OSD_OP_WATCH: 390 case CEPH_OSD_OP_OMAPGETKEYS: 391 case CEPH_OSD_OP_OMAPGETVALS: 392 case CEPH_OSD_OP_OMAPGETHEADER: 393 case CEPH_OSD_OP_OMAPGETVALSBYKEYS: 394 case CEPH_OSD_OP_OMAPSETVALS: 395 case CEPH_OSD_OP_OMAPSETHEADER: 396 case CEPH_OSD_OP_OMAPCLEAR: 397 case CEPH_OSD_OP_OMAPRMKEYS: 398 case CEPH_OSD_OP_OMAP_CMP: 399 case CEPH_OSD_OP_CLONERANGE: 400 case CEPH_OSD_OP_ASSERT_SRC_VERSION: 401 case CEPH_OSD_OP_SRC_CMPXATTR: 402 case CEPH_OSD_OP_GETXATTR: 403 case CEPH_OSD_OP_GETXATTRS: 404 case CEPH_OSD_OP_CMPXATTR: 405 case CEPH_OSD_OP_SETXATTR: 406 case CEPH_OSD_OP_SETXATTRS: 407 case CEPH_OSD_OP_RESETXATTRS: 408 case CEPH_OSD_OP_RMXATTR: 409 case CEPH_OSD_OP_PULL: 410 case CEPH_OSD_OP_PUSH: 411 case CEPH_OSD_OP_BALANCEREADS: 412 case CEPH_OSD_OP_UNBALANCEREADS: 413 case CEPH_OSD_OP_SCRUB: 414 case CEPH_OSD_OP_SCRUB_RESERVE: 415 case CEPH_OSD_OP_SCRUB_UNRESERVE: 416 case CEPH_OSD_OP_SCRUB_STOP: 417 case CEPH_OSD_OP_SCRUB_MAP: 418 case CEPH_OSD_OP_WRLOCK: 419 case CEPH_OSD_OP_WRUNLOCK: 420 case CEPH_OSD_OP_RDLOCK: 421 case CEPH_OSD_OP_RDUNLOCK: 422 case CEPH_OSD_OP_UPLOCK: 423 case CEPH_OSD_OP_DNLOCK: 424 case CEPH_OSD_OP_CALL: 425 case CEPH_OSD_OP_PGLS: 426 case CEPH_OSD_OP_PGLS_FILTER: 427 return true; 428 default: 429 return false; 430 } 431 } 432 433 /* 434 * This is an osd op init function for opcodes that have no data or 435 * other information associated with them. It also serves as a 436 * common init routine for all the other init functions, below. 437 */ 438 static struct ceph_osd_req_op * 439 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which, 440 u16 opcode) 441 { 442 struct ceph_osd_req_op *op; 443 444 BUG_ON(which >= osd_req->r_num_ops); 445 BUG_ON(!osd_req_opcode_valid(opcode)); 446 447 op = &osd_req->r_ops[which]; 448 memset(op, 0, sizeof (*op)); 449 op->op = opcode; 450 451 return op; 452 } 453 454 void osd_req_op_extent_init(struct ceph_osd_request *osd_req, 455 unsigned int which, u16 opcode, 456 u64 offset, u64 length, 457 u64 truncate_size, u32 truncate_seq) 458 { 459 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode); 460 size_t payload_len = 0; 461 462 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE); 463 464 op->extent.offset = offset; 465 op->extent.length = length; 466 op->extent.truncate_size = truncate_size; 467 op->extent.truncate_seq = truncate_seq; 468 if (opcode == CEPH_OSD_OP_WRITE) 469 payload_len += length; 470 471 op->payload_len = payload_len; 472 } 473 EXPORT_SYMBOL(osd_req_op_extent_init); 474 475 void osd_req_op_extent_update(struct ceph_osd_request *osd_req, 476 unsigned int which, u64 length) 477 { 478 struct ceph_osd_req_op *op; 479 u64 previous; 480 481 BUG_ON(which >= osd_req->r_num_ops); 482 op = &osd_req->r_ops[which]; 483 previous = op->extent.length; 484 485 if (length == previous) 486 return; /* Nothing to do */ 487 BUG_ON(length > previous); 488 489 op->extent.length = length; 490 op->payload_len -= previous - length; 491 } 492 EXPORT_SYMBOL(osd_req_op_extent_update); 493 494 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which, 495 u16 opcode, const char *class, const char *method, 496 const void *request_data, size_t request_data_size) 497 { 498 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode); 499 struct ceph_pagelist *pagelist; 500 size_t payload_len = 0; 501 size_t size; 502 503 BUG_ON(opcode != CEPH_OSD_OP_CALL); 504 505 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS); 506 BUG_ON(!pagelist); 507 ceph_pagelist_init(pagelist); 508 509 op->cls.class_name = class; 510 size = strlen(class); 511 BUG_ON(size > (size_t) U8_MAX); 512 op->cls.class_len = size; 513 ceph_pagelist_append(pagelist, class, size); 514 payload_len += size; 515 516 op->cls.method_name = method; 517 size = strlen(method); 518 BUG_ON(size > (size_t) U8_MAX); 519 op->cls.method_len = size; 520 ceph_pagelist_append(pagelist, method, size); 521 payload_len += size; 522 523 op->cls.request_data = request_data; 524 BUG_ON(request_data_size > (size_t) U32_MAX); 525 op->cls.request_data_len = (u32) request_data_size; 526 ceph_pagelist_append(pagelist, request_data, request_data_size); 527 payload_len += request_data_size; 528 529 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist); 530 531 op->cls.argc = 0; /* currently unused */ 532 533 op->payload_len = payload_len; 534 } 535 EXPORT_SYMBOL(osd_req_op_cls_init); 536 537 void osd_req_op_watch_init(struct ceph_osd_request *osd_req, 538 unsigned int which, u16 opcode, 539 u64 cookie, u64 version, int flag) 540 { 541 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode); 542 543 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH); 544 545 op->watch.cookie = cookie; 546 /* op->watch.ver = version; */ /* XXX 3847 */ 547 op->watch.ver = cpu_to_le64(version); 548 if (opcode == CEPH_OSD_OP_WATCH && flag) 549 op->watch.flag = (u8)1; 550 } 551 EXPORT_SYMBOL(osd_req_op_watch_init); 552 553 static void ceph_osdc_msg_data_set(struct ceph_msg *msg, 554 struct ceph_osd_data *osd_data) 555 { 556 u64 length = ceph_osd_data_length(osd_data); 557 558 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) { 559 BUG_ON(length > (u64) SIZE_MAX); 560 if (length) 561 ceph_msg_data_set_pages(msg, osd_data->pages, 562 length, osd_data->alignment); 563 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) { 564 BUG_ON(!length); 565 ceph_msg_data_set_pagelist(msg, osd_data->pagelist); 566 #ifdef CONFIG_BLOCK 567 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) { 568 ceph_msg_data_set_bio(msg, osd_data->bio, length); 569 #endif 570 } else { 571 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE); 572 } 573 } 574 575 static u64 osd_req_encode_op(struct ceph_osd_request *req, 576 struct ceph_osd_op *dst, unsigned int which) 577 { 578 struct ceph_osd_req_op *src; 579 u64 request_data_len = 0; 580 581 BUG_ON(which >= req->r_num_ops); 582 src = &req->r_ops[which]; 583 if (WARN_ON(!osd_req_opcode_valid(src->op))) { 584 pr_err("unrecognized osd opcode %d\n", src->op); 585 586 return 0; 587 } 588 589 switch (src->op) { 590 case CEPH_OSD_OP_STAT: 591 break; 592 case CEPH_OSD_OP_READ: 593 case CEPH_OSD_OP_WRITE: 594 if (src->op == CEPH_OSD_OP_WRITE) 595 request_data_len = src->extent.length; 596 dst->extent.offset = cpu_to_le64(src->extent.offset); 597 dst->extent.length = cpu_to_le64(src->extent.length); 598 dst->extent.truncate_size = 599 cpu_to_le64(src->extent.truncate_size); 600 dst->extent.truncate_seq = 601 cpu_to_le32(src->extent.truncate_seq); 602 if (src->op == CEPH_OSD_OP_WRITE) 603 ceph_osdc_msg_data_set(req->r_request, 604 &src->extent.osd_data); 605 else 606 ceph_osdc_msg_data_set(req->r_reply, 607 &src->extent.osd_data); 608 break; 609 case CEPH_OSD_OP_CALL: 610 dst->cls.class_len = src->cls.class_len; 611 dst->cls.method_len = src->cls.method_len; 612 dst->cls.indata_len = cpu_to_le32(src->cls.request_data_len); 613 ceph_osdc_msg_data_set(req->r_reply, &src->cls.response_data); 614 ceph_osdc_msg_data_set(req->r_request, &src->cls.request_info); 615 BUG_ON(src->cls.request_info.type != 616 CEPH_OSD_DATA_TYPE_PAGELIST); 617 request_data_len = src->cls.request_info.pagelist->length; 618 break; 619 case CEPH_OSD_OP_STARTSYNC: 620 break; 621 case CEPH_OSD_OP_NOTIFY_ACK: 622 case CEPH_OSD_OP_WATCH: 623 dst->watch.cookie = cpu_to_le64(src->watch.cookie); 624 dst->watch.ver = cpu_to_le64(src->watch.ver); 625 dst->watch.flag = src->watch.flag; 626 break; 627 default: 628 pr_err("unsupported osd opcode %s\n", 629 ceph_osd_op_name(src->op)); 630 WARN_ON(1); 631 632 return 0; 633 } 634 dst->op = cpu_to_le16(src->op); 635 dst->payload_len = cpu_to_le32(src->payload_len); 636 637 return request_data_len; 638 } 639 640 /* 641 * build new request AND message, calculate layout, and adjust file 642 * extent as needed. 643 * 644 * if the file was recently truncated, we include information about its 645 * old and new size so that the object can be updated appropriately. (we 646 * avoid synchronously deleting truncated objects because it's slow.) 647 * 648 * if @do_sync, include a 'startsync' command so that the osd will flush 649 * data quickly. 650 */ 651 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, 652 struct ceph_file_layout *layout, 653 struct ceph_vino vino, 654 u64 off, u64 *plen, int num_ops, 655 int opcode, int flags, 656 struct ceph_snap_context *snapc, 657 u32 truncate_seq, 658 u64 truncate_size, 659 bool use_mempool) 660 { 661 struct ceph_osd_request *req; 662 u64 objnum = 0; 663 u64 objoff = 0; 664 u64 objlen = 0; 665 u32 object_size; 666 u64 object_base; 667 int r; 668 669 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE); 670 671 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool, 672 GFP_NOFS); 673 if (!req) 674 return ERR_PTR(-ENOMEM); 675 676 req->r_flags = flags; 677 678 /* calculate max write size */ 679 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen); 680 if (r < 0) { 681 ceph_osdc_put_request(req); 682 return ERR_PTR(r); 683 } 684 685 object_size = le32_to_cpu(layout->fl_object_size); 686 object_base = off - objoff; 687 if (truncate_size <= object_base) { 688 truncate_size = 0; 689 } else { 690 truncate_size -= object_base; 691 if (truncate_size > object_size) 692 truncate_size = object_size; 693 } 694 695 osd_req_op_extent_init(req, 0, opcode, objoff, objlen, 696 truncate_size, truncate_seq); 697 698 /* 699 * A second op in the ops array means the caller wants to 700 * also issue a include a 'startsync' command so that the 701 * osd will flush data quickly. 702 */ 703 if (num_ops > 1) 704 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC); 705 706 req->r_file_layout = *layout; /* keep a copy */ 707 708 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", 709 vino.ino, objnum); 710 req->r_oid_len = strlen(req->r_oid); 711 712 return req; 713 } 714 EXPORT_SYMBOL(ceph_osdc_new_request); 715 716 /* 717 * We keep osd requests in an rbtree, sorted by ->r_tid. 718 */ 719 static void __insert_request(struct ceph_osd_client *osdc, 720 struct ceph_osd_request *new) 721 { 722 struct rb_node **p = &osdc->requests.rb_node; 723 struct rb_node *parent = NULL; 724 struct ceph_osd_request *req = NULL; 725 726 while (*p) { 727 parent = *p; 728 req = rb_entry(parent, struct ceph_osd_request, r_node); 729 if (new->r_tid < req->r_tid) 730 p = &(*p)->rb_left; 731 else if (new->r_tid > req->r_tid) 732 p = &(*p)->rb_right; 733 else 734 BUG(); 735 } 736 737 rb_link_node(&new->r_node, parent, p); 738 rb_insert_color(&new->r_node, &osdc->requests); 739 } 740 741 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc, 742 u64 tid) 743 { 744 struct ceph_osd_request *req; 745 struct rb_node *n = osdc->requests.rb_node; 746 747 while (n) { 748 req = rb_entry(n, struct ceph_osd_request, r_node); 749 if (tid < req->r_tid) 750 n = n->rb_left; 751 else if (tid > req->r_tid) 752 n = n->rb_right; 753 else 754 return req; 755 } 756 return NULL; 757 } 758 759 static struct ceph_osd_request * 760 __lookup_request_ge(struct ceph_osd_client *osdc, 761 u64 tid) 762 { 763 struct ceph_osd_request *req; 764 struct rb_node *n = osdc->requests.rb_node; 765 766 while (n) { 767 req = rb_entry(n, struct ceph_osd_request, r_node); 768 if (tid < req->r_tid) { 769 if (!n->rb_left) 770 return req; 771 n = n->rb_left; 772 } else if (tid > req->r_tid) { 773 n = n->rb_right; 774 } else { 775 return req; 776 } 777 } 778 return NULL; 779 } 780 781 /* 782 * Resubmit requests pending on the given osd. 783 */ 784 static void __kick_osd_requests(struct ceph_osd_client *osdc, 785 struct ceph_osd *osd) 786 { 787 struct ceph_osd_request *req, *nreq; 788 LIST_HEAD(resend); 789 int err; 790 791 dout("__kick_osd_requests osd%d\n", osd->o_osd); 792 err = __reset_osd(osdc, osd); 793 if (err) 794 return; 795 /* 796 * Build up a list of requests to resend by traversing the 797 * osd's list of requests. Requests for a given object are 798 * sent in tid order, and that is also the order they're 799 * kept on this list. Therefore all requests that are in 800 * flight will be found first, followed by all requests that 801 * have not yet been sent. And to resend requests while 802 * preserving this order we will want to put any sent 803 * requests back on the front of the osd client's unsent 804 * list. 805 * 806 * So we build a separate ordered list of already-sent 807 * requests for the affected osd and splice it onto the 808 * front of the osd client's unsent list. Once we've seen a 809 * request that has not yet been sent we're done. Those 810 * requests are already sitting right where they belong. 811 */ 812 list_for_each_entry(req, &osd->o_requests, r_osd_item) { 813 if (!req->r_sent) 814 break; 815 list_move_tail(&req->r_req_lru_item, &resend); 816 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid, 817 osd->o_osd); 818 if (!req->r_linger) 819 req->r_flags |= CEPH_OSD_FLAG_RETRY; 820 } 821 list_splice(&resend, &osdc->req_unsent); 822 823 /* 824 * Linger requests are re-registered before sending, which 825 * sets up a new tid for each. We add them to the unsent 826 * list at the end to keep things in tid order. 827 */ 828 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests, 829 r_linger_osd) { 830 /* 831 * reregister request prior to unregistering linger so 832 * that r_osd is preserved. 833 */ 834 BUG_ON(!list_empty(&req->r_req_lru_item)); 835 __register_request(osdc, req); 836 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent); 837 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests); 838 __unregister_linger_request(osdc, req); 839 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid, 840 osd->o_osd); 841 } 842 } 843 844 /* 845 * If the osd connection drops, we need to resubmit all requests. 846 */ 847 static void osd_reset(struct ceph_connection *con) 848 { 849 struct ceph_osd *osd = con->private; 850 struct ceph_osd_client *osdc; 851 852 if (!osd) 853 return; 854 dout("osd_reset osd%d\n", osd->o_osd); 855 osdc = osd->o_osdc; 856 down_read(&osdc->map_sem); 857 mutex_lock(&osdc->request_mutex); 858 __kick_osd_requests(osdc, osd); 859 __send_queued(osdc); 860 mutex_unlock(&osdc->request_mutex); 861 up_read(&osdc->map_sem); 862 } 863 864 /* 865 * Track open sessions with osds. 866 */ 867 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum) 868 { 869 struct ceph_osd *osd; 870 871 osd = kzalloc(sizeof(*osd), GFP_NOFS); 872 if (!osd) 873 return NULL; 874 875 atomic_set(&osd->o_ref, 1); 876 osd->o_osdc = osdc; 877 osd->o_osd = onum; 878 RB_CLEAR_NODE(&osd->o_node); 879 INIT_LIST_HEAD(&osd->o_requests); 880 INIT_LIST_HEAD(&osd->o_linger_requests); 881 INIT_LIST_HEAD(&osd->o_osd_lru); 882 osd->o_incarnation = 1; 883 884 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr); 885 886 INIT_LIST_HEAD(&osd->o_keepalive_item); 887 return osd; 888 } 889 890 static struct ceph_osd *get_osd(struct ceph_osd *osd) 891 { 892 if (atomic_inc_not_zero(&osd->o_ref)) { 893 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1, 894 atomic_read(&osd->o_ref)); 895 return osd; 896 } else { 897 dout("get_osd %p FAIL\n", osd); 898 return NULL; 899 } 900 } 901 902 static void put_osd(struct ceph_osd *osd) 903 { 904 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref), 905 atomic_read(&osd->o_ref) - 1); 906 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) { 907 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth; 908 909 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer); 910 kfree(osd); 911 } 912 } 913 914 /* 915 * remove an osd from our map 916 */ 917 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) 918 { 919 dout("__remove_osd %p\n", osd); 920 BUG_ON(!list_empty(&osd->o_requests)); 921 rb_erase(&osd->o_node, &osdc->osds); 922 list_del_init(&osd->o_osd_lru); 923 ceph_con_close(&osd->o_con); 924 put_osd(osd); 925 } 926 927 static void remove_all_osds(struct ceph_osd_client *osdc) 928 { 929 dout("%s %p\n", __func__, osdc); 930 mutex_lock(&osdc->request_mutex); 931 while (!RB_EMPTY_ROOT(&osdc->osds)) { 932 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds), 933 struct ceph_osd, o_node); 934 __remove_osd(osdc, osd); 935 } 936 mutex_unlock(&osdc->request_mutex); 937 } 938 939 static void __move_osd_to_lru(struct ceph_osd_client *osdc, 940 struct ceph_osd *osd) 941 { 942 dout("__move_osd_to_lru %p\n", osd); 943 BUG_ON(!list_empty(&osd->o_osd_lru)); 944 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); 945 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ; 946 } 947 948 static void __remove_osd_from_lru(struct ceph_osd *osd) 949 { 950 dout("__remove_osd_from_lru %p\n", osd); 951 if (!list_empty(&osd->o_osd_lru)) 952 list_del_init(&osd->o_osd_lru); 953 } 954 955 static void remove_old_osds(struct ceph_osd_client *osdc) 956 { 957 struct ceph_osd *osd, *nosd; 958 959 dout("__remove_old_osds %p\n", osdc); 960 mutex_lock(&osdc->request_mutex); 961 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { 962 if (time_before(jiffies, osd->lru_ttl)) 963 break; 964 __remove_osd(osdc, osd); 965 } 966 mutex_unlock(&osdc->request_mutex); 967 } 968 969 /* 970 * reset osd connect 971 */ 972 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) 973 { 974 struct ceph_entity_addr *peer_addr; 975 976 dout("__reset_osd %p osd%d\n", osd, osd->o_osd); 977 if (list_empty(&osd->o_requests) && 978 list_empty(&osd->o_linger_requests)) { 979 __remove_osd(osdc, osd); 980 981 return -ENODEV; 982 } 983 984 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd]; 985 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) && 986 !ceph_con_opened(&osd->o_con)) { 987 struct ceph_osd_request *req; 988 989 dout(" osd addr hasn't changed and connection never opened," 990 " letting msgr retry"); 991 /* touch each r_stamp for handle_timeout()'s benfit */ 992 list_for_each_entry(req, &osd->o_requests, r_osd_item) 993 req->r_stamp = jiffies; 994 995 return -EAGAIN; 996 } 997 998 ceph_con_close(&osd->o_con); 999 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr); 1000 osd->o_incarnation++; 1001 1002 return 0; 1003 } 1004 1005 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new) 1006 { 1007 struct rb_node **p = &osdc->osds.rb_node; 1008 struct rb_node *parent = NULL; 1009 struct ceph_osd *osd = NULL; 1010 1011 dout("__insert_osd %p osd%d\n", new, new->o_osd); 1012 while (*p) { 1013 parent = *p; 1014 osd = rb_entry(parent, struct ceph_osd, o_node); 1015 if (new->o_osd < osd->o_osd) 1016 p = &(*p)->rb_left; 1017 else if (new->o_osd > osd->o_osd) 1018 p = &(*p)->rb_right; 1019 else 1020 BUG(); 1021 } 1022 1023 rb_link_node(&new->o_node, parent, p); 1024 rb_insert_color(&new->o_node, &osdc->osds); 1025 } 1026 1027 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o) 1028 { 1029 struct ceph_osd *osd; 1030 struct rb_node *n = osdc->osds.rb_node; 1031 1032 while (n) { 1033 osd = rb_entry(n, struct ceph_osd, o_node); 1034 if (o < osd->o_osd) 1035 n = n->rb_left; 1036 else if (o > osd->o_osd) 1037 n = n->rb_right; 1038 else 1039 return osd; 1040 } 1041 return NULL; 1042 } 1043 1044 static void __schedule_osd_timeout(struct ceph_osd_client *osdc) 1045 { 1046 schedule_delayed_work(&osdc->timeout_work, 1047 osdc->client->options->osd_keepalive_timeout * HZ); 1048 } 1049 1050 static void __cancel_osd_timeout(struct ceph_osd_client *osdc) 1051 { 1052 cancel_delayed_work(&osdc->timeout_work); 1053 } 1054 1055 /* 1056 * Register request, assign tid. If this is the first request, set up 1057 * the timeout event. 1058 */ 1059 static void __register_request(struct ceph_osd_client *osdc, 1060 struct ceph_osd_request *req) 1061 { 1062 req->r_tid = ++osdc->last_tid; 1063 req->r_request->hdr.tid = cpu_to_le64(req->r_tid); 1064 dout("__register_request %p tid %lld\n", req, req->r_tid); 1065 __insert_request(osdc, req); 1066 ceph_osdc_get_request(req); 1067 osdc->num_requests++; 1068 if (osdc->num_requests == 1) { 1069 dout(" first request, scheduling timeout\n"); 1070 __schedule_osd_timeout(osdc); 1071 } 1072 } 1073 1074 /* 1075 * called under osdc->request_mutex 1076 */ 1077 static void __unregister_request(struct ceph_osd_client *osdc, 1078 struct ceph_osd_request *req) 1079 { 1080 if (RB_EMPTY_NODE(&req->r_node)) { 1081 dout("__unregister_request %p tid %lld not registered\n", 1082 req, req->r_tid); 1083 return; 1084 } 1085 1086 dout("__unregister_request %p tid %lld\n", req, req->r_tid); 1087 rb_erase(&req->r_node, &osdc->requests); 1088 osdc->num_requests--; 1089 1090 if (req->r_osd) { 1091 /* make sure the original request isn't in flight. */ 1092 ceph_msg_revoke(req->r_request); 1093 1094 list_del_init(&req->r_osd_item); 1095 if (list_empty(&req->r_osd->o_requests) && 1096 list_empty(&req->r_osd->o_linger_requests)) { 1097 dout("moving osd to %p lru\n", req->r_osd); 1098 __move_osd_to_lru(osdc, req->r_osd); 1099 } 1100 if (list_empty(&req->r_linger_item)) 1101 req->r_osd = NULL; 1102 } 1103 1104 list_del_init(&req->r_req_lru_item); 1105 ceph_osdc_put_request(req); 1106 1107 if (osdc->num_requests == 0) { 1108 dout(" no requests, canceling timeout\n"); 1109 __cancel_osd_timeout(osdc); 1110 } 1111 } 1112 1113 /* 1114 * Cancel a previously queued request message 1115 */ 1116 static void __cancel_request(struct ceph_osd_request *req) 1117 { 1118 if (req->r_sent && req->r_osd) { 1119 ceph_msg_revoke(req->r_request); 1120 req->r_sent = 0; 1121 } 1122 } 1123 1124 static void __register_linger_request(struct ceph_osd_client *osdc, 1125 struct ceph_osd_request *req) 1126 { 1127 dout("__register_linger_request %p\n", req); 1128 list_add_tail(&req->r_linger_item, &osdc->req_linger); 1129 if (req->r_osd) 1130 list_add_tail(&req->r_linger_osd, 1131 &req->r_osd->o_linger_requests); 1132 } 1133 1134 static void __unregister_linger_request(struct ceph_osd_client *osdc, 1135 struct ceph_osd_request *req) 1136 { 1137 dout("__unregister_linger_request %p\n", req); 1138 list_del_init(&req->r_linger_item); 1139 if (req->r_osd) { 1140 list_del_init(&req->r_linger_osd); 1141 1142 if (list_empty(&req->r_osd->o_requests) && 1143 list_empty(&req->r_osd->o_linger_requests)) { 1144 dout("moving osd to %p lru\n", req->r_osd); 1145 __move_osd_to_lru(osdc, req->r_osd); 1146 } 1147 if (list_empty(&req->r_osd_item)) 1148 req->r_osd = NULL; 1149 } 1150 } 1151 1152 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc, 1153 struct ceph_osd_request *req) 1154 { 1155 mutex_lock(&osdc->request_mutex); 1156 if (req->r_linger) { 1157 __unregister_linger_request(osdc, req); 1158 ceph_osdc_put_request(req); 1159 } 1160 mutex_unlock(&osdc->request_mutex); 1161 } 1162 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request); 1163 1164 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc, 1165 struct ceph_osd_request *req) 1166 { 1167 if (!req->r_linger) { 1168 dout("set_request_linger %p\n", req); 1169 req->r_linger = 1; 1170 /* 1171 * caller is now responsible for calling 1172 * unregister_linger_request 1173 */ 1174 ceph_osdc_get_request(req); 1175 } 1176 } 1177 EXPORT_SYMBOL(ceph_osdc_set_request_linger); 1178 1179 /* 1180 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct 1181 * (as needed), and set the request r_osd appropriately. If there is 1182 * no up osd, set r_osd to NULL. Move the request to the appropriate list 1183 * (unsent, homeless) or leave on in-flight lru. 1184 * 1185 * Return 0 if unchanged, 1 if changed, or negative on error. 1186 * 1187 * Caller should hold map_sem for read and request_mutex. 1188 */ 1189 static int __map_request(struct ceph_osd_client *osdc, 1190 struct ceph_osd_request *req, int force_resend) 1191 { 1192 struct ceph_pg pgid; 1193 int acting[CEPH_PG_MAX_SIZE]; 1194 int o = -1, num = 0; 1195 int err; 1196 1197 dout("map_request %p tid %lld\n", req, req->r_tid); 1198 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap, 1199 ceph_file_layout_pg_pool(req->r_file_layout)); 1200 if (err) { 1201 list_move(&req->r_req_lru_item, &osdc->req_notarget); 1202 return err; 1203 } 1204 req->r_pgid = pgid; 1205 1206 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting); 1207 if (err > 0) { 1208 o = acting[0]; 1209 num = err; 1210 } 1211 1212 if ((!force_resend && 1213 req->r_osd && req->r_osd->o_osd == o && 1214 req->r_sent >= req->r_osd->o_incarnation && 1215 req->r_num_pg_osds == num && 1216 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) || 1217 (req->r_osd == NULL && o == -1)) 1218 return 0; /* no change */ 1219 1220 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n", 1221 req->r_tid, pgid.pool, pgid.seed, o, 1222 req->r_osd ? req->r_osd->o_osd : -1); 1223 1224 /* record full pg acting set */ 1225 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num); 1226 req->r_num_pg_osds = num; 1227 1228 if (req->r_osd) { 1229 __cancel_request(req); 1230 list_del_init(&req->r_osd_item); 1231 req->r_osd = NULL; 1232 } 1233 1234 req->r_osd = __lookup_osd(osdc, o); 1235 if (!req->r_osd && o >= 0) { 1236 err = -ENOMEM; 1237 req->r_osd = create_osd(osdc, o); 1238 if (!req->r_osd) { 1239 list_move(&req->r_req_lru_item, &osdc->req_notarget); 1240 goto out; 1241 } 1242 1243 dout("map_request osd %p is osd%d\n", req->r_osd, o); 1244 __insert_osd(osdc, req->r_osd); 1245 1246 ceph_con_open(&req->r_osd->o_con, 1247 CEPH_ENTITY_TYPE_OSD, o, 1248 &osdc->osdmap->osd_addr[o]); 1249 } 1250 1251 if (req->r_osd) { 1252 __remove_osd_from_lru(req->r_osd); 1253 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests); 1254 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent); 1255 } else { 1256 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget); 1257 } 1258 err = 1; /* osd or pg changed */ 1259 1260 out: 1261 return err; 1262 } 1263 1264 /* 1265 * caller should hold map_sem (for read) and request_mutex 1266 */ 1267 static void __send_request(struct ceph_osd_client *osdc, 1268 struct ceph_osd_request *req) 1269 { 1270 void *p; 1271 1272 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n", 1273 req, req->r_tid, req->r_osd->o_osd, req->r_flags, 1274 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed); 1275 1276 /* fill in message content that changes each time we send it */ 1277 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch); 1278 put_unaligned_le32(req->r_flags, req->r_request_flags); 1279 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool); 1280 p = req->r_request_pgid; 1281 ceph_encode_64(&p, req->r_pgid.pool); 1282 ceph_encode_32(&p, req->r_pgid.seed); 1283 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */ 1284 memcpy(req->r_request_reassert_version, &req->r_reassert_version, 1285 sizeof(req->r_reassert_version)); 1286 1287 req->r_stamp = jiffies; 1288 list_move_tail(&req->r_req_lru_item, &osdc->req_lru); 1289 1290 ceph_msg_get(req->r_request); /* send consumes a ref */ 1291 ceph_con_send(&req->r_osd->o_con, req->r_request); 1292 req->r_sent = req->r_osd->o_incarnation; 1293 } 1294 1295 /* 1296 * Send any requests in the queue (req_unsent). 1297 */ 1298 static void __send_queued(struct ceph_osd_client *osdc) 1299 { 1300 struct ceph_osd_request *req, *tmp; 1301 1302 dout("__send_queued\n"); 1303 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) 1304 __send_request(osdc, req); 1305 } 1306 1307 /* 1308 * Timeout callback, called every N seconds when 1 or more osd 1309 * requests has been active for more than N seconds. When this 1310 * happens, we ping all OSDs with requests who have timed out to 1311 * ensure any communications channel reset is detected. Reset the 1312 * request timeouts another N seconds in the future as we go. 1313 * Reschedule the timeout event another N seconds in future (unless 1314 * there are no open requests). 1315 */ 1316 static void handle_timeout(struct work_struct *work) 1317 { 1318 struct ceph_osd_client *osdc = 1319 container_of(work, struct ceph_osd_client, timeout_work.work); 1320 struct ceph_osd_request *req; 1321 struct ceph_osd *osd; 1322 unsigned long keepalive = 1323 osdc->client->options->osd_keepalive_timeout * HZ; 1324 struct list_head slow_osds; 1325 dout("timeout\n"); 1326 down_read(&osdc->map_sem); 1327 1328 ceph_monc_request_next_osdmap(&osdc->client->monc); 1329 1330 mutex_lock(&osdc->request_mutex); 1331 1332 /* 1333 * ping osds that are a bit slow. this ensures that if there 1334 * is a break in the TCP connection we will notice, and reopen 1335 * a connection with that osd (from the fault callback). 1336 */ 1337 INIT_LIST_HEAD(&slow_osds); 1338 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) { 1339 if (time_before(jiffies, req->r_stamp + keepalive)) 1340 break; 1341 1342 osd = req->r_osd; 1343 BUG_ON(!osd); 1344 dout(" tid %llu is slow, will send keepalive on osd%d\n", 1345 req->r_tid, osd->o_osd); 1346 list_move_tail(&osd->o_keepalive_item, &slow_osds); 1347 } 1348 while (!list_empty(&slow_osds)) { 1349 osd = list_entry(slow_osds.next, struct ceph_osd, 1350 o_keepalive_item); 1351 list_del_init(&osd->o_keepalive_item); 1352 ceph_con_keepalive(&osd->o_con); 1353 } 1354 1355 __schedule_osd_timeout(osdc); 1356 __send_queued(osdc); 1357 mutex_unlock(&osdc->request_mutex); 1358 up_read(&osdc->map_sem); 1359 } 1360 1361 static void handle_osds_timeout(struct work_struct *work) 1362 { 1363 struct ceph_osd_client *osdc = 1364 container_of(work, struct ceph_osd_client, 1365 osds_timeout_work.work); 1366 unsigned long delay = 1367 osdc->client->options->osd_idle_ttl * HZ >> 2; 1368 1369 dout("osds timeout\n"); 1370 down_read(&osdc->map_sem); 1371 remove_old_osds(osdc); 1372 up_read(&osdc->map_sem); 1373 1374 schedule_delayed_work(&osdc->osds_timeout_work, 1375 round_jiffies_relative(delay)); 1376 } 1377 1378 static void complete_request(struct ceph_osd_request *req) 1379 { 1380 if (req->r_safe_callback) 1381 req->r_safe_callback(req, NULL); 1382 complete_all(&req->r_safe_completion); /* fsync waiter */ 1383 } 1384 1385 /* 1386 * handle osd op reply. either call the callback if it is specified, 1387 * or do the completion to wake up the waiting thread. 1388 */ 1389 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg, 1390 struct ceph_connection *con) 1391 { 1392 void *p, *end; 1393 struct ceph_osd_request *req; 1394 u64 tid; 1395 int object_len; 1396 unsigned int numops; 1397 int payload_len, flags; 1398 s32 result; 1399 s32 retry_attempt; 1400 struct ceph_pg pg; 1401 int err; 1402 u32 reassert_epoch; 1403 u64 reassert_version; 1404 u32 osdmap_epoch; 1405 int already_completed; 1406 u32 bytes; 1407 unsigned int i; 1408 1409 tid = le64_to_cpu(msg->hdr.tid); 1410 dout("handle_reply %p tid %llu\n", msg, tid); 1411 1412 p = msg->front.iov_base; 1413 end = p + msg->front.iov_len; 1414 1415 ceph_decode_need(&p, end, 4, bad); 1416 object_len = ceph_decode_32(&p); 1417 ceph_decode_need(&p, end, object_len, bad); 1418 p += object_len; 1419 1420 err = ceph_decode_pgid(&p, end, &pg); 1421 if (err) 1422 goto bad; 1423 1424 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad); 1425 flags = ceph_decode_64(&p); 1426 result = ceph_decode_32(&p); 1427 reassert_epoch = ceph_decode_32(&p); 1428 reassert_version = ceph_decode_64(&p); 1429 osdmap_epoch = ceph_decode_32(&p); 1430 1431 /* lookup */ 1432 mutex_lock(&osdc->request_mutex); 1433 req = __lookup_request(osdc, tid); 1434 if (req == NULL) { 1435 dout("handle_reply tid %llu dne\n", tid); 1436 goto bad_mutex; 1437 } 1438 ceph_osdc_get_request(req); 1439 1440 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid, 1441 req, result); 1442 1443 ceph_decode_need(&p, end, 4, bad); 1444 numops = ceph_decode_32(&p); 1445 if (numops > CEPH_OSD_MAX_OP) 1446 goto bad_put; 1447 if (numops != req->r_num_ops) 1448 goto bad_put; 1449 payload_len = 0; 1450 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad); 1451 for (i = 0; i < numops; i++) { 1452 struct ceph_osd_op *op = p; 1453 int len; 1454 1455 len = le32_to_cpu(op->payload_len); 1456 req->r_reply_op_len[i] = len; 1457 dout(" op %d has %d bytes\n", i, len); 1458 payload_len += len; 1459 p += sizeof(*op); 1460 } 1461 bytes = le32_to_cpu(msg->hdr.data_len); 1462 if (payload_len != bytes) { 1463 pr_warning("sum of op payload lens %d != data_len %d", 1464 payload_len, bytes); 1465 goto bad_put; 1466 } 1467 1468 ceph_decode_need(&p, end, 4 + numops * 4, bad); 1469 retry_attempt = ceph_decode_32(&p); 1470 for (i = 0; i < numops; i++) 1471 req->r_reply_op_result[i] = ceph_decode_32(&p); 1472 1473 if (!req->r_got_reply) { 1474 1475 req->r_result = result; 1476 dout("handle_reply result %d bytes %d\n", req->r_result, 1477 bytes); 1478 if (req->r_result == 0) 1479 req->r_result = bytes; 1480 1481 /* in case this is a write and we need to replay, */ 1482 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch); 1483 req->r_reassert_version.version = cpu_to_le64(reassert_version); 1484 1485 req->r_got_reply = 1; 1486 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) { 1487 dout("handle_reply tid %llu dup ack\n", tid); 1488 mutex_unlock(&osdc->request_mutex); 1489 goto done; 1490 } 1491 1492 dout("handle_reply tid %llu flags %d\n", tid, flags); 1493 1494 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK)) 1495 __register_linger_request(osdc, req); 1496 1497 /* either this is a read, or we got the safe response */ 1498 if (result < 0 || 1499 (flags & CEPH_OSD_FLAG_ONDISK) || 1500 ((flags & CEPH_OSD_FLAG_WRITE) == 0)) 1501 __unregister_request(osdc, req); 1502 1503 already_completed = req->r_completed; 1504 req->r_completed = 1; 1505 mutex_unlock(&osdc->request_mutex); 1506 if (already_completed) 1507 goto done; 1508 1509 if (req->r_callback) 1510 req->r_callback(req, msg); 1511 else 1512 complete_all(&req->r_completion); 1513 1514 if (flags & CEPH_OSD_FLAG_ONDISK) 1515 complete_request(req); 1516 1517 done: 1518 dout("req=%p req->r_linger=%d\n", req, req->r_linger); 1519 ceph_osdc_put_request(req); 1520 return; 1521 1522 bad_put: 1523 ceph_osdc_put_request(req); 1524 bad_mutex: 1525 mutex_unlock(&osdc->request_mutex); 1526 bad: 1527 pr_err("corrupt osd_op_reply got %d %d\n", 1528 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len)); 1529 ceph_msg_dump(msg); 1530 } 1531 1532 static void reset_changed_osds(struct ceph_osd_client *osdc) 1533 { 1534 struct rb_node *p, *n; 1535 1536 for (p = rb_first(&osdc->osds); p; p = n) { 1537 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node); 1538 1539 n = rb_next(p); 1540 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || 1541 memcmp(&osd->o_con.peer_addr, 1542 ceph_osd_addr(osdc->osdmap, 1543 osd->o_osd), 1544 sizeof(struct ceph_entity_addr)) != 0) 1545 __reset_osd(osdc, osd); 1546 } 1547 } 1548 1549 /* 1550 * Requeue requests whose mapping to an OSD has changed. If requests map to 1551 * no osd, request a new map. 1552 * 1553 * Caller should hold map_sem for read. 1554 */ 1555 static void kick_requests(struct ceph_osd_client *osdc, int force_resend) 1556 { 1557 struct ceph_osd_request *req, *nreq; 1558 struct rb_node *p; 1559 int needmap = 0; 1560 int err; 1561 1562 dout("kick_requests %s\n", force_resend ? " (force resend)" : ""); 1563 mutex_lock(&osdc->request_mutex); 1564 for (p = rb_first(&osdc->requests); p; ) { 1565 req = rb_entry(p, struct ceph_osd_request, r_node); 1566 p = rb_next(p); 1567 1568 /* 1569 * For linger requests that have not yet been 1570 * registered, move them to the linger list; they'll 1571 * be sent to the osd in the loop below. Unregister 1572 * the request before re-registering it as a linger 1573 * request to ensure the __map_request() below 1574 * will decide it needs to be sent. 1575 */ 1576 if (req->r_linger && list_empty(&req->r_linger_item)) { 1577 dout("%p tid %llu restart on osd%d\n", 1578 req, req->r_tid, 1579 req->r_osd ? req->r_osd->o_osd : -1); 1580 __unregister_request(osdc, req); 1581 __register_linger_request(osdc, req); 1582 continue; 1583 } 1584 1585 err = __map_request(osdc, req, force_resend); 1586 if (err < 0) 1587 continue; /* error */ 1588 if (req->r_osd == NULL) { 1589 dout("%p tid %llu maps to no osd\n", req, req->r_tid); 1590 needmap++; /* request a newer map */ 1591 } else if (err > 0) { 1592 if (!req->r_linger) { 1593 dout("%p tid %llu requeued on osd%d\n", req, 1594 req->r_tid, 1595 req->r_osd ? req->r_osd->o_osd : -1); 1596 req->r_flags |= CEPH_OSD_FLAG_RETRY; 1597 } 1598 } 1599 } 1600 1601 list_for_each_entry_safe(req, nreq, &osdc->req_linger, 1602 r_linger_item) { 1603 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd); 1604 1605 err = __map_request(osdc, req, force_resend); 1606 dout("__map_request returned %d\n", err); 1607 if (err == 0) 1608 continue; /* no change and no osd was specified */ 1609 if (err < 0) 1610 continue; /* hrm! */ 1611 if (req->r_osd == NULL) { 1612 dout("tid %llu maps to no valid osd\n", req->r_tid); 1613 needmap++; /* request a newer map */ 1614 continue; 1615 } 1616 1617 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid, 1618 req->r_osd ? req->r_osd->o_osd : -1); 1619 __register_request(osdc, req); 1620 __unregister_linger_request(osdc, req); 1621 } 1622 mutex_unlock(&osdc->request_mutex); 1623 1624 if (needmap) { 1625 dout("%d requests for down osds, need new map\n", needmap); 1626 ceph_monc_request_next_osdmap(&osdc->client->monc); 1627 } 1628 reset_changed_osds(osdc); 1629 } 1630 1631 1632 /* 1633 * Process updated osd map. 1634 * 1635 * The message contains any number of incremental and full maps, normally 1636 * indicating some sort of topology change in the cluster. Kick requests 1637 * off to different OSDs as needed. 1638 */ 1639 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) 1640 { 1641 void *p, *end, *next; 1642 u32 nr_maps, maplen; 1643 u32 epoch; 1644 struct ceph_osdmap *newmap = NULL, *oldmap; 1645 int err; 1646 struct ceph_fsid fsid; 1647 1648 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0); 1649 p = msg->front.iov_base; 1650 end = p + msg->front.iov_len; 1651 1652 /* verify fsid */ 1653 ceph_decode_need(&p, end, sizeof(fsid), bad); 1654 ceph_decode_copy(&p, &fsid, sizeof(fsid)); 1655 if (ceph_check_fsid(osdc->client, &fsid) < 0) 1656 return; 1657 1658 down_write(&osdc->map_sem); 1659 1660 /* incremental maps */ 1661 ceph_decode_32_safe(&p, end, nr_maps, bad); 1662 dout(" %d inc maps\n", nr_maps); 1663 while (nr_maps > 0) { 1664 ceph_decode_need(&p, end, 2*sizeof(u32), bad); 1665 epoch = ceph_decode_32(&p); 1666 maplen = ceph_decode_32(&p); 1667 ceph_decode_need(&p, end, maplen, bad); 1668 next = p + maplen; 1669 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) { 1670 dout("applying incremental map %u len %d\n", 1671 epoch, maplen); 1672 newmap = osdmap_apply_incremental(&p, next, 1673 osdc->osdmap, 1674 &osdc->client->msgr); 1675 if (IS_ERR(newmap)) { 1676 err = PTR_ERR(newmap); 1677 goto bad; 1678 } 1679 BUG_ON(!newmap); 1680 if (newmap != osdc->osdmap) { 1681 ceph_osdmap_destroy(osdc->osdmap); 1682 osdc->osdmap = newmap; 1683 } 1684 kick_requests(osdc, 0); 1685 } else { 1686 dout("ignoring incremental map %u len %d\n", 1687 epoch, maplen); 1688 } 1689 p = next; 1690 nr_maps--; 1691 } 1692 if (newmap) 1693 goto done; 1694 1695 /* full maps */ 1696 ceph_decode_32_safe(&p, end, nr_maps, bad); 1697 dout(" %d full maps\n", nr_maps); 1698 while (nr_maps) { 1699 ceph_decode_need(&p, end, 2*sizeof(u32), bad); 1700 epoch = ceph_decode_32(&p); 1701 maplen = ceph_decode_32(&p); 1702 ceph_decode_need(&p, end, maplen, bad); 1703 if (nr_maps > 1) { 1704 dout("skipping non-latest full map %u len %d\n", 1705 epoch, maplen); 1706 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) { 1707 dout("skipping full map %u len %d, " 1708 "older than our %u\n", epoch, maplen, 1709 osdc->osdmap->epoch); 1710 } else { 1711 int skipped_map = 0; 1712 1713 dout("taking full map %u len %d\n", epoch, maplen); 1714 newmap = osdmap_decode(&p, p+maplen); 1715 if (IS_ERR(newmap)) { 1716 err = PTR_ERR(newmap); 1717 goto bad; 1718 } 1719 BUG_ON(!newmap); 1720 oldmap = osdc->osdmap; 1721 osdc->osdmap = newmap; 1722 if (oldmap) { 1723 if (oldmap->epoch + 1 < newmap->epoch) 1724 skipped_map = 1; 1725 ceph_osdmap_destroy(oldmap); 1726 } 1727 kick_requests(osdc, skipped_map); 1728 } 1729 p += maplen; 1730 nr_maps--; 1731 } 1732 1733 done: 1734 downgrade_write(&osdc->map_sem); 1735 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch); 1736 1737 /* 1738 * subscribe to subsequent osdmap updates if full to ensure 1739 * we find out when we are no longer full and stop returning 1740 * ENOSPC. 1741 */ 1742 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) 1743 ceph_monc_request_next_osdmap(&osdc->client->monc); 1744 1745 mutex_lock(&osdc->request_mutex); 1746 __send_queued(osdc); 1747 mutex_unlock(&osdc->request_mutex); 1748 up_read(&osdc->map_sem); 1749 wake_up_all(&osdc->client->auth_wq); 1750 return; 1751 1752 bad: 1753 pr_err("osdc handle_map corrupt msg\n"); 1754 ceph_msg_dump(msg); 1755 up_write(&osdc->map_sem); 1756 return; 1757 } 1758 1759 /* 1760 * watch/notify callback event infrastructure 1761 * 1762 * These callbacks are used both for watch and notify operations. 1763 */ 1764 static void __release_event(struct kref *kref) 1765 { 1766 struct ceph_osd_event *event = 1767 container_of(kref, struct ceph_osd_event, kref); 1768 1769 dout("__release_event %p\n", event); 1770 kfree(event); 1771 } 1772 1773 static void get_event(struct ceph_osd_event *event) 1774 { 1775 kref_get(&event->kref); 1776 } 1777 1778 void ceph_osdc_put_event(struct ceph_osd_event *event) 1779 { 1780 kref_put(&event->kref, __release_event); 1781 } 1782 EXPORT_SYMBOL(ceph_osdc_put_event); 1783 1784 static void __insert_event(struct ceph_osd_client *osdc, 1785 struct ceph_osd_event *new) 1786 { 1787 struct rb_node **p = &osdc->event_tree.rb_node; 1788 struct rb_node *parent = NULL; 1789 struct ceph_osd_event *event = NULL; 1790 1791 while (*p) { 1792 parent = *p; 1793 event = rb_entry(parent, struct ceph_osd_event, node); 1794 if (new->cookie < event->cookie) 1795 p = &(*p)->rb_left; 1796 else if (new->cookie > event->cookie) 1797 p = &(*p)->rb_right; 1798 else 1799 BUG(); 1800 } 1801 1802 rb_link_node(&new->node, parent, p); 1803 rb_insert_color(&new->node, &osdc->event_tree); 1804 } 1805 1806 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc, 1807 u64 cookie) 1808 { 1809 struct rb_node **p = &osdc->event_tree.rb_node; 1810 struct rb_node *parent = NULL; 1811 struct ceph_osd_event *event = NULL; 1812 1813 while (*p) { 1814 parent = *p; 1815 event = rb_entry(parent, struct ceph_osd_event, node); 1816 if (cookie < event->cookie) 1817 p = &(*p)->rb_left; 1818 else if (cookie > event->cookie) 1819 p = &(*p)->rb_right; 1820 else 1821 return event; 1822 } 1823 return NULL; 1824 } 1825 1826 static void __remove_event(struct ceph_osd_event *event) 1827 { 1828 struct ceph_osd_client *osdc = event->osdc; 1829 1830 if (!RB_EMPTY_NODE(&event->node)) { 1831 dout("__remove_event removed %p\n", event); 1832 rb_erase(&event->node, &osdc->event_tree); 1833 ceph_osdc_put_event(event); 1834 } else { 1835 dout("__remove_event didn't remove %p\n", event); 1836 } 1837 } 1838 1839 int ceph_osdc_create_event(struct ceph_osd_client *osdc, 1840 void (*event_cb)(u64, u64, u8, void *), 1841 void *data, struct ceph_osd_event **pevent) 1842 { 1843 struct ceph_osd_event *event; 1844 1845 event = kmalloc(sizeof(*event), GFP_NOIO); 1846 if (!event) 1847 return -ENOMEM; 1848 1849 dout("create_event %p\n", event); 1850 event->cb = event_cb; 1851 event->one_shot = 0; 1852 event->data = data; 1853 event->osdc = osdc; 1854 INIT_LIST_HEAD(&event->osd_node); 1855 RB_CLEAR_NODE(&event->node); 1856 kref_init(&event->kref); /* one ref for us */ 1857 kref_get(&event->kref); /* one ref for the caller */ 1858 1859 spin_lock(&osdc->event_lock); 1860 event->cookie = ++osdc->event_count; 1861 __insert_event(osdc, event); 1862 spin_unlock(&osdc->event_lock); 1863 1864 *pevent = event; 1865 return 0; 1866 } 1867 EXPORT_SYMBOL(ceph_osdc_create_event); 1868 1869 void ceph_osdc_cancel_event(struct ceph_osd_event *event) 1870 { 1871 struct ceph_osd_client *osdc = event->osdc; 1872 1873 dout("cancel_event %p\n", event); 1874 spin_lock(&osdc->event_lock); 1875 __remove_event(event); 1876 spin_unlock(&osdc->event_lock); 1877 ceph_osdc_put_event(event); /* caller's */ 1878 } 1879 EXPORT_SYMBOL(ceph_osdc_cancel_event); 1880 1881 1882 static void do_event_work(struct work_struct *work) 1883 { 1884 struct ceph_osd_event_work *event_work = 1885 container_of(work, struct ceph_osd_event_work, work); 1886 struct ceph_osd_event *event = event_work->event; 1887 u64 ver = event_work->ver; 1888 u64 notify_id = event_work->notify_id; 1889 u8 opcode = event_work->opcode; 1890 1891 dout("do_event_work completing %p\n", event); 1892 event->cb(ver, notify_id, opcode, event->data); 1893 dout("do_event_work completed %p\n", event); 1894 ceph_osdc_put_event(event); 1895 kfree(event_work); 1896 } 1897 1898 1899 /* 1900 * Process osd watch notifications 1901 */ 1902 static void handle_watch_notify(struct ceph_osd_client *osdc, 1903 struct ceph_msg *msg) 1904 { 1905 void *p, *end; 1906 u8 proto_ver; 1907 u64 cookie, ver, notify_id; 1908 u8 opcode; 1909 struct ceph_osd_event *event; 1910 struct ceph_osd_event_work *event_work; 1911 1912 p = msg->front.iov_base; 1913 end = p + msg->front.iov_len; 1914 1915 ceph_decode_8_safe(&p, end, proto_ver, bad); 1916 ceph_decode_8_safe(&p, end, opcode, bad); 1917 ceph_decode_64_safe(&p, end, cookie, bad); 1918 ceph_decode_64_safe(&p, end, ver, bad); 1919 ceph_decode_64_safe(&p, end, notify_id, bad); 1920 1921 spin_lock(&osdc->event_lock); 1922 event = __find_event(osdc, cookie); 1923 if (event) { 1924 BUG_ON(event->one_shot); 1925 get_event(event); 1926 } 1927 spin_unlock(&osdc->event_lock); 1928 dout("handle_watch_notify cookie %lld ver %lld event %p\n", 1929 cookie, ver, event); 1930 if (event) { 1931 event_work = kmalloc(sizeof(*event_work), GFP_NOIO); 1932 if (!event_work) { 1933 dout("ERROR: could not allocate event_work\n"); 1934 goto done_err; 1935 } 1936 INIT_WORK(&event_work->work, do_event_work); 1937 event_work->event = event; 1938 event_work->ver = ver; 1939 event_work->notify_id = notify_id; 1940 event_work->opcode = opcode; 1941 if (!queue_work(osdc->notify_wq, &event_work->work)) { 1942 dout("WARNING: failed to queue notify event work\n"); 1943 goto done_err; 1944 } 1945 } 1946 1947 return; 1948 1949 done_err: 1950 ceph_osdc_put_event(event); 1951 return; 1952 1953 bad: 1954 pr_err("osdc handle_watch_notify corrupt msg\n"); 1955 return; 1956 } 1957 1958 /* 1959 * build new request AND message 1960 * 1961 */ 1962 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off, 1963 struct ceph_snap_context *snapc, u64 snap_id, 1964 struct timespec *mtime) 1965 { 1966 struct ceph_msg *msg = req->r_request; 1967 void *p; 1968 size_t msg_size; 1969 int flags = req->r_flags; 1970 u64 data_len; 1971 unsigned int i; 1972 1973 req->r_snapid = snap_id; 1974 req->r_snapc = ceph_get_snap_context(snapc); 1975 1976 /* encode request */ 1977 msg->hdr.version = cpu_to_le16(4); 1978 1979 p = msg->front.iov_base; 1980 ceph_encode_32(&p, 1); /* client_inc is always 1 */ 1981 req->r_request_osdmap_epoch = p; 1982 p += 4; 1983 req->r_request_flags = p; 1984 p += 4; 1985 if (req->r_flags & CEPH_OSD_FLAG_WRITE) 1986 ceph_encode_timespec(p, mtime); 1987 p += sizeof(struct ceph_timespec); 1988 req->r_request_reassert_version = p; 1989 p += sizeof(struct ceph_eversion); /* will get filled in */ 1990 1991 /* oloc */ 1992 ceph_encode_8(&p, 4); 1993 ceph_encode_8(&p, 4); 1994 ceph_encode_32(&p, 8 + 4 + 4); 1995 req->r_request_pool = p; 1996 p += 8; 1997 ceph_encode_32(&p, -1); /* preferred */ 1998 ceph_encode_32(&p, 0); /* key len */ 1999 2000 ceph_encode_8(&p, 1); 2001 req->r_request_pgid = p; 2002 p += 8 + 4; 2003 ceph_encode_32(&p, -1); /* preferred */ 2004 2005 /* oid */ 2006 ceph_encode_32(&p, req->r_oid_len); 2007 memcpy(p, req->r_oid, req->r_oid_len); 2008 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len); 2009 p += req->r_oid_len; 2010 2011 /* ops--can imply data */ 2012 ceph_encode_16(&p, (u16)req->r_num_ops); 2013 data_len = 0; 2014 for (i = 0; i < req->r_num_ops; i++) { 2015 data_len += osd_req_encode_op(req, p, i); 2016 p += sizeof(struct ceph_osd_op); 2017 } 2018 2019 /* snaps */ 2020 ceph_encode_64(&p, req->r_snapid); 2021 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0); 2022 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0); 2023 if (req->r_snapc) { 2024 for (i = 0; i < snapc->num_snaps; i++) { 2025 ceph_encode_64(&p, req->r_snapc->snaps[i]); 2026 } 2027 } 2028 2029 req->r_request_attempts = p; 2030 p += 4; 2031 2032 /* data */ 2033 if (flags & CEPH_OSD_FLAG_WRITE) { 2034 u16 data_off; 2035 2036 /* 2037 * The header "data_off" is a hint to the receiver 2038 * allowing it to align received data into its 2039 * buffers such that there's no need to re-copy 2040 * it before writing it to disk (direct I/O). 2041 */ 2042 data_off = (u16) (off & 0xffff); 2043 req->r_request->hdr.data_off = cpu_to_le16(data_off); 2044 } 2045 req->r_request->hdr.data_len = cpu_to_le32(data_len); 2046 2047 BUG_ON(p > msg->front.iov_base + msg->front.iov_len); 2048 msg_size = p - msg->front.iov_base; 2049 msg->front.iov_len = msg_size; 2050 msg->hdr.front_len = cpu_to_le32(msg_size); 2051 2052 dout("build_request msg_size was %d\n", (int)msg_size); 2053 } 2054 EXPORT_SYMBOL(ceph_osdc_build_request); 2055 2056 /* 2057 * Register request, send initial attempt. 2058 */ 2059 int ceph_osdc_start_request(struct ceph_osd_client *osdc, 2060 struct ceph_osd_request *req, 2061 bool nofail) 2062 { 2063 int rc = 0; 2064 2065 down_read(&osdc->map_sem); 2066 mutex_lock(&osdc->request_mutex); 2067 __register_request(osdc, req); 2068 WARN_ON(req->r_sent); 2069 rc = __map_request(osdc, req, 0); 2070 if (rc < 0) { 2071 if (nofail) { 2072 dout("osdc_start_request failed map, " 2073 " will retry %lld\n", req->r_tid); 2074 rc = 0; 2075 } 2076 goto out_unlock; 2077 } 2078 if (req->r_osd == NULL) { 2079 dout("send_request %p no up osds in pg\n", req); 2080 ceph_monc_request_next_osdmap(&osdc->client->monc); 2081 } else { 2082 __send_queued(osdc); 2083 } 2084 rc = 0; 2085 out_unlock: 2086 mutex_unlock(&osdc->request_mutex); 2087 up_read(&osdc->map_sem); 2088 return rc; 2089 } 2090 EXPORT_SYMBOL(ceph_osdc_start_request); 2091 2092 /* 2093 * wait for a request to complete 2094 */ 2095 int ceph_osdc_wait_request(struct ceph_osd_client *osdc, 2096 struct ceph_osd_request *req) 2097 { 2098 int rc; 2099 2100 rc = wait_for_completion_interruptible(&req->r_completion); 2101 if (rc < 0) { 2102 mutex_lock(&osdc->request_mutex); 2103 __cancel_request(req); 2104 __unregister_request(osdc, req); 2105 mutex_unlock(&osdc->request_mutex); 2106 complete_request(req); 2107 dout("wait_request tid %llu canceled/timed out\n", req->r_tid); 2108 return rc; 2109 } 2110 2111 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result); 2112 return req->r_result; 2113 } 2114 EXPORT_SYMBOL(ceph_osdc_wait_request); 2115 2116 /* 2117 * sync - wait for all in-flight requests to flush. avoid starvation. 2118 */ 2119 void ceph_osdc_sync(struct ceph_osd_client *osdc) 2120 { 2121 struct ceph_osd_request *req; 2122 u64 last_tid, next_tid = 0; 2123 2124 mutex_lock(&osdc->request_mutex); 2125 last_tid = osdc->last_tid; 2126 while (1) { 2127 req = __lookup_request_ge(osdc, next_tid); 2128 if (!req) 2129 break; 2130 if (req->r_tid > last_tid) 2131 break; 2132 2133 next_tid = req->r_tid + 1; 2134 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0) 2135 continue; 2136 2137 ceph_osdc_get_request(req); 2138 mutex_unlock(&osdc->request_mutex); 2139 dout("sync waiting on tid %llu (last is %llu)\n", 2140 req->r_tid, last_tid); 2141 wait_for_completion(&req->r_safe_completion); 2142 mutex_lock(&osdc->request_mutex); 2143 ceph_osdc_put_request(req); 2144 } 2145 mutex_unlock(&osdc->request_mutex); 2146 dout("sync done (thru tid %llu)\n", last_tid); 2147 } 2148 EXPORT_SYMBOL(ceph_osdc_sync); 2149 2150 /* 2151 * init, shutdown 2152 */ 2153 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) 2154 { 2155 int err; 2156 2157 dout("init\n"); 2158 osdc->client = client; 2159 osdc->osdmap = NULL; 2160 init_rwsem(&osdc->map_sem); 2161 init_completion(&osdc->map_waiters); 2162 osdc->last_requested_map = 0; 2163 mutex_init(&osdc->request_mutex); 2164 osdc->last_tid = 0; 2165 osdc->osds = RB_ROOT; 2166 INIT_LIST_HEAD(&osdc->osd_lru); 2167 osdc->requests = RB_ROOT; 2168 INIT_LIST_HEAD(&osdc->req_lru); 2169 INIT_LIST_HEAD(&osdc->req_unsent); 2170 INIT_LIST_HEAD(&osdc->req_notarget); 2171 INIT_LIST_HEAD(&osdc->req_linger); 2172 osdc->num_requests = 0; 2173 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); 2174 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); 2175 spin_lock_init(&osdc->event_lock); 2176 osdc->event_tree = RB_ROOT; 2177 osdc->event_count = 0; 2178 2179 schedule_delayed_work(&osdc->osds_timeout_work, 2180 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ)); 2181 2182 err = -ENOMEM; 2183 osdc->req_mempool = mempool_create_kmalloc_pool(10, 2184 sizeof(struct ceph_osd_request)); 2185 if (!osdc->req_mempool) 2186 goto out; 2187 2188 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP, 2189 OSD_OP_FRONT_LEN, 10, true, 2190 "osd_op"); 2191 if (err < 0) 2192 goto out_mempool; 2193 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY, 2194 OSD_OPREPLY_FRONT_LEN, 10, true, 2195 "osd_op_reply"); 2196 if (err < 0) 2197 goto out_msgpool; 2198 2199 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify"); 2200 if (IS_ERR(osdc->notify_wq)) { 2201 err = PTR_ERR(osdc->notify_wq); 2202 osdc->notify_wq = NULL; 2203 goto out_msgpool; 2204 } 2205 return 0; 2206 2207 out_msgpool: 2208 ceph_msgpool_destroy(&osdc->msgpool_op); 2209 out_mempool: 2210 mempool_destroy(osdc->req_mempool); 2211 out: 2212 return err; 2213 } 2214 2215 void ceph_osdc_stop(struct ceph_osd_client *osdc) 2216 { 2217 flush_workqueue(osdc->notify_wq); 2218 destroy_workqueue(osdc->notify_wq); 2219 cancel_delayed_work_sync(&osdc->timeout_work); 2220 cancel_delayed_work_sync(&osdc->osds_timeout_work); 2221 if (osdc->osdmap) { 2222 ceph_osdmap_destroy(osdc->osdmap); 2223 osdc->osdmap = NULL; 2224 } 2225 remove_all_osds(osdc); 2226 mempool_destroy(osdc->req_mempool); 2227 ceph_msgpool_destroy(&osdc->msgpool_op); 2228 ceph_msgpool_destroy(&osdc->msgpool_op_reply); 2229 } 2230 2231 /* 2232 * Read some contiguous pages. If we cross a stripe boundary, shorten 2233 * *plen. Return number of bytes read, or error. 2234 */ 2235 int ceph_osdc_readpages(struct ceph_osd_client *osdc, 2236 struct ceph_vino vino, struct ceph_file_layout *layout, 2237 u64 off, u64 *plen, 2238 u32 truncate_seq, u64 truncate_size, 2239 struct page **pages, int num_pages, int page_align) 2240 { 2241 struct ceph_osd_request *req; 2242 int rc = 0; 2243 2244 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, 2245 vino.snap, off, *plen); 2246 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1, 2247 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, 2248 NULL, truncate_seq, truncate_size, 2249 false); 2250 if (IS_ERR(req)) 2251 return PTR_ERR(req); 2252 2253 /* it may be a short read due to an object boundary */ 2254 2255 osd_req_op_extent_osd_data_pages(req, 0, false, 2256 pages, *plen, page_align, false, false); 2257 2258 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n", 2259 off, *plen, *plen, page_align); 2260 2261 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL); 2262 2263 rc = ceph_osdc_start_request(osdc, req, false); 2264 if (!rc) 2265 rc = ceph_osdc_wait_request(osdc, req); 2266 2267 ceph_osdc_put_request(req); 2268 dout("readpages result %d\n", rc); 2269 return rc; 2270 } 2271 EXPORT_SYMBOL(ceph_osdc_readpages); 2272 2273 /* 2274 * do a synchronous write on N pages 2275 */ 2276 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, 2277 struct ceph_file_layout *layout, 2278 struct ceph_snap_context *snapc, 2279 u64 off, u64 len, 2280 u32 truncate_seq, u64 truncate_size, 2281 struct timespec *mtime, 2282 struct page **pages, int num_pages) 2283 { 2284 struct ceph_osd_request *req; 2285 int rc = 0; 2286 int page_align = off & ~PAGE_MASK; 2287 2288 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */ 2289 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1, 2290 CEPH_OSD_OP_WRITE, 2291 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, 2292 snapc, truncate_seq, truncate_size, 2293 true); 2294 if (IS_ERR(req)) 2295 return PTR_ERR(req); 2296 2297 /* it may be a short write due to an object boundary */ 2298 osd_req_op_extent_osd_data_pages(req, 0, true, pages, len, page_align, 2299 false, false); 2300 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len); 2301 2302 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime); 2303 2304 rc = ceph_osdc_start_request(osdc, req, true); 2305 if (!rc) 2306 rc = ceph_osdc_wait_request(osdc, req); 2307 2308 ceph_osdc_put_request(req); 2309 if (rc == 0) 2310 rc = len; 2311 dout("writepages result %d\n", rc); 2312 return rc; 2313 } 2314 EXPORT_SYMBOL(ceph_osdc_writepages); 2315 2316 /* 2317 * handle incoming message 2318 */ 2319 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) 2320 { 2321 struct ceph_osd *osd = con->private; 2322 struct ceph_osd_client *osdc; 2323 int type = le16_to_cpu(msg->hdr.type); 2324 2325 if (!osd) 2326 goto out; 2327 osdc = osd->o_osdc; 2328 2329 switch (type) { 2330 case CEPH_MSG_OSD_MAP: 2331 ceph_osdc_handle_map(osdc, msg); 2332 break; 2333 case CEPH_MSG_OSD_OPREPLY: 2334 handle_reply(osdc, msg, con); 2335 break; 2336 case CEPH_MSG_WATCH_NOTIFY: 2337 handle_watch_notify(osdc, msg); 2338 break; 2339 2340 default: 2341 pr_err("received unknown message type %d %s\n", type, 2342 ceph_msg_type_name(type)); 2343 } 2344 out: 2345 ceph_msg_put(msg); 2346 } 2347 2348 /* 2349 * lookup and return message for incoming reply. set up reply message 2350 * pages. 2351 */ 2352 static struct ceph_msg *get_reply(struct ceph_connection *con, 2353 struct ceph_msg_header *hdr, 2354 int *skip) 2355 { 2356 struct ceph_osd *osd = con->private; 2357 struct ceph_osd_client *osdc = osd->o_osdc; 2358 struct ceph_msg *m; 2359 struct ceph_osd_request *req; 2360 int front = le32_to_cpu(hdr->front_len); 2361 int data_len = le32_to_cpu(hdr->data_len); 2362 u64 tid; 2363 2364 tid = le64_to_cpu(hdr->tid); 2365 mutex_lock(&osdc->request_mutex); 2366 req = __lookup_request(osdc, tid); 2367 if (!req) { 2368 *skip = 1; 2369 m = NULL; 2370 dout("get_reply unknown tid %llu from osd%d\n", tid, 2371 osd->o_osd); 2372 goto out; 2373 } 2374 2375 if (req->r_reply->con) 2376 dout("%s revoking msg %p from old con %p\n", __func__, 2377 req->r_reply, req->r_reply->con); 2378 ceph_msg_revoke_incoming(req->r_reply); 2379 2380 if (front > req->r_reply->front.iov_len) { 2381 pr_warning("get_reply front %d > preallocated %d\n", 2382 front, (int)req->r_reply->front.iov_len); 2383 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false); 2384 if (!m) 2385 goto out; 2386 ceph_msg_put(req->r_reply); 2387 req->r_reply = m; 2388 } 2389 m = ceph_msg_get(req->r_reply); 2390 2391 if (data_len > 0) { 2392 struct ceph_osd_data *osd_data; 2393 2394 /* 2395 * XXX This is assuming there is only one op containing 2396 * XXX page data. Probably OK for reads, but this 2397 * XXX ought to be done more generally. 2398 */ 2399 osd_data = osd_req_op_extent_osd_data(req, 0, false); 2400 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) { 2401 if (osd_data->pages && 2402 unlikely(osd_data->length < data_len)) { 2403 2404 pr_warning("tid %lld reply has %d bytes " 2405 "we had only %llu bytes ready\n", 2406 tid, data_len, osd_data->length); 2407 *skip = 1; 2408 ceph_msg_put(m); 2409 m = NULL; 2410 goto out; 2411 } 2412 } 2413 } 2414 *skip = 0; 2415 dout("get_reply tid %lld %p\n", tid, m); 2416 2417 out: 2418 mutex_unlock(&osdc->request_mutex); 2419 return m; 2420 2421 } 2422 2423 static struct ceph_msg *alloc_msg(struct ceph_connection *con, 2424 struct ceph_msg_header *hdr, 2425 int *skip) 2426 { 2427 struct ceph_osd *osd = con->private; 2428 int type = le16_to_cpu(hdr->type); 2429 int front = le32_to_cpu(hdr->front_len); 2430 2431 *skip = 0; 2432 switch (type) { 2433 case CEPH_MSG_OSD_MAP: 2434 case CEPH_MSG_WATCH_NOTIFY: 2435 return ceph_msg_new(type, front, GFP_NOFS, false); 2436 case CEPH_MSG_OSD_OPREPLY: 2437 return get_reply(con, hdr, skip); 2438 default: 2439 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type, 2440 osd->o_osd); 2441 *skip = 1; 2442 return NULL; 2443 } 2444 } 2445 2446 /* 2447 * Wrappers to refcount containing ceph_osd struct 2448 */ 2449 static struct ceph_connection *get_osd_con(struct ceph_connection *con) 2450 { 2451 struct ceph_osd *osd = con->private; 2452 if (get_osd(osd)) 2453 return con; 2454 return NULL; 2455 } 2456 2457 static void put_osd_con(struct ceph_connection *con) 2458 { 2459 struct ceph_osd *osd = con->private; 2460 put_osd(osd); 2461 } 2462 2463 /* 2464 * authentication 2465 */ 2466 /* 2467 * Note: returned pointer is the address of a structure that's 2468 * managed separately. Caller must *not* attempt to free it. 2469 */ 2470 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, 2471 int *proto, int force_new) 2472 { 2473 struct ceph_osd *o = con->private; 2474 struct ceph_osd_client *osdc = o->o_osdc; 2475 struct ceph_auth_client *ac = osdc->client->monc.auth; 2476 struct ceph_auth_handshake *auth = &o->o_auth; 2477 2478 if (force_new && auth->authorizer) { 2479 ceph_auth_destroy_authorizer(ac, auth->authorizer); 2480 auth->authorizer = NULL; 2481 } 2482 if (!auth->authorizer) { 2483 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD, 2484 auth); 2485 if (ret) 2486 return ERR_PTR(ret); 2487 } else { 2488 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD, 2489 auth); 2490 if (ret) 2491 return ERR_PTR(ret); 2492 } 2493 *proto = ac->protocol; 2494 2495 return auth; 2496 } 2497 2498 2499 static int verify_authorizer_reply(struct ceph_connection *con, int len) 2500 { 2501 struct ceph_osd *o = con->private; 2502 struct ceph_osd_client *osdc = o->o_osdc; 2503 struct ceph_auth_client *ac = osdc->client->monc.auth; 2504 2505 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len); 2506 } 2507 2508 static int invalidate_authorizer(struct ceph_connection *con) 2509 { 2510 struct ceph_osd *o = con->private; 2511 struct ceph_osd_client *osdc = o->o_osdc; 2512 struct ceph_auth_client *ac = osdc->client->monc.auth; 2513 2514 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); 2515 return ceph_monc_validate_auth(&osdc->client->monc); 2516 } 2517 2518 static const struct ceph_connection_operations osd_con_ops = { 2519 .get = get_osd_con, 2520 .put = put_osd_con, 2521 .dispatch = dispatch, 2522 .get_authorizer = get_authorizer, 2523 .verify_authorizer_reply = verify_authorizer_reply, 2524 .invalidate_authorizer = invalidate_authorizer, 2525 .alloc_msg = alloc_msg, 2526 .fault = osd_reset, 2527 }; 2528