1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/ceph/ceph_debug.h> 4 5 #include <linux/module.h> 6 #include <linux/err.h> 7 #include <linux/highmem.h> 8 #include <linux/mm.h> 9 #include <linux/pagemap.h> 10 #include <linux/slab.h> 11 #include <linux/uaccess.h> 12 #ifdef CONFIG_BLOCK 13 #include <linux/bio.h> 14 #endif 15 16 #include <linux/ceph/ceph_features.h> 17 #include <linux/ceph/libceph.h> 18 #include <linux/ceph/osd_client.h> 19 #include <linux/ceph/messenger.h> 20 #include <linux/ceph/decode.h> 21 #include <linux/ceph/auth.h> 22 #include <linux/ceph/pagelist.h> 23 #include <linux/ceph/striper.h> 24 25 #define OSD_OPREPLY_FRONT_LEN 512 26 27 static struct kmem_cache *ceph_osd_request_cache; 28 29 static const struct ceph_connection_operations osd_con_ops; 30 31 /* 32 * Implement client access to distributed object storage cluster. 33 * 34 * All data objects are stored within a cluster/cloud of OSDs, or 35 * "object storage devices." (Note that Ceph OSDs have _nothing_ to 36 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply 37 * remote daemons serving up and coordinating consistent and safe 38 * access to storage. 39 * 40 * Cluster membership and the mapping of data objects onto storage devices 41 * are described by the osd map. 42 * 43 * We keep track of pending OSD requests (read, write), resubmit 44 * requests to different OSDs when the cluster topology/data layout 45 * change, or retry the affected requests when the communications 46 * channel with an OSD is reset. 47 */ 48 49 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req); 50 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req); 51 static void link_linger(struct ceph_osd *osd, 52 struct ceph_osd_linger_request *lreq); 53 static void unlink_linger(struct ceph_osd *osd, 54 struct ceph_osd_linger_request *lreq); 55 static void clear_backoffs(struct ceph_osd *osd); 56 57 #if 1 58 static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem) 59 { 60 bool wrlocked = true; 61 62 if (unlikely(down_read_trylock(sem))) { 63 wrlocked = false; 64 up_read(sem); 65 } 66 67 return wrlocked; 68 } 69 static inline void verify_osdc_locked(struct ceph_osd_client *osdc) 70 { 71 WARN_ON(!rwsem_is_locked(&osdc->lock)); 72 } 73 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) 74 { 75 WARN_ON(!rwsem_is_wrlocked(&osdc->lock)); 76 } 77 static inline void verify_osd_locked(struct ceph_osd *osd) 78 { 79 struct ceph_osd_client *osdc = osd->o_osdc; 80 81 WARN_ON(!(mutex_is_locked(&osd->lock) && 82 rwsem_is_locked(&osdc->lock)) && 83 !rwsem_is_wrlocked(&osdc->lock)); 84 } 85 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) 86 { 87 WARN_ON(!mutex_is_locked(&lreq->lock)); 88 } 89 #else 90 static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { } 91 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { } 92 static inline void verify_osd_locked(struct ceph_osd *osd) { } 93 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { } 94 #endif 95 96 /* 97 * calculate the mapping of a file extent onto an object, and fill out the 98 * request accordingly. shorten extent as necessary if it crosses an 99 * object boundary. 100 * 101 * fill osd op in request message. 102 */ 103 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen, 104 u64 *objnum, u64 *objoff, u64 *objlen) 105 { 106 u64 orig_len = *plen; 107 u32 xlen; 108 109 /* object extent? */ 110 ceph_calc_file_object_mapping(layout, off, orig_len, objnum, 111 objoff, &xlen); 112 *objlen = xlen; 113 if (*objlen < orig_len) { 114 *plen = *objlen; 115 dout(" skipping last %llu, final file extent %llu~%llu\n", 116 orig_len - *plen, off, *plen); 117 } 118 119 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen); 120 return 0; 121 } 122 123 static void ceph_osd_data_init(struct ceph_osd_data *osd_data) 124 { 125 memset(osd_data, 0, sizeof (*osd_data)); 126 osd_data->type = CEPH_OSD_DATA_TYPE_NONE; 127 } 128 129 /* 130 * Consumes @pages if @own_pages is true. 131 */ 132 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data, 133 struct page **pages, u64 length, u32 alignment, 134 bool pages_from_pool, bool own_pages) 135 { 136 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES; 137 osd_data->pages = pages; 138 osd_data->length = length; 139 osd_data->alignment = alignment; 140 osd_data->pages_from_pool = pages_from_pool; 141 osd_data->own_pages = own_pages; 142 } 143 144 /* 145 * Consumes a ref on @pagelist. 146 */ 147 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data, 148 struct ceph_pagelist *pagelist) 149 { 150 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST; 151 osd_data->pagelist = pagelist; 152 } 153 154 #ifdef CONFIG_BLOCK 155 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data, 156 struct ceph_bio_iter *bio_pos, 157 u32 bio_length) 158 { 159 osd_data->type = CEPH_OSD_DATA_TYPE_BIO; 160 osd_data->bio_pos = *bio_pos; 161 osd_data->bio_length = bio_length; 162 } 163 #endif /* CONFIG_BLOCK */ 164 165 static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data, 166 struct ceph_bvec_iter *bvec_pos, 167 u32 num_bvecs) 168 { 169 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS; 170 osd_data->bvec_pos = *bvec_pos; 171 osd_data->num_bvecs = num_bvecs; 172 } 173 174 static void ceph_osd_iter_init(struct ceph_osd_data *osd_data, 175 struct iov_iter *iter) 176 { 177 osd_data->type = CEPH_OSD_DATA_TYPE_ITER; 178 osd_data->iter = *iter; 179 } 180 181 static struct ceph_osd_data * 182 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which) 183 { 184 BUG_ON(which >= osd_req->r_num_ops); 185 186 return &osd_req->r_ops[which].raw_data_in; 187 } 188 189 struct ceph_osd_data * 190 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req, 191 unsigned int which) 192 { 193 return osd_req_op_data(osd_req, which, extent, osd_data); 194 } 195 EXPORT_SYMBOL(osd_req_op_extent_osd_data); 196 197 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req, 198 unsigned int which, struct page **pages, 199 u64 length, u32 alignment, 200 bool pages_from_pool, bool own_pages) 201 { 202 struct ceph_osd_data *osd_data; 203 204 osd_data = osd_req_op_raw_data_in(osd_req, which); 205 ceph_osd_data_pages_init(osd_data, pages, length, alignment, 206 pages_from_pool, own_pages); 207 } 208 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages); 209 210 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req, 211 unsigned int which, struct page **pages, 212 u64 length, u32 alignment, 213 bool pages_from_pool, bool own_pages) 214 { 215 struct ceph_osd_data *osd_data; 216 217 osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 218 ceph_osd_data_pages_init(osd_data, pages, length, alignment, 219 pages_from_pool, own_pages); 220 } 221 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages); 222 223 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req, 224 unsigned int which, struct ceph_pagelist *pagelist) 225 { 226 struct ceph_osd_data *osd_data; 227 228 osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 229 ceph_osd_data_pagelist_init(osd_data, pagelist); 230 } 231 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist); 232 233 #ifdef CONFIG_BLOCK 234 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req, 235 unsigned int which, 236 struct ceph_bio_iter *bio_pos, 237 u32 bio_length) 238 { 239 struct ceph_osd_data *osd_data; 240 241 osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 242 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length); 243 } 244 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio); 245 #endif /* CONFIG_BLOCK */ 246 247 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req, 248 unsigned int which, 249 struct bio_vec *bvecs, u32 num_bvecs, 250 u32 bytes) 251 { 252 struct ceph_osd_data *osd_data; 253 struct ceph_bvec_iter it = { 254 .bvecs = bvecs, 255 .iter = { .bi_size = bytes }, 256 }; 257 258 osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 259 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs); 260 } 261 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs); 262 263 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req, 264 unsigned int which, 265 struct ceph_bvec_iter *bvec_pos) 266 { 267 struct ceph_osd_data *osd_data; 268 269 osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 270 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0); 271 } 272 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos); 273 274 /** 275 * osd_req_op_extent_osd_iter - Set up an operation with an iterator buffer 276 * @osd_req: The request to set up 277 * @which: Index of the operation in which to set the iter 278 * @iter: The buffer iterator 279 */ 280 void osd_req_op_extent_osd_iter(struct ceph_osd_request *osd_req, 281 unsigned int which, struct iov_iter *iter) 282 { 283 struct ceph_osd_data *osd_data; 284 285 osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 286 ceph_osd_iter_init(osd_data, iter); 287 } 288 EXPORT_SYMBOL(osd_req_op_extent_osd_iter); 289 290 static void osd_req_op_cls_request_info_pagelist( 291 struct ceph_osd_request *osd_req, 292 unsigned int which, struct ceph_pagelist *pagelist) 293 { 294 struct ceph_osd_data *osd_data; 295 296 osd_data = osd_req_op_data(osd_req, which, cls, request_info); 297 ceph_osd_data_pagelist_init(osd_data, pagelist); 298 } 299 300 void osd_req_op_cls_request_data_pagelist( 301 struct ceph_osd_request *osd_req, 302 unsigned int which, struct ceph_pagelist *pagelist) 303 { 304 struct ceph_osd_data *osd_data; 305 306 osd_data = osd_req_op_data(osd_req, which, cls, request_data); 307 ceph_osd_data_pagelist_init(osd_data, pagelist); 308 osd_req->r_ops[which].cls.indata_len += pagelist->length; 309 osd_req->r_ops[which].indata_len += pagelist->length; 310 } 311 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist); 312 313 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req, 314 unsigned int which, struct page **pages, u64 length, 315 u32 alignment, bool pages_from_pool, bool own_pages) 316 { 317 struct ceph_osd_data *osd_data; 318 319 osd_data = osd_req_op_data(osd_req, which, cls, request_data); 320 ceph_osd_data_pages_init(osd_data, pages, length, alignment, 321 pages_from_pool, own_pages); 322 osd_req->r_ops[which].cls.indata_len += length; 323 osd_req->r_ops[which].indata_len += length; 324 } 325 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages); 326 327 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req, 328 unsigned int which, 329 struct bio_vec *bvecs, u32 num_bvecs, 330 u32 bytes) 331 { 332 struct ceph_osd_data *osd_data; 333 struct ceph_bvec_iter it = { 334 .bvecs = bvecs, 335 .iter = { .bi_size = bytes }, 336 }; 337 338 osd_data = osd_req_op_data(osd_req, which, cls, request_data); 339 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs); 340 osd_req->r_ops[which].cls.indata_len += bytes; 341 osd_req->r_ops[which].indata_len += bytes; 342 } 343 EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs); 344 345 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req, 346 unsigned int which, struct page **pages, u64 length, 347 u32 alignment, bool pages_from_pool, bool own_pages) 348 { 349 struct ceph_osd_data *osd_data; 350 351 osd_data = osd_req_op_data(osd_req, which, cls, response_data); 352 ceph_osd_data_pages_init(osd_data, pages, length, alignment, 353 pages_from_pool, own_pages); 354 } 355 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages); 356 357 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data) 358 { 359 switch (osd_data->type) { 360 case CEPH_OSD_DATA_TYPE_NONE: 361 return 0; 362 case CEPH_OSD_DATA_TYPE_PAGES: 363 return osd_data->length; 364 case CEPH_OSD_DATA_TYPE_PAGELIST: 365 return (u64)osd_data->pagelist->length; 366 #ifdef CONFIG_BLOCK 367 case CEPH_OSD_DATA_TYPE_BIO: 368 return (u64)osd_data->bio_length; 369 #endif /* CONFIG_BLOCK */ 370 case CEPH_OSD_DATA_TYPE_BVECS: 371 return osd_data->bvec_pos.iter.bi_size; 372 case CEPH_OSD_DATA_TYPE_ITER: 373 return iov_iter_count(&osd_data->iter); 374 default: 375 WARN(true, "unrecognized data type %d\n", (int)osd_data->type); 376 return 0; 377 } 378 } 379 380 static void ceph_osd_data_release(struct ceph_osd_data *osd_data) 381 { 382 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) { 383 int num_pages; 384 385 num_pages = calc_pages_for((u64)osd_data->alignment, 386 (u64)osd_data->length); 387 ceph_release_page_vector(osd_data->pages, num_pages); 388 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) { 389 ceph_pagelist_release(osd_data->pagelist); 390 } 391 ceph_osd_data_init(osd_data); 392 } 393 394 static void osd_req_op_data_release(struct ceph_osd_request *osd_req, 395 unsigned int which) 396 { 397 struct ceph_osd_req_op *op; 398 399 BUG_ON(which >= osd_req->r_num_ops); 400 op = &osd_req->r_ops[which]; 401 402 switch (op->op) { 403 case CEPH_OSD_OP_READ: 404 case CEPH_OSD_OP_SPARSE_READ: 405 case CEPH_OSD_OP_WRITE: 406 case CEPH_OSD_OP_WRITEFULL: 407 kfree(op->extent.sparse_ext); 408 ceph_osd_data_release(&op->extent.osd_data); 409 break; 410 case CEPH_OSD_OP_CALL: 411 ceph_osd_data_release(&op->cls.request_info); 412 ceph_osd_data_release(&op->cls.request_data); 413 ceph_osd_data_release(&op->cls.response_data); 414 break; 415 case CEPH_OSD_OP_SETXATTR: 416 case CEPH_OSD_OP_CMPXATTR: 417 ceph_osd_data_release(&op->xattr.osd_data); 418 break; 419 case CEPH_OSD_OP_STAT: 420 ceph_osd_data_release(&op->raw_data_in); 421 break; 422 case CEPH_OSD_OP_NOTIFY_ACK: 423 ceph_osd_data_release(&op->notify_ack.request_data); 424 break; 425 case CEPH_OSD_OP_NOTIFY: 426 ceph_osd_data_release(&op->notify.request_data); 427 ceph_osd_data_release(&op->notify.response_data); 428 break; 429 case CEPH_OSD_OP_LIST_WATCHERS: 430 ceph_osd_data_release(&op->list_watchers.response_data); 431 break; 432 case CEPH_OSD_OP_COPY_FROM2: 433 ceph_osd_data_release(&op->copy_from.osd_data); 434 break; 435 default: 436 break; 437 } 438 } 439 440 /* 441 * Assumes @t is zero-initialized. 442 */ 443 static void target_init(struct ceph_osd_request_target *t) 444 { 445 ceph_oid_init(&t->base_oid); 446 ceph_oloc_init(&t->base_oloc); 447 ceph_oid_init(&t->target_oid); 448 ceph_oloc_init(&t->target_oloc); 449 450 ceph_osds_init(&t->acting); 451 ceph_osds_init(&t->up); 452 t->size = -1; 453 t->min_size = -1; 454 455 t->osd = CEPH_HOMELESS_OSD; 456 } 457 458 static void target_copy(struct ceph_osd_request_target *dest, 459 const struct ceph_osd_request_target *src) 460 { 461 ceph_oid_copy(&dest->base_oid, &src->base_oid); 462 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc); 463 ceph_oid_copy(&dest->target_oid, &src->target_oid); 464 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc); 465 466 dest->pgid = src->pgid; /* struct */ 467 dest->spgid = src->spgid; /* struct */ 468 dest->pg_num = src->pg_num; 469 dest->pg_num_mask = src->pg_num_mask; 470 ceph_osds_copy(&dest->acting, &src->acting); 471 ceph_osds_copy(&dest->up, &src->up); 472 dest->size = src->size; 473 dest->min_size = src->min_size; 474 dest->sort_bitwise = src->sort_bitwise; 475 dest->recovery_deletes = src->recovery_deletes; 476 477 dest->flags = src->flags; 478 dest->used_replica = src->used_replica; 479 dest->paused = src->paused; 480 481 dest->epoch = src->epoch; 482 dest->last_force_resend = src->last_force_resend; 483 484 dest->osd = src->osd; 485 } 486 487 static void target_destroy(struct ceph_osd_request_target *t) 488 { 489 ceph_oid_destroy(&t->base_oid); 490 ceph_oloc_destroy(&t->base_oloc); 491 ceph_oid_destroy(&t->target_oid); 492 ceph_oloc_destroy(&t->target_oloc); 493 } 494 495 /* 496 * requests 497 */ 498 static void request_release_checks(struct ceph_osd_request *req) 499 { 500 WARN_ON(!RB_EMPTY_NODE(&req->r_node)); 501 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node)); 502 WARN_ON(!list_empty(&req->r_private_item)); 503 WARN_ON(req->r_osd); 504 } 505 506 static void ceph_osdc_release_request(struct kref *kref) 507 { 508 struct ceph_osd_request *req = container_of(kref, 509 struct ceph_osd_request, r_kref); 510 unsigned int which; 511 512 dout("%s %p (r_request %p r_reply %p)\n", __func__, req, 513 req->r_request, req->r_reply); 514 request_release_checks(req); 515 516 if (req->r_request) 517 ceph_msg_put(req->r_request); 518 if (req->r_reply) 519 ceph_msg_put(req->r_reply); 520 521 for (which = 0; which < req->r_num_ops; which++) 522 osd_req_op_data_release(req, which); 523 524 target_destroy(&req->r_t); 525 ceph_put_snap_context(req->r_snapc); 526 527 if (req->r_mempool) 528 mempool_free(req, req->r_osdc->req_mempool); 529 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS) 530 kmem_cache_free(ceph_osd_request_cache, req); 531 else 532 kfree(req); 533 } 534 535 void ceph_osdc_get_request(struct ceph_osd_request *req) 536 { 537 dout("%s %p (was %d)\n", __func__, req, 538 kref_read(&req->r_kref)); 539 kref_get(&req->r_kref); 540 } 541 EXPORT_SYMBOL(ceph_osdc_get_request); 542 543 void ceph_osdc_put_request(struct ceph_osd_request *req) 544 { 545 if (req) { 546 dout("%s %p (was %d)\n", __func__, req, 547 kref_read(&req->r_kref)); 548 kref_put(&req->r_kref, ceph_osdc_release_request); 549 } 550 } 551 EXPORT_SYMBOL(ceph_osdc_put_request); 552 553 static void request_init(struct ceph_osd_request *req) 554 { 555 /* req only, each op is zeroed in osd_req_op_init() */ 556 memset(req, 0, sizeof(*req)); 557 558 kref_init(&req->r_kref); 559 init_completion(&req->r_completion); 560 RB_CLEAR_NODE(&req->r_node); 561 RB_CLEAR_NODE(&req->r_mc_node); 562 INIT_LIST_HEAD(&req->r_private_item); 563 564 target_init(&req->r_t); 565 } 566 567 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc, 568 struct ceph_snap_context *snapc, 569 unsigned int num_ops, 570 bool use_mempool, 571 gfp_t gfp_flags) 572 { 573 struct ceph_osd_request *req; 574 575 if (use_mempool) { 576 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS); 577 req = mempool_alloc(osdc->req_mempool, gfp_flags); 578 } else if (num_ops <= CEPH_OSD_SLAB_OPS) { 579 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags); 580 } else { 581 BUG_ON(num_ops > CEPH_OSD_MAX_OPS); 582 req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags); 583 } 584 if (unlikely(!req)) 585 return NULL; 586 587 request_init(req); 588 req->r_osdc = osdc; 589 req->r_mempool = use_mempool; 590 req->r_num_ops = num_ops; 591 req->r_snapid = CEPH_NOSNAP; 592 req->r_snapc = ceph_get_snap_context(snapc); 593 594 dout("%s req %p\n", __func__, req); 595 return req; 596 } 597 EXPORT_SYMBOL(ceph_osdc_alloc_request); 598 599 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc) 600 { 601 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0); 602 } 603 604 static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp, 605 int num_request_data_items, 606 int num_reply_data_items) 607 { 608 struct ceph_osd_client *osdc = req->r_osdc; 609 struct ceph_msg *msg; 610 int msg_size; 611 612 WARN_ON(req->r_request || req->r_reply); 613 WARN_ON(ceph_oid_empty(&req->r_base_oid)); 614 WARN_ON(ceph_oloc_empty(&req->r_base_oloc)); 615 616 /* create request message */ 617 msg_size = CEPH_ENCODING_START_BLK_LEN + 618 CEPH_PGID_ENCODING_LEN + 1; /* spgid */ 619 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */ 620 msg_size += CEPH_ENCODING_START_BLK_LEN + 621 sizeof(struct ceph_osd_reqid); /* reqid */ 622 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */ 623 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */ 624 msg_size += CEPH_ENCODING_START_BLK_LEN + 625 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */ 626 msg_size += 4 + req->r_base_oid.name_len; /* oid */ 627 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op); 628 msg_size += 8; /* snapid */ 629 msg_size += 8; /* snap_seq */ 630 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0); 631 msg_size += 4 + 8; /* retry_attempt, features */ 632 633 if (req->r_mempool) 634 msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size, 635 num_request_data_items); 636 else 637 msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size, 638 num_request_data_items, gfp, true); 639 if (!msg) 640 return -ENOMEM; 641 642 memset(msg->front.iov_base, 0, msg->front.iov_len); 643 req->r_request = msg; 644 645 /* create reply message */ 646 msg_size = OSD_OPREPLY_FRONT_LEN; 647 msg_size += req->r_base_oid.name_len; 648 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op); 649 650 if (req->r_mempool) 651 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size, 652 num_reply_data_items); 653 else 654 msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size, 655 num_reply_data_items, gfp, true); 656 if (!msg) 657 return -ENOMEM; 658 659 req->r_reply = msg; 660 661 return 0; 662 } 663 664 static bool osd_req_opcode_valid(u16 opcode) 665 { 666 switch (opcode) { 667 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true; 668 __CEPH_FORALL_OSD_OPS(GENERATE_CASE) 669 #undef GENERATE_CASE 670 default: 671 return false; 672 } 673 } 674 675 static void get_num_data_items(struct ceph_osd_request *req, 676 int *num_request_data_items, 677 int *num_reply_data_items) 678 { 679 struct ceph_osd_req_op *op; 680 681 *num_request_data_items = 0; 682 *num_reply_data_items = 0; 683 684 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) { 685 switch (op->op) { 686 /* request */ 687 case CEPH_OSD_OP_WRITE: 688 case CEPH_OSD_OP_WRITEFULL: 689 case CEPH_OSD_OP_SETXATTR: 690 case CEPH_OSD_OP_CMPXATTR: 691 case CEPH_OSD_OP_NOTIFY_ACK: 692 case CEPH_OSD_OP_COPY_FROM2: 693 *num_request_data_items += 1; 694 break; 695 696 /* reply */ 697 case CEPH_OSD_OP_STAT: 698 case CEPH_OSD_OP_READ: 699 case CEPH_OSD_OP_SPARSE_READ: 700 case CEPH_OSD_OP_LIST_WATCHERS: 701 *num_reply_data_items += 1; 702 break; 703 704 /* both */ 705 case CEPH_OSD_OP_NOTIFY: 706 *num_request_data_items += 1; 707 *num_reply_data_items += 1; 708 break; 709 case CEPH_OSD_OP_CALL: 710 *num_request_data_items += 2; 711 *num_reply_data_items += 1; 712 break; 713 714 default: 715 WARN_ON(!osd_req_opcode_valid(op->op)); 716 break; 717 } 718 } 719 } 720 721 /* 722 * oid, oloc and OSD op opcode(s) must be filled in before this function 723 * is called. 724 */ 725 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp) 726 { 727 int num_request_data_items, num_reply_data_items; 728 729 get_num_data_items(req, &num_request_data_items, &num_reply_data_items); 730 return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items, 731 num_reply_data_items); 732 } 733 EXPORT_SYMBOL(ceph_osdc_alloc_messages); 734 735 /* 736 * This is an osd op init function for opcodes that have no data or 737 * other information associated with them. It also serves as a 738 * common init routine for all the other init functions, below. 739 */ 740 struct ceph_osd_req_op * 741 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which, 742 u16 opcode, u32 flags) 743 { 744 struct ceph_osd_req_op *op; 745 746 BUG_ON(which >= osd_req->r_num_ops); 747 BUG_ON(!osd_req_opcode_valid(opcode)); 748 749 op = &osd_req->r_ops[which]; 750 memset(op, 0, sizeof (*op)); 751 op->op = opcode; 752 op->flags = flags; 753 754 return op; 755 } 756 EXPORT_SYMBOL(osd_req_op_init); 757 758 void osd_req_op_extent_init(struct ceph_osd_request *osd_req, 759 unsigned int which, u16 opcode, 760 u64 offset, u64 length, 761 u64 truncate_size, u32 truncate_seq) 762 { 763 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, 764 opcode, 0); 765 size_t payload_len = 0; 766 767 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && 768 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO && 769 opcode != CEPH_OSD_OP_TRUNCATE && opcode != CEPH_OSD_OP_SPARSE_READ); 770 771 op->extent.offset = offset; 772 op->extent.length = length; 773 op->extent.truncate_size = truncate_size; 774 op->extent.truncate_seq = truncate_seq; 775 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL) 776 payload_len += length; 777 778 op->indata_len = payload_len; 779 } 780 EXPORT_SYMBOL(osd_req_op_extent_init); 781 782 void osd_req_op_extent_update(struct ceph_osd_request *osd_req, 783 unsigned int which, u64 length) 784 { 785 struct ceph_osd_req_op *op; 786 u64 previous; 787 788 BUG_ON(which >= osd_req->r_num_ops); 789 op = &osd_req->r_ops[which]; 790 previous = op->extent.length; 791 792 if (length == previous) 793 return; /* Nothing to do */ 794 BUG_ON(length > previous); 795 796 op->extent.length = length; 797 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL) 798 op->indata_len -= previous - length; 799 } 800 EXPORT_SYMBOL(osd_req_op_extent_update); 801 802 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req, 803 unsigned int which, u64 offset_inc) 804 { 805 struct ceph_osd_req_op *op, *prev_op; 806 807 BUG_ON(which + 1 >= osd_req->r_num_ops); 808 809 prev_op = &osd_req->r_ops[which]; 810 op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags); 811 /* dup previous one */ 812 op->indata_len = prev_op->indata_len; 813 op->outdata_len = prev_op->outdata_len; 814 op->extent = prev_op->extent; 815 /* adjust offset */ 816 op->extent.offset += offset_inc; 817 op->extent.length -= offset_inc; 818 819 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL) 820 op->indata_len -= offset_inc; 821 } 822 EXPORT_SYMBOL(osd_req_op_extent_dup_last); 823 824 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which, 825 const char *class, const char *method) 826 { 827 struct ceph_osd_req_op *op; 828 struct ceph_pagelist *pagelist; 829 size_t payload_len = 0; 830 size_t size; 831 int ret; 832 833 op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0); 834 835 pagelist = ceph_pagelist_alloc(GFP_NOFS); 836 if (!pagelist) 837 return -ENOMEM; 838 839 op->cls.class_name = class; 840 size = strlen(class); 841 BUG_ON(size > (size_t) U8_MAX); 842 op->cls.class_len = size; 843 ret = ceph_pagelist_append(pagelist, class, size); 844 if (ret) 845 goto err_pagelist_free; 846 payload_len += size; 847 848 op->cls.method_name = method; 849 size = strlen(method); 850 BUG_ON(size > (size_t) U8_MAX); 851 op->cls.method_len = size; 852 ret = ceph_pagelist_append(pagelist, method, size); 853 if (ret) 854 goto err_pagelist_free; 855 payload_len += size; 856 857 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist); 858 op->indata_len = payload_len; 859 return 0; 860 861 err_pagelist_free: 862 ceph_pagelist_release(pagelist); 863 return ret; 864 } 865 EXPORT_SYMBOL(osd_req_op_cls_init); 866 867 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which, 868 u16 opcode, const char *name, const void *value, 869 size_t size, u8 cmp_op, u8 cmp_mode) 870 { 871 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, 872 opcode, 0); 873 struct ceph_pagelist *pagelist; 874 size_t payload_len; 875 int ret; 876 877 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR); 878 879 pagelist = ceph_pagelist_alloc(GFP_NOFS); 880 if (!pagelist) 881 return -ENOMEM; 882 883 payload_len = strlen(name); 884 op->xattr.name_len = payload_len; 885 ret = ceph_pagelist_append(pagelist, name, payload_len); 886 if (ret) 887 goto err_pagelist_free; 888 889 op->xattr.value_len = size; 890 ret = ceph_pagelist_append(pagelist, value, size); 891 if (ret) 892 goto err_pagelist_free; 893 payload_len += size; 894 895 op->xattr.cmp_op = cmp_op; 896 op->xattr.cmp_mode = cmp_mode; 897 898 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist); 899 op->indata_len = payload_len; 900 return 0; 901 902 err_pagelist_free: 903 ceph_pagelist_release(pagelist); 904 return ret; 905 } 906 EXPORT_SYMBOL(osd_req_op_xattr_init); 907 908 /* 909 * @watch_opcode: CEPH_OSD_WATCH_OP_* 910 */ 911 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which, 912 u8 watch_opcode, u64 cookie, u32 gen) 913 { 914 struct ceph_osd_req_op *op; 915 916 op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0); 917 op->watch.cookie = cookie; 918 op->watch.op = watch_opcode; 919 op->watch.gen = gen; 920 } 921 922 /* 923 * prot_ver, timeout and notify payload (may be empty) should already be 924 * encoded in @request_pl 925 */ 926 static void osd_req_op_notify_init(struct ceph_osd_request *req, int which, 927 u64 cookie, struct ceph_pagelist *request_pl) 928 { 929 struct ceph_osd_req_op *op; 930 931 op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0); 932 op->notify.cookie = cookie; 933 934 ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl); 935 op->indata_len = request_pl->length; 936 } 937 938 /* 939 * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_* 940 */ 941 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req, 942 unsigned int which, 943 u64 expected_object_size, 944 u64 expected_write_size, 945 u32 flags) 946 { 947 struct ceph_osd_req_op *op; 948 949 op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0); 950 op->alloc_hint.expected_object_size = expected_object_size; 951 op->alloc_hint.expected_write_size = expected_write_size; 952 op->alloc_hint.flags = flags; 953 954 /* 955 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed 956 * not worth a feature bit. Set FAILOK per-op flag to make 957 * sure older osds don't trip over an unsupported opcode. 958 */ 959 op->flags |= CEPH_OSD_OP_FLAG_FAILOK; 960 } 961 EXPORT_SYMBOL(osd_req_op_alloc_hint_init); 962 963 static void ceph_osdc_msg_data_add(struct ceph_msg *msg, 964 struct ceph_osd_data *osd_data) 965 { 966 u64 length = ceph_osd_data_length(osd_data); 967 968 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) { 969 BUG_ON(length > (u64) SIZE_MAX); 970 if (length) 971 ceph_msg_data_add_pages(msg, osd_data->pages, 972 length, osd_data->alignment, false); 973 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) { 974 BUG_ON(!length); 975 ceph_msg_data_add_pagelist(msg, osd_data->pagelist); 976 #ifdef CONFIG_BLOCK 977 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) { 978 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length); 979 #endif 980 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) { 981 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos); 982 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_ITER) { 983 ceph_msg_data_add_iter(msg, &osd_data->iter); 984 } else { 985 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE); 986 } 987 } 988 989 static u32 osd_req_encode_op(struct ceph_osd_op *dst, 990 const struct ceph_osd_req_op *src) 991 { 992 switch (src->op) { 993 case CEPH_OSD_OP_STAT: 994 break; 995 case CEPH_OSD_OP_READ: 996 case CEPH_OSD_OP_SPARSE_READ: 997 case CEPH_OSD_OP_WRITE: 998 case CEPH_OSD_OP_WRITEFULL: 999 case CEPH_OSD_OP_ZERO: 1000 case CEPH_OSD_OP_TRUNCATE: 1001 dst->extent.offset = cpu_to_le64(src->extent.offset); 1002 dst->extent.length = cpu_to_le64(src->extent.length); 1003 dst->extent.truncate_size = 1004 cpu_to_le64(src->extent.truncate_size); 1005 dst->extent.truncate_seq = 1006 cpu_to_le32(src->extent.truncate_seq); 1007 break; 1008 case CEPH_OSD_OP_CALL: 1009 dst->cls.class_len = src->cls.class_len; 1010 dst->cls.method_len = src->cls.method_len; 1011 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len); 1012 break; 1013 case CEPH_OSD_OP_WATCH: 1014 dst->watch.cookie = cpu_to_le64(src->watch.cookie); 1015 dst->watch.ver = cpu_to_le64(0); 1016 dst->watch.op = src->watch.op; 1017 dst->watch.gen = cpu_to_le32(src->watch.gen); 1018 break; 1019 case CEPH_OSD_OP_NOTIFY_ACK: 1020 break; 1021 case CEPH_OSD_OP_NOTIFY: 1022 dst->notify.cookie = cpu_to_le64(src->notify.cookie); 1023 break; 1024 case CEPH_OSD_OP_LIST_WATCHERS: 1025 break; 1026 case CEPH_OSD_OP_SETALLOCHINT: 1027 dst->alloc_hint.expected_object_size = 1028 cpu_to_le64(src->alloc_hint.expected_object_size); 1029 dst->alloc_hint.expected_write_size = 1030 cpu_to_le64(src->alloc_hint.expected_write_size); 1031 dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags); 1032 break; 1033 case CEPH_OSD_OP_SETXATTR: 1034 case CEPH_OSD_OP_CMPXATTR: 1035 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len); 1036 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len); 1037 dst->xattr.cmp_op = src->xattr.cmp_op; 1038 dst->xattr.cmp_mode = src->xattr.cmp_mode; 1039 break; 1040 case CEPH_OSD_OP_CREATE: 1041 case CEPH_OSD_OP_DELETE: 1042 break; 1043 case CEPH_OSD_OP_COPY_FROM2: 1044 dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid); 1045 dst->copy_from.src_version = 1046 cpu_to_le64(src->copy_from.src_version); 1047 dst->copy_from.flags = src->copy_from.flags; 1048 dst->copy_from.src_fadvise_flags = 1049 cpu_to_le32(src->copy_from.src_fadvise_flags); 1050 break; 1051 case CEPH_OSD_OP_ASSERT_VER: 1052 dst->assert_ver.unused = cpu_to_le64(0); 1053 dst->assert_ver.ver = cpu_to_le64(src->assert_ver.ver); 1054 break; 1055 default: 1056 pr_err("unsupported osd opcode %s\n", 1057 ceph_osd_op_name(src->op)); 1058 WARN_ON(1); 1059 1060 return 0; 1061 } 1062 1063 dst->op = cpu_to_le16(src->op); 1064 dst->flags = cpu_to_le32(src->flags); 1065 dst->payload_len = cpu_to_le32(src->indata_len); 1066 1067 return src->indata_len; 1068 } 1069 1070 /* 1071 * build new request AND message, calculate layout, and adjust file 1072 * extent as needed. 1073 * 1074 * if the file was recently truncated, we include information about its 1075 * old and new size so that the object can be updated appropriately. (we 1076 * avoid synchronously deleting truncated objects because it's slow.) 1077 */ 1078 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, 1079 struct ceph_file_layout *layout, 1080 struct ceph_vino vino, 1081 u64 off, u64 *plen, 1082 unsigned int which, int num_ops, 1083 int opcode, int flags, 1084 struct ceph_snap_context *snapc, 1085 u32 truncate_seq, 1086 u64 truncate_size, 1087 bool use_mempool) 1088 { 1089 struct ceph_osd_request *req; 1090 u64 objnum = 0; 1091 u64 objoff = 0; 1092 u64 objlen = 0; 1093 int r; 1094 1095 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && 1096 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE && 1097 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE && 1098 opcode != CEPH_OSD_OP_SPARSE_READ); 1099 1100 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool, 1101 GFP_NOFS); 1102 if (!req) { 1103 r = -ENOMEM; 1104 goto fail; 1105 } 1106 1107 /* calculate max write size */ 1108 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen); 1109 if (r) 1110 goto fail; 1111 1112 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) { 1113 osd_req_op_init(req, which, opcode, 0); 1114 } else { 1115 u32 object_size = layout->object_size; 1116 u32 object_base = off - objoff; 1117 if (!(truncate_seq == 1 && truncate_size == -1ULL)) { 1118 if (truncate_size <= object_base) { 1119 truncate_size = 0; 1120 } else { 1121 truncate_size -= object_base; 1122 if (truncate_size > object_size) 1123 truncate_size = object_size; 1124 } 1125 } 1126 osd_req_op_extent_init(req, which, opcode, objoff, objlen, 1127 truncate_size, truncate_seq); 1128 } 1129 1130 req->r_base_oloc.pool = layout->pool_id; 1131 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns); 1132 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum); 1133 req->r_flags = flags | osdc->client->options->read_from_replica; 1134 1135 req->r_snapid = vino.snap; 1136 if (flags & CEPH_OSD_FLAG_WRITE) 1137 req->r_data_offset = off; 1138 1139 if (num_ops > 1) 1140 /* 1141 * This is a special case for ceph_writepages_start(), but it 1142 * also covers ceph_uninline_data(). If more multi-op request 1143 * use cases emerge, we will need a separate helper. 1144 */ 1145 r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0); 1146 else 1147 r = ceph_osdc_alloc_messages(req, GFP_NOFS); 1148 if (r) 1149 goto fail; 1150 1151 return req; 1152 1153 fail: 1154 ceph_osdc_put_request(req); 1155 return ERR_PTR(r); 1156 } 1157 EXPORT_SYMBOL(ceph_osdc_new_request); 1158 1159 int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt) 1160 { 1161 op->extent.sparse_ext_cnt = cnt; 1162 op->extent.sparse_ext = kmalloc_array(cnt, 1163 sizeof(*op->extent.sparse_ext), 1164 GFP_NOFS); 1165 if (!op->extent.sparse_ext) 1166 return -ENOMEM; 1167 return 0; 1168 } 1169 EXPORT_SYMBOL(__ceph_alloc_sparse_ext_map); 1170 1171 /* 1172 * We keep osd requests in an rbtree, sorted by ->r_tid. 1173 */ 1174 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node) 1175 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node) 1176 1177 /* 1178 * Call @fn on each OSD request as long as @fn returns 0. 1179 */ 1180 static void for_each_request(struct ceph_osd_client *osdc, 1181 int (*fn)(struct ceph_osd_request *req, void *arg), 1182 void *arg) 1183 { 1184 struct rb_node *n, *p; 1185 1186 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) { 1187 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 1188 1189 for (p = rb_first(&osd->o_requests); p; ) { 1190 struct ceph_osd_request *req = 1191 rb_entry(p, struct ceph_osd_request, r_node); 1192 1193 p = rb_next(p); 1194 if (fn(req, arg)) 1195 return; 1196 } 1197 } 1198 1199 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) { 1200 struct ceph_osd_request *req = 1201 rb_entry(p, struct ceph_osd_request, r_node); 1202 1203 p = rb_next(p); 1204 if (fn(req, arg)) 1205 return; 1206 } 1207 } 1208 1209 static bool osd_homeless(struct ceph_osd *osd) 1210 { 1211 return osd->o_osd == CEPH_HOMELESS_OSD; 1212 } 1213 1214 static bool osd_registered(struct ceph_osd *osd) 1215 { 1216 verify_osdc_locked(osd->o_osdc); 1217 1218 return !RB_EMPTY_NODE(&osd->o_node); 1219 } 1220 1221 /* 1222 * Assumes @osd is zero-initialized. 1223 */ 1224 static void osd_init(struct ceph_osd *osd) 1225 { 1226 refcount_set(&osd->o_ref, 1); 1227 RB_CLEAR_NODE(&osd->o_node); 1228 spin_lock_init(&osd->o_requests_lock); 1229 osd->o_requests = RB_ROOT; 1230 osd->o_linger_requests = RB_ROOT; 1231 osd->o_backoff_mappings = RB_ROOT; 1232 osd->o_backoffs_by_id = RB_ROOT; 1233 INIT_LIST_HEAD(&osd->o_osd_lru); 1234 INIT_LIST_HEAD(&osd->o_keepalive_item); 1235 osd->o_incarnation = 1; 1236 mutex_init(&osd->lock); 1237 } 1238 1239 static void ceph_init_sparse_read(struct ceph_sparse_read *sr) 1240 { 1241 kfree(sr->sr_extent); 1242 memset(sr, '\0', sizeof(*sr)); 1243 sr->sr_state = CEPH_SPARSE_READ_HDR; 1244 } 1245 1246 static void osd_cleanup(struct ceph_osd *osd) 1247 { 1248 WARN_ON(!RB_EMPTY_NODE(&osd->o_node)); 1249 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests)); 1250 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests)); 1251 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings)); 1252 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id)); 1253 WARN_ON(!list_empty(&osd->o_osd_lru)); 1254 WARN_ON(!list_empty(&osd->o_keepalive_item)); 1255 1256 ceph_init_sparse_read(&osd->o_sparse_read); 1257 1258 if (osd->o_auth.authorizer) { 1259 WARN_ON(osd_homeless(osd)); 1260 ceph_auth_destroy_authorizer(osd->o_auth.authorizer); 1261 } 1262 } 1263 1264 /* 1265 * Track open sessions with osds. 1266 */ 1267 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum) 1268 { 1269 struct ceph_osd *osd; 1270 1271 WARN_ON(onum == CEPH_HOMELESS_OSD); 1272 1273 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL); 1274 osd_init(osd); 1275 osd->o_osdc = osdc; 1276 osd->o_osd = onum; 1277 osd->o_sparse_op_idx = -1; 1278 1279 ceph_init_sparse_read(&osd->o_sparse_read); 1280 1281 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr); 1282 1283 return osd; 1284 } 1285 1286 static struct ceph_osd *get_osd(struct ceph_osd *osd) 1287 { 1288 if (refcount_inc_not_zero(&osd->o_ref)) { 1289 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1, 1290 refcount_read(&osd->o_ref)); 1291 return osd; 1292 } else { 1293 dout("get_osd %p FAIL\n", osd); 1294 return NULL; 1295 } 1296 } 1297 1298 static void put_osd(struct ceph_osd *osd) 1299 { 1300 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref), 1301 refcount_read(&osd->o_ref) - 1); 1302 if (refcount_dec_and_test(&osd->o_ref)) { 1303 osd_cleanup(osd); 1304 kfree(osd); 1305 } 1306 } 1307 1308 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node) 1309 1310 static void __move_osd_to_lru(struct ceph_osd *osd) 1311 { 1312 struct ceph_osd_client *osdc = osd->o_osdc; 1313 1314 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 1315 BUG_ON(!list_empty(&osd->o_osd_lru)); 1316 1317 spin_lock(&osdc->osd_lru_lock); 1318 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); 1319 spin_unlock(&osdc->osd_lru_lock); 1320 1321 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl; 1322 } 1323 1324 static void maybe_move_osd_to_lru(struct ceph_osd *osd) 1325 { 1326 if (RB_EMPTY_ROOT(&osd->o_requests) && 1327 RB_EMPTY_ROOT(&osd->o_linger_requests)) 1328 __move_osd_to_lru(osd); 1329 } 1330 1331 static void __remove_osd_from_lru(struct ceph_osd *osd) 1332 { 1333 struct ceph_osd_client *osdc = osd->o_osdc; 1334 1335 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 1336 1337 spin_lock(&osdc->osd_lru_lock); 1338 if (!list_empty(&osd->o_osd_lru)) 1339 list_del_init(&osd->o_osd_lru); 1340 spin_unlock(&osdc->osd_lru_lock); 1341 } 1342 1343 /* 1344 * Close the connection and assign any leftover requests to the 1345 * homeless session. 1346 */ 1347 static void close_osd(struct ceph_osd *osd) 1348 { 1349 struct ceph_osd_client *osdc = osd->o_osdc; 1350 struct rb_node *n; 1351 1352 verify_osdc_wrlocked(osdc); 1353 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 1354 1355 ceph_con_close(&osd->o_con); 1356 1357 for (n = rb_first(&osd->o_requests); n; ) { 1358 struct ceph_osd_request *req = 1359 rb_entry(n, struct ceph_osd_request, r_node); 1360 1361 n = rb_next(n); /* unlink_request() */ 1362 1363 dout(" reassigning req %p tid %llu\n", req, req->r_tid); 1364 unlink_request(osd, req); 1365 link_request(&osdc->homeless_osd, req); 1366 } 1367 for (n = rb_first(&osd->o_linger_requests); n; ) { 1368 struct ceph_osd_linger_request *lreq = 1369 rb_entry(n, struct ceph_osd_linger_request, node); 1370 1371 n = rb_next(n); /* unlink_linger() */ 1372 1373 dout(" reassigning lreq %p linger_id %llu\n", lreq, 1374 lreq->linger_id); 1375 unlink_linger(osd, lreq); 1376 link_linger(&osdc->homeless_osd, lreq); 1377 } 1378 clear_backoffs(osd); 1379 1380 __remove_osd_from_lru(osd); 1381 erase_osd(&osdc->osds, osd); 1382 put_osd(osd); 1383 } 1384 1385 /* 1386 * reset osd connect 1387 */ 1388 static int reopen_osd(struct ceph_osd *osd) 1389 { 1390 struct ceph_entity_addr *peer_addr; 1391 1392 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 1393 1394 if (RB_EMPTY_ROOT(&osd->o_requests) && 1395 RB_EMPTY_ROOT(&osd->o_linger_requests)) { 1396 close_osd(osd); 1397 return -ENODEV; 1398 } 1399 1400 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd]; 1401 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) && 1402 !ceph_con_opened(&osd->o_con)) { 1403 struct rb_node *n; 1404 1405 dout("osd addr hasn't changed and connection never opened, " 1406 "letting msgr retry\n"); 1407 /* touch each r_stamp for handle_timeout()'s benfit */ 1408 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) { 1409 struct ceph_osd_request *req = 1410 rb_entry(n, struct ceph_osd_request, r_node); 1411 req->r_stamp = jiffies; 1412 } 1413 1414 return -EAGAIN; 1415 } 1416 1417 ceph_con_close(&osd->o_con); 1418 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr); 1419 osd->o_incarnation++; 1420 1421 return 0; 1422 } 1423 1424 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o, 1425 bool wrlocked) 1426 { 1427 struct ceph_osd *osd; 1428 1429 if (wrlocked) 1430 verify_osdc_wrlocked(osdc); 1431 else 1432 verify_osdc_locked(osdc); 1433 1434 if (o != CEPH_HOMELESS_OSD) 1435 osd = lookup_osd(&osdc->osds, o); 1436 else 1437 osd = &osdc->homeless_osd; 1438 if (!osd) { 1439 if (!wrlocked) 1440 return ERR_PTR(-EAGAIN); 1441 1442 osd = create_osd(osdc, o); 1443 insert_osd(&osdc->osds, osd); 1444 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, 1445 &osdc->osdmap->osd_addr[osd->o_osd]); 1446 } 1447 1448 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd); 1449 return osd; 1450 } 1451 1452 /* 1453 * Create request <-> OSD session relation. 1454 * 1455 * @req has to be assigned a tid, @osd may be homeless. 1456 */ 1457 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req) 1458 { 1459 verify_osd_locked(osd); 1460 WARN_ON(!req->r_tid || req->r_osd); 1461 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd, 1462 req, req->r_tid); 1463 1464 if (!osd_homeless(osd)) 1465 __remove_osd_from_lru(osd); 1466 else 1467 atomic_inc(&osd->o_osdc->num_homeless); 1468 1469 get_osd(osd); 1470 spin_lock(&osd->o_requests_lock); 1471 insert_request(&osd->o_requests, req); 1472 spin_unlock(&osd->o_requests_lock); 1473 req->r_osd = osd; 1474 } 1475 1476 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req) 1477 { 1478 verify_osd_locked(osd); 1479 WARN_ON(req->r_osd != osd); 1480 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd, 1481 req, req->r_tid); 1482 1483 req->r_osd = NULL; 1484 spin_lock(&osd->o_requests_lock); 1485 erase_request(&osd->o_requests, req); 1486 spin_unlock(&osd->o_requests_lock); 1487 put_osd(osd); 1488 1489 if (!osd_homeless(osd)) 1490 maybe_move_osd_to_lru(osd); 1491 else 1492 atomic_dec(&osd->o_osdc->num_homeless); 1493 } 1494 1495 static bool __pool_full(struct ceph_pg_pool_info *pi) 1496 { 1497 return pi->flags & CEPH_POOL_FLAG_FULL; 1498 } 1499 1500 static bool have_pool_full(struct ceph_osd_client *osdc) 1501 { 1502 struct rb_node *n; 1503 1504 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) { 1505 struct ceph_pg_pool_info *pi = 1506 rb_entry(n, struct ceph_pg_pool_info, node); 1507 1508 if (__pool_full(pi)) 1509 return true; 1510 } 1511 1512 return false; 1513 } 1514 1515 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id) 1516 { 1517 struct ceph_pg_pool_info *pi; 1518 1519 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id); 1520 if (!pi) 1521 return false; 1522 1523 return __pool_full(pi); 1524 } 1525 1526 /* 1527 * Returns whether a request should be blocked from being sent 1528 * based on the current osdmap and osd_client settings. 1529 */ 1530 static bool target_should_be_paused(struct ceph_osd_client *osdc, 1531 const struct ceph_osd_request_target *t, 1532 struct ceph_pg_pool_info *pi) 1533 { 1534 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD); 1535 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) || 1536 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 1537 __pool_full(pi); 1538 1539 WARN_ON(pi->id != t->target_oloc.pool); 1540 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) || 1541 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) || 1542 (osdc->osdmap->epoch < osdc->epoch_barrier); 1543 } 1544 1545 static int pick_random_replica(const struct ceph_osds *acting) 1546 { 1547 int i = get_random_u32_below(acting->size); 1548 1549 dout("%s picked osd%d, primary osd%d\n", __func__, 1550 acting->osds[i], acting->primary); 1551 return i; 1552 } 1553 1554 /* 1555 * Picks the closest replica based on client's location given by 1556 * crush_location option. Prefers the primary if the locality is 1557 * the same. 1558 */ 1559 static int pick_closest_replica(struct ceph_osd_client *osdc, 1560 const struct ceph_osds *acting) 1561 { 1562 struct ceph_options *opt = osdc->client->options; 1563 int best_i, best_locality; 1564 int i = 0, locality; 1565 1566 do { 1567 locality = ceph_get_crush_locality(osdc->osdmap, 1568 acting->osds[i], 1569 &opt->crush_locs); 1570 if (i == 0 || 1571 (locality >= 0 && best_locality < 0) || 1572 (locality >= 0 && best_locality >= 0 && 1573 locality < best_locality)) { 1574 best_i = i; 1575 best_locality = locality; 1576 } 1577 } while (++i < acting->size); 1578 1579 dout("%s picked osd%d with locality %d, primary osd%d\n", __func__, 1580 acting->osds[best_i], best_locality, acting->primary); 1581 return best_i; 1582 } 1583 1584 enum calc_target_result { 1585 CALC_TARGET_NO_ACTION = 0, 1586 CALC_TARGET_NEED_RESEND, 1587 CALC_TARGET_POOL_DNE, 1588 }; 1589 1590 static enum calc_target_result calc_target(struct ceph_osd_client *osdc, 1591 struct ceph_osd_request_target *t, 1592 bool any_change) 1593 { 1594 struct ceph_pg_pool_info *pi; 1595 struct ceph_pg pgid, last_pgid; 1596 struct ceph_osds up, acting; 1597 bool is_read = t->flags & CEPH_OSD_FLAG_READ; 1598 bool is_write = t->flags & CEPH_OSD_FLAG_WRITE; 1599 bool force_resend = false; 1600 bool unpaused = false; 1601 bool legacy_change = false; 1602 bool split = false; 1603 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE); 1604 bool recovery_deletes = ceph_osdmap_flag(osdc, 1605 CEPH_OSDMAP_RECOVERY_DELETES); 1606 enum calc_target_result ct_res; 1607 1608 t->epoch = osdc->osdmap->epoch; 1609 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool); 1610 if (!pi) { 1611 t->osd = CEPH_HOMELESS_OSD; 1612 ct_res = CALC_TARGET_POOL_DNE; 1613 goto out; 1614 } 1615 1616 if (osdc->osdmap->epoch == pi->last_force_request_resend) { 1617 if (t->last_force_resend < pi->last_force_request_resend) { 1618 t->last_force_resend = pi->last_force_request_resend; 1619 force_resend = true; 1620 } else if (t->last_force_resend == 0) { 1621 force_resend = true; 1622 } 1623 } 1624 1625 /* apply tiering */ 1626 ceph_oid_copy(&t->target_oid, &t->base_oid); 1627 ceph_oloc_copy(&t->target_oloc, &t->base_oloc); 1628 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) { 1629 if (is_read && pi->read_tier >= 0) 1630 t->target_oloc.pool = pi->read_tier; 1631 if (is_write && pi->write_tier >= 0) 1632 t->target_oloc.pool = pi->write_tier; 1633 1634 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool); 1635 if (!pi) { 1636 t->osd = CEPH_HOMELESS_OSD; 1637 ct_res = CALC_TARGET_POOL_DNE; 1638 goto out; 1639 } 1640 } 1641 1642 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid); 1643 last_pgid.pool = pgid.pool; 1644 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask); 1645 1646 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting); 1647 if (any_change && 1648 ceph_is_new_interval(&t->acting, 1649 &acting, 1650 &t->up, 1651 &up, 1652 t->size, 1653 pi->size, 1654 t->min_size, 1655 pi->min_size, 1656 t->pg_num, 1657 pi->pg_num, 1658 t->sort_bitwise, 1659 sort_bitwise, 1660 t->recovery_deletes, 1661 recovery_deletes, 1662 &last_pgid)) 1663 force_resend = true; 1664 1665 if (t->paused && !target_should_be_paused(osdc, t, pi)) { 1666 t->paused = false; 1667 unpaused = true; 1668 } 1669 legacy_change = ceph_pg_compare(&t->pgid, &pgid) || 1670 ceph_osds_changed(&t->acting, &acting, 1671 t->used_replica || any_change); 1672 if (t->pg_num) 1673 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num); 1674 1675 if (legacy_change || force_resend || split) { 1676 t->pgid = pgid; /* struct */ 1677 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid); 1678 ceph_osds_copy(&t->acting, &acting); 1679 ceph_osds_copy(&t->up, &up); 1680 t->size = pi->size; 1681 t->min_size = pi->min_size; 1682 t->pg_num = pi->pg_num; 1683 t->pg_num_mask = pi->pg_num_mask; 1684 t->sort_bitwise = sort_bitwise; 1685 t->recovery_deletes = recovery_deletes; 1686 1687 if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS | 1688 CEPH_OSD_FLAG_LOCALIZE_READS)) && 1689 !is_write && pi->type == CEPH_POOL_TYPE_REP && 1690 acting.size > 1) { 1691 int pos; 1692 1693 WARN_ON(!is_read || acting.osds[0] != acting.primary); 1694 if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) { 1695 pos = pick_random_replica(&acting); 1696 } else { 1697 pos = pick_closest_replica(osdc, &acting); 1698 } 1699 t->osd = acting.osds[pos]; 1700 t->used_replica = pos > 0; 1701 } else { 1702 t->osd = acting.primary; 1703 t->used_replica = false; 1704 } 1705 } 1706 1707 if (unpaused || legacy_change || force_resend || split) 1708 ct_res = CALC_TARGET_NEED_RESEND; 1709 else 1710 ct_res = CALC_TARGET_NO_ACTION; 1711 1712 out: 1713 dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused, 1714 legacy_change, force_resend, split, ct_res, t->osd); 1715 return ct_res; 1716 } 1717 1718 static struct ceph_spg_mapping *alloc_spg_mapping(void) 1719 { 1720 struct ceph_spg_mapping *spg; 1721 1722 spg = kmalloc(sizeof(*spg), GFP_NOIO); 1723 if (!spg) 1724 return NULL; 1725 1726 RB_CLEAR_NODE(&spg->node); 1727 spg->backoffs = RB_ROOT; 1728 return spg; 1729 } 1730 1731 static void free_spg_mapping(struct ceph_spg_mapping *spg) 1732 { 1733 WARN_ON(!RB_EMPTY_NODE(&spg->node)); 1734 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs)); 1735 1736 kfree(spg); 1737 } 1738 1739 /* 1740 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to 1741 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is 1742 * defined only within a specific spgid; it does not pass anything to 1743 * children on split, or to another primary. 1744 */ 1745 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare, 1746 RB_BYPTR, const struct ceph_spg *, node) 1747 1748 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid) 1749 { 1750 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits; 1751 } 1752 1753 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid, 1754 void **pkey, size_t *pkey_len) 1755 { 1756 if (hoid->key_len) { 1757 *pkey = hoid->key; 1758 *pkey_len = hoid->key_len; 1759 } else { 1760 *pkey = hoid->oid; 1761 *pkey_len = hoid->oid_len; 1762 } 1763 } 1764 1765 static int compare_names(const void *name1, size_t name1_len, 1766 const void *name2, size_t name2_len) 1767 { 1768 int ret; 1769 1770 ret = memcmp(name1, name2, min(name1_len, name2_len)); 1771 if (!ret) { 1772 if (name1_len < name2_len) 1773 ret = -1; 1774 else if (name1_len > name2_len) 1775 ret = 1; 1776 } 1777 return ret; 1778 } 1779 1780 static int hoid_compare(const struct ceph_hobject_id *lhs, 1781 const struct ceph_hobject_id *rhs) 1782 { 1783 void *effective_key1, *effective_key2; 1784 size_t effective_key1_len, effective_key2_len; 1785 int ret; 1786 1787 if (lhs->is_max < rhs->is_max) 1788 return -1; 1789 if (lhs->is_max > rhs->is_max) 1790 return 1; 1791 1792 if (lhs->pool < rhs->pool) 1793 return -1; 1794 if (lhs->pool > rhs->pool) 1795 return 1; 1796 1797 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs)) 1798 return -1; 1799 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs)) 1800 return 1; 1801 1802 ret = compare_names(lhs->nspace, lhs->nspace_len, 1803 rhs->nspace, rhs->nspace_len); 1804 if (ret) 1805 return ret; 1806 1807 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len); 1808 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len); 1809 ret = compare_names(effective_key1, effective_key1_len, 1810 effective_key2, effective_key2_len); 1811 if (ret) 1812 return ret; 1813 1814 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len); 1815 if (ret) 1816 return ret; 1817 1818 if (lhs->snapid < rhs->snapid) 1819 return -1; 1820 if (lhs->snapid > rhs->snapid) 1821 return 1; 1822 1823 return 0; 1824 } 1825 1826 /* 1827 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX 1828 * compat stuff here. 1829 * 1830 * Assumes @hoid is zero-initialized. 1831 */ 1832 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid) 1833 { 1834 u8 struct_v; 1835 u32 struct_len; 1836 int ret; 1837 1838 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v, 1839 &struct_len); 1840 if (ret) 1841 return ret; 1842 1843 if (struct_v < 4) { 1844 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v); 1845 goto e_inval; 1846 } 1847 1848 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len, 1849 GFP_NOIO); 1850 if (IS_ERR(hoid->key)) { 1851 ret = PTR_ERR(hoid->key); 1852 hoid->key = NULL; 1853 return ret; 1854 } 1855 1856 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len, 1857 GFP_NOIO); 1858 if (IS_ERR(hoid->oid)) { 1859 ret = PTR_ERR(hoid->oid); 1860 hoid->oid = NULL; 1861 return ret; 1862 } 1863 1864 ceph_decode_64_safe(p, end, hoid->snapid, e_inval); 1865 ceph_decode_32_safe(p, end, hoid->hash, e_inval); 1866 ceph_decode_8_safe(p, end, hoid->is_max, e_inval); 1867 1868 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len, 1869 GFP_NOIO); 1870 if (IS_ERR(hoid->nspace)) { 1871 ret = PTR_ERR(hoid->nspace); 1872 hoid->nspace = NULL; 1873 return ret; 1874 } 1875 1876 ceph_decode_64_safe(p, end, hoid->pool, e_inval); 1877 1878 ceph_hoid_build_hash_cache(hoid); 1879 return 0; 1880 1881 e_inval: 1882 return -EINVAL; 1883 } 1884 1885 static int hoid_encoding_size(const struct ceph_hobject_id *hoid) 1886 { 1887 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */ 1888 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len; 1889 } 1890 1891 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid) 1892 { 1893 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid)); 1894 ceph_encode_string(p, end, hoid->key, hoid->key_len); 1895 ceph_encode_string(p, end, hoid->oid, hoid->oid_len); 1896 ceph_encode_64(p, hoid->snapid); 1897 ceph_encode_32(p, hoid->hash); 1898 ceph_encode_8(p, hoid->is_max); 1899 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len); 1900 ceph_encode_64(p, hoid->pool); 1901 } 1902 1903 static void free_hoid(struct ceph_hobject_id *hoid) 1904 { 1905 if (hoid) { 1906 kfree(hoid->key); 1907 kfree(hoid->oid); 1908 kfree(hoid->nspace); 1909 kfree(hoid); 1910 } 1911 } 1912 1913 static struct ceph_osd_backoff *alloc_backoff(void) 1914 { 1915 struct ceph_osd_backoff *backoff; 1916 1917 backoff = kzalloc(sizeof(*backoff), GFP_NOIO); 1918 if (!backoff) 1919 return NULL; 1920 1921 RB_CLEAR_NODE(&backoff->spg_node); 1922 RB_CLEAR_NODE(&backoff->id_node); 1923 return backoff; 1924 } 1925 1926 static void free_backoff(struct ceph_osd_backoff *backoff) 1927 { 1928 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node)); 1929 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node)); 1930 1931 free_hoid(backoff->begin); 1932 free_hoid(backoff->end); 1933 kfree(backoff); 1934 } 1935 1936 /* 1937 * Within a specific spgid, backoffs are managed by ->begin hoid. 1938 */ 1939 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare, 1940 RB_BYVAL, spg_node); 1941 1942 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root, 1943 const struct ceph_hobject_id *hoid) 1944 { 1945 struct rb_node *n = root->rb_node; 1946 1947 while (n) { 1948 struct ceph_osd_backoff *cur = 1949 rb_entry(n, struct ceph_osd_backoff, spg_node); 1950 int cmp; 1951 1952 cmp = hoid_compare(hoid, cur->begin); 1953 if (cmp < 0) { 1954 n = n->rb_left; 1955 } else if (cmp > 0) { 1956 if (hoid_compare(hoid, cur->end) < 0) 1957 return cur; 1958 1959 n = n->rb_right; 1960 } else { 1961 return cur; 1962 } 1963 } 1964 1965 return NULL; 1966 } 1967 1968 /* 1969 * Each backoff has a unique id within its OSD session. 1970 */ 1971 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node) 1972 1973 static void clear_backoffs(struct ceph_osd *osd) 1974 { 1975 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) { 1976 struct ceph_spg_mapping *spg = 1977 rb_entry(rb_first(&osd->o_backoff_mappings), 1978 struct ceph_spg_mapping, node); 1979 1980 while (!RB_EMPTY_ROOT(&spg->backoffs)) { 1981 struct ceph_osd_backoff *backoff = 1982 rb_entry(rb_first(&spg->backoffs), 1983 struct ceph_osd_backoff, spg_node); 1984 1985 erase_backoff(&spg->backoffs, backoff); 1986 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff); 1987 free_backoff(backoff); 1988 } 1989 erase_spg_mapping(&osd->o_backoff_mappings, spg); 1990 free_spg_mapping(spg); 1991 } 1992 } 1993 1994 /* 1995 * Set up a temporary, non-owning view into @t. 1996 */ 1997 static void hoid_fill_from_target(struct ceph_hobject_id *hoid, 1998 const struct ceph_osd_request_target *t) 1999 { 2000 hoid->key = NULL; 2001 hoid->key_len = 0; 2002 hoid->oid = t->target_oid.name; 2003 hoid->oid_len = t->target_oid.name_len; 2004 hoid->snapid = CEPH_NOSNAP; 2005 hoid->hash = t->pgid.seed; 2006 hoid->is_max = false; 2007 if (t->target_oloc.pool_ns) { 2008 hoid->nspace = t->target_oloc.pool_ns->str; 2009 hoid->nspace_len = t->target_oloc.pool_ns->len; 2010 } else { 2011 hoid->nspace = NULL; 2012 hoid->nspace_len = 0; 2013 } 2014 hoid->pool = t->target_oloc.pool; 2015 ceph_hoid_build_hash_cache(hoid); 2016 } 2017 2018 static bool should_plug_request(struct ceph_osd_request *req) 2019 { 2020 struct ceph_osd *osd = req->r_osd; 2021 struct ceph_spg_mapping *spg; 2022 struct ceph_osd_backoff *backoff; 2023 struct ceph_hobject_id hoid; 2024 2025 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid); 2026 if (!spg) 2027 return false; 2028 2029 hoid_fill_from_target(&hoid, &req->r_t); 2030 backoff = lookup_containing_backoff(&spg->backoffs, &hoid); 2031 if (!backoff) 2032 return false; 2033 2034 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n", 2035 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool, 2036 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id); 2037 return true; 2038 } 2039 2040 /* 2041 * Keep get_num_data_items() in sync with this function. 2042 */ 2043 static void setup_request_data(struct ceph_osd_request *req) 2044 { 2045 struct ceph_msg *request_msg = req->r_request; 2046 struct ceph_msg *reply_msg = req->r_reply; 2047 struct ceph_osd_req_op *op; 2048 2049 if (req->r_request->num_data_items || req->r_reply->num_data_items) 2050 return; 2051 2052 WARN_ON(request_msg->data_length || reply_msg->data_length); 2053 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) { 2054 switch (op->op) { 2055 /* request */ 2056 case CEPH_OSD_OP_WRITE: 2057 case CEPH_OSD_OP_WRITEFULL: 2058 WARN_ON(op->indata_len != op->extent.length); 2059 ceph_osdc_msg_data_add(request_msg, 2060 &op->extent.osd_data); 2061 break; 2062 case CEPH_OSD_OP_SETXATTR: 2063 case CEPH_OSD_OP_CMPXATTR: 2064 WARN_ON(op->indata_len != op->xattr.name_len + 2065 op->xattr.value_len); 2066 ceph_osdc_msg_data_add(request_msg, 2067 &op->xattr.osd_data); 2068 break; 2069 case CEPH_OSD_OP_NOTIFY_ACK: 2070 ceph_osdc_msg_data_add(request_msg, 2071 &op->notify_ack.request_data); 2072 break; 2073 case CEPH_OSD_OP_COPY_FROM2: 2074 ceph_osdc_msg_data_add(request_msg, 2075 &op->copy_from.osd_data); 2076 break; 2077 2078 /* reply */ 2079 case CEPH_OSD_OP_STAT: 2080 ceph_osdc_msg_data_add(reply_msg, 2081 &op->raw_data_in); 2082 break; 2083 case CEPH_OSD_OP_READ: 2084 case CEPH_OSD_OP_SPARSE_READ: 2085 ceph_osdc_msg_data_add(reply_msg, 2086 &op->extent.osd_data); 2087 break; 2088 case CEPH_OSD_OP_LIST_WATCHERS: 2089 ceph_osdc_msg_data_add(reply_msg, 2090 &op->list_watchers.response_data); 2091 break; 2092 2093 /* both */ 2094 case CEPH_OSD_OP_CALL: 2095 WARN_ON(op->indata_len != op->cls.class_len + 2096 op->cls.method_len + 2097 op->cls.indata_len); 2098 ceph_osdc_msg_data_add(request_msg, 2099 &op->cls.request_info); 2100 /* optional, can be NONE */ 2101 ceph_osdc_msg_data_add(request_msg, 2102 &op->cls.request_data); 2103 /* optional, can be NONE */ 2104 ceph_osdc_msg_data_add(reply_msg, 2105 &op->cls.response_data); 2106 break; 2107 case CEPH_OSD_OP_NOTIFY: 2108 ceph_osdc_msg_data_add(request_msg, 2109 &op->notify.request_data); 2110 ceph_osdc_msg_data_add(reply_msg, 2111 &op->notify.response_data); 2112 break; 2113 } 2114 } 2115 } 2116 2117 static void encode_pgid(void **p, const struct ceph_pg *pgid) 2118 { 2119 ceph_encode_8(p, 1); 2120 ceph_encode_64(p, pgid->pool); 2121 ceph_encode_32(p, pgid->seed); 2122 ceph_encode_32(p, -1); /* preferred */ 2123 } 2124 2125 static void encode_spgid(void **p, const struct ceph_spg *spgid) 2126 { 2127 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1); 2128 encode_pgid(p, &spgid->pgid); 2129 ceph_encode_8(p, spgid->shard); 2130 } 2131 2132 static void encode_oloc(void **p, void *end, 2133 const struct ceph_object_locator *oloc) 2134 { 2135 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc)); 2136 ceph_encode_64(p, oloc->pool); 2137 ceph_encode_32(p, -1); /* preferred */ 2138 ceph_encode_32(p, 0); /* key len */ 2139 if (oloc->pool_ns) 2140 ceph_encode_string(p, end, oloc->pool_ns->str, 2141 oloc->pool_ns->len); 2142 else 2143 ceph_encode_32(p, 0); 2144 } 2145 2146 static void encode_request_partial(struct ceph_osd_request *req, 2147 struct ceph_msg *msg) 2148 { 2149 void *p = msg->front.iov_base; 2150 void *const end = p + msg->front_alloc_len; 2151 u32 data_len = 0; 2152 int i; 2153 2154 if (req->r_flags & CEPH_OSD_FLAG_WRITE) { 2155 /* snapshots aren't writeable */ 2156 WARN_ON(req->r_snapid != CEPH_NOSNAP); 2157 } else { 2158 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec || 2159 req->r_data_offset || req->r_snapc); 2160 } 2161 2162 setup_request_data(req); 2163 2164 encode_spgid(&p, &req->r_t.spgid); /* actual spg */ 2165 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */ 2166 ceph_encode_32(&p, req->r_osdc->osdmap->epoch); 2167 ceph_encode_32(&p, req->r_flags); 2168 2169 /* reqid */ 2170 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid)); 2171 memset(p, 0, sizeof(struct ceph_osd_reqid)); 2172 p += sizeof(struct ceph_osd_reqid); 2173 2174 /* trace */ 2175 memset(p, 0, sizeof(struct ceph_blkin_trace_info)); 2176 p += sizeof(struct ceph_blkin_trace_info); 2177 2178 ceph_encode_32(&p, 0); /* client_inc, always 0 */ 2179 ceph_encode_timespec64(p, &req->r_mtime); 2180 p += sizeof(struct ceph_timespec); 2181 2182 encode_oloc(&p, end, &req->r_t.target_oloc); 2183 ceph_encode_string(&p, end, req->r_t.target_oid.name, 2184 req->r_t.target_oid.name_len); 2185 2186 /* ops, can imply data */ 2187 ceph_encode_16(&p, req->r_num_ops); 2188 for (i = 0; i < req->r_num_ops; i++) { 2189 data_len += osd_req_encode_op(p, &req->r_ops[i]); 2190 p += sizeof(struct ceph_osd_op); 2191 } 2192 2193 ceph_encode_64(&p, req->r_snapid); /* snapid */ 2194 if (req->r_snapc) { 2195 ceph_encode_64(&p, req->r_snapc->seq); 2196 ceph_encode_32(&p, req->r_snapc->num_snaps); 2197 for (i = 0; i < req->r_snapc->num_snaps; i++) 2198 ceph_encode_64(&p, req->r_snapc->snaps[i]); 2199 } else { 2200 ceph_encode_64(&p, 0); /* snap_seq */ 2201 ceph_encode_32(&p, 0); /* snaps len */ 2202 } 2203 2204 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */ 2205 BUG_ON(p > end - 8); /* space for features */ 2206 2207 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */ 2208 /* front_len is finalized in encode_request_finish() */ 2209 msg->front.iov_len = p - msg->front.iov_base; 2210 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); 2211 msg->hdr.data_len = cpu_to_le32(data_len); 2212 /* 2213 * The header "data_off" is a hint to the receiver allowing it 2214 * to align received data into its buffers such that there's no 2215 * need to re-copy it before writing it to disk (direct I/O). 2216 */ 2217 msg->hdr.data_off = cpu_to_le16(req->r_data_offset); 2218 2219 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg, 2220 req->r_t.target_oid.name, req->r_t.target_oid.name_len); 2221 } 2222 2223 static void encode_request_finish(struct ceph_msg *msg) 2224 { 2225 void *p = msg->front.iov_base; 2226 void *const partial_end = p + msg->front.iov_len; 2227 void *const end = p + msg->front_alloc_len; 2228 2229 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) { 2230 /* luminous OSD -- encode features and be done */ 2231 p = partial_end; 2232 ceph_encode_64(&p, msg->con->peer_features); 2233 } else { 2234 struct { 2235 char spgid[CEPH_ENCODING_START_BLK_LEN + 2236 CEPH_PGID_ENCODING_LEN + 1]; 2237 __le32 hash; 2238 __le32 epoch; 2239 __le32 flags; 2240 char reqid[CEPH_ENCODING_START_BLK_LEN + 2241 sizeof(struct ceph_osd_reqid)]; 2242 char trace[sizeof(struct ceph_blkin_trace_info)]; 2243 __le32 client_inc; 2244 struct ceph_timespec mtime; 2245 } __packed head; 2246 struct ceph_pg pgid; 2247 void *oloc, *oid, *tail; 2248 int oloc_len, oid_len, tail_len; 2249 int len; 2250 2251 /* 2252 * Pre-luminous OSD -- reencode v8 into v4 using @head 2253 * as a temporary buffer. Encode the raw PG; the rest 2254 * is just a matter of moving oloc, oid and tail blobs 2255 * around. 2256 */ 2257 memcpy(&head, p, sizeof(head)); 2258 p += sizeof(head); 2259 2260 oloc = p; 2261 p += CEPH_ENCODING_START_BLK_LEN; 2262 pgid.pool = ceph_decode_64(&p); 2263 p += 4 + 4; /* preferred, key len */ 2264 len = ceph_decode_32(&p); 2265 p += len; /* nspace */ 2266 oloc_len = p - oloc; 2267 2268 oid = p; 2269 len = ceph_decode_32(&p); 2270 p += len; 2271 oid_len = p - oid; 2272 2273 tail = p; 2274 tail_len = partial_end - p; 2275 2276 p = msg->front.iov_base; 2277 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc)); 2278 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch)); 2279 ceph_encode_copy(&p, &head.flags, sizeof(head.flags)); 2280 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime)); 2281 2282 /* reassert_version */ 2283 memset(p, 0, sizeof(struct ceph_eversion)); 2284 p += sizeof(struct ceph_eversion); 2285 2286 BUG_ON(p >= oloc); 2287 memmove(p, oloc, oloc_len); 2288 p += oloc_len; 2289 2290 pgid.seed = le32_to_cpu(head.hash); 2291 encode_pgid(&p, &pgid); /* raw pg */ 2292 2293 BUG_ON(p >= oid); 2294 memmove(p, oid, oid_len); 2295 p += oid_len; 2296 2297 /* tail -- ops, snapid, snapc, retry_attempt */ 2298 BUG_ON(p >= tail); 2299 memmove(p, tail, tail_len); 2300 p += tail_len; 2301 2302 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */ 2303 } 2304 2305 BUG_ON(p > end); 2306 msg->front.iov_len = p - msg->front.iov_base; 2307 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); 2308 2309 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg, 2310 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len), 2311 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len), 2312 le16_to_cpu(msg->hdr.version)); 2313 } 2314 2315 /* 2316 * @req has to be assigned a tid and registered. 2317 */ 2318 static void send_request(struct ceph_osd_request *req) 2319 { 2320 struct ceph_osd *osd = req->r_osd; 2321 2322 verify_osd_locked(osd); 2323 WARN_ON(osd->o_osd != req->r_t.osd); 2324 2325 /* backoff? */ 2326 if (should_plug_request(req)) 2327 return; 2328 2329 /* 2330 * We may have a previously queued request message hanging 2331 * around. Cancel it to avoid corrupting the msgr. 2332 */ 2333 if (req->r_sent) 2334 ceph_msg_revoke(req->r_request); 2335 2336 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR; 2337 if (req->r_attempts) 2338 req->r_flags |= CEPH_OSD_FLAG_RETRY; 2339 else 2340 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY); 2341 2342 encode_request_partial(req, req->r_request); 2343 2344 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n", 2345 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed, 2346 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed, 2347 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags, 2348 req->r_attempts); 2349 2350 req->r_t.paused = false; 2351 req->r_stamp = jiffies; 2352 req->r_attempts++; 2353 2354 req->r_sent = osd->o_incarnation; 2355 req->r_request->hdr.tid = cpu_to_le64(req->r_tid); 2356 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request)); 2357 } 2358 2359 static void maybe_request_map(struct ceph_osd_client *osdc) 2360 { 2361 bool continuous = false; 2362 2363 verify_osdc_locked(osdc); 2364 WARN_ON(!osdc->osdmap->epoch); 2365 2366 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 2367 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) || 2368 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) { 2369 dout("%s osdc %p continuous\n", __func__, osdc); 2370 continuous = true; 2371 } else { 2372 dout("%s osdc %p onetime\n", __func__, osdc); 2373 } 2374 2375 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP, 2376 osdc->osdmap->epoch + 1, continuous)) 2377 ceph_monc_renew_subs(&osdc->client->monc); 2378 } 2379 2380 static void complete_request(struct ceph_osd_request *req, int err); 2381 static void send_map_check(struct ceph_osd_request *req); 2382 2383 static void __submit_request(struct ceph_osd_request *req, bool wrlocked) 2384 { 2385 struct ceph_osd_client *osdc = req->r_osdc; 2386 struct ceph_osd *osd; 2387 enum calc_target_result ct_res; 2388 int err = 0; 2389 bool need_send = false; 2390 bool promoted = false; 2391 2392 WARN_ON(req->r_tid); 2393 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked); 2394 2395 again: 2396 ct_res = calc_target(osdc, &req->r_t, false); 2397 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked) 2398 goto promote; 2399 2400 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked); 2401 if (IS_ERR(osd)) { 2402 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked); 2403 goto promote; 2404 } 2405 2406 if (osdc->abort_err) { 2407 dout("req %p abort_err %d\n", req, osdc->abort_err); 2408 err = osdc->abort_err; 2409 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) { 2410 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch, 2411 osdc->epoch_barrier); 2412 req->r_t.paused = true; 2413 maybe_request_map(osdc); 2414 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) && 2415 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) { 2416 dout("req %p pausewr\n", req); 2417 req->r_t.paused = true; 2418 maybe_request_map(osdc); 2419 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) && 2420 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) { 2421 dout("req %p pauserd\n", req); 2422 req->r_t.paused = true; 2423 maybe_request_map(osdc); 2424 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) && 2425 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY | 2426 CEPH_OSD_FLAG_FULL_FORCE)) && 2427 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 2428 pool_full(osdc, req->r_t.base_oloc.pool))) { 2429 dout("req %p full/pool_full\n", req); 2430 if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) { 2431 err = -ENOSPC; 2432 } else { 2433 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL)) 2434 pr_warn_ratelimited("cluster is full (osdmap FULL)\n"); 2435 else 2436 pr_warn_ratelimited("pool %lld is full or reached quota\n", 2437 req->r_t.base_oloc.pool); 2438 req->r_t.paused = true; 2439 maybe_request_map(osdc); 2440 } 2441 } else if (!osd_homeless(osd)) { 2442 need_send = true; 2443 } else { 2444 maybe_request_map(osdc); 2445 } 2446 2447 mutex_lock(&osd->lock); 2448 /* 2449 * Assign the tid atomically with send_request() to protect 2450 * multiple writes to the same object from racing with each 2451 * other, resulting in out of order ops on the OSDs. 2452 */ 2453 req->r_tid = atomic64_inc_return(&osdc->last_tid); 2454 link_request(osd, req); 2455 if (need_send) 2456 send_request(req); 2457 else if (err) 2458 complete_request(req, err); 2459 mutex_unlock(&osd->lock); 2460 2461 if (!err && ct_res == CALC_TARGET_POOL_DNE) 2462 send_map_check(req); 2463 2464 if (promoted) 2465 downgrade_write(&osdc->lock); 2466 return; 2467 2468 promote: 2469 up_read(&osdc->lock); 2470 down_write(&osdc->lock); 2471 wrlocked = true; 2472 promoted = true; 2473 goto again; 2474 } 2475 2476 static void account_request(struct ceph_osd_request *req) 2477 { 2478 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK)); 2479 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE))); 2480 2481 req->r_flags |= CEPH_OSD_FLAG_ONDISK; 2482 atomic_inc(&req->r_osdc->num_requests); 2483 2484 req->r_start_stamp = jiffies; 2485 req->r_start_latency = ktime_get(); 2486 } 2487 2488 static void submit_request(struct ceph_osd_request *req, bool wrlocked) 2489 { 2490 ceph_osdc_get_request(req); 2491 account_request(req); 2492 __submit_request(req, wrlocked); 2493 } 2494 2495 static void finish_request(struct ceph_osd_request *req) 2496 { 2497 struct ceph_osd_client *osdc = req->r_osdc; 2498 2499 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid)); 2500 dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 2501 2502 req->r_end_latency = ktime_get(); 2503 2504 if (req->r_osd) { 2505 ceph_init_sparse_read(&req->r_osd->o_sparse_read); 2506 unlink_request(req->r_osd, req); 2507 } 2508 atomic_dec(&osdc->num_requests); 2509 2510 /* 2511 * If an OSD has failed or returned and a request has been sent 2512 * twice, it's possible to get a reply and end up here while the 2513 * request message is queued for delivery. We will ignore the 2514 * reply, so not a big deal, but better to try and catch it. 2515 */ 2516 ceph_msg_revoke(req->r_request); 2517 ceph_msg_revoke_incoming(req->r_reply); 2518 } 2519 2520 static void __complete_request(struct ceph_osd_request *req) 2521 { 2522 dout("%s req %p tid %llu cb %ps result %d\n", __func__, req, 2523 req->r_tid, req->r_callback, req->r_result); 2524 2525 if (req->r_callback) 2526 req->r_callback(req); 2527 complete_all(&req->r_completion); 2528 ceph_osdc_put_request(req); 2529 } 2530 2531 static void complete_request_workfn(struct work_struct *work) 2532 { 2533 struct ceph_osd_request *req = 2534 container_of(work, struct ceph_osd_request, r_complete_work); 2535 2536 __complete_request(req); 2537 } 2538 2539 /* 2540 * This is open-coded in handle_reply(). 2541 */ 2542 static void complete_request(struct ceph_osd_request *req, int err) 2543 { 2544 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err); 2545 2546 req->r_result = err; 2547 finish_request(req); 2548 2549 INIT_WORK(&req->r_complete_work, complete_request_workfn); 2550 queue_work(req->r_osdc->completion_wq, &req->r_complete_work); 2551 } 2552 2553 static void cancel_map_check(struct ceph_osd_request *req) 2554 { 2555 struct ceph_osd_client *osdc = req->r_osdc; 2556 struct ceph_osd_request *lookup_req; 2557 2558 verify_osdc_wrlocked(osdc); 2559 2560 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid); 2561 if (!lookup_req) 2562 return; 2563 2564 WARN_ON(lookup_req != req); 2565 erase_request_mc(&osdc->map_checks, req); 2566 ceph_osdc_put_request(req); 2567 } 2568 2569 static void cancel_request(struct ceph_osd_request *req) 2570 { 2571 dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 2572 2573 cancel_map_check(req); 2574 finish_request(req); 2575 complete_all(&req->r_completion); 2576 ceph_osdc_put_request(req); 2577 } 2578 2579 static void abort_request(struct ceph_osd_request *req, int err) 2580 { 2581 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err); 2582 2583 cancel_map_check(req); 2584 complete_request(req, err); 2585 } 2586 2587 static int abort_fn(struct ceph_osd_request *req, void *arg) 2588 { 2589 int err = *(int *)arg; 2590 2591 abort_request(req, err); 2592 return 0; /* continue iteration */ 2593 } 2594 2595 /* 2596 * Abort all in-flight requests with @err and arrange for all future 2597 * requests to be failed immediately. 2598 */ 2599 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err) 2600 { 2601 dout("%s osdc %p err %d\n", __func__, osdc, err); 2602 down_write(&osdc->lock); 2603 for_each_request(osdc, abort_fn, &err); 2604 osdc->abort_err = err; 2605 up_write(&osdc->lock); 2606 } 2607 EXPORT_SYMBOL(ceph_osdc_abort_requests); 2608 2609 void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc) 2610 { 2611 down_write(&osdc->lock); 2612 osdc->abort_err = 0; 2613 up_write(&osdc->lock); 2614 } 2615 EXPORT_SYMBOL(ceph_osdc_clear_abort_err); 2616 2617 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb) 2618 { 2619 if (likely(eb > osdc->epoch_barrier)) { 2620 dout("updating epoch_barrier from %u to %u\n", 2621 osdc->epoch_barrier, eb); 2622 osdc->epoch_barrier = eb; 2623 /* Request map if we're not to the barrier yet */ 2624 if (eb > osdc->osdmap->epoch) 2625 maybe_request_map(osdc); 2626 } 2627 } 2628 2629 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb) 2630 { 2631 down_read(&osdc->lock); 2632 if (unlikely(eb > osdc->epoch_barrier)) { 2633 up_read(&osdc->lock); 2634 down_write(&osdc->lock); 2635 update_epoch_barrier(osdc, eb); 2636 up_write(&osdc->lock); 2637 } else { 2638 up_read(&osdc->lock); 2639 } 2640 } 2641 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier); 2642 2643 /* 2644 * We can end up releasing caps as a result of abort_request(). 2645 * In that case, we probably want to ensure that the cap release message 2646 * has an updated epoch barrier in it, so set the epoch barrier prior to 2647 * aborting the first request. 2648 */ 2649 static int abort_on_full_fn(struct ceph_osd_request *req, void *arg) 2650 { 2651 struct ceph_osd_client *osdc = req->r_osdc; 2652 bool *victims = arg; 2653 2654 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) && 2655 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 2656 pool_full(osdc, req->r_t.base_oloc.pool))) { 2657 if (!*victims) { 2658 update_epoch_barrier(osdc, osdc->osdmap->epoch); 2659 *victims = true; 2660 } 2661 abort_request(req, -ENOSPC); 2662 } 2663 2664 return 0; /* continue iteration */ 2665 } 2666 2667 /* 2668 * Drop all pending requests that are stalled waiting on a full condition to 2669 * clear, and complete them with ENOSPC as the return code. Set the 2670 * osdc->epoch_barrier to the latest map epoch that we've seen if any were 2671 * cancelled. 2672 */ 2673 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc) 2674 { 2675 bool victims = false; 2676 2677 if (ceph_test_opt(osdc->client, ABORT_ON_FULL) && 2678 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc))) 2679 for_each_request(osdc, abort_on_full_fn, &victims); 2680 } 2681 2682 static void check_pool_dne(struct ceph_osd_request *req) 2683 { 2684 struct ceph_osd_client *osdc = req->r_osdc; 2685 struct ceph_osdmap *map = osdc->osdmap; 2686 2687 verify_osdc_wrlocked(osdc); 2688 WARN_ON(!map->epoch); 2689 2690 if (req->r_attempts) { 2691 /* 2692 * We sent a request earlier, which means that 2693 * previously the pool existed, and now it does not 2694 * (i.e., it was deleted). 2695 */ 2696 req->r_map_dne_bound = map->epoch; 2697 dout("%s req %p tid %llu pool disappeared\n", __func__, req, 2698 req->r_tid); 2699 } else { 2700 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__, 2701 req, req->r_tid, req->r_map_dne_bound, map->epoch); 2702 } 2703 2704 if (req->r_map_dne_bound) { 2705 if (map->epoch >= req->r_map_dne_bound) { 2706 /* we had a new enough map */ 2707 pr_info_ratelimited("tid %llu pool does not exist\n", 2708 req->r_tid); 2709 complete_request(req, -ENOENT); 2710 } 2711 } else { 2712 send_map_check(req); 2713 } 2714 } 2715 2716 static void map_check_cb(struct ceph_mon_generic_request *greq) 2717 { 2718 struct ceph_osd_client *osdc = &greq->monc->client->osdc; 2719 struct ceph_osd_request *req; 2720 u64 tid = greq->private_data; 2721 2722 WARN_ON(greq->result || !greq->u.newest); 2723 2724 down_write(&osdc->lock); 2725 req = lookup_request_mc(&osdc->map_checks, tid); 2726 if (!req) { 2727 dout("%s tid %llu dne\n", __func__, tid); 2728 goto out_unlock; 2729 } 2730 2731 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__, 2732 req, req->r_tid, req->r_map_dne_bound, greq->u.newest); 2733 if (!req->r_map_dne_bound) 2734 req->r_map_dne_bound = greq->u.newest; 2735 erase_request_mc(&osdc->map_checks, req); 2736 check_pool_dne(req); 2737 2738 ceph_osdc_put_request(req); 2739 out_unlock: 2740 up_write(&osdc->lock); 2741 } 2742 2743 static void send_map_check(struct ceph_osd_request *req) 2744 { 2745 struct ceph_osd_client *osdc = req->r_osdc; 2746 struct ceph_osd_request *lookup_req; 2747 int ret; 2748 2749 verify_osdc_wrlocked(osdc); 2750 2751 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid); 2752 if (lookup_req) { 2753 WARN_ON(lookup_req != req); 2754 return; 2755 } 2756 2757 ceph_osdc_get_request(req); 2758 insert_request_mc(&osdc->map_checks, req); 2759 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap", 2760 map_check_cb, req->r_tid); 2761 WARN_ON(ret); 2762 } 2763 2764 /* 2765 * lingering requests, watch/notify v2 infrastructure 2766 */ 2767 static void linger_release(struct kref *kref) 2768 { 2769 struct ceph_osd_linger_request *lreq = 2770 container_of(kref, struct ceph_osd_linger_request, kref); 2771 2772 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq, 2773 lreq->reg_req, lreq->ping_req); 2774 WARN_ON(!RB_EMPTY_NODE(&lreq->node)); 2775 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node)); 2776 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node)); 2777 WARN_ON(!list_empty(&lreq->scan_item)); 2778 WARN_ON(!list_empty(&lreq->pending_lworks)); 2779 WARN_ON(lreq->osd); 2780 2781 if (lreq->request_pl) 2782 ceph_pagelist_release(lreq->request_pl); 2783 if (lreq->notify_id_pages) 2784 ceph_release_page_vector(lreq->notify_id_pages, 1); 2785 2786 ceph_osdc_put_request(lreq->reg_req); 2787 ceph_osdc_put_request(lreq->ping_req); 2788 target_destroy(&lreq->t); 2789 kfree(lreq); 2790 } 2791 2792 static void linger_put(struct ceph_osd_linger_request *lreq) 2793 { 2794 if (lreq) 2795 kref_put(&lreq->kref, linger_release); 2796 } 2797 2798 static struct ceph_osd_linger_request * 2799 linger_get(struct ceph_osd_linger_request *lreq) 2800 { 2801 kref_get(&lreq->kref); 2802 return lreq; 2803 } 2804 2805 static struct ceph_osd_linger_request * 2806 linger_alloc(struct ceph_osd_client *osdc) 2807 { 2808 struct ceph_osd_linger_request *lreq; 2809 2810 lreq = kzalloc(sizeof(*lreq), GFP_NOIO); 2811 if (!lreq) 2812 return NULL; 2813 2814 kref_init(&lreq->kref); 2815 mutex_init(&lreq->lock); 2816 RB_CLEAR_NODE(&lreq->node); 2817 RB_CLEAR_NODE(&lreq->osdc_node); 2818 RB_CLEAR_NODE(&lreq->mc_node); 2819 INIT_LIST_HEAD(&lreq->scan_item); 2820 INIT_LIST_HEAD(&lreq->pending_lworks); 2821 init_completion(&lreq->reg_commit_wait); 2822 init_completion(&lreq->notify_finish_wait); 2823 2824 lreq->osdc = osdc; 2825 target_init(&lreq->t); 2826 2827 dout("%s lreq %p\n", __func__, lreq); 2828 return lreq; 2829 } 2830 2831 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node) 2832 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node) 2833 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node) 2834 2835 /* 2836 * Create linger request <-> OSD session relation. 2837 * 2838 * @lreq has to be registered, @osd may be homeless. 2839 */ 2840 static void link_linger(struct ceph_osd *osd, 2841 struct ceph_osd_linger_request *lreq) 2842 { 2843 verify_osd_locked(osd); 2844 WARN_ON(!lreq->linger_id || lreq->osd); 2845 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd, 2846 osd->o_osd, lreq, lreq->linger_id); 2847 2848 if (!osd_homeless(osd)) 2849 __remove_osd_from_lru(osd); 2850 else 2851 atomic_inc(&osd->o_osdc->num_homeless); 2852 2853 get_osd(osd); 2854 insert_linger(&osd->o_linger_requests, lreq); 2855 lreq->osd = osd; 2856 } 2857 2858 static void unlink_linger(struct ceph_osd *osd, 2859 struct ceph_osd_linger_request *lreq) 2860 { 2861 verify_osd_locked(osd); 2862 WARN_ON(lreq->osd != osd); 2863 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd, 2864 osd->o_osd, lreq, lreq->linger_id); 2865 2866 lreq->osd = NULL; 2867 erase_linger(&osd->o_linger_requests, lreq); 2868 put_osd(osd); 2869 2870 if (!osd_homeless(osd)) 2871 maybe_move_osd_to_lru(osd); 2872 else 2873 atomic_dec(&osd->o_osdc->num_homeless); 2874 } 2875 2876 static bool __linger_registered(struct ceph_osd_linger_request *lreq) 2877 { 2878 verify_osdc_locked(lreq->osdc); 2879 2880 return !RB_EMPTY_NODE(&lreq->osdc_node); 2881 } 2882 2883 static bool linger_registered(struct ceph_osd_linger_request *lreq) 2884 { 2885 struct ceph_osd_client *osdc = lreq->osdc; 2886 bool registered; 2887 2888 down_read(&osdc->lock); 2889 registered = __linger_registered(lreq); 2890 up_read(&osdc->lock); 2891 2892 return registered; 2893 } 2894 2895 static void linger_register(struct ceph_osd_linger_request *lreq) 2896 { 2897 struct ceph_osd_client *osdc = lreq->osdc; 2898 2899 verify_osdc_wrlocked(osdc); 2900 WARN_ON(lreq->linger_id); 2901 2902 linger_get(lreq); 2903 lreq->linger_id = ++osdc->last_linger_id; 2904 insert_linger_osdc(&osdc->linger_requests, lreq); 2905 } 2906 2907 static void linger_unregister(struct ceph_osd_linger_request *lreq) 2908 { 2909 struct ceph_osd_client *osdc = lreq->osdc; 2910 2911 verify_osdc_wrlocked(osdc); 2912 2913 erase_linger_osdc(&osdc->linger_requests, lreq); 2914 linger_put(lreq); 2915 } 2916 2917 static void cancel_linger_request(struct ceph_osd_request *req) 2918 { 2919 struct ceph_osd_linger_request *lreq = req->r_priv; 2920 2921 WARN_ON(!req->r_linger); 2922 cancel_request(req); 2923 linger_put(lreq); 2924 } 2925 2926 struct linger_work { 2927 struct work_struct work; 2928 struct ceph_osd_linger_request *lreq; 2929 struct list_head pending_item; 2930 unsigned long queued_stamp; 2931 2932 union { 2933 struct { 2934 u64 notify_id; 2935 u64 notifier_id; 2936 void *payload; /* points into @msg front */ 2937 size_t payload_len; 2938 2939 struct ceph_msg *msg; /* for ceph_msg_put() */ 2940 } notify; 2941 struct { 2942 int err; 2943 } error; 2944 }; 2945 }; 2946 2947 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq, 2948 work_func_t workfn) 2949 { 2950 struct linger_work *lwork; 2951 2952 lwork = kzalloc(sizeof(*lwork), GFP_NOIO); 2953 if (!lwork) 2954 return NULL; 2955 2956 INIT_WORK(&lwork->work, workfn); 2957 INIT_LIST_HEAD(&lwork->pending_item); 2958 lwork->lreq = linger_get(lreq); 2959 2960 return lwork; 2961 } 2962 2963 static void lwork_free(struct linger_work *lwork) 2964 { 2965 struct ceph_osd_linger_request *lreq = lwork->lreq; 2966 2967 mutex_lock(&lreq->lock); 2968 list_del(&lwork->pending_item); 2969 mutex_unlock(&lreq->lock); 2970 2971 linger_put(lreq); 2972 kfree(lwork); 2973 } 2974 2975 static void lwork_queue(struct linger_work *lwork) 2976 { 2977 struct ceph_osd_linger_request *lreq = lwork->lreq; 2978 struct ceph_osd_client *osdc = lreq->osdc; 2979 2980 verify_lreq_locked(lreq); 2981 WARN_ON(!list_empty(&lwork->pending_item)); 2982 2983 lwork->queued_stamp = jiffies; 2984 list_add_tail(&lwork->pending_item, &lreq->pending_lworks); 2985 queue_work(osdc->notify_wq, &lwork->work); 2986 } 2987 2988 static void do_watch_notify(struct work_struct *w) 2989 { 2990 struct linger_work *lwork = container_of(w, struct linger_work, work); 2991 struct ceph_osd_linger_request *lreq = lwork->lreq; 2992 2993 if (!linger_registered(lreq)) { 2994 dout("%s lreq %p not registered\n", __func__, lreq); 2995 goto out; 2996 } 2997 2998 WARN_ON(!lreq->is_watch); 2999 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n", 3000 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id, 3001 lwork->notify.payload_len); 3002 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id, 3003 lwork->notify.notifier_id, lwork->notify.payload, 3004 lwork->notify.payload_len); 3005 3006 out: 3007 ceph_msg_put(lwork->notify.msg); 3008 lwork_free(lwork); 3009 } 3010 3011 static void do_watch_error(struct work_struct *w) 3012 { 3013 struct linger_work *lwork = container_of(w, struct linger_work, work); 3014 struct ceph_osd_linger_request *lreq = lwork->lreq; 3015 3016 if (!linger_registered(lreq)) { 3017 dout("%s lreq %p not registered\n", __func__, lreq); 3018 goto out; 3019 } 3020 3021 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err); 3022 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err); 3023 3024 out: 3025 lwork_free(lwork); 3026 } 3027 3028 static void queue_watch_error(struct ceph_osd_linger_request *lreq) 3029 { 3030 struct linger_work *lwork; 3031 3032 lwork = lwork_alloc(lreq, do_watch_error); 3033 if (!lwork) { 3034 pr_err("failed to allocate error-lwork\n"); 3035 return; 3036 } 3037 3038 lwork->error.err = lreq->last_error; 3039 lwork_queue(lwork); 3040 } 3041 3042 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq, 3043 int result) 3044 { 3045 if (!completion_done(&lreq->reg_commit_wait)) { 3046 lreq->reg_commit_error = (result <= 0 ? result : 0); 3047 complete_all(&lreq->reg_commit_wait); 3048 } 3049 } 3050 3051 static void linger_commit_cb(struct ceph_osd_request *req) 3052 { 3053 struct ceph_osd_linger_request *lreq = req->r_priv; 3054 3055 mutex_lock(&lreq->lock); 3056 if (req != lreq->reg_req) { 3057 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n", 3058 __func__, lreq, lreq->linger_id, req, lreq->reg_req); 3059 goto out; 3060 } 3061 3062 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq, 3063 lreq->linger_id, req->r_result); 3064 linger_reg_commit_complete(lreq, req->r_result); 3065 lreq->committed = true; 3066 3067 if (!lreq->is_watch) { 3068 struct ceph_osd_data *osd_data = 3069 osd_req_op_data(req, 0, notify, response_data); 3070 void *p = page_address(osd_data->pages[0]); 3071 3072 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY || 3073 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 3074 3075 /* make note of the notify_id */ 3076 if (req->r_ops[0].outdata_len >= sizeof(u64)) { 3077 lreq->notify_id = ceph_decode_64(&p); 3078 dout("lreq %p notify_id %llu\n", lreq, 3079 lreq->notify_id); 3080 } else { 3081 dout("lreq %p no notify_id\n", lreq); 3082 } 3083 } 3084 3085 out: 3086 mutex_unlock(&lreq->lock); 3087 linger_put(lreq); 3088 } 3089 3090 static int normalize_watch_error(int err) 3091 { 3092 /* 3093 * Translate ENOENT -> ENOTCONN so that a delete->disconnection 3094 * notification and a failure to reconnect because we raced with 3095 * the delete appear the same to the user. 3096 */ 3097 if (err == -ENOENT) 3098 err = -ENOTCONN; 3099 3100 return err; 3101 } 3102 3103 static void linger_reconnect_cb(struct ceph_osd_request *req) 3104 { 3105 struct ceph_osd_linger_request *lreq = req->r_priv; 3106 3107 mutex_lock(&lreq->lock); 3108 if (req != lreq->reg_req) { 3109 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n", 3110 __func__, lreq, lreq->linger_id, req, lreq->reg_req); 3111 goto out; 3112 } 3113 3114 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__, 3115 lreq, lreq->linger_id, req->r_result, lreq->last_error); 3116 if (req->r_result < 0) { 3117 if (!lreq->last_error) { 3118 lreq->last_error = normalize_watch_error(req->r_result); 3119 queue_watch_error(lreq); 3120 } 3121 } 3122 3123 out: 3124 mutex_unlock(&lreq->lock); 3125 linger_put(lreq); 3126 } 3127 3128 static void send_linger(struct ceph_osd_linger_request *lreq) 3129 { 3130 struct ceph_osd_client *osdc = lreq->osdc; 3131 struct ceph_osd_request *req; 3132 int ret; 3133 3134 verify_osdc_wrlocked(osdc); 3135 mutex_lock(&lreq->lock); 3136 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id); 3137 3138 if (lreq->reg_req) { 3139 if (lreq->reg_req->r_osd) 3140 cancel_linger_request(lreq->reg_req); 3141 ceph_osdc_put_request(lreq->reg_req); 3142 } 3143 3144 req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO); 3145 BUG_ON(!req); 3146 3147 target_copy(&req->r_t, &lreq->t); 3148 req->r_mtime = lreq->mtime; 3149 3150 if (lreq->is_watch && lreq->committed) { 3151 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT, 3152 lreq->linger_id, ++lreq->register_gen); 3153 dout("lreq %p reconnect register_gen %u\n", lreq, 3154 req->r_ops[0].watch.gen); 3155 req->r_callback = linger_reconnect_cb; 3156 } else { 3157 if (lreq->is_watch) { 3158 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH, 3159 lreq->linger_id, 0); 3160 } else { 3161 lreq->notify_id = 0; 3162 3163 refcount_inc(&lreq->request_pl->refcnt); 3164 osd_req_op_notify_init(req, 0, lreq->linger_id, 3165 lreq->request_pl); 3166 ceph_osd_data_pages_init( 3167 osd_req_op_data(req, 0, notify, response_data), 3168 lreq->notify_id_pages, PAGE_SIZE, 0, false, false); 3169 } 3170 dout("lreq %p register\n", lreq); 3171 req->r_callback = linger_commit_cb; 3172 } 3173 3174 ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 3175 BUG_ON(ret); 3176 3177 req->r_priv = linger_get(lreq); 3178 req->r_linger = true; 3179 lreq->reg_req = req; 3180 mutex_unlock(&lreq->lock); 3181 3182 submit_request(req, true); 3183 } 3184 3185 static void linger_ping_cb(struct ceph_osd_request *req) 3186 { 3187 struct ceph_osd_linger_request *lreq = req->r_priv; 3188 3189 mutex_lock(&lreq->lock); 3190 if (req != lreq->ping_req) { 3191 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n", 3192 __func__, lreq, lreq->linger_id, req, lreq->ping_req); 3193 goto out; 3194 } 3195 3196 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n", 3197 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent, 3198 lreq->last_error); 3199 if (lreq->register_gen == req->r_ops[0].watch.gen) { 3200 if (!req->r_result) { 3201 lreq->watch_valid_thru = lreq->ping_sent; 3202 } else if (!lreq->last_error) { 3203 lreq->last_error = normalize_watch_error(req->r_result); 3204 queue_watch_error(lreq); 3205 } 3206 } else { 3207 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq, 3208 lreq->register_gen, req->r_ops[0].watch.gen); 3209 } 3210 3211 out: 3212 mutex_unlock(&lreq->lock); 3213 linger_put(lreq); 3214 } 3215 3216 static void send_linger_ping(struct ceph_osd_linger_request *lreq) 3217 { 3218 struct ceph_osd_client *osdc = lreq->osdc; 3219 struct ceph_osd_request *req; 3220 int ret; 3221 3222 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) { 3223 dout("%s PAUSERD\n", __func__); 3224 return; 3225 } 3226 3227 lreq->ping_sent = jiffies; 3228 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n", 3229 __func__, lreq, lreq->linger_id, lreq->ping_sent, 3230 lreq->register_gen); 3231 3232 if (lreq->ping_req) { 3233 if (lreq->ping_req->r_osd) 3234 cancel_linger_request(lreq->ping_req); 3235 ceph_osdc_put_request(lreq->ping_req); 3236 } 3237 3238 req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO); 3239 BUG_ON(!req); 3240 3241 target_copy(&req->r_t, &lreq->t); 3242 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id, 3243 lreq->register_gen); 3244 req->r_callback = linger_ping_cb; 3245 3246 ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 3247 BUG_ON(ret); 3248 3249 req->r_priv = linger_get(lreq); 3250 req->r_linger = true; 3251 lreq->ping_req = req; 3252 3253 ceph_osdc_get_request(req); 3254 account_request(req); 3255 req->r_tid = atomic64_inc_return(&osdc->last_tid); 3256 link_request(lreq->osd, req); 3257 send_request(req); 3258 } 3259 3260 static void linger_submit(struct ceph_osd_linger_request *lreq) 3261 { 3262 struct ceph_osd_client *osdc = lreq->osdc; 3263 struct ceph_osd *osd; 3264 3265 down_write(&osdc->lock); 3266 linger_register(lreq); 3267 3268 calc_target(osdc, &lreq->t, false); 3269 osd = lookup_create_osd(osdc, lreq->t.osd, true); 3270 link_linger(osd, lreq); 3271 3272 send_linger(lreq); 3273 up_write(&osdc->lock); 3274 } 3275 3276 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq) 3277 { 3278 struct ceph_osd_client *osdc = lreq->osdc; 3279 struct ceph_osd_linger_request *lookup_lreq; 3280 3281 verify_osdc_wrlocked(osdc); 3282 3283 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks, 3284 lreq->linger_id); 3285 if (!lookup_lreq) 3286 return; 3287 3288 WARN_ON(lookup_lreq != lreq); 3289 erase_linger_mc(&osdc->linger_map_checks, lreq); 3290 linger_put(lreq); 3291 } 3292 3293 /* 3294 * @lreq has to be both registered and linked. 3295 */ 3296 static void __linger_cancel(struct ceph_osd_linger_request *lreq) 3297 { 3298 if (lreq->ping_req && lreq->ping_req->r_osd) 3299 cancel_linger_request(lreq->ping_req); 3300 if (lreq->reg_req && lreq->reg_req->r_osd) 3301 cancel_linger_request(lreq->reg_req); 3302 cancel_linger_map_check(lreq); 3303 unlink_linger(lreq->osd, lreq); 3304 linger_unregister(lreq); 3305 } 3306 3307 static void linger_cancel(struct ceph_osd_linger_request *lreq) 3308 { 3309 struct ceph_osd_client *osdc = lreq->osdc; 3310 3311 down_write(&osdc->lock); 3312 if (__linger_registered(lreq)) 3313 __linger_cancel(lreq); 3314 up_write(&osdc->lock); 3315 } 3316 3317 static void send_linger_map_check(struct ceph_osd_linger_request *lreq); 3318 3319 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq) 3320 { 3321 struct ceph_osd_client *osdc = lreq->osdc; 3322 struct ceph_osdmap *map = osdc->osdmap; 3323 3324 verify_osdc_wrlocked(osdc); 3325 WARN_ON(!map->epoch); 3326 3327 if (lreq->register_gen) { 3328 lreq->map_dne_bound = map->epoch; 3329 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__, 3330 lreq, lreq->linger_id); 3331 } else { 3332 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n", 3333 __func__, lreq, lreq->linger_id, lreq->map_dne_bound, 3334 map->epoch); 3335 } 3336 3337 if (lreq->map_dne_bound) { 3338 if (map->epoch >= lreq->map_dne_bound) { 3339 /* we had a new enough map */ 3340 pr_info("linger_id %llu pool does not exist\n", 3341 lreq->linger_id); 3342 linger_reg_commit_complete(lreq, -ENOENT); 3343 __linger_cancel(lreq); 3344 } 3345 } else { 3346 send_linger_map_check(lreq); 3347 } 3348 } 3349 3350 static void linger_map_check_cb(struct ceph_mon_generic_request *greq) 3351 { 3352 struct ceph_osd_client *osdc = &greq->monc->client->osdc; 3353 struct ceph_osd_linger_request *lreq; 3354 u64 linger_id = greq->private_data; 3355 3356 WARN_ON(greq->result || !greq->u.newest); 3357 3358 down_write(&osdc->lock); 3359 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id); 3360 if (!lreq) { 3361 dout("%s linger_id %llu dne\n", __func__, linger_id); 3362 goto out_unlock; 3363 } 3364 3365 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n", 3366 __func__, lreq, lreq->linger_id, lreq->map_dne_bound, 3367 greq->u.newest); 3368 if (!lreq->map_dne_bound) 3369 lreq->map_dne_bound = greq->u.newest; 3370 erase_linger_mc(&osdc->linger_map_checks, lreq); 3371 check_linger_pool_dne(lreq); 3372 3373 linger_put(lreq); 3374 out_unlock: 3375 up_write(&osdc->lock); 3376 } 3377 3378 static void send_linger_map_check(struct ceph_osd_linger_request *lreq) 3379 { 3380 struct ceph_osd_client *osdc = lreq->osdc; 3381 struct ceph_osd_linger_request *lookup_lreq; 3382 int ret; 3383 3384 verify_osdc_wrlocked(osdc); 3385 3386 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks, 3387 lreq->linger_id); 3388 if (lookup_lreq) { 3389 WARN_ON(lookup_lreq != lreq); 3390 return; 3391 } 3392 3393 linger_get(lreq); 3394 insert_linger_mc(&osdc->linger_map_checks, lreq); 3395 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap", 3396 linger_map_check_cb, lreq->linger_id); 3397 WARN_ON(ret); 3398 } 3399 3400 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq) 3401 { 3402 int ret; 3403 3404 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id); 3405 ret = wait_for_completion_killable(&lreq->reg_commit_wait); 3406 return ret ?: lreq->reg_commit_error; 3407 } 3408 3409 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq, 3410 unsigned long timeout) 3411 { 3412 long left; 3413 3414 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id); 3415 left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait, 3416 ceph_timeout_jiffies(timeout)); 3417 if (left <= 0) 3418 left = left ?: -ETIMEDOUT; 3419 else 3420 left = lreq->notify_finish_error; /* completed */ 3421 3422 return left; 3423 } 3424 3425 /* 3426 * Timeout callback, called every N seconds. When 1 or more OSD 3427 * requests has been active for more than N seconds, we send a keepalive 3428 * (tag + timestamp) to its OSD to ensure any communications channel 3429 * reset is detected. 3430 */ 3431 static void handle_timeout(struct work_struct *work) 3432 { 3433 struct ceph_osd_client *osdc = 3434 container_of(work, struct ceph_osd_client, timeout_work.work); 3435 struct ceph_options *opts = osdc->client->options; 3436 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout; 3437 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout; 3438 LIST_HEAD(slow_osds); 3439 struct rb_node *n, *p; 3440 3441 dout("%s osdc %p\n", __func__, osdc); 3442 down_write(&osdc->lock); 3443 3444 /* 3445 * ping osds that are a bit slow. this ensures that if there 3446 * is a break in the TCP connection we will notice, and reopen 3447 * a connection with that osd (from the fault callback). 3448 */ 3449 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) { 3450 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 3451 bool found = false; 3452 3453 for (p = rb_first(&osd->o_requests); p; ) { 3454 struct ceph_osd_request *req = 3455 rb_entry(p, struct ceph_osd_request, r_node); 3456 3457 p = rb_next(p); /* abort_request() */ 3458 3459 if (time_before(req->r_stamp, cutoff)) { 3460 dout(" req %p tid %llu on osd%d is laggy\n", 3461 req, req->r_tid, osd->o_osd); 3462 found = true; 3463 } 3464 if (opts->osd_request_timeout && 3465 time_before(req->r_start_stamp, expiry_cutoff)) { 3466 pr_err_ratelimited("tid %llu on osd%d timeout\n", 3467 req->r_tid, osd->o_osd); 3468 abort_request(req, -ETIMEDOUT); 3469 } 3470 } 3471 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) { 3472 struct ceph_osd_linger_request *lreq = 3473 rb_entry(p, struct ceph_osd_linger_request, node); 3474 3475 dout(" lreq %p linger_id %llu is served by osd%d\n", 3476 lreq, lreq->linger_id, osd->o_osd); 3477 found = true; 3478 3479 mutex_lock(&lreq->lock); 3480 if (lreq->is_watch && lreq->committed && !lreq->last_error) 3481 send_linger_ping(lreq); 3482 mutex_unlock(&lreq->lock); 3483 } 3484 3485 if (found) 3486 list_move_tail(&osd->o_keepalive_item, &slow_osds); 3487 } 3488 3489 if (opts->osd_request_timeout) { 3490 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) { 3491 struct ceph_osd_request *req = 3492 rb_entry(p, struct ceph_osd_request, r_node); 3493 3494 p = rb_next(p); /* abort_request() */ 3495 3496 if (time_before(req->r_start_stamp, expiry_cutoff)) { 3497 pr_err_ratelimited("tid %llu on osd%d timeout\n", 3498 req->r_tid, osdc->homeless_osd.o_osd); 3499 abort_request(req, -ETIMEDOUT); 3500 } 3501 } 3502 } 3503 3504 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds)) 3505 maybe_request_map(osdc); 3506 3507 while (!list_empty(&slow_osds)) { 3508 struct ceph_osd *osd = list_first_entry(&slow_osds, 3509 struct ceph_osd, 3510 o_keepalive_item); 3511 list_del_init(&osd->o_keepalive_item); 3512 ceph_con_keepalive(&osd->o_con); 3513 } 3514 3515 up_write(&osdc->lock); 3516 schedule_delayed_work(&osdc->timeout_work, 3517 osdc->client->options->osd_keepalive_timeout); 3518 } 3519 3520 static void handle_osds_timeout(struct work_struct *work) 3521 { 3522 struct ceph_osd_client *osdc = 3523 container_of(work, struct ceph_osd_client, 3524 osds_timeout_work.work); 3525 unsigned long delay = osdc->client->options->osd_idle_ttl / 4; 3526 struct ceph_osd *osd, *nosd; 3527 3528 dout("%s osdc %p\n", __func__, osdc); 3529 down_write(&osdc->lock); 3530 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { 3531 if (time_before(jiffies, osd->lru_ttl)) 3532 break; 3533 3534 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests)); 3535 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests)); 3536 close_osd(osd); 3537 } 3538 3539 up_write(&osdc->lock); 3540 schedule_delayed_work(&osdc->osds_timeout_work, 3541 round_jiffies_relative(delay)); 3542 } 3543 3544 static int ceph_oloc_decode(void **p, void *end, 3545 struct ceph_object_locator *oloc) 3546 { 3547 u8 struct_v, struct_cv; 3548 u32 len; 3549 void *struct_end; 3550 int ret = 0; 3551 3552 ceph_decode_need(p, end, 1 + 1 + 4, e_inval); 3553 struct_v = ceph_decode_8(p); 3554 struct_cv = ceph_decode_8(p); 3555 if (struct_v < 3) { 3556 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n", 3557 struct_v, struct_cv); 3558 goto e_inval; 3559 } 3560 if (struct_cv > 6) { 3561 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n", 3562 struct_v, struct_cv); 3563 goto e_inval; 3564 } 3565 len = ceph_decode_32(p); 3566 ceph_decode_need(p, end, len, e_inval); 3567 struct_end = *p + len; 3568 3569 oloc->pool = ceph_decode_64(p); 3570 *p += 4; /* skip preferred */ 3571 3572 len = ceph_decode_32(p); 3573 if (len > 0) { 3574 pr_warn("ceph_object_locator::key is set\n"); 3575 goto e_inval; 3576 } 3577 3578 if (struct_v >= 5) { 3579 bool changed = false; 3580 3581 len = ceph_decode_32(p); 3582 if (len > 0) { 3583 ceph_decode_need(p, end, len, e_inval); 3584 if (!oloc->pool_ns || 3585 ceph_compare_string(oloc->pool_ns, *p, len)) 3586 changed = true; 3587 *p += len; 3588 } else { 3589 if (oloc->pool_ns) 3590 changed = true; 3591 } 3592 if (changed) { 3593 /* redirect changes namespace */ 3594 pr_warn("ceph_object_locator::nspace is changed\n"); 3595 goto e_inval; 3596 } 3597 } 3598 3599 if (struct_v >= 6) { 3600 s64 hash = ceph_decode_64(p); 3601 if (hash != -1) { 3602 pr_warn("ceph_object_locator::hash is set\n"); 3603 goto e_inval; 3604 } 3605 } 3606 3607 /* skip the rest */ 3608 *p = struct_end; 3609 out: 3610 return ret; 3611 3612 e_inval: 3613 ret = -EINVAL; 3614 goto out; 3615 } 3616 3617 static int ceph_redirect_decode(void **p, void *end, 3618 struct ceph_request_redirect *redir) 3619 { 3620 u8 struct_v, struct_cv; 3621 u32 len; 3622 void *struct_end; 3623 int ret; 3624 3625 ceph_decode_need(p, end, 1 + 1 + 4, e_inval); 3626 struct_v = ceph_decode_8(p); 3627 struct_cv = ceph_decode_8(p); 3628 if (struct_cv > 1) { 3629 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n", 3630 struct_v, struct_cv); 3631 goto e_inval; 3632 } 3633 len = ceph_decode_32(p); 3634 ceph_decode_need(p, end, len, e_inval); 3635 struct_end = *p + len; 3636 3637 ret = ceph_oloc_decode(p, end, &redir->oloc); 3638 if (ret) 3639 goto out; 3640 3641 len = ceph_decode_32(p); 3642 if (len > 0) { 3643 pr_warn("ceph_request_redirect::object_name is set\n"); 3644 goto e_inval; 3645 } 3646 3647 /* skip the rest */ 3648 *p = struct_end; 3649 out: 3650 return ret; 3651 3652 e_inval: 3653 ret = -EINVAL; 3654 goto out; 3655 } 3656 3657 struct MOSDOpReply { 3658 struct ceph_pg pgid; 3659 u64 flags; 3660 int result; 3661 u32 epoch; 3662 int num_ops; 3663 u32 outdata_len[CEPH_OSD_MAX_OPS]; 3664 s32 rval[CEPH_OSD_MAX_OPS]; 3665 int retry_attempt; 3666 struct ceph_eversion replay_version; 3667 u64 user_version; 3668 struct ceph_request_redirect redirect; 3669 }; 3670 3671 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m) 3672 { 3673 void *p = msg->front.iov_base; 3674 void *const end = p + msg->front.iov_len; 3675 u16 version = le16_to_cpu(msg->hdr.version); 3676 struct ceph_eversion bad_replay_version; 3677 u8 decode_redir; 3678 u32 len; 3679 int ret; 3680 int i; 3681 3682 ceph_decode_32_safe(&p, end, len, e_inval); 3683 ceph_decode_need(&p, end, len, e_inval); 3684 p += len; /* skip oid */ 3685 3686 ret = ceph_decode_pgid(&p, end, &m->pgid); 3687 if (ret) 3688 return ret; 3689 3690 ceph_decode_64_safe(&p, end, m->flags, e_inval); 3691 ceph_decode_32_safe(&p, end, m->result, e_inval); 3692 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval); 3693 memcpy(&bad_replay_version, p, sizeof(bad_replay_version)); 3694 p += sizeof(bad_replay_version); 3695 ceph_decode_32_safe(&p, end, m->epoch, e_inval); 3696 3697 ceph_decode_32_safe(&p, end, m->num_ops, e_inval); 3698 if (m->num_ops > ARRAY_SIZE(m->outdata_len)) 3699 goto e_inval; 3700 3701 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op), 3702 e_inval); 3703 for (i = 0; i < m->num_ops; i++) { 3704 struct ceph_osd_op *op = p; 3705 3706 m->outdata_len[i] = le32_to_cpu(op->payload_len); 3707 p += sizeof(*op); 3708 } 3709 3710 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval); 3711 for (i = 0; i < m->num_ops; i++) 3712 ceph_decode_32_safe(&p, end, m->rval[i], e_inval); 3713 3714 if (version >= 5) { 3715 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval); 3716 memcpy(&m->replay_version, p, sizeof(m->replay_version)); 3717 p += sizeof(m->replay_version); 3718 ceph_decode_64_safe(&p, end, m->user_version, e_inval); 3719 } else { 3720 m->replay_version = bad_replay_version; /* struct */ 3721 m->user_version = le64_to_cpu(m->replay_version.version); 3722 } 3723 3724 if (version >= 6) { 3725 if (version >= 7) 3726 ceph_decode_8_safe(&p, end, decode_redir, e_inval); 3727 else 3728 decode_redir = 1; 3729 } else { 3730 decode_redir = 0; 3731 } 3732 3733 if (decode_redir) { 3734 ret = ceph_redirect_decode(&p, end, &m->redirect); 3735 if (ret) 3736 return ret; 3737 } else { 3738 ceph_oloc_init(&m->redirect.oloc); 3739 } 3740 3741 return 0; 3742 3743 e_inval: 3744 return -EINVAL; 3745 } 3746 3747 /* 3748 * Handle MOSDOpReply. Set ->r_result and call the callback if it is 3749 * specified. 3750 */ 3751 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg) 3752 { 3753 struct ceph_osd_client *osdc = osd->o_osdc; 3754 struct ceph_osd_request *req; 3755 struct MOSDOpReply m; 3756 u64 tid = le64_to_cpu(msg->hdr.tid); 3757 u32 data_len = 0; 3758 int ret; 3759 int i; 3760 3761 dout("%s msg %p tid %llu\n", __func__, msg, tid); 3762 3763 down_read(&osdc->lock); 3764 if (!osd_registered(osd)) { 3765 dout("%s osd%d unknown\n", __func__, osd->o_osd); 3766 goto out_unlock_osdc; 3767 } 3768 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num)); 3769 3770 mutex_lock(&osd->lock); 3771 req = lookup_request(&osd->o_requests, tid); 3772 if (!req) { 3773 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid); 3774 goto out_unlock_session; 3775 } 3776 3777 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns; 3778 ret = decode_MOSDOpReply(msg, &m); 3779 m.redirect.oloc.pool_ns = NULL; 3780 if (ret) { 3781 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n", 3782 req->r_tid, ret); 3783 ceph_msg_dump(msg); 3784 goto fail_request; 3785 } 3786 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n", 3787 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed, 3788 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch), 3789 le64_to_cpu(m.replay_version.version), m.user_version); 3790 3791 if (m.retry_attempt >= 0) { 3792 if (m.retry_attempt != req->r_attempts - 1) { 3793 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n", 3794 req, req->r_tid, m.retry_attempt, 3795 req->r_attempts - 1); 3796 goto out_unlock_session; 3797 } 3798 } else { 3799 WARN_ON(1); /* MOSDOpReply v4 is assumed */ 3800 } 3801 3802 if (!ceph_oloc_empty(&m.redirect.oloc)) { 3803 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid, 3804 m.redirect.oloc.pool); 3805 unlink_request(osd, req); 3806 mutex_unlock(&osd->lock); 3807 3808 /* 3809 * Not ceph_oloc_copy() - changing pool_ns is not 3810 * supported. 3811 */ 3812 req->r_t.target_oloc.pool = m.redirect.oloc.pool; 3813 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED | 3814 CEPH_OSD_FLAG_IGNORE_OVERLAY | 3815 CEPH_OSD_FLAG_IGNORE_CACHE; 3816 req->r_tid = 0; 3817 __submit_request(req, false); 3818 goto out_unlock_osdc; 3819 } 3820 3821 if (m.result == -EAGAIN) { 3822 dout("req %p tid %llu EAGAIN\n", req, req->r_tid); 3823 unlink_request(osd, req); 3824 mutex_unlock(&osd->lock); 3825 3826 /* 3827 * The object is missing on the replica or not (yet) 3828 * readable. Clear pgid to force a resend to the primary 3829 * via legacy_change. 3830 */ 3831 req->r_t.pgid.pool = 0; 3832 req->r_t.pgid.seed = 0; 3833 WARN_ON(!req->r_t.used_replica); 3834 req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS | 3835 CEPH_OSD_FLAG_LOCALIZE_READS); 3836 req->r_tid = 0; 3837 __submit_request(req, false); 3838 goto out_unlock_osdc; 3839 } 3840 3841 if (m.num_ops != req->r_num_ops) { 3842 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops, 3843 req->r_num_ops, req->r_tid); 3844 goto fail_request; 3845 } 3846 for (i = 0; i < req->r_num_ops; i++) { 3847 dout(" req %p tid %llu op %d rval %d len %u\n", req, 3848 req->r_tid, i, m.rval[i], m.outdata_len[i]); 3849 req->r_ops[i].rval = m.rval[i]; 3850 req->r_ops[i].outdata_len = m.outdata_len[i]; 3851 data_len += m.outdata_len[i]; 3852 } 3853 if (data_len != le32_to_cpu(msg->hdr.data_len)) { 3854 pr_err("sum of lens %u != %u for tid %llu\n", data_len, 3855 le32_to_cpu(msg->hdr.data_len), req->r_tid); 3856 goto fail_request; 3857 } 3858 dout("%s req %p tid %llu result %d data_len %u\n", __func__, 3859 req, req->r_tid, m.result, data_len); 3860 3861 /* 3862 * Since we only ever request ONDISK, we should only ever get 3863 * one (type of) reply back. 3864 */ 3865 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK)); 3866 req->r_version = m.user_version; 3867 req->r_result = m.result ?: data_len; 3868 finish_request(req); 3869 mutex_unlock(&osd->lock); 3870 up_read(&osdc->lock); 3871 3872 __complete_request(req); 3873 return; 3874 3875 fail_request: 3876 complete_request(req, -EIO); 3877 out_unlock_session: 3878 mutex_unlock(&osd->lock); 3879 out_unlock_osdc: 3880 up_read(&osdc->lock); 3881 } 3882 3883 static void set_pool_was_full(struct ceph_osd_client *osdc) 3884 { 3885 struct rb_node *n; 3886 3887 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) { 3888 struct ceph_pg_pool_info *pi = 3889 rb_entry(n, struct ceph_pg_pool_info, node); 3890 3891 pi->was_full = __pool_full(pi); 3892 } 3893 } 3894 3895 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id) 3896 { 3897 struct ceph_pg_pool_info *pi; 3898 3899 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id); 3900 if (!pi) 3901 return false; 3902 3903 return pi->was_full && !__pool_full(pi); 3904 } 3905 3906 static enum calc_target_result 3907 recalc_linger_target(struct ceph_osd_linger_request *lreq) 3908 { 3909 struct ceph_osd_client *osdc = lreq->osdc; 3910 enum calc_target_result ct_res; 3911 3912 ct_res = calc_target(osdc, &lreq->t, true); 3913 if (ct_res == CALC_TARGET_NEED_RESEND) { 3914 struct ceph_osd *osd; 3915 3916 osd = lookup_create_osd(osdc, lreq->t.osd, true); 3917 if (osd != lreq->osd) { 3918 unlink_linger(lreq->osd, lreq); 3919 link_linger(osd, lreq); 3920 } 3921 } 3922 3923 return ct_res; 3924 } 3925 3926 /* 3927 * Requeue requests whose mapping to an OSD has changed. 3928 */ 3929 static void scan_requests(struct ceph_osd *osd, 3930 bool force_resend, 3931 bool cleared_full, 3932 bool check_pool_cleared_full, 3933 struct rb_root *need_resend, 3934 struct list_head *need_resend_linger) 3935 { 3936 struct ceph_osd_client *osdc = osd->o_osdc; 3937 struct rb_node *n; 3938 bool force_resend_writes; 3939 3940 for (n = rb_first(&osd->o_linger_requests); n; ) { 3941 struct ceph_osd_linger_request *lreq = 3942 rb_entry(n, struct ceph_osd_linger_request, node); 3943 enum calc_target_result ct_res; 3944 3945 n = rb_next(n); /* recalc_linger_target() */ 3946 3947 dout("%s lreq %p linger_id %llu\n", __func__, lreq, 3948 lreq->linger_id); 3949 ct_res = recalc_linger_target(lreq); 3950 switch (ct_res) { 3951 case CALC_TARGET_NO_ACTION: 3952 force_resend_writes = cleared_full || 3953 (check_pool_cleared_full && 3954 pool_cleared_full(osdc, lreq->t.base_oloc.pool)); 3955 if (!force_resend && !force_resend_writes) 3956 break; 3957 3958 fallthrough; 3959 case CALC_TARGET_NEED_RESEND: 3960 cancel_linger_map_check(lreq); 3961 /* 3962 * scan_requests() for the previous epoch(s) 3963 * may have already added it to the list, since 3964 * it's not unlinked here. 3965 */ 3966 if (list_empty(&lreq->scan_item)) 3967 list_add_tail(&lreq->scan_item, need_resend_linger); 3968 break; 3969 case CALC_TARGET_POOL_DNE: 3970 list_del_init(&lreq->scan_item); 3971 check_linger_pool_dne(lreq); 3972 break; 3973 } 3974 } 3975 3976 for (n = rb_first(&osd->o_requests); n; ) { 3977 struct ceph_osd_request *req = 3978 rb_entry(n, struct ceph_osd_request, r_node); 3979 enum calc_target_result ct_res; 3980 3981 n = rb_next(n); /* unlink_request(), check_pool_dne() */ 3982 3983 dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 3984 ct_res = calc_target(osdc, &req->r_t, false); 3985 switch (ct_res) { 3986 case CALC_TARGET_NO_ACTION: 3987 force_resend_writes = cleared_full || 3988 (check_pool_cleared_full && 3989 pool_cleared_full(osdc, req->r_t.base_oloc.pool)); 3990 if (!force_resend && 3991 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) || 3992 !force_resend_writes)) 3993 break; 3994 3995 fallthrough; 3996 case CALC_TARGET_NEED_RESEND: 3997 cancel_map_check(req); 3998 unlink_request(osd, req); 3999 insert_request(need_resend, req); 4000 break; 4001 case CALC_TARGET_POOL_DNE: 4002 check_pool_dne(req); 4003 break; 4004 } 4005 } 4006 } 4007 4008 static int handle_one_map(struct ceph_osd_client *osdc, 4009 void *p, void *end, bool incremental, 4010 struct rb_root *need_resend, 4011 struct list_head *need_resend_linger) 4012 { 4013 struct ceph_osdmap *newmap; 4014 struct rb_node *n; 4015 bool skipped_map = false; 4016 bool was_full; 4017 4018 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL); 4019 set_pool_was_full(osdc); 4020 4021 if (incremental) 4022 newmap = osdmap_apply_incremental(&p, end, 4023 ceph_msgr2(osdc->client), 4024 osdc->osdmap); 4025 else 4026 newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client)); 4027 if (IS_ERR(newmap)) 4028 return PTR_ERR(newmap); 4029 4030 if (newmap != osdc->osdmap) { 4031 /* 4032 * Preserve ->was_full before destroying the old map. 4033 * For pools that weren't in the old map, ->was_full 4034 * should be false. 4035 */ 4036 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) { 4037 struct ceph_pg_pool_info *pi = 4038 rb_entry(n, struct ceph_pg_pool_info, node); 4039 struct ceph_pg_pool_info *old_pi; 4040 4041 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id); 4042 if (old_pi) 4043 pi->was_full = old_pi->was_full; 4044 else 4045 WARN_ON(pi->was_full); 4046 } 4047 4048 if (osdc->osdmap->epoch && 4049 osdc->osdmap->epoch + 1 < newmap->epoch) { 4050 WARN_ON(incremental); 4051 skipped_map = true; 4052 } 4053 4054 ceph_osdmap_destroy(osdc->osdmap); 4055 osdc->osdmap = newmap; 4056 } 4057 4058 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL); 4059 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true, 4060 need_resend, need_resend_linger); 4061 4062 for (n = rb_first(&osdc->osds); n; ) { 4063 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 4064 4065 n = rb_next(n); /* close_osd() */ 4066 4067 scan_requests(osd, skipped_map, was_full, true, need_resend, 4068 need_resend_linger); 4069 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || 4070 memcmp(&osd->o_con.peer_addr, 4071 ceph_osd_addr(osdc->osdmap, osd->o_osd), 4072 sizeof(struct ceph_entity_addr))) 4073 close_osd(osd); 4074 } 4075 4076 return 0; 4077 } 4078 4079 static void kick_requests(struct ceph_osd_client *osdc, 4080 struct rb_root *need_resend, 4081 struct list_head *need_resend_linger) 4082 { 4083 struct ceph_osd_linger_request *lreq, *nlreq; 4084 enum calc_target_result ct_res; 4085 struct rb_node *n; 4086 4087 /* make sure need_resend targets reflect latest map */ 4088 for (n = rb_first(need_resend); n; ) { 4089 struct ceph_osd_request *req = 4090 rb_entry(n, struct ceph_osd_request, r_node); 4091 4092 n = rb_next(n); 4093 4094 if (req->r_t.epoch < osdc->osdmap->epoch) { 4095 ct_res = calc_target(osdc, &req->r_t, false); 4096 if (ct_res == CALC_TARGET_POOL_DNE) { 4097 erase_request(need_resend, req); 4098 check_pool_dne(req); 4099 } 4100 } 4101 } 4102 4103 for (n = rb_first(need_resend); n; ) { 4104 struct ceph_osd_request *req = 4105 rb_entry(n, struct ceph_osd_request, r_node); 4106 struct ceph_osd *osd; 4107 4108 n = rb_next(n); 4109 erase_request(need_resend, req); /* before link_request() */ 4110 4111 osd = lookup_create_osd(osdc, req->r_t.osd, true); 4112 link_request(osd, req); 4113 if (!req->r_linger) { 4114 if (!osd_homeless(osd) && !req->r_t.paused) 4115 send_request(req); 4116 } else { 4117 cancel_linger_request(req); 4118 } 4119 } 4120 4121 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) { 4122 if (!osd_homeless(lreq->osd)) 4123 send_linger(lreq); 4124 4125 list_del_init(&lreq->scan_item); 4126 } 4127 } 4128 4129 /* 4130 * Process updated osd map. 4131 * 4132 * The message contains any number of incremental and full maps, normally 4133 * indicating some sort of topology change in the cluster. Kick requests 4134 * off to different OSDs as needed. 4135 */ 4136 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) 4137 { 4138 void *p = msg->front.iov_base; 4139 void *const end = p + msg->front.iov_len; 4140 u32 nr_maps, maplen; 4141 u32 epoch; 4142 struct ceph_fsid fsid; 4143 struct rb_root need_resend = RB_ROOT; 4144 LIST_HEAD(need_resend_linger); 4145 bool handled_incremental = false; 4146 bool was_pauserd, was_pausewr; 4147 bool pauserd, pausewr; 4148 int err; 4149 4150 dout("%s have %u\n", __func__, osdc->osdmap->epoch); 4151 down_write(&osdc->lock); 4152 4153 /* verify fsid */ 4154 ceph_decode_need(&p, end, sizeof(fsid), bad); 4155 ceph_decode_copy(&p, &fsid, sizeof(fsid)); 4156 if (ceph_check_fsid(osdc->client, &fsid) < 0) 4157 goto bad; 4158 4159 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD); 4160 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) || 4161 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 4162 have_pool_full(osdc); 4163 4164 /* incremental maps */ 4165 ceph_decode_32_safe(&p, end, nr_maps, bad); 4166 dout(" %d inc maps\n", nr_maps); 4167 while (nr_maps > 0) { 4168 ceph_decode_need(&p, end, 2*sizeof(u32), bad); 4169 epoch = ceph_decode_32(&p); 4170 maplen = ceph_decode_32(&p); 4171 ceph_decode_need(&p, end, maplen, bad); 4172 if (osdc->osdmap->epoch && 4173 osdc->osdmap->epoch + 1 == epoch) { 4174 dout("applying incremental map %u len %d\n", 4175 epoch, maplen); 4176 err = handle_one_map(osdc, p, p + maplen, true, 4177 &need_resend, &need_resend_linger); 4178 if (err) 4179 goto bad; 4180 handled_incremental = true; 4181 } else { 4182 dout("ignoring incremental map %u len %d\n", 4183 epoch, maplen); 4184 } 4185 p += maplen; 4186 nr_maps--; 4187 } 4188 if (handled_incremental) 4189 goto done; 4190 4191 /* full maps */ 4192 ceph_decode_32_safe(&p, end, nr_maps, bad); 4193 dout(" %d full maps\n", nr_maps); 4194 while (nr_maps) { 4195 ceph_decode_need(&p, end, 2*sizeof(u32), bad); 4196 epoch = ceph_decode_32(&p); 4197 maplen = ceph_decode_32(&p); 4198 ceph_decode_need(&p, end, maplen, bad); 4199 if (nr_maps > 1) { 4200 dout("skipping non-latest full map %u len %d\n", 4201 epoch, maplen); 4202 } else if (osdc->osdmap->epoch >= epoch) { 4203 dout("skipping full map %u len %d, " 4204 "older than our %u\n", epoch, maplen, 4205 osdc->osdmap->epoch); 4206 } else { 4207 dout("taking full map %u len %d\n", epoch, maplen); 4208 err = handle_one_map(osdc, p, p + maplen, false, 4209 &need_resend, &need_resend_linger); 4210 if (err) 4211 goto bad; 4212 } 4213 p += maplen; 4214 nr_maps--; 4215 } 4216 4217 done: 4218 /* 4219 * subscribe to subsequent osdmap updates if full to ensure 4220 * we find out when we are no longer full and stop returning 4221 * ENOSPC. 4222 */ 4223 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD); 4224 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) || 4225 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 4226 have_pool_full(osdc); 4227 if (was_pauserd || was_pausewr || pauserd || pausewr || 4228 osdc->osdmap->epoch < osdc->epoch_barrier) 4229 maybe_request_map(osdc); 4230 4231 kick_requests(osdc, &need_resend, &need_resend_linger); 4232 4233 ceph_osdc_abort_on_full(osdc); 4234 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP, 4235 osdc->osdmap->epoch); 4236 up_write(&osdc->lock); 4237 wake_up_all(&osdc->client->auth_wq); 4238 return; 4239 4240 bad: 4241 pr_err("osdc handle_map corrupt msg\n"); 4242 ceph_msg_dump(msg); 4243 up_write(&osdc->lock); 4244 } 4245 4246 /* 4247 * Resubmit requests pending on the given osd. 4248 */ 4249 static void kick_osd_requests(struct ceph_osd *osd) 4250 { 4251 struct rb_node *n; 4252 4253 clear_backoffs(osd); 4254 4255 for (n = rb_first(&osd->o_requests); n; ) { 4256 struct ceph_osd_request *req = 4257 rb_entry(n, struct ceph_osd_request, r_node); 4258 4259 n = rb_next(n); /* cancel_linger_request() */ 4260 4261 if (!req->r_linger) { 4262 if (!req->r_t.paused) 4263 send_request(req); 4264 } else { 4265 cancel_linger_request(req); 4266 } 4267 } 4268 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) { 4269 struct ceph_osd_linger_request *lreq = 4270 rb_entry(n, struct ceph_osd_linger_request, node); 4271 4272 send_linger(lreq); 4273 } 4274 } 4275 4276 /* 4277 * If the osd connection drops, we need to resubmit all requests. 4278 */ 4279 static void osd_fault(struct ceph_connection *con) 4280 { 4281 struct ceph_osd *osd = con->private; 4282 struct ceph_osd_client *osdc = osd->o_osdc; 4283 4284 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 4285 4286 down_write(&osdc->lock); 4287 if (!osd_registered(osd)) { 4288 dout("%s osd%d unknown\n", __func__, osd->o_osd); 4289 goto out_unlock; 4290 } 4291 4292 if (!reopen_osd(osd)) 4293 kick_osd_requests(osd); 4294 maybe_request_map(osdc); 4295 4296 out_unlock: 4297 up_write(&osdc->lock); 4298 } 4299 4300 struct MOSDBackoff { 4301 struct ceph_spg spgid; 4302 u32 map_epoch; 4303 u8 op; 4304 u64 id; 4305 struct ceph_hobject_id *begin; 4306 struct ceph_hobject_id *end; 4307 }; 4308 4309 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m) 4310 { 4311 void *p = msg->front.iov_base; 4312 void *const end = p + msg->front.iov_len; 4313 u8 struct_v; 4314 u32 struct_len; 4315 int ret; 4316 4317 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len); 4318 if (ret) 4319 return ret; 4320 4321 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid); 4322 if (ret) 4323 return ret; 4324 4325 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval); 4326 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval); 4327 ceph_decode_8_safe(&p, end, m->op, e_inval); 4328 ceph_decode_64_safe(&p, end, m->id, e_inval); 4329 4330 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO); 4331 if (!m->begin) 4332 return -ENOMEM; 4333 4334 ret = decode_hoid(&p, end, m->begin); 4335 if (ret) { 4336 free_hoid(m->begin); 4337 return ret; 4338 } 4339 4340 m->end = kzalloc(sizeof(*m->end), GFP_NOIO); 4341 if (!m->end) { 4342 free_hoid(m->begin); 4343 return -ENOMEM; 4344 } 4345 4346 ret = decode_hoid(&p, end, m->end); 4347 if (ret) { 4348 free_hoid(m->begin); 4349 free_hoid(m->end); 4350 return ret; 4351 } 4352 4353 return 0; 4354 4355 e_inval: 4356 return -EINVAL; 4357 } 4358 4359 static struct ceph_msg *create_backoff_message( 4360 const struct ceph_osd_backoff *backoff, 4361 u32 map_epoch) 4362 { 4363 struct ceph_msg *msg; 4364 void *p, *end; 4365 int msg_size; 4366 4367 msg_size = CEPH_ENCODING_START_BLK_LEN + 4368 CEPH_PGID_ENCODING_LEN + 1; /* spgid */ 4369 msg_size += 4 + 1 + 8; /* map_epoch, op, id */ 4370 msg_size += CEPH_ENCODING_START_BLK_LEN + 4371 hoid_encoding_size(backoff->begin); 4372 msg_size += CEPH_ENCODING_START_BLK_LEN + 4373 hoid_encoding_size(backoff->end); 4374 4375 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true); 4376 if (!msg) 4377 return NULL; 4378 4379 p = msg->front.iov_base; 4380 end = p + msg->front_alloc_len; 4381 4382 encode_spgid(&p, &backoff->spgid); 4383 ceph_encode_32(&p, map_epoch); 4384 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK); 4385 ceph_encode_64(&p, backoff->id); 4386 encode_hoid(&p, end, backoff->begin); 4387 encode_hoid(&p, end, backoff->end); 4388 BUG_ON(p != end); 4389 4390 msg->front.iov_len = p - msg->front.iov_base; 4391 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */ 4392 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); 4393 4394 return msg; 4395 } 4396 4397 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m) 4398 { 4399 struct ceph_spg_mapping *spg; 4400 struct ceph_osd_backoff *backoff; 4401 struct ceph_msg *msg; 4402 4403 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd, 4404 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id); 4405 4406 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid); 4407 if (!spg) { 4408 spg = alloc_spg_mapping(); 4409 if (!spg) { 4410 pr_err("%s failed to allocate spg\n", __func__); 4411 return; 4412 } 4413 spg->spgid = m->spgid; /* struct */ 4414 insert_spg_mapping(&osd->o_backoff_mappings, spg); 4415 } 4416 4417 backoff = alloc_backoff(); 4418 if (!backoff) { 4419 pr_err("%s failed to allocate backoff\n", __func__); 4420 return; 4421 } 4422 backoff->spgid = m->spgid; /* struct */ 4423 backoff->id = m->id; 4424 backoff->begin = m->begin; 4425 m->begin = NULL; /* backoff now owns this */ 4426 backoff->end = m->end; 4427 m->end = NULL; /* ditto */ 4428 4429 insert_backoff(&spg->backoffs, backoff); 4430 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff); 4431 4432 /* 4433 * Ack with original backoff's epoch so that the OSD can 4434 * discard this if there was a PG split. 4435 */ 4436 msg = create_backoff_message(backoff, m->map_epoch); 4437 if (!msg) { 4438 pr_err("%s failed to allocate msg\n", __func__); 4439 return; 4440 } 4441 ceph_con_send(&osd->o_con, msg); 4442 } 4443 4444 static bool target_contained_by(const struct ceph_osd_request_target *t, 4445 const struct ceph_hobject_id *begin, 4446 const struct ceph_hobject_id *end) 4447 { 4448 struct ceph_hobject_id hoid; 4449 int cmp; 4450 4451 hoid_fill_from_target(&hoid, t); 4452 cmp = hoid_compare(&hoid, begin); 4453 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0); 4454 } 4455 4456 static void handle_backoff_unblock(struct ceph_osd *osd, 4457 const struct MOSDBackoff *m) 4458 { 4459 struct ceph_spg_mapping *spg; 4460 struct ceph_osd_backoff *backoff; 4461 struct rb_node *n; 4462 4463 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd, 4464 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id); 4465 4466 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id); 4467 if (!backoff) { 4468 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n", 4469 __func__, osd->o_osd, m->spgid.pgid.pool, 4470 m->spgid.pgid.seed, m->spgid.shard, m->id); 4471 return; 4472 } 4473 4474 if (hoid_compare(backoff->begin, m->begin) && 4475 hoid_compare(backoff->end, m->end)) { 4476 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n", 4477 __func__, osd->o_osd, m->spgid.pgid.pool, 4478 m->spgid.pgid.seed, m->spgid.shard, m->id); 4479 /* unblock it anyway... */ 4480 } 4481 4482 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid); 4483 BUG_ON(!spg); 4484 4485 erase_backoff(&spg->backoffs, backoff); 4486 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff); 4487 free_backoff(backoff); 4488 4489 if (RB_EMPTY_ROOT(&spg->backoffs)) { 4490 erase_spg_mapping(&osd->o_backoff_mappings, spg); 4491 free_spg_mapping(spg); 4492 } 4493 4494 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) { 4495 struct ceph_osd_request *req = 4496 rb_entry(n, struct ceph_osd_request, r_node); 4497 4498 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) { 4499 /* 4500 * Match against @m, not @backoff -- the PG may 4501 * have split on the OSD. 4502 */ 4503 if (target_contained_by(&req->r_t, m->begin, m->end)) { 4504 /* 4505 * If no other installed backoff applies, 4506 * resend. 4507 */ 4508 send_request(req); 4509 } 4510 } 4511 } 4512 } 4513 4514 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg) 4515 { 4516 struct ceph_osd_client *osdc = osd->o_osdc; 4517 struct MOSDBackoff m; 4518 int ret; 4519 4520 down_read(&osdc->lock); 4521 if (!osd_registered(osd)) { 4522 dout("%s osd%d unknown\n", __func__, osd->o_osd); 4523 up_read(&osdc->lock); 4524 return; 4525 } 4526 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num)); 4527 4528 mutex_lock(&osd->lock); 4529 ret = decode_MOSDBackoff(msg, &m); 4530 if (ret) { 4531 pr_err("failed to decode MOSDBackoff: %d\n", ret); 4532 ceph_msg_dump(msg); 4533 goto out_unlock; 4534 } 4535 4536 switch (m.op) { 4537 case CEPH_OSD_BACKOFF_OP_BLOCK: 4538 handle_backoff_block(osd, &m); 4539 break; 4540 case CEPH_OSD_BACKOFF_OP_UNBLOCK: 4541 handle_backoff_unblock(osd, &m); 4542 break; 4543 default: 4544 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op); 4545 } 4546 4547 free_hoid(m.begin); 4548 free_hoid(m.end); 4549 4550 out_unlock: 4551 mutex_unlock(&osd->lock); 4552 up_read(&osdc->lock); 4553 } 4554 4555 /* 4556 * Process osd watch notifications 4557 */ 4558 static void handle_watch_notify(struct ceph_osd_client *osdc, 4559 struct ceph_msg *msg) 4560 { 4561 void *p = msg->front.iov_base; 4562 void *const end = p + msg->front.iov_len; 4563 struct ceph_osd_linger_request *lreq; 4564 struct linger_work *lwork; 4565 u8 proto_ver, opcode; 4566 u64 cookie, notify_id; 4567 u64 notifier_id = 0; 4568 s32 return_code = 0; 4569 void *payload = NULL; 4570 u32 payload_len = 0; 4571 4572 ceph_decode_8_safe(&p, end, proto_ver, bad); 4573 ceph_decode_8_safe(&p, end, opcode, bad); 4574 ceph_decode_64_safe(&p, end, cookie, bad); 4575 p += 8; /* skip ver */ 4576 ceph_decode_64_safe(&p, end, notify_id, bad); 4577 4578 if (proto_ver >= 1) { 4579 ceph_decode_32_safe(&p, end, payload_len, bad); 4580 ceph_decode_need(&p, end, payload_len, bad); 4581 payload = p; 4582 p += payload_len; 4583 } 4584 4585 if (le16_to_cpu(msg->hdr.version) >= 2) 4586 ceph_decode_32_safe(&p, end, return_code, bad); 4587 4588 if (le16_to_cpu(msg->hdr.version) >= 3) 4589 ceph_decode_64_safe(&p, end, notifier_id, bad); 4590 4591 down_read(&osdc->lock); 4592 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie); 4593 if (!lreq) { 4594 dout("%s opcode %d cookie %llu dne\n", __func__, opcode, 4595 cookie); 4596 goto out_unlock_osdc; 4597 } 4598 4599 mutex_lock(&lreq->lock); 4600 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__, 4601 opcode, cookie, lreq, lreq->is_watch); 4602 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) { 4603 if (!lreq->last_error) { 4604 lreq->last_error = -ENOTCONN; 4605 queue_watch_error(lreq); 4606 } 4607 } else if (!lreq->is_watch) { 4608 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */ 4609 if (lreq->notify_id && lreq->notify_id != notify_id) { 4610 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq, 4611 lreq->notify_id, notify_id); 4612 } else if (!completion_done(&lreq->notify_finish_wait)) { 4613 struct ceph_msg_data *data = 4614 msg->num_data_items ? &msg->data[0] : NULL; 4615 4616 if (data) { 4617 if (lreq->preply_pages) { 4618 WARN_ON(data->type != 4619 CEPH_MSG_DATA_PAGES); 4620 *lreq->preply_pages = data->pages; 4621 *lreq->preply_len = data->length; 4622 data->own_pages = false; 4623 } 4624 } 4625 lreq->notify_finish_error = return_code; 4626 complete_all(&lreq->notify_finish_wait); 4627 } 4628 } else { 4629 /* CEPH_WATCH_EVENT_NOTIFY */ 4630 lwork = lwork_alloc(lreq, do_watch_notify); 4631 if (!lwork) { 4632 pr_err("failed to allocate notify-lwork\n"); 4633 goto out_unlock_lreq; 4634 } 4635 4636 lwork->notify.notify_id = notify_id; 4637 lwork->notify.notifier_id = notifier_id; 4638 lwork->notify.payload = payload; 4639 lwork->notify.payload_len = payload_len; 4640 lwork->notify.msg = ceph_msg_get(msg); 4641 lwork_queue(lwork); 4642 } 4643 4644 out_unlock_lreq: 4645 mutex_unlock(&lreq->lock); 4646 out_unlock_osdc: 4647 up_read(&osdc->lock); 4648 return; 4649 4650 bad: 4651 pr_err("osdc handle_watch_notify corrupt msg\n"); 4652 } 4653 4654 /* 4655 * Register request, send initial attempt. 4656 */ 4657 void ceph_osdc_start_request(struct ceph_osd_client *osdc, 4658 struct ceph_osd_request *req) 4659 { 4660 down_read(&osdc->lock); 4661 submit_request(req, false); 4662 up_read(&osdc->lock); 4663 } 4664 EXPORT_SYMBOL(ceph_osdc_start_request); 4665 4666 /* 4667 * Unregister request. If @req was registered, it isn't completed: 4668 * r_result isn't set and __complete_request() isn't invoked. 4669 * 4670 * If @req wasn't registered, this call may have raced with 4671 * handle_reply(), in which case r_result would already be set and 4672 * __complete_request() would be getting invoked, possibly even 4673 * concurrently with this call. 4674 */ 4675 void ceph_osdc_cancel_request(struct ceph_osd_request *req) 4676 { 4677 struct ceph_osd_client *osdc = req->r_osdc; 4678 4679 down_write(&osdc->lock); 4680 if (req->r_osd) 4681 cancel_request(req); 4682 up_write(&osdc->lock); 4683 } 4684 EXPORT_SYMBOL(ceph_osdc_cancel_request); 4685 4686 /* 4687 * @timeout: in jiffies, 0 means "wait forever" 4688 */ 4689 static int wait_request_timeout(struct ceph_osd_request *req, 4690 unsigned long timeout) 4691 { 4692 long left; 4693 4694 dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 4695 left = wait_for_completion_killable_timeout(&req->r_completion, 4696 ceph_timeout_jiffies(timeout)); 4697 if (left <= 0) { 4698 left = left ?: -ETIMEDOUT; 4699 ceph_osdc_cancel_request(req); 4700 } else { 4701 left = req->r_result; /* completed */ 4702 } 4703 4704 return left; 4705 } 4706 4707 /* 4708 * wait for a request to complete 4709 */ 4710 int ceph_osdc_wait_request(struct ceph_osd_client *osdc, 4711 struct ceph_osd_request *req) 4712 { 4713 return wait_request_timeout(req, 0); 4714 } 4715 EXPORT_SYMBOL(ceph_osdc_wait_request); 4716 4717 /* 4718 * sync - wait for all in-flight requests to flush. avoid starvation. 4719 */ 4720 void ceph_osdc_sync(struct ceph_osd_client *osdc) 4721 { 4722 struct rb_node *n, *p; 4723 u64 last_tid = atomic64_read(&osdc->last_tid); 4724 4725 again: 4726 down_read(&osdc->lock); 4727 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) { 4728 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 4729 4730 mutex_lock(&osd->lock); 4731 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) { 4732 struct ceph_osd_request *req = 4733 rb_entry(p, struct ceph_osd_request, r_node); 4734 4735 if (req->r_tid > last_tid) 4736 break; 4737 4738 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE)) 4739 continue; 4740 4741 ceph_osdc_get_request(req); 4742 mutex_unlock(&osd->lock); 4743 up_read(&osdc->lock); 4744 dout("%s waiting on req %p tid %llu last_tid %llu\n", 4745 __func__, req, req->r_tid, last_tid); 4746 wait_for_completion(&req->r_completion); 4747 ceph_osdc_put_request(req); 4748 goto again; 4749 } 4750 4751 mutex_unlock(&osd->lock); 4752 } 4753 4754 up_read(&osdc->lock); 4755 dout("%s done last_tid %llu\n", __func__, last_tid); 4756 } 4757 EXPORT_SYMBOL(ceph_osdc_sync); 4758 4759 /* 4760 * Returns a handle, caller owns a ref. 4761 */ 4762 struct ceph_osd_linger_request * 4763 ceph_osdc_watch(struct ceph_osd_client *osdc, 4764 struct ceph_object_id *oid, 4765 struct ceph_object_locator *oloc, 4766 rados_watchcb2_t wcb, 4767 rados_watcherrcb_t errcb, 4768 void *data) 4769 { 4770 struct ceph_osd_linger_request *lreq; 4771 int ret; 4772 4773 lreq = linger_alloc(osdc); 4774 if (!lreq) 4775 return ERR_PTR(-ENOMEM); 4776 4777 lreq->is_watch = true; 4778 lreq->wcb = wcb; 4779 lreq->errcb = errcb; 4780 lreq->data = data; 4781 lreq->watch_valid_thru = jiffies; 4782 4783 ceph_oid_copy(&lreq->t.base_oid, oid); 4784 ceph_oloc_copy(&lreq->t.base_oloc, oloc); 4785 lreq->t.flags = CEPH_OSD_FLAG_WRITE; 4786 ktime_get_real_ts64(&lreq->mtime); 4787 4788 linger_submit(lreq); 4789 ret = linger_reg_commit_wait(lreq); 4790 if (ret) { 4791 linger_cancel(lreq); 4792 goto err_put_lreq; 4793 } 4794 4795 return lreq; 4796 4797 err_put_lreq: 4798 linger_put(lreq); 4799 return ERR_PTR(ret); 4800 } 4801 EXPORT_SYMBOL(ceph_osdc_watch); 4802 4803 /* 4804 * Releases a ref. 4805 * 4806 * Times out after mount_timeout to preserve rbd unmap behaviour 4807 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap 4808 * with mount_timeout"). 4809 */ 4810 int ceph_osdc_unwatch(struct ceph_osd_client *osdc, 4811 struct ceph_osd_linger_request *lreq) 4812 { 4813 struct ceph_options *opts = osdc->client->options; 4814 struct ceph_osd_request *req; 4815 int ret; 4816 4817 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 4818 if (!req) 4819 return -ENOMEM; 4820 4821 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid); 4822 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc); 4823 req->r_flags = CEPH_OSD_FLAG_WRITE; 4824 ktime_get_real_ts64(&req->r_mtime); 4825 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH, 4826 lreq->linger_id, 0); 4827 4828 ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 4829 if (ret) 4830 goto out_put_req; 4831 4832 ceph_osdc_start_request(osdc, req); 4833 linger_cancel(lreq); 4834 linger_put(lreq); 4835 ret = wait_request_timeout(req, opts->mount_timeout); 4836 4837 out_put_req: 4838 ceph_osdc_put_request(req); 4839 return ret; 4840 } 4841 EXPORT_SYMBOL(ceph_osdc_unwatch); 4842 4843 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which, 4844 u64 notify_id, u64 cookie, void *payload, 4845 u32 payload_len) 4846 { 4847 struct ceph_osd_req_op *op; 4848 struct ceph_pagelist *pl; 4849 int ret; 4850 4851 op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0); 4852 4853 pl = ceph_pagelist_alloc(GFP_NOIO); 4854 if (!pl) 4855 return -ENOMEM; 4856 4857 ret = ceph_pagelist_encode_64(pl, notify_id); 4858 ret |= ceph_pagelist_encode_64(pl, cookie); 4859 if (payload) { 4860 ret |= ceph_pagelist_encode_32(pl, payload_len); 4861 ret |= ceph_pagelist_append(pl, payload, payload_len); 4862 } else { 4863 ret |= ceph_pagelist_encode_32(pl, 0); 4864 } 4865 if (ret) { 4866 ceph_pagelist_release(pl); 4867 return -ENOMEM; 4868 } 4869 4870 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl); 4871 op->indata_len = pl->length; 4872 return 0; 4873 } 4874 4875 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc, 4876 struct ceph_object_id *oid, 4877 struct ceph_object_locator *oloc, 4878 u64 notify_id, 4879 u64 cookie, 4880 void *payload, 4881 u32 payload_len) 4882 { 4883 struct ceph_osd_request *req; 4884 int ret; 4885 4886 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 4887 if (!req) 4888 return -ENOMEM; 4889 4890 ceph_oid_copy(&req->r_base_oid, oid); 4891 ceph_oloc_copy(&req->r_base_oloc, oloc); 4892 req->r_flags = CEPH_OSD_FLAG_READ; 4893 4894 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload, 4895 payload_len); 4896 if (ret) 4897 goto out_put_req; 4898 4899 ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 4900 if (ret) 4901 goto out_put_req; 4902 4903 ceph_osdc_start_request(osdc, req); 4904 ret = ceph_osdc_wait_request(osdc, req); 4905 4906 out_put_req: 4907 ceph_osdc_put_request(req); 4908 return ret; 4909 } 4910 EXPORT_SYMBOL(ceph_osdc_notify_ack); 4911 4912 /* 4913 * @timeout: in seconds 4914 * 4915 * @preply_{pages,len} are initialized both on success and error. 4916 * The caller is responsible for: 4917 * 4918 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)) 4919 */ 4920 int ceph_osdc_notify(struct ceph_osd_client *osdc, 4921 struct ceph_object_id *oid, 4922 struct ceph_object_locator *oloc, 4923 void *payload, 4924 u32 payload_len, 4925 u32 timeout, 4926 struct page ***preply_pages, 4927 size_t *preply_len) 4928 { 4929 struct ceph_osd_linger_request *lreq; 4930 int ret; 4931 4932 WARN_ON(!timeout); 4933 if (preply_pages) { 4934 *preply_pages = NULL; 4935 *preply_len = 0; 4936 } 4937 4938 lreq = linger_alloc(osdc); 4939 if (!lreq) 4940 return -ENOMEM; 4941 4942 lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO); 4943 if (!lreq->request_pl) { 4944 ret = -ENOMEM; 4945 goto out_put_lreq; 4946 } 4947 4948 ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */ 4949 ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout); 4950 ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len); 4951 ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len); 4952 if (ret) { 4953 ret = -ENOMEM; 4954 goto out_put_lreq; 4955 } 4956 4957 /* for notify_id */ 4958 lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO); 4959 if (IS_ERR(lreq->notify_id_pages)) { 4960 ret = PTR_ERR(lreq->notify_id_pages); 4961 lreq->notify_id_pages = NULL; 4962 goto out_put_lreq; 4963 } 4964 4965 lreq->preply_pages = preply_pages; 4966 lreq->preply_len = preply_len; 4967 4968 ceph_oid_copy(&lreq->t.base_oid, oid); 4969 ceph_oloc_copy(&lreq->t.base_oloc, oloc); 4970 lreq->t.flags = CEPH_OSD_FLAG_READ; 4971 4972 linger_submit(lreq); 4973 ret = linger_reg_commit_wait(lreq); 4974 if (!ret) 4975 ret = linger_notify_finish_wait(lreq, 4976 msecs_to_jiffies(2 * timeout * MSEC_PER_SEC)); 4977 else 4978 dout("lreq %p failed to initiate notify %d\n", lreq, ret); 4979 4980 linger_cancel(lreq); 4981 out_put_lreq: 4982 linger_put(lreq); 4983 return ret; 4984 } 4985 EXPORT_SYMBOL(ceph_osdc_notify); 4986 4987 /* 4988 * Return the number of milliseconds since the watch was last 4989 * confirmed, or an error. If there is an error, the watch is no 4990 * longer valid, and should be destroyed with ceph_osdc_unwatch(). 4991 */ 4992 int ceph_osdc_watch_check(struct ceph_osd_client *osdc, 4993 struct ceph_osd_linger_request *lreq) 4994 { 4995 unsigned long stamp, age; 4996 int ret; 4997 4998 down_read(&osdc->lock); 4999 mutex_lock(&lreq->lock); 5000 stamp = lreq->watch_valid_thru; 5001 if (!list_empty(&lreq->pending_lworks)) { 5002 struct linger_work *lwork = 5003 list_first_entry(&lreq->pending_lworks, 5004 struct linger_work, 5005 pending_item); 5006 5007 if (time_before(lwork->queued_stamp, stamp)) 5008 stamp = lwork->queued_stamp; 5009 } 5010 age = jiffies - stamp; 5011 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__, 5012 lreq, lreq->linger_id, age, lreq->last_error); 5013 /* we are truncating to msecs, so return a safe upper bound */ 5014 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age); 5015 5016 mutex_unlock(&lreq->lock); 5017 up_read(&osdc->lock); 5018 return ret; 5019 } 5020 5021 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item) 5022 { 5023 u8 struct_v; 5024 u32 struct_len; 5025 int ret; 5026 5027 ret = ceph_start_decoding(p, end, 2, "watch_item_t", 5028 &struct_v, &struct_len); 5029 if (ret) 5030 goto bad; 5031 5032 ret = -EINVAL; 5033 ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad); 5034 ceph_decode_64_safe(p, end, item->cookie, bad); 5035 ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */ 5036 5037 if (struct_v >= 2) { 5038 ret = ceph_decode_entity_addr(p, end, &item->addr); 5039 if (ret) 5040 goto bad; 5041 } else { 5042 ret = 0; 5043 } 5044 5045 dout("%s %s%llu cookie %llu addr %s\n", __func__, 5046 ENTITY_NAME(item->name), item->cookie, 5047 ceph_pr_addr(&item->addr)); 5048 bad: 5049 return ret; 5050 } 5051 5052 static int decode_watchers(void **p, void *end, 5053 struct ceph_watch_item **watchers, 5054 u32 *num_watchers) 5055 { 5056 u8 struct_v; 5057 u32 struct_len; 5058 int i; 5059 int ret; 5060 5061 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t", 5062 &struct_v, &struct_len); 5063 if (ret) 5064 return ret; 5065 5066 *num_watchers = ceph_decode_32(p); 5067 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO); 5068 if (!*watchers) 5069 return -ENOMEM; 5070 5071 for (i = 0; i < *num_watchers; i++) { 5072 ret = decode_watcher(p, end, *watchers + i); 5073 if (ret) { 5074 kfree(*watchers); 5075 return ret; 5076 } 5077 } 5078 5079 return 0; 5080 } 5081 5082 /* 5083 * On success, the caller is responsible for: 5084 * 5085 * kfree(watchers); 5086 */ 5087 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc, 5088 struct ceph_object_id *oid, 5089 struct ceph_object_locator *oloc, 5090 struct ceph_watch_item **watchers, 5091 u32 *num_watchers) 5092 { 5093 struct ceph_osd_request *req; 5094 struct page **pages; 5095 int ret; 5096 5097 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 5098 if (!req) 5099 return -ENOMEM; 5100 5101 ceph_oid_copy(&req->r_base_oid, oid); 5102 ceph_oloc_copy(&req->r_base_oloc, oloc); 5103 req->r_flags = CEPH_OSD_FLAG_READ; 5104 5105 pages = ceph_alloc_page_vector(1, GFP_NOIO); 5106 if (IS_ERR(pages)) { 5107 ret = PTR_ERR(pages); 5108 goto out_put_req; 5109 } 5110 5111 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0); 5112 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers, 5113 response_data), 5114 pages, PAGE_SIZE, 0, false, true); 5115 5116 ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 5117 if (ret) 5118 goto out_put_req; 5119 5120 ceph_osdc_start_request(osdc, req); 5121 ret = ceph_osdc_wait_request(osdc, req); 5122 if (ret >= 0) { 5123 void *p = page_address(pages[0]); 5124 void *const end = p + req->r_ops[0].outdata_len; 5125 5126 ret = decode_watchers(&p, end, watchers, num_watchers); 5127 } 5128 5129 out_put_req: 5130 ceph_osdc_put_request(req); 5131 return ret; 5132 } 5133 EXPORT_SYMBOL(ceph_osdc_list_watchers); 5134 5135 /* 5136 * Call all pending notify callbacks - for use after a watch is 5137 * unregistered, to make sure no more callbacks for it will be invoked 5138 */ 5139 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc) 5140 { 5141 dout("%s osdc %p\n", __func__, osdc); 5142 flush_workqueue(osdc->notify_wq); 5143 } 5144 EXPORT_SYMBOL(ceph_osdc_flush_notifies); 5145 5146 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc) 5147 { 5148 down_read(&osdc->lock); 5149 maybe_request_map(osdc); 5150 up_read(&osdc->lock); 5151 } 5152 EXPORT_SYMBOL(ceph_osdc_maybe_request_map); 5153 5154 /* 5155 * Execute an OSD class method on an object. 5156 * 5157 * @flags: CEPH_OSD_FLAG_* 5158 * @resp_len: in/out param for reply length 5159 */ 5160 int ceph_osdc_call(struct ceph_osd_client *osdc, 5161 struct ceph_object_id *oid, 5162 struct ceph_object_locator *oloc, 5163 const char *class, const char *method, 5164 unsigned int flags, 5165 struct page *req_page, size_t req_len, 5166 struct page **resp_pages, size_t *resp_len) 5167 { 5168 struct ceph_osd_request *req; 5169 int ret; 5170 5171 if (req_len > PAGE_SIZE) 5172 return -E2BIG; 5173 5174 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 5175 if (!req) 5176 return -ENOMEM; 5177 5178 ceph_oid_copy(&req->r_base_oid, oid); 5179 ceph_oloc_copy(&req->r_base_oloc, oloc); 5180 req->r_flags = flags; 5181 5182 ret = osd_req_op_cls_init(req, 0, class, method); 5183 if (ret) 5184 goto out_put_req; 5185 5186 if (req_page) 5187 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len, 5188 0, false, false); 5189 if (resp_pages) 5190 osd_req_op_cls_response_data_pages(req, 0, resp_pages, 5191 *resp_len, 0, false, false); 5192 5193 ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 5194 if (ret) 5195 goto out_put_req; 5196 5197 ceph_osdc_start_request(osdc, req); 5198 ret = ceph_osdc_wait_request(osdc, req); 5199 if (ret >= 0) { 5200 ret = req->r_ops[0].rval; 5201 if (resp_pages) 5202 *resp_len = req->r_ops[0].outdata_len; 5203 } 5204 5205 out_put_req: 5206 ceph_osdc_put_request(req); 5207 return ret; 5208 } 5209 EXPORT_SYMBOL(ceph_osdc_call); 5210 5211 /* 5212 * reset all osd connections 5213 */ 5214 void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc) 5215 { 5216 struct rb_node *n; 5217 5218 down_write(&osdc->lock); 5219 for (n = rb_first(&osdc->osds); n; ) { 5220 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 5221 5222 n = rb_next(n); 5223 if (!reopen_osd(osd)) 5224 kick_osd_requests(osd); 5225 } 5226 up_write(&osdc->lock); 5227 } 5228 5229 /* 5230 * init, shutdown 5231 */ 5232 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) 5233 { 5234 int err; 5235 5236 dout("init\n"); 5237 osdc->client = client; 5238 init_rwsem(&osdc->lock); 5239 osdc->osds = RB_ROOT; 5240 INIT_LIST_HEAD(&osdc->osd_lru); 5241 spin_lock_init(&osdc->osd_lru_lock); 5242 osd_init(&osdc->homeless_osd); 5243 osdc->homeless_osd.o_osdc = osdc; 5244 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD; 5245 osdc->last_linger_id = CEPH_LINGER_ID_START; 5246 osdc->linger_requests = RB_ROOT; 5247 osdc->map_checks = RB_ROOT; 5248 osdc->linger_map_checks = RB_ROOT; 5249 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); 5250 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); 5251 5252 err = -ENOMEM; 5253 osdc->osdmap = ceph_osdmap_alloc(); 5254 if (!osdc->osdmap) 5255 goto out; 5256 5257 osdc->req_mempool = mempool_create_slab_pool(10, 5258 ceph_osd_request_cache); 5259 if (!osdc->req_mempool) 5260 goto out_map; 5261 5262 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP, 5263 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op"); 5264 if (err < 0) 5265 goto out_mempool; 5266 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY, 5267 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, 5268 "osd_op_reply"); 5269 if (err < 0) 5270 goto out_msgpool; 5271 5272 err = -ENOMEM; 5273 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify"); 5274 if (!osdc->notify_wq) 5275 goto out_msgpool_reply; 5276 5277 osdc->completion_wq = create_singlethread_workqueue("ceph-completion"); 5278 if (!osdc->completion_wq) 5279 goto out_notify_wq; 5280 5281 schedule_delayed_work(&osdc->timeout_work, 5282 osdc->client->options->osd_keepalive_timeout); 5283 schedule_delayed_work(&osdc->osds_timeout_work, 5284 round_jiffies_relative(osdc->client->options->osd_idle_ttl)); 5285 5286 return 0; 5287 5288 out_notify_wq: 5289 destroy_workqueue(osdc->notify_wq); 5290 out_msgpool_reply: 5291 ceph_msgpool_destroy(&osdc->msgpool_op_reply); 5292 out_msgpool: 5293 ceph_msgpool_destroy(&osdc->msgpool_op); 5294 out_mempool: 5295 mempool_destroy(osdc->req_mempool); 5296 out_map: 5297 ceph_osdmap_destroy(osdc->osdmap); 5298 out: 5299 return err; 5300 } 5301 5302 void ceph_osdc_stop(struct ceph_osd_client *osdc) 5303 { 5304 destroy_workqueue(osdc->completion_wq); 5305 destroy_workqueue(osdc->notify_wq); 5306 cancel_delayed_work_sync(&osdc->timeout_work); 5307 cancel_delayed_work_sync(&osdc->osds_timeout_work); 5308 5309 down_write(&osdc->lock); 5310 while (!RB_EMPTY_ROOT(&osdc->osds)) { 5311 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds), 5312 struct ceph_osd, o_node); 5313 close_osd(osd); 5314 } 5315 up_write(&osdc->lock); 5316 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1); 5317 osd_cleanup(&osdc->homeless_osd); 5318 5319 WARN_ON(!list_empty(&osdc->osd_lru)); 5320 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests)); 5321 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks)); 5322 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks)); 5323 WARN_ON(atomic_read(&osdc->num_requests)); 5324 WARN_ON(atomic_read(&osdc->num_homeless)); 5325 5326 ceph_osdmap_destroy(osdc->osdmap); 5327 mempool_destroy(osdc->req_mempool); 5328 ceph_msgpool_destroy(&osdc->msgpool_op); 5329 ceph_msgpool_destroy(&osdc->msgpool_op_reply); 5330 } 5331 5332 int osd_req_op_copy_from_init(struct ceph_osd_request *req, 5333 u64 src_snapid, u64 src_version, 5334 struct ceph_object_id *src_oid, 5335 struct ceph_object_locator *src_oloc, 5336 u32 src_fadvise_flags, 5337 u32 dst_fadvise_flags, 5338 u32 truncate_seq, u64 truncate_size, 5339 u8 copy_from_flags) 5340 { 5341 struct ceph_osd_req_op *op; 5342 struct page **pages; 5343 void *p, *end; 5344 5345 pages = ceph_alloc_page_vector(1, GFP_KERNEL); 5346 if (IS_ERR(pages)) 5347 return PTR_ERR(pages); 5348 5349 op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2, 5350 dst_fadvise_flags); 5351 op->copy_from.snapid = src_snapid; 5352 op->copy_from.src_version = src_version; 5353 op->copy_from.flags = copy_from_flags; 5354 op->copy_from.src_fadvise_flags = src_fadvise_flags; 5355 5356 p = page_address(pages[0]); 5357 end = p + PAGE_SIZE; 5358 ceph_encode_string(&p, end, src_oid->name, src_oid->name_len); 5359 encode_oloc(&p, end, src_oloc); 5360 ceph_encode_32(&p, truncate_seq); 5361 ceph_encode_64(&p, truncate_size); 5362 op->indata_len = PAGE_SIZE - (end - p); 5363 5364 ceph_osd_data_pages_init(&op->copy_from.osd_data, pages, 5365 op->indata_len, 0, false, true); 5366 return 0; 5367 } 5368 EXPORT_SYMBOL(osd_req_op_copy_from_init); 5369 5370 int __init ceph_osdc_setup(void) 5371 { 5372 size_t size = sizeof(struct ceph_osd_request) + 5373 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op); 5374 5375 BUG_ON(ceph_osd_request_cache); 5376 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size, 5377 0, 0, NULL); 5378 5379 return ceph_osd_request_cache ? 0 : -ENOMEM; 5380 } 5381 5382 void ceph_osdc_cleanup(void) 5383 { 5384 BUG_ON(!ceph_osd_request_cache); 5385 kmem_cache_destroy(ceph_osd_request_cache); 5386 ceph_osd_request_cache = NULL; 5387 } 5388 5389 /* 5390 * handle incoming message 5391 */ 5392 static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg) 5393 { 5394 struct ceph_osd *osd = con->private; 5395 struct ceph_osd_client *osdc = osd->o_osdc; 5396 int type = le16_to_cpu(msg->hdr.type); 5397 5398 switch (type) { 5399 case CEPH_MSG_OSD_MAP: 5400 ceph_osdc_handle_map(osdc, msg); 5401 break; 5402 case CEPH_MSG_OSD_OPREPLY: 5403 handle_reply(osd, msg); 5404 break; 5405 case CEPH_MSG_OSD_BACKOFF: 5406 handle_backoff(osd, msg); 5407 break; 5408 case CEPH_MSG_WATCH_NOTIFY: 5409 handle_watch_notify(osdc, msg); 5410 break; 5411 5412 default: 5413 pr_err("received unknown message type %d %s\n", type, 5414 ceph_msg_type_name(type)); 5415 } 5416 5417 ceph_msg_put(msg); 5418 } 5419 5420 /* How much sparse data was requested? */ 5421 static u64 sparse_data_requested(struct ceph_osd_request *req) 5422 { 5423 u64 len = 0; 5424 5425 if (req->r_flags & CEPH_OSD_FLAG_READ) { 5426 int i; 5427 5428 for (i = 0; i < req->r_num_ops; ++i) { 5429 struct ceph_osd_req_op *op = &req->r_ops[i]; 5430 5431 if (op->op == CEPH_OSD_OP_SPARSE_READ) 5432 len += op->extent.length; 5433 } 5434 } 5435 return len; 5436 } 5437 5438 /* 5439 * Lookup and return message for incoming reply. Don't try to do 5440 * anything about a larger than preallocated data portion of the 5441 * message at the moment - for now, just skip the message. 5442 */ 5443 static struct ceph_msg *get_reply(struct ceph_connection *con, 5444 struct ceph_msg_header *hdr, 5445 int *skip) 5446 { 5447 struct ceph_osd *osd = con->private; 5448 struct ceph_osd_client *osdc = osd->o_osdc; 5449 struct ceph_msg *m = NULL; 5450 struct ceph_osd_request *req; 5451 int front_len = le32_to_cpu(hdr->front_len); 5452 int data_len = le32_to_cpu(hdr->data_len); 5453 u64 tid = le64_to_cpu(hdr->tid); 5454 u64 srlen; 5455 5456 down_read(&osdc->lock); 5457 if (!osd_registered(osd)) { 5458 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd); 5459 *skip = 1; 5460 goto out_unlock_osdc; 5461 } 5462 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num)); 5463 5464 mutex_lock(&osd->lock); 5465 req = lookup_request(&osd->o_requests, tid); 5466 if (!req) { 5467 dout("%s osd%d tid %llu unknown, skipping\n", __func__, 5468 osd->o_osd, tid); 5469 *skip = 1; 5470 goto out_unlock_session; 5471 } 5472 5473 ceph_msg_revoke_incoming(req->r_reply); 5474 5475 if (front_len > req->r_reply->front_alloc_len) { 5476 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n", 5477 __func__, osd->o_osd, req->r_tid, front_len, 5478 req->r_reply->front_alloc_len); 5479 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS, 5480 false); 5481 if (!m) 5482 goto out_unlock_session; 5483 ceph_msg_put(req->r_reply); 5484 req->r_reply = m; 5485 } 5486 5487 srlen = sparse_data_requested(req); 5488 if (!srlen && data_len > req->r_reply->data_length) { 5489 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n", 5490 __func__, osd->o_osd, req->r_tid, data_len, 5491 req->r_reply->data_length); 5492 m = NULL; 5493 *skip = 1; 5494 goto out_unlock_session; 5495 } 5496 5497 m = ceph_msg_get(req->r_reply); 5498 m->sparse_read = (bool)srlen; 5499 5500 dout("get_reply tid %lld %p\n", tid, m); 5501 5502 out_unlock_session: 5503 mutex_unlock(&osd->lock); 5504 out_unlock_osdc: 5505 up_read(&osdc->lock); 5506 return m; 5507 } 5508 5509 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr) 5510 { 5511 struct ceph_msg *m; 5512 int type = le16_to_cpu(hdr->type); 5513 u32 front_len = le32_to_cpu(hdr->front_len); 5514 u32 data_len = le32_to_cpu(hdr->data_len); 5515 5516 m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false); 5517 if (!m) 5518 return NULL; 5519 5520 if (data_len) { 5521 struct page **pages; 5522 5523 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len), 5524 GFP_NOIO); 5525 if (IS_ERR(pages)) { 5526 ceph_msg_put(m); 5527 return NULL; 5528 } 5529 5530 ceph_msg_data_add_pages(m, pages, data_len, 0, true); 5531 } 5532 5533 return m; 5534 } 5535 5536 static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con, 5537 struct ceph_msg_header *hdr, 5538 int *skip) 5539 { 5540 struct ceph_osd *osd = con->private; 5541 int type = le16_to_cpu(hdr->type); 5542 5543 *skip = 0; 5544 switch (type) { 5545 case CEPH_MSG_OSD_MAP: 5546 case CEPH_MSG_OSD_BACKOFF: 5547 case CEPH_MSG_WATCH_NOTIFY: 5548 return alloc_msg_with_page_vector(hdr); 5549 case CEPH_MSG_OSD_OPREPLY: 5550 return get_reply(con, hdr, skip); 5551 default: 5552 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__, 5553 osd->o_osd, type); 5554 *skip = 1; 5555 return NULL; 5556 } 5557 } 5558 5559 /* 5560 * Wrappers to refcount containing ceph_osd struct 5561 */ 5562 static struct ceph_connection *osd_get_con(struct ceph_connection *con) 5563 { 5564 struct ceph_osd *osd = con->private; 5565 if (get_osd(osd)) 5566 return con; 5567 return NULL; 5568 } 5569 5570 static void osd_put_con(struct ceph_connection *con) 5571 { 5572 struct ceph_osd *osd = con->private; 5573 put_osd(osd); 5574 } 5575 5576 /* 5577 * authentication 5578 */ 5579 5580 /* 5581 * Note: returned pointer is the address of a structure that's 5582 * managed separately. Caller must *not* attempt to free it. 5583 */ 5584 static struct ceph_auth_handshake * 5585 osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new) 5586 { 5587 struct ceph_osd *o = con->private; 5588 struct ceph_osd_client *osdc = o->o_osdc; 5589 struct ceph_auth_client *ac = osdc->client->monc.auth; 5590 struct ceph_auth_handshake *auth = &o->o_auth; 5591 int ret; 5592 5593 ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD, 5594 force_new, proto, NULL, NULL); 5595 if (ret) 5596 return ERR_PTR(ret); 5597 5598 return auth; 5599 } 5600 5601 static int osd_add_authorizer_challenge(struct ceph_connection *con, 5602 void *challenge_buf, int challenge_buf_len) 5603 { 5604 struct ceph_osd *o = con->private; 5605 struct ceph_osd_client *osdc = o->o_osdc; 5606 struct ceph_auth_client *ac = osdc->client->monc.auth; 5607 5608 return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer, 5609 challenge_buf, challenge_buf_len); 5610 } 5611 5612 static int osd_verify_authorizer_reply(struct ceph_connection *con) 5613 { 5614 struct ceph_osd *o = con->private; 5615 struct ceph_osd_client *osdc = o->o_osdc; 5616 struct ceph_auth_client *ac = osdc->client->monc.auth; 5617 struct ceph_auth_handshake *auth = &o->o_auth; 5618 5619 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer, 5620 auth->authorizer_reply_buf, auth->authorizer_reply_buf_len, 5621 NULL, NULL, NULL, NULL); 5622 } 5623 5624 static int osd_invalidate_authorizer(struct ceph_connection *con) 5625 { 5626 struct ceph_osd *o = con->private; 5627 struct ceph_osd_client *osdc = o->o_osdc; 5628 struct ceph_auth_client *ac = osdc->client->monc.auth; 5629 5630 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); 5631 return ceph_monc_validate_auth(&osdc->client->monc); 5632 } 5633 5634 static int osd_get_auth_request(struct ceph_connection *con, 5635 void *buf, int *buf_len, 5636 void **authorizer, int *authorizer_len) 5637 { 5638 struct ceph_osd *o = con->private; 5639 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth; 5640 struct ceph_auth_handshake *auth = &o->o_auth; 5641 int ret; 5642 5643 ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD, 5644 buf, buf_len); 5645 if (ret) 5646 return ret; 5647 5648 *authorizer = auth->authorizer_buf; 5649 *authorizer_len = auth->authorizer_buf_len; 5650 return 0; 5651 } 5652 5653 static int osd_handle_auth_reply_more(struct ceph_connection *con, 5654 void *reply, int reply_len, 5655 void *buf, int *buf_len, 5656 void **authorizer, int *authorizer_len) 5657 { 5658 struct ceph_osd *o = con->private; 5659 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth; 5660 struct ceph_auth_handshake *auth = &o->o_auth; 5661 int ret; 5662 5663 ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len, 5664 buf, buf_len); 5665 if (ret) 5666 return ret; 5667 5668 *authorizer = auth->authorizer_buf; 5669 *authorizer_len = auth->authorizer_buf_len; 5670 return 0; 5671 } 5672 5673 static int osd_handle_auth_done(struct ceph_connection *con, 5674 u64 global_id, void *reply, int reply_len, 5675 u8 *session_key, int *session_key_len, 5676 u8 *con_secret, int *con_secret_len) 5677 { 5678 struct ceph_osd *o = con->private; 5679 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth; 5680 struct ceph_auth_handshake *auth = &o->o_auth; 5681 5682 return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len, 5683 session_key, session_key_len, 5684 con_secret, con_secret_len); 5685 } 5686 5687 static int osd_handle_auth_bad_method(struct ceph_connection *con, 5688 int used_proto, int result, 5689 const int *allowed_protos, int proto_cnt, 5690 const int *allowed_modes, int mode_cnt) 5691 { 5692 struct ceph_osd *o = con->private; 5693 struct ceph_mon_client *monc = &o->o_osdc->client->monc; 5694 int ret; 5695 5696 if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD, 5697 used_proto, result, 5698 allowed_protos, proto_cnt, 5699 allowed_modes, mode_cnt)) { 5700 ret = ceph_monc_validate_auth(monc); 5701 if (ret) 5702 return ret; 5703 } 5704 5705 return -EACCES; 5706 } 5707 5708 static void osd_reencode_message(struct ceph_msg *msg) 5709 { 5710 int type = le16_to_cpu(msg->hdr.type); 5711 5712 if (type == CEPH_MSG_OSD_OP) 5713 encode_request_finish(msg); 5714 } 5715 5716 static int osd_sign_message(struct ceph_msg *msg) 5717 { 5718 struct ceph_osd *o = msg->con->private; 5719 struct ceph_auth_handshake *auth = &o->o_auth; 5720 5721 return ceph_auth_sign_message(auth, msg); 5722 } 5723 5724 static int osd_check_message_signature(struct ceph_msg *msg) 5725 { 5726 struct ceph_osd *o = msg->con->private; 5727 struct ceph_auth_handshake *auth = &o->o_auth; 5728 5729 return ceph_auth_check_message_signature(auth, msg); 5730 } 5731 5732 static void advance_cursor(struct ceph_msg_data_cursor *cursor, size_t len, 5733 bool zero) 5734 { 5735 while (len) { 5736 struct page *page; 5737 size_t poff, plen; 5738 5739 page = ceph_msg_data_next(cursor, &poff, &plen); 5740 if (plen > len) 5741 plen = len; 5742 if (zero) 5743 zero_user_segment(page, poff, poff + plen); 5744 len -= plen; 5745 ceph_msg_data_advance(cursor, plen); 5746 } 5747 } 5748 5749 static int prep_next_sparse_read(struct ceph_connection *con, 5750 struct ceph_msg_data_cursor *cursor) 5751 { 5752 struct ceph_osd *o = con->private; 5753 struct ceph_sparse_read *sr = &o->o_sparse_read; 5754 struct ceph_osd_request *req; 5755 struct ceph_osd_req_op *op; 5756 5757 spin_lock(&o->o_requests_lock); 5758 req = lookup_request(&o->o_requests, le64_to_cpu(con->in_msg->hdr.tid)); 5759 if (!req) { 5760 spin_unlock(&o->o_requests_lock); 5761 return -EBADR; 5762 } 5763 5764 if (o->o_sparse_op_idx < 0) { 5765 u64 srlen = sparse_data_requested(req); 5766 5767 dout("%s: [%d] starting new sparse read req. srlen=0x%llx\n", 5768 __func__, o->o_osd, srlen); 5769 ceph_msg_data_cursor_init(cursor, con->in_msg, srlen); 5770 } else { 5771 u64 end; 5772 5773 op = &req->r_ops[o->o_sparse_op_idx]; 5774 5775 WARN_ON_ONCE(op->extent.sparse_ext); 5776 5777 /* hand back buffer we took earlier */ 5778 op->extent.sparse_ext = sr->sr_extent; 5779 sr->sr_extent = NULL; 5780 op->extent.sparse_ext_cnt = sr->sr_count; 5781 sr->sr_ext_len = 0; 5782 dout("%s: [%d] completed extent array len %d cursor->resid %zd\n", 5783 __func__, o->o_osd, op->extent.sparse_ext_cnt, cursor->resid); 5784 /* Advance to end of data for this operation */ 5785 end = ceph_sparse_ext_map_end(op); 5786 if (end < sr->sr_req_len) 5787 advance_cursor(cursor, sr->sr_req_len - end, false); 5788 } 5789 5790 ceph_init_sparse_read(sr); 5791 5792 /* find next op in this request (if any) */ 5793 while (++o->o_sparse_op_idx < req->r_num_ops) { 5794 op = &req->r_ops[o->o_sparse_op_idx]; 5795 if (op->op == CEPH_OSD_OP_SPARSE_READ) 5796 goto found; 5797 } 5798 5799 /* reset for next sparse read request */ 5800 spin_unlock(&o->o_requests_lock); 5801 o->o_sparse_op_idx = -1; 5802 return 0; 5803 found: 5804 sr->sr_req_off = op->extent.offset; 5805 sr->sr_req_len = op->extent.length; 5806 sr->sr_pos = sr->sr_req_off; 5807 dout("%s: [%d] new sparse read op at idx %d 0x%llx~0x%llx\n", __func__, 5808 o->o_osd, o->o_sparse_op_idx, sr->sr_req_off, sr->sr_req_len); 5809 5810 /* hand off request's sparse extent map buffer */ 5811 sr->sr_ext_len = op->extent.sparse_ext_cnt; 5812 op->extent.sparse_ext_cnt = 0; 5813 sr->sr_extent = op->extent.sparse_ext; 5814 op->extent.sparse_ext = NULL; 5815 5816 spin_unlock(&o->o_requests_lock); 5817 return 1; 5818 } 5819 5820 #ifdef __BIG_ENDIAN 5821 static inline void convert_extent_map(struct ceph_sparse_read *sr) 5822 { 5823 int i; 5824 5825 for (i = 0; i < sr->sr_count; i++) { 5826 struct ceph_sparse_extent *ext = &sr->sr_extent[i]; 5827 5828 ext->off = le64_to_cpu((__force __le64)ext->off); 5829 ext->len = le64_to_cpu((__force __le64)ext->len); 5830 } 5831 } 5832 #else 5833 static inline void convert_extent_map(struct ceph_sparse_read *sr) 5834 { 5835 } 5836 #endif 5837 5838 #define MAX_EXTENTS 4096 5839 5840 static int osd_sparse_read(struct ceph_connection *con, 5841 struct ceph_msg_data_cursor *cursor, 5842 char **pbuf) 5843 { 5844 struct ceph_osd *o = con->private; 5845 struct ceph_sparse_read *sr = &o->o_sparse_read; 5846 u32 count = sr->sr_count; 5847 u64 eoff, elen; 5848 int ret; 5849 5850 switch (sr->sr_state) { 5851 case CEPH_SPARSE_READ_HDR: 5852 next_op: 5853 ret = prep_next_sparse_read(con, cursor); 5854 if (ret <= 0) 5855 return ret; 5856 5857 /* number of extents */ 5858 ret = sizeof(sr->sr_count); 5859 *pbuf = (char *)&sr->sr_count; 5860 sr->sr_state = CEPH_SPARSE_READ_EXTENTS; 5861 break; 5862 case CEPH_SPARSE_READ_EXTENTS: 5863 /* Convert sr_count to host-endian */ 5864 count = le32_to_cpu((__force __le32)sr->sr_count); 5865 sr->sr_count = count; 5866 dout("[%d] got %u extents\n", o->o_osd, count); 5867 5868 if (count > 0) { 5869 if (!sr->sr_extent || count > sr->sr_ext_len) { 5870 /* 5871 * Apply a hard cap to the number of extents. 5872 * If we have more, assume something is wrong. 5873 */ 5874 if (count > MAX_EXTENTS) { 5875 dout("%s: OSD returned 0x%x extents in a single reply!\n", 5876 __func__, count); 5877 return -EREMOTEIO; 5878 } 5879 5880 /* no extent array provided, or too short */ 5881 kfree(sr->sr_extent); 5882 sr->sr_extent = kmalloc_array(count, 5883 sizeof(*sr->sr_extent), 5884 GFP_NOIO); 5885 if (!sr->sr_extent) 5886 return -ENOMEM; 5887 sr->sr_ext_len = count; 5888 } 5889 ret = count * sizeof(*sr->sr_extent); 5890 *pbuf = (char *)sr->sr_extent; 5891 sr->sr_state = CEPH_SPARSE_READ_DATA_LEN; 5892 break; 5893 } 5894 /* No extents? Read data len */ 5895 fallthrough; 5896 case CEPH_SPARSE_READ_DATA_LEN: 5897 convert_extent_map(sr); 5898 ret = sizeof(sr->sr_datalen); 5899 *pbuf = (char *)&sr->sr_datalen; 5900 sr->sr_state = CEPH_SPARSE_READ_DATA; 5901 break; 5902 case CEPH_SPARSE_READ_DATA: 5903 if (sr->sr_index >= count) { 5904 sr->sr_state = CEPH_SPARSE_READ_HDR; 5905 goto next_op; 5906 } 5907 5908 eoff = sr->sr_extent[sr->sr_index].off; 5909 elen = sr->sr_extent[sr->sr_index].len; 5910 5911 dout("[%d] ext %d off 0x%llx len 0x%llx\n", 5912 o->o_osd, sr->sr_index, eoff, elen); 5913 5914 if (elen > INT_MAX) { 5915 dout("Sparse read extent length too long (0x%llx)\n", 5916 elen); 5917 return -EREMOTEIO; 5918 } 5919 5920 /* zero out anything from sr_pos to start of extent */ 5921 if (sr->sr_pos < eoff) 5922 advance_cursor(cursor, eoff - sr->sr_pos, true); 5923 5924 /* Set position to end of extent */ 5925 sr->sr_pos = eoff + elen; 5926 5927 /* send back the new length and nullify the ptr */ 5928 cursor->sr_resid = elen; 5929 ret = elen; 5930 *pbuf = NULL; 5931 5932 /* Bump the array index */ 5933 ++sr->sr_index; 5934 break; 5935 } 5936 return ret; 5937 } 5938 5939 static const struct ceph_connection_operations osd_con_ops = { 5940 .get = osd_get_con, 5941 .put = osd_put_con, 5942 .sparse_read = osd_sparse_read, 5943 .alloc_msg = osd_alloc_msg, 5944 .dispatch = osd_dispatch, 5945 .fault = osd_fault, 5946 .reencode_message = osd_reencode_message, 5947 .get_authorizer = osd_get_authorizer, 5948 .add_authorizer_challenge = osd_add_authorizer_challenge, 5949 .verify_authorizer_reply = osd_verify_authorizer_reply, 5950 .invalidate_authorizer = osd_invalidate_authorizer, 5951 .sign_message = osd_sign_message, 5952 .check_message_signature = osd_check_message_signature, 5953 .get_auth_request = osd_get_auth_request, 5954 .handle_auth_reply_more = osd_handle_auth_reply_more, 5955 .handle_auth_done = osd_handle_auth_done, 5956 .handle_auth_bad_method = osd_handle_auth_bad_method, 5957 }; 5958