1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/ceph/ceph_debug.h>
5 #include <linux/module.h>
7 #include <linux/highmem.h>
9 #include <linux/pagemap.h>
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
13 #include <linux/bio.h>
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/osd_client.h>
19 #include <linux/ceph/messenger.h>
20 #include <linux/ceph/decode.h>
21 #include <linux/ceph/auth.h>
22 #include <linux/ceph/pagelist.h>
23 #include <linux/ceph/striper.h>
25 #define OSD_OPREPLY_FRONT_LEN 512
27 static struct kmem_cache *ceph_osd_request_cache;
29 static const struct ceph_connection_operations osd_con_ops;
32 * Implement client access to distributed object storage cluster.
34 * All data objects are stored within a cluster/cloud of OSDs, or
35 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
36 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
37 * remote daemons serving up and coordinating consistent and safe
40 * Cluster membership and the mapping of data objects onto storage devices
41 * are described by the osd map.
43 * We keep track of pending OSD requests (read, write), resubmit
44 * requests to different OSDs when the cluster topology/data layout
45 * change, or retry the affected requests when the communications
46 * channel with an OSD is reset.
49 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51 static void link_linger(struct ceph_osd *osd,
52 struct ceph_osd_linger_request *lreq);
53 static void unlink_linger(struct ceph_osd *osd,
54 struct ceph_osd_linger_request *lreq);
55 static void clear_backoffs(struct ceph_osd *osd);
58 static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
62 if (unlikely(down_read_trylock(sem))) {
69 static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
71 WARN_ON(!rwsem_is_locked(&osdc->lock));
73 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
75 WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
77 static inline void verify_osd_locked(struct ceph_osd *osd)
79 struct ceph_osd_client *osdc = osd->o_osdc;
81 WARN_ON(!(mutex_is_locked(&osd->lock) &&
82 rwsem_is_locked(&osdc->lock)) &&
83 !rwsem_is_wrlocked(&osdc->lock));
85 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
87 WARN_ON(!mutex_is_locked(&lreq->lock));
90 static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
91 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
92 static inline void verify_osd_locked(struct ceph_osd *osd) { }
93 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
97 * calculate the mapping of a file extent onto an object, and fill out the
98 * request accordingly. shorten extent as necessary if it crosses an
101 * fill osd op in request message.
103 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104 u64 *objnum, u64 *objoff, u64 *objlen)
106 u64 orig_len = *plen;
110 ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
113 if (*objlen < orig_len) {
115 dout(" skipping last %llu, final file extent %llu~%llu\n",
116 orig_len - *plen, off, *plen);
119 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
123 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
125 memset(osd_data, 0, sizeof (*osd_data));
126 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
130 * Consumes @pages if @own_pages is true.
132 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
133 struct page **pages, u64 length, u32 alignment,
134 bool pages_from_pool, bool own_pages)
136 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
137 osd_data->pages = pages;
138 osd_data->length = length;
139 osd_data->alignment = alignment;
140 osd_data->pages_from_pool = pages_from_pool;
141 osd_data->own_pages = own_pages;
145 * Consumes a ref on @pagelist.
147 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
148 struct ceph_pagelist *pagelist)
150 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
151 osd_data->pagelist = pagelist;
155 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
156 struct ceph_bio_iter *bio_pos,
159 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
160 osd_data->bio_pos = *bio_pos;
161 osd_data->bio_length = bio_length;
163 #endif /* CONFIG_BLOCK */
165 static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
166 struct ceph_bvec_iter *bvec_pos,
169 osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170 osd_data->bvec_pos = *bvec_pos;
171 osd_data->num_bvecs = num_bvecs;
174 static struct ceph_osd_data *
175 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
177 BUG_ON(which >= osd_req->r_num_ops);
179 return &osd_req->r_ops[which].raw_data_in;
182 struct ceph_osd_data *
183 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
186 return osd_req_op_data(osd_req, which, extent, osd_data);
188 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
190 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
191 unsigned int which, struct page **pages,
192 u64 length, u32 alignment,
193 bool pages_from_pool, bool own_pages)
195 struct ceph_osd_data *osd_data;
197 osd_data = osd_req_op_raw_data_in(osd_req, which);
198 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
199 pages_from_pool, own_pages);
201 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
203 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
204 unsigned int which, struct page **pages,
205 u64 length, u32 alignment,
206 bool pages_from_pool, bool own_pages)
208 struct ceph_osd_data *osd_data;
210 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
211 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
212 pages_from_pool, own_pages);
214 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
216 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
217 unsigned int which, struct ceph_pagelist *pagelist)
219 struct ceph_osd_data *osd_data;
221 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
222 ceph_osd_data_pagelist_init(osd_data, pagelist);
224 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
227 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
229 struct ceph_bio_iter *bio_pos,
232 struct ceph_osd_data *osd_data;
234 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
235 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
237 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
238 #endif /* CONFIG_BLOCK */
240 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
242 struct bio_vec *bvecs, u32 num_bvecs,
245 struct ceph_osd_data *osd_data;
246 struct ceph_bvec_iter it = {
248 .iter = { .bi_size = bytes },
251 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
252 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
254 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
256 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
258 struct ceph_bvec_iter *bvec_pos)
260 struct ceph_osd_data *osd_data;
262 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
263 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
265 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
267 static void osd_req_op_cls_request_info_pagelist(
268 struct ceph_osd_request *osd_req,
269 unsigned int which, struct ceph_pagelist *pagelist)
271 struct ceph_osd_data *osd_data;
273 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
274 ceph_osd_data_pagelist_init(osd_data, pagelist);
277 void osd_req_op_cls_request_data_pagelist(
278 struct ceph_osd_request *osd_req,
279 unsigned int which, struct ceph_pagelist *pagelist)
281 struct ceph_osd_data *osd_data;
283 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
284 ceph_osd_data_pagelist_init(osd_data, pagelist);
285 osd_req->r_ops[which].cls.indata_len += pagelist->length;
286 osd_req->r_ops[which].indata_len += pagelist->length;
288 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
290 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
291 unsigned int which, struct page **pages, u64 length,
292 u32 alignment, bool pages_from_pool, bool own_pages)
294 struct ceph_osd_data *osd_data;
296 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
297 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
298 pages_from_pool, own_pages);
299 osd_req->r_ops[which].cls.indata_len += length;
300 osd_req->r_ops[which].indata_len += length;
302 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
304 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
306 struct bio_vec *bvecs, u32 num_bvecs,
309 struct ceph_osd_data *osd_data;
310 struct ceph_bvec_iter it = {
312 .iter = { .bi_size = bytes },
315 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
316 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
317 osd_req->r_ops[which].cls.indata_len += bytes;
318 osd_req->r_ops[which].indata_len += bytes;
320 EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
322 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
323 unsigned int which, struct page **pages, u64 length,
324 u32 alignment, bool pages_from_pool, bool own_pages)
326 struct ceph_osd_data *osd_data;
328 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
329 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
330 pages_from_pool, own_pages);
332 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
334 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
336 switch (osd_data->type) {
337 case CEPH_OSD_DATA_TYPE_NONE:
339 case CEPH_OSD_DATA_TYPE_PAGES:
340 return osd_data->length;
341 case CEPH_OSD_DATA_TYPE_PAGELIST:
342 return (u64)osd_data->pagelist->length;
344 case CEPH_OSD_DATA_TYPE_BIO:
345 return (u64)osd_data->bio_length;
346 #endif /* CONFIG_BLOCK */
347 case CEPH_OSD_DATA_TYPE_BVECS:
348 return osd_data->bvec_pos.iter.bi_size;
350 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
355 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
357 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
360 num_pages = calc_pages_for((u64)osd_data->alignment,
361 (u64)osd_data->length);
362 ceph_release_page_vector(osd_data->pages, num_pages);
363 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
364 ceph_pagelist_release(osd_data->pagelist);
366 ceph_osd_data_init(osd_data);
369 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
372 struct ceph_osd_req_op *op;
374 BUG_ON(which >= osd_req->r_num_ops);
375 op = &osd_req->r_ops[which];
378 case CEPH_OSD_OP_READ:
379 case CEPH_OSD_OP_WRITE:
380 case CEPH_OSD_OP_WRITEFULL:
381 ceph_osd_data_release(&op->extent.osd_data);
383 case CEPH_OSD_OP_CALL:
384 ceph_osd_data_release(&op->cls.request_info);
385 ceph_osd_data_release(&op->cls.request_data);
386 ceph_osd_data_release(&op->cls.response_data);
388 case CEPH_OSD_OP_SETXATTR:
389 case CEPH_OSD_OP_CMPXATTR:
390 ceph_osd_data_release(&op->xattr.osd_data);
392 case CEPH_OSD_OP_STAT:
393 ceph_osd_data_release(&op->raw_data_in);
395 case CEPH_OSD_OP_NOTIFY_ACK:
396 ceph_osd_data_release(&op->notify_ack.request_data);
398 case CEPH_OSD_OP_NOTIFY:
399 ceph_osd_data_release(&op->notify.request_data);
400 ceph_osd_data_release(&op->notify.response_data);
402 case CEPH_OSD_OP_LIST_WATCHERS:
403 ceph_osd_data_release(&op->list_watchers.response_data);
405 case CEPH_OSD_OP_COPY_FROM:
406 ceph_osd_data_release(&op->copy_from.osd_data);
414 * Assumes @t is zero-initialized.
416 static void target_init(struct ceph_osd_request_target *t)
418 ceph_oid_init(&t->base_oid);
419 ceph_oloc_init(&t->base_oloc);
420 ceph_oid_init(&t->target_oid);
421 ceph_oloc_init(&t->target_oloc);
423 ceph_osds_init(&t->acting);
424 ceph_osds_init(&t->up);
428 t->osd = CEPH_HOMELESS_OSD;
431 static void target_copy(struct ceph_osd_request_target *dest,
432 const struct ceph_osd_request_target *src)
434 ceph_oid_copy(&dest->base_oid, &src->base_oid);
435 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
436 ceph_oid_copy(&dest->target_oid, &src->target_oid);
437 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
439 dest->pgid = src->pgid; /* struct */
440 dest->spgid = src->spgid; /* struct */
441 dest->pg_num = src->pg_num;
442 dest->pg_num_mask = src->pg_num_mask;
443 ceph_osds_copy(&dest->acting, &src->acting);
444 ceph_osds_copy(&dest->up, &src->up);
445 dest->size = src->size;
446 dest->min_size = src->min_size;
447 dest->sort_bitwise = src->sort_bitwise;
449 dest->flags = src->flags;
450 dest->paused = src->paused;
452 dest->epoch = src->epoch;
453 dest->last_force_resend = src->last_force_resend;
455 dest->osd = src->osd;
458 static void target_destroy(struct ceph_osd_request_target *t)
460 ceph_oid_destroy(&t->base_oid);
461 ceph_oloc_destroy(&t->base_oloc);
462 ceph_oid_destroy(&t->target_oid);
463 ceph_oloc_destroy(&t->target_oloc);
469 static void request_release_checks(struct ceph_osd_request *req)
471 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
472 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
473 WARN_ON(!list_empty(&req->r_private_item));
477 static void ceph_osdc_release_request(struct kref *kref)
479 struct ceph_osd_request *req = container_of(kref,
480 struct ceph_osd_request, r_kref);
483 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
484 req->r_request, req->r_reply);
485 request_release_checks(req);
488 ceph_msg_put(req->r_request);
490 ceph_msg_put(req->r_reply);
492 for (which = 0; which < req->r_num_ops; which++)
493 osd_req_op_data_release(req, which);
495 target_destroy(&req->r_t);
496 ceph_put_snap_context(req->r_snapc);
499 mempool_free(req, req->r_osdc->req_mempool);
500 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
501 kmem_cache_free(ceph_osd_request_cache, req);
506 void ceph_osdc_get_request(struct ceph_osd_request *req)
508 dout("%s %p (was %d)\n", __func__, req,
509 kref_read(&req->r_kref));
510 kref_get(&req->r_kref);
512 EXPORT_SYMBOL(ceph_osdc_get_request);
514 void ceph_osdc_put_request(struct ceph_osd_request *req)
517 dout("%s %p (was %d)\n", __func__, req,
518 kref_read(&req->r_kref));
519 kref_put(&req->r_kref, ceph_osdc_release_request);
522 EXPORT_SYMBOL(ceph_osdc_put_request);
524 static void request_init(struct ceph_osd_request *req)
526 /* req only, each op is zeroed in _osd_req_op_init() */
527 memset(req, 0, sizeof(*req));
529 kref_init(&req->r_kref);
530 init_completion(&req->r_completion);
531 RB_CLEAR_NODE(&req->r_node);
532 RB_CLEAR_NODE(&req->r_mc_node);
533 INIT_LIST_HEAD(&req->r_private_item);
535 target_init(&req->r_t);
539 * This is ugly, but it allows us to reuse linger registration and ping
540 * requests, keeping the structure of the code around send_linger{_ping}()
541 * reasonable. Setting up a min_nr=2 mempool for each linger request
542 * and dealing with copying ops (this blasts req only, watch op remains
543 * intact) isn't any better.
545 static void request_reinit(struct ceph_osd_request *req)
547 struct ceph_osd_client *osdc = req->r_osdc;
548 bool mempool = req->r_mempool;
549 unsigned int num_ops = req->r_num_ops;
550 u64 snapid = req->r_snapid;
551 struct ceph_snap_context *snapc = req->r_snapc;
552 bool linger = req->r_linger;
553 struct ceph_msg *request_msg = req->r_request;
554 struct ceph_msg *reply_msg = req->r_reply;
556 dout("%s req %p\n", __func__, req);
557 WARN_ON(kref_read(&req->r_kref) != 1);
558 request_release_checks(req);
560 WARN_ON(kref_read(&request_msg->kref) != 1);
561 WARN_ON(kref_read(&reply_msg->kref) != 1);
562 target_destroy(&req->r_t);
566 req->r_mempool = mempool;
567 req->r_num_ops = num_ops;
568 req->r_snapid = snapid;
569 req->r_snapc = snapc;
570 req->r_linger = linger;
571 req->r_request = request_msg;
572 req->r_reply = reply_msg;
575 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
576 struct ceph_snap_context *snapc,
577 unsigned int num_ops,
581 struct ceph_osd_request *req;
584 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
585 req = mempool_alloc(osdc->req_mempool, gfp_flags);
586 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
587 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
589 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
590 req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
597 req->r_mempool = use_mempool;
598 req->r_num_ops = num_ops;
599 req->r_snapid = CEPH_NOSNAP;
600 req->r_snapc = ceph_get_snap_context(snapc);
602 dout("%s req %p\n", __func__, req);
605 EXPORT_SYMBOL(ceph_osdc_alloc_request);
607 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
609 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
612 static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
613 int num_request_data_items,
614 int num_reply_data_items)
616 struct ceph_osd_client *osdc = req->r_osdc;
617 struct ceph_msg *msg;
620 WARN_ON(req->r_request || req->r_reply);
621 WARN_ON(ceph_oid_empty(&req->r_base_oid));
622 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
624 /* create request message */
625 msg_size = CEPH_ENCODING_START_BLK_LEN +
626 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
627 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
628 msg_size += CEPH_ENCODING_START_BLK_LEN +
629 sizeof(struct ceph_osd_reqid); /* reqid */
630 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
631 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
632 msg_size += CEPH_ENCODING_START_BLK_LEN +
633 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
634 msg_size += 4 + req->r_base_oid.name_len; /* oid */
635 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
636 msg_size += 8; /* snapid */
637 msg_size += 8; /* snap_seq */
638 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
639 msg_size += 4 + 8; /* retry_attempt, features */
642 msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
643 num_request_data_items);
645 msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
646 num_request_data_items, gfp, true);
650 memset(msg->front.iov_base, 0, msg->front.iov_len);
651 req->r_request = msg;
653 /* create reply message */
654 msg_size = OSD_OPREPLY_FRONT_LEN;
655 msg_size += req->r_base_oid.name_len;
656 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
659 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
660 num_reply_data_items);
662 msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
663 num_reply_data_items, gfp, true);
672 static bool osd_req_opcode_valid(u16 opcode)
675 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
676 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
683 static void get_num_data_items(struct ceph_osd_request *req,
684 int *num_request_data_items,
685 int *num_reply_data_items)
687 struct ceph_osd_req_op *op;
689 *num_request_data_items = 0;
690 *num_reply_data_items = 0;
692 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
695 case CEPH_OSD_OP_WRITE:
696 case CEPH_OSD_OP_WRITEFULL:
697 case CEPH_OSD_OP_SETXATTR:
698 case CEPH_OSD_OP_CMPXATTR:
699 case CEPH_OSD_OP_NOTIFY_ACK:
700 case CEPH_OSD_OP_COPY_FROM:
701 *num_request_data_items += 1;
705 case CEPH_OSD_OP_STAT:
706 case CEPH_OSD_OP_READ:
707 case CEPH_OSD_OP_LIST_WATCHERS:
708 *num_reply_data_items += 1;
712 case CEPH_OSD_OP_NOTIFY:
713 *num_request_data_items += 1;
714 *num_reply_data_items += 1;
716 case CEPH_OSD_OP_CALL:
717 *num_request_data_items += 2;
718 *num_reply_data_items += 1;
722 WARN_ON(!osd_req_opcode_valid(op->op));
729 * oid, oloc and OSD op opcode(s) must be filled in before this function
732 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
734 int num_request_data_items, num_reply_data_items;
736 get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
737 return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
738 num_reply_data_items);
740 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
743 * This is an osd op init function for opcodes that have no data or
744 * other information associated with them. It also serves as a
745 * common init routine for all the other init functions, below.
747 static struct ceph_osd_req_op *
748 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
749 u16 opcode, u32 flags)
751 struct ceph_osd_req_op *op;
753 BUG_ON(which >= osd_req->r_num_ops);
754 BUG_ON(!osd_req_opcode_valid(opcode));
756 op = &osd_req->r_ops[which];
757 memset(op, 0, sizeof (*op));
764 void osd_req_op_init(struct ceph_osd_request *osd_req,
765 unsigned int which, u16 opcode, u32 flags)
767 (void)_osd_req_op_init(osd_req, which, opcode, flags);
769 EXPORT_SYMBOL(osd_req_op_init);
771 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
772 unsigned int which, u16 opcode,
773 u64 offset, u64 length,
774 u64 truncate_size, u32 truncate_seq)
776 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
778 size_t payload_len = 0;
780 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
781 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
782 opcode != CEPH_OSD_OP_TRUNCATE);
784 op->extent.offset = offset;
785 op->extent.length = length;
786 op->extent.truncate_size = truncate_size;
787 op->extent.truncate_seq = truncate_seq;
788 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
789 payload_len += length;
791 op->indata_len = payload_len;
793 EXPORT_SYMBOL(osd_req_op_extent_init);
795 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
796 unsigned int which, u64 length)
798 struct ceph_osd_req_op *op;
801 BUG_ON(which >= osd_req->r_num_ops);
802 op = &osd_req->r_ops[which];
803 previous = op->extent.length;
805 if (length == previous)
806 return; /* Nothing to do */
807 BUG_ON(length > previous);
809 op->extent.length = length;
810 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
811 op->indata_len -= previous - length;
813 EXPORT_SYMBOL(osd_req_op_extent_update);
815 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
816 unsigned int which, u64 offset_inc)
818 struct ceph_osd_req_op *op, *prev_op;
820 BUG_ON(which + 1 >= osd_req->r_num_ops);
822 prev_op = &osd_req->r_ops[which];
823 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
824 /* dup previous one */
825 op->indata_len = prev_op->indata_len;
826 op->outdata_len = prev_op->outdata_len;
827 op->extent = prev_op->extent;
829 op->extent.offset += offset_inc;
830 op->extent.length -= offset_inc;
832 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
833 op->indata_len -= offset_inc;
835 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
837 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
838 const char *class, const char *method)
840 struct ceph_osd_req_op *op;
841 struct ceph_pagelist *pagelist;
842 size_t payload_len = 0;
845 op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
847 pagelist = ceph_pagelist_alloc(GFP_NOFS);
851 op->cls.class_name = class;
852 size = strlen(class);
853 BUG_ON(size > (size_t) U8_MAX);
854 op->cls.class_len = size;
855 ceph_pagelist_append(pagelist, class, size);
858 op->cls.method_name = method;
859 size = strlen(method);
860 BUG_ON(size > (size_t) U8_MAX);
861 op->cls.method_len = size;
862 ceph_pagelist_append(pagelist, method, size);
865 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
867 op->indata_len = payload_len;
870 EXPORT_SYMBOL(osd_req_op_cls_init);
872 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
873 u16 opcode, const char *name, const void *value,
874 size_t size, u8 cmp_op, u8 cmp_mode)
876 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
878 struct ceph_pagelist *pagelist;
881 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
883 pagelist = ceph_pagelist_alloc(GFP_NOFS);
887 payload_len = strlen(name);
888 op->xattr.name_len = payload_len;
889 ceph_pagelist_append(pagelist, name, payload_len);
891 op->xattr.value_len = size;
892 ceph_pagelist_append(pagelist, value, size);
895 op->xattr.cmp_op = cmp_op;
896 op->xattr.cmp_mode = cmp_mode;
898 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
899 op->indata_len = payload_len;
902 EXPORT_SYMBOL(osd_req_op_xattr_init);
905 * @watch_opcode: CEPH_OSD_WATCH_OP_*
907 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
908 u64 cookie, u8 watch_opcode)
910 struct ceph_osd_req_op *op;
912 op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
913 op->watch.cookie = cookie;
914 op->watch.op = watch_opcode;
918 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
920 u64 expected_object_size,
921 u64 expected_write_size)
923 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
924 CEPH_OSD_OP_SETALLOCHINT,
927 op->alloc_hint.expected_object_size = expected_object_size;
928 op->alloc_hint.expected_write_size = expected_write_size;
931 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
932 * not worth a feature bit. Set FAILOK per-op flag to make
933 * sure older osds don't trip over an unsupported opcode.
935 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
937 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
939 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
940 struct ceph_osd_data *osd_data)
942 u64 length = ceph_osd_data_length(osd_data);
944 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
945 BUG_ON(length > (u64) SIZE_MAX);
947 ceph_msg_data_add_pages(msg, osd_data->pages,
948 length, osd_data->alignment);
949 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
951 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
953 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
954 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
956 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
957 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
959 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
963 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
964 const struct ceph_osd_req_op *src)
967 case CEPH_OSD_OP_STAT:
969 case CEPH_OSD_OP_READ:
970 case CEPH_OSD_OP_WRITE:
971 case CEPH_OSD_OP_WRITEFULL:
972 case CEPH_OSD_OP_ZERO:
973 case CEPH_OSD_OP_TRUNCATE:
974 dst->extent.offset = cpu_to_le64(src->extent.offset);
975 dst->extent.length = cpu_to_le64(src->extent.length);
976 dst->extent.truncate_size =
977 cpu_to_le64(src->extent.truncate_size);
978 dst->extent.truncate_seq =
979 cpu_to_le32(src->extent.truncate_seq);
981 case CEPH_OSD_OP_CALL:
982 dst->cls.class_len = src->cls.class_len;
983 dst->cls.method_len = src->cls.method_len;
984 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
986 case CEPH_OSD_OP_WATCH:
987 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
988 dst->watch.ver = cpu_to_le64(0);
989 dst->watch.op = src->watch.op;
990 dst->watch.gen = cpu_to_le32(src->watch.gen);
992 case CEPH_OSD_OP_NOTIFY_ACK:
994 case CEPH_OSD_OP_NOTIFY:
995 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
997 case CEPH_OSD_OP_LIST_WATCHERS:
999 case CEPH_OSD_OP_SETALLOCHINT:
1000 dst->alloc_hint.expected_object_size =
1001 cpu_to_le64(src->alloc_hint.expected_object_size);
1002 dst->alloc_hint.expected_write_size =
1003 cpu_to_le64(src->alloc_hint.expected_write_size);
1005 case CEPH_OSD_OP_SETXATTR:
1006 case CEPH_OSD_OP_CMPXATTR:
1007 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1008 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1009 dst->xattr.cmp_op = src->xattr.cmp_op;
1010 dst->xattr.cmp_mode = src->xattr.cmp_mode;
1012 case CEPH_OSD_OP_CREATE:
1013 case CEPH_OSD_OP_DELETE:
1015 case CEPH_OSD_OP_COPY_FROM:
1016 dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
1017 dst->copy_from.src_version =
1018 cpu_to_le64(src->copy_from.src_version);
1019 dst->copy_from.flags = src->copy_from.flags;
1020 dst->copy_from.src_fadvise_flags =
1021 cpu_to_le32(src->copy_from.src_fadvise_flags);
1024 pr_err("unsupported osd opcode %s\n",
1025 ceph_osd_op_name(src->op));
1031 dst->op = cpu_to_le16(src->op);
1032 dst->flags = cpu_to_le32(src->flags);
1033 dst->payload_len = cpu_to_le32(src->indata_len);
1035 return src->indata_len;
1039 * build new request AND message, calculate layout, and adjust file
1042 * if the file was recently truncated, we include information about its
1043 * old and new size so that the object can be updated appropriately. (we
1044 * avoid synchronously deleting truncated objects because it's slow.)
1046 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
1047 struct ceph_file_layout *layout,
1048 struct ceph_vino vino,
1050 unsigned int which, int num_ops,
1051 int opcode, int flags,
1052 struct ceph_snap_context *snapc,
1057 struct ceph_osd_request *req;
1063 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1064 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1065 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
1067 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1074 /* calculate max write size */
1075 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1079 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1080 osd_req_op_init(req, which, opcode, 0);
1082 u32 object_size = layout->object_size;
1083 u32 object_base = off - objoff;
1084 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1085 if (truncate_size <= object_base) {
1088 truncate_size -= object_base;
1089 if (truncate_size > object_size)
1090 truncate_size = object_size;
1093 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1094 truncate_size, truncate_seq);
1097 req->r_flags = flags;
1098 req->r_base_oloc.pool = layout->pool_id;
1099 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1100 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1102 req->r_snapid = vino.snap;
1103 if (flags & CEPH_OSD_FLAG_WRITE)
1104 req->r_data_offset = off;
1108 * This is a special case for ceph_writepages_start(), but it
1109 * also covers ceph_uninline_data(). If more multi-op request
1110 * use cases emerge, we will need a separate helper.
1112 r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
1114 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1121 ceph_osdc_put_request(req);
1124 EXPORT_SYMBOL(ceph_osdc_new_request);
1127 * We keep osd requests in an rbtree, sorted by ->r_tid.
1129 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1130 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1133 * Call @fn on each OSD request as long as @fn returns 0.
1135 static void for_each_request(struct ceph_osd_client *osdc,
1136 int (*fn)(struct ceph_osd_request *req, void *arg),
1139 struct rb_node *n, *p;
1141 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1142 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1144 for (p = rb_first(&osd->o_requests); p; ) {
1145 struct ceph_osd_request *req =
1146 rb_entry(p, struct ceph_osd_request, r_node);
1154 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1155 struct ceph_osd_request *req =
1156 rb_entry(p, struct ceph_osd_request, r_node);
1164 static bool osd_homeless(struct ceph_osd *osd)
1166 return osd->o_osd == CEPH_HOMELESS_OSD;
1169 static bool osd_registered(struct ceph_osd *osd)
1171 verify_osdc_locked(osd->o_osdc);
1173 return !RB_EMPTY_NODE(&osd->o_node);
1177 * Assumes @osd is zero-initialized.
1179 static void osd_init(struct ceph_osd *osd)
1181 refcount_set(&osd->o_ref, 1);
1182 RB_CLEAR_NODE(&osd->o_node);
1183 osd->o_requests = RB_ROOT;
1184 osd->o_linger_requests = RB_ROOT;
1185 osd->o_backoff_mappings = RB_ROOT;
1186 osd->o_backoffs_by_id = RB_ROOT;
1187 INIT_LIST_HEAD(&osd->o_osd_lru);
1188 INIT_LIST_HEAD(&osd->o_keepalive_item);
1189 osd->o_incarnation = 1;
1190 mutex_init(&osd->lock);
1193 static void osd_cleanup(struct ceph_osd *osd)
1195 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1196 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1197 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1198 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1199 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1200 WARN_ON(!list_empty(&osd->o_osd_lru));
1201 WARN_ON(!list_empty(&osd->o_keepalive_item));
1203 if (osd->o_auth.authorizer) {
1204 WARN_ON(osd_homeless(osd));
1205 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1210 * Track open sessions with osds.
1212 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1214 struct ceph_osd *osd;
1216 WARN_ON(onum == CEPH_HOMELESS_OSD);
1218 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1223 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1228 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1230 if (refcount_inc_not_zero(&osd->o_ref)) {
1231 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1232 refcount_read(&osd->o_ref));
1235 dout("get_osd %p FAIL\n", osd);
1240 static void put_osd(struct ceph_osd *osd)
1242 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1243 refcount_read(&osd->o_ref) - 1);
1244 if (refcount_dec_and_test(&osd->o_ref)) {
1250 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1252 static void __move_osd_to_lru(struct ceph_osd *osd)
1254 struct ceph_osd_client *osdc = osd->o_osdc;
1256 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1257 BUG_ON(!list_empty(&osd->o_osd_lru));
1259 spin_lock(&osdc->osd_lru_lock);
1260 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1261 spin_unlock(&osdc->osd_lru_lock);
1263 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1266 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1268 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1269 RB_EMPTY_ROOT(&osd->o_linger_requests))
1270 __move_osd_to_lru(osd);
1273 static void __remove_osd_from_lru(struct ceph_osd *osd)
1275 struct ceph_osd_client *osdc = osd->o_osdc;
1277 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1279 spin_lock(&osdc->osd_lru_lock);
1280 if (!list_empty(&osd->o_osd_lru))
1281 list_del_init(&osd->o_osd_lru);
1282 spin_unlock(&osdc->osd_lru_lock);
1286 * Close the connection and assign any leftover requests to the
1289 static void close_osd(struct ceph_osd *osd)
1291 struct ceph_osd_client *osdc = osd->o_osdc;
1294 verify_osdc_wrlocked(osdc);
1295 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1297 ceph_con_close(&osd->o_con);
1299 for (n = rb_first(&osd->o_requests); n; ) {
1300 struct ceph_osd_request *req =
1301 rb_entry(n, struct ceph_osd_request, r_node);
1303 n = rb_next(n); /* unlink_request() */
1305 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1306 unlink_request(osd, req);
1307 link_request(&osdc->homeless_osd, req);
1309 for (n = rb_first(&osd->o_linger_requests); n; ) {
1310 struct ceph_osd_linger_request *lreq =
1311 rb_entry(n, struct ceph_osd_linger_request, node);
1313 n = rb_next(n); /* unlink_linger() */
1315 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1317 unlink_linger(osd, lreq);
1318 link_linger(&osdc->homeless_osd, lreq);
1320 clear_backoffs(osd);
1322 __remove_osd_from_lru(osd);
1323 erase_osd(&osdc->osds, osd);
1330 static int reopen_osd(struct ceph_osd *osd)
1332 struct ceph_entity_addr *peer_addr;
1334 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1336 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1337 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1342 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1343 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1344 !ceph_con_opened(&osd->o_con)) {
1347 dout("osd addr hasn't changed and connection never opened, "
1348 "letting msgr retry\n");
1349 /* touch each r_stamp for handle_timeout()'s benfit */
1350 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1351 struct ceph_osd_request *req =
1352 rb_entry(n, struct ceph_osd_request, r_node);
1353 req->r_stamp = jiffies;
1359 ceph_con_close(&osd->o_con);
1360 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1361 osd->o_incarnation++;
1366 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1369 struct ceph_osd *osd;
1372 verify_osdc_wrlocked(osdc);
1374 verify_osdc_locked(osdc);
1376 if (o != CEPH_HOMELESS_OSD)
1377 osd = lookup_osd(&osdc->osds, o);
1379 osd = &osdc->homeless_osd;
1382 return ERR_PTR(-EAGAIN);
1384 osd = create_osd(osdc, o);
1385 insert_osd(&osdc->osds, osd);
1386 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1387 &osdc->osdmap->osd_addr[osd->o_osd]);
1390 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1395 * Create request <-> OSD session relation.
1397 * @req has to be assigned a tid, @osd may be homeless.
1399 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1401 verify_osd_locked(osd);
1402 WARN_ON(!req->r_tid || req->r_osd);
1403 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1406 if (!osd_homeless(osd))
1407 __remove_osd_from_lru(osd);
1409 atomic_inc(&osd->o_osdc->num_homeless);
1412 insert_request(&osd->o_requests, req);
1416 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1418 verify_osd_locked(osd);
1419 WARN_ON(req->r_osd != osd);
1420 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1424 erase_request(&osd->o_requests, req);
1427 if (!osd_homeless(osd))
1428 maybe_move_osd_to_lru(osd);
1430 atomic_dec(&osd->o_osdc->num_homeless);
1433 static bool __pool_full(struct ceph_pg_pool_info *pi)
1435 return pi->flags & CEPH_POOL_FLAG_FULL;
1438 static bool have_pool_full(struct ceph_osd_client *osdc)
1442 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1443 struct ceph_pg_pool_info *pi =
1444 rb_entry(n, struct ceph_pg_pool_info, node);
1446 if (__pool_full(pi))
1453 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1455 struct ceph_pg_pool_info *pi;
1457 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1461 return __pool_full(pi);
1465 * Returns whether a request should be blocked from being sent
1466 * based on the current osdmap and osd_client settings.
1468 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1469 const struct ceph_osd_request_target *t,
1470 struct ceph_pg_pool_info *pi)
1472 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1473 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1474 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1477 WARN_ON(pi->id != t->target_oloc.pool);
1478 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1479 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1480 (osdc->osdmap->epoch < osdc->epoch_barrier);
1483 enum calc_target_result {
1484 CALC_TARGET_NO_ACTION = 0,
1485 CALC_TARGET_NEED_RESEND,
1486 CALC_TARGET_POOL_DNE,
1489 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1490 struct ceph_osd_request_target *t,
1491 struct ceph_connection *con,
1494 struct ceph_pg_pool_info *pi;
1495 struct ceph_pg pgid, last_pgid;
1496 struct ceph_osds up, acting;
1497 bool force_resend = false;
1498 bool unpaused = false;
1501 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1502 bool recovery_deletes = ceph_osdmap_flag(osdc,
1503 CEPH_OSDMAP_RECOVERY_DELETES);
1504 enum calc_target_result ct_res;
1506 t->epoch = osdc->osdmap->epoch;
1507 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1509 t->osd = CEPH_HOMELESS_OSD;
1510 ct_res = CALC_TARGET_POOL_DNE;
1514 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1515 if (t->last_force_resend < pi->last_force_request_resend) {
1516 t->last_force_resend = pi->last_force_request_resend;
1517 force_resend = true;
1518 } else if (t->last_force_resend == 0) {
1519 force_resend = true;
1524 ceph_oid_copy(&t->target_oid, &t->base_oid);
1525 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1526 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1527 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1528 t->target_oloc.pool = pi->read_tier;
1529 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1530 t->target_oloc.pool = pi->write_tier;
1532 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1534 t->osd = CEPH_HOMELESS_OSD;
1535 ct_res = CALC_TARGET_POOL_DNE;
1540 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
1541 last_pgid.pool = pgid.pool;
1542 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1544 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1546 ceph_is_new_interval(&t->acting,
1558 t->recovery_deletes,
1561 force_resend = true;
1563 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1567 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1568 ceph_osds_changed(&t->acting, &acting, any_change);
1570 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1572 if (legacy_change || force_resend || split) {
1573 t->pgid = pgid; /* struct */
1574 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1575 ceph_osds_copy(&t->acting, &acting);
1576 ceph_osds_copy(&t->up, &up);
1578 t->min_size = pi->min_size;
1579 t->pg_num = pi->pg_num;
1580 t->pg_num_mask = pi->pg_num_mask;
1581 t->sort_bitwise = sort_bitwise;
1582 t->recovery_deletes = recovery_deletes;
1584 t->osd = acting.primary;
1587 if (unpaused || legacy_change || force_resend ||
1588 (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1590 ct_res = CALC_TARGET_NEED_RESEND;
1592 ct_res = CALC_TARGET_NO_ACTION;
1595 dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1599 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1601 struct ceph_spg_mapping *spg;
1603 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1607 RB_CLEAR_NODE(&spg->node);
1608 spg->backoffs = RB_ROOT;
1612 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1614 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1615 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1621 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1622 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1623 * defined only within a specific spgid; it does not pass anything to
1624 * children on split, or to another primary.
1626 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1627 RB_BYPTR, const struct ceph_spg *, node)
1629 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1631 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1634 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1635 void **pkey, size_t *pkey_len)
1637 if (hoid->key_len) {
1639 *pkey_len = hoid->key_len;
1642 *pkey_len = hoid->oid_len;
1646 static int compare_names(const void *name1, size_t name1_len,
1647 const void *name2, size_t name2_len)
1651 ret = memcmp(name1, name2, min(name1_len, name2_len));
1653 if (name1_len < name2_len)
1655 else if (name1_len > name2_len)
1661 static int hoid_compare(const struct ceph_hobject_id *lhs,
1662 const struct ceph_hobject_id *rhs)
1664 void *effective_key1, *effective_key2;
1665 size_t effective_key1_len, effective_key2_len;
1668 if (lhs->is_max < rhs->is_max)
1670 if (lhs->is_max > rhs->is_max)
1673 if (lhs->pool < rhs->pool)
1675 if (lhs->pool > rhs->pool)
1678 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1680 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1683 ret = compare_names(lhs->nspace, lhs->nspace_len,
1684 rhs->nspace, rhs->nspace_len);
1688 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1689 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1690 ret = compare_names(effective_key1, effective_key1_len,
1691 effective_key2, effective_key2_len);
1695 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1699 if (lhs->snapid < rhs->snapid)
1701 if (lhs->snapid > rhs->snapid)
1708 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1709 * compat stuff here.
1711 * Assumes @hoid is zero-initialized.
1713 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1719 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1725 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1729 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1731 if (IS_ERR(hoid->key)) {
1732 ret = PTR_ERR(hoid->key);
1737 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1739 if (IS_ERR(hoid->oid)) {
1740 ret = PTR_ERR(hoid->oid);
1745 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1746 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1747 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1749 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1751 if (IS_ERR(hoid->nspace)) {
1752 ret = PTR_ERR(hoid->nspace);
1753 hoid->nspace = NULL;
1757 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1759 ceph_hoid_build_hash_cache(hoid);
1766 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1768 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1769 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1772 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1774 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1775 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1776 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1777 ceph_encode_64(p, hoid->snapid);
1778 ceph_encode_32(p, hoid->hash);
1779 ceph_encode_8(p, hoid->is_max);
1780 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1781 ceph_encode_64(p, hoid->pool);
1784 static void free_hoid(struct ceph_hobject_id *hoid)
1789 kfree(hoid->nspace);
1794 static struct ceph_osd_backoff *alloc_backoff(void)
1796 struct ceph_osd_backoff *backoff;
1798 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1802 RB_CLEAR_NODE(&backoff->spg_node);
1803 RB_CLEAR_NODE(&backoff->id_node);
1807 static void free_backoff(struct ceph_osd_backoff *backoff)
1809 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1810 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1812 free_hoid(backoff->begin);
1813 free_hoid(backoff->end);
1818 * Within a specific spgid, backoffs are managed by ->begin hoid.
1820 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1821 RB_BYVAL, spg_node);
1823 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1824 const struct ceph_hobject_id *hoid)
1826 struct rb_node *n = root->rb_node;
1829 struct ceph_osd_backoff *cur =
1830 rb_entry(n, struct ceph_osd_backoff, spg_node);
1833 cmp = hoid_compare(hoid, cur->begin);
1836 } else if (cmp > 0) {
1837 if (hoid_compare(hoid, cur->end) < 0)
1850 * Each backoff has a unique id within its OSD session.
1852 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1854 static void clear_backoffs(struct ceph_osd *osd)
1856 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1857 struct ceph_spg_mapping *spg =
1858 rb_entry(rb_first(&osd->o_backoff_mappings),
1859 struct ceph_spg_mapping, node);
1861 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1862 struct ceph_osd_backoff *backoff =
1863 rb_entry(rb_first(&spg->backoffs),
1864 struct ceph_osd_backoff, spg_node);
1866 erase_backoff(&spg->backoffs, backoff);
1867 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1868 free_backoff(backoff);
1870 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1871 free_spg_mapping(spg);
1876 * Set up a temporary, non-owning view into @t.
1878 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1879 const struct ceph_osd_request_target *t)
1883 hoid->oid = t->target_oid.name;
1884 hoid->oid_len = t->target_oid.name_len;
1885 hoid->snapid = CEPH_NOSNAP;
1886 hoid->hash = t->pgid.seed;
1887 hoid->is_max = false;
1888 if (t->target_oloc.pool_ns) {
1889 hoid->nspace = t->target_oloc.pool_ns->str;
1890 hoid->nspace_len = t->target_oloc.pool_ns->len;
1892 hoid->nspace = NULL;
1893 hoid->nspace_len = 0;
1895 hoid->pool = t->target_oloc.pool;
1896 ceph_hoid_build_hash_cache(hoid);
1899 static bool should_plug_request(struct ceph_osd_request *req)
1901 struct ceph_osd *osd = req->r_osd;
1902 struct ceph_spg_mapping *spg;
1903 struct ceph_osd_backoff *backoff;
1904 struct ceph_hobject_id hoid;
1906 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1910 hoid_fill_from_target(&hoid, &req->r_t);
1911 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1915 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1916 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1917 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1922 * Keep get_num_data_items() in sync with this function.
1924 static void setup_request_data(struct ceph_osd_request *req)
1926 struct ceph_msg *request_msg = req->r_request;
1927 struct ceph_msg *reply_msg = req->r_reply;
1928 struct ceph_osd_req_op *op;
1930 if (req->r_request->num_data_items || req->r_reply->num_data_items)
1933 WARN_ON(request_msg->data_length || reply_msg->data_length);
1934 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
1937 case CEPH_OSD_OP_WRITE:
1938 case CEPH_OSD_OP_WRITEFULL:
1939 WARN_ON(op->indata_len != op->extent.length);
1940 ceph_osdc_msg_data_add(request_msg,
1941 &op->extent.osd_data);
1943 case CEPH_OSD_OP_SETXATTR:
1944 case CEPH_OSD_OP_CMPXATTR:
1945 WARN_ON(op->indata_len != op->xattr.name_len +
1946 op->xattr.value_len);
1947 ceph_osdc_msg_data_add(request_msg,
1948 &op->xattr.osd_data);
1950 case CEPH_OSD_OP_NOTIFY_ACK:
1951 ceph_osdc_msg_data_add(request_msg,
1952 &op->notify_ack.request_data);
1954 case CEPH_OSD_OP_COPY_FROM:
1955 ceph_osdc_msg_data_add(request_msg,
1956 &op->copy_from.osd_data);
1960 case CEPH_OSD_OP_STAT:
1961 ceph_osdc_msg_data_add(reply_msg,
1964 case CEPH_OSD_OP_READ:
1965 ceph_osdc_msg_data_add(reply_msg,
1966 &op->extent.osd_data);
1968 case CEPH_OSD_OP_LIST_WATCHERS:
1969 ceph_osdc_msg_data_add(reply_msg,
1970 &op->list_watchers.response_data);
1974 case CEPH_OSD_OP_CALL:
1975 WARN_ON(op->indata_len != op->cls.class_len +
1976 op->cls.method_len +
1977 op->cls.indata_len);
1978 ceph_osdc_msg_data_add(request_msg,
1979 &op->cls.request_info);
1980 /* optional, can be NONE */
1981 ceph_osdc_msg_data_add(request_msg,
1982 &op->cls.request_data);
1983 /* optional, can be NONE */
1984 ceph_osdc_msg_data_add(reply_msg,
1985 &op->cls.response_data);
1987 case CEPH_OSD_OP_NOTIFY:
1988 ceph_osdc_msg_data_add(request_msg,
1989 &op->notify.request_data);
1990 ceph_osdc_msg_data_add(reply_msg,
1991 &op->notify.response_data);
1997 static void encode_pgid(void **p, const struct ceph_pg *pgid)
1999 ceph_encode_8(p, 1);
2000 ceph_encode_64(p, pgid->pool);
2001 ceph_encode_32(p, pgid->seed);
2002 ceph_encode_32(p, -1); /* preferred */
2005 static void encode_spgid(void **p, const struct ceph_spg *spgid)
2007 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
2008 encode_pgid(p, &spgid->pgid);
2009 ceph_encode_8(p, spgid->shard);
2012 static void encode_oloc(void **p, void *end,
2013 const struct ceph_object_locator *oloc)
2015 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
2016 ceph_encode_64(p, oloc->pool);
2017 ceph_encode_32(p, -1); /* preferred */
2018 ceph_encode_32(p, 0); /* key len */
2020 ceph_encode_string(p, end, oloc->pool_ns->str,
2021 oloc->pool_ns->len);
2023 ceph_encode_32(p, 0);
2026 static void encode_request_partial(struct ceph_osd_request *req,
2027 struct ceph_msg *msg)
2029 void *p = msg->front.iov_base;
2030 void *const end = p + msg->front_alloc_len;
2034 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2035 /* snapshots aren't writeable */
2036 WARN_ON(req->r_snapid != CEPH_NOSNAP);
2038 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2039 req->r_data_offset || req->r_snapc);
2042 setup_request_data(req);
2044 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
2045 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2046 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2047 ceph_encode_32(&p, req->r_flags);
2050 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
2051 memset(p, 0, sizeof(struct ceph_osd_reqid));
2052 p += sizeof(struct ceph_osd_reqid);
2055 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
2056 p += sizeof(struct ceph_blkin_trace_info);
2058 ceph_encode_32(&p, 0); /* client_inc, always 0 */
2059 ceph_encode_timespec64(p, &req->r_mtime);
2060 p += sizeof(struct ceph_timespec);
2062 encode_oloc(&p, end, &req->r_t.target_oloc);
2063 ceph_encode_string(&p, end, req->r_t.target_oid.name,
2064 req->r_t.target_oid.name_len);
2066 /* ops, can imply data */
2067 ceph_encode_16(&p, req->r_num_ops);
2068 for (i = 0; i < req->r_num_ops; i++) {
2069 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2070 p += sizeof(struct ceph_osd_op);
2073 ceph_encode_64(&p, req->r_snapid); /* snapid */
2075 ceph_encode_64(&p, req->r_snapc->seq);
2076 ceph_encode_32(&p, req->r_snapc->num_snaps);
2077 for (i = 0; i < req->r_snapc->num_snaps; i++)
2078 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2080 ceph_encode_64(&p, 0); /* snap_seq */
2081 ceph_encode_32(&p, 0); /* snaps len */
2084 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2085 BUG_ON(p > end - 8); /* space for features */
2087 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2088 /* front_len is finalized in encode_request_finish() */
2089 msg->front.iov_len = p - msg->front.iov_base;
2090 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2091 msg->hdr.data_len = cpu_to_le32(data_len);
2093 * The header "data_off" is a hint to the receiver allowing it
2094 * to align received data into its buffers such that there's no
2095 * need to re-copy it before writing it to disk (direct I/O).
2097 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2099 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2100 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2103 static void encode_request_finish(struct ceph_msg *msg)
2105 void *p = msg->front.iov_base;
2106 void *const partial_end = p + msg->front.iov_len;
2107 void *const end = p + msg->front_alloc_len;
2109 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2110 /* luminous OSD -- encode features and be done */
2112 ceph_encode_64(&p, msg->con->peer_features);
2115 char spgid[CEPH_ENCODING_START_BLK_LEN +
2116 CEPH_PGID_ENCODING_LEN + 1];
2120 char reqid[CEPH_ENCODING_START_BLK_LEN +
2121 sizeof(struct ceph_osd_reqid)];
2122 char trace[sizeof(struct ceph_blkin_trace_info)];
2124 struct ceph_timespec mtime;
2126 struct ceph_pg pgid;
2127 void *oloc, *oid, *tail;
2128 int oloc_len, oid_len, tail_len;
2132 * Pre-luminous OSD -- reencode v8 into v4 using @head
2133 * as a temporary buffer. Encode the raw PG; the rest
2134 * is just a matter of moving oloc, oid and tail blobs
2137 memcpy(&head, p, sizeof(head));
2141 p += CEPH_ENCODING_START_BLK_LEN;
2142 pgid.pool = ceph_decode_64(&p);
2143 p += 4 + 4; /* preferred, key len */
2144 len = ceph_decode_32(&p);
2145 p += len; /* nspace */
2146 oloc_len = p - oloc;
2149 len = ceph_decode_32(&p);
2154 tail_len = partial_end - p;
2156 p = msg->front.iov_base;
2157 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2158 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2159 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2160 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2162 /* reassert_version */
2163 memset(p, 0, sizeof(struct ceph_eversion));
2164 p += sizeof(struct ceph_eversion);
2167 memmove(p, oloc, oloc_len);
2170 pgid.seed = le32_to_cpu(head.hash);
2171 encode_pgid(&p, &pgid); /* raw pg */
2174 memmove(p, oid, oid_len);
2177 /* tail -- ops, snapid, snapc, retry_attempt */
2179 memmove(p, tail, tail_len);
2182 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2186 msg->front.iov_len = p - msg->front.iov_base;
2187 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2189 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2190 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2191 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2192 le16_to_cpu(msg->hdr.version));
2196 * @req has to be assigned a tid and registered.
2198 static void send_request(struct ceph_osd_request *req)
2200 struct ceph_osd *osd = req->r_osd;
2202 verify_osd_locked(osd);
2203 WARN_ON(osd->o_osd != req->r_t.osd);
2206 if (should_plug_request(req))
2210 * We may have a previously queued request message hanging
2211 * around. Cancel it to avoid corrupting the msgr.
2214 ceph_msg_revoke(req->r_request);
2216 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2217 if (req->r_attempts)
2218 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2220 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2222 encode_request_partial(req, req->r_request);
2224 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2225 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2226 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2227 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2230 req->r_t.paused = false;
2231 req->r_stamp = jiffies;
2234 req->r_sent = osd->o_incarnation;
2235 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2236 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2239 static void maybe_request_map(struct ceph_osd_client *osdc)
2241 bool continuous = false;
2243 verify_osdc_locked(osdc);
2244 WARN_ON(!osdc->osdmap->epoch);
2246 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2247 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2248 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2249 dout("%s osdc %p continuous\n", __func__, osdc);
2252 dout("%s osdc %p onetime\n", __func__, osdc);
2255 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2256 osdc->osdmap->epoch + 1, continuous))
2257 ceph_monc_renew_subs(&osdc->client->monc);
2260 static void complete_request(struct ceph_osd_request *req, int err);
2261 static void send_map_check(struct ceph_osd_request *req);
2263 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2265 struct ceph_osd_client *osdc = req->r_osdc;
2266 struct ceph_osd *osd;
2267 enum calc_target_result ct_res;
2269 bool need_send = false;
2270 bool promoted = false;
2272 WARN_ON(req->r_tid);
2273 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2276 ct_res = calc_target(osdc, &req->r_t, NULL, false);
2277 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2280 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2282 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2286 if (osdc->abort_err) {
2287 dout("req %p abort_err %d\n", req, osdc->abort_err);
2288 err = osdc->abort_err;
2289 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2290 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2291 osdc->epoch_barrier);
2292 req->r_t.paused = true;
2293 maybe_request_map(osdc);
2294 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2295 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2296 dout("req %p pausewr\n", req);
2297 req->r_t.paused = true;
2298 maybe_request_map(osdc);
2299 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2300 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2301 dout("req %p pauserd\n", req);
2302 req->r_t.paused = true;
2303 maybe_request_map(osdc);
2304 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2305 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2306 CEPH_OSD_FLAG_FULL_FORCE)) &&
2307 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2308 pool_full(osdc, req->r_t.base_oloc.pool))) {
2309 dout("req %p full/pool_full\n", req);
2310 if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
2313 pr_warn_ratelimited("FULL or reached pool quota\n");
2314 req->r_t.paused = true;
2315 maybe_request_map(osdc);
2317 } else if (!osd_homeless(osd)) {
2320 maybe_request_map(osdc);
2323 mutex_lock(&osd->lock);
2325 * Assign the tid atomically with send_request() to protect
2326 * multiple writes to the same object from racing with each
2327 * other, resulting in out of order ops on the OSDs.
2329 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2330 link_request(osd, req);
2334 complete_request(req, err);
2335 mutex_unlock(&osd->lock);
2337 if (!err && ct_res == CALC_TARGET_POOL_DNE)
2338 send_map_check(req);
2341 downgrade_write(&osdc->lock);
2345 up_read(&osdc->lock);
2346 down_write(&osdc->lock);
2352 static void account_request(struct ceph_osd_request *req)
2354 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2355 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2357 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2358 atomic_inc(&req->r_osdc->num_requests);
2360 req->r_start_stamp = jiffies;
2363 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2365 ceph_osdc_get_request(req);
2366 account_request(req);
2367 __submit_request(req, wrlocked);
2370 static void finish_request(struct ceph_osd_request *req)
2372 struct ceph_osd_client *osdc = req->r_osdc;
2374 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2375 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2378 unlink_request(req->r_osd, req);
2379 atomic_dec(&osdc->num_requests);
2382 * If an OSD has failed or returned and a request has been sent
2383 * twice, it's possible to get a reply and end up here while the
2384 * request message is queued for delivery. We will ignore the
2385 * reply, so not a big deal, but better to try and catch it.
2387 ceph_msg_revoke(req->r_request);
2388 ceph_msg_revoke_incoming(req->r_reply);
2391 static void __complete_request(struct ceph_osd_request *req)
2393 dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2394 req->r_tid, req->r_callback, req->r_result);
2396 if (req->r_callback)
2397 req->r_callback(req);
2398 complete_all(&req->r_completion);
2399 ceph_osdc_put_request(req);
2402 static void complete_request_workfn(struct work_struct *work)
2404 struct ceph_osd_request *req =
2405 container_of(work, struct ceph_osd_request, r_complete_work);
2407 __complete_request(req);
2411 * This is open-coded in handle_reply().
2413 static void complete_request(struct ceph_osd_request *req, int err)
2415 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2417 req->r_result = err;
2418 finish_request(req);
2420 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2421 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
2424 static void cancel_map_check(struct ceph_osd_request *req)
2426 struct ceph_osd_client *osdc = req->r_osdc;
2427 struct ceph_osd_request *lookup_req;
2429 verify_osdc_wrlocked(osdc);
2431 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2435 WARN_ON(lookup_req != req);
2436 erase_request_mc(&osdc->map_checks, req);
2437 ceph_osdc_put_request(req);
2440 static void cancel_request(struct ceph_osd_request *req)
2442 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2444 cancel_map_check(req);
2445 finish_request(req);
2446 complete_all(&req->r_completion);
2447 ceph_osdc_put_request(req);
2450 static void abort_request(struct ceph_osd_request *req, int err)
2452 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2454 cancel_map_check(req);
2455 complete_request(req, err);
2458 static int abort_fn(struct ceph_osd_request *req, void *arg)
2460 int err = *(int *)arg;
2462 abort_request(req, err);
2463 return 0; /* continue iteration */
2467 * Abort all in-flight requests with @err and arrange for all future
2468 * requests to be failed immediately.
2470 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2472 dout("%s osdc %p err %d\n", __func__, osdc, err);
2473 down_write(&osdc->lock);
2474 for_each_request(osdc, abort_fn, &err);
2475 osdc->abort_err = err;
2476 up_write(&osdc->lock);
2478 EXPORT_SYMBOL(ceph_osdc_abort_requests);
2480 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2482 if (likely(eb > osdc->epoch_barrier)) {
2483 dout("updating epoch_barrier from %u to %u\n",
2484 osdc->epoch_barrier, eb);
2485 osdc->epoch_barrier = eb;
2486 /* Request map if we're not to the barrier yet */
2487 if (eb > osdc->osdmap->epoch)
2488 maybe_request_map(osdc);
2492 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2494 down_read(&osdc->lock);
2495 if (unlikely(eb > osdc->epoch_barrier)) {
2496 up_read(&osdc->lock);
2497 down_write(&osdc->lock);
2498 update_epoch_barrier(osdc, eb);
2499 up_write(&osdc->lock);
2501 up_read(&osdc->lock);
2504 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2507 * We can end up releasing caps as a result of abort_request().
2508 * In that case, we probably want to ensure that the cap release message
2509 * has an updated epoch barrier in it, so set the epoch barrier prior to
2510 * aborting the first request.
2512 static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2514 struct ceph_osd_client *osdc = req->r_osdc;
2515 bool *victims = arg;
2517 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2518 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2519 pool_full(osdc, req->r_t.base_oloc.pool))) {
2521 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2524 abort_request(req, -ENOSPC);
2527 return 0; /* continue iteration */
2531 * Drop all pending requests that are stalled waiting on a full condition to
2532 * clear, and complete them with ENOSPC as the return code. Set the
2533 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2536 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2538 bool victims = false;
2540 if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2541 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
2542 for_each_request(osdc, abort_on_full_fn, &victims);
2545 static void check_pool_dne(struct ceph_osd_request *req)
2547 struct ceph_osd_client *osdc = req->r_osdc;
2548 struct ceph_osdmap *map = osdc->osdmap;
2550 verify_osdc_wrlocked(osdc);
2551 WARN_ON(!map->epoch);
2553 if (req->r_attempts) {
2555 * We sent a request earlier, which means that
2556 * previously the pool existed, and now it does not
2557 * (i.e., it was deleted).
2559 req->r_map_dne_bound = map->epoch;
2560 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2563 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2564 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2567 if (req->r_map_dne_bound) {
2568 if (map->epoch >= req->r_map_dne_bound) {
2569 /* we had a new enough map */
2570 pr_info_ratelimited("tid %llu pool does not exist\n",
2572 complete_request(req, -ENOENT);
2575 send_map_check(req);
2579 static void map_check_cb(struct ceph_mon_generic_request *greq)
2581 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2582 struct ceph_osd_request *req;
2583 u64 tid = greq->private_data;
2585 WARN_ON(greq->result || !greq->u.newest);
2587 down_write(&osdc->lock);
2588 req = lookup_request_mc(&osdc->map_checks, tid);
2590 dout("%s tid %llu dne\n", __func__, tid);
2594 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2595 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2596 if (!req->r_map_dne_bound)
2597 req->r_map_dne_bound = greq->u.newest;
2598 erase_request_mc(&osdc->map_checks, req);
2599 check_pool_dne(req);
2601 ceph_osdc_put_request(req);
2603 up_write(&osdc->lock);
2606 static void send_map_check(struct ceph_osd_request *req)
2608 struct ceph_osd_client *osdc = req->r_osdc;
2609 struct ceph_osd_request *lookup_req;
2612 verify_osdc_wrlocked(osdc);
2614 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2616 WARN_ON(lookup_req != req);
2620 ceph_osdc_get_request(req);
2621 insert_request_mc(&osdc->map_checks, req);
2622 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2623 map_check_cb, req->r_tid);
2628 * lingering requests, watch/notify v2 infrastructure
2630 static void linger_release(struct kref *kref)
2632 struct ceph_osd_linger_request *lreq =
2633 container_of(kref, struct ceph_osd_linger_request, kref);
2635 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2636 lreq->reg_req, lreq->ping_req);
2637 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2638 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2639 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2640 WARN_ON(!list_empty(&lreq->scan_item));
2641 WARN_ON(!list_empty(&lreq->pending_lworks));
2645 ceph_osdc_put_request(lreq->reg_req);
2647 ceph_osdc_put_request(lreq->ping_req);
2648 target_destroy(&lreq->t);
2652 static void linger_put(struct ceph_osd_linger_request *lreq)
2655 kref_put(&lreq->kref, linger_release);
2658 static struct ceph_osd_linger_request *
2659 linger_get(struct ceph_osd_linger_request *lreq)
2661 kref_get(&lreq->kref);
2665 static struct ceph_osd_linger_request *
2666 linger_alloc(struct ceph_osd_client *osdc)
2668 struct ceph_osd_linger_request *lreq;
2670 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2674 kref_init(&lreq->kref);
2675 mutex_init(&lreq->lock);
2676 RB_CLEAR_NODE(&lreq->node);
2677 RB_CLEAR_NODE(&lreq->osdc_node);
2678 RB_CLEAR_NODE(&lreq->mc_node);
2679 INIT_LIST_HEAD(&lreq->scan_item);
2680 INIT_LIST_HEAD(&lreq->pending_lworks);
2681 init_completion(&lreq->reg_commit_wait);
2682 init_completion(&lreq->notify_finish_wait);
2685 target_init(&lreq->t);
2687 dout("%s lreq %p\n", __func__, lreq);
2691 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2692 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2693 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2696 * Create linger request <-> OSD session relation.
2698 * @lreq has to be registered, @osd may be homeless.
2700 static void link_linger(struct ceph_osd *osd,
2701 struct ceph_osd_linger_request *lreq)
2703 verify_osd_locked(osd);
2704 WARN_ON(!lreq->linger_id || lreq->osd);
2705 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2706 osd->o_osd, lreq, lreq->linger_id);
2708 if (!osd_homeless(osd))
2709 __remove_osd_from_lru(osd);
2711 atomic_inc(&osd->o_osdc->num_homeless);
2714 insert_linger(&osd->o_linger_requests, lreq);
2718 static void unlink_linger(struct ceph_osd *osd,
2719 struct ceph_osd_linger_request *lreq)
2721 verify_osd_locked(osd);
2722 WARN_ON(lreq->osd != osd);
2723 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2724 osd->o_osd, lreq, lreq->linger_id);
2727 erase_linger(&osd->o_linger_requests, lreq);
2730 if (!osd_homeless(osd))
2731 maybe_move_osd_to_lru(osd);
2733 atomic_dec(&osd->o_osdc->num_homeless);
2736 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2738 verify_osdc_locked(lreq->osdc);
2740 return !RB_EMPTY_NODE(&lreq->osdc_node);
2743 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2745 struct ceph_osd_client *osdc = lreq->osdc;
2748 down_read(&osdc->lock);
2749 registered = __linger_registered(lreq);
2750 up_read(&osdc->lock);
2755 static void linger_register(struct ceph_osd_linger_request *lreq)
2757 struct ceph_osd_client *osdc = lreq->osdc;
2759 verify_osdc_wrlocked(osdc);
2760 WARN_ON(lreq->linger_id);
2763 lreq->linger_id = ++osdc->last_linger_id;
2764 insert_linger_osdc(&osdc->linger_requests, lreq);
2767 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2769 struct ceph_osd_client *osdc = lreq->osdc;
2771 verify_osdc_wrlocked(osdc);
2773 erase_linger_osdc(&osdc->linger_requests, lreq);
2777 static void cancel_linger_request(struct ceph_osd_request *req)
2779 struct ceph_osd_linger_request *lreq = req->r_priv;
2781 WARN_ON(!req->r_linger);
2782 cancel_request(req);
2786 struct linger_work {
2787 struct work_struct work;
2788 struct ceph_osd_linger_request *lreq;
2789 struct list_head pending_item;
2790 unsigned long queued_stamp;
2796 void *payload; /* points into @msg front */
2799 struct ceph_msg *msg; /* for ceph_msg_put() */
2807 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2810 struct linger_work *lwork;
2812 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2816 INIT_WORK(&lwork->work, workfn);
2817 INIT_LIST_HEAD(&lwork->pending_item);
2818 lwork->lreq = linger_get(lreq);
2823 static void lwork_free(struct linger_work *lwork)
2825 struct ceph_osd_linger_request *lreq = lwork->lreq;
2827 mutex_lock(&lreq->lock);
2828 list_del(&lwork->pending_item);
2829 mutex_unlock(&lreq->lock);
2835 static void lwork_queue(struct linger_work *lwork)
2837 struct ceph_osd_linger_request *lreq = lwork->lreq;
2838 struct ceph_osd_client *osdc = lreq->osdc;
2840 verify_lreq_locked(lreq);
2841 WARN_ON(!list_empty(&lwork->pending_item));
2843 lwork->queued_stamp = jiffies;
2844 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2845 queue_work(osdc->notify_wq, &lwork->work);
2848 static void do_watch_notify(struct work_struct *w)
2850 struct linger_work *lwork = container_of(w, struct linger_work, work);
2851 struct ceph_osd_linger_request *lreq = lwork->lreq;
2853 if (!linger_registered(lreq)) {
2854 dout("%s lreq %p not registered\n", __func__, lreq);
2858 WARN_ON(!lreq->is_watch);
2859 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2860 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2861 lwork->notify.payload_len);
2862 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2863 lwork->notify.notifier_id, lwork->notify.payload,
2864 lwork->notify.payload_len);
2867 ceph_msg_put(lwork->notify.msg);
2871 static void do_watch_error(struct work_struct *w)
2873 struct linger_work *lwork = container_of(w, struct linger_work, work);
2874 struct ceph_osd_linger_request *lreq = lwork->lreq;
2876 if (!linger_registered(lreq)) {
2877 dout("%s lreq %p not registered\n", __func__, lreq);
2881 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2882 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2888 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2890 struct linger_work *lwork;
2892 lwork = lwork_alloc(lreq, do_watch_error);
2894 pr_err("failed to allocate error-lwork\n");
2898 lwork->error.err = lreq->last_error;
2902 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2905 if (!completion_done(&lreq->reg_commit_wait)) {
2906 lreq->reg_commit_error = (result <= 0 ? result : 0);
2907 complete_all(&lreq->reg_commit_wait);
2911 static void linger_commit_cb(struct ceph_osd_request *req)
2913 struct ceph_osd_linger_request *lreq = req->r_priv;
2915 mutex_lock(&lreq->lock);
2916 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2917 lreq->linger_id, req->r_result);
2918 linger_reg_commit_complete(lreq, req->r_result);
2919 lreq->committed = true;
2921 if (!lreq->is_watch) {
2922 struct ceph_osd_data *osd_data =
2923 osd_req_op_data(req, 0, notify, response_data);
2924 void *p = page_address(osd_data->pages[0]);
2926 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2927 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2929 /* make note of the notify_id */
2930 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2931 lreq->notify_id = ceph_decode_64(&p);
2932 dout("lreq %p notify_id %llu\n", lreq,
2935 dout("lreq %p no notify_id\n", lreq);
2939 mutex_unlock(&lreq->lock);
2943 static int normalize_watch_error(int err)
2946 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2947 * notification and a failure to reconnect because we raced with
2948 * the delete appear the same to the user.
2956 static void linger_reconnect_cb(struct ceph_osd_request *req)
2958 struct ceph_osd_linger_request *lreq = req->r_priv;
2960 mutex_lock(&lreq->lock);
2961 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2962 lreq, lreq->linger_id, req->r_result, lreq->last_error);
2963 if (req->r_result < 0) {
2964 if (!lreq->last_error) {
2965 lreq->last_error = normalize_watch_error(req->r_result);
2966 queue_watch_error(lreq);
2970 mutex_unlock(&lreq->lock);
2974 static void send_linger(struct ceph_osd_linger_request *lreq)
2976 struct ceph_osd_request *req = lreq->reg_req;
2977 struct ceph_osd_req_op *op = &req->r_ops[0];
2979 verify_osdc_wrlocked(req->r_osdc);
2980 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2983 cancel_linger_request(req);
2985 request_reinit(req);
2986 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2987 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2988 req->r_flags = lreq->t.flags;
2989 req->r_mtime = lreq->mtime;
2991 mutex_lock(&lreq->lock);
2992 if (lreq->is_watch && lreq->committed) {
2993 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2994 op->watch.cookie != lreq->linger_id);
2995 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2996 op->watch.gen = ++lreq->register_gen;
2997 dout("lreq %p reconnect register_gen %u\n", lreq,
2999 req->r_callback = linger_reconnect_cb;
3001 if (!lreq->is_watch)
3002 lreq->notify_id = 0;
3004 WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
3005 dout("lreq %p register\n", lreq);
3006 req->r_callback = linger_commit_cb;
3008 mutex_unlock(&lreq->lock);
3010 req->r_priv = linger_get(lreq);
3011 req->r_linger = true;
3013 submit_request(req, true);
3016 static void linger_ping_cb(struct ceph_osd_request *req)
3018 struct ceph_osd_linger_request *lreq = req->r_priv;
3020 mutex_lock(&lreq->lock);
3021 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3022 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3024 if (lreq->register_gen == req->r_ops[0].watch.gen) {
3025 if (!req->r_result) {
3026 lreq->watch_valid_thru = lreq->ping_sent;
3027 } else if (!lreq->last_error) {
3028 lreq->last_error = normalize_watch_error(req->r_result);
3029 queue_watch_error(lreq);
3032 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3033 lreq->register_gen, req->r_ops[0].watch.gen);
3036 mutex_unlock(&lreq->lock);
3040 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3042 struct ceph_osd_client *osdc = lreq->osdc;
3043 struct ceph_osd_request *req = lreq->ping_req;
3044 struct ceph_osd_req_op *op = &req->r_ops[0];
3046 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3047 dout("%s PAUSERD\n", __func__);
3051 lreq->ping_sent = jiffies;
3052 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3053 __func__, lreq, lreq->linger_id, lreq->ping_sent,
3054 lreq->register_gen);
3057 cancel_linger_request(req);
3059 request_reinit(req);
3060 target_copy(&req->r_t, &lreq->t);
3062 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3063 op->watch.cookie != lreq->linger_id ||
3064 op->watch.op != CEPH_OSD_WATCH_OP_PING);
3065 op->watch.gen = lreq->register_gen;
3066 req->r_callback = linger_ping_cb;
3067 req->r_priv = linger_get(lreq);
3068 req->r_linger = true;
3070 ceph_osdc_get_request(req);
3071 account_request(req);
3072 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3073 link_request(lreq->osd, req);
3077 static void linger_submit(struct ceph_osd_linger_request *lreq)
3079 struct ceph_osd_client *osdc = lreq->osdc;
3080 struct ceph_osd *osd;
3082 down_write(&osdc->lock);
3083 linger_register(lreq);
3084 if (lreq->is_watch) {
3085 lreq->reg_req->r_ops[0].watch.cookie = lreq->linger_id;
3086 lreq->ping_req->r_ops[0].watch.cookie = lreq->linger_id;
3088 lreq->reg_req->r_ops[0].notify.cookie = lreq->linger_id;
3091 calc_target(osdc, &lreq->t, NULL, false);
3092 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3093 link_linger(osd, lreq);
3096 up_write(&osdc->lock);
3099 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3101 struct ceph_osd_client *osdc = lreq->osdc;
3102 struct ceph_osd_linger_request *lookup_lreq;
3104 verify_osdc_wrlocked(osdc);
3106 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3111 WARN_ON(lookup_lreq != lreq);
3112 erase_linger_mc(&osdc->linger_map_checks, lreq);
3117 * @lreq has to be both registered and linked.
3119 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3121 if (lreq->is_watch && lreq->ping_req->r_osd)
3122 cancel_linger_request(lreq->ping_req);
3123 if (lreq->reg_req->r_osd)
3124 cancel_linger_request(lreq->reg_req);
3125 cancel_linger_map_check(lreq);
3126 unlink_linger(lreq->osd, lreq);
3127 linger_unregister(lreq);
3130 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3132 struct ceph_osd_client *osdc = lreq->osdc;
3134 down_write(&osdc->lock);
3135 if (__linger_registered(lreq))
3136 __linger_cancel(lreq);
3137 up_write(&osdc->lock);
3140 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3142 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3144 struct ceph_osd_client *osdc = lreq->osdc;
3145 struct ceph_osdmap *map = osdc->osdmap;
3147 verify_osdc_wrlocked(osdc);
3148 WARN_ON(!map->epoch);
3150 if (lreq->register_gen) {
3151 lreq->map_dne_bound = map->epoch;
3152 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3153 lreq, lreq->linger_id);
3155 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3156 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3160 if (lreq->map_dne_bound) {
3161 if (map->epoch >= lreq->map_dne_bound) {
3162 /* we had a new enough map */
3163 pr_info("linger_id %llu pool does not exist\n",
3165 linger_reg_commit_complete(lreq, -ENOENT);
3166 __linger_cancel(lreq);
3169 send_linger_map_check(lreq);
3173 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3175 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3176 struct ceph_osd_linger_request *lreq;
3177 u64 linger_id = greq->private_data;
3179 WARN_ON(greq->result || !greq->u.newest);
3181 down_write(&osdc->lock);
3182 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3184 dout("%s linger_id %llu dne\n", __func__, linger_id);
3188 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3189 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3191 if (!lreq->map_dne_bound)
3192 lreq->map_dne_bound = greq->u.newest;
3193 erase_linger_mc(&osdc->linger_map_checks, lreq);
3194 check_linger_pool_dne(lreq);
3198 up_write(&osdc->lock);
3201 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3203 struct ceph_osd_client *osdc = lreq->osdc;
3204 struct ceph_osd_linger_request *lookup_lreq;
3207 verify_osdc_wrlocked(osdc);
3209 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3212 WARN_ON(lookup_lreq != lreq);
3217 insert_linger_mc(&osdc->linger_map_checks, lreq);
3218 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3219 linger_map_check_cb, lreq->linger_id);
3223 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3227 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3228 ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3229 return ret ?: lreq->reg_commit_error;
3232 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3236 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3237 ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3238 return ret ?: lreq->notify_finish_error;
3242 * Timeout callback, called every N seconds. When 1 or more OSD
3243 * requests has been active for more than N seconds, we send a keepalive
3244 * (tag + timestamp) to its OSD to ensure any communications channel
3245 * reset is detected.
3247 static void handle_timeout(struct work_struct *work)
3249 struct ceph_osd_client *osdc =
3250 container_of(work, struct ceph_osd_client, timeout_work.work);
3251 struct ceph_options *opts = osdc->client->options;
3252 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3253 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3254 LIST_HEAD(slow_osds);
3255 struct rb_node *n, *p;
3257 dout("%s osdc %p\n", __func__, osdc);
3258 down_write(&osdc->lock);
3261 * ping osds that are a bit slow. this ensures that if there
3262 * is a break in the TCP connection we will notice, and reopen
3263 * a connection with that osd (from the fault callback).
3265 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3266 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3269 for (p = rb_first(&osd->o_requests); p; ) {
3270 struct ceph_osd_request *req =
3271 rb_entry(p, struct ceph_osd_request, r_node);
3273 p = rb_next(p); /* abort_request() */
3275 if (time_before(req->r_stamp, cutoff)) {
3276 dout(" req %p tid %llu on osd%d is laggy\n",
3277 req, req->r_tid, osd->o_osd);
3280 if (opts->osd_request_timeout &&
3281 time_before(req->r_start_stamp, expiry_cutoff)) {
3282 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3283 req->r_tid, osd->o_osd);
3284 abort_request(req, -ETIMEDOUT);
3287 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3288 struct ceph_osd_linger_request *lreq =
3289 rb_entry(p, struct ceph_osd_linger_request, node);
3291 dout(" lreq %p linger_id %llu is served by osd%d\n",
3292 lreq, lreq->linger_id, osd->o_osd);
3295 mutex_lock(&lreq->lock);
3296 if (lreq->is_watch && lreq->committed && !lreq->last_error)
3297 send_linger_ping(lreq);
3298 mutex_unlock(&lreq->lock);
3302 list_move_tail(&osd->o_keepalive_item, &slow_osds);
3305 if (opts->osd_request_timeout) {
3306 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3307 struct ceph_osd_request *req =
3308 rb_entry(p, struct ceph_osd_request, r_node);
3310 p = rb_next(p); /* abort_request() */
3312 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3313 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3314 req->r_tid, osdc->homeless_osd.o_osd);
3315 abort_request(req, -ETIMEDOUT);
3320 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3321 maybe_request_map(osdc);
3323 while (!list_empty(&slow_osds)) {
3324 struct ceph_osd *osd = list_first_entry(&slow_osds,
3327 list_del_init(&osd->o_keepalive_item);
3328 ceph_con_keepalive(&osd->o_con);
3331 up_write(&osdc->lock);
3332 schedule_delayed_work(&osdc->timeout_work,
3333 osdc->client->options->osd_keepalive_timeout);
3336 static void handle_osds_timeout(struct work_struct *work)
3338 struct ceph_osd_client *osdc =
3339 container_of(work, struct ceph_osd_client,
3340 osds_timeout_work.work);
3341 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3342 struct ceph_osd *osd, *nosd;
3344 dout("%s osdc %p\n", __func__, osdc);
3345 down_write(&osdc->lock);
3346 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3347 if (time_before(jiffies, osd->lru_ttl))
3350 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3351 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3355 up_write(&osdc->lock);
3356 schedule_delayed_work(&osdc->osds_timeout_work,
3357 round_jiffies_relative(delay));
3360 static int ceph_oloc_decode(void **p, void *end,
3361 struct ceph_object_locator *oloc)
3363 u8 struct_v, struct_cv;
3368 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3369 struct_v = ceph_decode_8(p);
3370 struct_cv = ceph_decode_8(p);
3372 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3373 struct_v, struct_cv);
3376 if (struct_cv > 6) {
3377 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3378 struct_v, struct_cv);
3381 len = ceph_decode_32(p);
3382 ceph_decode_need(p, end, len, e_inval);
3383 struct_end = *p + len;
3385 oloc->pool = ceph_decode_64(p);
3386 *p += 4; /* skip preferred */
3388 len = ceph_decode_32(p);
3390 pr_warn("ceph_object_locator::key is set\n");
3394 if (struct_v >= 5) {
3395 bool changed = false;
3397 len = ceph_decode_32(p);
3399 ceph_decode_need(p, end, len, e_inval);
3400 if (!oloc->pool_ns ||
3401 ceph_compare_string(oloc->pool_ns, *p, len))
3409 /* redirect changes namespace */
3410 pr_warn("ceph_object_locator::nspace is changed\n");
3415 if (struct_v >= 6) {
3416 s64 hash = ceph_decode_64(p);
3418 pr_warn("ceph_object_locator::hash is set\n");
3433 static int ceph_redirect_decode(void **p, void *end,
3434 struct ceph_request_redirect *redir)
3436 u8 struct_v, struct_cv;
3441 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3442 struct_v = ceph_decode_8(p);
3443 struct_cv = ceph_decode_8(p);
3444 if (struct_cv > 1) {
3445 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3446 struct_v, struct_cv);
3449 len = ceph_decode_32(p);
3450 ceph_decode_need(p, end, len, e_inval);
3451 struct_end = *p + len;
3453 ret = ceph_oloc_decode(p, end, &redir->oloc);
3457 len = ceph_decode_32(p);
3459 pr_warn("ceph_request_redirect::object_name is set\n");
3463 len = ceph_decode_32(p);
3464 *p += len; /* skip osd_instructions */
3476 struct MOSDOpReply {
3477 struct ceph_pg pgid;
3482 u32 outdata_len[CEPH_OSD_MAX_OPS];
3483 s32 rval[CEPH_OSD_MAX_OPS];
3485 struct ceph_eversion replay_version;
3487 struct ceph_request_redirect redirect;
3490 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3492 void *p = msg->front.iov_base;
3493 void *const end = p + msg->front.iov_len;
3494 u16 version = le16_to_cpu(msg->hdr.version);
3495 struct ceph_eversion bad_replay_version;
3501 ceph_decode_32_safe(&p, end, len, e_inval);
3502 ceph_decode_need(&p, end, len, e_inval);
3503 p += len; /* skip oid */
3505 ret = ceph_decode_pgid(&p, end, &m->pgid);
3509 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3510 ceph_decode_32_safe(&p, end, m->result, e_inval);
3511 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3512 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3513 p += sizeof(bad_replay_version);
3514 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3516 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3517 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3520 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3522 for (i = 0; i < m->num_ops; i++) {
3523 struct ceph_osd_op *op = p;
3525 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3529 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3530 for (i = 0; i < m->num_ops; i++)
3531 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3534 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3535 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3536 p += sizeof(m->replay_version);
3537 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3539 m->replay_version = bad_replay_version; /* struct */
3540 m->user_version = le64_to_cpu(m->replay_version.version);
3545 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3553 ret = ceph_redirect_decode(&p, end, &m->redirect);
3557 ceph_oloc_init(&m->redirect.oloc);
3567 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3570 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3572 struct ceph_osd_client *osdc = osd->o_osdc;
3573 struct ceph_osd_request *req;
3574 struct MOSDOpReply m;
3575 u64 tid = le64_to_cpu(msg->hdr.tid);
3580 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3582 down_read(&osdc->lock);
3583 if (!osd_registered(osd)) {
3584 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3585 goto out_unlock_osdc;
3587 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3589 mutex_lock(&osd->lock);
3590 req = lookup_request(&osd->o_requests, tid);
3592 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3593 goto out_unlock_session;
3596 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3597 ret = decode_MOSDOpReply(msg, &m);
3598 m.redirect.oloc.pool_ns = NULL;
3600 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3605 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3606 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3607 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3608 le64_to_cpu(m.replay_version.version), m.user_version);
3610 if (m.retry_attempt >= 0) {
3611 if (m.retry_attempt != req->r_attempts - 1) {
3612 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3613 req, req->r_tid, m.retry_attempt,
3614 req->r_attempts - 1);
3615 goto out_unlock_session;
3618 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3621 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3622 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3623 m.redirect.oloc.pool);
3624 unlink_request(osd, req);
3625 mutex_unlock(&osd->lock);
3628 * Not ceph_oloc_copy() - changing pool_ns is not
3631 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3632 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3634 __submit_request(req, false);
3635 goto out_unlock_osdc;
3638 if (m.num_ops != req->r_num_ops) {
3639 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3640 req->r_num_ops, req->r_tid);
3643 for (i = 0; i < req->r_num_ops; i++) {
3644 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3645 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3646 req->r_ops[i].rval = m.rval[i];
3647 req->r_ops[i].outdata_len = m.outdata_len[i];
3648 data_len += m.outdata_len[i];
3650 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3651 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3652 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3655 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3656 req, req->r_tid, m.result, data_len);
3659 * Since we only ever request ONDISK, we should only ever get
3660 * one (type of) reply back.
3662 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3663 req->r_result = m.result ?: data_len;
3664 finish_request(req);
3665 mutex_unlock(&osd->lock);
3666 up_read(&osdc->lock);
3668 __complete_request(req);
3672 complete_request(req, -EIO);
3674 mutex_unlock(&osd->lock);
3676 up_read(&osdc->lock);
3679 static void set_pool_was_full(struct ceph_osd_client *osdc)
3683 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3684 struct ceph_pg_pool_info *pi =
3685 rb_entry(n, struct ceph_pg_pool_info, node);
3687 pi->was_full = __pool_full(pi);
3691 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3693 struct ceph_pg_pool_info *pi;
3695 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3699 return pi->was_full && !__pool_full(pi);
3702 static enum calc_target_result
3703 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3705 struct ceph_osd_client *osdc = lreq->osdc;
3706 enum calc_target_result ct_res;
3708 ct_res = calc_target(osdc, &lreq->t, NULL, true);
3709 if (ct_res == CALC_TARGET_NEED_RESEND) {
3710 struct ceph_osd *osd;
3712 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3713 if (osd != lreq->osd) {
3714 unlink_linger(lreq->osd, lreq);
3715 link_linger(osd, lreq);
3723 * Requeue requests whose mapping to an OSD has changed.
3725 static void scan_requests(struct ceph_osd *osd,
3728 bool check_pool_cleared_full,
3729 struct rb_root *need_resend,
3730 struct list_head *need_resend_linger)
3732 struct ceph_osd_client *osdc = osd->o_osdc;
3734 bool force_resend_writes;
3736 for (n = rb_first(&osd->o_linger_requests); n; ) {
3737 struct ceph_osd_linger_request *lreq =
3738 rb_entry(n, struct ceph_osd_linger_request, node);
3739 enum calc_target_result ct_res;
3741 n = rb_next(n); /* recalc_linger_target() */
3743 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3745 ct_res = recalc_linger_target(lreq);
3747 case CALC_TARGET_NO_ACTION:
3748 force_resend_writes = cleared_full ||
3749 (check_pool_cleared_full &&
3750 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3751 if (!force_resend && !force_resend_writes)
3755 case CALC_TARGET_NEED_RESEND:
3756 cancel_linger_map_check(lreq);
3758 * scan_requests() for the previous epoch(s)
3759 * may have already added it to the list, since
3760 * it's not unlinked here.
3762 if (list_empty(&lreq->scan_item))
3763 list_add_tail(&lreq->scan_item, need_resend_linger);
3765 case CALC_TARGET_POOL_DNE:
3766 list_del_init(&lreq->scan_item);
3767 check_linger_pool_dne(lreq);
3772 for (n = rb_first(&osd->o_requests); n; ) {
3773 struct ceph_osd_request *req =
3774 rb_entry(n, struct ceph_osd_request, r_node);
3775 enum calc_target_result ct_res;
3777 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3779 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3780 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3783 case CALC_TARGET_NO_ACTION:
3784 force_resend_writes = cleared_full ||
3785 (check_pool_cleared_full &&
3786 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3787 if (!force_resend &&
3788 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3789 !force_resend_writes))
3793 case CALC_TARGET_NEED_RESEND:
3794 cancel_map_check(req);
3795 unlink_request(osd, req);
3796 insert_request(need_resend, req);
3798 case CALC_TARGET_POOL_DNE:
3799 check_pool_dne(req);
3805 static int handle_one_map(struct ceph_osd_client *osdc,
3806 void *p, void *end, bool incremental,
3807 struct rb_root *need_resend,
3808 struct list_head *need_resend_linger)
3810 struct ceph_osdmap *newmap;
3812 bool skipped_map = false;
3815 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3816 set_pool_was_full(osdc);
3819 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3821 newmap = ceph_osdmap_decode(&p, end);
3823 return PTR_ERR(newmap);
3825 if (newmap != osdc->osdmap) {
3827 * Preserve ->was_full before destroying the old map.
3828 * For pools that weren't in the old map, ->was_full
3831 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3832 struct ceph_pg_pool_info *pi =
3833 rb_entry(n, struct ceph_pg_pool_info, node);
3834 struct ceph_pg_pool_info *old_pi;
3836 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3838 pi->was_full = old_pi->was_full;
3840 WARN_ON(pi->was_full);
3843 if (osdc->osdmap->epoch &&
3844 osdc->osdmap->epoch + 1 < newmap->epoch) {
3845 WARN_ON(incremental);
3849 ceph_osdmap_destroy(osdc->osdmap);
3850 osdc->osdmap = newmap;
3853 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3854 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3855 need_resend, need_resend_linger);
3857 for (n = rb_first(&osdc->osds); n; ) {
3858 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3860 n = rb_next(n); /* close_osd() */
3862 scan_requests(osd, skipped_map, was_full, true, need_resend,
3863 need_resend_linger);
3864 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3865 memcmp(&osd->o_con.peer_addr,
3866 ceph_osd_addr(osdc->osdmap, osd->o_osd),
3867 sizeof(struct ceph_entity_addr)))
3874 static void kick_requests(struct ceph_osd_client *osdc,
3875 struct rb_root *need_resend,
3876 struct list_head *need_resend_linger)
3878 struct ceph_osd_linger_request *lreq, *nlreq;
3879 enum calc_target_result ct_res;
3882 /* make sure need_resend targets reflect latest map */
3883 for (n = rb_first(need_resend); n; ) {
3884 struct ceph_osd_request *req =
3885 rb_entry(n, struct ceph_osd_request, r_node);
3889 if (req->r_t.epoch < osdc->osdmap->epoch) {
3890 ct_res = calc_target(osdc, &req->r_t, NULL, false);
3891 if (ct_res == CALC_TARGET_POOL_DNE) {
3892 erase_request(need_resend, req);
3893 check_pool_dne(req);
3898 for (n = rb_first(need_resend); n; ) {
3899 struct ceph_osd_request *req =
3900 rb_entry(n, struct ceph_osd_request, r_node);
3901 struct ceph_osd *osd;
3904 erase_request(need_resend, req); /* before link_request() */
3906 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3907 link_request(osd, req);
3908 if (!req->r_linger) {
3909 if (!osd_homeless(osd) && !req->r_t.paused)
3912 cancel_linger_request(req);
3916 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3917 if (!osd_homeless(lreq->osd))
3920 list_del_init(&lreq->scan_item);
3925 * Process updated osd map.
3927 * The message contains any number of incremental and full maps, normally
3928 * indicating some sort of topology change in the cluster. Kick requests
3929 * off to different OSDs as needed.
3931 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3933 void *p = msg->front.iov_base;
3934 void *const end = p + msg->front.iov_len;
3935 u32 nr_maps, maplen;
3937 struct ceph_fsid fsid;
3938 struct rb_root need_resend = RB_ROOT;
3939 LIST_HEAD(need_resend_linger);
3940 bool handled_incremental = false;
3941 bool was_pauserd, was_pausewr;
3942 bool pauserd, pausewr;
3945 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3946 down_write(&osdc->lock);
3949 ceph_decode_need(&p, end, sizeof(fsid), bad);
3950 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3951 if (ceph_check_fsid(osdc->client, &fsid) < 0)
3954 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3955 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3956 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3957 have_pool_full(osdc);
3959 /* incremental maps */
3960 ceph_decode_32_safe(&p, end, nr_maps, bad);
3961 dout(" %d inc maps\n", nr_maps);
3962 while (nr_maps > 0) {
3963 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3964 epoch = ceph_decode_32(&p);
3965 maplen = ceph_decode_32(&p);
3966 ceph_decode_need(&p, end, maplen, bad);
3967 if (osdc->osdmap->epoch &&
3968 osdc->osdmap->epoch + 1 == epoch) {
3969 dout("applying incremental map %u len %d\n",
3971 err = handle_one_map(osdc, p, p + maplen, true,
3972 &need_resend, &need_resend_linger);
3975 handled_incremental = true;
3977 dout("ignoring incremental map %u len %d\n",
3983 if (handled_incremental)
3987 ceph_decode_32_safe(&p, end, nr_maps, bad);
3988 dout(" %d full maps\n", nr_maps);
3990 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3991 epoch = ceph_decode_32(&p);
3992 maplen = ceph_decode_32(&p);
3993 ceph_decode_need(&p, end, maplen, bad);
3995 dout("skipping non-latest full map %u len %d\n",
3997 } else if (osdc->osdmap->epoch >= epoch) {
3998 dout("skipping full map %u len %d, "
3999 "older than our %u\n", epoch, maplen,
4000 osdc->osdmap->epoch);
4002 dout("taking full map %u len %d\n", epoch, maplen);
4003 err = handle_one_map(osdc, p, p + maplen, false,
4004 &need_resend, &need_resend_linger);
4014 * subscribe to subsequent osdmap updates if full to ensure
4015 * we find out when we are no longer full and stop returning
4018 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4019 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4020 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
4021 have_pool_full(osdc);
4022 if (was_pauserd || was_pausewr || pauserd || pausewr ||
4023 osdc->osdmap->epoch < osdc->epoch_barrier)
4024 maybe_request_map(osdc);
4026 kick_requests(osdc, &need_resend, &need_resend_linger);
4028 ceph_osdc_abort_on_full(osdc);
4029 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
4030 osdc->osdmap->epoch);
4031 up_write(&osdc->lock);
4032 wake_up_all(&osdc->client->auth_wq);
4036 pr_err("osdc handle_map corrupt msg\n");
4038 up_write(&osdc->lock);
4042 * Resubmit requests pending on the given osd.
4044 static void kick_osd_requests(struct ceph_osd *osd)
4048 clear_backoffs(osd);
4050 for (n = rb_first(&osd->o_requests); n; ) {
4051 struct ceph_osd_request *req =
4052 rb_entry(n, struct ceph_osd_request, r_node);
4054 n = rb_next(n); /* cancel_linger_request() */
4056 if (!req->r_linger) {
4057 if (!req->r_t.paused)
4060 cancel_linger_request(req);
4063 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4064 struct ceph_osd_linger_request *lreq =
4065 rb_entry(n, struct ceph_osd_linger_request, node);
4072 * If the osd connection drops, we need to resubmit all requests.
4074 static void osd_fault(struct ceph_connection *con)
4076 struct ceph_osd *osd = con->private;
4077 struct ceph_osd_client *osdc = osd->o_osdc;
4079 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4081 down_write(&osdc->lock);
4082 if (!osd_registered(osd)) {
4083 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4087 if (!reopen_osd(osd))
4088 kick_osd_requests(osd);
4089 maybe_request_map(osdc);
4092 up_write(&osdc->lock);
4095 struct MOSDBackoff {
4096 struct ceph_spg spgid;
4100 struct ceph_hobject_id *begin;
4101 struct ceph_hobject_id *end;
4104 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4106 void *p = msg->front.iov_base;
4107 void *const end = p + msg->front.iov_len;
4112 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4116 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4120 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4121 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4122 ceph_decode_8_safe(&p, end, m->op, e_inval);
4123 ceph_decode_64_safe(&p, end, m->id, e_inval);
4125 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4129 ret = decode_hoid(&p, end, m->begin);
4131 free_hoid(m->begin);
4135 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4137 free_hoid(m->begin);
4141 ret = decode_hoid(&p, end, m->end);
4143 free_hoid(m->begin);
4154 static struct ceph_msg *create_backoff_message(
4155 const struct ceph_osd_backoff *backoff,
4158 struct ceph_msg *msg;
4162 msg_size = CEPH_ENCODING_START_BLK_LEN +
4163 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4164 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4165 msg_size += CEPH_ENCODING_START_BLK_LEN +
4166 hoid_encoding_size(backoff->begin);
4167 msg_size += CEPH_ENCODING_START_BLK_LEN +
4168 hoid_encoding_size(backoff->end);
4170 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4174 p = msg->front.iov_base;
4175 end = p + msg->front_alloc_len;
4177 encode_spgid(&p, &backoff->spgid);
4178 ceph_encode_32(&p, map_epoch);
4179 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4180 ceph_encode_64(&p, backoff->id);
4181 encode_hoid(&p, end, backoff->begin);
4182 encode_hoid(&p, end, backoff->end);
4185 msg->front.iov_len = p - msg->front.iov_base;
4186 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4187 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4192 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4194 struct ceph_spg_mapping *spg;
4195 struct ceph_osd_backoff *backoff;
4196 struct ceph_msg *msg;
4198 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4199 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4201 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4203 spg = alloc_spg_mapping();
4205 pr_err("%s failed to allocate spg\n", __func__);
4208 spg->spgid = m->spgid; /* struct */
4209 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4212 backoff = alloc_backoff();
4214 pr_err("%s failed to allocate backoff\n", __func__);
4217 backoff->spgid = m->spgid; /* struct */
4218 backoff->id = m->id;
4219 backoff->begin = m->begin;
4220 m->begin = NULL; /* backoff now owns this */
4221 backoff->end = m->end;
4222 m->end = NULL; /* ditto */
4224 insert_backoff(&spg->backoffs, backoff);
4225 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4228 * Ack with original backoff's epoch so that the OSD can
4229 * discard this if there was a PG split.
4231 msg = create_backoff_message(backoff, m->map_epoch);
4233 pr_err("%s failed to allocate msg\n", __func__);
4236 ceph_con_send(&osd->o_con, msg);
4239 static bool target_contained_by(const struct ceph_osd_request_target *t,
4240 const struct ceph_hobject_id *begin,
4241 const struct ceph_hobject_id *end)
4243 struct ceph_hobject_id hoid;
4246 hoid_fill_from_target(&hoid, t);
4247 cmp = hoid_compare(&hoid, begin);
4248 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4251 static void handle_backoff_unblock(struct ceph_osd *osd,
4252 const struct MOSDBackoff *m)
4254 struct ceph_spg_mapping *spg;
4255 struct ceph_osd_backoff *backoff;
4258 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4259 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4261 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4263 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4264 __func__, osd->o_osd, m->spgid.pgid.pool,
4265 m->spgid.pgid.seed, m->spgid.shard, m->id);
4269 if (hoid_compare(backoff->begin, m->begin) &&
4270 hoid_compare(backoff->end, m->end)) {
4271 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4272 __func__, osd->o_osd, m->spgid.pgid.pool,
4273 m->spgid.pgid.seed, m->spgid.shard, m->id);
4274 /* unblock it anyway... */
4277 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4280 erase_backoff(&spg->backoffs, backoff);
4281 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4282 free_backoff(backoff);
4284 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4285 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4286 free_spg_mapping(spg);
4289 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4290 struct ceph_osd_request *req =
4291 rb_entry(n, struct ceph_osd_request, r_node);
4293 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4295 * Match against @m, not @backoff -- the PG may
4296 * have split on the OSD.
4298 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4300 * If no other installed backoff applies,
4309 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4311 struct ceph_osd_client *osdc = osd->o_osdc;
4312 struct MOSDBackoff m;
4315 down_read(&osdc->lock);
4316 if (!osd_registered(osd)) {
4317 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4318 up_read(&osdc->lock);
4321 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4323 mutex_lock(&osd->lock);
4324 ret = decode_MOSDBackoff(msg, &m);
4326 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4332 case CEPH_OSD_BACKOFF_OP_BLOCK:
4333 handle_backoff_block(osd, &m);
4335 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4336 handle_backoff_unblock(osd, &m);
4339 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4346 mutex_unlock(&osd->lock);
4347 up_read(&osdc->lock);
4351 * Process osd watch notifications
4353 static void handle_watch_notify(struct ceph_osd_client *osdc,
4354 struct ceph_msg *msg)
4356 void *p = msg->front.iov_base;
4357 void *const end = p + msg->front.iov_len;
4358 struct ceph_osd_linger_request *lreq;
4359 struct linger_work *lwork;
4360 u8 proto_ver, opcode;
4361 u64 cookie, notify_id;
4362 u64 notifier_id = 0;
4363 s32 return_code = 0;
4364 void *payload = NULL;
4365 u32 payload_len = 0;
4367 ceph_decode_8_safe(&p, end, proto_ver, bad);
4368 ceph_decode_8_safe(&p, end, opcode, bad);
4369 ceph_decode_64_safe(&p, end, cookie, bad);
4370 p += 8; /* skip ver */
4371 ceph_decode_64_safe(&p, end, notify_id, bad);
4373 if (proto_ver >= 1) {
4374 ceph_decode_32_safe(&p, end, payload_len, bad);
4375 ceph_decode_need(&p, end, payload_len, bad);
4380 if (le16_to_cpu(msg->hdr.version) >= 2)
4381 ceph_decode_32_safe(&p, end, return_code, bad);
4383 if (le16_to_cpu(msg->hdr.version) >= 3)
4384 ceph_decode_64_safe(&p, end, notifier_id, bad);
4386 down_read(&osdc->lock);
4387 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4389 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4391 goto out_unlock_osdc;
4394 mutex_lock(&lreq->lock);
4395 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4396 opcode, cookie, lreq, lreq->is_watch);
4397 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4398 if (!lreq->last_error) {
4399 lreq->last_error = -ENOTCONN;
4400 queue_watch_error(lreq);
4402 } else if (!lreq->is_watch) {
4403 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4404 if (lreq->notify_id && lreq->notify_id != notify_id) {
4405 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4406 lreq->notify_id, notify_id);
4407 } else if (!completion_done(&lreq->notify_finish_wait)) {
4408 struct ceph_msg_data *data =
4409 msg->num_data_items ? &msg->data[0] : NULL;
4412 if (lreq->preply_pages) {
4413 WARN_ON(data->type !=
4414 CEPH_MSG_DATA_PAGES);
4415 *lreq->preply_pages = data->pages;
4416 *lreq->preply_len = data->length;
4418 ceph_release_page_vector(data->pages,
4419 calc_pages_for(0, data->length));
4422 lreq->notify_finish_error = return_code;
4423 complete_all(&lreq->notify_finish_wait);
4426 /* CEPH_WATCH_EVENT_NOTIFY */
4427 lwork = lwork_alloc(lreq, do_watch_notify);
4429 pr_err("failed to allocate notify-lwork\n");
4430 goto out_unlock_lreq;
4433 lwork->notify.notify_id = notify_id;
4434 lwork->notify.notifier_id = notifier_id;
4435 lwork->notify.payload = payload;
4436 lwork->notify.payload_len = payload_len;
4437 lwork->notify.msg = ceph_msg_get(msg);
4442 mutex_unlock(&lreq->lock);
4444 up_read(&osdc->lock);
4448 pr_err("osdc handle_watch_notify corrupt msg\n");
4452 * Register request, send initial attempt.
4454 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4455 struct ceph_osd_request *req,
4458 down_read(&osdc->lock);
4459 submit_request(req, false);
4460 up_read(&osdc->lock);
4464 EXPORT_SYMBOL(ceph_osdc_start_request);
4467 * Unregister a registered request. The request is not completed:
4468 * ->r_result isn't set and __complete_request() isn't called.
4470 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4472 struct ceph_osd_client *osdc = req->r_osdc;
4474 down_write(&osdc->lock);
4476 cancel_request(req);
4477 up_write(&osdc->lock);
4479 EXPORT_SYMBOL(ceph_osdc_cancel_request);
4482 * @timeout: in jiffies, 0 means "wait forever"
4484 static int wait_request_timeout(struct ceph_osd_request *req,
4485 unsigned long timeout)
4489 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
4490 left = wait_for_completion_killable_timeout(&req->r_completion,
4491 ceph_timeout_jiffies(timeout));
4493 left = left ?: -ETIMEDOUT;
4494 ceph_osdc_cancel_request(req);
4496 left = req->r_result; /* completed */
4503 * wait for a request to complete
4505 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4506 struct ceph_osd_request *req)
4508 return wait_request_timeout(req, 0);
4510 EXPORT_SYMBOL(ceph_osdc_wait_request);
4513 * sync - wait for all in-flight requests to flush. avoid starvation.
4515 void ceph_osdc_sync(struct ceph_osd_client *osdc)
4517 struct rb_node *n, *p;
4518 u64 last_tid = atomic64_read(&osdc->last_tid);
4521 down_read(&osdc->lock);
4522 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4523 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4525 mutex_lock(&osd->lock);
4526 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4527 struct ceph_osd_request *req =
4528 rb_entry(p, struct ceph_osd_request, r_node);
4530 if (req->r_tid > last_tid)
4533 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4536 ceph_osdc_get_request(req);
4537 mutex_unlock(&osd->lock);
4538 up_read(&osdc->lock);
4539 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4540 __func__, req, req->r_tid, last_tid);
4541 wait_for_completion(&req->r_completion);
4542 ceph_osdc_put_request(req);
4546 mutex_unlock(&osd->lock);
4549 up_read(&osdc->lock);
4550 dout("%s done last_tid %llu\n", __func__, last_tid);
4552 EXPORT_SYMBOL(ceph_osdc_sync);
4554 static struct ceph_osd_request *
4555 alloc_linger_request(struct ceph_osd_linger_request *lreq)
4557 struct ceph_osd_request *req;
4559 req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4563 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4564 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4568 static struct ceph_osd_request *
4569 alloc_watch_request(struct ceph_osd_linger_request *lreq, u8 watch_opcode)
4571 struct ceph_osd_request *req;
4573 req = alloc_linger_request(lreq);
4578 * Pass 0 for cookie because we don't know it yet, it will be
4579 * filled in by linger_submit().
4581 osd_req_op_watch_init(req, 0, 0, watch_opcode);
4583 if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4584 ceph_osdc_put_request(req);
4592 * Returns a handle, caller owns a ref.
4594 struct ceph_osd_linger_request *
4595 ceph_osdc_watch(struct ceph_osd_client *osdc,
4596 struct ceph_object_id *oid,
4597 struct ceph_object_locator *oloc,
4598 rados_watchcb2_t wcb,
4599 rados_watcherrcb_t errcb,
4602 struct ceph_osd_linger_request *lreq;
4605 lreq = linger_alloc(osdc);
4607 return ERR_PTR(-ENOMEM);
4609 lreq->is_watch = true;
4611 lreq->errcb = errcb;
4613 lreq->watch_valid_thru = jiffies;
4615 ceph_oid_copy(&lreq->t.base_oid, oid);
4616 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4617 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4618 ktime_get_real_ts64(&lreq->mtime);
4620 lreq->reg_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_WATCH);
4621 if (!lreq->reg_req) {
4626 lreq->ping_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_PING);
4627 if (!lreq->ping_req) {
4632 linger_submit(lreq);
4633 ret = linger_reg_commit_wait(lreq);
4635 linger_cancel(lreq);
4643 return ERR_PTR(ret);
4645 EXPORT_SYMBOL(ceph_osdc_watch);
4650 * Times out after mount_timeout to preserve rbd unmap behaviour
4651 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4652 * with mount_timeout").
4654 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4655 struct ceph_osd_linger_request *lreq)
4657 struct ceph_options *opts = osdc->client->options;
4658 struct ceph_osd_request *req;
4661 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4665 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4666 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4667 req->r_flags = CEPH_OSD_FLAG_WRITE;
4668 ktime_get_real_ts64(&req->r_mtime);
4669 osd_req_op_watch_init(req, 0, lreq->linger_id,
4670 CEPH_OSD_WATCH_OP_UNWATCH);
4672 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4676 ceph_osdc_start_request(osdc, req, false);
4677 linger_cancel(lreq);
4679 ret = wait_request_timeout(req, opts->mount_timeout);
4682 ceph_osdc_put_request(req);
4685 EXPORT_SYMBOL(ceph_osdc_unwatch);
4687 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4688 u64 notify_id, u64 cookie, void *payload,
4691 struct ceph_osd_req_op *op;
4692 struct ceph_pagelist *pl;
4695 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4697 pl = ceph_pagelist_alloc(GFP_NOIO);
4701 ret = ceph_pagelist_encode_64(pl, notify_id);
4702 ret |= ceph_pagelist_encode_64(pl, cookie);
4704 ret |= ceph_pagelist_encode_32(pl, payload_len);
4705 ret |= ceph_pagelist_append(pl, payload, payload_len);
4707 ret |= ceph_pagelist_encode_32(pl, 0);
4710 ceph_pagelist_release(pl);
4714 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4715 op->indata_len = pl->length;
4719 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4720 struct ceph_object_id *oid,
4721 struct ceph_object_locator *oloc,
4727 struct ceph_osd_request *req;
4730 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4734 ceph_oid_copy(&req->r_base_oid, oid);
4735 ceph_oloc_copy(&req->r_base_oloc, oloc);
4736 req->r_flags = CEPH_OSD_FLAG_READ;
4738 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4743 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4747 ceph_osdc_start_request(osdc, req, false);
4748 ret = ceph_osdc_wait_request(osdc, req);
4751 ceph_osdc_put_request(req);
4754 EXPORT_SYMBOL(ceph_osdc_notify_ack);
4756 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4757 u64 cookie, u32 prot_ver, u32 timeout,
4758 void *payload, u32 payload_len)
4760 struct ceph_osd_req_op *op;
4761 struct ceph_pagelist *pl;
4764 op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4765 op->notify.cookie = cookie;
4767 pl = ceph_pagelist_alloc(GFP_NOIO);
4771 ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4772 ret |= ceph_pagelist_encode_32(pl, timeout);
4773 ret |= ceph_pagelist_encode_32(pl, payload_len);
4774 ret |= ceph_pagelist_append(pl, payload, payload_len);
4776 ceph_pagelist_release(pl);
4780 ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4781 op->indata_len = pl->length;
4786 * @timeout: in seconds
4788 * @preply_{pages,len} are initialized both on success and error.
4789 * The caller is responsible for:
4791 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4793 int ceph_osdc_notify(struct ceph_osd_client *osdc,
4794 struct ceph_object_id *oid,
4795 struct ceph_object_locator *oloc,
4799 struct page ***preply_pages,
4802 struct ceph_osd_linger_request *lreq;
4803 struct page **pages;
4808 *preply_pages = NULL;
4812 lreq = linger_alloc(osdc);
4816 lreq->preply_pages = preply_pages;
4817 lreq->preply_len = preply_len;
4819 ceph_oid_copy(&lreq->t.base_oid, oid);
4820 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4821 lreq->t.flags = CEPH_OSD_FLAG_READ;
4823 lreq->reg_req = alloc_linger_request(lreq);
4824 if (!lreq->reg_req) {
4830 * Pass 0 for cookie because we don't know it yet, it will be
4831 * filled in by linger_submit().
4833 ret = osd_req_op_notify_init(lreq->reg_req, 0, 0, 1, timeout,
4834 payload, payload_len);
4839 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4840 if (IS_ERR(pages)) {
4841 ret = PTR_ERR(pages);
4844 ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4846 pages, PAGE_SIZE, 0, false, true);
4848 ret = ceph_osdc_alloc_messages(lreq->reg_req, GFP_NOIO);
4852 linger_submit(lreq);
4853 ret = linger_reg_commit_wait(lreq);
4855 ret = linger_notify_finish_wait(lreq);
4857 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4859 linger_cancel(lreq);
4864 EXPORT_SYMBOL(ceph_osdc_notify);
4867 * Return the number of milliseconds since the watch was last
4868 * confirmed, or an error. If there is an error, the watch is no
4869 * longer valid, and should be destroyed with ceph_osdc_unwatch().
4871 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4872 struct ceph_osd_linger_request *lreq)
4874 unsigned long stamp, age;
4877 down_read(&osdc->lock);
4878 mutex_lock(&lreq->lock);
4879 stamp = lreq->watch_valid_thru;
4880 if (!list_empty(&lreq->pending_lworks)) {
4881 struct linger_work *lwork =
4882 list_first_entry(&lreq->pending_lworks,
4886 if (time_before(lwork->queued_stamp, stamp))
4887 stamp = lwork->queued_stamp;
4889 age = jiffies - stamp;
4890 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4891 lreq, lreq->linger_id, age, lreq->last_error);
4892 /* we are truncating to msecs, so return a safe upper bound */
4893 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4895 mutex_unlock(&lreq->lock);
4896 up_read(&osdc->lock);
4900 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4906 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4907 &struct_v, &struct_len);
4912 ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
4913 ceph_decode_64_safe(p, end, item->cookie, bad);
4914 ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
4916 if (struct_v >= 2) {
4917 ret = ceph_decode_entity_addr(p, end, &item->addr);
4924 dout("%s %s%llu cookie %llu addr %s\n", __func__,
4925 ENTITY_NAME(item->name), item->cookie,
4926 ceph_pr_addr(&item->addr));
4931 static int decode_watchers(void **p, void *end,
4932 struct ceph_watch_item **watchers,
4940 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4941 &struct_v, &struct_len);
4945 *num_watchers = ceph_decode_32(p);
4946 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4950 for (i = 0; i < *num_watchers; i++) {
4951 ret = decode_watcher(p, end, *watchers + i);
4962 * On success, the caller is responsible for:
4966 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4967 struct ceph_object_id *oid,
4968 struct ceph_object_locator *oloc,
4969 struct ceph_watch_item **watchers,
4972 struct ceph_osd_request *req;
4973 struct page **pages;
4976 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4980 ceph_oid_copy(&req->r_base_oid, oid);
4981 ceph_oloc_copy(&req->r_base_oloc, oloc);
4982 req->r_flags = CEPH_OSD_FLAG_READ;
4984 pages = ceph_alloc_page_vector(1, GFP_NOIO);
4985 if (IS_ERR(pages)) {
4986 ret = PTR_ERR(pages);
4990 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4991 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4993 pages, PAGE_SIZE, 0, false, true);
4995 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4999 ceph_osdc_start_request(osdc, req, false);
5000 ret = ceph_osdc_wait_request(osdc, req);
5002 void *p = page_address(pages[0]);
5003 void *const end = p + req->r_ops[0].outdata_len;
5005 ret = decode_watchers(&p, end, watchers, num_watchers);
5009 ceph_osdc_put_request(req);
5012 EXPORT_SYMBOL(ceph_osdc_list_watchers);
5015 * Call all pending notify callbacks - for use after a watch is
5016 * unregistered, to make sure no more callbacks for it will be invoked
5018 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5020 dout("%s osdc %p\n", __func__, osdc);
5021 flush_workqueue(osdc->notify_wq);
5023 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5025 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
5027 down_read(&osdc->lock);
5028 maybe_request_map(osdc);
5029 up_read(&osdc->lock);
5031 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5034 * Execute an OSD class method on an object.
5036 * @flags: CEPH_OSD_FLAG_*
5037 * @resp_len: in/out param for reply length
5039 int ceph_osdc_call(struct ceph_osd_client *osdc,
5040 struct ceph_object_id *oid,
5041 struct ceph_object_locator *oloc,
5042 const char *class, const char *method,
5044 struct page *req_page, size_t req_len,
5045 struct page **resp_pages, size_t *resp_len)
5047 struct ceph_osd_request *req;
5050 if (req_len > PAGE_SIZE)
5053 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5057 ceph_oid_copy(&req->r_base_oid, oid);
5058 ceph_oloc_copy(&req->r_base_oloc, oloc);
5059 req->r_flags = flags;
5061 ret = osd_req_op_cls_init(req, 0, class, method);
5066 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5069 osd_req_op_cls_response_data_pages(req, 0, resp_pages,
5070 *resp_len, 0, false, false);
5072 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5076 ceph_osdc_start_request(osdc, req, false);
5077 ret = ceph_osdc_wait_request(osdc, req);
5079 ret = req->r_ops[0].rval;
5081 *resp_len = req->r_ops[0].outdata_len;
5085 ceph_osdc_put_request(req);
5088 EXPORT_SYMBOL(ceph_osdc_call);
5093 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5098 osdc->client = client;
5099 init_rwsem(&osdc->lock);
5100 osdc->osds = RB_ROOT;
5101 INIT_LIST_HEAD(&osdc->osd_lru);
5102 spin_lock_init(&osdc->osd_lru_lock);
5103 osd_init(&osdc->homeless_osd);
5104 osdc->homeless_osd.o_osdc = osdc;
5105 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5106 osdc->last_linger_id = CEPH_LINGER_ID_START;
5107 osdc->linger_requests = RB_ROOT;
5108 osdc->map_checks = RB_ROOT;
5109 osdc->linger_map_checks = RB_ROOT;
5110 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
5111 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5114 osdc->osdmap = ceph_osdmap_alloc();
5118 osdc->req_mempool = mempool_create_slab_pool(10,
5119 ceph_osd_request_cache);
5120 if (!osdc->req_mempool)
5123 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5124 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
5127 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5128 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
5134 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5135 if (!osdc->notify_wq)
5136 goto out_msgpool_reply;
5138 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5139 if (!osdc->completion_wq)
5142 schedule_delayed_work(&osdc->timeout_work,
5143 osdc->client->options->osd_keepalive_timeout);
5144 schedule_delayed_work(&osdc->osds_timeout_work,
5145 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5150 destroy_workqueue(osdc->notify_wq);
5152 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5154 ceph_msgpool_destroy(&osdc->msgpool_op);
5156 mempool_destroy(osdc->req_mempool);
5158 ceph_osdmap_destroy(osdc->osdmap);
5163 void ceph_osdc_stop(struct ceph_osd_client *osdc)
5165 destroy_workqueue(osdc->completion_wq);
5166 destroy_workqueue(osdc->notify_wq);
5167 cancel_delayed_work_sync(&osdc->timeout_work);
5168 cancel_delayed_work_sync(&osdc->osds_timeout_work);
5170 down_write(&osdc->lock);
5171 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5172 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5173 struct ceph_osd, o_node);
5176 up_write(&osdc->lock);
5177 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5178 osd_cleanup(&osdc->homeless_osd);
5180 WARN_ON(!list_empty(&osdc->osd_lru));
5181 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
5182 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5183 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5184 WARN_ON(atomic_read(&osdc->num_requests));
5185 WARN_ON(atomic_read(&osdc->num_homeless));
5187 ceph_osdmap_destroy(osdc->osdmap);
5188 mempool_destroy(osdc->req_mempool);
5189 ceph_msgpool_destroy(&osdc->msgpool_op);
5190 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5194 * Read some contiguous pages. If we cross a stripe boundary, shorten
5195 * *plen. Return number of bytes read, or error.
5197 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5198 struct ceph_vino vino, struct ceph_file_layout *layout,
5200 u32 truncate_seq, u64 truncate_size,
5201 struct page **pages, int num_pages, int page_align)
5203 struct ceph_osd_request *req;
5206 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5207 vino.snap, off, *plen);
5208 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
5209 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5210 NULL, truncate_seq, truncate_size,
5213 return PTR_ERR(req);
5215 /* it may be a short read due to an object boundary */
5216 osd_req_op_extent_osd_data_pages(req, 0,
5217 pages, *plen, page_align, false, false);
5219 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
5220 off, *plen, *plen, page_align);
5222 rc = ceph_osdc_start_request(osdc, req, false);
5224 rc = ceph_osdc_wait_request(osdc, req);
5226 ceph_osdc_put_request(req);
5227 dout("readpages result %d\n", rc);
5230 EXPORT_SYMBOL(ceph_osdc_readpages);
5233 * do a synchronous write on N pages
5235 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5236 struct ceph_file_layout *layout,
5237 struct ceph_snap_context *snapc,
5239 u32 truncate_seq, u64 truncate_size,
5240 struct timespec64 *mtime,
5241 struct page **pages, int num_pages)
5243 struct ceph_osd_request *req;
5245 int page_align = off & ~PAGE_MASK;
5247 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
5248 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5249 snapc, truncate_seq, truncate_size,
5252 return PTR_ERR(req);
5254 /* it may be a short write due to an object boundary */
5255 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
5257 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
5259 req->r_mtime = *mtime;
5260 rc = ceph_osdc_start_request(osdc, req, true);
5262 rc = ceph_osdc_wait_request(osdc, req);
5264 ceph_osdc_put_request(req);
5267 dout("writepages result %d\n", rc);
5270 EXPORT_SYMBOL(ceph_osdc_writepages);
5272 static int osd_req_op_copy_from_init(struct ceph_osd_request *req,
5273 u64 src_snapid, u64 src_version,
5274 struct ceph_object_id *src_oid,
5275 struct ceph_object_locator *src_oloc,
5276 u32 src_fadvise_flags,
5277 u32 dst_fadvise_flags,
5280 struct ceph_osd_req_op *op;
5281 struct page **pages;
5284 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
5286 return PTR_ERR(pages);
5288 op = _osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM, dst_fadvise_flags);
5289 op->copy_from.snapid = src_snapid;
5290 op->copy_from.src_version = src_version;
5291 op->copy_from.flags = copy_from_flags;
5292 op->copy_from.src_fadvise_flags = src_fadvise_flags;
5294 p = page_address(pages[0]);
5295 end = p + PAGE_SIZE;
5296 ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
5297 encode_oloc(&p, end, src_oloc);
5298 op->indata_len = PAGE_SIZE - (end - p);
5300 ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
5301 op->indata_len, 0, false, true);
5305 int ceph_osdc_copy_from(struct ceph_osd_client *osdc,
5306 u64 src_snapid, u64 src_version,
5307 struct ceph_object_id *src_oid,
5308 struct ceph_object_locator *src_oloc,
5309 u32 src_fadvise_flags,
5310 struct ceph_object_id *dst_oid,
5311 struct ceph_object_locator *dst_oloc,
5312 u32 dst_fadvise_flags,
5315 struct ceph_osd_request *req;
5318 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
5322 req->r_flags = CEPH_OSD_FLAG_WRITE;
5324 ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
5325 ceph_oid_copy(&req->r_t.base_oid, dst_oid);
5327 ret = osd_req_op_copy_from_init(req, src_snapid, src_version, src_oid,
5328 src_oloc, src_fadvise_flags,
5329 dst_fadvise_flags, copy_from_flags);
5333 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
5337 ceph_osdc_start_request(osdc, req, false);
5338 ret = ceph_osdc_wait_request(osdc, req);
5341 ceph_osdc_put_request(req);
5344 EXPORT_SYMBOL(ceph_osdc_copy_from);
5346 int __init ceph_osdc_setup(void)
5348 size_t size = sizeof(struct ceph_osd_request) +
5349 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5351 BUG_ON(ceph_osd_request_cache);
5352 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5355 return ceph_osd_request_cache ? 0 : -ENOMEM;
5358 void ceph_osdc_cleanup(void)
5360 BUG_ON(!ceph_osd_request_cache);
5361 kmem_cache_destroy(ceph_osd_request_cache);
5362 ceph_osd_request_cache = NULL;
5366 * handle incoming message
5368 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5370 struct ceph_osd *osd = con->private;
5371 struct ceph_osd_client *osdc = osd->o_osdc;
5372 int type = le16_to_cpu(msg->hdr.type);
5375 case CEPH_MSG_OSD_MAP:
5376 ceph_osdc_handle_map(osdc, msg);
5378 case CEPH_MSG_OSD_OPREPLY:
5379 handle_reply(osd, msg);
5381 case CEPH_MSG_OSD_BACKOFF:
5382 handle_backoff(osd, msg);
5384 case CEPH_MSG_WATCH_NOTIFY:
5385 handle_watch_notify(osdc, msg);
5389 pr_err("received unknown message type %d %s\n", type,
5390 ceph_msg_type_name(type));
5397 * Lookup and return message for incoming reply. Don't try to do
5398 * anything about a larger than preallocated data portion of the
5399 * message at the moment - for now, just skip the message.
5401 static struct ceph_msg *get_reply(struct ceph_connection *con,
5402 struct ceph_msg_header *hdr,
5405 struct ceph_osd *osd = con->private;
5406 struct ceph_osd_client *osdc = osd->o_osdc;
5407 struct ceph_msg *m = NULL;
5408 struct ceph_osd_request *req;
5409 int front_len = le32_to_cpu(hdr->front_len);
5410 int data_len = le32_to_cpu(hdr->data_len);
5411 u64 tid = le64_to_cpu(hdr->tid);
5413 down_read(&osdc->lock);
5414 if (!osd_registered(osd)) {
5415 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5417 goto out_unlock_osdc;
5419 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5421 mutex_lock(&osd->lock);
5422 req = lookup_request(&osd->o_requests, tid);
5424 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5427 goto out_unlock_session;
5430 ceph_msg_revoke_incoming(req->r_reply);
5432 if (front_len > req->r_reply->front_alloc_len) {
5433 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5434 __func__, osd->o_osd, req->r_tid, front_len,
5435 req->r_reply->front_alloc_len);
5436 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5439 goto out_unlock_session;
5440 ceph_msg_put(req->r_reply);
5444 if (data_len > req->r_reply->data_length) {
5445 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5446 __func__, osd->o_osd, req->r_tid, data_len,
5447 req->r_reply->data_length);
5450 goto out_unlock_session;
5453 m = ceph_msg_get(req->r_reply);
5454 dout("get_reply tid %lld %p\n", tid, m);
5457 mutex_unlock(&osd->lock);
5459 up_read(&osdc->lock);
5464 * TODO: switch to a msg-owned pagelist
5466 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5469 int type = le16_to_cpu(hdr->type);
5470 u32 front_len = le32_to_cpu(hdr->front_len);
5471 u32 data_len = le32_to_cpu(hdr->data_len);
5473 m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
5478 struct page **pages;
5479 struct ceph_osd_data osd_data;
5481 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5483 if (IS_ERR(pages)) {
5488 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5490 ceph_osdc_msg_data_add(m, &osd_data);
5496 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5497 struct ceph_msg_header *hdr,
5500 struct ceph_osd *osd = con->private;
5501 int type = le16_to_cpu(hdr->type);
5505 case CEPH_MSG_OSD_MAP:
5506 case CEPH_MSG_OSD_BACKOFF:
5507 case CEPH_MSG_WATCH_NOTIFY:
5508 return alloc_msg_with_page_vector(hdr);
5509 case CEPH_MSG_OSD_OPREPLY:
5510 return get_reply(con, hdr, skip);
5512 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5520 * Wrappers to refcount containing ceph_osd struct
5522 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5524 struct ceph_osd *osd = con->private;
5530 static void put_osd_con(struct ceph_connection *con)
5532 struct ceph_osd *osd = con->private;
5540 * Note: returned pointer is the address of a structure that's
5541 * managed separately. Caller must *not* attempt to free it.
5543 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
5544 int *proto, int force_new)
5546 struct ceph_osd *o = con->private;
5547 struct ceph_osd_client *osdc = o->o_osdc;
5548 struct ceph_auth_client *ac = osdc->client->monc.auth;
5549 struct ceph_auth_handshake *auth = &o->o_auth;
5551 if (force_new && auth->authorizer) {
5552 ceph_auth_destroy_authorizer(auth->authorizer);
5553 auth->authorizer = NULL;
5555 if (!auth->authorizer) {
5556 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5559 return ERR_PTR(ret);
5561 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5564 return ERR_PTR(ret);
5566 *proto = ac->protocol;
5571 static int add_authorizer_challenge(struct ceph_connection *con,
5572 void *challenge_buf, int challenge_buf_len)
5574 struct ceph_osd *o = con->private;
5575 struct ceph_osd_client *osdc = o->o_osdc;
5576 struct ceph_auth_client *ac = osdc->client->monc.auth;
5578 return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
5579 challenge_buf, challenge_buf_len);
5582 static int verify_authorizer_reply(struct ceph_connection *con)
5584 struct ceph_osd *o = con->private;
5585 struct ceph_osd_client *osdc = o->o_osdc;
5586 struct ceph_auth_client *ac = osdc->client->monc.auth;
5588 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
5591 static int invalidate_authorizer(struct ceph_connection *con)
5593 struct ceph_osd *o = con->private;
5594 struct ceph_osd_client *osdc = o->o_osdc;
5595 struct ceph_auth_client *ac = osdc->client->monc.auth;
5597 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
5598 return ceph_monc_validate_auth(&osdc->client->monc);
5601 static void osd_reencode_message(struct ceph_msg *msg)
5603 int type = le16_to_cpu(msg->hdr.type);
5605 if (type == CEPH_MSG_OSD_OP)
5606 encode_request_finish(msg);
5609 static int osd_sign_message(struct ceph_msg *msg)
5611 struct ceph_osd *o = msg->con->private;
5612 struct ceph_auth_handshake *auth = &o->o_auth;
5614 return ceph_auth_sign_message(auth, msg);
5617 static int osd_check_message_signature(struct ceph_msg *msg)
5619 struct ceph_osd *o = msg->con->private;
5620 struct ceph_auth_handshake *auth = &o->o_auth;
5622 return ceph_auth_check_message_signature(auth, msg);
5625 static const struct ceph_connection_operations osd_con_ops = {
5628 .dispatch = dispatch,
5629 .get_authorizer = get_authorizer,
5630 .add_authorizer_challenge = add_authorizer_challenge,
5631 .verify_authorizer_reply = verify_authorizer_reply,
5632 .invalidate_authorizer = invalidate_authorizer,
5633 .alloc_msg = alloc_msg,
5634 .reencode_message = osd_reencode_message,
5635 .sign_message = osd_sign_message,
5636 .check_message_signature = osd_check_message_signature,