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 (!err && 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 (req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2451 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2452 pool_full(osdc, req->r_t.base_oloc.pool))) {
2454 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2457 abort_request(req, -ENOSPC);
2460 return 0; /* continue iteration */
2464 * Drop all pending requests that are stalled waiting on a full condition to
2465 * clear, and complete them with ENOSPC as the return code. Set the
2466 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2469 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2471 bool victims = false;
2473 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc))
2474 for_each_request(osdc, abort_on_full_fn, &victims);
2477 static void check_pool_dne(struct ceph_osd_request *req)
2479 struct ceph_osd_client *osdc = req->r_osdc;
2480 struct ceph_osdmap *map = osdc->osdmap;
2482 verify_osdc_wrlocked(osdc);
2483 WARN_ON(!map->epoch);
2485 if (req->r_attempts) {
2487 * We sent a request earlier, which means that
2488 * previously the pool existed, and now it does not
2489 * (i.e., it was deleted).
2491 req->r_map_dne_bound = map->epoch;
2492 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2495 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2496 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2499 if (req->r_map_dne_bound) {
2500 if (map->epoch >= req->r_map_dne_bound) {
2501 /* we had a new enough map */
2502 pr_info_ratelimited("tid %llu pool does not exist\n",
2504 complete_request(req, -ENOENT);
2507 send_map_check(req);
2511 static void map_check_cb(struct ceph_mon_generic_request *greq)
2513 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2514 struct ceph_osd_request *req;
2515 u64 tid = greq->private_data;
2517 WARN_ON(greq->result || !greq->u.newest);
2519 down_write(&osdc->lock);
2520 req = lookup_request_mc(&osdc->map_checks, tid);
2522 dout("%s tid %llu dne\n", __func__, tid);
2526 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2527 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2528 if (!req->r_map_dne_bound)
2529 req->r_map_dne_bound = greq->u.newest;
2530 erase_request_mc(&osdc->map_checks, req);
2531 check_pool_dne(req);
2533 ceph_osdc_put_request(req);
2535 up_write(&osdc->lock);
2538 static void send_map_check(struct ceph_osd_request *req)
2540 struct ceph_osd_client *osdc = req->r_osdc;
2541 struct ceph_osd_request *lookup_req;
2544 verify_osdc_wrlocked(osdc);
2546 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2548 WARN_ON(lookup_req != req);
2552 ceph_osdc_get_request(req);
2553 insert_request_mc(&osdc->map_checks, req);
2554 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2555 map_check_cb, req->r_tid);
2560 * lingering requests, watch/notify v2 infrastructure
2562 static void linger_release(struct kref *kref)
2564 struct ceph_osd_linger_request *lreq =
2565 container_of(kref, struct ceph_osd_linger_request, kref);
2567 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2568 lreq->reg_req, lreq->ping_req);
2569 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2570 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2571 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2572 WARN_ON(!list_empty(&lreq->scan_item));
2573 WARN_ON(!list_empty(&lreq->pending_lworks));
2577 ceph_osdc_put_request(lreq->reg_req);
2579 ceph_osdc_put_request(lreq->ping_req);
2580 target_destroy(&lreq->t);
2584 static void linger_put(struct ceph_osd_linger_request *lreq)
2587 kref_put(&lreq->kref, linger_release);
2590 static struct ceph_osd_linger_request *
2591 linger_get(struct ceph_osd_linger_request *lreq)
2593 kref_get(&lreq->kref);
2597 static struct ceph_osd_linger_request *
2598 linger_alloc(struct ceph_osd_client *osdc)
2600 struct ceph_osd_linger_request *lreq;
2602 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2606 kref_init(&lreq->kref);
2607 mutex_init(&lreq->lock);
2608 RB_CLEAR_NODE(&lreq->node);
2609 RB_CLEAR_NODE(&lreq->osdc_node);
2610 RB_CLEAR_NODE(&lreq->mc_node);
2611 INIT_LIST_HEAD(&lreq->scan_item);
2612 INIT_LIST_HEAD(&lreq->pending_lworks);
2613 init_completion(&lreq->reg_commit_wait);
2614 init_completion(&lreq->notify_finish_wait);
2617 target_init(&lreq->t);
2619 dout("%s lreq %p\n", __func__, lreq);
2623 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2624 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2625 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2628 * Create linger request <-> OSD session relation.
2630 * @lreq has to be registered, @osd may be homeless.
2632 static void link_linger(struct ceph_osd *osd,
2633 struct ceph_osd_linger_request *lreq)
2635 verify_osd_locked(osd);
2636 WARN_ON(!lreq->linger_id || lreq->osd);
2637 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2638 osd->o_osd, lreq, lreq->linger_id);
2640 if (!osd_homeless(osd))
2641 __remove_osd_from_lru(osd);
2643 atomic_inc(&osd->o_osdc->num_homeless);
2646 insert_linger(&osd->o_linger_requests, lreq);
2650 static void unlink_linger(struct ceph_osd *osd,
2651 struct ceph_osd_linger_request *lreq)
2653 verify_osd_locked(osd);
2654 WARN_ON(lreq->osd != osd);
2655 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2656 osd->o_osd, lreq, lreq->linger_id);
2659 erase_linger(&osd->o_linger_requests, lreq);
2662 if (!osd_homeless(osd))
2663 maybe_move_osd_to_lru(osd);
2665 atomic_dec(&osd->o_osdc->num_homeless);
2668 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2670 verify_osdc_locked(lreq->osdc);
2672 return !RB_EMPTY_NODE(&lreq->osdc_node);
2675 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2677 struct ceph_osd_client *osdc = lreq->osdc;
2680 down_read(&osdc->lock);
2681 registered = __linger_registered(lreq);
2682 up_read(&osdc->lock);
2687 static void linger_register(struct ceph_osd_linger_request *lreq)
2689 struct ceph_osd_client *osdc = lreq->osdc;
2691 verify_osdc_wrlocked(osdc);
2692 WARN_ON(lreq->linger_id);
2695 lreq->linger_id = ++osdc->last_linger_id;
2696 insert_linger_osdc(&osdc->linger_requests, lreq);
2699 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2701 struct ceph_osd_client *osdc = lreq->osdc;
2703 verify_osdc_wrlocked(osdc);
2705 erase_linger_osdc(&osdc->linger_requests, lreq);
2709 static void cancel_linger_request(struct ceph_osd_request *req)
2711 struct ceph_osd_linger_request *lreq = req->r_priv;
2713 WARN_ON(!req->r_linger);
2714 cancel_request(req);
2718 struct linger_work {
2719 struct work_struct work;
2720 struct ceph_osd_linger_request *lreq;
2721 struct list_head pending_item;
2722 unsigned long queued_stamp;
2728 void *payload; /* points into @msg front */
2731 struct ceph_msg *msg; /* for ceph_msg_put() */
2739 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2742 struct linger_work *lwork;
2744 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2748 INIT_WORK(&lwork->work, workfn);
2749 INIT_LIST_HEAD(&lwork->pending_item);
2750 lwork->lreq = linger_get(lreq);
2755 static void lwork_free(struct linger_work *lwork)
2757 struct ceph_osd_linger_request *lreq = lwork->lreq;
2759 mutex_lock(&lreq->lock);
2760 list_del(&lwork->pending_item);
2761 mutex_unlock(&lreq->lock);
2767 static void lwork_queue(struct linger_work *lwork)
2769 struct ceph_osd_linger_request *lreq = lwork->lreq;
2770 struct ceph_osd_client *osdc = lreq->osdc;
2772 verify_lreq_locked(lreq);
2773 WARN_ON(!list_empty(&lwork->pending_item));
2775 lwork->queued_stamp = jiffies;
2776 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2777 queue_work(osdc->notify_wq, &lwork->work);
2780 static void do_watch_notify(struct work_struct *w)
2782 struct linger_work *lwork = container_of(w, struct linger_work, work);
2783 struct ceph_osd_linger_request *lreq = lwork->lreq;
2785 if (!linger_registered(lreq)) {
2786 dout("%s lreq %p not registered\n", __func__, lreq);
2790 WARN_ON(!lreq->is_watch);
2791 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2792 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2793 lwork->notify.payload_len);
2794 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2795 lwork->notify.notifier_id, lwork->notify.payload,
2796 lwork->notify.payload_len);
2799 ceph_msg_put(lwork->notify.msg);
2803 static void do_watch_error(struct work_struct *w)
2805 struct linger_work *lwork = container_of(w, struct linger_work, work);
2806 struct ceph_osd_linger_request *lreq = lwork->lreq;
2808 if (!linger_registered(lreq)) {
2809 dout("%s lreq %p not registered\n", __func__, lreq);
2813 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2814 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2820 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2822 struct linger_work *lwork;
2824 lwork = lwork_alloc(lreq, do_watch_error);
2826 pr_err("failed to allocate error-lwork\n");
2830 lwork->error.err = lreq->last_error;
2834 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2837 if (!completion_done(&lreq->reg_commit_wait)) {
2838 lreq->reg_commit_error = (result <= 0 ? result : 0);
2839 complete_all(&lreq->reg_commit_wait);
2843 static void linger_commit_cb(struct ceph_osd_request *req)
2845 struct ceph_osd_linger_request *lreq = req->r_priv;
2847 mutex_lock(&lreq->lock);
2848 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2849 lreq->linger_id, req->r_result);
2850 linger_reg_commit_complete(lreq, req->r_result);
2851 lreq->committed = true;
2853 if (!lreq->is_watch) {
2854 struct ceph_osd_data *osd_data =
2855 osd_req_op_data(req, 0, notify, response_data);
2856 void *p = page_address(osd_data->pages[0]);
2858 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2859 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2861 /* make note of the notify_id */
2862 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2863 lreq->notify_id = ceph_decode_64(&p);
2864 dout("lreq %p notify_id %llu\n", lreq,
2867 dout("lreq %p no notify_id\n", lreq);
2871 mutex_unlock(&lreq->lock);
2875 static int normalize_watch_error(int err)
2878 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2879 * notification and a failure to reconnect because we raced with
2880 * the delete appear the same to the user.
2888 static void linger_reconnect_cb(struct ceph_osd_request *req)
2890 struct ceph_osd_linger_request *lreq = req->r_priv;
2892 mutex_lock(&lreq->lock);
2893 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2894 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2895 if (req->r_result < 0) {
2896 if (!lreq->last_error) {
2897 lreq->last_error = normalize_watch_error(req->r_result);
2898 queue_watch_error(lreq);
2902 mutex_unlock(&lreq->lock);
2906 static void send_linger(struct ceph_osd_linger_request *lreq)
2908 struct ceph_osd_request *req = lreq->reg_req;
2909 struct ceph_osd_req_op *op = &req->r_ops[0];
2911 verify_osdc_wrlocked(req->r_osdc);
2912 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2915 cancel_linger_request(req);
2917 request_reinit(req);
2918 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2919 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2920 req->r_flags = lreq->t.flags;
2921 req->r_mtime = lreq->mtime;
2923 mutex_lock(&lreq->lock);
2924 if (lreq->is_watch && lreq->committed) {
2925 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2926 op->watch.cookie != lreq->linger_id);
2927 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2928 op->watch.gen = ++lreq->register_gen;
2929 dout("lreq %p reconnect register_gen %u\n", lreq,
2931 req->r_callback = linger_reconnect_cb;
2933 if (!lreq->is_watch)
2934 lreq->notify_id = 0;
2936 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2937 dout("lreq %p register\n", lreq);
2938 req->r_callback = linger_commit_cb;
2940 mutex_unlock(&lreq->lock);
2942 req->r_priv = linger_get(lreq);
2943 req->r_linger = true;
2945 submit_request(req, true);
2948 static void linger_ping_cb(struct ceph_osd_request *req)
2950 struct ceph_osd_linger_request *lreq = req->r_priv;
2952 mutex_lock(&lreq->lock);
2953 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2954 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2956 if (lreq->register_gen == req->r_ops[0].watch.gen) {
2957 if (!req->r_result) {
2958 lreq->watch_valid_thru = lreq->ping_sent;
2959 } else if (!lreq->last_error) {
2960 lreq->last_error = normalize_watch_error(req->r_result);
2961 queue_watch_error(lreq);
2964 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2965 lreq->register_gen, req->r_ops[0].watch.gen);
2968 mutex_unlock(&lreq->lock);
2972 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2974 struct ceph_osd_client *osdc = lreq->osdc;
2975 struct ceph_osd_request *req = lreq->ping_req;
2976 struct ceph_osd_req_op *op = &req->r_ops[0];
2978 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2979 dout("%s PAUSERD\n", __func__);
2983 lreq->ping_sent = jiffies;
2984 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2985 __func__, lreq, lreq->linger_id, lreq->ping_sent,
2986 lreq->register_gen);
2989 cancel_linger_request(req);
2991 request_reinit(req);
2992 target_copy(&req->r_t, &lreq->t);
2994 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2995 op->watch.cookie != lreq->linger_id ||
2996 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2997 op->watch.gen = lreq->register_gen;
2998 req->r_callback = linger_ping_cb;
2999 req->r_priv = linger_get(lreq);
3000 req->r_linger = true;
3002 ceph_osdc_get_request(req);
3003 account_request(req);
3004 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3005 link_request(lreq->osd, req);
3009 static void linger_submit(struct ceph_osd_linger_request *lreq)
3011 struct ceph_osd_client *osdc = lreq->osdc;
3012 struct ceph_osd *osd;
3014 calc_target(osdc, &lreq->t, NULL, false);
3015 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3016 link_linger(osd, lreq);
3021 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3023 struct ceph_osd_client *osdc = lreq->osdc;
3024 struct ceph_osd_linger_request *lookup_lreq;
3026 verify_osdc_wrlocked(osdc);
3028 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3033 WARN_ON(lookup_lreq != lreq);
3034 erase_linger_mc(&osdc->linger_map_checks, lreq);
3039 * @lreq has to be both registered and linked.
3041 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3043 if (lreq->is_watch && lreq->ping_req->r_osd)
3044 cancel_linger_request(lreq->ping_req);
3045 if (lreq->reg_req->r_osd)
3046 cancel_linger_request(lreq->reg_req);
3047 cancel_linger_map_check(lreq);
3048 unlink_linger(lreq->osd, lreq);
3049 linger_unregister(lreq);
3052 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3054 struct ceph_osd_client *osdc = lreq->osdc;
3056 down_write(&osdc->lock);
3057 if (__linger_registered(lreq))
3058 __linger_cancel(lreq);
3059 up_write(&osdc->lock);
3062 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3064 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3066 struct ceph_osd_client *osdc = lreq->osdc;
3067 struct ceph_osdmap *map = osdc->osdmap;
3069 verify_osdc_wrlocked(osdc);
3070 WARN_ON(!map->epoch);
3072 if (lreq->register_gen) {
3073 lreq->map_dne_bound = map->epoch;
3074 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3075 lreq, lreq->linger_id);
3077 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3078 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3082 if (lreq->map_dne_bound) {
3083 if (map->epoch >= lreq->map_dne_bound) {
3084 /* we had a new enough map */
3085 pr_info("linger_id %llu pool does not exist\n",
3087 linger_reg_commit_complete(lreq, -ENOENT);
3088 __linger_cancel(lreq);
3091 send_linger_map_check(lreq);
3095 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3097 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3098 struct ceph_osd_linger_request *lreq;
3099 u64 linger_id = greq->private_data;
3101 WARN_ON(greq->result || !greq->u.newest);
3103 down_write(&osdc->lock);
3104 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3106 dout("%s linger_id %llu dne\n", __func__, linger_id);
3110 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3111 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3113 if (!lreq->map_dne_bound)
3114 lreq->map_dne_bound = greq->u.newest;
3115 erase_linger_mc(&osdc->linger_map_checks, lreq);
3116 check_linger_pool_dne(lreq);
3120 up_write(&osdc->lock);
3123 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3125 struct ceph_osd_client *osdc = lreq->osdc;
3126 struct ceph_osd_linger_request *lookup_lreq;
3129 verify_osdc_wrlocked(osdc);
3131 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3134 WARN_ON(lookup_lreq != lreq);
3139 insert_linger_mc(&osdc->linger_map_checks, lreq);
3140 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3141 linger_map_check_cb, lreq->linger_id);
3145 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3149 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3150 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3151 return ret ?: lreq->reg_commit_error;
3154 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3158 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3159 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3160 return ret ?: lreq->notify_finish_error;
3164 * Timeout callback, called every N seconds. When 1 or more OSD
3165 * requests has been active for more than N seconds, we send a keepalive
3166 * (tag + timestamp) to its OSD to ensure any communications channel
3167 * reset is detected.
3169 static void handle_timeout(struct work_struct *work)
3171 struct ceph_osd_client *osdc =
3172 container_of(work, struct ceph_osd_client, timeout_work.work);
3173 struct ceph_options *opts = osdc->client->options;
3174 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3175 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3176 LIST_HEAD(slow_osds);
3177 struct rb_node *n, *p;
3179 dout("%s osdc %p\n", __func__, osdc);
3180 down_write(&osdc->lock);
3183 * ping osds that are a bit slow. this ensures that if there
3184 * is a break in the TCP connection we will notice, and reopen
3185 * a connection with that osd (from the fault callback).
3187 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3188 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3191 for (p = rb_first(&osd->o_requests); p; ) {
3192 struct ceph_osd_request *req =
3193 rb_entry(p, struct ceph_osd_request, r_node);
3195 p = rb_next(p); /* abort_request() */
3197 if (time_before(req->r_stamp, cutoff)) {
3198 dout(" req %p tid %llu on osd%d is laggy\n",
3199 req, req->r_tid, osd->o_osd);
3202 if (opts->osd_request_timeout &&
3203 time_before(req->r_start_stamp, expiry_cutoff)) {
3204 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3205 req->r_tid, osd->o_osd);
3206 abort_request(req, -ETIMEDOUT);
3209 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3210 struct ceph_osd_linger_request *lreq =
3211 rb_entry(p, struct ceph_osd_linger_request, node);
3213 dout(" lreq %p linger_id %llu is served by osd%d\n",
3214 lreq, lreq->linger_id, osd->o_osd);
3217 mutex_lock(&lreq->lock);
3218 if (lreq->is_watch && lreq->committed && !lreq->last_error)
3219 send_linger_ping(lreq);
3220 mutex_unlock(&lreq->lock);
3224 list_move_tail(&osd->o_keepalive_item, &slow_osds);
3227 if (opts->osd_request_timeout) {
3228 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3229 struct ceph_osd_request *req =
3230 rb_entry(p, struct ceph_osd_request, r_node);
3232 p = rb_next(p); /* abort_request() */
3234 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3235 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3236 req->r_tid, osdc->homeless_osd.o_osd);
3237 abort_request(req, -ETIMEDOUT);
3242 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3243 maybe_request_map(osdc);
3245 while (!list_empty(&slow_osds)) {
3246 struct ceph_osd *osd = list_first_entry(&slow_osds,
3249 list_del_init(&osd->o_keepalive_item);
3250 ceph_con_keepalive(&osd->o_con);
3253 up_write(&osdc->lock);
3254 schedule_delayed_work(&osdc->timeout_work,
3255 osdc->client->options->osd_keepalive_timeout);
3258 static void handle_osds_timeout(struct work_struct *work)
3260 struct ceph_osd_client *osdc =
3261 container_of(work, struct ceph_osd_client,
3262 osds_timeout_work.work);
3263 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3264 struct ceph_osd *osd, *nosd;
3266 dout("%s osdc %p\n", __func__, osdc);
3267 down_write(&osdc->lock);
3268 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3269 if (time_before(jiffies, osd->lru_ttl))
3272 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3273 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3277 up_write(&osdc->lock);
3278 schedule_delayed_work(&osdc->osds_timeout_work,
3279 round_jiffies_relative(delay));
3282 static int ceph_oloc_decode(void **p, void *end,
3283 struct ceph_object_locator *oloc)
3285 u8 struct_v, struct_cv;
3290 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3291 struct_v = ceph_decode_8(p);
3292 struct_cv = ceph_decode_8(p);
3294 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3295 struct_v, struct_cv);
3298 if (struct_cv > 6) {
3299 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3300 struct_v, struct_cv);
3303 len = ceph_decode_32(p);
3304 ceph_decode_need(p, end, len, e_inval);
3305 struct_end = *p + len;
3307 oloc->pool = ceph_decode_64(p);
3308 *p += 4; /* skip preferred */
3310 len = ceph_decode_32(p);
3312 pr_warn("ceph_object_locator::key is set\n");
3316 if (struct_v >= 5) {
3317 bool changed = false;
3319 len = ceph_decode_32(p);
3321 ceph_decode_need(p, end, len, e_inval);
3322 if (!oloc->pool_ns ||
3323 ceph_compare_string(oloc->pool_ns, *p, len))
3331 /* redirect changes namespace */
3332 pr_warn("ceph_object_locator::nspace is changed\n");
3337 if (struct_v >= 6) {
3338 s64 hash = ceph_decode_64(p);
3340 pr_warn("ceph_object_locator::hash is set\n");
3355 static int ceph_redirect_decode(void **p, void *end,
3356 struct ceph_request_redirect *redir)
3358 u8 struct_v, struct_cv;
3363 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3364 struct_v = ceph_decode_8(p);
3365 struct_cv = ceph_decode_8(p);
3366 if (struct_cv > 1) {
3367 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3368 struct_v, struct_cv);
3371 len = ceph_decode_32(p);
3372 ceph_decode_need(p, end, len, e_inval);
3373 struct_end = *p + len;
3375 ret = ceph_oloc_decode(p, end, &redir->oloc);
3379 len = ceph_decode_32(p);
3381 pr_warn("ceph_request_redirect::object_name is set\n");
3385 len = ceph_decode_32(p);
3386 *p += len; /* skip osd_instructions */
3398 struct MOSDOpReply {
3399 struct ceph_pg pgid;
3404 u32 outdata_len[CEPH_OSD_MAX_OPS];
3405 s32 rval[CEPH_OSD_MAX_OPS];
3407 struct ceph_eversion replay_version;
3409 struct ceph_request_redirect redirect;
3412 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3414 void *p = msg->front.iov_base;
3415 void *const end = p + msg->front.iov_len;
3416 u16 version = le16_to_cpu(msg->hdr.version);
3417 struct ceph_eversion bad_replay_version;
3423 ceph_decode_32_safe(&p, end, len, e_inval);
3424 ceph_decode_need(&p, end, len, e_inval);
3425 p += len; /* skip oid */
3427 ret = ceph_decode_pgid(&p, end, &m->pgid);
3431 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3432 ceph_decode_32_safe(&p, end, m->result, e_inval);
3433 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3434 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3435 p += sizeof(bad_replay_version);
3436 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3438 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3439 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3442 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3444 for (i = 0; i < m->num_ops; i++) {
3445 struct ceph_osd_op *op = p;
3447 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3451 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3452 for (i = 0; i < m->num_ops; i++)
3453 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3456 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3457 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3458 p += sizeof(m->replay_version);
3459 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3461 m->replay_version = bad_replay_version; /* struct */
3462 m->user_version = le64_to_cpu(m->replay_version.version);
3467 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3475 ret = ceph_redirect_decode(&p, end, &m->redirect);
3479 ceph_oloc_init(&m->redirect.oloc);
3489 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3492 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3494 struct ceph_osd_client *osdc = osd->o_osdc;
3495 struct ceph_osd_request *req;
3496 struct MOSDOpReply m;
3497 u64 tid = le64_to_cpu(msg->hdr.tid);
3502 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3504 down_read(&osdc->lock);
3505 if (!osd_registered(osd)) {
3506 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3507 goto out_unlock_osdc;
3509 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3511 mutex_lock(&osd->lock);
3512 req = lookup_request(&osd->o_requests, tid);
3514 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3515 goto out_unlock_session;
3518 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3519 ret = decode_MOSDOpReply(msg, &m);
3520 m.redirect.oloc.pool_ns = NULL;
3522 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3527 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3528 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3529 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3530 le64_to_cpu(m.replay_version.version), m.user_version);
3532 if (m.retry_attempt >= 0) {
3533 if (m.retry_attempt != req->r_attempts - 1) {
3534 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3535 req, req->r_tid, m.retry_attempt,
3536 req->r_attempts - 1);
3537 goto out_unlock_session;
3540 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3543 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3544 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3545 m.redirect.oloc.pool);
3546 unlink_request(osd, req);
3547 mutex_unlock(&osd->lock);
3550 * Not ceph_oloc_copy() - changing pool_ns is not
3553 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3554 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3556 __submit_request(req, false);
3557 goto out_unlock_osdc;
3560 if (m.num_ops != req->r_num_ops) {
3561 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3562 req->r_num_ops, req->r_tid);
3565 for (i = 0; i < req->r_num_ops; i++) {
3566 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3567 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3568 req->r_ops[i].rval = m.rval[i];
3569 req->r_ops[i].outdata_len = m.outdata_len[i];
3570 data_len += m.outdata_len[i];
3572 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3573 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3574 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3577 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3578 req, req->r_tid, m.result, data_len);
3581 * Since we only ever request ONDISK, we should only ever get
3582 * one (type of) reply back.
3584 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3585 req->r_result = m.result ?: data_len;
3586 finish_request(req);
3587 mutex_unlock(&osd->lock);
3588 up_read(&osdc->lock);
3590 __complete_request(req);
3594 complete_request(req, -EIO);
3596 mutex_unlock(&osd->lock);
3598 up_read(&osdc->lock);
3601 static void set_pool_was_full(struct ceph_osd_client *osdc)
3605 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3606 struct ceph_pg_pool_info *pi =
3607 rb_entry(n, struct ceph_pg_pool_info, node);
3609 pi->was_full = __pool_full(pi);
3613 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3615 struct ceph_pg_pool_info *pi;
3617 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3621 return pi->was_full && !__pool_full(pi);
3624 static enum calc_target_result
3625 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3627 struct ceph_osd_client *osdc = lreq->osdc;
3628 enum calc_target_result ct_res;
3630 ct_res = calc_target(osdc, &lreq->t, NULL, true);
3631 if (ct_res == CALC_TARGET_NEED_RESEND) {
3632 struct ceph_osd *osd;
3634 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3635 if (osd != lreq->osd) {
3636 unlink_linger(lreq->osd, lreq);
3637 link_linger(osd, lreq);
3645 * Requeue requests whose mapping to an OSD has changed.
3647 static void scan_requests(struct ceph_osd *osd,
3650 bool check_pool_cleared_full,
3651 struct rb_root *need_resend,
3652 struct list_head *need_resend_linger)
3654 struct ceph_osd_client *osdc = osd->o_osdc;
3656 bool force_resend_writes;
3658 for (n = rb_first(&osd->o_linger_requests); n; ) {
3659 struct ceph_osd_linger_request *lreq =
3660 rb_entry(n, struct ceph_osd_linger_request, node);
3661 enum calc_target_result ct_res;
3663 n = rb_next(n); /* recalc_linger_target() */
3665 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3667 ct_res = recalc_linger_target(lreq);
3669 case CALC_TARGET_NO_ACTION:
3670 force_resend_writes = cleared_full ||
3671 (check_pool_cleared_full &&
3672 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3673 if (!force_resend && !force_resend_writes)
3677 case CALC_TARGET_NEED_RESEND:
3678 cancel_linger_map_check(lreq);
3680 * scan_requests() for the previous epoch(s)
3681 * may have already added it to the list, since
3682 * it's not unlinked here.
3684 if (list_empty(&lreq->scan_item))
3685 list_add_tail(&lreq->scan_item, need_resend_linger);
3687 case CALC_TARGET_POOL_DNE:
3688 list_del_init(&lreq->scan_item);
3689 check_linger_pool_dne(lreq);
3694 for (n = rb_first(&osd->o_requests); n; ) {
3695 struct ceph_osd_request *req =
3696 rb_entry(n, struct ceph_osd_request, r_node);
3697 enum calc_target_result ct_res;
3699 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3701 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3702 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3705 case CALC_TARGET_NO_ACTION:
3706 force_resend_writes = cleared_full ||
3707 (check_pool_cleared_full &&
3708 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3709 if (!force_resend &&
3710 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3711 !force_resend_writes))
3715 case CALC_TARGET_NEED_RESEND:
3716 cancel_map_check(req);
3717 unlink_request(osd, req);
3718 insert_request(need_resend, req);
3720 case CALC_TARGET_POOL_DNE:
3721 check_pool_dne(req);
3727 static int handle_one_map(struct ceph_osd_client *osdc,
3728 void *p, void *end, bool incremental,
3729 struct rb_root *need_resend,
3730 struct list_head *need_resend_linger)
3732 struct ceph_osdmap *newmap;
3734 bool skipped_map = false;
3737 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3738 set_pool_was_full(osdc);
3741 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3743 newmap = ceph_osdmap_decode(&p, end);
3745 return PTR_ERR(newmap);
3747 if (newmap != osdc->osdmap) {
3749 * Preserve ->was_full before destroying the old map.
3750 * For pools that weren't in the old map, ->was_full
3753 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3754 struct ceph_pg_pool_info *pi =
3755 rb_entry(n, struct ceph_pg_pool_info, node);
3756 struct ceph_pg_pool_info *old_pi;
3758 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3760 pi->was_full = old_pi->was_full;
3762 WARN_ON(pi->was_full);
3765 if (osdc->osdmap->epoch &&
3766 osdc->osdmap->epoch + 1 < newmap->epoch) {
3767 WARN_ON(incremental);
3771 ceph_osdmap_destroy(osdc->osdmap);
3772 osdc->osdmap = newmap;
3775 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3776 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3777 need_resend, need_resend_linger);
3779 for (n = rb_first(&osdc->osds); n; ) {
3780 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3782 n = rb_next(n); /* close_osd() */
3784 scan_requests(osd, skipped_map, was_full, true, need_resend,
3785 need_resend_linger);
3786 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3787 memcmp(&osd->o_con.peer_addr,
3788 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3789 sizeof(struct ceph_entity_addr)))
3796 static void kick_requests(struct ceph_osd_client *osdc,
3797 struct rb_root *need_resend,
3798 struct list_head *need_resend_linger)
3800 struct ceph_osd_linger_request *lreq, *nlreq;
3801 enum calc_target_result ct_res;
3804 /* make sure need_resend targets reflect latest map */
3805 for (n = rb_first(need_resend); n; ) {
3806 struct ceph_osd_request *req =
3807 rb_entry(n, struct ceph_osd_request, r_node);
3811 if (req->r_t.epoch < osdc->osdmap->epoch) {
3812 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3813 if (ct_res == CALC_TARGET_POOL_DNE) {
3814 erase_request(need_resend, req);
3815 check_pool_dne(req);
3820 for (n = rb_first(need_resend); n; ) {
3821 struct ceph_osd_request *req =
3822 rb_entry(n, struct ceph_osd_request, r_node);
3823 struct ceph_osd *osd;
3826 erase_request(need_resend, req); /* before link_request() */
3828 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3829 link_request(osd, req);
3830 if (!req->r_linger) {
3831 if (!osd_homeless(osd) && !req->r_t.paused)
3834 cancel_linger_request(req);
3838 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3839 if (!osd_homeless(lreq->osd))
3842 list_del_init(&lreq->scan_item);
3847 * Process updated osd map.
3849 * The message contains any number of incremental and full maps, normally
3850 * indicating some sort of topology change in the cluster. Kick requests
3851 * off to different OSDs as needed.
3853 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3855 void *p = msg->front.iov_base;
3856 void *const end = p + msg->front.iov_len;
3857 u32 nr_maps, maplen;
3859 struct ceph_fsid fsid;
3860 struct rb_root need_resend = RB_ROOT;
3861 LIST_HEAD(need_resend_linger);
3862 bool handled_incremental = false;
3863 bool was_pauserd, was_pausewr;
3864 bool pauserd, pausewr;
3867 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3868 down_write(&osdc->lock);
3871 ceph_decode_need(&p, end, sizeof(fsid), bad);
3872 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3873 if (ceph_check_fsid(osdc->client, &fsid) < 0)
3876 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3877 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3878 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3879 have_pool_full(osdc);
3881 /* incremental maps */
3882 ceph_decode_32_safe(&p, end, nr_maps, bad);
3883 dout(" %d inc maps\n", nr_maps);
3884 while (nr_maps > 0) {
3885 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3886 epoch = ceph_decode_32(&p);
3887 maplen = ceph_decode_32(&p);
3888 ceph_decode_need(&p, end, maplen, bad);
3889 if (osdc->osdmap->epoch &&
3890 osdc->osdmap->epoch + 1 == epoch) {
3891 dout("applying incremental map %u len %d\n",
3893 err = handle_one_map(osdc, p, p + maplen, true,
3894 &need_resend, &need_resend_linger);
3897 handled_incremental = true;
3899 dout("ignoring incremental map %u len %d\n",
3905 if (handled_incremental)
3909 ceph_decode_32_safe(&p, end, nr_maps, bad);
3910 dout(" %d full maps\n", nr_maps);
3912 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3913 epoch = ceph_decode_32(&p);
3914 maplen = ceph_decode_32(&p);
3915 ceph_decode_need(&p, end, maplen, bad);
3917 dout("skipping non-latest full map %u len %d\n",
3919 } else if (osdc->osdmap->epoch >= epoch) {
3920 dout("skipping full map %u len %d, "
3921 "older than our %u\n", epoch, maplen,
3922 osdc->osdmap->epoch);
3924 dout("taking full map %u len %d\n", epoch, maplen);
3925 err = handle_one_map(osdc, p, p + maplen, false,
3926 &need_resend, &need_resend_linger);
3936 * subscribe to subsequent osdmap updates if full to ensure
3937 * we find out when we are no longer full and stop returning
3940 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3941 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3942 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3943 have_pool_full(osdc);
3944 if (was_pauserd || was_pausewr || pauserd || pausewr ||
3945 osdc->osdmap->epoch < osdc->epoch_barrier)
3946 maybe_request_map(osdc);
3948 kick_requests(osdc, &need_resend, &need_resend_linger);
3950 ceph_osdc_abort_on_full(osdc);
3951 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3952 osdc->osdmap->epoch);
3953 up_write(&osdc->lock);
3954 wake_up_all(&osdc->client->auth_wq);
3958 pr_err("osdc handle_map corrupt msg\n");
3960 up_write(&osdc->lock);
3964 * Resubmit requests pending on the given osd.
3966 static void kick_osd_requests(struct ceph_osd *osd)
3970 clear_backoffs(osd);
3972 for (n = rb_first(&osd->o_requests); n; ) {
3973 struct ceph_osd_request *req =
3974 rb_entry(n, struct ceph_osd_request, r_node);
3976 n = rb_next(n); /* cancel_linger_request() */
3978 if (!req->r_linger) {
3979 if (!req->r_t.paused)
3982 cancel_linger_request(req);
3985 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3986 struct ceph_osd_linger_request *lreq =
3987 rb_entry(n, struct ceph_osd_linger_request, node);
3994 * If the osd connection drops, we need to resubmit all requests.
3996 static void osd_fault(struct ceph_connection *con)
3998 struct ceph_osd *osd = con->private;
3999 struct ceph_osd_client *osdc = osd->o_osdc;
4001 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4003 down_write(&osdc->lock);
4004 if (!osd_registered(osd)) {
4005 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4009 if (!reopen_osd(osd))
4010 kick_osd_requests(osd);
4011 maybe_request_map(osdc);
4014 up_write(&osdc->lock);
4017 struct MOSDBackoff {
4018 struct ceph_spg spgid;
4022 struct ceph_hobject_id *begin;
4023 struct ceph_hobject_id *end;
4026 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4028 void *p = msg->front.iov_base;
4029 void *const end = p + msg->front.iov_len;
4034 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4038 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4042 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4043 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4044 ceph_decode_8_safe(&p, end, m->op, e_inval);
4045 ceph_decode_64_safe(&p, end, m->id, e_inval);
4047 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4051 ret = decode_hoid(&p, end, m->begin);
4053 free_hoid(m->begin);
4057 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4059 free_hoid(m->begin);
4063 ret = decode_hoid(&p, end, m->end);
4065 free_hoid(m->begin);
4076 static struct ceph_msg *create_backoff_message(
4077 const struct ceph_osd_backoff *backoff,
4080 struct ceph_msg *msg;
4084 msg_size = CEPH_ENCODING_START_BLK_LEN +
4085 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4086 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4087 msg_size += CEPH_ENCODING_START_BLK_LEN +
4088 hoid_encoding_size(backoff->begin);
4089 msg_size += CEPH_ENCODING_START_BLK_LEN +
4090 hoid_encoding_size(backoff->end);
4092 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4096 p = msg->front.iov_base;
4097 end = p + msg->front_alloc_len;
4099 encode_spgid(&p, &backoff->spgid);
4100 ceph_encode_32(&p, map_epoch);
4101 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4102 ceph_encode_64(&p, backoff->id);
4103 encode_hoid(&p, end, backoff->begin);
4104 encode_hoid(&p, end, backoff->end);
4107 msg->front.iov_len = p - msg->front.iov_base;
4108 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4109 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4114 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4116 struct ceph_spg_mapping *spg;
4117 struct ceph_osd_backoff *backoff;
4118 struct ceph_msg *msg;
4120 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4121 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4123 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4125 spg = alloc_spg_mapping();
4127 pr_err("%s failed to allocate spg\n", __func__);
4130 spg->spgid = m->spgid; /* struct */
4131 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4134 backoff = alloc_backoff();
4136 pr_err("%s failed to allocate backoff\n", __func__);
4139 backoff->spgid = m->spgid; /* struct */
4140 backoff->id = m->id;
4141 backoff->begin = m->begin;
4142 m->begin = NULL; /* backoff now owns this */
4143 backoff->end = m->end;
4144 m->end = NULL; /* ditto */
4146 insert_backoff(&spg->backoffs, backoff);
4147 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4150 * Ack with original backoff's epoch so that the OSD can
4151 * discard this if there was a PG split.
4153 msg = create_backoff_message(backoff, m->map_epoch);
4155 pr_err("%s failed to allocate msg\n", __func__);
4158 ceph_con_send(&osd->o_con, msg);
4161 static bool target_contained_by(const struct ceph_osd_request_target *t,
4162 const struct ceph_hobject_id *begin,
4163 const struct ceph_hobject_id *end)
4165 struct ceph_hobject_id hoid;
4168 hoid_fill_from_target(&hoid, t);
4169 cmp = hoid_compare(&hoid, begin);
4170 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4173 static void handle_backoff_unblock(struct ceph_osd *osd,
4174 const struct MOSDBackoff *m)
4176 struct ceph_spg_mapping *spg;
4177 struct ceph_osd_backoff *backoff;
4180 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4181 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4183 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4185 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4186 __func__, osd->o_osd, m->spgid.pgid.pool,
4187 m->spgid.pgid.seed, m->spgid.shard, m->id);
4191 if (hoid_compare(backoff->begin, m->begin) &&
4192 hoid_compare(backoff->end, m->end)) {
4193 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4194 __func__, osd->o_osd, m->spgid.pgid.pool,
4195 m->spgid.pgid.seed, m->spgid.shard, m->id);
4196 /* unblock it anyway... */
4199 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4202 erase_backoff(&spg->backoffs, backoff);
4203 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4204 free_backoff(backoff);
4206 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4207 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4208 free_spg_mapping(spg);
4211 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4212 struct ceph_osd_request *req =
4213 rb_entry(n, struct ceph_osd_request, r_node);
4215 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4217 * Match against @m, not @backoff -- the PG may
4218 * have split on the OSD.
4220 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4222 * If no other installed backoff applies,
4231 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4233 struct ceph_osd_client *osdc = osd->o_osdc;
4234 struct MOSDBackoff m;
4237 down_read(&osdc->lock);
4238 if (!osd_registered(osd)) {
4239 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4240 up_read(&osdc->lock);
4243 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4245 mutex_lock(&osd->lock);
4246 ret = decode_MOSDBackoff(msg, &m);
4248 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4254 case CEPH_OSD_BACKOFF_OP_BLOCK:
4255 handle_backoff_block(osd, &m);
4257 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4258 handle_backoff_unblock(osd, &m);
4261 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4268 mutex_unlock(&osd->lock);
4269 up_read(&osdc->lock);
4273 * Process osd watch notifications
4275 static void handle_watch_notify(struct ceph_osd_client *osdc,
4276 struct ceph_msg *msg)
4278 void *p = msg->front.iov_base;
4279 void *const end = p + msg->front.iov_len;
4280 struct ceph_osd_linger_request *lreq;
4281 struct linger_work *lwork;
4282 u8 proto_ver, opcode;
4283 u64 cookie, notify_id;
4284 u64 notifier_id = 0;
4285 s32 return_code = 0;
4286 void *payload = NULL;
4287 u32 payload_len = 0;
4289 ceph_decode_8_safe(&p, end, proto_ver, bad);
4290 ceph_decode_8_safe(&p, end, opcode, bad);
4291 ceph_decode_64_safe(&p, end, cookie, bad);
4292 p += 8; /* skip ver */
4293 ceph_decode_64_safe(&p, end, notify_id, bad);
4295 if (proto_ver >= 1) {
4296 ceph_decode_32_safe(&p, end, payload_len, bad);
4297 ceph_decode_need(&p, end, payload_len, bad);
4302 if (le16_to_cpu(msg->hdr.version) >= 2)
4303 ceph_decode_32_safe(&p, end, return_code, bad);
4305 if (le16_to_cpu(msg->hdr.version) >= 3)
4306 ceph_decode_64_safe(&p, end, notifier_id, bad);
4308 down_read(&osdc->lock);
4309 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4311 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4313 goto out_unlock_osdc;
4316 mutex_lock(&lreq->lock);
4317 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4318 opcode, cookie, lreq, lreq->is_watch);
4319 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4320 if (!lreq->last_error) {
4321 lreq->last_error = -ENOTCONN;
4322 queue_watch_error(lreq);
4324 } else if (!lreq->is_watch) {
4325 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4326 if (lreq->notify_id && lreq->notify_id != notify_id) {
4327 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4328 lreq->notify_id, notify_id);
4329 } else if (!completion_done(&lreq->notify_finish_wait)) {
4330 struct ceph_msg_data *data =
4331 list_first_entry_or_null(&msg->data,
4332 struct ceph_msg_data,
4336 if (lreq->preply_pages) {
4337 WARN_ON(data->type !=
4338 CEPH_MSG_DATA_PAGES);
4339 *lreq->preply_pages = data->pages;
4340 *lreq->preply_len = data->length;
4342 ceph_release_page_vector(data->pages,
4343 calc_pages_for(0, data->length));
4346 lreq->notify_finish_error = return_code;
4347 complete_all(&lreq->notify_finish_wait);
4350 /* CEPH_WATCH_EVENT_NOTIFY */
4351 lwork = lwork_alloc(lreq, do_watch_notify);
4353 pr_err("failed to allocate notify-lwork\n");
4354 goto out_unlock_lreq;
4357 lwork->notify.notify_id = notify_id;
4358 lwork->notify.notifier_id = notifier_id;
4359 lwork->notify.payload = payload;
4360 lwork->notify.payload_len = payload_len;
4361 lwork->notify.msg = ceph_msg_get(msg);
4366 mutex_unlock(&lreq->lock);
4368 up_read(&osdc->lock);
4372 pr_err("osdc handle_watch_notify corrupt msg\n");
4376 * Register request, send initial attempt.
4378 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4379 struct ceph_osd_request *req,
4382 down_read(&osdc->lock);
4383 submit_request(req, false);
4384 up_read(&osdc->lock);
4388 EXPORT_SYMBOL(ceph_osdc_start_request);
4391 * Unregister a registered request. The request is not completed:
4392 * ->r_result isn't set and __complete_request() isn't called.
4394 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4396 struct ceph_osd_client *osdc = req->r_osdc;
4398 down_write(&osdc->lock);
4400 cancel_request(req);
4401 up_write(&osdc->lock);
4403 EXPORT_SYMBOL(ceph_osdc_cancel_request);
4406 * @timeout: in jiffies, 0 means "wait forever"
4408 static int wait_request_timeout(struct ceph_osd_request *req,
4409 unsigned long timeout)
4413 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
4414 left = wait_for_completion_killable_timeout(&req->r_completion,
4415 ceph_timeout_jiffies(timeout));
4417 left = left ?: -ETIMEDOUT;
4418 ceph_osdc_cancel_request(req);
4420 left = req->r_result; /* completed */
4427 * wait for a request to complete
4429 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4430 struct ceph_osd_request *req)
4432 return wait_request_timeout(req, 0);
4434 EXPORT_SYMBOL(ceph_osdc_wait_request);
4437 * sync - wait for all in-flight requests to flush. avoid starvation.
4439 void ceph_osdc_sync(struct ceph_osd_client *osdc)
4441 struct rb_node *n, *p;
4442 u64 last_tid = atomic64_read(&osdc->last_tid);
4445 down_read(&osdc->lock);
4446 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4447 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4449 mutex_lock(&osd->lock);
4450 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4451 struct ceph_osd_request *req =
4452 rb_entry(p, struct ceph_osd_request, r_node);
4454 if (req->r_tid > last_tid)
4457 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4460 ceph_osdc_get_request(req);
4461 mutex_unlock(&osd->lock);
4462 up_read(&osdc->lock);
4463 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4464 __func__, req, req->r_tid, last_tid);
4465 wait_for_completion(&req->r_completion);
4466 ceph_osdc_put_request(req);
4470 mutex_unlock(&osd->lock);
4473 up_read(&osdc->lock);
4474 dout("%s done last_tid %llu\n", __func__, last_tid);
4476 EXPORT_SYMBOL(ceph_osdc_sync);
4478 static struct ceph_osd_request *
4479 alloc_linger_request(struct ceph_osd_linger_request *lreq)
4481 struct ceph_osd_request *req;
4483 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4487 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4488 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4490 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4491 ceph_osdc_put_request(req);
4499 * Returns a handle, caller owns a ref.
4501 struct ceph_osd_linger_request *
4502 ceph_osdc_watch(struct ceph_osd_client *osdc,
4503 struct ceph_object_id *oid,
4504 struct ceph_object_locator *oloc,
4505 rados_watchcb2_t wcb,
4506 rados_watcherrcb_t errcb,
4509 struct ceph_osd_linger_request *lreq;
4512 lreq = linger_alloc(osdc);
4514 return ERR_PTR(-ENOMEM);
4516 lreq->is_watch = true;
4518 lreq->errcb = errcb;
4520 lreq->watch_valid_thru = jiffies;
4522 ceph_oid_copy(&lreq->t.base_oid, oid);
4523 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4524 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4525 ktime_get_real_ts(&lreq->mtime);
4527 lreq->reg_req = alloc_linger_request(lreq);
4528 if (!lreq->reg_req) {
4533 lreq->ping_req = alloc_linger_request(lreq);
4534 if (!lreq->ping_req) {
4539 down_write(&osdc->lock);
4540 linger_register(lreq); /* before osd_req_op_* */
4541 osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4542 CEPH_OSD_WATCH_OP_WATCH);
4543 osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4544 CEPH_OSD_WATCH_OP_PING);
4545 linger_submit(lreq);
4546 up_write(&osdc->lock);
4548 ret = linger_reg_commit_wait(lreq);
4550 linger_cancel(lreq);
4558 return ERR_PTR(ret);
4560 EXPORT_SYMBOL(ceph_osdc_watch);
4565 * Times out after mount_timeout to preserve rbd unmap behaviour
4566 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4567 * with mount_timeout").
4569 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4570 struct ceph_osd_linger_request *lreq)
4572 struct ceph_options *opts = osdc->client->options;
4573 struct ceph_osd_request *req;
4576 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4580 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4581 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4582 req->r_flags = CEPH_OSD_FLAG_WRITE;
4583 ktime_get_real_ts(&req->r_mtime);
4584 osd_req_op_watch_init(req, 0, lreq->linger_id,
4585 CEPH_OSD_WATCH_OP_UNWATCH);
4587 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4591 ceph_osdc_start_request(osdc, req, false);
4592 linger_cancel(lreq);
4594 ret = wait_request_timeout(req, opts->mount_timeout);
4597 ceph_osdc_put_request(req);
4600 EXPORT_SYMBOL(ceph_osdc_unwatch);
4602 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4603 u64 notify_id, u64 cookie, void *payload,
4606 struct ceph_osd_req_op *op;
4607 struct ceph_pagelist *pl;
4610 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4612 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4616 ceph_pagelist_init(pl);
4617 ret = ceph_pagelist_encode_64(pl, notify_id);
4618 ret |= ceph_pagelist_encode_64(pl, cookie);
4620 ret |= ceph_pagelist_encode_32(pl, payload_len);
4621 ret |= ceph_pagelist_append(pl, payload, payload_len);
4623 ret |= ceph_pagelist_encode_32(pl, 0);
4626 ceph_pagelist_release(pl);
4630 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4631 op->indata_len = pl->length;
4635 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4636 struct ceph_object_id *oid,
4637 struct ceph_object_locator *oloc,
4643 struct ceph_osd_request *req;
4646 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4650 ceph_oid_copy(&req->r_base_oid, oid);
4651 ceph_oloc_copy(&req->r_base_oloc, oloc);
4652 req->r_flags = CEPH_OSD_FLAG_READ;
4654 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4658 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4663 ceph_osdc_start_request(osdc, req, false);
4664 ret = ceph_osdc_wait_request(osdc, req);
4667 ceph_osdc_put_request(req);
4670 EXPORT_SYMBOL(ceph_osdc_notify_ack);
4672 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4673 u64 cookie, u32 prot_ver, u32 timeout,
4674 void *payload, size_t payload_len)
4676 struct ceph_osd_req_op *op;
4677 struct ceph_pagelist *pl;
4680 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4681 op->notify.cookie = cookie;
4683 pl = kmalloc(sizeof(*pl), GFP_NOIO);
4687 ceph_pagelist_init(pl);
4688 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4689 ret |= ceph_pagelist_encode_32(pl, timeout);
4690 ret |= ceph_pagelist_encode_32(pl, payload_len);
4691 ret |= ceph_pagelist_append(pl, payload, payload_len);
4693 ceph_pagelist_release(pl);
4697 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4698 op->indata_len = pl->length;
4703 * @timeout: in seconds
4705 * @preply_{pages,len} are initialized both on success and error.
4706 * The caller is responsible for:
4708 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4710 int ceph_osdc_notify(struct ceph_osd_client *osdc,
4711 struct ceph_object_id *oid,
4712 struct ceph_object_locator *oloc,
4716 struct page ***preply_pages,
4719 struct ceph_osd_linger_request *lreq;
4720 struct page **pages;
4725 *preply_pages = NULL;
4729 lreq = linger_alloc(osdc);
4733 lreq->preply_pages = preply_pages;
4734 lreq->preply_len = preply_len;
4736 ceph_oid_copy(&lreq->t.base_oid, oid);
4737 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4738 lreq->t.flags = CEPH_OSD_FLAG_READ;
4740 lreq->reg_req = alloc_linger_request(lreq);
4741 if (!lreq->reg_req) {
4747 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4748 if (IS_ERR(pages)) {
4749 ret = PTR_ERR(pages);
4753 down_write(&osdc->lock);
4754 linger_register(lreq); /* before osd_req_op_* */
4755 ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4756 timeout, payload, payload_len);
4758 linger_unregister(lreq);
4759 up_write(&osdc->lock);
4760 ceph_release_page_vector(pages, 1);
4763 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4765 pages, PAGE_SIZE, 0, false, true);
4766 linger_submit(lreq);
4767 up_write(&osdc->lock);
4769 ret = linger_reg_commit_wait(lreq);
4771 ret = linger_notify_finish_wait(lreq);
4773 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4775 linger_cancel(lreq);
4780 EXPORT_SYMBOL(ceph_osdc_notify);
4783 * Return the number of milliseconds since the watch was last
4784 * confirmed, or an error. If there is an error, the watch is no
4785 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4787 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4788 struct ceph_osd_linger_request *lreq)
4790 unsigned long stamp, age;
4793 down_read(&osdc->lock);
4794 mutex_lock(&lreq->lock);
4795 stamp = lreq->watch_valid_thru;
4796 if (!list_empty(&lreq->pending_lworks)) {
4797 struct linger_work *lwork =
4798 list_first_entry(&lreq->pending_lworks,
4802 if (time_before(lwork->queued_stamp, stamp))
4803 stamp = lwork->queued_stamp;
4805 age = jiffies - stamp;
4806 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4807 lreq, lreq->linger_id, age, lreq->last_error);
4808 /* we are truncating to msecs, so return a safe upper bound */
4809 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4811 mutex_unlock(&lreq->lock);
4812 up_read(&osdc->lock);
4816 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4822 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4823 &struct_v, &struct_len);
4827 ceph_decode_copy(p, &item->name, sizeof(item->name));
4828 item->cookie = ceph_decode_64(p);
4829 *p += 4; /* skip timeout_seconds */
4830 if (struct_v >= 2) {
4831 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4832 ceph_decode_addr(&item->addr);
4835 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4836 ENTITY_NAME(item->name), item->cookie,
4837 ceph_pr_addr(&item->addr.in_addr));
4841 static int decode_watchers(void **p, void *end,
4842 struct ceph_watch_item **watchers,
4850 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4851 &struct_v, &struct_len);
4855 *num_watchers = ceph_decode_32(p);
4856 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4860 for (i = 0; i < *num_watchers; i++) {
4861 ret = decode_watcher(p, end, *watchers + i);
4872 * On success, the caller is responsible for:
4876 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4877 struct ceph_object_id *oid,
4878 struct ceph_object_locator *oloc,
4879 struct ceph_watch_item **watchers,
4882 struct ceph_osd_request *req;
4883 struct page **pages;
4886 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4890 ceph_oid_copy(&req->r_base_oid, oid);
4891 ceph_oloc_copy(&req->r_base_oloc, oloc);
4892 req->r_flags = CEPH_OSD_FLAG_READ;
4894 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4898 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4899 if (IS_ERR(pages)) {
4900 ret = PTR_ERR(pages);
4904 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4905 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4907 pages, PAGE_SIZE, 0, false, true);
4909 ceph_osdc_start_request(osdc, req, false);
4910 ret = ceph_osdc_wait_request(osdc, req);
4912 void *p = page_address(pages[0]);
4913 void *const end = p + req->r_ops[0].outdata_len;
4915 ret = decode_watchers(&p, end, watchers, num_watchers);
4919 ceph_osdc_put_request(req);
4922 EXPORT_SYMBOL(ceph_osdc_list_watchers);
4925 * Call all pending notify callbacks - for use after a watch is
4926 * unregistered, to make sure no more callbacks for it will be invoked
4928 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4930 dout("%s osdc %p\n", __func__, osdc);
4931 flush_workqueue(osdc->notify_wq);
4933 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4935 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4937 down_read(&osdc->lock);
4938 maybe_request_map(osdc);
4939 up_read(&osdc->lock);
4941 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4944 * Execute an OSD class method on an object.
4946 * @flags: CEPH_OSD_FLAG_*
4947 * @resp_len: in/out param for reply length
4949 int ceph_osdc_call(struct ceph_osd_client *osdc,
4950 struct ceph_object_id *oid,
4951 struct ceph_object_locator *oloc,
4952 const char *class, const char *method,
4954 struct page *req_page, size_t req_len,
4955 struct page *resp_page, size_t *resp_len)
4957 struct ceph_osd_request *req;
4960 if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4963 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4967 ceph_oid_copy(&req->r_base_oid, oid);
4968 ceph_oloc_copy(&req->r_base_oloc, oloc);
4969 req->r_flags = flags;
4971 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4975 ret = osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4980 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4983 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
4984 *resp_len, 0, false, false);
4986 ceph_osdc_start_request(osdc, req, false);
4987 ret = ceph_osdc_wait_request(osdc, req);
4989 ret = req->r_ops[0].rval;
4991 *resp_len = req->r_ops[0].outdata_len;
4995 ceph_osdc_put_request(req);
4998 EXPORT_SYMBOL(ceph_osdc_call);
5003 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5008 osdc->client = client;
5009 init_rwsem(&osdc->lock);
5010 osdc->osds = RB_ROOT;
5011 INIT_LIST_HEAD(&osdc->osd_lru);
5012 spin_lock_init(&osdc->osd_lru_lock);
5013 osd_init(&osdc->homeless_osd);
5014 osdc->homeless_osd.o_osdc = osdc;
5015 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5016 osdc->last_linger_id = CEPH_LINGER_ID_START;
5017 osdc->linger_requests = RB_ROOT;
5018 osdc->map_checks = RB_ROOT;
5019 osdc->linger_map_checks = RB_ROOT;
5020 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
5021 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5024 osdc->osdmap = ceph_osdmap_alloc();
5028 osdc->req_mempool = mempool_create_slab_pool(10,
5029 ceph_osd_request_cache);
5030 if (!osdc->req_mempool)
5033 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5034 PAGE_SIZE, 10, true, "osd_op");
5037 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5038 PAGE_SIZE, 10, true, "osd_op_reply");
5043 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5044 if (!osdc->notify_wq)
5045 goto out_msgpool_reply;
5047 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5048 if (!osdc->completion_wq)
5051 schedule_delayed_work(&osdc->timeout_work,
5052 osdc->client->options->osd_keepalive_timeout);
5053 schedule_delayed_work(&osdc->osds_timeout_work,
5054 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5059 destroy_workqueue(osdc->notify_wq);
5061 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5063 ceph_msgpool_destroy(&osdc->msgpool_op);
5065 mempool_destroy(osdc->req_mempool);
5067 ceph_osdmap_destroy(osdc->osdmap);
5072 void ceph_osdc_stop(struct ceph_osd_client *osdc)
5074 destroy_workqueue(osdc->completion_wq);
5075 destroy_workqueue(osdc->notify_wq);
5076 cancel_delayed_work_sync(&osdc->timeout_work);
5077 cancel_delayed_work_sync(&osdc->osds_timeout_work);
5079 down_write(&osdc->lock);
5080 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5081 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5082 struct ceph_osd, o_node);
5085 up_write(&osdc->lock);
5086 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5087 osd_cleanup(&osdc->homeless_osd);
5089 WARN_ON(!list_empty(&osdc->osd_lru));
5090 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
5091 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5092 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5093 WARN_ON(atomic_read(&osdc->num_requests));
5094 WARN_ON(atomic_read(&osdc->num_homeless));
5096 ceph_osdmap_destroy(osdc->osdmap);
5097 mempool_destroy(osdc->req_mempool);
5098 ceph_msgpool_destroy(&osdc->msgpool_op);
5099 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5103 * Read some contiguous pages. If we cross a stripe boundary, shorten
5104 * *plen. Return number of bytes read, or error.
5106 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5107 struct ceph_vino vino, struct ceph_file_layout *layout,
5109 u32 truncate_seq, u64 truncate_size,
5110 struct page **pages, int num_pages, int page_align)
5112 struct ceph_osd_request *req;
5115 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5116 vino.snap, off, *plen);
5117 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
5118 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5119 NULL, truncate_seq, truncate_size,
5122 return PTR_ERR(req);
5124 /* it may be a short read due to an object boundary */
5125 osd_req_op_extent_osd_data_pages(req, 0,
5126 pages, *plen, page_align, false, false);
5128 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
5129 off, *plen, *plen, page_align);
5131 rc = ceph_osdc_start_request(osdc, req, false);
5133 rc = ceph_osdc_wait_request(osdc, req);
5135 ceph_osdc_put_request(req);
5136 dout("readpages result %d\n", rc);
5139 EXPORT_SYMBOL(ceph_osdc_readpages);
5142 * do a synchronous write on N pages
5144 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5145 struct ceph_file_layout *layout,
5146 struct ceph_snap_context *snapc,
5148 u32 truncate_seq, u64 truncate_size,
5149 struct timespec *mtime,
5150 struct page **pages, int num_pages)
5152 struct ceph_osd_request *req;
5154 int page_align = off & ~PAGE_MASK;
5156 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
5157 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5158 snapc, truncate_seq, truncate_size,
5161 return PTR_ERR(req);
5163 /* it may be a short write due to an object boundary */
5164 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
5166 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
5168 req->r_mtime = *mtime;
5169 rc = ceph_osdc_start_request(osdc, req, true);
5171 rc = ceph_osdc_wait_request(osdc, req);
5173 ceph_osdc_put_request(req);
5176 dout("writepages result %d\n", rc);
5179 EXPORT_SYMBOL(ceph_osdc_writepages);
5181 int __init ceph_osdc_setup(void)
5183 size_t size = sizeof(struct ceph_osd_request) +
5184 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5186 BUG_ON(ceph_osd_request_cache);
5187 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5190 return ceph_osd_request_cache ? 0 : -ENOMEM;
5193 void ceph_osdc_cleanup(void)
5195 BUG_ON(!ceph_osd_request_cache);
5196 kmem_cache_destroy(ceph_osd_request_cache);
5197 ceph_osd_request_cache = NULL;
5201 * handle incoming message
5203 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5205 struct ceph_osd *osd = con->private;
5206 struct ceph_osd_client *osdc = osd->o_osdc;
5207 int type = le16_to_cpu(msg->hdr.type);
5210 case CEPH_MSG_OSD_MAP:
5211 ceph_osdc_handle_map(osdc, msg);
5213 case CEPH_MSG_OSD_OPREPLY:
5214 handle_reply(osd, msg);
5216 case CEPH_MSG_OSD_BACKOFF:
5217 handle_backoff(osd, msg);
5219 case CEPH_MSG_WATCH_NOTIFY:
5220 handle_watch_notify(osdc, msg);
5224 pr_err("received unknown message type %d %s\n", type,
5225 ceph_msg_type_name(type));
5232 * Lookup and return message for incoming reply. Don't try to do
5233 * anything about a larger than preallocated data portion of the
5234 * message at the moment - for now, just skip the message.
5236 static struct ceph_msg *get_reply(struct ceph_connection *con,
5237 struct ceph_msg_header *hdr,
5240 struct ceph_osd *osd = con->private;
5241 struct ceph_osd_client *osdc = osd->o_osdc;
5242 struct ceph_msg *m = NULL;
5243 struct ceph_osd_request *req;
5244 int front_len = le32_to_cpu(hdr->front_len);
5245 int data_len = le32_to_cpu(hdr->data_len);
5246 u64 tid = le64_to_cpu(hdr->tid);
5248 down_read(&osdc->lock);
5249 if (!osd_registered(osd)) {
5250 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5252 goto out_unlock_osdc;
5254 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5256 mutex_lock(&osd->lock);
5257 req = lookup_request(&osd->o_requests, tid);
5259 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5262 goto out_unlock_session;
5265 ceph_msg_revoke_incoming(req->r_reply);
5267 if (front_len > req->r_reply->front_alloc_len) {
5268 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5269 __func__, osd->o_osd, req->r_tid, front_len,
5270 req->r_reply->front_alloc_len);
5271 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5274 goto out_unlock_session;
5275 ceph_msg_put(req->r_reply);
5279 if (data_len > req->r_reply->data_length) {
5280 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5281 __func__, osd->o_osd, req->r_tid, data_len,
5282 req->r_reply->data_length);
5285 goto out_unlock_session;
5288 m = ceph_msg_get(req->r_reply);
5289 dout("get_reply tid %lld %p\n", tid, m);
5292 mutex_unlock(&osd->lock);
5294 up_read(&osdc->lock);
5299 * TODO: switch to a msg-owned pagelist
5301 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5304 int type = le16_to_cpu(hdr->type);
5305 u32 front_len = le32_to_cpu(hdr->front_len);
5306 u32 data_len = le32_to_cpu(hdr->data_len);
5308 m = ceph_msg_new(type, front_len, GFP_NOIO, false);
5313 struct page **pages;
5314 struct ceph_osd_data osd_data;
5316 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5318 if (IS_ERR(pages)) {
5323 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5325 ceph_osdc_msg_data_add(m, &osd_data);
5331 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5332 struct ceph_msg_header *hdr,
5335 struct ceph_osd *osd = con->private;
5336 int type = le16_to_cpu(hdr->type);
5340 case CEPH_MSG_OSD_MAP:
5341 case CEPH_MSG_OSD_BACKOFF:
5342 case CEPH_MSG_WATCH_NOTIFY:
5343 return alloc_msg_with_page_vector(hdr);
5344 case CEPH_MSG_OSD_OPREPLY:
5345 return get_reply(con, hdr, skip);
5347 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5355 * Wrappers to refcount containing ceph_osd struct
5357 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5359 struct ceph_osd *osd = con->private;
5365 static void put_osd_con(struct ceph_connection *con)
5367 struct ceph_osd *osd = con->private;
5375 * Note: returned pointer is the address of a structure that's
5376 * managed separately. Caller must *not* attempt to free it.
5378 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
5379 int *proto, int force_new)
5381 struct ceph_osd *o = con->private;
5382 struct ceph_osd_client *osdc = o->o_osdc;
5383 struct ceph_auth_client *ac = osdc->client->monc.auth;
5384 struct ceph_auth_handshake *auth = &o->o_auth;
5386 if (force_new && auth->authorizer) {
5387 ceph_auth_destroy_authorizer(auth->authorizer);
5388 auth->authorizer = NULL;
5390 if (!auth->authorizer) {
5391 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5394 return ERR_PTR(ret);
5396 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5399 return ERR_PTR(ret);
5401 *proto = ac->protocol;
5407 static int verify_authorizer_reply(struct ceph_connection *con)
5409 struct ceph_osd *o = con->private;
5410 struct ceph_osd_client *osdc = o->o_osdc;
5411 struct ceph_auth_client *ac = osdc->client->monc.auth;
5413 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
5416 static int invalidate_authorizer(struct ceph_connection *con)
5418 struct ceph_osd *o = con->private;
5419 struct ceph_osd_client *osdc = o->o_osdc;
5420 struct ceph_auth_client *ac = osdc->client->monc.auth;
5422 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
5423 return ceph_monc_validate_auth(&osdc->client->monc);
5426 static void osd_reencode_message(struct ceph_msg *msg)
5428 int type = le16_to_cpu(msg->hdr.type);
5430 if (type == CEPH_MSG_OSD_OP)
5431 encode_request_finish(msg);
5434 static int osd_sign_message(struct ceph_msg *msg)
5436 struct ceph_osd *o = msg->con->private;
5437 struct ceph_auth_handshake *auth = &o->o_auth;
5439 return ceph_auth_sign_message(auth, msg);
5442 static int osd_check_message_signature(struct ceph_msg *msg)
5444 struct ceph_osd *o = msg->con->private;
5445 struct ceph_auth_handshake *auth = &o->o_auth;
5447 return ceph_auth_check_message_signature(auth, msg);
5450 static const struct ceph_connection_operations osd_con_ops = {
5453 .dispatch = dispatch,
5454 .get_authorizer = get_authorizer,
5455 .verify_authorizer_reply = verify_authorizer_reply,
5456 .invalidate_authorizer = invalidate_authorizer,
5457 .alloc_msg = alloc_msg,
5458 .reencode_message = osd_reencode_message,
5459 .sign_message = osd_sign_message,
5460 .check_message_signature = osd_check_message_signature,