1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/ceph/ceph_debug.h>
5 #include <linux/module.h>
7 #include <linux/highmem.h>
9 #include <linux/pagemap.h>
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
13 #include <linux/bio.h>
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>
25 #define OSD_OPREPLY_FRONT_LEN 512
27 static struct kmem_cache *ceph_osd_request_cache;
29 static const struct ceph_connection_operations osd_con_ops;
32 * Implement client access to distributed object storage cluster.
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
40 * Cluster membership and the mapping of data objects onto storage devices
41 * are described by the osd map.
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.
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);
58 static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
62 if (unlikely(down_read_trylock(sem))) {
69 static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
71 WARN_ON(!rwsem_is_locked(&osdc->lock));
73 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
75 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
77 static inline void verify_osd_locked(struct ceph_osd *osd)
79 struct ceph_osd_client *osdc = osd->o_osdc;
81 WARN_ON(!(mutex_is_locked(&osd->lock) &&
82 rwsem_is_locked(&osdc->lock)) &&
83 !rwsem_is_wrlocked(&osdc->lock));
85 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
87 WARN_ON(!mutex_is_locked(&lreq->lock));
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) { }
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
101 * fill osd op in request message.
103 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104 u64 *objnum, u64 *objoff, u64 *objlen)
106 u64 orig_len = *plen;
110 ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
113 if (*objlen < orig_len) {
115 dout(" skipping last %llu, final file extent %llu~%llu\n",
116 orig_len - *plen, off, *plen);
119 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
123 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
125 memset(osd_data, 0, sizeof (*osd_data));
126 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
130 * Consumes @pages if @own_pages is true.
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)
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;
145 * Consumes a ref on @pagelist.
147 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
148 struct ceph_pagelist *pagelist)
150 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
151 osd_data->pagelist = pagelist;
155 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
156 struct ceph_bio_iter *bio_pos,
159 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
160 osd_data->bio_pos = *bio_pos;
161 osd_data->bio_length = bio_length;
163 #endif /* CONFIG_BLOCK */
165 static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
166 struct ceph_bvec_iter *bvec_pos,
169 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170 osd_data->bvec_pos = *bvec_pos;
171 osd_data->num_bvecs = num_bvecs;
174 static struct ceph_osd_data *
175 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
177 BUG_ON(which >= osd_req->r_num_ops);
179 return &osd_req->r_ops[which].raw_data_in;
182 struct ceph_osd_data *
183 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
186 return osd_req_op_data(osd_req, which, extent, osd_data);
188 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
190 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
191 unsigned int which, struct page **pages,
192 u64 length, u32 alignment,
193 bool pages_from_pool, bool own_pages)
195 struct ceph_osd_data *osd_data;
197 osd_data = osd_req_op_raw_data_in(osd_req, which);
198 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
199 pages_from_pool, own_pages);
201 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
203 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
204 unsigned int which, struct page **pages,
205 u64 length, u32 alignment,
206 bool pages_from_pool, bool own_pages)
208 struct ceph_osd_data *osd_data;
210 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
211 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
212 pages_from_pool, own_pages);
214 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
216 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
217 unsigned int which, struct ceph_pagelist *pagelist)
219 struct ceph_osd_data *osd_data;
221 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
222 ceph_osd_data_pagelist_init(osd_data, pagelist);
224 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
227 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
229 struct ceph_bio_iter *bio_pos,
232 struct ceph_osd_data *osd_data;
234 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
235 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
237 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
238 #endif /* CONFIG_BLOCK */
240 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
242 struct bio_vec *bvecs, u32 num_bvecs,
245 struct ceph_osd_data *osd_data;
246 struct ceph_bvec_iter it = {
248 .iter = { .bi_size = bytes },
251 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
252 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
254 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
256 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
258 struct ceph_bvec_iter *bvec_pos)
260 struct ceph_osd_data *osd_data;
262 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
263 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
265 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
267 static void osd_req_op_cls_request_info_pagelist(
268 struct ceph_osd_request *osd_req,
269 unsigned int which, struct ceph_pagelist *pagelist)
271 struct ceph_osd_data *osd_data;
273 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
274 ceph_osd_data_pagelist_init(osd_data, pagelist);
277 void osd_req_op_cls_request_data_pagelist(
278 struct ceph_osd_request *osd_req,
279 unsigned int which, struct ceph_pagelist *pagelist)
281 struct ceph_osd_data *osd_data;
283 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
284 ceph_osd_data_pagelist_init(osd_data, pagelist);
285 osd_req->r_ops[which].cls.indata_len += pagelist->length;
286 osd_req->r_ops[which].indata_len += pagelist->length;
288 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
290 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
291 unsigned int which, struct page **pages, u64 length,
292 u32 alignment, bool pages_from_pool, bool own_pages)
294 struct ceph_osd_data *osd_data;
296 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
297 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
298 pages_from_pool, own_pages);
299 osd_req->r_ops[which].cls.indata_len += length;
300 osd_req->r_ops[which].indata_len += length;
302 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
304 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
306 struct bio_vec *bvecs, u32 num_bvecs,
309 struct ceph_osd_data *osd_data;
310 struct ceph_bvec_iter it = {
312 .iter = { .bi_size = bytes },
315 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
316 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
317 osd_req->r_ops[which].cls.indata_len += bytes;
318 osd_req->r_ops[which].indata_len += bytes;
320 EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
322 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
323 unsigned int which, struct page **pages, u64 length,
324 u32 alignment, bool pages_from_pool, bool own_pages)
326 struct ceph_osd_data *osd_data;
328 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
329 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
330 pages_from_pool, own_pages);
332 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
334 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
336 switch (osd_data->type) {
337 case CEPH_OSD_DATA_TYPE_NONE:
339 case CEPH_OSD_DATA_TYPE_PAGES:
340 return osd_data->length;
341 case CEPH_OSD_DATA_TYPE_PAGELIST:
342 return (u64)osd_data->pagelist->length;
344 case CEPH_OSD_DATA_TYPE_BIO:
345 return (u64)osd_data->bio_length;
346 #endif /* CONFIG_BLOCK */
347 case CEPH_OSD_DATA_TYPE_BVECS:
348 return osd_data->bvec_pos.iter.bi_size;
350 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
355 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
357 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
360 num_pages = calc_pages_for((u64)osd_data->alignment,
361 (u64)osd_data->length);
362 ceph_release_page_vector(osd_data->pages, num_pages);
363 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
364 ceph_pagelist_release(osd_data->pagelist);
366 ceph_osd_data_init(osd_data);
369 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
372 struct ceph_osd_req_op *op;
374 BUG_ON(which >= osd_req->r_num_ops);
375 op = &osd_req->r_ops[which];
378 case CEPH_OSD_OP_READ:
379 case CEPH_OSD_OP_WRITE:
380 case CEPH_OSD_OP_WRITEFULL:
381 ceph_osd_data_release(&op->extent.osd_data);
383 case CEPH_OSD_OP_CALL:
384 ceph_osd_data_release(&op->cls.request_info);
385 ceph_osd_data_release(&op->cls.request_data);
386 ceph_osd_data_release(&op->cls.response_data);
388 case CEPH_OSD_OP_SETXATTR:
389 case CEPH_OSD_OP_CMPXATTR:
390 ceph_osd_data_release(&op->xattr.osd_data);
392 case CEPH_OSD_OP_STAT:
393 ceph_osd_data_release(&op->raw_data_in);
395 case CEPH_OSD_OP_NOTIFY_ACK:
396 ceph_osd_data_release(&op->notify_ack.request_data);
398 case CEPH_OSD_OP_NOTIFY:
399 ceph_osd_data_release(&op->notify.request_data);
400 ceph_osd_data_release(&op->notify.response_data);
402 case CEPH_OSD_OP_LIST_WATCHERS:
403 ceph_osd_data_release(&op->list_watchers.response_data);
405 case CEPH_OSD_OP_COPY_FROM2:
406 ceph_osd_data_release(&op->copy_from.osd_data);
414 * Assumes @t is zero-initialized.
416 static void target_init(struct ceph_osd_request_target *t)
418 ceph_oid_init(&t->base_oid);
419 ceph_oloc_init(&t->base_oloc);
420 ceph_oid_init(&t->target_oid);
421 ceph_oloc_init(&t->target_oloc);
423 ceph_osds_init(&t->acting);
424 ceph_osds_init(&t->up);
428 t->osd = CEPH_HOMELESS_OSD;
431 static void target_copy(struct ceph_osd_request_target *dest,
432 const struct ceph_osd_request_target *src)
434 ceph_oid_copy(&dest->base_oid, &src->base_oid);
435 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
436 ceph_oid_copy(&dest->target_oid, &src->target_oid);
437 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
439 dest->pgid = src->pgid; /* struct */
440 dest->spgid = src->spgid; /* struct */
441 dest->pg_num = src->pg_num;
442 dest->pg_num_mask = src->pg_num_mask;
443 ceph_osds_copy(&dest->acting, &src->acting);
444 ceph_osds_copy(&dest->up, &src->up);
445 dest->size = src->size;
446 dest->min_size = src->min_size;
447 dest->sort_bitwise = src->sort_bitwise;
449 dest->flags = src->flags;
450 dest->paused = src->paused;
452 dest->epoch = src->epoch;
453 dest->last_force_resend = src->last_force_resend;
455 dest->osd = src->osd;
458 static void target_destroy(struct ceph_osd_request_target *t)
460 ceph_oid_destroy(&t->base_oid);
461 ceph_oloc_destroy(&t->base_oloc);
462 ceph_oid_destroy(&t->target_oid);
463 ceph_oloc_destroy(&t->target_oloc);
469 static void request_release_checks(struct ceph_osd_request *req)
471 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
472 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
473 WARN_ON(!list_empty(&req->r_private_item));
477 static void ceph_osdc_release_request(struct kref *kref)
479 struct ceph_osd_request *req = container_of(kref,
480 struct ceph_osd_request, r_kref);
483 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
484 req->r_request, req->r_reply);
485 request_release_checks(req);
488 ceph_msg_put(req->r_request);
490 ceph_msg_put(req->r_reply);
492 for (which = 0; which < req->r_num_ops; which++)
493 osd_req_op_data_release(req, which);
495 target_destroy(&req->r_t);
496 ceph_put_snap_context(req->r_snapc);
499 mempool_free(req, req->r_osdc->req_mempool);
500 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
501 kmem_cache_free(ceph_osd_request_cache, req);
506 void ceph_osdc_get_request(struct ceph_osd_request *req)
508 dout("%s %p (was %d)\n", __func__, req,
509 kref_read(&req->r_kref));
510 kref_get(&req->r_kref);
512 EXPORT_SYMBOL(ceph_osdc_get_request);
514 void ceph_osdc_put_request(struct ceph_osd_request *req)
517 dout("%s %p (was %d)\n", __func__, req,
518 kref_read(&req->r_kref));
519 kref_put(&req->r_kref, ceph_osdc_release_request);
522 EXPORT_SYMBOL(ceph_osdc_put_request);
524 static void request_init(struct ceph_osd_request *req)
526 /* req only, each op is zeroed in _osd_req_op_init() */
527 memset(req, 0, sizeof(*req));
529 kref_init(&req->r_kref);
530 init_completion(&req->r_completion);
531 RB_CLEAR_NODE(&req->r_node);
532 RB_CLEAR_NODE(&req->r_mc_node);
533 INIT_LIST_HEAD(&req->r_private_item);
535 target_init(&req->r_t);
539 * This is ugly, but it allows us to reuse linger registration and ping
540 * requests, keeping the structure of the code around send_linger{_ping}()
541 * reasonable. Setting up a min_nr=2 mempool for each linger request
542 * and dealing with copying ops (this blasts req only, watch op remains
543 * intact) isn't any better.
545 static void request_reinit(struct ceph_osd_request *req)
547 struct ceph_osd_client *osdc = req->r_osdc;
548 bool mempool = req->r_mempool;
549 unsigned int num_ops = req->r_num_ops;
550 u64 snapid = req->r_snapid;
551 struct ceph_snap_context *snapc = req->r_snapc;
552 bool linger = req->r_linger;
553 struct ceph_msg *request_msg = req->r_request;
554 struct ceph_msg *reply_msg = req->r_reply;
556 dout("%s req %p\n", __func__, req);
557 WARN_ON(kref_read(&req->r_kref) != 1);
558 request_release_checks(req);
560 WARN_ON(kref_read(&request_msg->kref) != 1);
561 WARN_ON(kref_read(&reply_msg->kref) != 1);
562 target_destroy(&req->r_t);
566 req->r_mempool = mempool;
567 req->r_num_ops = num_ops;
568 req->r_snapid = snapid;
569 req->r_snapc = snapc;
570 req->r_linger = linger;
571 req->r_request = request_msg;
572 req->r_reply = reply_msg;
575 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
576 struct ceph_snap_context *snapc,
577 unsigned int num_ops,
581 struct ceph_osd_request *req;
584 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
585 req = mempool_alloc(osdc->req_mempool, gfp_flags);
586 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
587 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
589 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
590 req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
597 req->r_mempool = use_mempool;
598 req->r_num_ops = num_ops;
599 req->r_snapid = CEPH_NOSNAP;
600 req->r_snapc = ceph_get_snap_context(snapc);
602 dout("%s req %p\n", __func__, req);
605 EXPORT_SYMBOL(ceph_osdc_alloc_request);
607 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
609 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
612 static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
613 int num_request_data_items,
614 int num_reply_data_items)
616 struct ceph_osd_client *osdc = req->r_osdc;
617 struct ceph_msg *msg;
620 WARN_ON(req->r_request || req->r_reply);
621 WARN_ON(ceph_oid_empty(&req->r_base_oid));
622 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
624 /* create request message */
625 msg_size = CEPH_ENCODING_START_BLK_LEN +
626 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
627 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
628 msg_size += CEPH_ENCODING_START_BLK_LEN +
629 sizeof(struct ceph_osd_reqid); /* reqid */
630 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
631 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
632 msg_size += CEPH_ENCODING_START_BLK_LEN +
633 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
634 msg_size += 4 + req->r_base_oid.name_len; /* oid */
635 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
636 msg_size += 8; /* snapid */
637 msg_size += 8; /* snap_seq */
638 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
639 msg_size += 4 + 8; /* retry_attempt, features */
642 msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
643 num_request_data_items);
645 msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
646 num_request_data_items, gfp, true);
650 memset(msg->front.iov_base, 0, msg->front.iov_len);
651 req->r_request = msg;
653 /* create reply message */
654 msg_size = OSD_OPREPLY_FRONT_LEN;
655 msg_size += req->r_base_oid.name_len;
656 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
659 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
660 num_reply_data_items);
662 msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
663 num_reply_data_items, gfp, true);
672 static bool osd_req_opcode_valid(u16 opcode)
675 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
676 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
683 static void get_num_data_items(struct ceph_osd_request *req,
684 int *num_request_data_items,
685 int *num_reply_data_items)
687 struct ceph_osd_req_op *op;
689 *num_request_data_items = 0;
690 *num_reply_data_items = 0;
692 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
695 case CEPH_OSD_OP_WRITE:
696 case CEPH_OSD_OP_WRITEFULL:
697 case CEPH_OSD_OP_SETXATTR:
698 case CEPH_OSD_OP_CMPXATTR:
699 case CEPH_OSD_OP_NOTIFY_ACK:
700 case CEPH_OSD_OP_COPY_FROM2:
701 *num_request_data_items += 1;
705 case CEPH_OSD_OP_STAT:
706 case CEPH_OSD_OP_READ:
707 case CEPH_OSD_OP_LIST_WATCHERS:
708 *num_reply_data_items += 1;
712 case CEPH_OSD_OP_NOTIFY:
713 *num_request_data_items += 1;
714 *num_reply_data_items += 1;
716 case CEPH_OSD_OP_CALL:
717 *num_request_data_items += 2;
718 *num_reply_data_items += 1;
722 WARN_ON(!osd_req_opcode_valid(op->op));
729 * oid, oloc and OSD op opcode(s) must be filled in before this function
732 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
734 int num_request_data_items, num_reply_data_items;
736 get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
737 return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
738 num_reply_data_items);
740 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
743 * This is an osd op init function for opcodes that have no data or
744 * other information associated with them. It also serves as a
745 * common init routine for all the other init functions, below.
747 static struct ceph_osd_req_op *
748 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
749 u16 opcode, u32 flags)
751 struct ceph_osd_req_op *op;
753 BUG_ON(which >= osd_req->r_num_ops);
754 BUG_ON(!osd_req_opcode_valid(opcode));
756 op = &osd_req->r_ops[which];
757 memset(op, 0, sizeof (*op));
764 void osd_req_op_init(struct ceph_osd_request *osd_req,
765 unsigned int which, u16 opcode, u32 flags)
767 (void)_osd_req_op_init(osd_req, which, opcode, flags);
769 EXPORT_SYMBOL(osd_req_op_init);
771 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
772 unsigned int which, u16 opcode,
773 u64 offset, u64 length,
774 u64 truncate_size, u32 truncate_seq)
776 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
778 size_t payload_len = 0;
780 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
781 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
782 opcode != CEPH_OSD_OP_TRUNCATE);
784 op->extent.offset = offset;
785 op->extent.length = length;
786 op->extent.truncate_size = truncate_size;
787 op->extent.truncate_seq = truncate_seq;
788 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
789 payload_len += length;
791 op->indata_len = payload_len;
793 EXPORT_SYMBOL(osd_req_op_extent_init);
795 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
796 unsigned int which, u64 length)
798 struct ceph_osd_req_op *op;
801 BUG_ON(which >= osd_req->r_num_ops);
802 op = &osd_req->r_ops[which];
803 previous = op->extent.length;
805 if (length == previous)
806 return; /* Nothing to do */
807 BUG_ON(length > previous);
809 op->extent.length = length;
810 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
811 op->indata_len -= previous - length;
813 EXPORT_SYMBOL(osd_req_op_extent_update);
815 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
816 unsigned int which, u64 offset_inc)
818 struct ceph_osd_req_op *op, *prev_op;
820 BUG_ON(which + 1 >= osd_req->r_num_ops);
822 prev_op = &osd_req->r_ops[which];
823 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
824 /* dup previous one */
825 op->indata_len = prev_op->indata_len;
826 op->outdata_len = prev_op->outdata_len;
827 op->extent = prev_op->extent;
829 op->extent.offset += offset_inc;
830 op->extent.length -= offset_inc;
832 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
833 op->indata_len -= offset_inc;
835 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
837 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
838 const char *class, const char *method)
840 struct ceph_osd_req_op *op;
841 struct ceph_pagelist *pagelist;
842 size_t payload_len = 0;
846 op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
848 pagelist = ceph_pagelist_alloc(GFP_NOFS);
852 op->cls.class_name = class;
853 size = strlen(class);
854 BUG_ON(size > (size_t) U8_MAX);
855 op->cls.class_len = size;
856 ret = ceph_pagelist_append(pagelist, class, size);
858 goto err_pagelist_free;
861 op->cls.method_name = method;
862 size = strlen(method);
863 BUG_ON(size > (size_t) U8_MAX);
864 op->cls.method_len = size;
865 ret = ceph_pagelist_append(pagelist, method, size);
867 goto err_pagelist_free;
870 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
871 op->indata_len = payload_len;
875 ceph_pagelist_release(pagelist);
878 EXPORT_SYMBOL(osd_req_op_cls_init);
880 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
881 u16 opcode, const char *name, const void *value,
882 size_t size, u8 cmp_op, u8 cmp_mode)
884 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
886 struct ceph_pagelist *pagelist;
890 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
892 pagelist = ceph_pagelist_alloc(GFP_NOFS);
896 payload_len = strlen(name);
897 op->xattr.name_len = payload_len;
898 ret = ceph_pagelist_append(pagelist, name, payload_len);
900 goto err_pagelist_free;
902 op->xattr.value_len = size;
903 ret = ceph_pagelist_append(pagelist, value, size);
905 goto err_pagelist_free;
908 op->xattr.cmp_op = cmp_op;
909 op->xattr.cmp_mode = cmp_mode;
911 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
912 op->indata_len = payload_len;
916 ceph_pagelist_release(pagelist);
919 EXPORT_SYMBOL(osd_req_op_xattr_init);
922 * @watch_opcode: CEPH_OSD_WATCH_OP_*
924 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
925 u64 cookie, u8 watch_opcode)
927 struct ceph_osd_req_op *op;
929 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
930 op->watch.cookie = cookie;
931 op->watch.op = watch_opcode;
935 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
937 u64 expected_object_size,
938 u64 expected_write_size)
940 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
941 CEPH_OSD_OP_SETALLOCHINT,
944 op->alloc_hint.expected_object_size = expected_object_size;
945 op->alloc_hint.expected_write_size = expected_write_size;
948 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
949 * not worth a feature bit. Set FAILOK per-op flag to make
950 * sure older osds don't trip over an unsupported opcode.
952 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
954 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
956 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
957 struct ceph_osd_data *osd_data)
959 u64 length = ceph_osd_data_length(osd_data);
961 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
962 BUG_ON(length > (u64) SIZE_MAX);
964 ceph_msg_data_add_pages(msg, osd_data->pages,
965 length, osd_data->alignment, false);
966 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
968 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
970 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
971 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
973 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
974 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
976 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
980 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
981 const struct ceph_osd_req_op *src)
984 case CEPH_OSD_OP_STAT:
986 case CEPH_OSD_OP_READ:
987 case CEPH_OSD_OP_WRITE:
988 case CEPH_OSD_OP_WRITEFULL:
989 case CEPH_OSD_OP_ZERO:
990 case CEPH_OSD_OP_TRUNCATE:
991 dst->extent.offset = cpu_to_le64(src->extent.offset);
992 dst->extent.length = cpu_to_le64(src->extent.length);
993 dst->extent.truncate_size =
994 cpu_to_le64(src->extent.truncate_size);
995 dst->extent.truncate_seq =
996 cpu_to_le32(src->extent.truncate_seq);
998 case CEPH_OSD_OP_CALL:
999 dst->cls.class_len = src->cls.class_len;
1000 dst->cls.method_len = src->cls.method_len;
1001 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
1003 case CEPH_OSD_OP_WATCH:
1004 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
1005 dst->watch.ver = cpu_to_le64(0);
1006 dst->watch.op = src->watch.op;
1007 dst->watch.gen = cpu_to_le32(src->watch.gen);
1009 case CEPH_OSD_OP_NOTIFY_ACK:
1011 case CEPH_OSD_OP_NOTIFY:
1012 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
1014 case CEPH_OSD_OP_LIST_WATCHERS:
1016 case CEPH_OSD_OP_SETALLOCHINT:
1017 dst->alloc_hint.expected_object_size =
1018 cpu_to_le64(src->alloc_hint.expected_object_size);
1019 dst->alloc_hint.expected_write_size =
1020 cpu_to_le64(src->alloc_hint.expected_write_size);
1022 case CEPH_OSD_OP_SETXATTR:
1023 case CEPH_OSD_OP_CMPXATTR:
1024 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1025 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1026 dst->xattr.cmp_op = src->xattr.cmp_op;
1027 dst->xattr.cmp_mode = src->xattr.cmp_mode;
1029 case CEPH_OSD_OP_CREATE:
1030 case CEPH_OSD_OP_DELETE:
1032 case CEPH_OSD_OP_COPY_FROM2:
1033 dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
1034 dst->copy_from.src_version =
1035 cpu_to_le64(src->copy_from.src_version);
1036 dst->copy_from.flags = src->copy_from.flags;
1037 dst->copy_from.src_fadvise_flags =
1038 cpu_to_le32(src->copy_from.src_fadvise_flags);
1041 pr_err("unsupported osd opcode %s\n",
1042 ceph_osd_op_name(src->op));
1048 dst->op = cpu_to_le16(src->op);
1049 dst->flags = cpu_to_le32(src->flags);
1050 dst->payload_len = cpu_to_le32(src->indata_len);
1052 return src->indata_len;
1056 * build new request AND message, calculate layout, and adjust file
1059 * if the file was recently truncated, we include information about its
1060 * old and new size so that the object can be updated appropriately. (we
1061 * avoid synchronously deleting truncated objects because it's slow.)
1063 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
1064 struct ceph_file_layout *layout,
1065 struct ceph_vino vino,
1067 unsigned int which, int num_ops,
1068 int opcode, int flags,
1069 struct ceph_snap_context *snapc,
1074 struct ceph_osd_request *req;
1080 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1081 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1082 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
1084 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1091 /* calculate max write size */
1092 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1096 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1097 osd_req_op_init(req, which, opcode, 0);
1099 u32 object_size = layout->object_size;
1100 u32 object_base = off - objoff;
1101 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1102 if (truncate_size <= object_base) {
1105 truncate_size -= object_base;
1106 if (truncate_size > object_size)
1107 truncate_size = object_size;
1110 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1111 truncate_size, truncate_seq);
1114 req->r_flags = flags;
1115 req->r_base_oloc.pool = layout->pool_id;
1116 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1117 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1119 req->r_snapid = vino.snap;
1120 if (flags & CEPH_OSD_FLAG_WRITE)
1121 req->r_data_offset = off;
1125 * This is a special case for ceph_writepages_start(), but it
1126 * also covers ceph_uninline_data(). If more multi-op request
1127 * use cases emerge, we will need a separate helper.
1129 r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
1131 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1138 ceph_osdc_put_request(req);
1141 EXPORT_SYMBOL(ceph_osdc_new_request);
1144 * We keep osd requests in an rbtree, sorted by ->r_tid.
1146 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1147 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1150 * Call @fn on each OSD request as long as @fn returns 0.
1152 static void for_each_request(struct ceph_osd_client *osdc,
1153 int (*fn)(struct ceph_osd_request *req, void *arg),
1156 struct rb_node *n, *p;
1158 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1159 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1161 for (p = rb_first(&osd->o_requests); p; ) {
1162 struct ceph_osd_request *req =
1163 rb_entry(p, struct ceph_osd_request, r_node);
1171 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1172 struct ceph_osd_request *req =
1173 rb_entry(p, struct ceph_osd_request, r_node);
1181 static bool osd_homeless(struct ceph_osd *osd)
1183 return osd->o_osd == CEPH_HOMELESS_OSD;
1186 static bool osd_registered(struct ceph_osd *osd)
1188 verify_osdc_locked(osd->o_osdc);
1190 return !RB_EMPTY_NODE(&osd->o_node);
1194 * Assumes @osd is zero-initialized.
1196 static void osd_init(struct ceph_osd *osd)
1198 refcount_set(&osd->o_ref, 1);
1199 RB_CLEAR_NODE(&osd->o_node);
1200 osd->o_requests = RB_ROOT;
1201 osd->o_linger_requests = RB_ROOT;
1202 osd->o_backoff_mappings = RB_ROOT;
1203 osd->o_backoffs_by_id = RB_ROOT;
1204 INIT_LIST_HEAD(&osd->o_osd_lru);
1205 INIT_LIST_HEAD(&osd->o_keepalive_item);
1206 osd->o_incarnation = 1;
1207 mutex_init(&osd->lock);
1210 static void osd_cleanup(struct ceph_osd *osd)
1212 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1213 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1214 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1215 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1216 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1217 WARN_ON(!list_empty(&osd->o_osd_lru));
1218 WARN_ON(!list_empty(&osd->o_keepalive_item));
1220 if (osd->o_auth.authorizer) {
1221 WARN_ON(osd_homeless(osd));
1222 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1227 * Track open sessions with osds.
1229 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1231 struct ceph_osd *osd;
1233 WARN_ON(onum == CEPH_HOMELESS_OSD);
1235 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1240 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1245 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1247 if (refcount_inc_not_zero(&osd->o_ref)) {
1248 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1249 refcount_read(&osd->o_ref));
1252 dout("get_osd %p FAIL\n", osd);
1257 static void put_osd(struct ceph_osd *osd)
1259 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1260 refcount_read(&osd->o_ref) - 1);
1261 if (refcount_dec_and_test(&osd->o_ref)) {
1267 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1269 static void __move_osd_to_lru(struct ceph_osd *osd)
1271 struct ceph_osd_client *osdc = osd->o_osdc;
1273 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1274 BUG_ON(!list_empty(&osd->o_osd_lru));
1276 spin_lock(&osdc->osd_lru_lock);
1277 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1278 spin_unlock(&osdc->osd_lru_lock);
1280 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1283 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1285 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1286 RB_EMPTY_ROOT(&osd->o_linger_requests))
1287 __move_osd_to_lru(osd);
1290 static void __remove_osd_from_lru(struct ceph_osd *osd)
1292 struct ceph_osd_client *osdc = osd->o_osdc;
1294 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1296 spin_lock(&osdc->osd_lru_lock);
1297 if (!list_empty(&osd->o_osd_lru))
1298 list_del_init(&osd->o_osd_lru);
1299 spin_unlock(&osdc->osd_lru_lock);
1303 * Close the connection and assign any leftover requests to the
1306 static void close_osd(struct ceph_osd *osd)
1308 struct ceph_osd_client *osdc = osd->o_osdc;
1311 verify_osdc_wrlocked(osdc);
1312 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1314 ceph_con_close(&osd->o_con);
1316 for (n = rb_first(&osd->o_requests); n; ) {
1317 struct ceph_osd_request *req =
1318 rb_entry(n, struct ceph_osd_request, r_node);
1320 n = rb_next(n); /* unlink_request() */
1322 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1323 unlink_request(osd, req);
1324 link_request(&osdc->homeless_osd, req);
1326 for (n = rb_first(&osd->o_linger_requests); n; ) {
1327 struct ceph_osd_linger_request *lreq =
1328 rb_entry(n, struct ceph_osd_linger_request, node);
1330 n = rb_next(n); /* unlink_linger() */
1332 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1334 unlink_linger(osd, lreq);
1335 link_linger(&osdc->homeless_osd, lreq);
1337 clear_backoffs(osd);
1339 __remove_osd_from_lru(osd);
1340 erase_osd(&osdc->osds, osd);
1347 static int reopen_osd(struct ceph_osd *osd)
1349 struct ceph_entity_addr *peer_addr;
1351 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1353 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1354 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1359 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1360 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1361 !ceph_con_opened(&osd->o_con)) {
1364 dout("osd addr hasn't changed and connection never opened, "
1365 "letting msgr retry\n");
1366 /* touch each r_stamp for handle_timeout()'s benfit */
1367 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1368 struct ceph_osd_request *req =
1369 rb_entry(n, struct ceph_osd_request, r_node);
1370 req->r_stamp = jiffies;
1376 ceph_con_close(&osd->o_con);
1377 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1378 osd->o_incarnation++;
1383 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1386 struct ceph_osd *osd;
1389 verify_osdc_wrlocked(osdc);
1391 verify_osdc_locked(osdc);
1393 if (o != CEPH_HOMELESS_OSD)
1394 osd = lookup_osd(&osdc->osds, o);
1396 osd = &osdc->homeless_osd;
1399 return ERR_PTR(-EAGAIN);
1401 osd = create_osd(osdc, o);
1402 insert_osd(&osdc->osds, osd);
1403 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1404 &osdc->osdmap->osd_addr[osd->o_osd]);
1407 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1412 * Create request <-> OSD session relation.
1414 * @req has to be assigned a tid, @osd may be homeless.
1416 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1418 verify_osd_locked(osd);
1419 WARN_ON(!req->r_tid || req->r_osd);
1420 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1423 if (!osd_homeless(osd))
1424 __remove_osd_from_lru(osd);
1426 atomic_inc(&osd->o_osdc->num_homeless);
1429 insert_request(&osd->o_requests, req);
1433 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1435 verify_osd_locked(osd);
1436 WARN_ON(req->r_osd != osd);
1437 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1441 erase_request(&osd->o_requests, req);
1444 if (!osd_homeless(osd))
1445 maybe_move_osd_to_lru(osd);
1447 atomic_dec(&osd->o_osdc->num_homeless);
1450 static bool __pool_full(struct ceph_pg_pool_info *pi)
1452 return pi->flags & CEPH_POOL_FLAG_FULL;
1455 static bool have_pool_full(struct ceph_osd_client *osdc)
1459 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1460 struct ceph_pg_pool_info *pi =
1461 rb_entry(n, struct ceph_pg_pool_info, node);
1463 if (__pool_full(pi))
1470 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1472 struct ceph_pg_pool_info *pi;
1474 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1478 return __pool_full(pi);
1482 * Returns whether a request should be blocked from being sent
1483 * based on the current osdmap and osd_client settings.
1485 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1486 const struct ceph_osd_request_target *t,
1487 struct ceph_pg_pool_info *pi)
1489 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1490 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1491 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1494 WARN_ON(pi->id != t->target_oloc.pool);
1495 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1496 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1497 (osdc->osdmap->epoch < osdc->epoch_barrier);
1500 static int pick_random_replica(const struct ceph_osds *acting)
1502 int i = prandom_u32() % acting->size;
1504 dout("%s picked osd%d, primary osd%d\n", __func__,
1505 acting->osds[i], acting->primary);
1510 * Picks the closest replica based on client's location given by
1511 * crush_location option. Prefers the primary if the locality is
1514 static int pick_closest_replica(struct ceph_osd_client *osdc,
1515 const struct ceph_osds *acting)
1517 struct ceph_options *opt = osdc->client->options;
1518 int best_i, best_locality;
1519 int i = 0, locality;
1522 locality = ceph_get_crush_locality(osdc->osdmap,
1526 (locality >= 0 && best_locality < 0) ||
1527 (locality >= 0 && best_locality >= 0 &&
1528 locality < best_locality)) {
1530 best_locality = locality;
1532 } while (++i < acting->size);
1534 dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
1535 acting->osds[best_i], best_locality, acting->primary);
1539 enum calc_target_result {
1540 CALC_TARGET_NO_ACTION = 0,
1541 CALC_TARGET_NEED_RESEND,
1542 CALC_TARGET_POOL_DNE,
1545 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1546 struct ceph_osd_request_target *t,
1549 struct ceph_pg_pool_info *pi;
1550 struct ceph_pg pgid, last_pgid;
1551 struct ceph_osds up, acting;
1552 bool is_read = t->flags & CEPH_OSD_FLAG_READ;
1553 bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
1554 bool force_resend = false;
1555 bool unpaused = false;
1556 bool legacy_change = false;
1558 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1559 bool recovery_deletes = ceph_osdmap_flag(osdc,
1560 CEPH_OSDMAP_RECOVERY_DELETES);
1561 enum calc_target_result ct_res;
1563 t->epoch = osdc->osdmap->epoch;
1564 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1566 t->osd = CEPH_HOMELESS_OSD;
1567 ct_res = CALC_TARGET_POOL_DNE;
1571 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1572 if (t->last_force_resend < pi->last_force_request_resend) {
1573 t->last_force_resend = pi->last_force_request_resend;
1574 force_resend = true;
1575 } else if (t->last_force_resend == 0) {
1576 force_resend = true;
1581 ceph_oid_copy(&t->target_oid, &t->base_oid);
1582 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1583 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1584 if (is_read && pi->read_tier >= 0)
1585 t->target_oloc.pool = pi->read_tier;
1586 if (is_write && pi->write_tier >= 0)
1587 t->target_oloc.pool = pi->write_tier;
1589 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1591 t->osd = CEPH_HOMELESS_OSD;
1592 ct_res = CALC_TARGET_POOL_DNE;
1597 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
1598 last_pgid.pool = pgid.pool;
1599 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1601 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1603 ceph_is_new_interval(&t->acting,
1615 t->recovery_deletes,
1618 force_resend = true;
1620 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1624 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1625 ceph_osds_changed(&t->acting, &acting,
1626 t->used_replica || any_change);
1628 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1630 if (legacy_change || force_resend || split) {
1631 t->pgid = pgid; /* struct */
1632 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1633 ceph_osds_copy(&t->acting, &acting);
1634 ceph_osds_copy(&t->up, &up);
1636 t->min_size = pi->min_size;
1637 t->pg_num = pi->pg_num;
1638 t->pg_num_mask = pi->pg_num_mask;
1639 t->sort_bitwise = sort_bitwise;
1640 t->recovery_deletes = recovery_deletes;
1642 if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
1643 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
1644 !is_write && pi->type == CEPH_POOL_TYPE_REP &&
1648 WARN_ON(!is_read || acting.osds[0] != acting.primary);
1649 if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
1650 pos = pick_random_replica(&acting);
1652 pos = pick_closest_replica(osdc, &acting);
1654 t->osd = acting.osds[pos];
1655 t->used_replica = pos > 0;
1657 t->osd = acting.primary;
1658 t->used_replica = false;
1662 if (unpaused || legacy_change || force_resend || split)
1663 ct_res = CALC_TARGET_NEED_RESEND;
1665 ct_res = CALC_TARGET_NO_ACTION;
1668 dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1669 legacy_change, force_resend, split, ct_res, t->osd);
1673 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1675 struct ceph_spg_mapping *spg;
1677 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1681 RB_CLEAR_NODE(&spg->node);
1682 spg->backoffs = RB_ROOT;
1686 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1688 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1689 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1695 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1696 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1697 * defined only within a specific spgid; it does not pass anything to
1698 * children on split, or to another primary.
1700 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1701 RB_BYPTR, const struct ceph_spg *, node)
1703 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1705 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1708 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1709 void **pkey, size_t *pkey_len)
1711 if (hoid->key_len) {
1713 *pkey_len = hoid->key_len;
1716 *pkey_len = hoid->oid_len;
1720 static int compare_names(const void *name1, size_t name1_len,
1721 const void *name2, size_t name2_len)
1725 ret = memcmp(name1, name2, min(name1_len, name2_len));
1727 if (name1_len < name2_len)
1729 else if (name1_len > name2_len)
1735 static int hoid_compare(const struct ceph_hobject_id *lhs,
1736 const struct ceph_hobject_id *rhs)
1738 void *effective_key1, *effective_key2;
1739 size_t effective_key1_len, effective_key2_len;
1742 if (lhs->is_max < rhs->is_max)
1744 if (lhs->is_max > rhs->is_max)
1747 if (lhs->pool < rhs->pool)
1749 if (lhs->pool > rhs->pool)
1752 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1754 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1757 ret = compare_names(lhs->nspace, lhs->nspace_len,
1758 rhs->nspace, rhs->nspace_len);
1762 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1763 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1764 ret = compare_names(effective_key1, effective_key1_len,
1765 effective_key2, effective_key2_len);
1769 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1773 if (lhs->snapid < rhs->snapid)
1775 if (lhs->snapid > rhs->snapid)
1782 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1783 * compat stuff here.
1785 * Assumes @hoid is zero-initialized.
1787 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1793 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1799 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1803 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1805 if (IS_ERR(hoid->key)) {
1806 ret = PTR_ERR(hoid->key);
1811 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1813 if (IS_ERR(hoid->oid)) {
1814 ret = PTR_ERR(hoid->oid);
1819 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1820 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1821 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1823 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1825 if (IS_ERR(hoid->nspace)) {
1826 ret = PTR_ERR(hoid->nspace);
1827 hoid->nspace = NULL;
1831 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1833 ceph_hoid_build_hash_cache(hoid);
1840 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1842 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1843 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1846 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1848 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1849 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1850 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1851 ceph_encode_64(p, hoid->snapid);
1852 ceph_encode_32(p, hoid->hash);
1853 ceph_encode_8(p, hoid->is_max);
1854 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1855 ceph_encode_64(p, hoid->pool);
1858 static void free_hoid(struct ceph_hobject_id *hoid)
1863 kfree(hoid->nspace);
1868 static struct ceph_osd_backoff *alloc_backoff(void)
1870 struct ceph_osd_backoff *backoff;
1872 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1876 RB_CLEAR_NODE(&backoff->spg_node);
1877 RB_CLEAR_NODE(&backoff->id_node);
1881 static void free_backoff(struct ceph_osd_backoff *backoff)
1883 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1884 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1886 free_hoid(backoff->begin);
1887 free_hoid(backoff->end);
1892 * Within a specific spgid, backoffs are managed by ->begin hoid.
1894 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1895 RB_BYVAL, spg_node);
1897 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1898 const struct ceph_hobject_id *hoid)
1900 struct rb_node *n = root->rb_node;
1903 struct ceph_osd_backoff *cur =
1904 rb_entry(n, struct ceph_osd_backoff, spg_node);
1907 cmp = hoid_compare(hoid, cur->begin);
1910 } else if (cmp > 0) {
1911 if (hoid_compare(hoid, cur->end) < 0)
1924 * Each backoff has a unique id within its OSD session.
1926 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1928 static void clear_backoffs(struct ceph_osd *osd)
1930 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1931 struct ceph_spg_mapping *spg =
1932 rb_entry(rb_first(&osd->o_backoff_mappings),
1933 struct ceph_spg_mapping, node);
1935 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1936 struct ceph_osd_backoff *backoff =
1937 rb_entry(rb_first(&spg->backoffs),
1938 struct ceph_osd_backoff, spg_node);
1940 erase_backoff(&spg->backoffs, backoff);
1941 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1942 free_backoff(backoff);
1944 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1945 free_spg_mapping(spg);
1950 * Set up a temporary, non-owning view into @t.
1952 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1953 const struct ceph_osd_request_target *t)
1957 hoid->oid = t->target_oid.name;
1958 hoid->oid_len = t->target_oid.name_len;
1959 hoid->snapid = CEPH_NOSNAP;
1960 hoid->hash = t->pgid.seed;
1961 hoid->is_max = false;
1962 if (t->target_oloc.pool_ns) {
1963 hoid->nspace = t->target_oloc.pool_ns->str;
1964 hoid->nspace_len = t->target_oloc.pool_ns->len;
1966 hoid->nspace = NULL;
1967 hoid->nspace_len = 0;
1969 hoid->pool = t->target_oloc.pool;
1970 ceph_hoid_build_hash_cache(hoid);
1973 static bool should_plug_request(struct ceph_osd_request *req)
1975 struct ceph_osd *osd = req->r_osd;
1976 struct ceph_spg_mapping *spg;
1977 struct ceph_osd_backoff *backoff;
1978 struct ceph_hobject_id hoid;
1980 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1984 hoid_fill_from_target(&hoid, &req->r_t);
1985 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1989 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1990 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1991 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1996 * Keep get_num_data_items() in sync with this function.
1998 static void setup_request_data(struct ceph_osd_request *req)
2000 struct ceph_msg *request_msg = req->r_request;
2001 struct ceph_msg *reply_msg = req->r_reply;
2002 struct ceph_osd_req_op *op;
2004 if (req->r_request->num_data_items || req->r_reply->num_data_items)
2007 WARN_ON(request_msg->data_length || reply_msg->data_length);
2008 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
2011 case CEPH_OSD_OP_WRITE:
2012 case CEPH_OSD_OP_WRITEFULL:
2013 WARN_ON(op->indata_len != op->extent.length);
2014 ceph_osdc_msg_data_add(request_msg,
2015 &op->extent.osd_data);
2017 case CEPH_OSD_OP_SETXATTR:
2018 case CEPH_OSD_OP_CMPXATTR:
2019 WARN_ON(op->indata_len != op->xattr.name_len +
2020 op->xattr.value_len);
2021 ceph_osdc_msg_data_add(request_msg,
2022 &op->xattr.osd_data);
2024 case CEPH_OSD_OP_NOTIFY_ACK:
2025 ceph_osdc_msg_data_add(request_msg,
2026 &op->notify_ack.request_data);
2028 case CEPH_OSD_OP_COPY_FROM2:
2029 ceph_osdc_msg_data_add(request_msg,
2030 &op->copy_from.osd_data);
2034 case CEPH_OSD_OP_STAT:
2035 ceph_osdc_msg_data_add(reply_msg,
2038 case CEPH_OSD_OP_READ:
2039 ceph_osdc_msg_data_add(reply_msg,
2040 &op->extent.osd_data);
2042 case CEPH_OSD_OP_LIST_WATCHERS:
2043 ceph_osdc_msg_data_add(reply_msg,
2044 &op->list_watchers.response_data);
2048 case CEPH_OSD_OP_CALL:
2049 WARN_ON(op->indata_len != op->cls.class_len +
2050 op->cls.method_len +
2051 op->cls.indata_len);
2052 ceph_osdc_msg_data_add(request_msg,
2053 &op->cls.request_info);
2054 /* optional, can be NONE */
2055 ceph_osdc_msg_data_add(request_msg,
2056 &op->cls.request_data);
2057 /* optional, can be NONE */
2058 ceph_osdc_msg_data_add(reply_msg,
2059 &op->cls.response_data);
2061 case CEPH_OSD_OP_NOTIFY:
2062 ceph_osdc_msg_data_add(request_msg,
2063 &op->notify.request_data);
2064 ceph_osdc_msg_data_add(reply_msg,
2065 &op->notify.response_data);
2071 static void encode_pgid(void **p, const struct ceph_pg *pgid)
2073 ceph_encode_8(p, 1);
2074 ceph_encode_64(p, pgid->pool);
2075 ceph_encode_32(p, pgid->seed);
2076 ceph_encode_32(p, -1); /* preferred */
2079 static void encode_spgid(void **p, const struct ceph_spg *spgid)
2081 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
2082 encode_pgid(p, &spgid->pgid);
2083 ceph_encode_8(p, spgid->shard);
2086 static void encode_oloc(void **p, void *end,
2087 const struct ceph_object_locator *oloc)
2089 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
2090 ceph_encode_64(p, oloc->pool);
2091 ceph_encode_32(p, -1); /* preferred */
2092 ceph_encode_32(p, 0); /* key len */
2094 ceph_encode_string(p, end, oloc->pool_ns->str,
2095 oloc->pool_ns->len);
2097 ceph_encode_32(p, 0);
2100 static void encode_request_partial(struct ceph_osd_request *req,
2101 struct ceph_msg *msg)
2103 void *p = msg->front.iov_base;
2104 void *const end = p + msg->front_alloc_len;
2108 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2109 /* snapshots aren't writeable */
2110 WARN_ON(req->r_snapid != CEPH_NOSNAP);
2112 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2113 req->r_data_offset || req->r_snapc);
2116 setup_request_data(req);
2118 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
2119 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2120 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2121 ceph_encode_32(&p, req->r_flags);
2124 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
2125 memset(p, 0, sizeof(struct ceph_osd_reqid));
2126 p += sizeof(struct ceph_osd_reqid);
2129 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
2130 p += sizeof(struct ceph_blkin_trace_info);
2132 ceph_encode_32(&p, 0); /* client_inc, always 0 */
2133 ceph_encode_timespec64(p, &req->r_mtime);
2134 p += sizeof(struct ceph_timespec);
2136 encode_oloc(&p, end, &req->r_t.target_oloc);
2137 ceph_encode_string(&p, end, req->r_t.target_oid.name,
2138 req->r_t.target_oid.name_len);
2140 /* ops, can imply data */
2141 ceph_encode_16(&p, req->r_num_ops);
2142 for (i = 0; i < req->r_num_ops; i++) {
2143 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2144 p += sizeof(struct ceph_osd_op);
2147 ceph_encode_64(&p, req->r_snapid); /* snapid */
2149 ceph_encode_64(&p, req->r_snapc->seq);
2150 ceph_encode_32(&p, req->r_snapc->num_snaps);
2151 for (i = 0; i < req->r_snapc->num_snaps; i++)
2152 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2154 ceph_encode_64(&p, 0); /* snap_seq */
2155 ceph_encode_32(&p, 0); /* snaps len */
2158 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2159 BUG_ON(p > end - 8); /* space for features */
2161 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2162 /* front_len is finalized in encode_request_finish() */
2163 msg->front.iov_len = p - msg->front.iov_base;
2164 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2165 msg->hdr.data_len = cpu_to_le32(data_len);
2167 * The header "data_off" is a hint to the receiver allowing it
2168 * to align received data into its buffers such that there's no
2169 * need to re-copy it before writing it to disk (direct I/O).
2171 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2173 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2174 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2177 static void encode_request_finish(struct ceph_msg *msg)
2179 void *p = msg->front.iov_base;
2180 void *const partial_end = p + msg->front.iov_len;
2181 void *const end = p + msg->front_alloc_len;
2183 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2184 /* luminous OSD -- encode features and be done */
2186 ceph_encode_64(&p, msg->con->peer_features);
2189 char spgid[CEPH_ENCODING_START_BLK_LEN +
2190 CEPH_PGID_ENCODING_LEN + 1];
2194 char reqid[CEPH_ENCODING_START_BLK_LEN +
2195 sizeof(struct ceph_osd_reqid)];
2196 char trace[sizeof(struct ceph_blkin_trace_info)];
2198 struct ceph_timespec mtime;
2200 struct ceph_pg pgid;
2201 void *oloc, *oid, *tail;
2202 int oloc_len, oid_len, tail_len;
2206 * Pre-luminous OSD -- reencode v8 into v4 using @head
2207 * as a temporary buffer. Encode the raw PG; the rest
2208 * is just a matter of moving oloc, oid and tail blobs
2211 memcpy(&head, p, sizeof(head));
2215 p += CEPH_ENCODING_START_BLK_LEN;
2216 pgid.pool = ceph_decode_64(&p);
2217 p += 4 + 4; /* preferred, key len */
2218 len = ceph_decode_32(&p);
2219 p += len; /* nspace */
2220 oloc_len = p - oloc;
2223 len = ceph_decode_32(&p);
2228 tail_len = partial_end - p;
2230 p = msg->front.iov_base;
2231 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2232 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2233 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2234 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2236 /* reassert_version */
2237 memset(p, 0, sizeof(struct ceph_eversion));
2238 p += sizeof(struct ceph_eversion);
2241 memmove(p, oloc, oloc_len);
2244 pgid.seed = le32_to_cpu(head.hash);
2245 encode_pgid(&p, &pgid); /* raw pg */
2248 memmove(p, oid, oid_len);
2251 /* tail -- ops, snapid, snapc, retry_attempt */
2253 memmove(p, tail, tail_len);
2256 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2260 msg->front.iov_len = p - msg->front.iov_base;
2261 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2263 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2264 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2265 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2266 le16_to_cpu(msg->hdr.version));
2270 * @req has to be assigned a tid and registered.
2272 static void send_request(struct ceph_osd_request *req)
2274 struct ceph_osd *osd = req->r_osd;
2276 verify_osd_locked(osd);
2277 WARN_ON(osd->o_osd != req->r_t.osd);
2280 if (should_plug_request(req))
2284 * We may have a previously queued request message hanging
2285 * around. Cancel it to avoid corrupting the msgr.
2288 ceph_msg_revoke(req->r_request);
2290 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2291 if (req->r_attempts)
2292 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2294 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2296 encode_request_partial(req, req->r_request);
2298 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2299 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2300 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2301 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2304 req->r_t.paused = false;
2305 req->r_stamp = jiffies;
2308 req->r_sent = osd->o_incarnation;
2309 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2310 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2313 static void maybe_request_map(struct ceph_osd_client *osdc)
2315 bool continuous = false;
2317 verify_osdc_locked(osdc);
2318 WARN_ON(!osdc->osdmap->epoch);
2320 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2321 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2322 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2323 dout("%s osdc %p continuous\n", __func__, osdc);
2326 dout("%s osdc %p onetime\n", __func__, osdc);
2329 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2330 osdc->osdmap->epoch + 1, continuous))
2331 ceph_monc_renew_subs(&osdc->client->monc);
2334 static void complete_request(struct ceph_osd_request *req, int err);
2335 static void send_map_check(struct ceph_osd_request *req);
2337 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2339 struct ceph_osd_client *osdc = req->r_osdc;
2340 struct ceph_osd *osd;
2341 enum calc_target_result ct_res;
2343 bool need_send = false;
2344 bool promoted = false;
2346 WARN_ON(req->r_tid);
2347 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2350 ct_res = calc_target(osdc, &req->r_t, false);
2351 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2354 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2356 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2360 if (osdc->abort_err) {
2361 dout("req %p abort_err %d\n", req, osdc->abort_err);
2362 err = osdc->abort_err;
2363 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2364 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2365 osdc->epoch_barrier);
2366 req->r_t.paused = true;
2367 maybe_request_map(osdc);
2368 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2369 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2370 dout("req %p pausewr\n", req);
2371 req->r_t.paused = true;
2372 maybe_request_map(osdc);
2373 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2374 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2375 dout("req %p pauserd\n", req);
2376 req->r_t.paused = true;
2377 maybe_request_map(osdc);
2378 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2379 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2380 CEPH_OSD_FLAG_FULL_FORCE)) &&
2381 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2382 pool_full(osdc, req->r_t.base_oloc.pool))) {
2383 dout("req %p full/pool_full\n", req);
2384 if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
2387 pr_warn_ratelimited("FULL or reached pool quota\n");
2388 req->r_t.paused = true;
2389 maybe_request_map(osdc);
2391 } else if (!osd_homeless(osd)) {
2394 maybe_request_map(osdc);
2397 mutex_lock(&osd->lock);
2399 * Assign the tid atomically with send_request() to protect
2400 * multiple writes to the same object from racing with each
2401 * other, resulting in out of order ops on the OSDs.
2403 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2404 link_request(osd, req);
2408 complete_request(req, err);
2409 mutex_unlock(&osd->lock);
2411 if (!err && ct_res == CALC_TARGET_POOL_DNE)
2412 send_map_check(req);
2415 downgrade_write(&osdc->lock);
2419 up_read(&osdc->lock);
2420 down_write(&osdc->lock);
2426 static void account_request(struct ceph_osd_request *req)
2428 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2429 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2431 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2432 atomic_inc(&req->r_osdc->num_requests);
2434 req->r_start_stamp = jiffies;
2435 req->r_start_latency = ktime_get();
2438 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2440 ceph_osdc_get_request(req);
2441 account_request(req);
2442 __submit_request(req, wrlocked);
2445 static void finish_request(struct ceph_osd_request *req)
2447 struct ceph_osd_client *osdc = req->r_osdc;
2449 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2450 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2452 req->r_end_latency = ktime_get();
2455 unlink_request(req->r_osd, req);
2456 atomic_dec(&osdc->num_requests);
2459 * If an OSD has failed or returned and a request has been sent
2460 * twice, it's possible to get a reply and end up here while the
2461 * request message is queued for delivery. We will ignore the
2462 * reply, so not a big deal, but better to try and catch it.
2464 ceph_msg_revoke(req->r_request);
2465 ceph_msg_revoke_incoming(req->r_reply);
2468 static void __complete_request(struct ceph_osd_request *req)
2470 dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2471 req->r_tid, req->r_callback, req->r_result);
2473 if (req->r_callback)
2474 req->r_callback(req);
2475 complete_all(&req->r_completion);
2476 ceph_osdc_put_request(req);
2479 static void complete_request_workfn(struct work_struct *work)
2481 struct ceph_osd_request *req =
2482 container_of(work, struct ceph_osd_request, r_complete_work);
2484 __complete_request(req);
2488 * This is open-coded in handle_reply().
2490 static void complete_request(struct ceph_osd_request *req, int err)
2492 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2494 req->r_result = err;
2495 finish_request(req);
2497 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2498 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
2501 static void cancel_map_check(struct ceph_osd_request *req)
2503 struct ceph_osd_client *osdc = req->r_osdc;
2504 struct ceph_osd_request *lookup_req;
2506 verify_osdc_wrlocked(osdc);
2508 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2512 WARN_ON(lookup_req != req);
2513 erase_request_mc(&osdc->map_checks, req);
2514 ceph_osdc_put_request(req);
2517 static void cancel_request(struct ceph_osd_request *req)
2519 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2521 cancel_map_check(req);
2522 finish_request(req);
2523 complete_all(&req->r_completion);
2524 ceph_osdc_put_request(req);
2527 static void abort_request(struct ceph_osd_request *req, int err)
2529 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2531 cancel_map_check(req);
2532 complete_request(req, err);
2535 static int abort_fn(struct ceph_osd_request *req, void *arg)
2537 int err = *(int *)arg;
2539 abort_request(req, err);
2540 return 0; /* continue iteration */
2544 * Abort all in-flight requests with @err and arrange for all future
2545 * requests to be failed immediately.
2547 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2549 dout("%s osdc %p err %d\n", __func__, osdc, err);
2550 down_write(&osdc->lock);
2551 for_each_request(osdc, abort_fn, &err);
2552 osdc->abort_err = err;
2553 up_write(&osdc->lock);
2555 EXPORT_SYMBOL(ceph_osdc_abort_requests);
2557 void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
2559 down_write(&osdc->lock);
2560 osdc->abort_err = 0;
2561 up_write(&osdc->lock);
2563 EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
2565 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2567 if (likely(eb > osdc->epoch_barrier)) {
2568 dout("updating epoch_barrier from %u to %u\n",
2569 osdc->epoch_barrier, eb);
2570 osdc->epoch_barrier = eb;
2571 /* Request map if we're not to the barrier yet */
2572 if (eb > osdc->osdmap->epoch)
2573 maybe_request_map(osdc);
2577 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2579 down_read(&osdc->lock);
2580 if (unlikely(eb > osdc->epoch_barrier)) {
2581 up_read(&osdc->lock);
2582 down_write(&osdc->lock);
2583 update_epoch_barrier(osdc, eb);
2584 up_write(&osdc->lock);
2586 up_read(&osdc->lock);
2589 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2592 * We can end up releasing caps as a result of abort_request().
2593 * In that case, we probably want to ensure that the cap release message
2594 * has an updated epoch barrier in it, so set the epoch barrier prior to
2595 * aborting the first request.
2597 static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2599 struct ceph_osd_client *osdc = req->r_osdc;
2600 bool *victims = arg;
2602 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2603 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2604 pool_full(osdc, req->r_t.base_oloc.pool))) {
2606 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2609 abort_request(req, -ENOSPC);
2612 return 0; /* continue iteration */
2616 * Drop all pending requests that are stalled waiting on a full condition to
2617 * clear, and complete them with ENOSPC as the return code. Set the
2618 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2621 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2623 bool victims = false;
2625 if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2626 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
2627 for_each_request(osdc, abort_on_full_fn, &victims);
2630 static void check_pool_dne(struct ceph_osd_request *req)
2632 struct ceph_osd_client *osdc = req->r_osdc;
2633 struct ceph_osdmap *map = osdc->osdmap;
2635 verify_osdc_wrlocked(osdc);
2636 WARN_ON(!map->epoch);
2638 if (req->r_attempts) {
2640 * We sent a request earlier, which means that
2641 * previously the pool existed, and now it does not
2642 * (i.e., it was deleted).
2644 req->r_map_dne_bound = map->epoch;
2645 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2648 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2649 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2652 if (req->r_map_dne_bound) {
2653 if (map->epoch >= req->r_map_dne_bound) {
2654 /* we had a new enough map */
2655 pr_info_ratelimited("tid %llu pool does not exist\n",
2657 complete_request(req, -ENOENT);
2660 send_map_check(req);
2664 static void map_check_cb(struct ceph_mon_generic_request *greq)
2666 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2667 struct ceph_osd_request *req;
2668 u64 tid = greq->private_data;
2670 WARN_ON(greq->result || !greq->u.newest);
2672 down_write(&osdc->lock);
2673 req = lookup_request_mc(&osdc->map_checks, tid);
2675 dout("%s tid %llu dne\n", __func__, tid);
2679 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2680 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2681 if (!req->r_map_dne_bound)
2682 req->r_map_dne_bound = greq->u.newest;
2683 erase_request_mc(&osdc->map_checks, req);
2684 check_pool_dne(req);
2686 ceph_osdc_put_request(req);
2688 up_write(&osdc->lock);
2691 static void send_map_check(struct ceph_osd_request *req)
2693 struct ceph_osd_client *osdc = req->r_osdc;
2694 struct ceph_osd_request *lookup_req;
2697 verify_osdc_wrlocked(osdc);
2699 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2701 WARN_ON(lookup_req != req);
2705 ceph_osdc_get_request(req);
2706 insert_request_mc(&osdc->map_checks, req);
2707 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2708 map_check_cb, req->r_tid);
2713 * lingering requests, watch/notify v2 infrastructure
2715 static void linger_release(struct kref *kref)
2717 struct ceph_osd_linger_request *lreq =
2718 container_of(kref, struct ceph_osd_linger_request, kref);
2720 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2721 lreq->reg_req, lreq->ping_req);
2722 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2723 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2724 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2725 WARN_ON(!list_empty(&lreq->scan_item));
2726 WARN_ON(!list_empty(&lreq->pending_lworks));
2730 ceph_osdc_put_request(lreq->reg_req);
2732 ceph_osdc_put_request(lreq->ping_req);
2733 target_destroy(&lreq->t);
2737 static void linger_put(struct ceph_osd_linger_request *lreq)
2740 kref_put(&lreq->kref, linger_release);
2743 static struct ceph_osd_linger_request *
2744 linger_get(struct ceph_osd_linger_request *lreq)
2746 kref_get(&lreq->kref);
2750 static struct ceph_osd_linger_request *
2751 linger_alloc(struct ceph_osd_client *osdc)
2753 struct ceph_osd_linger_request *lreq;
2755 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2759 kref_init(&lreq->kref);
2760 mutex_init(&lreq->lock);
2761 RB_CLEAR_NODE(&lreq->node);
2762 RB_CLEAR_NODE(&lreq->osdc_node);
2763 RB_CLEAR_NODE(&lreq->mc_node);
2764 INIT_LIST_HEAD(&lreq->scan_item);
2765 INIT_LIST_HEAD(&lreq->pending_lworks);
2766 init_completion(&lreq->reg_commit_wait);
2767 init_completion(&lreq->notify_finish_wait);
2770 target_init(&lreq->t);
2772 dout("%s lreq %p\n", __func__, lreq);
2776 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2777 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2778 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2781 * Create linger request <-> OSD session relation.
2783 * @lreq has to be registered, @osd may be homeless.
2785 static void link_linger(struct ceph_osd *osd,
2786 struct ceph_osd_linger_request *lreq)
2788 verify_osd_locked(osd);
2789 WARN_ON(!lreq->linger_id || lreq->osd);
2790 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2791 osd->o_osd, lreq, lreq->linger_id);
2793 if (!osd_homeless(osd))
2794 __remove_osd_from_lru(osd);
2796 atomic_inc(&osd->o_osdc->num_homeless);
2799 insert_linger(&osd->o_linger_requests, lreq);
2803 static void unlink_linger(struct ceph_osd *osd,
2804 struct ceph_osd_linger_request *lreq)
2806 verify_osd_locked(osd);
2807 WARN_ON(lreq->osd != osd);
2808 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2809 osd->o_osd, lreq, lreq->linger_id);
2812 erase_linger(&osd->o_linger_requests, lreq);
2815 if (!osd_homeless(osd))
2816 maybe_move_osd_to_lru(osd);
2818 atomic_dec(&osd->o_osdc->num_homeless);
2821 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2823 verify_osdc_locked(lreq->osdc);
2825 return !RB_EMPTY_NODE(&lreq->osdc_node);
2828 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2830 struct ceph_osd_client *osdc = lreq->osdc;
2833 down_read(&osdc->lock);
2834 registered = __linger_registered(lreq);
2835 up_read(&osdc->lock);
2840 static void linger_register(struct ceph_osd_linger_request *lreq)
2842 struct ceph_osd_client *osdc = lreq->osdc;
2844 verify_osdc_wrlocked(osdc);
2845 WARN_ON(lreq->linger_id);
2848 lreq->linger_id = ++osdc->last_linger_id;
2849 insert_linger_osdc(&osdc->linger_requests, lreq);
2852 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2854 struct ceph_osd_client *osdc = lreq->osdc;
2856 verify_osdc_wrlocked(osdc);
2858 erase_linger_osdc(&osdc->linger_requests, lreq);
2862 static void cancel_linger_request(struct ceph_osd_request *req)
2864 struct ceph_osd_linger_request *lreq = req->r_priv;
2866 WARN_ON(!req->r_linger);
2867 cancel_request(req);
2871 struct linger_work {
2872 struct work_struct work;
2873 struct ceph_osd_linger_request *lreq;
2874 struct list_head pending_item;
2875 unsigned long queued_stamp;
2881 void *payload; /* points into @msg front */
2884 struct ceph_msg *msg; /* for ceph_msg_put() */
2892 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2895 struct linger_work *lwork;
2897 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2901 INIT_WORK(&lwork->work, workfn);
2902 INIT_LIST_HEAD(&lwork->pending_item);
2903 lwork->lreq = linger_get(lreq);
2908 static void lwork_free(struct linger_work *lwork)
2910 struct ceph_osd_linger_request *lreq = lwork->lreq;
2912 mutex_lock(&lreq->lock);
2913 list_del(&lwork->pending_item);
2914 mutex_unlock(&lreq->lock);
2920 static void lwork_queue(struct linger_work *lwork)
2922 struct ceph_osd_linger_request *lreq = lwork->lreq;
2923 struct ceph_osd_client *osdc = lreq->osdc;
2925 verify_lreq_locked(lreq);
2926 WARN_ON(!list_empty(&lwork->pending_item));
2928 lwork->queued_stamp = jiffies;
2929 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2930 queue_work(osdc->notify_wq, &lwork->work);
2933 static void do_watch_notify(struct work_struct *w)
2935 struct linger_work *lwork = container_of(w, struct linger_work, work);
2936 struct ceph_osd_linger_request *lreq = lwork->lreq;
2938 if (!linger_registered(lreq)) {
2939 dout("%s lreq %p not registered\n", __func__, lreq);
2943 WARN_ON(!lreq->is_watch);
2944 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2945 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2946 lwork->notify.payload_len);
2947 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2948 lwork->notify.notifier_id, lwork->notify.payload,
2949 lwork->notify.payload_len);
2952 ceph_msg_put(lwork->notify.msg);
2956 static void do_watch_error(struct work_struct *w)
2958 struct linger_work *lwork = container_of(w, struct linger_work, work);
2959 struct ceph_osd_linger_request *lreq = lwork->lreq;
2961 if (!linger_registered(lreq)) {
2962 dout("%s lreq %p not registered\n", __func__, lreq);
2966 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2967 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2973 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2975 struct linger_work *lwork;
2977 lwork = lwork_alloc(lreq, do_watch_error);
2979 pr_err("failed to allocate error-lwork\n");
2983 lwork->error.err = lreq->last_error;
2987 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2990 if (!completion_done(&lreq->reg_commit_wait)) {
2991 lreq->reg_commit_error = (result <= 0 ? result : 0);
2992 complete_all(&lreq->reg_commit_wait);
2996 static void linger_commit_cb(struct ceph_osd_request *req)
2998 struct ceph_osd_linger_request *lreq = req->r_priv;
3000 mutex_lock(&lreq->lock);
3001 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
3002 lreq->linger_id, req->r_result);
3003 linger_reg_commit_complete(lreq, req->r_result);
3004 lreq->committed = true;
3006 if (!lreq->is_watch) {
3007 struct ceph_osd_data *osd_data =
3008 osd_req_op_data(req, 0, notify, response_data);
3009 void *p = page_address(osd_data->pages[0]);
3011 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
3012 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
3014 /* make note of the notify_id */
3015 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
3016 lreq->notify_id = ceph_decode_64(&p);
3017 dout("lreq %p notify_id %llu\n", lreq,
3020 dout("lreq %p no notify_id\n", lreq);
3024 mutex_unlock(&lreq->lock);
3028 static int normalize_watch_error(int err)
3031 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
3032 * notification and a failure to reconnect because we raced with
3033 * the delete appear the same to the user.
3041 static void linger_reconnect_cb(struct ceph_osd_request *req)
3043 struct ceph_osd_linger_request *lreq = req->r_priv;
3045 mutex_lock(&lreq->lock);
3046 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
3047 lreq, lreq->linger_id, req->r_result, lreq->last_error);
3048 if (req->r_result < 0) {
3049 if (!lreq->last_error) {
3050 lreq->last_error = normalize_watch_error(req->r_result);
3051 queue_watch_error(lreq);
3055 mutex_unlock(&lreq->lock);
3059 static void send_linger(struct ceph_osd_linger_request *lreq)
3061 struct ceph_osd_request *req = lreq->reg_req;
3062 struct ceph_osd_req_op *op = &req->r_ops[0];
3064 verify_osdc_wrlocked(req->r_osdc);
3065 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3068 cancel_linger_request(req);
3070 request_reinit(req);
3071 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3072 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3073 req->r_flags = lreq->t.flags;
3074 req->r_mtime = lreq->mtime;
3076 mutex_lock(&lreq->lock);
3077 if (lreq->is_watch && lreq->committed) {
3078 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3079 op->watch.cookie != lreq->linger_id);
3080 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
3081 op->watch.gen = ++lreq->register_gen;
3082 dout("lreq %p reconnect register_gen %u\n", lreq,
3084 req->r_callback = linger_reconnect_cb;
3086 if (!lreq->is_watch)
3087 lreq->notify_id = 0;
3089 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
3090 dout("lreq %p register\n", lreq);
3091 req->r_callback = linger_commit_cb;
3093 mutex_unlock(&lreq->lock);
3095 req->r_priv = linger_get(lreq);
3096 req->r_linger = true;
3098 submit_request(req, true);
3101 static void linger_ping_cb(struct ceph_osd_request *req)
3103 struct ceph_osd_linger_request *lreq = req->r_priv;
3105 mutex_lock(&lreq->lock);
3106 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3107 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3109 if (lreq->register_gen == req->r_ops[0].watch.gen) {
3110 if (!req->r_result) {
3111 lreq->watch_valid_thru = lreq->ping_sent;
3112 } else if (!lreq->last_error) {
3113 lreq->last_error = normalize_watch_error(req->r_result);
3114 queue_watch_error(lreq);
3117 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3118 lreq->register_gen, req->r_ops[0].watch.gen);
3121 mutex_unlock(&lreq->lock);
3125 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3127 struct ceph_osd_client *osdc = lreq->osdc;
3128 struct ceph_osd_request *req = lreq->ping_req;
3129 struct ceph_osd_req_op *op = &req->r_ops[0];
3131 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3132 dout("%s PAUSERD\n", __func__);
3136 lreq->ping_sent = jiffies;
3137 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3138 __func__, lreq, lreq->linger_id, lreq->ping_sent,
3139 lreq->register_gen);
3142 cancel_linger_request(req);
3144 request_reinit(req);
3145 target_copy(&req->r_t, &lreq->t);
3147 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3148 op->watch.cookie != lreq->linger_id ||
3149 op->watch.op != CEPH_OSD_WATCH_OP_PING);
3150 op->watch.gen = lreq->register_gen;
3151 req->r_callback = linger_ping_cb;
3152 req->r_priv = linger_get(lreq);
3153 req->r_linger = true;
3155 ceph_osdc_get_request(req);
3156 account_request(req);
3157 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3158 link_request(lreq->osd, req);
3162 static void linger_submit(struct ceph_osd_linger_request *lreq)
3164 struct ceph_osd_client *osdc = lreq->osdc;
3165 struct ceph_osd *osd;
3167 down_write(&osdc->lock);
3168 linger_register(lreq);
3169 if (lreq->is_watch) {
3170 lreq->reg_req->r_ops[0].watch.cookie = lreq->linger_id;
3171 lreq->ping_req->r_ops[0].watch.cookie = lreq->linger_id;
3173 lreq->reg_req->r_ops[0].notify.cookie = lreq->linger_id;
3176 calc_target(osdc, &lreq->t, false);
3177 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3178 link_linger(osd, lreq);
3181 up_write(&osdc->lock);
3184 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3186 struct ceph_osd_client *osdc = lreq->osdc;
3187 struct ceph_osd_linger_request *lookup_lreq;
3189 verify_osdc_wrlocked(osdc);
3191 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3196 WARN_ON(lookup_lreq != lreq);
3197 erase_linger_mc(&osdc->linger_map_checks, lreq);
3202 * @lreq has to be both registered and linked.
3204 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3206 if (lreq->is_watch && lreq->ping_req->r_osd)
3207 cancel_linger_request(lreq->ping_req);
3208 if (lreq->reg_req->r_osd)
3209 cancel_linger_request(lreq->reg_req);
3210 cancel_linger_map_check(lreq);
3211 unlink_linger(lreq->osd, lreq);
3212 linger_unregister(lreq);
3215 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3217 struct ceph_osd_client *osdc = lreq->osdc;
3219 down_write(&osdc->lock);
3220 if (__linger_registered(lreq))
3221 __linger_cancel(lreq);
3222 up_write(&osdc->lock);
3225 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3227 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3229 struct ceph_osd_client *osdc = lreq->osdc;
3230 struct ceph_osdmap *map = osdc->osdmap;
3232 verify_osdc_wrlocked(osdc);
3233 WARN_ON(!map->epoch);
3235 if (lreq->register_gen) {
3236 lreq->map_dne_bound = map->epoch;
3237 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3238 lreq, lreq->linger_id);
3240 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3241 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3245 if (lreq->map_dne_bound) {
3246 if (map->epoch >= lreq->map_dne_bound) {
3247 /* we had a new enough map */
3248 pr_info("linger_id %llu pool does not exist\n",
3250 linger_reg_commit_complete(lreq, -ENOENT);
3251 __linger_cancel(lreq);
3254 send_linger_map_check(lreq);
3258 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3260 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3261 struct ceph_osd_linger_request *lreq;
3262 u64 linger_id = greq->private_data;
3264 WARN_ON(greq->result || !greq->u.newest);
3266 down_write(&osdc->lock);
3267 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3269 dout("%s linger_id %llu dne\n", __func__, linger_id);
3273 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3274 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3276 if (!lreq->map_dne_bound)
3277 lreq->map_dne_bound = greq->u.newest;
3278 erase_linger_mc(&osdc->linger_map_checks, lreq);
3279 check_linger_pool_dne(lreq);
3283 up_write(&osdc->lock);
3286 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3288 struct ceph_osd_client *osdc = lreq->osdc;
3289 struct ceph_osd_linger_request *lookup_lreq;
3292 verify_osdc_wrlocked(osdc);
3294 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3297 WARN_ON(lookup_lreq != lreq);
3302 insert_linger_mc(&osdc->linger_map_checks, lreq);
3303 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3304 linger_map_check_cb, lreq->linger_id);
3308 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3312 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3313 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3314 return ret ?: lreq->reg_commit_error;
3317 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3321 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3322 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3323 return ret ?: lreq->notify_finish_error;
3327 * Timeout callback, called every N seconds. When 1 or more OSD
3328 * requests has been active for more than N seconds, we send a keepalive
3329 * (tag + timestamp) to its OSD to ensure any communications channel
3330 * reset is detected.
3332 static void handle_timeout(struct work_struct *work)
3334 struct ceph_osd_client *osdc =
3335 container_of(work, struct ceph_osd_client, timeout_work.work);
3336 struct ceph_options *opts = osdc->client->options;
3337 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3338 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3339 LIST_HEAD(slow_osds);
3340 struct rb_node *n, *p;
3342 dout("%s osdc %p\n", __func__, osdc);
3343 down_write(&osdc->lock);
3346 * ping osds that are a bit slow. this ensures that if there
3347 * is a break in the TCP connection we will notice, and reopen
3348 * a connection with that osd (from the fault callback).
3350 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3351 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3354 for (p = rb_first(&osd->o_requests); p; ) {
3355 struct ceph_osd_request *req =
3356 rb_entry(p, struct ceph_osd_request, r_node);
3358 p = rb_next(p); /* abort_request() */
3360 if (time_before(req->r_stamp, cutoff)) {
3361 dout(" req %p tid %llu on osd%d is laggy\n",
3362 req, req->r_tid, osd->o_osd);
3365 if (opts->osd_request_timeout &&
3366 time_before(req->r_start_stamp, expiry_cutoff)) {
3367 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3368 req->r_tid, osd->o_osd);
3369 abort_request(req, -ETIMEDOUT);
3372 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3373 struct ceph_osd_linger_request *lreq =
3374 rb_entry(p, struct ceph_osd_linger_request, node);
3376 dout(" lreq %p linger_id %llu is served by osd%d\n",
3377 lreq, lreq->linger_id, osd->o_osd);
3380 mutex_lock(&lreq->lock);
3381 if (lreq->is_watch && lreq->committed && !lreq->last_error)
3382 send_linger_ping(lreq);
3383 mutex_unlock(&lreq->lock);
3387 list_move_tail(&osd->o_keepalive_item, &slow_osds);
3390 if (opts->osd_request_timeout) {
3391 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3392 struct ceph_osd_request *req =
3393 rb_entry(p, struct ceph_osd_request, r_node);
3395 p = rb_next(p); /* abort_request() */
3397 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3398 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3399 req->r_tid, osdc->homeless_osd.o_osd);
3400 abort_request(req, -ETIMEDOUT);
3405 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3406 maybe_request_map(osdc);
3408 while (!list_empty(&slow_osds)) {
3409 struct ceph_osd *osd = list_first_entry(&slow_osds,
3412 list_del_init(&osd->o_keepalive_item);
3413 ceph_con_keepalive(&osd->o_con);
3416 up_write(&osdc->lock);
3417 schedule_delayed_work(&osdc->timeout_work,
3418 osdc->client->options->osd_keepalive_timeout);
3421 static void handle_osds_timeout(struct work_struct *work)
3423 struct ceph_osd_client *osdc =
3424 container_of(work, struct ceph_osd_client,
3425 osds_timeout_work.work);
3426 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3427 struct ceph_osd *osd, *nosd;
3429 dout("%s osdc %p\n", __func__, osdc);
3430 down_write(&osdc->lock);
3431 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3432 if (time_before(jiffies, osd->lru_ttl))
3435 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3436 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3440 up_write(&osdc->lock);
3441 schedule_delayed_work(&osdc->osds_timeout_work,
3442 round_jiffies_relative(delay));
3445 static int ceph_oloc_decode(void **p, void *end,
3446 struct ceph_object_locator *oloc)
3448 u8 struct_v, struct_cv;
3453 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3454 struct_v = ceph_decode_8(p);
3455 struct_cv = ceph_decode_8(p);
3457 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3458 struct_v, struct_cv);
3461 if (struct_cv > 6) {
3462 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3463 struct_v, struct_cv);
3466 len = ceph_decode_32(p);
3467 ceph_decode_need(p, end, len, e_inval);
3468 struct_end = *p + len;
3470 oloc->pool = ceph_decode_64(p);
3471 *p += 4; /* skip preferred */
3473 len = ceph_decode_32(p);
3475 pr_warn("ceph_object_locator::key is set\n");
3479 if (struct_v >= 5) {
3480 bool changed = false;
3482 len = ceph_decode_32(p);
3484 ceph_decode_need(p, end, len, e_inval);
3485 if (!oloc->pool_ns ||
3486 ceph_compare_string(oloc->pool_ns, *p, len))
3494 /* redirect changes namespace */
3495 pr_warn("ceph_object_locator::nspace is changed\n");
3500 if (struct_v >= 6) {
3501 s64 hash = ceph_decode_64(p);
3503 pr_warn("ceph_object_locator::hash is set\n");
3518 static int ceph_redirect_decode(void **p, void *end,
3519 struct ceph_request_redirect *redir)
3521 u8 struct_v, struct_cv;
3526 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3527 struct_v = ceph_decode_8(p);
3528 struct_cv = ceph_decode_8(p);
3529 if (struct_cv > 1) {
3530 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3531 struct_v, struct_cv);
3534 len = ceph_decode_32(p);
3535 ceph_decode_need(p, end, len, e_inval);
3536 struct_end = *p + len;
3538 ret = ceph_oloc_decode(p, end, &redir->oloc);
3542 len = ceph_decode_32(p);
3544 pr_warn("ceph_request_redirect::object_name is set\n");
3558 struct MOSDOpReply {
3559 struct ceph_pg pgid;
3564 u32 outdata_len[CEPH_OSD_MAX_OPS];
3565 s32 rval[CEPH_OSD_MAX_OPS];
3567 struct ceph_eversion replay_version;
3569 struct ceph_request_redirect redirect;
3572 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3574 void *p = msg->front.iov_base;
3575 void *const end = p + msg->front.iov_len;
3576 u16 version = le16_to_cpu(msg->hdr.version);
3577 struct ceph_eversion bad_replay_version;
3583 ceph_decode_32_safe(&p, end, len, e_inval);
3584 ceph_decode_need(&p, end, len, e_inval);
3585 p += len; /* skip oid */
3587 ret = ceph_decode_pgid(&p, end, &m->pgid);
3591 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3592 ceph_decode_32_safe(&p, end, m->result, e_inval);
3593 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3594 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3595 p += sizeof(bad_replay_version);
3596 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3598 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3599 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3602 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3604 for (i = 0; i < m->num_ops; i++) {
3605 struct ceph_osd_op *op = p;
3607 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3611 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3612 for (i = 0; i < m->num_ops; i++)
3613 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3616 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3617 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3618 p += sizeof(m->replay_version);
3619 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3621 m->replay_version = bad_replay_version; /* struct */
3622 m->user_version = le64_to_cpu(m->replay_version.version);
3627 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3635 ret = ceph_redirect_decode(&p, end, &m->redirect);
3639 ceph_oloc_init(&m->redirect.oloc);
3649 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3652 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3654 struct ceph_osd_client *osdc = osd->o_osdc;
3655 struct ceph_osd_request *req;
3656 struct MOSDOpReply m;
3657 u64 tid = le64_to_cpu(msg->hdr.tid);
3662 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3664 down_read(&osdc->lock);
3665 if (!osd_registered(osd)) {
3666 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3667 goto out_unlock_osdc;
3669 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3671 mutex_lock(&osd->lock);
3672 req = lookup_request(&osd->o_requests, tid);
3674 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3675 goto out_unlock_session;
3678 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3679 ret = decode_MOSDOpReply(msg, &m);
3680 m.redirect.oloc.pool_ns = NULL;
3682 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3687 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3688 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3689 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3690 le64_to_cpu(m.replay_version.version), m.user_version);
3692 if (m.retry_attempt >= 0) {
3693 if (m.retry_attempt != req->r_attempts - 1) {
3694 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3695 req, req->r_tid, m.retry_attempt,
3696 req->r_attempts - 1);
3697 goto out_unlock_session;
3700 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3703 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3704 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3705 m.redirect.oloc.pool);
3706 unlink_request(osd, req);
3707 mutex_unlock(&osd->lock);
3710 * Not ceph_oloc_copy() - changing pool_ns is not
3713 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3714 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
3715 CEPH_OSD_FLAG_IGNORE_OVERLAY |
3716 CEPH_OSD_FLAG_IGNORE_CACHE;
3718 __submit_request(req, false);
3719 goto out_unlock_osdc;
3722 if (m.result == -EAGAIN) {
3723 dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
3724 unlink_request(osd, req);
3725 mutex_unlock(&osd->lock);
3728 * The object is missing on the replica or not (yet)
3729 * readable. Clear pgid to force a resend to the primary
3730 * via legacy_change.
3732 req->r_t.pgid.pool = 0;
3733 req->r_t.pgid.seed = 0;
3734 WARN_ON(!req->r_t.used_replica);
3735 req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
3736 CEPH_OSD_FLAG_LOCALIZE_READS);
3738 __submit_request(req, false);
3739 goto out_unlock_osdc;
3742 if (m.num_ops != req->r_num_ops) {
3743 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3744 req->r_num_ops, req->r_tid);
3747 for (i = 0; i < req->r_num_ops; i++) {
3748 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3749 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3750 req->r_ops[i].rval = m.rval[i];
3751 req->r_ops[i].outdata_len = m.outdata_len[i];
3752 data_len += m.outdata_len[i];
3754 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3755 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3756 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3759 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3760 req, req->r_tid, m.result, data_len);
3763 * Since we only ever request ONDISK, we should only ever get
3764 * one (type of) reply back.
3766 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3767 req->r_result = m.result ?: data_len;
3768 finish_request(req);
3769 mutex_unlock(&osd->lock);
3770 up_read(&osdc->lock);
3772 __complete_request(req);
3776 complete_request(req, -EIO);
3778 mutex_unlock(&osd->lock);
3780 up_read(&osdc->lock);
3783 static void set_pool_was_full(struct ceph_osd_client *osdc)
3787 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3788 struct ceph_pg_pool_info *pi =
3789 rb_entry(n, struct ceph_pg_pool_info, node);
3791 pi->was_full = __pool_full(pi);
3795 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3797 struct ceph_pg_pool_info *pi;
3799 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3803 return pi->was_full && !__pool_full(pi);
3806 static enum calc_target_result
3807 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3809 struct ceph_osd_client *osdc = lreq->osdc;
3810 enum calc_target_result ct_res;
3812 ct_res = calc_target(osdc, &lreq->t, true);
3813 if (ct_res == CALC_TARGET_NEED_RESEND) {
3814 struct ceph_osd *osd;
3816 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3817 if (osd != lreq->osd) {
3818 unlink_linger(lreq->osd, lreq);
3819 link_linger(osd, lreq);
3827 * Requeue requests whose mapping to an OSD has changed.
3829 static void scan_requests(struct ceph_osd *osd,
3832 bool check_pool_cleared_full,
3833 struct rb_root *need_resend,
3834 struct list_head *need_resend_linger)
3836 struct ceph_osd_client *osdc = osd->o_osdc;
3838 bool force_resend_writes;
3840 for (n = rb_first(&osd->o_linger_requests); n; ) {
3841 struct ceph_osd_linger_request *lreq =
3842 rb_entry(n, struct ceph_osd_linger_request, node);
3843 enum calc_target_result ct_res;
3845 n = rb_next(n); /* recalc_linger_target() */
3847 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3849 ct_res = recalc_linger_target(lreq);
3851 case CALC_TARGET_NO_ACTION:
3852 force_resend_writes = cleared_full ||
3853 (check_pool_cleared_full &&
3854 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3855 if (!force_resend && !force_resend_writes)
3859 case CALC_TARGET_NEED_RESEND:
3860 cancel_linger_map_check(lreq);
3862 * scan_requests() for the previous epoch(s)
3863 * may have already added it to the list, since
3864 * it's not unlinked here.
3866 if (list_empty(&lreq->scan_item))
3867 list_add_tail(&lreq->scan_item, need_resend_linger);
3869 case CALC_TARGET_POOL_DNE:
3870 list_del_init(&lreq->scan_item);
3871 check_linger_pool_dne(lreq);
3876 for (n = rb_first(&osd->o_requests); n; ) {
3877 struct ceph_osd_request *req =
3878 rb_entry(n, struct ceph_osd_request, r_node);
3879 enum calc_target_result ct_res;
3881 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3883 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3884 ct_res = calc_target(osdc, &req->r_t, false);
3886 case CALC_TARGET_NO_ACTION:
3887 force_resend_writes = cleared_full ||
3888 (check_pool_cleared_full &&
3889 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3890 if (!force_resend &&
3891 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3892 !force_resend_writes))
3896 case CALC_TARGET_NEED_RESEND:
3897 cancel_map_check(req);
3898 unlink_request(osd, req);
3899 insert_request(need_resend, req);
3901 case CALC_TARGET_POOL_DNE:
3902 check_pool_dne(req);
3908 static int handle_one_map(struct ceph_osd_client *osdc,
3909 void *p, void *end, bool incremental,
3910 struct rb_root *need_resend,
3911 struct list_head *need_resend_linger)
3913 struct ceph_osdmap *newmap;
3915 bool skipped_map = false;
3918 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3919 set_pool_was_full(osdc);
3922 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3924 newmap = ceph_osdmap_decode(&p, end);
3926 return PTR_ERR(newmap);
3928 if (newmap != osdc->osdmap) {
3930 * Preserve ->was_full before destroying the old map.
3931 * For pools that weren't in the old map, ->was_full
3934 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3935 struct ceph_pg_pool_info *pi =
3936 rb_entry(n, struct ceph_pg_pool_info, node);
3937 struct ceph_pg_pool_info *old_pi;
3939 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3941 pi->was_full = old_pi->was_full;
3943 WARN_ON(pi->was_full);
3946 if (osdc->osdmap->epoch &&
3947 osdc->osdmap->epoch + 1 < newmap->epoch) {
3948 WARN_ON(incremental);
3952 ceph_osdmap_destroy(osdc->osdmap);
3953 osdc->osdmap = newmap;
3956 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3957 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3958 need_resend, need_resend_linger);
3960 for (n = rb_first(&osdc->osds); n; ) {
3961 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3963 n = rb_next(n); /* close_osd() */
3965 scan_requests(osd, skipped_map, was_full, true, need_resend,
3966 need_resend_linger);
3967 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3968 memcmp(&osd->o_con.peer_addr,
3969 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3970 sizeof(struct ceph_entity_addr)))
3977 static void kick_requests(struct ceph_osd_client *osdc,
3978 struct rb_root *need_resend,
3979 struct list_head *need_resend_linger)
3981 struct ceph_osd_linger_request *lreq, *nlreq;
3982 enum calc_target_result ct_res;
3985 /* make sure need_resend targets reflect latest map */
3986 for (n = rb_first(need_resend); n; ) {
3987 struct ceph_osd_request *req =
3988 rb_entry(n, struct ceph_osd_request, r_node);
3992 if (req->r_t.epoch < osdc->osdmap->epoch) {
3993 ct_res = calc_target(osdc, &req->r_t, false);
3994 if (ct_res == CALC_TARGET_POOL_DNE) {
3995 erase_request(need_resend, req);
3996 check_pool_dne(req);
4001 for (n = rb_first(need_resend); n; ) {
4002 struct ceph_osd_request *req =
4003 rb_entry(n, struct ceph_osd_request, r_node);
4004 struct ceph_osd *osd;
4007 erase_request(need_resend, req); /* before link_request() */
4009 osd = lookup_create_osd(osdc, req->r_t.osd, true);
4010 link_request(osd, req);
4011 if (!req->r_linger) {
4012 if (!osd_homeless(osd) && !req->r_t.paused)
4015 cancel_linger_request(req);
4019 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
4020 if (!osd_homeless(lreq->osd))
4023 list_del_init(&lreq->scan_item);
4028 * Process updated osd map.
4030 * The message contains any number of incremental and full maps, normally
4031 * indicating some sort of topology change in the cluster. Kick requests
4032 * off to different OSDs as needed.
4034 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
4036 void *p = msg->front.iov_base;
4037 void *const end = p + msg->front.iov_len;
4038 u32 nr_maps, maplen;
4040 struct ceph_fsid fsid;
4041 struct rb_root need_resend = RB_ROOT;
4042 LIST_HEAD(need_resend_linger);
4043 bool handled_incremental = false;
4044 bool was_pauserd, was_pausewr;
4045 bool pauserd, pausewr;
4048 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
4049 down_write(&osdc->lock);
4052 ceph_decode_need(&p, end, sizeof(fsid), bad);
4053 ceph_decode_copy(&p, &fsid, sizeof(fsid));
4054 if (ceph_check_fsid(osdc->client, &fsid) < 0)
4057 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4058 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4059 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
4060 have_pool_full(osdc);
4062 /* incremental maps */
4063 ceph_decode_32_safe(&p, end, nr_maps, bad);
4064 dout(" %d inc maps\n", nr_maps);
4065 while (nr_maps > 0) {
4066 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
4067 epoch = ceph_decode_32(&p);
4068 maplen = ceph_decode_32(&p);
4069 ceph_decode_need(&p, end, maplen, bad);
4070 if (osdc->osdmap->epoch &&
4071 osdc->osdmap->epoch + 1 == epoch) {
4072 dout("applying incremental map %u len %d\n",
4074 err = handle_one_map(osdc, p, p + maplen, true,
4075 &need_resend, &need_resend_linger);
4078 handled_incremental = true;
4080 dout("ignoring incremental map %u len %d\n",
4086 if (handled_incremental)
4090 ceph_decode_32_safe(&p, end, nr_maps, bad);
4091 dout(" %d full maps\n", nr_maps);
4093 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
4094 epoch = ceph_decode_32(&p);
4095 maplen = ceph_decode_32(&p);
4096 ceph_decode_need(&p, end, maplen, bad);
4098 dout("skipping non-latest full map %u len %d\n",
4100 } else if (osdc->osdmap->epoch >= epoch) {
4101 dout("skipping full map %u len %d, "
4102 "older than our %u\n", epoch, maplen,
4103 osdc->osdmap->epoch);
4105 dout("taking full map %u len %d\n", epoch, maplen);
4106 err = handle_one_map(osdc, p, p + maplen, false,
4107 &need_resend, &need_resend_linger);
4117 * subscribe to subsequent osdmap updates if full to ensure
4118 * we find out when we are no longer full and stop returning
4121 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4122 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4123 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
4124 have_pool_full(osdc);
4125 if (was_pauserd || was_pausewr || pauserd || pausewr ||
4126 osdc->osdmap->epoch < osdc->epoch_barrier)
4127 maybe_request_map(osdc);
4129 kick_requests(osdc, &need_resend, &need_resend_linger);
4131 ceph_osdc_abort_on_full(osdc);
4132 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
4133 osdc->osdmap->epoch);
4134 up_write(&osdc->lock);
4135 wake_up_all(&osdc->client->auth_wq);
4139 pr_err("osdc handle_map corrupt msg\n");
4141 up_write(&osdc->lock);
4145 * Resubmit requests pending on the given osd.
4147 static void kick_osd_requests(struct ceph_osd *osd)
4151 clear_backoffs(osd);
4153 for (n = rb_first(&osd->o_requests); n; ) {
4154 struct ceph_osd_request *req =
4155 rb_entry(n, struct ceph_osd_request, r_node);
4157 n = rb_next(n); /* cancel_linger_request() */
4159 if (!req->r_linger) {
4160 if (!req->r_t.paused)
4163 cancel_linger_request(req);
4166 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4167 struct ceph_osd_linger_request *lreq =
4168 rb_entry(n, struct ceph_osd_linger_request, node);
4175 * If the osd connection drops, we need to resubmit all requests.
4177 static void osd_fault(struct ceph_connection *con)
4179 struct ceph_osd *osd = con->private;
4180 struct ceph_osd_client *osdc = osd->o_osdc;
4182 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4184 down_write(&osdc->lock);
4185 if (!osd_registered(osd)) {
4186 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4190 if (!reopen_osd(osd))
4191 kick_osd_requests(osd);
4192 maybe_request_map(osdc);
4195 up_write(&osdc->lock);
4198 struct MOSDBackoff {
4199 struct ceph_spg spgid;
4203 struct ceph_hobject_id *begin;
4204 struct ceph_hobject_id *end;
4207 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4209 void *p = msg->front.iov_base;
4210 void *const end = p + msg->front.iov_len;
4215 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4219 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4223 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4224 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4225 ceph_decode_8_safe(&p, end, m->op, e_inval);
4226 ceph_decode_64_safe(&p, end, m->id, e_inval);
4228 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4232 ret = decode_hoid(&p, end, m->begin);
4234 free_hoid(m->begin);
4238 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4240 free_hoid(m->begin);
4244 ret = decode_hoid(&p, end, m->end);
4246 free_hoid(m->begin);
4257 static struct ceph_msg *create_backoff_message(
4258 const struct ceph_osd_backoff *backoff,
4261 struct ceph_msg *msg;
4265 msg_size = CEPH_ENCODING_START_BLK_LEN +
4266 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4267 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4268 msg_size += CEPH_ENCODING_START_BLK_LEN +
4269 hoid_encoding_size(backoff->begin);
4270 msg_size += CEPH_ENCODING_START_BLK_LEN +
4271 hoid_encoding_size(backoff->end);
4273 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4277 p = msg->front.iov_base;
4278 end = p + msg->front_alloc_len;
4280 encode_spgid(&p, &backoff->spgid);
4281 ceph_encode_32(&p, map_epoch);
4282 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4283 ceph_encode_64(&p, backoff->id);
4284 encode_hoid(&p, end, backoff->begin);
4285 encode_hoid(&p, end, backoff->end);
4288 msg->front.iov_len = p - msg->front.iov_base;
4289 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4290 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4295 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4297 struct ceph_spg_mapping *spg;
4298 struct ceph_osd_backoff *backoff;
4299 struct ceph_msg *msg;
4301 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4302 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4304 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4306 spg = alloc_spg_mapping();
4308 pr_err("%s failed to allocate spg\n", __func__);
4311 spg->spgid = m->spgid; /* struct */
4312 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4315 backoff = alloc_backoff();
4317 pr_err("%s failed to allocate backoff\n", __func__);
4320 backoff->spgid = m->spgid; /* struct */
4321 backoff->id = m->id;
4322 backoff->begin = m->begin;
4323 m->begin = NULL; /* backoff now owns this */
4324 backoff->end = m->end;
4325 m->end = NULL; /* ditto */
4327 insert_backoff(&spg->backoffs, backoff);
4328 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4331 * Ack with original backoff's epoch so that the OSD can
4332 * discard this if there was a PG split.
4334 msg = create_backoff_message(backoff, m->map_epoch);
4336 pr_err("%s failed to allocate msg\n", __func__);
4339 ceph_con_send(&osd->o_con, msg);
4342 static bool target_contained_by(const struct ceph_osd_request_target *t,
4343 const struct ceph_hobject_id *begin,
4344 const struct ceph_hobject_id *end)
4346 struct ceph_hobject_id hoid;
4349 hoid_fill_from_target(&hoid, t);
4350 cmp = hoid_compare(&hoid, begin);
4351 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4354 static void handle_backoff_unblock(struct ceph_osd *osd,
4355 const struct MOSDBackoff *m)
4357 struct ceph_spg_mapping *spg;
4358 struct ceph_osd_backoff *backoff;
4361 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4362 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4364 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4366 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4367 __func__, osd->o_osd, m->spgid.pgid.pool,
4368 m->spgid.pgid.seed, m->spgid.shard, m->id);
4372 if (hoid_compare(backoff->begin, m->begin) &&
4373 hoid_compare(backoff->end, m->end)) {
4374 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4375 __func__, osd->o_osd, m->spgid.pgid.pool,
4376 m->spgid.pgid.seed, m->spgid.shard, m->id);
4377 /* unblock it anyway... */
4380 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4383 erase_backoff(&spg->backoffs, backoff);
4384 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4385 free_backoff(backoff);
4387 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4388 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4389 free_spg_mapping(spg);
4392 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4393 struct ceph_osd_request *req =
4394 rb_entry(n, struct ceph_osd_request, r_node);
4396 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4398 * Match against @m, not @backoff -- the PG may
4399 * have split on the OSD.
4401 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4403 * If no other installed backoff applies,
4412 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4414 struct ceph_osd_client *osdc = osd->o_osdc;
4415 struct MOSDBackoff m;
4418 down_read(&osdc->lock);
4419 if (!osd_registered(osd)) {
4420 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4421 up_read(&osdc->lock);
4424 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4426 mutex_lock(&osd->lock);
4427 ret = decode_MOSDBackoff(msg, &m);
4429 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4435 case CEPH_OSD_BACKOFF_OP_BLOCK:
4436 handle_backoff_block(osd, &m);
4438 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4439 handle_backoff_unblock(osd, &m);
4442 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4449 mutex_unlock(&osd->lock);
4450 up_read(&osdc->lock);
4454 * Process osd watch notifications
4456 static void handle_watch_notify(struct ceph_osd_client *osdc,
4457 struct ceph_msg *msg)
4459 void *p = msg->front.iov_base;
4460 void *const end = p + msg->front.iov_len;
4461 struct ceph_osd_linger_request *lreq;
4462 struct linger_work *lwork;
4463 u8 proto_ver, opcode;
4464 u64 cookie, notify_id;
4465 u64 notifier_id = 0;
4466 s32 return_code = 0;
4467 void *payload = NULL;
4468 u32 payload_len = 0;
4470 ceph_decode_8_safe(&p, end, proto_ver, bad);
4471 ceph_decode_8_safe(&p, end, opcode, bad);
4472 ceph_decode_64_safe(&p, end, cookie, bad);
4473 p += 8; /* skip ver */
4474 ceph_decode_64_safe(&p, end, notify_id, bad);
4476 if (proto_ver >= 1) {
4477 ceph_decode_32_safe(&p, end, payload_len, bad);
4478 ceph_decode_need(&p, end, payload_len, bad);
4483 if (le16_to_cpu(msg->hdr.version) >= 2)
4484 ceph_decode_32_safe(&p, end, return_code, bad);
4486 if (le16_to_cpu(msg->hdr.version) >= 3)
4487 ceph_decode_64_safe(&p, end, notifier_id, bad);
4489 down_read(&osdc->lock);
4490 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4492 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4494 goto out_unlock_osdc;
4497 mutex_lock(&lreq->lock);
4498 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4499 opcode, cookie, lreq, lreq->is_watch);
4500 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4501 if (!lreq->last_error) {
4502 lreq->last_error = -ENOTCONN;
4503 queue_watch_error(lreq);
4505 } else if (!lreq->is_watch) {
4506 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4507 if (lreq->notify_id && lreq->notify_id != notify_id) {
4508 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4509 lreq->notify_id, notify_id);
4510 } else if (!completion_done(&lreq->notify_finish_wait)) {
4511 struct ceph_msg_data *data =
4512 msg->num_data_items ? &msg->data[0] : NULL;
4515 if (lreq->preply_pages) {
4516 WARN_ON(data->type !=
4517 CEPH_MSG_DATA_PAGES);
4518 *lreq->preply_pages = data->pages;
4519 *lreq->preply_len = data->length;
4520 data->own_pages = false;
4523 lreq->notify_finish_error = return_code;
4524 complete_all(&lreq->notify_finish_wait);
4527 /* CEPH_WATCH_EVENT_NOTIFY */
4528 lwork = lwork_alloc(lreq, do_watch_notify);
4530 pr_err("failed to allocate notify-lwork\n");
4531 goto out_unlock_lreq;
4534 lwork->notify.notify_id = notify_id;
4535 lwork->notify.notifier_id = notifier_id;
4536 lwork->notify.payload = payload;
4537 lwork->notify.payload_len = payload_len;
4538 lwork->notify.msg = ceph_msg_get(msg);
4543 mutex_unlock(&lreq->lock);
4545 up_read(&osdc->lock);
4549 pr_err("osdc handle_watch_notify corrupt msg\n");
4553 * Register request, send initial attempt.
4555 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4556 struct ceph_osd_request *req,
4559 down_read(&osdc->lock);
4560 submit_request(req, false);
4561 up_read(&osdc->lock);
4565 EXPORT_SYMBOL(ceph_osdc_start_request);
4568 * Unregister a registered request. The request is not completed:
4569 * ->r_result isn't set and __complete_request() isn't called.
4571 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4573 struct ceph_osd_client *osdc = req->r_osdc;
4575 down_write(&osdc->lock);
4577 cancel_request(req);
4578 up_write(&osdc->lock);
4580 EXPORT_SYMBOL(ceph_osdc_cancel_request);
4583 * @timeout: in jiffies, 0 means "wait forever"
4585 static int wait_request_timeout(struct ceph_osd_request *req,
4586 unsigned long timeout)
4590 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
4591 left = wait_for_completion_killable_timeout(&req->r_completion,
4592 ceph_timeout_jiffies(timeout));
4594 left = left ?: -ETIMEDOUT;
4595 ceph_osdc_cancel_request(req);
4597 left = req->r_result; /* completed */
4604 * wait for a request to complete
4606 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4607 struct ceph_osd_request *req)
4609 return wait_request_timeout(req, 0);
4611 EXPORT_SYMBOL(ceph_osdc_wait_request);
4614 * sync - wait for all in-flight requests to flush. avoid starvation.
4616 void ceph_osdc_sync(struct ceph_osd_client *osdc)
4618 struct rb_node *n, *p;
4619 u64 last_tid = atomic64_read(&osdc->last_tid);
4622 down_read(&osdc->lock);
4623 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4624 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4626 mutex_lock(&osd->lock);
4627 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4628 struct ceph_osd_request *req =
4629 rb_entry(p, struct ceph_osd_request, r_node);
4631 if (req->r_tid > last_tid)
4634 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4637 ceph_osdc_get_request(req);
4638 mutex_unlock(&osd->lock);
4639 up_read(&osdc->lock);
4640 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4641 __func__, req, req->r_tid, last_tid);
4642 wait_for_completion(&req->r_completion);
4643 ceph_osdc_put_request(req);
4647 mutex_unlock(&osd->lock);
4650 up_read(&osdc->lock);
4651 dout("%s done last_tid %llu\n", __func__, last_tid);
4653 EXPORT_SYMBOL(ceph_osdc_sync);
4655 static struct ceph_osd_request *
4656 alloc_linger_request(struct ceph_osd_linger_request *lreq)
4658 struct ceph_osd_request *req;
4660 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4664 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4665 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4669 static struct ceph_osd_request *
4670 alloc_watch_request(struct ceph_osd_linger_request *lreq, u8 watch_opcode)
4672 struct ceph_osd_request *req;
4674 req = alloc_linger_request(lreq);
4679 * Pass 0 for cookie because we don't know it yet, it will be
4680 * filled in by linger_submit().
4682 osd_req_op_watch_init(req, 0, 0, watch_opcode);
4684 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4685 ceph_osdc_put_request(req);
4693 * Returns a handle, caller owns a ref.
4695 struct ceph_osd_linger_request *
4696 ceph_osdc_watch(struct ceph_osd_client *osdc,
4697 struct ceph_object_id *oid,
4698 struct ceph_object_locator *oloc,
4699 rados_watchcb2_t wcb,
4700 rados_watcherrcb_t errcb,
4703 struct ceph_osd_linger_request *lreq;
4706 lreq = linger_alloc(osdc);
4708 return ERR_PTR(-ENOMEM);
4710 lreq->is_watch = true;
4712 lreq->errcb = errcb;
4714 lreq->watch_valid_thru = jiffies;
4716 ceph_oid_copy(&lreq->t.base_oid, oid);
4717 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4718 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4719 ktime_get_real_ts64(&lreq->mtime);
4721 lreq->reg_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_WATCH);
4722 if (!lreq->reg_req) {
4727 lreq->ping_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_PING);
4728 if (!lreq->ping_req) {
4733 linger_submit(lreq);
4734 ret = linger_reg_commit_wait(lreq);
4736 linger_cancel(lreq);
4744 return ERR_PTR(ret);
4746 EXPORT_SYMBOL(ceph_osdc_watch);
4751 * Times out after mount_timeout to preserve rbd unmap behaviour
4752 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4753 * with mount_timeout").
4755 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4756 struct ceph_osd_linger_request *lreq)
4758 struct ceph_options *opts = osdc->client->options;
4759 struct ceph_osd_request *req;
4762 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4766 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4767 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4768 req->r_flags = CEPH_OSD_FLAG_WRITE;
4769 ktime_get_real_ts64(&req->r_mtime);
4770 osd_req_op_watch_init(req, 0, lreq->linger_id,
4771 CEPH_OSD_WATCH_OP_UNWATCH);
4773 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4777 ceph_osdc_start_request(osdc, req, false);
4778 linger_cancel(lreq);
4780 ret = wait_request_timeout(req, opts->mount_timeout);
4783 ceph_osdc_put_request(req);
4786 EXPORT_SYMBOL(ceph_osdc_unwatch);
4788 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4789 u64 notify_id, u64 cookie, void *payload,
4792 struct ceph_osd_req_op *op;
4793 struct ceph_pagelist *pl;
4796 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4798 pl = ceph_pagelist_alloc(GFP_NOIO);
4802 ret = ceph_pagelist_encode_64(pl, notify_id);
4803 ret |= ceph_pagelist_encode_64(pl, cookie);
4805 ret |= ceph_pagelist_encode_32(pl, payload_len);
4806 ret |= ceph_pagelist_append(pl, payload, payload_len);
4808 ret |= ceph_pagelist_encode_32(pl, 0);
4811 ceph_pagelist_release(pl);
4815 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4816 op->indata_len = pl->length;
4820 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4821 struct ceph_object_id *oid,
4822 struct ceph_object_locator *oloc,
4828 struct ceph_osd_request *req;
4831 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4835 ceph_oid_copy(&req->r_base_oid, oid);
4836 ceph_oloc_copy(&req->r_base_oloc, oloc);
4837 req->r_flags = CEPH_OSD_FLAG_READ;
4839 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4844 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4848 ceph_osdc_start_request(osdc, req, false);
4849 ret = ceph_osdc_wait_request(osdc, req);
4852 ceph_osdc_put_request(req);
4855 EXPORT_SYMBOL(ceph_osdc_notify_ack);
4857 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4858 u64 cookie, u32 prot_ver, u32 timeout,
4859 void *payload, u32 payload_len)
4861 struct ceph_osd_req_op *op;
4862 struct ceph_pagelist *pl;
4865 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4866 op->notify.cookie = cookie;
4868 pl = ceph_pagelist_alloc(GFP_NOIO);
4872 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4873 ret |= ceph_pagelist_encode_32(pl, timeout);
4874 ret |= ceph_pagelist_encode_32(pl, payload_len);
4875 ret |= ceph_pagelist_append(pl, payload, payload_len);
4877 ceph_pagelist_release(pl);
4881 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4882 op->indata_len = pl->length;
4887 * @timeout: in seconds
4889 * @preply_{pages,len} are initialized both on success and error.
4890 * The caller is responsible for:
4892 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4894 int ceph_osdc_notify(struct ceph_osd_client *osdc,
4895 struct ceph_object_id *oid,
4896 struct ceph_object_locator *oloc,
4900 struct page ***preply_pages,
4903 struct ceph_osd_linger_request *lreq;
4904 struct page **pages;
4909 *preply_pages = NULL;
4913 lreq = linger_alloc(osdc);
4917 lreq->preply_pages = preply_pages;
4918 lreq->preply_len = preply_len;
4920 ceph_oid_copy(&lreq->t.base_oid, oid);
4921 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4922 lreq->t.flags = CEPH_OSD_FLAG_READ;
4924 lreq->reg_req = alloc_linger_request(lreq);
4925 if (!lreq->reg_req) {
4931 * Pass 0 for cookie because we don't know it yet, it will be
4932 * filled in by linger_submit().
4934 ret = osd_req_op_notify_init(lreq->reg_req, 0, 0, 1, timeout,
4935 payload, payload_len);
4940 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4941 if (IS_ERR(pages)) {
4942 ret = PTR_ERR(pages);
4945 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4947 pages, PAGE_SIZE, 0, false, true);
4949 ret = ceph_osdc_alloc_messages(lreq->reg_req, GFP_NOIO);
4953 linger_submit(lreq);
4954 ret = linger_reg_commit_wait(lreq);
4956 ret = linger_notify_finish_wait(lreq);
4958 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4960 linger_cancel(lreq);
4965 EXPORT_SYMBOL(ceph_osdc_notify);
4968 * Return the number of milliseconds since the watch was last
4969 * confirmed, or an error. If there is an error, the watch is no
4970 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4972 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4973 struct ceph_osd_linger_request *lreq)
4975 unsigned long stamp, age;
4978 down_read(&osdc->lock);
4979 mutex_lock(&lreq->lock);
4980 stamp = lreq->watch_valid_thru;
4981 if (!list_empty(&lreq->pending_lworks)) {
4982 struct linger_work *lwork =
4983 list_first_entry(&lreq->pending_lworks,
4987 if (time_before(lwork->queued_stamp, stamp))
4988 stamp = lwork->queued_stamp;
4990 age = jiffies - stamp;
4991 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4992 lreq, lreq->linger_id, age, lreq->last_error);
4993 /* we are truncating to msecs, so return a safe upper bound */
4994 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4996 mutex_unlock(&lreq->lock);
4997 up_read(&osdc->lock);
5001 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
5007 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
5008 &struct_v, &struct_len);
5013 ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
5014 ceph_decode_64_safe(p, end, item->cookie, bad);
5015 ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
5017 if (struct_v >= 2) {
5018 ret = ceph_decode_entity_addr(p, end, &item->addr);
5025 dout("%s %s%llu cookie %llu addr %s\n", __func__,
5026 ENTITY_NAME(item->name), item->cookie,
5027 ceph_pr_addr(&item->addr));
5032 static int decode_watchers(void **p, void *end,
5033 struct ceph_watch_item **watchers,
5041 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
5042 &struct_v, &struct_len);
5046 *num_watchers = ceph_decode_32(p);
5047 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
5051 for (i = 0; i < *num_watchers; i++) {
5052 ret = decode_watcher(p, end, *watchers + i);
5063 * On success, the caller is responsible for:
5067 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
5068 struct ceph_object_id *oid,
5069 struct ceph_object_locator *oloc,
5070 struct ceph_watch_item **watchers,
5073 struct ceph_osd_request *req;
5074 struct page **pages;
5077 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5081 ceph_oid_copy(&req->r_base_oid, oid);
5082 ceph_oloc_copy(&req->r_base_oloc, oloc);
5083 req->r_flags = CEPH_OSD_FLAG_READ;
5085 pages = ceph_alloc_page_vector(1, GFP_NOIO);
5086 if (IS_ERR(pages)) {
5087 ret = PTR_ERR(pages);
5091 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5092 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5094 pages, PAGE_SIZE, 0, false, true);
5096 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5100 ceph_osdc_start_request(osdc, req, false);
5101 ret = ceph_osdc_wait_request(osdc, req);
5103 void *p = page_address(pages[0]);
5104 void *const end = p + req->r_ops[0].outdata_len;
5106 ret = decode_watchers(&p, end, watchers, num_watchers);
5110 ceph_osdc_put_request(req);
5113 EXPORT_SYMBOL(ceph_osdc_list_watchers);
5116 * Call all pending notify callbacks - for use after a watch is
5117 * unregistered, to make sure no more callbacks for it will be invoked
5119 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5121 dout("%s osdc %p\n", __func__, osdc);
5122 flush_workqueue(osdc->notify_wq);
5124 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5126 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
5128 down_read(&osdc->lock);
5129 maybe_request_map(osdc);
5130 up_read(&osdc->lock);
5132 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5135 * Execute an OSD class method on an object.
5137 * @flags: CEPH_OSD_FLAG_*
5138 * @resp_len: in/out param for reply length
5140 int ceph_osdc_call(struct ceph_osd_client *osdc,
5141 struct ceph_object_id *oid,
5142 struct ceph_object_locator *oloc,
5143 const char *class, const char *method,
5145 struct page *req_page, size_t req_len,
5146 struct page **resp_pages, size_t *resp_len)
5148 struct ceph_osd_request *req;
5151 if (req_len > PAGE_SIZE)
5154 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5158 ceph_oid_copy(&req->r_base_oid, oid);
5159 ceph_oloc_copy(&req->r_base_oloc, oloc);
5160 req->r_flags = flags;
5162 ret = osd_req_op_cls_init(req, 0, class, method);
5167 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5170 osd_req_op_cls_response_data_pages(req, 0, resp_pages,
5171 *resp_len, 0, false, false);
5173 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5177 ceph_osdc_start_request(osdc, req, false);
5178 ret = ceph_osdc_wait_request(osdc, req);
5180 ret = req->r_ops[0].rval;
5182 *resp_len = req->r_ops[0].outdata_len;
5186 ceph_osdc_put_request(req);
5189 EXPORT_SYMBOL(ceph_osdc_call);
5192 * reset all osd connections
5194 void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5198 down_write(&osdc->lock);
5199 for (n = rb_first(&osdc->osds); n; ) {
5200 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5203 if (!reopen_osd(osd))
5204 kick_osd_requests(osd);
5206 up_write(&osdc->lock);
5212 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5217 osdc->client = client;
5218 init_rwsem(&osdc->lock);
5219 osdc->osds = RB_ROOT;
5220 INIT_LIST_HEAD(&osdc->osd_lru);
5221 spin_lock_init(&osdc->osd_lru_lock);
5222 osd_init(&osdc->homeless_osd);
5223 osdc->homeless_osd.o_osdc = osdc;
5224 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5225 osdc->last_linger_id = CEPH_LINGER_ID_START;
5226 osdc->linger_requests = RB_ROOT;
5227 osdc->map_checks = RB_ROOT;
5228 osdc->linger_map_checks = RB_ROOT;
5229 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
5230 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5233 osdc->osdmap = ceph_osdmap_alloc();
5237 osdc->req_mempool = mempool_create_slab_pool(10,
5238 ceph_osd_request_cache);
5239 if (!osdc->req_mempool)
5242 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5243 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
5246 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5247 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
5253 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5254 if (!osdc->notify_wq)
5255 goto out_msgpool_reply;
5257 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5258 if (!osdc->completion_wq)
5261 schedule_delayed_work(&osdc->timeout_work,
5262 osdc->client->options->osd_keepalive_timeout);
5263 schedule_delayed_work(&osdc->osds_timeout_work,
5264 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5269 destroy_workqueue(osdc->notify_wq);
5271 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5273 ceph_msgpool_destroy(&osdc->msgpool_op);
5275 mempool_destroy(osdc->req_mempool);
5277 ceph_osdmap_destroy(osdc->osdmap);
5282 void ceph_osdc_stop(struct ceph_osd_client *osdc)
5284 destroy_workqueue(osdc->completion_wq);
5285 destroy_workqueue(osdc->notify_wq);
5286 cancel_delayed_work_sync(&osdc->timeout_work);
5287 cancel_delayed_work_sync(&osdc->osds_timeout_work);
5289 down_write(&osdc->lock);
5290 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5291 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5292 struct ceph_osd, o_node);
5295 up_write(&osdc->lock);
5296 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5297 osd_cleanup(&osdc->homeless_osd);
5299 WARN_ON(!list_empty(&osdc->osd_lru));
5300 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
5301 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5302 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5303 WARN_ON(atomic_read(&osdc->num_requests));
5304 WARN_ON(atomic_read(&osdc->num_homeless));
5306 ceph_osdmap_destroy(osdc->osdmap);
5307 mempool_destroy(osdc->req_mempool);
5308 ceph_msgpool_destroy(&osdc->msgpool_op);
5309 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5312 static int osd_req_op_copy_from_init(struct ceph_osd_request *req,
5313 u64 src_snapid, u64 src_version,
5314 struct ceph_object_id *src_oid,
5315 struct ceph_object_locator *src_oloc,
5316 u32 src_fadvise_flags,
5317 u32 dst_fadvise_flags,
5318 u32 truncate_seq, u64 truncate_size,
5321 struct ceph_osd_req_op *op;
5322 struct page **pages;
5325 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
5327 return PTR_ERR(pages);
5329 op = _osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
5331 op->copy_from.snapid = src_snapid;
5332 op->copy_from.src_version = src_version;
5333 op->copy_from.flags = copy_from_flags;
5334 op->copy_from.src_fadvise_flags = src_fadvise_flags;
5336 p = page_address(pages[0]);
5337 end = p + PAGE_SIZE;
5338 ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
5339 encode_oloc(&p, end, src_oloc);
5340 ceph_encode_32(&p, truncate_seq);
5341 ceph_encode_64(&p, truncate_size);
5342 op->indata_len = PAGE_SIZE - (end - p);
5344 ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
5345 op->indata_len, 0, false, true);
5349 int ceph_osdc_copy_from(struct ceph_osd_client *osdc,
5350 u64 src_snapid, u64 src_version,
5351 struct ceph_object_id *src_oid,
5352 struct ceph_object_locator *src_oloc,
5353 u32 src_fadvise_flags,
5354 struct ceph_object_id *dst_oid,
5355 struct ceph_object_locator *dst_oloc,
5356 u32 dst_fadvise_flags,
5357 u32 truncate_seq, u64 truncate_size,
5360 struct ceph_osd_request *req;
5363 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
5367 req->r_flags = CEPH_OSD_FLAG_WRITE;
5369 ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
5370 ceph_oid_copy(&req->r_t.base_oid, dst_oid);
5372 ret = osd_req_op_copy_from_init(req, src_snapid, src_version, src_oid,
5373 src_oloc, src_fadvise_flags,
5374 dst_fadvise_flags, truncate_seq,
5375 truncate_size, copy_from_flags);
5379 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
5383 ceph_osdc_start_request(osdc, req, false);
5384 ret = ceph_osdc_wait_request(osdc, req);
5387 ceph_osdc_put_request(req);
5390 EXPORT_SYMBOL(ceph_osdc_copy_from);
5392 int __init ceph_osdc_setup(void)
5394 size_t size = sizeof(struct ceph_osd_request) +
5395 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5397 BUG_ON(ceph_osd_request_cache);
5398 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5401 return ceph_osd_request_cache ? 0 : -ENOMEM;
5404 void ceph_osdc_cleanup(void)
5406 BUG_ON(!ceph_osd_request_cache);
5407 kmem_cache_destroy(ceph_osd_request_cache);
5408 ceph_osd_request_cache = NULL;
5412 * handle incoming message
5414 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5416 struct ceph_osd *osd = con->private;
5417 struct ceph_osd_client *osdc = osd->o_osdc;
5418 int type = le16_to_cpu(msg->hdr.type);
5421 case CEPH_MSG_OSD_MAP:
5422 ceph_osdc_handle_map(osdc, msg);
5424 case CEPH_MSG_OSD_OPREPLY:
5425 handle_reply(osd, msg);
5427 case CEPH_MSG_OSD_BACKOFF:
5428 handle_backoff(osd, msg);
5430 case CEPH_MSG_WATCH_NOTIFY:
5431 handle_watch_notify(osdc, msg);
5435 pr_err("received unknown message type %d %s\n", type,
5436 ceph_msg_type_name(type));
5443 * Lookup and return message for incoming reply. Don't try to do
5444 * anything about a larger than preallocated data portion of the
5445 * message at the moment - for now, just skip the message.
5447 static struct ceph_msg *get_reply(struct ceph_connection *con,
5448 struct ceph_msg_header *hdr,
5451 struct ceph_osd *osd = con->private;
5452 struct ceph_osd_client *osdc = osd->o_osdc;
5453 struct ceph_msg *m = NULL;
5454 struct ceph_osd_request *req;
5455 int front_len = le32_to_cpu(hdr->front_len);
5456 int data_len = le32_to_cpu(hdr->data_len);
5457 u64 tid = le64_to_cpu(hdr->tid);
5459 down_read(&osdc->lock);
5460 if (!osd_registered(osd)) {
5461 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5463 goto out_unlock_osdc;
5465 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5467 mutex_lock(&osd->lock);
5468 req = lookup_request(&osd->o_requests, tid);
5470 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5473 goto out_unlock_session;
5476 ceph_msg_revoke_incoming(req->r_reply);
5478 if (front_len > req->r_reply->front_alloc_len) {
5479 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5480 __func__, osd->o_osd, req->r_tid, front_len,
5481 req->r_reply->front_alloc_len);
5482 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5485 goto out_unlock_session;
5486 ceph_msg_put(req->r_reply);
5490 if (data_len > req->r_reply->data_length) {
5491 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5492 __func__, osd->o_osd, req->r_tid, data_len,
5493 req->r_reply->data_length);
5496 goto out_unlock_session;
5499 m = ceph_msg_get(req->r_reply);
5500 dout("get_reply tid %lld %p\n", tid, m);
5503 mutex_unlock(&osd->lock);
5505 up_read(&osdc->lock);
5509 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
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);
5516 m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
5521 struct page **pages;
5523 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5525 if (IS_ERR(pages)) {
5530 ceph_msg_data_add_pages(m, pages, data_len, 0, true);
5536 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5537 struct ceph_msg_header *hdr,
5540 struct ceph_osd *osd = con->private;
5541 int type = le16_to_cpu(hdr->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);
5552 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5560 * Wrappers to refcount containing ceph_osd struct
5562 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5564 struct ceph_osd *osd = con->private;
5570 static void put_osd_con(struct ceph_connection *con)
5572 struct ceph_osd *osd = con->private;
5580 * Note: returned pointer is the address of a structure that's
5581 * managed separately. Caller must *not* attempt to free it.
5583 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
5584 int *proto, int force_new)
5586 struct ceph_osd *o = con->private;
5587 struct ceph_osd_client *osdc = o->o_osdc;
5588 struct ceph_auth_client *ac = osdc->client->monc.auth;
5589 struct ceph_auth_handshake *auth = &o->o_auth;
5591 if (force_new && auth->authorizer) {
5592 ceph_auth_destroy_authorizer(auth->authorizer);
5593 auth->authorizer = NULL;
5595 if (!auth->authorizer) {
5596 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5599 return ERR_PTR(ret);
5601 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5604 return ERR_PTR(ret);
5606 *proto = ac->protocol;
5611 static int add_authorizer_challenge(struct ceph_connection *con,
5612 void *challenge_buf, int challenge_buf_len)
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;
5618 return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
5619 challenge_buf, challenge_buf_len);
5622 static int verify_authorizer_reply(struct ceph_connection *con)
5624 struct ceph_osd *o = con->private;
5625 struct ceph_osd_client *osdc = o->o_osdc;
5626 struct ceph_auth_client *ac = osdc->client->monc.auth;
5628 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
5631 static int invalidate_authorizer(struct ceph_connection *con)
5633 struct ceph_osd *o = con->private;
5634 struct ceph_osd_client *osdc = o->o_osdc;
5635 struct ceph_auth_client *ac = osdc->client->monc.auth;
5637 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
5638 return ceph_monc_validate_auth(&osdc->client->monc);
5641 static void osd_reencode_message(struct ceph_msg *msg)
5643 int type = le16_to_cpu(msg->hdr.type);
5645 if (type == CEPH_MSG_OSD_OP)
5646 encode_request_finish(msg);
5649 static int osd_sign_message(struct ceph_msg *msg)
5651 struct ceph_osd *o = msg->con->private;
5652 struct ceph_auth_handshake *auth = &o->o_auth;
5654 return ceph_auth_sign_message(auth, msg);
5657 static int osd_check_message_signature(struct ceph_msg *msg)
5659 struct ceph_osd *o = msg->con->private;
5660 struct ceph_auth_handshake *auth = &o->o_auth;
5662 return ceph_auth_check_message_signature(auth, msg);
5665 static const struct ceph_connection_operations osd_con_ops = {
5668 .dispatch = dispatch,
5669 .get_authorizer = get_authorizer,
5670 .add_authorizer_challenge = add_authorizer_challenge,
5671 .verify_authorizer_reply = verify_authorizer_reply,
5672 .invalidate_authorizer = invalidate_authorizer,
5673 .alloc_msg = alloc_msg,
5674 .reencode_message = osd_reencode_message,
5675 .sign_message = osd_sign_message,
5676 .check_message_signature = osd_check_message_signature,