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