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