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;
129 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
130 struct page **pages, u64 length, u32 alignment,
131 bool pages_from_pool, bool own_pages)
133 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
134 osd_data->pages = pages;
135 osd_data->length = length;
136 osd_data->alignment = alignment;
137 osd_data->pages_from_pool = pages_from_pool;
138 osd_data->own_pages = own_pages;
141 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
142 struct ceph_pagelist *pagelist)
144 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
145 osd_data->pagelist = pagelist;
149 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
150 struct ceph_bio_iter *bio_pos,
153 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
154 osd_data->bio_pos = *bio_pos;
155 osd_data->bio_length = bio_length;
157 #endif /* CONFIG_BLOCK */
159 static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
160 struct ceph_bvec_iter *bvec_pos,
163 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
164 osd_data->bvec_pos = *bvec_pos;
165 osd_data->num_bvecs = num_bvecs;
168 #define osd_req_op_data(oreq, whch, typ, fld) \
170 struct ceph_osd_request *__oreq = (oreq); \
171 unsigned int __whch = (whch); \
172 BUG_ON(__whch >= __oreq->r_num_ops); \
173 &__oreq->r_ops[__whch].typ.fld; \
176 static struct ceph_osd_data *
177 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
179 BUG_ON(which >= osd_req->r_num_ops);
181 return &osd_req->r_ops[which].raw_data_in;
184 struct ceph_osd_data *
185 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
188 return osd_req_op_data(osd_req, which, extent, osd_data);
190 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
192 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
193 unsigned int which, struct page **pages,
194 u64 length, u32 alignment,
195 bool pages_from_pool, bool own_pages)
197 struct ceph_osd_data *osd_data;
199 osd_data = osd_req_op_raw_data_in(osd_req, which);
200 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
201 pages_from_pool, own_pages);
203 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
205 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
206 unsigned int which, struct page **pages,
207 u64 length, u32 alignment,
208 bool pages_from_pool, bool own_pages)
210 struct ceph_osd_data *osd_data;
212 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
213 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
214 pages_from_pool, own_pages);
216 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
218 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
219 unsigned int which, struct ceph_pagelist *pagelist)
221 struct ceph_osd_data *osd_data;
223 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
224 ceph_osd_data_pagelist_init(osd_data, pagelist);
226 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
229 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
231 struct ceph_bio_iter *bio_pos,
234 struct ceph_osd_data *osd_data;
236 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
237 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
239 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
240 #endif /* CONFIG_BLOCK */
242 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
244 struct bio_vec *bvecs, u32 num_bvecs,
247 struct ceph_osd_data *osd_data;
248 struct ceph_bvec_iter it = {
250 .iter = { .bi_size = bytes },
253 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
254 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
256 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
258 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
260 struct ceph_bvec_iter *bvec_pos)
262 struct ceph_osd_data *osd_data;
264 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
265 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
267 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
269 static void osd_req_op_cls_request_info_pagelist(
270 struct ceph_osd_request *osd_req,
271 unsigned int which, struct ceph_pagelist *pagelist)
273 struct ceph_osd_data *osd_data;
275 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
276 ceph_osd_data_pagelist_init(osd_data, pagelist);
279 void osd_req_op_cls_request_data_pagelist(
280 struct ceph_osd_request *osd_req,
281 unsigned int which, struct ceph_pagelist *pagelist)
283 struct ceph_osd_data *osd_data;
285 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
286 ceph_osd_data_pagelist_init(osd_data, pagelist);
287 osd_req->r_ops[which].cls.indata_len += pagelist->length;
288 osd_req->r_ops[which].indata_len += pagelist->length;
290 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
292 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
293 unsigned int which, struct page **pages, u64 length,
294 u32 alignment, bool pages_from_pool, bool own_pages)
296 struct ceph_osd_data *osd_data;
298 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
299 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
300 pages_from_pool, own_pages);
301 osd_req->r_ops[which].cls.indata_len += length;
302 osd_req->r_ops[which].indata_len += length;
304 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
306 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
308 struct bio_vec *bvecs, u32 num_bvecs,
311 struct ceph_osd_data *osd_data;
312 struct ceph_bvec_iter it = {
314 .iter = { .bi_size = bytes },
317 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
318 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
319 osd_req->r_ops[which].cls.indata_len += bytes;
320 osd_req->r_ops[which].indata_len += bytes;
322 EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
324 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
325 unsigned int which, struct page **pages, u64 length,
326 u32 alignment, bool pages_from_pool, bool own_pages)
328 struct ceph_osd_data *osd_data;
330 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
331 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
332 pages_from_pool, own_pages);
334 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
336 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
338 switch (osd_data->type) {
339 case CEPH_OSD_DATA_TYPE_NONE:
341 case CEPH_OSD_DATA_TYPE_PAGES:
342 return osd_data->length;
343 case CEPH_OSD_DATA_TYPE_PAGELIST:
344 return (u64)osd_data->pagelist->length;
346 case CEPH_OSD_DATA_TYPE_BIO:
347 return (u64)osd_data->bio_length;
348 #endif /* CONFIG_BLOCK */
349 case CEPH_OSD_DATA_TYPE_BVECS:
350 return osd_data->bvec_pos.iter.bi_size;
352 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
357 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
359 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
362 num_pages = calc_pages_for((u64)osd_data->alignment,
363 (u64)osd_data->length);
364 ceph_release_page_vector(osd_data->pages, num_pages);
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);
411 * Assumes @t is zero-initialized.
413 static void target_init(struct ceph_osd_request_target *t)
415 ceph_oid_init(&t->base_oid);
416 ceph_oloc_init(&t->base_oloc);
417 ceph_oid_init(&t->target_oid);
418 ceph_oloc_init(&t->target_oloc);
420 ceph_osds_init(&t->acting);
421 ceph_osds_init(&t->up);
425 t->osd = CEPH_HOMELESS_OSD;
428 static void target_copy(struct ceph_osd_request_target *dest,
429 const struct ceph_osd_request_target *src)
431 ceph_oid_copy(&dest->base_oid, &src->base_oid);
432 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
433 ceph_oid_copy(&dest->target_oid, &src->target_oid);
434 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
436 dest->pgid = src->pgid; /* struct */
437 dest->spgid = src->spgid; /* struct */
438 dest->pg_num = src->pg_num;
439 dest->pg_num_mask = src->pg_num_mask;
440 ceph_osds_copy(&dest->acting, &src->acting);
441 ceph_osds_copy(&dest->up, &src->up);
442 dest->size = src->size;
443 dest->min_size = src->min_size;
444 dest->sort_bitwise = src->sort_bitwise;
446 dest->flags = src->flags;
447 dest->paused = src->paused;
449 dest->epoch = src->epoch;
450 dest->last_force_resend = src->last_force_resend;
452 dest->osd = src->osd;
455 static void target_destroy(struct ceph_osd_request_target *t)
457 ceph_oid_destroy(&t->base_oid);
458 ceph_oloc_destroy(&t->base_oloc);
459 ceph_oid_destroy(&t->target_oid);
460 ceph_oloc_destroy(&t->target_oloc);
466 static void request_release_checks(struct ceph_osd_request *req)
468 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
469 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
470 WARN_ON(!list_empty(&req->r_unsafe_item));
474 static void ceph_osdc_release_request(struct kref *kref)
476 struct ceph_osd_request *req = container_of(kref,
477 struct ceph_osd_request, r_kref);
480 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
481 req->r_request, req->r_reply);
482 request_release_checks(req);
485 ceph_msg_put(req->r_request);
487 ceph_msg_put(req->r_reply);
489 for (which = 0; which < req->r_num_ops; which++)
490 osd_req_op_data_release(req, which);
492 target_destroy(&req->r_t);
493 ceph_put_snap_context(req->r_snapc);
496 mempool_free(req, req->r_osdc->req_mempool);
497 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
498 kmem_cache_free(ceph_osd_request_cache, req);
503 void ceph_osdc_get_request(struct ceph_osd_request *req)
505 dout("%s %p (was %d)\n", __func__, req,
506 kref_read(&req->r_kref));
507 kref_get(&req->r_kref);
509 EXPORT_SYMBOL(ceph_osdc_get_request);
511 void ceph_osdc_put_request(struct ceph_osd_request *req)
514 dout("%s %p (was %d)\n", __func__, req,
515 kref_read(&req->r_kref));
516 kref_put(&req->r_kref, ceph_osdc_release_request);
519 EXPORT_SYMBOL(ceph_osdc_put_request);
521 static void request_init(struct ceph_osd_request *req)
523 /* req only, each op is zeroed in _osd_req_op_init() */
524 memset(req, 0, sizeof(*req));
526 kref_init(&req->r_kref);
527 init_completion(&req->r_completion);
528 RB_CLEAR_NODE(&req->r_node);
529 RB_CLEAR_NODE(&req->r_mc_node);
530 INIT_LIST_HEAD(&req->r_unsafe_item);
532 target_init(&req->r_t);
536 * This is ugly, but it allows us to reuse linger registration and ping
537 * requests, keeping the structure of the code around send_linger{_ping}()
538 * reasonable. Setting up a min_nr=2 mempool for each linger request
539 * and dealing with copying ops (this blasts req only, watch op remains
540 * intact) isn't any better.
542 static void request_reinit(struct ceph_osd_request *req)
544 struct ceph_osd_client *osdc = req->r_osdc;
545 bool mempool = req->r_mempool;
546 unsigned int num_ops = req->r_num_ops;
547 u64 snapid = req->r_snapid;
548 struct ceph_snap_context *snapc = req->r_snapc;
549 bool linger = req->r_linger;
550 struct ceph_msg *request_msg = req->r_request;
551 struct ceph_msg *reply_msg = req->r_reply;
553 dout("%s req %p\n", __func__, req);
554 WARN_ON(kref_read(&req->r_kref) != 1);
555 request_release_checks(req);
557 WARN_ON(kref_read(&request_msg->kref) != 1);
558 WARN_ON(kref_read(&reply_msg->kref) != 1);
559 target_destroy(&req->r_t);
563 req->r_mempool = mempool;
564 req->r_num_ops = num_ops;
565 req->r_snapid = snapid;
566 req->r_snapc = snapc;
567 req->r_linger = linger;
568 req->r_request = request_msg;
569 req->r_reply = reply_msg;
572 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
573 struct ceph_snap_context *snapc,
574 unsigned int num_ops,
578 struct ceph_osd_request *req;
581 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
582 req = mempool_alloc(osdc->req_mempool, gfp_flags);
583 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
584 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
586 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
587 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
595 req->r_mempool = use_mempool;
596 req->r_num_ops = num_ops;
597 req->r_snapid = CEPH_NOSNAP;
598 req->r_snapc = ceph_get_snap_context(snapc);
600 dout("%s req %p\n", __func__, req);
603 EXPORT_SYMBOL(ceph_osdc_alloc_request);
605 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
607 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
610 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
612 struct ceph_osd_client *osdc = req->r_osdc;
613 struct ceph_msg *msg;
616 WARN_ON(ceph_oid_empty(&req->r_base_oid));
617 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
619 /* create request message */
620 msg_size = CEPH_ENCODING_START_BLK_LEN +
621 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
622 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
623 msg_size += CEPH_ENCODING_START_BLK_LEN +
624 sizeof(struct ceph_osd_reqid); /* reqid */
625 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
626 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
627 msg_size += CEPH_ENCODING_START_BLK_LEN +
628 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
629 msg_size += 4 + req->r_base_oid.name_len; /* oid */
630 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
631 msg_size += 8; /* snapid */
632 msg_size += 8; /* snap_seq */
633 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
634 msg_size += 4 + 8; /* retry_attempt, features */
637 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
639 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
643 memset(msg->front.iov_base, 0, msg->front.iov_len);
644 req->r_request = msg;
646 /* create reply message */
647 msg_size = OSD_OPREPLY_FRONT_LEN;
648 msg_size += req->r_base_oid.name_len;
649 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
652 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
654 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
662 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
664 static bool osd_req_opcode_valid(u16 opcode)
667 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
668 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
676 * This is an osd op init function for opcodes that have no data or
677 * other information associated with them. It also serves as a
678 * common init routine for all the other init functions, below.
680 static struct ceph_osd_req_op *
681 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
682 u16 opcode, u32 flags)
684 struct ceph_osd_req_op *op;
686 BUG_ON(which >= osd_req->r_num_ops);
687 BUG_ON(!osd_req_opcode_valid(opcode));
689 op = &osd_req->r_ops[which];
690 memset(op, 0, sizeof (*op));
697 void osd_req_op_init(struct ceph_osd_request *osd_req,
698 unsigned int which, u16 opcode, u32 flags)
700 (void)_osd_req_op_init(osd_req, which, opcode, flags);
702 EXPORT_SYMBOL(osd_req_op_init);
704 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
705 unsigned int which, u16 opcode,
706 u64 offset, u64 length,
707 u64 truncate_size, u32 truncate_seq)
709 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
711 size_t payload_len = 0;
713 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
714 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
715 opcode != CEPH_OSD_OP_TRUNCATE);
717 op->extent.offset = offset;
718 op->extent.length = length;
719 op->extent.truncate_size = truncate_size;
720 op->extent.truncate_seq = truncate_seq;
721 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
722 payload_len += length;
724 op->indata_len = payload_len;
726 EXPORT_SYMBOL(osd_req_op_extent_init);
728 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
729 unsigned int which, u64 length)
731 struct ceph_osd_req_op *op;
734 BUG_ON(which >= osd_req->r_num_ops);
735 op = &osd_req->r_ops[which];
736 previous = op->extent.length;
738 if (length == previous)
739 return; /* Nothing to do */
740 BUG_ON(length > previous);
742 op->extent.length = length;
743 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
744 op->indata_len -= previous - length;
746 EXPORT_SYMBOL(osd_req_op_extent_update);
748 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
749 unsigned int which, u64 offset_inc)
751 struct ceph_osd_req_op *op, *prev_op;
753 BUG_ON(which + 1 >= osd_req->r_num_ops);
755 prev_op = &osd_req->r_ops[which];
756 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
757 /* dup previous one */
758 op->indata_len = prev_op->indata_len;
759 op->outdata_len = prev_op->outdata_len;
760 op->extent = prev_op->extent;
762 op->extent.offset += offset_inc;
763 op->extent.length -= offset_inc;
765 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
766 op->indata_len -= offset_inc;
768 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
770 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
771 u16 opcode, const char *class, const char *method)
773 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
775 struct ceph_pagelist *pagelist;
776 size_t payload_len = 0;
779 BUG_ON(opcode != CEPH_OSD_OP_CALL);
781 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
785 ceph_pagelist_init(pagelist);
787 op->cls.class_name = class;
788 size = strlen(class);
789 BUG_ON(size > (size_t) U8_MAX);
790 op->cls.class_len = size;
791 ceph_pagelist_append(pagelist, class, size);
794 op->cls.method_name = method;
795 size = strlen(method);
796 BUG_ON(size > (size_t) U8_MAX);
797 op->cls.method_len = size;
798 ceph_pagelist_append(pagelist, method, size);
801 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
803 op->indata_len = payload_len;
806 EXPORT_SYMBOL(osd_req_op_cls_init);
808 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
809 u16 opcode, const char *name, const void *value,
810 size_t size, u8 cmp_op, u8 cmp_mode)
812 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
814 struct ceph_pagelist *pagelist;
817 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
819 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
823 ceph_pagelist_init(pagelist);
825 payload_len = strlen(name);
826 op->xattr.name_len = payload_len;
827 ceph_pagelist_append(pagelist, name, payload_len);
829 op->xattr.value_len = size;
830 ceph_pagelist_append(pagelist, value, size);
833 op->xattr.cmp_op = cmp_op;
834 op->xattr.cmp_mode = cmp_mode;
836 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
837 op->indata_len = payload_len;
840 EXPORT_SYMBOL(osd_req_op_xattr_init);
843 * @watch_opcode: CEPH_OSD_WATCH_OP_*
845 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
846 u64 cookie, u8 watch_opcode)
848 struct ceph_osd_req_op *op;
850 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
851 op->watch.cookie = cookie;
852 op->watch.op = watch_opcode;
856 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
858 u64 expected_object_size,
859 u64 expected_write_size)
861 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
862 CEPH_OSD_OP_SETALLOCHINT,
865 op->alloc_hint.expected_object_size = expected_object_size;
866 op->alloc_hint.expected_write_size = expected_write_size;
869 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
870 * not worth a feature bit. Set FAILOK per-op flag to make
871 * sure older osds don't trip over an unsupported opcode.
873 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
875 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
877 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
878 struct ceph_osd_data *osd_data)
880 u64 length = ceph_osd_data_length(osd_data);
882 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
883 BUG_ON(length > (u64) SIZE_MAX);
885 ceph_msg_data_add_pages(msg, osd_data->pages,
886 length, osd_data->alignment);
887 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
889 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
891 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
892 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
894 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
895 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
897 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
901 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
902 const struct ceph_osd_req_op *src)
904 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
905 pr_err("unrecognized osd opcode %d\n", src->op);
911 case CEPH_OSD_OP_STAT:
913 case CEPH_OSD_OP_READ:
914 case CEPH_OSD_OP_WRITE:
915 case CEPH_OSD_OP_WRITEFULL:
916 case CEPH_OSD_OP_ZERO:
917 case CEPH_OSD_OP_TRUNCATE:
918 dst->extent.offset = cpu_to_le64(src->extent.offset);
919 dst->extent.length = cpu_to_le64(src->extent.length);
920 dst->extent.truncate_size =
921 cpu_to_le64(src->extent.truncate_size);
922 dst->extent.truncate_seq =
923 cpu_to_le32(src->extent.truncate_seq);
925 case CEPH_OSD_OP_CALL:
926 dst->cls.class_len = src->cls.class_len;
927 dst->cls.method_len = src->cls.method_len;
928 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
930 case CEPH_OSD_OP_WATCH:
931 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
932 dst->watch.ver = cpu_to_le64(0);
933 dst->watch.op = src->watch.op;
934 dst->watch.gen = cpu_to_le32(src->watch.gen);
936 case CEPH_OSD_OP_NOTIFY_ACK:
938 case CEPH_OSD_OP_NOTIFY:
939 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
941 case CEPH_OSD_OP_LIST_WATCHERS:
943 case CEPH_OSD_OP_SETALLOCHINT:
944 dst->alloc_hint.expected_object_size =
945 cpu_to_le64(src->alloc_hint.expected_object_size);
946 dst->alloc_hint.expected_write_size =
947 cpu_to_le64(src->alloc_hint.expected_write_size);
949 case CEPH_OSD_OP_SETXATTR:
950 case CEPH_OSD_OP_CMPXATTR:
951 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
952 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
953 dst->xattr.cmp_op = src->xattr.cmp_op;
954 dst->xattr.cmp_mode = src->xattr.cmp_mode;
956 case CEPH_OSD_OP_CREATE:
957 case CEPH_OSD_OP_DELETE:
960 pr_err("unsupported osd opcode %s\n",
961 ceph_osd_op_name(src->op));
967 dst->op = cpu_to_le16(src->op);
968 dst->flags = cpu_to_le32(src->flags);
969 dst->payload_len = cpu_to_le32(src->indata_len);
971 return src->indata_len;
975 * build new request AND message, calculate layout, and adjust file
978 * if the file was recently truncated, we include information about its
979 * old and new size so that the object can be updated appropriately. (we
980 * avoid synchronously deleting truncated objects because it's slow.)
982 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
983 struct ceph_file_layout *layout,
984 struct ceph_vino vino,
986 unsigned int which, int num_ops,
987 int opcode, int flags,
988 struct ceph_snap_context *snapc,
993 struct ceph_osd_request *req;
999 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1000 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1001 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
1003 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1010 /* calculate max write size */
1011 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1015 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1016 osd_req_op_init(req, which, opcode, 0);
1018 u32 object_size = layout->object_size;
1019 u32 object_base = off - objoff;
1020 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1021 if (truncate_size <= object_base) {
1024 truncate_size -= object_base;
1025 if (truncate_size > object_size)
1026 truncate_size = object_size;
1029 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1030 truncate_size, truncate_seq);
1033 req->r_abort_on_full = true;
1034 req->r_flags = flags;
1035 req->r_base_oloc.pool = layout->pool_id;
1036 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1037 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1039 req->r_snapid = vino.snap;
1040 if (flags & CEPH_OSD_FLAG_WRITE)
1041 req->r_data_offset = off;
1043 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1050 ceph_osdc_put_request(req);
1053 EXPORT_SYMBOL(ceph_osdc_new_request);
1056 * We keep osd requests in an rbtree, sorted by ->r_tid.
1058 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1059 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1062 * Call @fn on each OSD request as long as @fn returns 0.
1064 static void for_each_request(struct ceph_osd_client *osdc,
1065 int (*fn)(struct ceph_osd_request *req, void *arg),
1068 struct rb_node *n, *p;
1070 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1071 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1073 for (p = rb_first(&osd->o_requests); p; ) {
1074 struct ceph_osd_request *req =
1075 rb_entry(p, struct ceph_osd_request, r_node);
1083 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1084 struct ceph_osd_request *req =
1085 rb_entry(p, struct ceph_osd_request, r_node);
1093 static bool osd_homeless(struct ceph_osd *osd)
1095 return osd->o_osd == CEPH_HOMELESS_OSD;
1098 static bool osd_registered(struct ceph_osd *osd)
1100 verify_osdc_locked(osd->o_osdc);
1102 return !RB_EMPTY_NODE(&osd->o_node);
1106 * Assumes @osd is zero-initialized.
1108 static void osd_init(struct ceph_osd *osd)
1110 refcount_set(&osd->o_ref, 1);
1111 RB_CLEAR_NODE(&osd->o_node);
1112 osd->o_requests = RB_ROOT;
1113 osd->o_linger_requests = RB_ROOT;
1114 osd->o_backoff_mappings = RB_ROOT;
1115 osd->o_backoffs_by_id = RB_ROOT;
1116 INIT_LIST_HEAD(&osd->o_osd_lru);
1117 INIT_LIST_HEAD(&osd->o_keepalive_item);
1118 osd->o_incarnation = 1;
1119 mutex_init(&osd->lock);
1122 static void osd_cleanup(struct ceph_osd *osd)
1124 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1125 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1126 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1127 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1128 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1129 WARN_ON(!list_empty(&osd->o_osd_lru));
1130 WARN_ON(!list_empty(&osd->o_keepalive_item));
1132 if (osd->o_auth.authorizer) {
1133 WARN_ON(osd_homeless(osd));
1134 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1139 * Track open sessions with osds.
1141 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1143 struct ceph_osd *osd;
1145 WARN_ON(onum == CEPH_HOMELESS_OSD);
1147 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1152 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1157 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1159 if (refcount_inc_not_zero(&osd->o_ref)) {
1160 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1161 refcount_read(&osd->o_ref));
1164 dout("get_osd %p FAIL\n", osd);
1169 static void put_osd(struct ceph_osd *osd)
1171 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1172 refcount_read(&osd->o_ref) - 1);
1173 if (refcount_dec_and_test(&osd->o_ref)) {
1179 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1181 static void __move_osd_to_lru(struct ceph_osd *osd)
1183 struct ceph_osd_client *osdc = osd->o_osdc;
1185 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1186 BUG_ON(!list_empty(&osd->o_osd_lru));
1188 spin_lock(&osdc->osd_lru_lock);
1189 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1190 spin_unlock(&osdc->osd_lru_lock);
1192 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1195 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1197 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1198 RB_EMPTY_ROOT(&osd->o_linger_requests))
1199 __move_osd_to_lru(osd);
1202 static void __remove_osd_from_lru(struct ceph_osd *osd)
1204 struct ceph_osd_client *osdc = osd->o_osdc;
1206 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1208 spin_lock(&osdc->osd_lru_lock);
1209 if (!list_empty(&osd->o_osd_lru))
1210 list_del_init(&osd->o_osd_lru);
1211 spin_unlock(&osdc->osd_lru_lock);
1215 * Close the connection and assign any leftover requests to the
1218 static void close_osd(struct ceph_osd *osd)
1220 struct ceph_osd_client *osdc = osd->o_osdc;
1223 verify_osdc_wrlocked(osdc);
1224 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1226 ceph_con_close(&osd->o_con);
1228 for (n = rb_first(&osd->o_requests); n; ) {
1229 struct ceph_osd_request *req =
1230 rb_entry(n, struct ceph_osd_request, r_node);
1232 n = rb_next(n); /* unlink_request() */
1234 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1235 unlink_request(osd, req);
1236 link_request(&osdc->homeless_osd, req);
1238 for (n = rb_first(&osd->o_linger_requests); n; ) {
1239 struct ceph_osd_linger_request *lreq =
1240 rb_entry(n, struct ceph_osd_linger_request, node);
1242 n = rb_next(n); /* unlink_linger() */
1244 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1246 unlink_linger(osd, lreq);
1247 link_linger(&osdc->homeless_osd, lreq);
1249 clear_backoffs(osd);
1251 __remove_osd_from_lru(osd);
1252 erase_osd(&osdc->osds, osd);
1259 static int reopen_osd(struct ceph_osd *osd)
1261 struct ceph_entity_addr *peer_addr;
1263 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1265 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1266 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1271 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1272 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1273 !ceph_con_opened(&osd->o_con)) {
1276 dout("osd addr hasn't changed and connection never opened, "
1277 "letting msgr retry\n");
1278 /* touch each r_stamp for handle_timeout()'s benfit */
1279 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1280 struct ceph_osd_request *req =
1281 rb_entry(n, struct ceph_osd_request, r_node);
1282 req->r_stamp = jiffies;
1288 ceph_con_close(&osd->o_con);
1289 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1290 osd->o_incarnation++;
1295 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1298 struct ceph_osd *osd;
1301 verify_osdc_wrlocked(osdc);
1303 verify_osdc_locked(osdc);
1305 if (o != CEPH_HOMELESS_OSD)
1306 osd = lookup_osd(&osdc->osds, o);
1308 osd = &osdc->homeless_osd;
1311 return ERR_PTR(-EAGAIN);
1313 osd = create_osd(osdc, o);
1314 insert_osd(&osdc->osds, osd);
1315 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1316 &osdc->osdmap->osd_addr[osd->o_osd]);
1319 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1324 * Create request <-> OSD session relation.
1326 * @req has to be assigned a tid, @osd may be homeless.
1328 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1330 verify_osd_locked(osd);
1331 WARN_ON(!req->r_tid || req->r_osd);
1332 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1335 if (!osd_homeless(osd))
1336 __remove_osd_from_lru(osd);
1338 atomic_inc(&osd->o_osdc->num_homeless);
1341 insert_request(&osd->o_requests, req);
1345 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1347 verify_osd_locked(osd);
1348 WARN_ON(req->r_osd != osd);
1349 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1353 erase_request(&osd->o_requests, req);
1356 if (!osd_homeless(osd))
1357 maybe_move_osd_to_lru(osd);
1359 atomic_dec(&osd->o_osdc->num_homeless);
1362 static bool __pool_full(struct ceph_pg_pool_info *pi)
1364 return pi->flags & CEPH_POOL_FLAG_FULL;
1367 static bool have_pool_full(struct ceph_osd_client *osdc)
1371 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1372 struct ceph_pg_pool_info *pi =
1373 rb_entry(n, struct ceph_pg_pool_info, node);
1375 if (__pool_full(pi))
1382 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1384 struct ceph_pg_pool_info *pi;
1386 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1390 return __pool_full(pi);
1394 * Returns whether a request should be blocked from being sent
1395 * based on the current osdmap and osd_client settings.
1397 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1398 const struct ceph_osd_request_target *t,
1399 struct ceph_pg_pool_info *pi)
1401 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1402 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1403 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1406 WARN_ON(pi->id != t->target_oloc.pool);
1407 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1408 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1409 (osdc->osdmap->epoch < osdc->epoch_barrier);
1412 enum calc_target_result {
1413 CALC_TARGET_NO_ACTION = 0,
1414 CALC_TARGET_NEED_RESEND,
1415 CALC_TARGET_POOL_DNE,
1418 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1419 struct ceph_osd_request_target *t,
1420 struct ceph_connection *con,
1423 struct ceph_pg_pool_info *pi;
1424 struct ceph_pg pgid, last_pgid;
1425 struct ceph_osds up, acting;
1426 bool force_resend = false;
1427 bool unpaused = false;
1430 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1431 bool recovery_deletes = ceph_osdmap_flag(osdc,
1432 CEPH_OSDMAP_RECOVERY_DELETES);
1433 enum calc_target_result ct_res;
1436 t->epoch = osdc->osdmap->epoch;
1437 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1439 t->osd = CEPH_HOMELESS_OSD;
1440 ct_res = CALC_TARGET_POOL_DNE;
1444 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1445 if (t->last_force_resend < pi->last_force_request_resend) {
1446 t->last_force_resend = pi->last_force_request_resend;
1447 force_resend = true;
1448 } else if (t->last_force_resend == 0) {
1449 force_resend = true;
1454 ceph_oid_copy(&t->target_oid, &t->base_oid);
1455 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1456 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1457 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1458 t->target_oloc.pool = pi->read_tier;
1459 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1460 t->target_oloc.pool = pi->write_tier;
1462 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1464 t->osd = CEPH_HOMELESS_OSD;
1465 ct_res = CALC_TARGET_POOL_DNE;
1470 ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1473 WARN_ON(ret != -ENOENT);
1474 t->osd = CEPH_HOMELESS_OSD;
1475 ct_res = CALC_TARGET_POOL_DNE;
1478 last_pgid.pool = pgid.pool;
1479 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1481 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1483 ceph_is_new_interval(&t->acting,
1495 t->recovery_deletes,
1498 force_resend = true;
1500 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1504 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1505 ceph_osds_changed(&t->acting, &acting, any_change);
1507 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1509 if (legacy_change || force_resend || split) {
1510 t->pgid = pgid; /* struct */
1511 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1512 ceph_osds_copy(&t->acting, &acting);
1513 ceph_osds_copy(&t->up, &up);
1515 t->min_size = pi->min_size;
1516 t->pg_num = pi->pg_num;
1517 t->pg_num_mask = pi->pg_num_mask;
1518 t->sort_bitwise = sort_bitwise;
1519 t->recovery_deletes = recovery_deletes;
1521 t->osd = acting.primary;
1524 if (unpaused || legacy_change || force_resend ||
1525 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1527 ct_res = CALC_TARGET_NEED_RESEND;
1529 ct_res = CALC_TARGET_NO_ACTION;
1532 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1536 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1538 struct ceph_spg_mapping *spg;
1540 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1544 RB_CLEAR_NODE(&spg->node);
1545 spg->backoffs = RB_ROOT;
1549 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1551 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1552 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1558 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1559 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1560 * defined only within a specific spgid; it does not pass anything to
1561 * children on split, or to another primary.
1563 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1564 RB_BYPTR, const struct ceph_spg *, node)
1566 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1568 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1571 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1572 void **pkey, size_t *pkey_len)
1574 if (hoid->key_len) {
1576 *pkey_len = hoid->key_len;
1579 *pkey_len = hoid->oid_len;
1583 static int compare_names(const void *name1, size_t name1_len,
1584 const void *name2, size_t name2_len)
1588 ret = memcmp(name1, name2, min(name1_len, name2_len));
1590 if (name1_len < name2_len)
1592 else if (name1_len > name2_len)
1598 static int hoid_compare(const struct ceph_hobject_id *lhs,
1599 const struct ceph_hobject_id *rhs)
1601 void *effective_key1, *effective_key2;
1602 size_t effective_key1_len, effective_key2_len;
1605 if (lhs->is_max < rhs->is_max)
1607 if (lhs->is_max > rhs->is_max)
1610 if (lhs->pool < rhs->pool)
1612 if (lhs->pool > rhs->pool)
1615 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1617 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1620 ret = compare_names(lhs->nspace, lhs->nspace_len,
1621 rhs->nspace, rhs->nspace_len);
1625 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1626 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1627 ret = compare_names(effective_key1, effective_key1_len,
1628 effective_key2, effective_key2_len);
1632 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1636 if (lhs->snapid < rhs->snapid)
1638 if (lhs->snapid > rhs->snapid)
1645 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1646 * compat stuff here.
1648 * Assumes @hoid is zero-initialized.
1650 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1656 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1662 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1666 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1668 if (IS_ERR(hoid->key)) {
1669 ret = PTR_ERR(hoid->key);
1674 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1676 if (IS_ERR(hoid->oid)) {
1677 ret = PTR_ERR(hoid->oid);
1682 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1683 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1684 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1686 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1688 if (IS_ERR(hoid->nspace)) {
1689 ret = PTR_ERR(hoid->nspace);
1690 hoid->nspace = NULL;
1694 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1696 ceph_hoid_build_hash_cache(hoid);
1703 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1705 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1706 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1709 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1711 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1712 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1713 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1714 ceph_encode_64(p, hoid->snapid);
1715 ceph_encode_32(p, hoid->hash);
1716 ceph_encode_8(p, hoid->is_max);
1717 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1718 ceph_encode_64(p, hoid->pool);
1721 static void free_hoid(struct ceph_hobject_id *hoid)
1726 kfree(hoid->nspace);
1731 static struct ceph_osd_backoff *alloc_backoff(void)
1733 struct ceph_osd_backoff *backoff;
1735 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1739 RB_CLEAR_NODE(&backoff->spg_node);
1740 RB_CLEAR_NODE(&backoff->id_node);
1744 static void free_backoff(struct ceph_osd_backoff *backoff)
1746 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1747 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1749 free_hoid(backoff->begin);
1750 free_hoid(backoff->end);
1755 * Within a specific spgid, backoffs are managed by ->begin hoid.
1757 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1758 RB_BYVAL, spg_node);
1760 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1761 const struct ceph_hobject_id *hoid)
1763 struct rb_node *n = root->rb_node;
1766 struct ceph_osd_backoff *cur =
1767 rb_entry(n, struct ceph_osd_backoff, spg_node);
1770 cmp = hoid_compare(hoid, cur->begin);
1773 } else if (cmp > 0) {
1774 if (hoid_compare(hoid, cur->end) < 0)
1787 * Each backoff has a unique id within its OSD session.
1789 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1791 static void clear_backoffs(struct ceph_osd *osd)
1793 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1794 struct ceph_spg_mapping *spg =
1795 rb_entry(rb_first(&osd->o_backoff_mappings),
1796 struct ceph_spg_mapping, node);
1798 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1799 struct ceph_osd_backoff *backoff =
1800 rb_entry(rb_first(&spg->backoffs),
1801 struct ceph_osd_backoff, spg_node);
1803 erase_backoff(&spg->backoffs, backoff);
1804 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1805 free_backoff(backoff);
1807 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1808 free_spg_mapping(spg);
1813 * Set up a temporary, non-owning view into @t.
1815 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1816 const struct ceph_osd_request_target *t)
1820 hoid->oid = t->target_oid.name;
1821 hoid->oid_len = t->target_oid.name_len;
1822 hoid->snapid = CEPH_NOSNAP;
1823 hoid->hash = t->pgid.seed;
1824 hoid->is_max = false;
1825 if (t->target_oloc.pool_ns) {
1826 hoid->nspace = t->target_oloc.pool_ns->str;
1827 hoid->nspace_len = t->target_oloc.pool_ns->len;
1829 hoid->nspace = NULL;
1830 hoid->nspace_len = 0;
1832 hoid->pool = t->target_oloc.pool;
1833 ceph_hoid_build_hash_cache(hoid);
1836 static bool should_plug_request(struct ceph_osd_request *req)
1838 struct ceph_osd *osd = req->r_osd;
1839 struct ceph_spg_mapping *spg;
1840 struct ceph_osd_backoff *backoff;
1841 struct ceph_hobject_id hoid;
1843 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1847 hoid_fill_from_target(&hoid, &req->r_t);
1848 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1852 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1853 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1854 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1858 static void setup_request_data(struct ceph_osd_request *req,
1859 struct ceph_msg *msg)
1864 if (!list_empty(&msg->data))
1867 WARN_ON(msg->data_length);
1868 for (i = 0; i < req->r_num_ops; i++) {
1869 struct ceph_osd_req_op *op = &req->r_ops[i];
1873 case CEPH_OSD_OP_WRITE:
1874 case CEPH_OSD_OP_WRITEFULL:
1875 WARN_ON(op->indata_len != op->extent.length);
1876 ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1878 case CEPH_OSD_OP_SETXATTR:
1879 case CEPH_OSD_OP_CMPXATTR:
1880 WARN_ON(op->indata_len != op->xattr.name_len +
1881 op->xattr.value_len);
1882 ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1884 case CEPH_OSD_OP_NOTIFY_ACK:
1885 ceph_osdc_msg_data_add(msg,
1886 &op->notify_ack.request_data);
1890 case CEPH_OSD_OP_STAT:
1891 ceph_osdc_msg_data_add(req->r_reply,
1894 case CEPH_OSD_OP_READ:
1895 ceph_osdc_msg_data_add(req->r_reply,
1896 &op->extent.osd_data);
1898 case CEPH_OSD_OP_LIST_WATCHERS:
1899 ceph_osdc_msg_data_add(req->r_reply,
1900 &op->list_watchers.response_data);
1904 case CEPH_OSD_OP_CALL:
1905 WARN_ON(op->indata_len != op->cls.class_len +
1906 op->cls.method_len +
1907 op->cls.indata_len);
1908 ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1909 /* optional, can be NONE */
1910 ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1911 /* optional, can be NONE */
1912 ceph_osdc_msg_data_add(req->r_reply,
1913 &op->cls.response_data);
1915 case CEPH_OSD_OP_NOTIFY:
1916 ceph_osdc_msg_data_add(msg,
1917 &op->notify.request_data);
1918 ceph_osdc_msg_data_add(req->r_reply,
1919 &op->notify.response_data);
1923 data_len += op->indata_len;
1926 WARN_ON(data_len != msg->data_length);
1929 static void encode_pgid(void **p, const struct ceph_pg *pgid)
1931 ceph_encode_8(p, 1);
1932 ceph_encode_64(p, pgid->pool);
1933 ceph_encode_32(p, pgid->seed);
1934 ceph_encode_32(p, -1); /* preferred */
1937 static void encode_spgid(void **p, const struct ceph_spg *spgid)
1939 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1940 encode_pgid(p, &spgid->pgid);
1941 ceph_encode_8(p, spgid->shard);
1944 static void encode_oloc(void **p, void *end,
1945 const struct ceph_object_locator *oloc)
1947 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1948 ceph_encode_64(p, oloc->pool);
1949 ceph_encode_32(p, -1); /* preferred */
1950 ceph_encode_32(p, 0); /* key len */
1952 ceph_encode_string(p, end, oloc->pool_ns->str,
1953 oloc->pool_ns->len);
1955 ceph_encode_32(p, 0);
1958 static void encode_request_partial(struct ceph_osd_request *req,
1959 struct ceph_msg *msg)
1961 void *p = msg->front.iov_base;
1962 void *const end = p + msg->front_alloc_len;
1966 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1967 /* snapshots aren't writeable */
1968 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1970 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1971 req->r_data_offset || req->r_snapc);
1974 setup_request_data(req, msg);
1976 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1977 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1978 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1979 ceph_encode_32(&p, req->r_flags);
1982 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1983 memset(p, 0, sizeof(struct ceph_osd_reqid));
1984 p += sizeof(struct ceph_osd_reqid);
1987 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1988 p += sizeof(struct ceph_blkin_trace_info);
1990 ceph_encode_32(&p, 0); /* client_inc, always 0 */
1991 ceph_encode_timespec(p, &req->r_mtime);
1992 p += sizeof(struct ceph_timespec);
1994 encode_oloc(&p, end, &req->r_t.target_oloc);
1995 ceph_encode_string(&p, end, req->r_t.target_oid.name,
1996 req->r_t.target_oid.name_len);
1998 /* ops, can imply data */
1999 ceph_encode_16(&p, req->r_num_ops);
2000 for (i = 0; i < req->r_num_ops; i++) {
2001 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2002 p += sizeof(struct ceph_osd_op);
2005 ceph_encode_64(&p, req->r_snapid); /* snapid */
2007 ceph_encode_64(&p, req->r_snapc->seq);
2008 ceph_encode_32(&p, req->r_snapc->num_snaps);
2009 for (i = 0; i < req->r_snapc->num_snaps; i++)
2010 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2012 ceph_encode_64(&p, 0); /* snap_seq */
2013 ceph_encode_32(&p, 0); /* snaps len */
2016 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2017 BUG_ON(p > end - 8); /* space for features */
2019 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2020 /* front_len is finalized in encode_request_finish() */
2021 msg->front.iov_len = p - msg->front.iov_base;
2022 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2023 msg->hdr.data_len = cpu_to_le32(data_len);
2025 * The header "data_off" is a hint to the receiver allowing it
2026 * to align received data into its buffers such that there's no
2027 * need to re-copy it before writing it to disk (direct I/O).
2029 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2031 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2032 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2035 static void encode_request_finish(struct ceph_msg *msg)
2037 void *p = msg->front.iov_base;
2038 void *const partial_end = p + msg->front.iov_len;
2039 void *const end = p + msg->front_alloc_len;
2041 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2042 /* luminous OSD -- encode features and be done */
2044 ceph_encode_64(&p, msg->con->peer_features);
2047 char spgid[CEPH_ENCODING_START_BLK_LEN +
2048 CEPH_PGID_ENCODING_LEN + 1];
2052 char reqid[CEPH_ENCODING_START_BLK_LEN +
2053 sizeof(struct ceph_osd_reqid)];
2054 char trace[sizeof(struct ceph_blkin_trace_info)];
2056 struct ceph_timespec mtime;
2058 struct ceph_pg pgid;
2059 void *oloc, *oid, *tail;
2060 int oloc_len, oid_len, tail_len;
2064 * Pre-luminous OSD -- reencode v8 into v4 using @head
2065 * as a temporary buffer. Encode the raw PG; the rest
2066 * is just a matter of moving oloc, oid and tail blobs
2069 memcpy(&head, p, sizeof(head));
2073 p += CEPH_ENCODING_START_BLK_LEN;
2074 pgid.pool = ceph_decode_64(&p);
2075 p += 4 + 4; /* preferred, key len */
2076 len = ceph_decode_32(&p);
2077 p += len; /* nspace */
2078 oloc_len = p - oloc;
2081 len = ceph_decode_32(&p);
2086 tail_len = partial_end - p;
2088 p = msg->front.iov_base;
2089 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2090 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2091 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2092 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2094 /* reassert_version */
2095 memset(p, 0, sizeof(struct ceph_eversion));
2096 p += sizeof(struct ceph_eversion);
2099 memmove(p, oloc, oloc_len);
2102 pgid.seed = le32_to_cpu(head.hash);
2103 encode_pgid(&p, &pgid); /* raw pg */
2106 memmove(p, oid, oid_len);
2109 /* tail -- ops, snapid, snapc, retry_attempt */
2111 memmove(p, tail, tail_len);
2114 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2118 msg->front.iov_len = p - msg->front.iov_base;
2119 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2121 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2122 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2123 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2124 le16_to_cpu(msg->hdr.version));
2128 * @req has to be assigned a tid and registered.
2130 static void send_request(struct ceph_osd_request *req)
2132 struct ceph_osd *osd = req->r_osd;
2134 verify_osd_locked(osd);
2135 WARN_ON(osd->o_osd != req->r_t.osd);
2138 if (should_plug_request(req))
2142 * We may have a previously queued request message hanging
2143 * around. Cancel it to avoid corrupting the msgr.
2146 ceph_msg_revoke(req->r_request);
2148 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2149 if (req->r_attempts)
2150 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2152 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2154 encode_request_partial(req, req->r_request);
2156 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2157 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2158 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2159 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2162 req->r_t.paused = false;
2163 req->r_stamp = jiffies;
2166 req->r_sent = osd->o_incarnation;
2167 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2168 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2171 static void maybe_request_map(struct ceph_osd_client *osdc)
2173 bool continuous = false;
2175 verify_osdc_locked(osdc);
2176 WARN_ON(!osdc->osdmap->epoch);
2178 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2179 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2180 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2181 dout("%s osdc %p continuous\n", __func__, osdc);
2184 dout("%s osdc %p onetime\n", __func__, osdc);
2187 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2188 osdc->osdmap->epoch + 1, continuous))
2189 ceph_monc_renew_subs(&osdc->client->monc);
2192 static void complete_request(struct ceph_osd_request *req, int err);
2193 static void send_map_check(struct ceph_osd_request *req);
2195 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2197 struct ceph_osd_client *osdc = req->r_osdc;
2198 struct ceph_osd *osd;
2199 enum calc_target_result ct_res;
2201 bool need_send = false;
2202 bool promoted = false;
2204 WARN_ON(req->r_tid);
2205 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2208 ct_res = calc_target(osdc, &req->r_t, NULL, false);
2209 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2212 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2214 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2218 if (osdc->abort_err) {
2219 dout("req %p abort_err %d\n", req, osdc->abort_err);
2220 err = osdc->abort_err;
2221 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2222 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2223 osdc->epoch_barrier);
2224 req->r_t.paused = true;
2225 maybe_request_map(osdc);
2226 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2227 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2228 dout("req %p pausewr\n", req);
2229 req->r_t.paused = true;
2230 maybe_request_map(osdc);
2231 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2232 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2233 dout("req %p pauserd\n", req);
2234 req->r_t.paused = true;
2235 maybe_request_map(osdc);
2236 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2237 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2238 CEPH_OSD_FLAG_FULL_FORCE)) &&
2239 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2240 pool_full(osdc, req->r_t.base_oloc.pool))) {
2241 dout("req %p full/pool_full\n", req);
2242 if (req->r_abort_on_full) {
2245 pr_warn_ratelimited("FULL or reached pool quota\n");
2246 req->r_t.paused = true;
2247 maybe_request_map(osdc);
2249 } else if (!osd_homeless(osd)) {
2252 maybe_request_map(osdc);
2255 mutex_lock(&osd->lock);
2257 * Assign the tid atomically with send_request() to protect
2258 * multiple writes to the same object from racing with each
2259 * other, resulting in out of order ops on the OSDs.
2261 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2262 link_request(osd, req);
2266 complete_request(req, err);
2267 mutex_unlock(&osd->lock);
2269 if (ct_res == CALC_TARGET_POOL_DNE)
2270 send_map_check(req);
2273 downgrade_write(&osdc->lock);
2277 up_read(&osdc->lock);
2278 down_write(&osdc->lock);
2284 static void account_request(struct ceph_osd_request *req)
2286 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2287 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2289 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2290 atomic_inc(&req->r_osdc->num_requests);
2292 req->r_start_stamp = jiffies;
2295 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2297 ceph_osdc_get_request(req);
2298 account_request(req);
2299 __submit_request(req, wrlocked);
2302 static void finish_request(struct ceph_osd_request *req)
2304 struct ceph_osd_client *osdc = req->r_osdc;
2306 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2307 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2310 unlink_request(req->r_osd, req);
2311 atomic_dec(&osdc->num_requests);
2314 * If an OSD has failed or returned and a request has been sent
2315 * twice, it's possible to get a reply and end up here while the
2316 * request message is queued for delivery. We will ignore the
2317 * reply, so not a big deal, but better to try and catch it.
2319 ceph_msg_revoke(req->r_request);
2320 ceph_msg_revoke_incoming(req->r_reply);
2323 static void __complete_request(struct ceph_osd_request *req)
2325 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2326 req->r_tid, req->r_callback, req->r_result);
2328 if (req->r_callback)
2329 req->r_callback(req);
2330 complete_all(&req->r_completion);
2331 ceph_osdc_put_request(req);
2334 static void complete_request_workfn(struct work_struct *work)
2336 struct ceph_osd_request *req =
2337 container_of(work, struct ceph_osd_request, r_complete_work);
2339 __complete_request(req);
2343 * This is open-coded in handle_reply().
2345 static void complete_request(struct ceph_osd_request *req, int err)
2347 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2349 req->r_result = err;
2350 finish_request(req);
2352 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2353 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
2356 static void cancel_map_check(struct ceph_osd_request *req)
2358 struct ceph_osd_client *osdc = req->r_osdc;
2359 struct ceph_osd_request *lookup_req;
2361 verify_osdc_wrlocked(osdc);
2363 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2367 WARN_ON(lookup_req != req);
2368 erase_request_mc(&osdc->map_checks, req);
2369 ceph_osdc_put_request(req);
2372 static void cancel_request(struct ceph_osd_request *req)
2374 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2376 cancel_map_check(req);
2377 finish_request(req);
2378 complete_all(&req->r_completion);
2379 ceph_osdc_put_request(req);
2382 static void abort_request(struct ceph_osd_request *req, int err)
2384 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2386 cancel_map_check(req);
2387 complete_request(req, err);
2390 static int abort_fn(struct ceph_osd_request *req, void *arg)
2392 int err = *(int *)arg;
2394 abort_request(req, err);
2395 return 0; /* continue iteration */
2399 * Abort all in-flight requests with @err and arrange for all future
2400 * requests to be failed immediately.
2402 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2404 dout("%s osdc %p err %d\n", __func__, osdc, err);
2405 down_write(&osdc->lock);
2406 for_each_request(osdc, abort_fn, &err);
2407 osdc->abort_err = err;
2408 up_write(&osdc->lock);
2410 EXPORT_SYMBOL(ceph_osdc_abort_requests);
2412 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2414 if (likely(eb > osdc->epoch_barrier)) {
2415 dout("updating epoch_barrier from %u to %u\n",
2416 osdc->epoch_barrier, eb);
2417 osdc->epoch_barrier = eb;
2418 /* Request map if we're not to the barrier yet */
2419 if (eb > osdc->osdmap->epoch)
2420 maybe_request_map(osdc);
2424 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2426 down_read(&osdc->lock);
2427 if (unlikely(eb > osdc->epoch_barrier)) {
2428 up_read(&osdc->lock);
2429 down_write(&osdc->lock);
2430 update_epoch_barrier(osdc, eb);
2431 up_write(&osdc->lock);
2433 up_read(&osdc->lock);
2436 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2439 * We can end up releasing caps as a result of abort_request().
2440 * In that case, we probably want to ensure that the cap release message
2441 * has an updated epoch barrier in it, so set the epoch barrier prior to
2442 * aborting the first request.
2444 static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2446 struct ceph_osd_client *osdc = req->r_osdc;
2447 bool *victims = arg;
2449 if (req->r_abort_on_full &&
2450 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2451 pool_full(osdc, req->r_t.target_oloc.pool))) {
2453 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2456 abort_request(req, -ENOSPC);
2459 return 0; /* continue iteration */
2463 * Drop all pending requests that are stalled waiting on a full condition to
2464 * clear, and complete them with ENOSPC as the return code. Set the
2465 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2468 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2470 bool victims = false;
2472 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc))
2473 for_each_request(osdc, abort_on_full_fn, &victims);
2476 static void check_pool_dne(struct ceph_osd_request *req)
2478 struct ceph_osd_client *osdc = req->r_osdc;
2479 struct ceph_osdmap *map = osdc->osdmap;
2481 verify_osdc_wrlocked(osdc);
2482 WARN_ON(!map->epoch);
2484 if (req->r_attempts) {
2486 * We sent a request earlier, which means that
2487 * previously the pool existed, and now it does not
2488 * (i.e., it was deleted).
2490 req->r_map_dne_bound = map->epoch;
2491 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2494 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2495 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2498 if (req->r_map_dne_bound) {
2499 if (map->epoch >= req->r_map_dne_bound) {
2500 /* we had a new enough map */
2501 pr_info_ratelimited("tid %llu pool does not exist\n",
2503 complete_request(req, -ENOENT);
2506 send_map_check(req);
2510 static void map_check_cb(struct ceph_mon_generic_request *greq)
2512 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2513 struct ceph_osd_request *req;
2514 u64 tid = greq->private_data;
2516 WARN_ON(greq->result || !greq->u.newest);
2518 down_write(&osdc->lock);
2519 req = lookup_request_mc(&osdc->map_checks, tid);
2521 dout("%s tid %llu dne\n", __func__, tid);
2525 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2526 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2527 if (!req->r_map_dne_bound)
2528 req->r_map_dne_bound = greq->u.newest;
2529 erase_request_mc(&osdc->map_checks, req);
2530 check_pool_dne(req);
2532 ceph_osdc_put_request(req);
2534 up_write(&osdc->lock);
2537 static void send_map_check(struct ceph_osd_request *req)
2539 struct ceph_osd_client *osdc = req->r_osdc;
2540 struct ceph_osd_request *lookup_req;
2543 verify_osdc_wrlocked(osdc);
2545 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2547 WARN_ON(lookup_req != req);
2551 ceph_osdc_get_request(req);
2552 insert_request_mc(&osdc->map_checks, req);
2553 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2554 map_check_cb, req->r_tid);
2559 * lingering requests, watch/notify v2 infrastructure
2561 static void linger_release(struct kref *kref)
2563 struct ceph_osd_linger_request *lreq =
2564 container_of(kref, struct ceph_osd_linger_request, kref);
2566 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2567 lreq->reg_req, lreq->ping_req);
2568 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2569 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2570 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2571 WARN_ON(!list_empty(&lreq->scan_item));
2572 WARN_ON(!list_empty(&lreq->pending_lworks));
2576 ceph_osdc_put_request(lreq->reg_req);
2578 ceph_osdc_put_request(lreq->ping_req);
2579 target_destroy(&lreq->t);
2583 static void linger_put(struct ceph_osd_linger_request *lreq)
2586 kref_put(&lreq->kref, linger_release);
2589 static struct ceph_osd_linger_request *
2590 linger_get(struct ceph_osd_linger_request *lreq)
2592 kref_get(&lreq->kref);
2596 static struct ceph_osd_linger_request *
2597 linger_alloc(struct ceph_osd_client *osdc)
2599 struct ceph_osd_linger_request *lreq;
2601 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2605 kref_init(&lreq->kref);
2606 mutex_init(&lreq->lock);
2607 RB_CLEAR_NODE(&lreq->node);
2608 RB_CLEAR_NODE(&lreq->osdc_node);
2609 RB_CLEAR_NODE(&lreq->mc_node);
2610 INIT_LIST_HEAD(&lreq->scan_item);
2611 INIT_LIST_HEAD(&lreq->pending_lworks);
2612 init_completion(&lreq->reg_commit_wait);
2613 init_completion(&lreq->notify_finish_wait);
2616 target_init(&lreq->t);
2618 dout("%s lreq %p\n", __func__, lreq);
2622 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2623 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2624 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2627 * Create linger request <-> OSD session relation.
2629 * @lreq has to be registered, @osd may be homeless.
2631 static void link_linger(struct ceph_osd *osd,
2632 struct ceph_osd_linger_request *lreq)
2634 verify_osd_locked(osd);
2635 WARN_ON(!lreq->linger_id || lreq->osd);
2636 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2637 osd->o_osd, lreq, lreq->linger_id);
2639 if (!osd_homeless(osd))
2640 __remove_osd_from_lru(osd);
2642 atomic_inc(&osd->o_osdc->num_homeless);
2645 insert_linger(&osd->o_linger_requests, lreq);
2649 static void unlink_linger(struct ceph_osd *osd,
2650 struct ceph_osd_linger_request *lreq)
2652 verify_osd_locked(osd);
2653 WARN_ON(lreq->osd != osd);
2654 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2655 osd->o_osd, lreq, lreq->linger_id);
2658 erase_linger(&osd->o_linger_requests, lreq);
2661 if (!osd_homeless(osd))
2662 maybe_move_osd_to_lru(osd);
2664 atomic_dec(&osd->o_osdc->num_homeless);
2667 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2669 verify_osdc_locked(lreq->osdc);
2671 return !RB_EMPTY_NODE(&lreq->osdc_node);
2674 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2676 struct ceph_osd_client *osdc = lreq->osdc;
2679 down_read(&osdc->lock);
2680 registered = __linger_registered(lreq);
2681 up_read(&osdc->lock);
2686 static void linger_register(struct ceph_osd_linger_request *lreq)
2688 struct ceph_osd_client *osdc = lreq->osdc;
2690 verify_osdc_wrlocked(osdc);
2691 WARN_ON(lreq->linger_id);
2694 lreq->linger_id = ++osdc->last_linger_id;
2695 insert_linger_osdc(&osdc->linger_requests, lreq);
2698 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2700 struct ceph_osd_client *osdc = lreq->osdc;
2702 verify_osdc_wrlocked(osdc);
2704 erase_linger_osdc(&osdc->linger_requests, lreq);
2708 static void cancel_linger_request(struct ceph_osd_request *req)
2710 struct ceph_osd_linger_request *lreq = req->r_priv;
2712 WARN_ON(!req->r_linger);
2713 cancel_request(req);
2717 struct linger_work {
2718 struct work_struct work;
2719 struct ceph_osd_linger_request *lreq;
2720 struct list_head pending_item;
2721 unsigned long queued_stamp;
2727 void *payload; /* points into @msg front */
2730 struct ceph_msg *msg; /* for ceph_msg_put() */
2738 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2741 struct linger_work *lwork;
2743 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2747 INIT_WORK(&lwork->work, workfn);
2748 INIT_LIST_HEAD(&lwork->pending_item);
2749 lwork->lreq = linger_get(lreq);
2754 static void lwork_free(struct linger_work *lwork)
2756 struct ceph_osd_linger_request *lreq = lwork->lreq;
2758 mutex_lock(&lreq->lock);
2759 list_del(&lwork->pending_item);
2760 mutex_unlock(&lreq->lock);
2766 static void lwork_queue(struct linger_work *lwork)
2768 struct ceph_osd_linger_request *lreq = lwork->lreq;
2769 struct ceph_osd_client *osdc = lreq->osdc;
2771 verify_lreq_locked(lreq);
2772 WARN_ON(!list_empty(&lwork->pending_item));
2774 lwork->queued_stamp = jiffies;
2775 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2776 queue_work(osdc->notify_wq, &lwork->work);
2779 static void do_watch_notify(struct work_struct *w)
2781 struct linger_work *lwork = container_of(w, struct linger_work, work);
2782 struct ceph_osd_linger_request *lreq = lwork->lreq;
2784 if (!linger_registered(lreq)) {
2785 dout("%s lreq %p not registered\n", __func__, lreq);
2789 WARN_ON(!lreq->is_watch);
2790 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2791 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2792 lwork->notify.payload_len);
2793 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2794 lwork->notify.notifier_id, lwork->notify.payload,
2795 lwork->notify.payload_len);
2798 ceph_msg_put(lwork->notify.msg);
2802 static void do_watch_error(struct work_struct *w)
2804 struct linger_work *lwork = container_of(w, struct linger_work, work);
2805 struct ceph_osd_linger_request *lreq = lwork->lreq;
2807 if (!linger_registered(lreq)) {
2808 dout("%s lreq %p not registered\n", __func__, lreq);
2812 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2813 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2819 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2821 struct linger_work *lwork;
2823 lwork = lwork_alloc(lreq, do_watch_error);
2825 pr_err("failed to allocate error-lwork\n");
2829 lwork->error.err = lreq->last_error;
2833 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2836 if (!completion_done(&lreq->reg_commit_wait)) {
2837 lreq->reg_commit_error = (result <= 0 ? result : 0);
2838 complete_all(&lreq->reg_commit_wait);
2842 static void linger_commit_cb(struct ceph_osd_request *req)
2844 struct ceph_osd_linger_request *lreq = req->r_priv;
2846 mutex_lock(&lreq->lock);
2847 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2848 lreq->linger_id, req->r_result);
2849 linger_reg_commit_complete(lreq, req->r_result);
2850 lreq->committed = true;
2852 if (!lreq->is_watch) {
2853 struct ceph_osd_data *osd_data =
2854 osd_req_op_data(req, 0, notify, response_data);
2855 void *p = page_address(osd_data->pages[0]);
2857 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2858 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2860 /* make note of the notify_id */
2861 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2862 lreq->notify_id = ceph_decode_64(&p);
2863 dout("lreq %p notify_id %llu\n", lreq,
2866 dout("lreq %p no notify_id\n", lreq);
2870 mutex_unlock(&lreq->lock);
2874 static int normalize_watch_error(int err)
2877 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2878 * notification and a failure to reconnect because we raced with
2879 * the delete appear the same to the user.
2887 static void linger_reconnect_cb(struct ceph_osd_request *req)
2889 struct ceph_osd_linger_request *lreq = req->r_priv;
2891 mutex_lock(&lreq->lock);
2892 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2893 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2894 if (req->r_result < 0) {
2895 if (!lreq->last_error) {
2896 lreq->last_error = normalize_watch_error(req->r_result);
2897 queue_watch_error(lreq);
2901 mutex_unlock(&lreq->lock);
2905 static void send_linger(struct ceph_osd_linger_request *lreq)
2907 struct ceph_osd_request *req = lreq->reg_req;
2908 struct ceph_osd_req_op *op = &req->r_ops[0];
2910 verify_osdc_wrlocked(req->r_osdc);
2911 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2914 cancel_linger_request(req);
2916 request_reinit(req);
2917 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2918 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2919 req->r_flags = lreq->t.flags;
2920 req->r_mtime = lreq->mtime;
2922 mutex_lock(&lreq->lock);
2923 if (lreq->is_watch && lreq->committed) {
2924 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2925 op->watch.cookie != lreq->linger_id);
2926 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2927 op->watch.gen = ++lreq->register_gen;
2928 dout("lreq %p reconnect register_gen %u\n", lreq,
2930 req->r_callback = linger_reconnect_cb;
2932 if (!lreq->is_watch)
2933 lreq->notify_id = 0;
2935 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2936 dout("lreq %p register\n", lreq);
2937 req->r_callback = linger_commit_cb;
2939 mutex_unlock(&lreq->lock);
2941 req->r_priv = linger_get(lreq);
2942 req->r_linger = true;
2944 submit_request(req, true);
2947 static void linger_ping_cb(struct ceph_osd_request *req)
2949 struct ceph_osd_linger_request *lreq = req->r_priv;
2951 mutex_lock(&lreq->lock);
2952 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2953 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2955 if (lreq->register_gen == req->r_ops[0].watch.gen) {
2956 if (!req->r_result) {
2957 lreq->watch_valid_thru = lreq->ping_sent;
2958 } else if (!lreq->last_error) {
2959 lreq->last_error = normalize_watch_error(req->r_result);
2960 queue_watch_error(lreq);
2963 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2964 lreq->register_gen, req->r_ops[0].watch.gen);
2967 mutex_unlock(&lreq->lock);
2971 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2973 struct ceph_osd_client *osdc = lreq->osdc;
2974 struct ceph_osd_request *req = lreq->ping_req;
2975 struct ceph_osd_req_op *op = &req->r_ops[0];
2977 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2978 dout("%s PAUSERD\n", __func__);
2982 lreq->ping_sent = jiffies;
2983 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2984 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2985 lreq->register_gen);
2988 cancel_linger_request(req);
2990 request_reinit(req);
2991 target_copy(&req->r_t, &lreq->t);
2993 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2994 op->watch.cookie != lreq->linger_id ||
2995 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2996 op->watch.gen = lreq->register_gen;
2997 req->r_callback = linger_ping_cb;
2998 req->r_priv = linger_get(lreq);
2999 req->r_linger = true;
3001 ceph_osdc_get_request(req);
3002 account_request(req);
3003 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3004 link_request(lreq->osd, req);
3008 static void linger_submit(struct ceph_osd_linger_request *lreq)
3010 struct ceph_osd_client *osdc = lreq->osdc;
3011 struct ceph_osd *osd;
3013 calc_target(osdc, &lreq->t, NULL, false);
3014 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3015 link_linger(osd, lreq);
3020 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3022 struct ceph_osd_client *osdc = lreq->osdc;
3023 struct ceph_osd_linger_request *lookup_lreq;
3025 verify_osdc_wrlocked(osdc);
3027 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3032 WARN_ON(lookup_lreq != lreq);
3033 erase_linger_mc(&osdc->linger_map_checks, lreq);
3038 * @lreq has to be both registered and linked.
3040 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3042 if (lreq->is_watch && lreq->ping_req->r_osd)
3043 cancel_linger_request(lreq->ping_req);
3044 if (lreq->reg_req->r_osd)
3045 cancel_linger_request(lreq->reg_req);
3046 cancel_linger_map_check(lreq);
3047 unlink_linger(lreq->osd, lreq);
3048 linger_unregister(lreq);
3051 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3053 struct ceph_osd_client *osdc = lreq->osdc;
3055 down_write(&osdc->lock);
3056 if (__linger_registered(lreq))
3057 __linger_cancel(lreq);
3058 up_write(&osdc->lock);
3061 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3063 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3065 struct ceph_osd_client *osdc = lreq->osdc;
3066 struct ceph_osdmap *map = osdc->osdmap;
3068 verify_osdc_wrlocked(osdc);
3069 WARN_ON(!map->epoch);
3071 if (lreq->register_gen) {
3072 lreq->map_dne_bound = map->epoch;
3073 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3074 lreq, lreq->linger_id);
3076 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3077 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3081 if (lreq->map_dne_bound) {
3082 if (map->epoch >= lreq->map_dne_bound) {
3083 /* we had a new enough map */
3084 pr_info("linger_id %llu pool does not exist\n",
3086 linger_reg_commit_complete(lreq, -ENOENT);
3087 __linger_cancel(lreq);
3090 send_linger_map_check(lreq);
3094 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3096 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3097 struct ceph_osd_linger_request *lreq;
3098 u64 linger_id = greq->private_data;
3100 WARN_ON(greq->result || !greq->u.newest);
3102 down_write(&osdc->lock);
3103 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3105 dout("%s linger_id %llu dne\n", __func__, linger_id);
3109 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3110 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3112 if (!lreq->map_dne_bound)
3113 lreq->map_dne_bound = greq->u.newest;
3114 erase_linger_mc(&osdc->linger_map_checks, lreq);
3115 check_linger_pool_dne(lreq);
3119 up_write(&osdc->lock);
3122 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3124 struct ceph_osd_client *osdc = lreq->osdc;
3125 struct ceph_osd_linger_request *lookup_lreq;
3128 verify_osdc_wrlocked(osdc);
3130 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3133 WARN_ON(lookup_lreq != lreq);
3138 insert_linger_mc(&osdc->linger_map_checks, lreq);
3139 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3140 linger_map_check_cb, lreq->linger_id);
3144 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3148 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3149 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3150 return ret ?: lreq->reg_commit_error;
3153 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3157 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3158 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3159 return ret ?: lreq->notify_finish_error;
3163 * Timeout callback, called every N seconds. When 1 or more OSD
3164 * requests has been active for more than N seconds, we send a keepalive
3165 * (tag + timestamp) to its OSD to ensure any communications channel
3166 * reset is detected.
3168 static void handle_timeout(struct work_struct *work)
3170 struct ceph_osd_client *osdc =
3171 container_of(work, struct ceph_osd_client, timeout_work.work);
3172 struct ceph_options *opts = osdc->client->options;
3173 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3174 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3175 LIST_HEAD(slow_osds);
3176 struct rb_node *n, *p;
3178 dout("%s osdc %p\n", __func__, osdc);
3179 down_write(&osdc->lock);
3182 * ping osds that are a bit slow. this ensures that if there
3183 * is a break in the TCP connection we will notice, and reopen
3184 * a connection with that osd (from the fault callback).
3186 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3187 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3190 for (p = rb_first(&osd->o_requests); p; ) {
3191 struct ceph_osd_request *req =
3192 rb_entry(p, struct ceph_osd_request, r_node);
3194 p = rb_next(p); /* abort_request() */
3196 if (time_before(req->r_stamp, cutoff)) {
3197 dout(" req %p tid %llu on osd%d is laggy\n",
3198 req, req->r_tid, osd->o_osd);
3201 if (opts->osd_request_timeout &&
3202 time_before(req->r_start_stamp, expiry_cutoff)) {
3203 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3204 req->r_tid, osd->o_osd);
3205 abort_request(req, -ETIMEDOUT);
3208 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3209 struct ceph_osd_linger_request *lreq =
3210 rb_entry(p, struct ceph_osd_linger_request, node);
3212 dout(" lreq %p linger_id %llu is served by osd%d\n",
3213 lreq, lreq->linger_id, osd->o_osd);
3216 mutex_lock(&lreq->lock);
3217 if (lreq->is_watch && lreq->committed && !lreq->last_error)
3218 send_linger_ping(lreq);
3219 mutex_unlock(&lreq->lock);
3223 list_move_tail(&osd->o_keepalive_item, &slow_osds);
3226 if (opts->osd_request_timeout) {
3227 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3228 struct ceph_osd_request *req =
3229 rb_entry(p, struct ceph_osd_request, r_node);
3231 p = rb_next(p); /* abort_request() */
3233 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3234 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3235 req->r_tid, osdc->homeless_osd.o_osd);
3236 abort_request(req, -ETIMEDOUT);
3241 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3242 maybe_request_map(osdc);
3244 while (!list_empty(&slow_osds)) {
3245 struct ceph_osd *osd = list_first_entry(&slow_osds,
3248 list_del_init(&osd->o_keepalive_item);
3249 ceph_con_keepalive(&osd->o_con);
3252 up_write(&osdc->lock);
3253 schedule_delayed_work(&osdc->timeout_work,
3254 osdc->client->options->osd_keepalive_timeout);
3257 static void handle_osds_timeout(struct work_struct *work)
3259 struct ceph_osd_client *osdc =
3260 container_of(work, struct ceph_osd_client,
3261 osds_timeout_work.work);
3262 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3263 struct ceph_osd *osd, *nosd;
3265 dout("%s osdc %p\n", __func__, osdc);
3266 down_write(&osdc->lock);
3267 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3268 if (time_before(jiffies, osd->lru_ttl))
3271 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3272 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3276 up_write(&osdc->lock);
3277 schedule_delayed_work(&osdc->osds_timeout_work,
3278 round_jiffies_relative(delay));
3281 static int ceph_oloc_decode(void **p, void *end,
3282 struct ceph_object_locator *oloc)
3284 u8 struct_v, struct_cv;
3289 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3290 struct_v = ceph_decode_8(p);
3291 struct_cv = ceph_decode_8(p);
3293 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3294 struct_v, struct_cv);
3297 if (struct_cv > 6) {
3298 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3299 struct_v, struct_cv);
3302 len = ceph_decode_32(p);
3303 ceph_decode_need(p, end, len, e_inval);
3304 struct_end = *p + len;
3306 oloc->pool = ceph_decode_64(p);
3307 *p += 4; /* skip preferred */
3309 len = ceph_decode_32(p);
3311 pr_warn("ceph_object_locator::key is set\n");
3315 if (struct_v >= 5) {
3316 bool changed = false;
3318 len = ceph_decode_32(p);
3320 ceph_decode_need(p, end, len, e_inval);
3321 if (!oloc->pool_ns ||
3322 ceph_compare_string(oloc->pool_ns, *p, len))
3330 /* redirect changes namespace */
3331 pr_warn("ceph_object_locator::nspace is changed\n");
3336 if (struct_v >= 6) {
3337 s64 hash = ceph_decode_64(p);
3339 pr_warn("ceph_object_locator::hash is set\n");
3354 static int ceph_redirect_decode(void **p, void *end,
3355 struct ceph_request_redirect *redir)
3357 u8 struct_v, struct_cv;
3362 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3363 struct_v = ceph_decode_8(p);
3364 struct_cv = ceph_decode_8(p);
3365 if (struct_cv > 1) {
3366 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3367 struct_v, struct_cv);
3370 len = ceph_decode_32(p);
3371 ceph_decode_need(p, end, len, e_inval);
3372 struct_end = *p + len;
3374 ret = ceph_oloc_decode(p, end, &redir->oloc);
3378 len = ceph_decode_32(p);
3380 pr_warn("ceph_request_redirect::object_name is set\n");
3384 len = ceph_decode_32(p);
3385 *p += len; /* skip osd_instructions */
3397 struct MOSDOpReply {
3398 struct ceph_pg pgid;
3403 u32 outdata_len[CEPH_OSD_MAX_OPS];
3404 s32 rval[CEPH_OSD_MAX_OPS];
3406 struct ceph_eversion replay_version;
3408 struct ceph_request_redirect redirect;
3411 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3413 void *p = msg->front.iov_base;
3414 void *const end = p + msg->front.iov_len;
3415 u16 version = le16_to_cpu(msg->hdr.version);
3416 struct ceph_eversion bad_replay_version;
3422 ceph_decode_32_safe(&p, end, len, e_inval);
3423 ceph_decode_need(&p, end, len, e_inval);
3424 p += len; /* skip oid */
3426 ret = ceph_decode_pgid(&p, end, &m->pgid);
3430 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3431 ceph_decode_32_safe(&p, end, m->result, e_inval);
3432 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3433 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3434 p += sizeof(bad_replay_version);
3435 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3437 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3438 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3441 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3443 for (i = 0; i < m->num_ops; i++) {
3444 struct ceph_osd_op *op = p;
3446 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3450 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3451 for (i = 0; i < m->num_ops; i++)
3452 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3455 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3456 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3457 p += sizeof(m->replay_version);
3458 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3460 m->replay_version = bad_replay_version; /* struct */
3461 m->user_version = le64_to_cpu(m->replay_version.version);
3466 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3474 ret = ceph_redirect_decode(&p, end, &m->redirect);
3478 ceph_oloc_init(&m->redirect.oloc);
3488 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3491 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3493 struct ceph_osd_client *osdc = osd->o_osdc;
3494 struct ceph_osd_request *req;
3495 struct MOSDOpReply m;
3496 u64 tid = le64_to_cpu(msg->hdr.tid);
3501 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3503 down_read(&osdc->lock);
3504 if (!osd_registered(osd)) {
3505 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3506 goto out_unlock_osdc;
3508 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3510 mutex_lock(&osd->lock);
3511 req = lookup_request(&osd->o_requests, tid);
3513 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3514 goto out_unlock_session;
3517 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3518 ret = decode_MOSDOpReply(msg, &m);
3519 m.redirect.oloc.pool_ns = NULL;
3521 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3526 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3527 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3528 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3529 le64_to_cpu(m.replay_version.version), m.user_version);
3531 if (m.retry_attempt >= 0) {
3532 if (m.retry_attempt != req->r_attempts - 1) {
3533 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3534 req, req->r_tid, m.retry_attempt,
3535 req->r_attempts - 1);
3536 goto out_unlock_session;
3539 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3542 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3543 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3544 m.redirect.oloc.pool);
3545 unlink_request(osd, req);
3546 mutex_unlock(&osd->lock);
3549 * Not ceph_oloc_copy() - changing pool_ns is not
3552 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3553 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3555 __submit_request(req, false);
3556 goto out_unlock_osdc;
3559 if (m.num_ops != req->r_num_ops) {
3560 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3561 req->r_num_ops, req->r_tid);
3564 for (i = 0; i < req->r_num_ops; i++) {
3565 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3566 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3567 req->r_ops[i].rval = m.rval[i];
3568 req->r_ops[i].outdata_len = m.outdata_len[i];
3569 data_len += m.outdata_len[i];
3571 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3572 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3573 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3576 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3577 req, req->r_tid, m.result, data_len);
3580 * Since we only ever request ONDISK, we should only ever get
3581 * one (type of) reply back.
3583 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3584 req->r_result = m.result ?: data_len;
3585 finish_request(req);
3586 mutex_unlock(&osd->lock);
3587 up_read(&osdc->lock);
3589 __complete_request(req);
3593 complete_request(req, -EIO);
3595 mutex_unlock(&osd->lock);
3597 up_read(&osdc->lock);
3600 static void set_pool_was_full(struct ceph_osd_client *osdc)
3604 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3605 struct ceph_pg_pool_info *pi =
3606 rb_entry(n, struct ceph_pg_pool_info, node);
3608 pi->was_full = __pool_full(pi);
3612 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3614 struct ceph_pg_pool_info *pi;
3616 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3620 return pi->was_full && !__pool_full(pi);
3623 static enum calc_target_result
3624 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3626 struct ceph_osd_client *osdc = lreq->osdc;
3627 enum calc_target_result ct_res;
3629 ct_res = calc_target(osdc, &lreq->t, NULL, true);
3630 if (ct_res == CALC_TARGET_NEED_RESEND) {
3631 struct ceph_osd *osd;
3633 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3634 if (osd != lreq->osd) {
3635 unlink_linger(lreq->osd, lreq);
3636 link_linger(osd, lreq);
3644 * Requeue requests whose mapping to an OSD has changed.
3646 static void scan_requests(struct ceph_osd *osd,
3649 bool check_pool_cleared_full,
3650 struct rb_root *need_resend,
3651 struct list_head *need_resend_linger)
3653 struct ceph_osd_client *osdc = osd->o_osdc;
3655 bool force_resend_writes;
3657 for (n = rb_first(&osd->o_linger_requests); n; ) {
3658 struct ceph_osd_linger_request *lreq =
3659 rb_entry(n, struct ceph_osd_linger_request, node);
3660 enum calc_target_result ct_res;
3662 n = rb_next(n); /* recalc_linger_target() */
3664 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3666 ct_res = recalc_linger_target(lreq);
3668 case CALC_TARGET_NO_ACTION:
3669 force_resend_writes = cleared_full ||
3670 (check_pool_cleared_full &&
3671 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3672 if (!force_resend && !force_resend_writes)
3676 case CALC_TARGET_NEED_RESEND:
3677 cancel_linger_map_check(lreq);
3679 * scan_requests() for the previous epoch(s)
3680 * may have already added it to the list, since
3681 * it's not unlinked here.
3683 if (list_empty(&lreq->scan_item))
3684 list_add_tail(&lreq->scan_item, need_resend_linger);
3686 case CALC_TARGET_POOL_DNE:
3687 list_del_init(&lreq->scan_item);
3688 check_linger_pool_dne(lreq);
3693 for (n = rb_first(&osd->o_requests); n; ) {
3694 struct ceph_osd_request *req =
3695 rb_entry(n, struct ceph_osd_request, r_node);
3696 enum calc_target_result ct_res;
3698 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3700 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3701 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3704 case CALC_TARGET_NO_ACTION:
3705 force_resend_writes = cleared_full ||
3706 (check_pool_cleared_full &&
3707 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3708 if (!force_resend &&
3709 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3710 !force_resend_writes))
3714 case CALC_TARGET_NEED_RESEND:
3715 cancel_map_check(req);
3716 unlink_request(osd, req);
3717 insert_request(need_resend, req);
3719 case CALC_TARGET_POOL_DNE:
3720 check_pool_dne(req);
3726 static int handle_one_map(struct ceph_osd_client *osdc,
3727 void *p, void *end, bool incremental,
3728 struct rb_root *need_resend,
3729 struct list_head *need_resend_linger)
3731 struct ceph_osdmap *newmap;
3733 bool skipped_map = false;
3736 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3737 set_pool_was_full(osdc);
3740 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3742 newmap = ceph_osdmap_decode(&p, end);
3744 return PTR_ERR(newmap);
3746 if (newmap != osdc->osdmap) {
3748 * Preserve ->was_full before destroying the old map.
3749 * For pools that weren't in the old map, ->was_full
3752 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3753 struct ceph_pg_pool_info *pi =
3754 rb_entry(n, struct ceph_pg_pool_info, node);
3755 struct ceph_pg_pool_info *old_pi;
3757 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3759 pi->was_full = old_pi->was_full;
3761 WARN_ON(pi->was_full);
3764 if (osdc->osdmap->epoch &&
3765 osdc->osdmap->epoch + 1 < newmap->epoch) {
3766 WARN_ON(incremental);
3770 ceph_osdmap_destroy(osdc->osdmap);
3771 osdc->osdmap = newmap;
3774 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3775 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3776 need_resend, need_resend_linger);
3778 for (n = rb_first(&osdc->osds); n; ) {
3779 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3781 n = rb_next(n); /* close_osd() */
3783 scan_requests(osd, skipped_map, was_full, true, need_resend,
3784 need_resend_linger);
3785 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3786 memcmp(&osd->o_con.peer_addr,
3787 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3788 sizeof(struct ceph_entity_addr)))
3795 static void kick_requests(struct ceph_osd_client *osdc,
3796 struct rb_root *need_resend,
3797 struct list_head *need_resend_linger)
3799 struct ceph_osd_linger_request *lreq, *nlreq;
3800 enum calc_target_result ct_res;
3803 /* make sure need_resend targets reflect latest map */
3804 for (n = rb_first(need_resend); n; ) {
3805 struct ceph_osd_request *req =
3806 rb_entry(n, struct ceph_osd_request, r_node);
3810 if (req->r_t.epoch < osdc->osdmap->epoch) {
3811 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3812 if (ct_res == CALC_TARGET_POOL_DNE) {
3813 erase_request(need_resend, req);
3814 check_pool_dne(req);
3819 for (n = rb_first(need_resend); n; ) {
3820 struct ceph_osd_request *req =
3821 rb_entry(n, struct ceph_osd_request, r_node);
3822 struct ceph_osd *osd;
3825 erase_request(need_resend, req); /* before link_request() */
3827 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3828 link_request(osd, req);
3829 if (!req->r_linger) {
3830 if (!osd_homeless(osd) && !req->r_t.paused)
3833 cancel_linger_request(req);
3837 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3838 if (!osd_homeless(lreq->osd))
3841 list_del_init(&lreq->scan_item);
3846 * Process updated osd map.
3848 * The message contains any number of incremental and full maps, normally
3849 * indicating some sort of topology change in the cluster. Kick requests
3850 * off to different OSDs as needed.
3852 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3854 void *p = msg->front.iov_base;
3855 void *const end = p + msg->front.iov_len;
3856 u32 nr_maps, maplen;
3858 struct ceph_fsid fsid;
3859 struct rb_root need_resend = RB_ROOT;
3860 LIST_HEAD(need_resend_linger);
3861 bool handled_incremental = false;
3862 bool was_pauserd, was_pausewr;
3863 bool pauserd, pausewr;
3866 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3867 down_write(&osdc->lock);
3870 ceph_decode_need(&p, end, sizeof(fsid), bad);
3871 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3872 if (ceph_check_fsid(osdc->client, &fsid) < 0)
3875 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3876 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3877 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3878 have_pool_full(osdc);
3880 /* incremental maps */
3881 ceph_decode_32_safe(&p, end, nr_maps, bad);
3882 dout(" %d inc maps\n", nr_maps);
3883 while (nr_maps > 0) {
3884 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3885 epoch = ceph_decode_32(&p);
3886 maplen = ceph_decode_32(&p);
3887 ceph_decode_need(&p, end, maplen, bad);
3888 if (osdc->osdmap->epoch &&
3889 osdc->osdmap->epoch + 1 == epoch) {
3890 dout("applying incremental map %u len %d\n",
3892 err = handle_one_map(osdc, p, p + maplen, true,
3893 &need_resend, &need_resend_linger);
3896 handled_incremental = true;
3898 dout("ignoring incremental map %u len %d\n",
3904 if (handled_incremental)
3908 ceph_decode_32_safe(&p, end, nr_maps, bad);
3909 dout(" %d full maps\n", nr_maps);
3911 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3912 epoch = ceph_decode_32(&p);
3913 maplen = ceph_decode_32(&p);
3914 ceph_decode_need(&p, end, maplen, bad);
3916 dout("skipping non-latest full map %u len %d\n",
3918 } else if (osdc->osdmap->epoch >= epoch) {
3919 dout("skipping full map %u len %d, "
3920 "older than our %u\n", epoch, maplen,
3921 osdc->osdmap->epoch);
3923 dout("taking full map %u len %d\n", epoch, maplen);
3924 err = handle_one_map(osdc, p, p + maplen, false,
3925 &need_resend, &need_resend_linger);
3935 * subscribe to subsequent osdmap updates if full to ensure
3936 * we find out when we are no longer full and stop returning
3939 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3940 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3941 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3942 have_pool_full(osdc);
3943 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3944 osdc->osdmap->epoch < osdc->epoch_barrier)
3945 maybe_request_map(osdc);
3947 kick_requests(osdc, &need_resend, &need_resend_linger);
3949 ceph_osdc_abort_on_full(osdc);
3950 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3951 osdc->osdmap->epoch);
3952 up_write(&osdc->lock);
3953 wake_up_all(&osdc->client->auth_wq);
3957 pr_err("osdc handle_map corrupt msg\n");
3959 up_write(&osdc->lock);
3963 * Resubmit requests pending on the given osd.
3965 static void kick_osd_requests(struct ceph_osd *osd)
3969 clear_backoffs(osd);
3971 for (n = rb_first(&osd->o_requests); n; ) {
3972 struct ceph_osd_request *req =
3973 rb_entry(n, struct ceph_osd_request, r_node);
3975 n = rb_next(n); /* cancel_linger_request() */
3977 if (!req->r_linger) {
3978 if (!req->r_t.paused)
3981 cancel_linger_request(req);
3984 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3985 struct ceph_osd_linger_request *lreq =
3986 rb_entry(n, struct ceph_osd_linger_request, node);
3993 * If the osd connection drops, we need to resubmit all requests.
3995 static void osd_fault(struct ceph_connection *con)
3997 struct ceph_osd *osd = con->private;
3998 struct ceph_osd_client *osdc = osd->o_osdc;
4000 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4002 down_write(&osdc->lock);
4003 if (!osd_registered(osd)) {
4004 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4008 if (!reopen_osd(osd))
4009 kick_osd_requests(osd);
4010 maybe_request_map(osdc);
4013 up_write(&osdc->lock);
4016 struct MOSDBackoff {
4017 struct ceph_spg spgid;
4021 struct ceph_hobject_id *begin;
4022 struct ceph_hobject_id *end;
4025 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4027 void *p = msg->front.iov_base;
4028 void *const end = p + msg->front.iov_len;
4033 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4037 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4041 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4042 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4043 ceph_decode_8_safe(&p, end, m->op, e_inval);
4044 ceph_decode_64_safe(&p, end, m->id, e_inval);
4046 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4050 ret = decode_hoid(&p, end, m->begin);
4052 free_hoid(m->begin);
4056 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4058 free_hoid(m->begin);
4062 ret = decode_hoid(&p, end, m->end);
4064 free_hoid(m->begin);
4075 static struct ceph_msg *create_backoff_message(
4076 const struct ceph_osd_backoff *backoff,
4079 struct ceph_msg *msg;
4083 msg_size = CEPH_ENCODING_START_BLK_LEN +
4084 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4085 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4086 msg_size += CEPH_ENCODING_START_BLK_LEN +
4087 hoid_encoding_size(backoff->begin);
4088 msg_size += CEPH_ENCODING_START_BLK_LEN +
4089 hoid_encoding_size(backoff->end);
4091 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4095 p = msg->front.iov_base;
4096 end = p + msg->front_alloc_len;
4098 encode_spgid(&p, &backoff->spgid);
4099 ceph_encode_32(&p, map_epoch);
4100 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4101 ceph_encode_64(&p, backoff->id);
4102 encode_hoid(&p, end, backoff->begin);
4103 encode_hoid(&p, end, backoff->end);
4106 msg->front.iov_len = p - msg->front.iov_base;
4107 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4108 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4113 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4115 struct ceph_spg_mapping *spg;
4116 struct ceph_osd_backoff *backoff;
4117 struct ceph_msg *msg;
4119 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4120 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4122 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4124 spg = alloc_spg_mapping();
4126 pr_err("%s failed to allocate spg\n", __func__);
4129 spg->spgid = m->spgid; /* struct */
4130 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4133 backoff = alloc_backoff();
4135 pr_err("%s failed to allocate backoff\n", __func__);
4138 backoff->spgid = m->spgid; /* struct */
4139 backoff->id = m->id;
4140 backoff->begin = m->begin;
4141 m->begin = NULL; /* backoff now owns this */
4142 backoff->end = m->end;
4143 m->end = NULL; /* ditto */
4145 insert_backoff(&spg->backoffs, backoff);
4146 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4149 * Ack with original backoff's epoch so that the OSD can
4150 * discard this if there was a PG split.
4152 msg = create_backoff_message(backoff, m->map_epoch);
4154 pr_err("%s failed to allocate msg\n", __func__);
4157 ceph_con_send(&osd->o_con, msg);
4160 static bool target_contained_by(const struct ceph_osd_request_target *t,
4161 const struct ceph_hobject_id *begin,
4162 const struct ceph_hobject_id *end)
4164 struct ceph_hobject_id hoid;
4167 hoid_fill_from_target(&hoid, t);
4168 cmp = hoid_compare(&hoid, begin);
4169 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4172 static void handle_backoff_unblock(struct ceph_osd *osd,
4173 const struct MOSDBackoff *m)
4175 struct ceph_spg_mapping *spg;
4176 struct ceph_osd_backoff *backoff;
4179 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4180 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4182 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4184 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4185 __func__, osd->o_osd, m->spgid.pgid.pool,
4186 m->spgid.pgid.seed, m->spgid.shard, m->id);
4190 if (hoid_compare(backoff->begin, m->begin) &&
4191 hoid_compare(backoff->end, m->end)) {
4192 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4193 __func__, osd->o_osd, m->spgid.pgid.pool,
4194 m->spgid.pgid.seed, m->spgid.shard, m->id);
4195 /* unblock it anyway... */
4198 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4201 erase_backoff(&spg->backoffs, backoff);
4202 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4203 free_backoff(backoff);
4205 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4206 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4207 free_spg_mapping(spg);
4210 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4211 struct ceph_osd_request *req =
4212 rb_entry(n, struct ceph_osd_request, r_node);
4214 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4216 * Match against @m, not @backoff -- the PG may
4217 * have split on the OSD.
4219 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4221 * If no other installed backoff applies,
4230 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4232 struct ceph_osd_client *osdc = osd->o_osdc;
4233 struct MOSDBackoff m;
4236 down_read(&osdc->lock);
4237 if (!osd_registered(osd)) {
4238 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4239 up_read(&osdc->lock);
4242 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4244 mutex_lock(&osd->lock);
4245 ret = decode_MOSDBackoff(msg, &m);
4247 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4253 case CEPH_OSD_BACKOFF_OP_BLOCK:
4254 handle_backoff_block(osd, &m);
4256 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4257 handle_backoff_unblock(osd, &m);
4260 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4267 mutex_unlock(&osd->lock);
4268 up_read(&osdc->lock);
4272 * Process osd watch notifications
4274 static void handle_watch_notify(struct ceph_osd_client *osdc,
4275 struct ceph_msg *msg)
4277 void *p = msg->front.iov_base;
4278 void *const end = p + msg->front.iov_len;
4279 struct ceph_osd_linger_request *lreq;
4280 struct linger_work *lwork;
4281 u8 proto_ver, opcode;
4282 u64 cookie, notify_id;
4283 u64 notifier_id = 0;
4284 s32 return_code = 0;
4285 void *payload = NULL;
4286 u32 payload_len = 0;
4288 ceph_decode_8_safe(&p, end, proto_ver, bad);
4289 ceph_decode_8_safe(&p, end, opcode, bad);
4290 ceph_decode_64_safe(&p, end, cookie, bad);
4291 p += 8; /* skip ver */
4292 ceph_decode_64_safe(&p, end, notify_id, bad);
4294 if (proto_ver >= 1) {
4295 ceph_decode_32_safe(&p, end, payload_len, bad);
4296 ceph_decode_need(&p, end, payload_len, bad);
4301 if (le16_to_cpu(msg->hdr.version) >= 2)
4302 ceph_decode_32_safe(&p, end, return_code, bad);
4304 if (le16_to_cpu(msg->hdr.version) >= 3)
4305 ceph_decode_64_safe(&p, end, notifier_id, bad);
4307 down_read(&osdc->lock);
4308 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4310 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4312 goto out_unlock_osdc;
4315 mutex_lock(&lreq->lock);
4316 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4317 opcode, cookie, lreq, lreq->is_watch);
4318 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4319 if (!lreq->last_error) {
4320 lreq->last_error = -ENOTCONN;
4321 queue_watch_error(lreq);
4323 } else if (!lreq->is_watch) {
4324 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4325 if (lreq->notify_id && lreq->notify_id != notify_id) {
4326 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4327 lreq->notify_id, notify_id);
4328 } else if (!completion_done(&lreq->notify_finish_wait)) {
4329 struct ceph_msg_data *data =
4330 list_first_entry_or_null(&msg->data,
4331 struct ceph_msg_data,
4335 if (lreq->preply_pages) {
4336 WARN_ON(data->type !=
4337 CEPH_MSG_DATA_PAGES);
4338 *lreq->preply_pages = data->pages;
4339 *lreq->preply_len = data->length;
4341 ceph_release_page_vector(data->pages,
4342 calc_pages_for(0, data->length));
4345 lreq->notify_finish_error = return_code;
4346 complete_all(&lreq->notify_finish_wait);
4349 /* CEPH_WATCH_EVENT_NOTIFY */
4350 lwork = lwork_alloc(lreq, do_watch_notify);
4352 pr_err("failed to allocate notify-lwork\n");
4353 goto out_unlock_lreq;
4356 lwork->notify.notify_id = notify_id;
4357 lwork->notify.notifier_id = notifier_id;
4358 lwork->notify.payload = payload;
4359 lwork->notify.payload_len = payload_len;
4360 lwork->notify.msg = ceph_msg_get(msg);
4365 mutex_unlock(&lreq->lock);
4367 up_read(&osdc->lock);
4371 pr_err("osdc handle_watch_notify corrupt msg\n");
4375 * Register request, send initial attempt.
4377 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4378 struct ceph_osd_request *req,
4381 down_read(&osdc->lock);
4382 submit_request(req, false);
4383 up_read(&osdc->lock);
4387 EXPORT_SYMBOL(ceph_osdc_start_request);
4390 * Unregister a registered request. The request is not completed:
4391 * ->r_result isn't set and __complete_request() isn't called.
4393 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4395 struct ceph_osd_client *osdc = req->r_osdc;
4397 down_write(&osdc->lock);
4399 cancel_request(req);
4400 up_write(&osdc->lock);
4402 EXPORT_SYMBOL(ceph_osdc_cancel_request);
4405 * @timeout: in jiffies, 0 means "wait forever"
4407 static int wait_request_timeout(struct ceph_osd_request *req,
4408 unsigned long timeout)
4412 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
4413 left = wait_for_completion_killable_timeout(&req->r_completion,
4414 ceph_timeout_jiffies(timeout));
4416 left = left ?: -ETIMEDOUT;
4417 ceph_osdc_cancel_request(req);
4419 left = req->r_result; /* completed */
4426 * wait for a request to complete
4428 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4429 struct ceph_osd_request *req)
4431 return wait_request_timeout(req, 0);
4433 EXPORT_SYMBOL(ceph_osdc_wait_request);
4436 * sync - wait for all in-flight requests to flush. avoid starvation.
4438 void ceph_osdc_sync(struct ceph_osd_client *osdc)
4440 struct rb_node *n, *p;
4441 u64 last_tid = atomic64_read(&osdc->last_tid);
4444 down_read(&osdc->lock);
4445 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4446 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4448 mutex_lock(&osd->lock);
4449 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4450 struct ceph_osd_request *req =
4451 rb_entry(p, struct ceph_osd_request, r_node);
4453 if (req->r_tid > last_tid)
4456 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4459 ceph_osdc_get_request(req);
4460 mutex_unlock(&osd->lock);
4461 up_read(&osdc->lock);
4462 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4463 __func__, req, req->r_tid, last_tid);
4464 wait_for_completion(&req->r_completion);
4465 ceph_osdc_put_request(req);
4469 mutex_unlock(&osd->lock);
4472 up_read(&osdc->lock);
4473 dout("%s done last_tid %llu\n", __func__, last_tid);
4475 EXPORT_SYMBOL(ceph_osdc_sync);
4477 static struct ceph_osd_request *
4478 alloc_linger_request(struct ceph_osd_linger_request *lreq)
4480 struct ceph_osd_request *req;
4482 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4486 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4487 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4489 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4490 ceph_osdc_put_request(req);
4498 * Returns a handle, caller owns a ref.
4500 struct ceph_osd_linger_request *
4501 ceph_osdc_watch(struct ceph_osd_client *osdc,
4502 struct ceph_object_id *oid,
4503 struct ceph_object_locator *oloc,
4504 rados_watchcb2_t wcb,
4505 rados_watcherrcb_t errcb,
4508 struct ceph_osd_linger_request *lreq;
4511 lreq = linger_alloc(osdc);
4513 return ERR_PTR(-ENOMEM);
4515 lreq->is_watch = true;
4517 lreq->errcb = errcb;
4519 lreq->watch_valid_thru = jiffies;
4521 ceph_oid_copy(&lreq->t.base_oid, oid);
4522 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4523 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4524 ktime_get_real_ts(&lreq->mtime);
4526 lreq->reg_req = alloc_linger_request(lreq);
4527 if (!lreq->reg_req) {
4532 lreq->ping_req = alloc_linger_request(lreq);
4533 if (!lreq->ping_req) {
4538 down_write(&osdc->lock);
4539 linger_register(lreq); /* before osd_req_op_* */
4540 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4541 CEPH_OSD_WATCH_OP_WATCH);
4542 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4543 CEPH_OSD_WATCH_OP_PING);
4544 linger_submit(lreq);
4545 up_write(&osdc->lock);
4547 ret = linger_reg_commit_wait(lreq);
4549 linger_cancel(lreq);
4557 return ERR_PTR(ret);
4559 EXPORT_SYMBOL(ceph_osdc_watch);
4564 * Times out after mount_timeout to preserve rbd unmap behaviour
4565 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4566 * with mount_timeout").
4568 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4569 struct ceph_osd_linger_request *lreq)
4571 struct ceph_options *opts = osdc->client->options;
4572 struct ceph_osd_request *req;
4575 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4579 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4580 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4581 req->r_flags = CEPH_OSD_FLAG_WRITE;
4582 ktime_get_real_ts(&req->r_mtime);
4583 osd_req_op_watch_init(req, 0, lreq->linger_id,
4584 CEPH_OSD_WATCH_OP_UNWATCH);
4586 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4590 ceph_osdc_start_request(osdc, req, false);
4591 linger_cancel(lreq);
4593 ret = wait_request_timeout(req, opts->mount_timeout);
4596 ceph_osdc_put_request(req);
4599 EXPORT_SYMBOL(ceph_osdc_unwatch);
4601 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4602 u64 notify_id, u64 cookie, void *payload,
4605 struct ceph_osd_req_op *op;
4606 struct ceph_pagelist *pl;
4609 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4611 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4615 ceph_pagelist_init(pl);
4616 ret = ceph_pagelist_encode_64(pl, notify_id);
4617 ret |= ceph_pagelist_encode_64(pl, cookie);
4619 ret |= ceph_pagelist_encode_32(pl, payload_len);
4620 ret |= ceph_pagelist_append(pl, payload, payload_len);
4622 ret |= ceph_pagelist_encode_32(pl, 0);
4625 ceph_pagelist_release(pl);
4629 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4630 op->indata_len = pl->length;
4634 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4635 struct ceph_object_id *oid,
4636 struct ceph_object_locator *oloc,
4642 struct ceph_osd_request *req;
4645 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4649 ceph_oid_copy(&req->r_base_oid, oid);
4650 ceph_oloc_copy(&req->r_base_oloc, oloc);
4651 req->r_flags = CEPH_OSD_FLAG_READ;
4653 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4657 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4662 ceph_osdc_start_request(osdc, req, false);
4663 ret = ceph_osdc_wait_request(osdc, req);
4666 ceph_osdc_put_request(req);
4669 EXPORT_SYMBOL(ceph_osdc_notify_ack);
4671 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4672 u64 cookie, u32 prot_ver, u32 timeout,
4673 void *payload, size_t payload_len)
4675 struct ceph_osd_req_op *op;
4676 struct ceph_pagelist *pl;
4679 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4680 op->notify.cookie = cookie;
4682 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4686 ceph_pagelist_init(pl);
4687 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4688 ret |= ceph_pagelist_encode_32(pl, timeout);
4689 ret |= ceph_pagelist_encode_32(pl, payload_len);
4690 ret |= ceph_pagelist_append(pl, payload, payload_len);
4692 ceph_pagelist_release(pl);
4696 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4697 op->indata_len = pl->length;
4702 * @timeout: in seconds
4704 * @preply_{pages,len} are initialized both on success and error.
4705 * The caller is responsible for:
4707 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4709 int ceph_osdc_notify(struct ceph_osd_client *osdc,
4710 struct ceph_object_id *oid,
4711 struct ceph_object_locator *oloc,
4715 struct page ***preply_pages,
4718 struct ceph_osd_linger_request *lreq;
4719 struct page **pages;
4724 *preply_pages = NULL;
4728 lreq = linger_alloc(osdc);
4732 lreq->preply_pages = preply_pages;
4733 lreq->preply_len = preply_len;
4735 ceph_oid_copy(&lreq->t.base_oid, oid);
4736 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4737 lreq->t.flags = CEPH_OSD_FLAG_READ;
4739 lreq->reg_req = alloc_linger_request(lreq);
4740 if (!lreq->reg_req) {
4746 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4747 if (IS_ERR(pages)) {
4748 ret = PTR_ERR(pages);
4752 down_write(&osdc->lock);
4753 linger_register(lreq); /* before osd_req_op_* */
4754 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4755 timeout, payload, payload_len);
4757 linger_unregister(lreq);
4758 up_write(&osdc->lock);
4759 ceph_release_page_vector(pages, 1);
4762 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4764 pages, PAGE_SIZE, 0, false, true);
4765 linger_submit(lreq);
4766 up_write(&osdc->lock);
4768 ret = linger_reg_commit_wait(lreq);
4770 ret = linger_notify_finish_wait(lreq);
4772 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4774 linger_cancel(lreq);
4779 EXPORT_SYMBOL(ceph_osdc_notify);
4782 * Return the number of milliseconds since the watch was last
4783 * confirmed, or an error. If there is an error, the watch is no
4784 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4786 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4787 struct ceph_osd_linger_request *lreq)
4789 unsigned long stamp, age;
4792 down_read(&osdc->lock);
4793 mutex_lock(&lreq->lock);
4794 stamp = lreq->watch_valid_thru;
4795 if (!list_empty(&lreq->pending_lworks)) {
4796 struct linger_work *lwork =
4797 list_first_entry(&lreq->pending_lworks,
4801 if (time_before(lwork->queued_stamp, stamp))
4802 stamp = lwork->queued_stamp;
4804 age = jiffies - stamp;
4805 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4806 lreq, lreq->linger_id, age, lreq->last_error);
4807 /* we are truncating to msecs, so return a safe upper bound */
4808 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4810 mutex_unlock(&lreq->lock);
4811 up_read(&osdc->lock);
4815 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4821 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4822 &struct_v, &struct_len);
4826 ceph_decode_copy(p, &item->name, sizeof(item->name));
4827 item->cookie = ceph_decode_64(p);
4828 *p += 4; /* skip timeout_seconds */
4829 if (struct_v >= 2) {
4830 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4831 ceph_decode_addr(&item->addr);
4834 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4835 ENTITY_NAME(item->name), item->cookie,
4836 ceph_pr_addr(&item->addr.in_addr));
4840 static int decode_watchers(void **p, void *end,
4841 struct ceph_watch_item **watchers,
4849 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4850 &struct_v, &struct_len);
4854 *num_watchers = ceph_decode_32(p);
4855 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4859 for (i = 0; i < *num_watchers; i++) {
4860 ret = decode_watcher(p, end, *watchers + i);
4871 * On success, the caller is responsible for:
4875 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4876 struct ceph_object_id *oid,
4877 struct ceph_object_locator *oloc,
4878 struct ceph_watch_item **watchers,
4881 struct ceph_osd_request *req;
4882 struct page **pages;
4885 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4889 ceph_oid_copy(&req->r_base_oid, oid);
4890 ceph_oloc_copy(&req->r_base_oloc, oloc);
4891 req->r_flags = CEPH_OSD_FLAG_READ;
4893 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4897 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4898 if (IS_ERR(pages)) {
4899 ret = PTR_ERR(pages);
4903 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4904 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4906 pages, PAGE_SIZE, 0, false, true);
4908 ceph_osdc_start_request(osdc, req, false);
4909 ret = ceph_osdc_wait_request(osdc, req);
4911 void *p = page_address(pages[0]);
4912 void *const end = p + req->r_ops[0].outdata_len;
4914 ret = decode_watchers(&p, end, watchers, num_watchers);
4918 ceph_osdc_put_request(req);
4921 EXPORT_SYMBOL(ceph_osdc_list_watchers);
4924 * Call all pending notify callbacks - for use after a watch is
4925 * unregistered, to make sure no more callbacks for it will be invoked
4927 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4929 dout("%s osdc %p\n", __func__, osdc);
4930 flush_workqueue(osdc->notify_wq);
4932 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4934 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4936 down_read(&osdc->lock);
4937 maybe_request_map(osdc);
4938 up_read(&osdc->lock);
4940 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4943 * Execute an OSD class method on an object.
4945 * @flags: CEPH_OSD_FLAG_*
4946 * @resp_len: in/out param for reply length
4948 int ceph_osdc_call(struct ceph_osd_client *osdc,
4949 struct ceph_object_id *oid,
4950 struct ceph_object_locator *oloc,
4951 const char *class, const char *method,
4953 struct page *req_page, size_t req_len,
4954 struct page *resp_page, size_t *resp_len)
4956 struct ceph_osd_request *req;
4959 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4962 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4966 ceph_oid_copy(&req->r_base_oid, oid);
4967 ceph_oloc_copy(&req->r_base_oloc, oloc);
4968 req->r_flags = flags;
4970 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4974 ret = osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4979 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4982 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
4983 *resp_len, 0, false, false);
4985 ceph_osdc_start_request(osdc, req, false);
4986 ret = ceph_osdc_wait_request(osdc, req);
4988 ret = req->r_ops[0].rval;
4990 *resp_len = req->r_ops[0].outdata_len;
4994 ceph_osdc_put_request(req);
4997 EXPORT_SYMBOL(ceph_osdc_call);
5002 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5007 osdc->client = client;
5008 init_rwsem(&osdc->lock);
5009 osdc->osds = RB_ROOT;
5010 INIT_LIST_HEAD(&osdc->osd_lru);
5011 spin_lock_init(&osdc->osd_lru_lock);
5012 osd_init(&osdc->homeless_osd);
5013 osdc->homeless_osd.o_osdc = osdc;
5014 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5015 osdc->last_linger_id = CEPH_LINGER_ID_START;
5016 osdc->linger_requests = RB_ROOT;
5017 osdc->map_checks = RB_ROOT;
5018 osdc->linger_map_checks = RB_ROOT;
5019 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
5020 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5023 osdc->osdmap = ceph_osdmap_alloc();
5027 osdc->req_mempool = mempool_create_slab_pool(10,
5028 ceph_osd_request_cache);
5029 if (!osdc->req_mempool)
5032 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5033 PAGE_SIZE, 10, true, "osd_op");
5036 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5037 PAGE_SIZE, 10, true, "osd_op_reply");
5042 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5043 if (!osdc->notify_wq)
5044 goto out_msgpool_reply;
5046 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5047 if (!osdc->completion_wq)
5050 schedule_delayed_work(&osdc->timeout_work,
5051 osdc->client->options->osd_keepalive_timeout);
5052 schedule_delayed_work(&osdc->osds_timeout_work,
5053 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5058 destroy_workqueue(osdc->notify_wq);
5060 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5062 ceph_msgpool_destroy(&osdc->msgpool_op);
5064 mempool_destroy(osdc->req_mempool);
5066 ceph_osdmap_destroy(osdc->osdmap);
5071 void ceph_osdc_stop(struct ceph_osd_client *osdc)
5073 destroy_workqueue(osdc->completion_wq);
5074 destroy_workqueue(osdc->notify_wq);
5075 cancel_delayed_work_sync(&osdc->timeout_work);
5076 cancel_delayed_work_sync(&osdc->osds_timeout_work);
5078 down_write(&osdc->lock);
5079 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5080 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5081 struct ceph_osd, o_node);
5084 up_write(&osdc->lock);
5085 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5086 osd_cleanup(&osdc->homeless_osd);
5088 WARN_ON(!list_empty(&osdc->osd_lru));
5089 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
5090 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5091 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5092 WARN_ON(atomic_read(&osdc->num_requests));
5093 WARN_ON(atomic_read(&osdc->num_homeless));
5095 ceph_osdmap_destroy(osdc->osdmap);
5096 mempool_destroy(osdc->req_mempool);
5097 ceph_msgpool_destroy(&osdc->msgpool_op);
5098 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5102 * Read some contiguous pages. If we cross a stripe boundary, shorten
5103 * *plen. Return number of bytes read, or error.
5105 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5106 struct ceph_vino vino, struct ceph_file_layout *layout,
5108 u32 truncate_seq, u64 truncate_size,
5109 struct page **pages, int num_pages, int page_align)
5111 struct ceph_osd_request *req;
5114 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5115 vino.snap, off, *plen);
5116 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
5117 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5118 NULL, truncate_seq, truncate_size,
5121 return PTR_ERR(req);
5123 /* it may be a short read due to an object boundary */
5124 osd_req_op_extent_osd_data_pages(req, 0,
5125 pages, *plen, page_align, false, false);
5127 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
5128 off, *plen, *plen, page_align);
5130 rc = ceph_osdc_start_request(osdc, req, false);
5132 rc = ceph_osdc_wait_request(osdc, req);
5134 ceph_osdc_put_request(req);
5135 dout("readpages result %d\n", rc);
5138 EXPORT_SYMBOL(ceph_osdc_readpages);
5141 * do a synchronous write on N pages
5143 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5144 struct ceph_file_layout *layout,
5145 struct ceph_snap_context *snapc,
5147 u32 truncate_seq, u64 truncate_size,
5148 struct timespec *mtime,
5149 struct page **pages, int num_pages)
5151 struct ceph_osd_request *req;
5153 int page_align = off & ~PAGE_MASK;
5155 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
5156 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5157 snapc, truncate_seq, truncate_size,
5160 return PTR_ERR(req);
5162 /* it may be a short write due to an object boundary */
5163 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
5165 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
5167 req->r_mtime = *mtime;
5168 rc = ceph_osdc_start_request(osdc, req, true);
5170 rc = ceph_osdc_wait_request(osdc, req);
5172 ceph_osdc_put_request(req);
5175 dout("writepages result %d\n", rc);
5178 EXPORT_SYMBOL(ceph_osdc_writepages);
5180 int __init ceph_osdc_setup(void)
5182 size_t size = sizeof(struct ceph_osd_request) +
5183 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5185 BUG_ON(ceph_osd_request_cache);
5186 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5189 return ceph_osd_request_cache ? 0 : -ENOMEM;
5192 void ceph_osdc_cleanup(void)
5194 BUG_ON(!ceph_osd_request_cache);
5195 kmem_cache_destroy(ceph_osd_request_cache);
5196 ceph_osd_request_cache = NULL;
5200 * handle incoming message
5202 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5204 struct ceph_osd *osd = con->private;
5205 struct ceph_osd_client *osdc = osd->o_osdc;
5206 int type = le16_to_cpu(msg->hdr.type);
5209 case CEPH_MSG_OSD_MAP:
5210 ceph_osdc_handle_map(osdc, msg);
5212 case CEPH_MSG_OSD_OPREPLY:
5213 handle_reply(osd, msg);
5215 case CEPH_MSG_OSD_BACKOFF:
5216 handle_backoff(osd, msg);
5218 case CEPH_MSG_WATCH_NOTIFY:
5219 handle_watch_notify(osdc, msg);
5223 pr_err("received unknown message type %d %s\n", type,
5224 ceph_msg_type_name(type));
5231 * Lookup and return message for incoming reply. Don't try to do
5232 * anything about a larger than preallocated data portion of the
5233 * message at the moment - for now, just skip the message.
5235 static struct ceph_msg *get_reply(struct ceph_connection *con,
5236 struct ceph_msg_header *hdr,
5239 struct ceph_osd *osd = con->private;
5240 struct ceph_osd_client *osdc = osd->o_osdc;
5241 struct ceph_msg *m = NULL;
5242 struct ceph_osd_request *req;
5243 int front_len = le32_to_cpu(hdr->front_len);
5244 int data_len = le32_to_cpu(hdr->data_len);
5245 u64 tid = le64_to_cpu(hdr->tid);
5247 down_read(&osdc->lock);
5248 if (!osd_registered(osd)) {
5249 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5251 goto out_unlock_osdc;
5253 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5255 mutex_lock(&osd->lock);
5256 req = lookup_request(&osd->o_requests, tid);
5258 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5261 goto out_unlock_session;
5264 ceph_msg_revoke_incoming(req->r_reply);
5266 if (front_len > req->r_reply->front_alloc_len) {
5267 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5268 __func__, osd->o_osd, req->r_tid, front_len,
5269 req->r_reply->front_alloc_len);
5270 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5273 goto out_unlock_session;
5274 ceph_msg_put(req->r_reply);
5278 if (data_len > req->r_reply->data_length) {
5279 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5280 __func__, osd->o_osd, req->r_tid, data_len,
5281 req->r_reply->data_length);
5284 goto out_unlock_session;
5287 m = ceph_msg_get(req->r_reply);
5288 dout("get_reply tid %lld %p\n", tid, m);
5291 mutex_unlock(&osd->lock);
5293 up_read(&osdc->lock);
5298 * TODO: switch to a msg-owned pagelist
5300 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5303 int type = le16_to_cpu(hdr->type);
5304 u32 front_len = le32_to_cpu(hdr->front_len);
5305 u32 data_len = le32_to_cpu(hdr->data_len);
5307 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
5312 struct page **pages;
5313 struct ceph_osd_data osd_data;
5315 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5317 if (IS_ERR(pages)) {
5322 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5324 ceph_osdc_msg_data_add(m, &osd_data);
5330 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5331 struct ceph_msg_header *hdr,
5334 struct ceph_osd *osd = con->private;
5335 int type = le16_to_cpu(hdr->type);
5339 case CEPH_MSG_OSD_MAP:
5340 case CEPH_MSG_OSD_BACKOFF:
5341 case CEPH_MSG_WATCH_NOTIFY:
5342 return alloc_msg_with_page_vector(hdr);
5343 case CEPH_MSG_OSD_OPREPLY:
5344 return get_reply(con, hdr, skip);
5346 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5354 * Wrappers to refcount containing ceph_osd struct
5356 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5358 struct ceph_osd *osd = con->private;
5364 static void put_osd_con(struct ceph_connection *con)
5366 struct ceph_osd *osd = con->private;
5374 * Note: returned pointer is the address of a structure that's
5375 * managed separately. Caller must *not* attempt to free it.
5377 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
5378 int *proto, int force_new)
5380 struct ceph_osd *o = con->private;
5381 struct ceph_osd_client *osdc = o->o_osdc;
5382 struct ceph_auth_client *ac = osdc->client->monc.auth;
5383 struct ceph_auth_handshake *auth = &o->o_auth;
5385 if (force_new && auth->authorizer) {
5386 ceph_auth_destroy_authorizer(auth->authorizer);
5387 auth->authorizer = NULL;
5389 if (!auth->authorizer) {
5390 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5393 return ERR_PTR(ret);
5395 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5398 return ERR_PTR(ret);
5400 *proto = ac->protocol;
5406 static int verify_authorizer_reply(struct ceph_connection *con)
5408 struct ceph_osd *o = con->private;
5409 struct ceph_osd_client *osdc = o->o_osdc;
5410 struct ceph_auth_client *ac = osdc->client->monc.auth;
5412 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
5415 static int invalidate_authorizer(struct ceph_connection *con)
5417 struct ceph_osd *o = con->private;
5418 struct ceph_osd_client *osdc = o->o_osdc;
5419 struct ceph_auth_client *ac = osdc->client->monc.auth;
5421 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
5422 return ceph_monc_validate_auth(&osdc->client->monc);
5425 static void osd_reencode_message(struct ceph_msg *msg)
5427 int type = le16_to_cpu(msg->hdr.type);
5429 if (type == CEPH_MSG_OSD_OP)
5430 encode_request_finish(msg);
5433 static int osd_sign_message(struct ceph_msg *msg)
5435 struct ceph_osd *o = msg->con->private;
5436 struct ceph_auth_handshake *auth = &o->o_auth;
5438 return ceph_auth_sign_message(auth, msg);
5441 static int osd_check_message_signature(struct ceph_msg *msg)
5443 struct ceph_osd *o = msg->con->private;
5444 struct ceph_auth_handshake *auth = &o->o_auth;
5446 return ceph_auth_check_message_signature(auth, msg);
5449 static const struct ceph_connection_operations osd_con_ops = {
5452 .dispatch = dispatch,
5453 .get_authorizer = get_authorizer,
5454 .verify_authorizer_reply = verify_authorizer_reply,
5455 .invalidate_authorizer = invalidate_authorizer,
5456 .alloc_msg = alloc_msg,
5457 .reencode_message = osd_reencode_message,
5458 .sign_message = osd_sign_message,
5459 .check_message_signature = osd_check_message_signature,