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 void ceph_osd_iter_init(struct ceph_osd_data *osd_data,
175 struct iov_iter *iter)
177 osd_data->type = CEPH_OSD_DATA_TYPE_ITER;
178 osd_data->iter = *iter;
181 static struct ceph_osd_data *
182 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
184 BUG_ON(which >= osd_req->r_num_ops);
186 return &osd_req->r_ops[which].raw_data_in;
189 struct ceph_osd_data *
190 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
193 return osd_req_op_data(osd_req, which, extent, osd_data);
195 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
197 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
198 unsigned int which, struct page **pages,
199 u64 length, u32 alignment,
200 bool pages_from_pool, bool own_pages)
202 struct ceph_osd_data *osd_data;
204 osd_data = osd_req_op_raw_data_in(osd_req, which);
205 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
206 pages_from_pool, own_pages);
208 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
210 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
211 unsigned int which, struct page **pages,
212 u64 length, u32 alignment,
213 bool pages_from_pool, bool own_pages)
215 struct ceph_osd_data *osd_data;
217 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
218 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
219 pages_from_pool, own_pages);
221 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
223 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
224 unsigned int which, struct ceph_pagelist *pagelist)
226 struct ceph_osd_data *osd_data;
228 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
229 ceph_osd_data_pagelist_init(osd_data, pagelist);
231 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
234 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
236 struct ceph_bio_iter *bio_pos,
239 struct ceph_osd_data *osd_data;
241 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
242 ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
244 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
245 #endif /* CONFIG_BLOCK */
247 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
249 struct bio_vec *bvecs, u32 num_bvecs,
252 struct ceph_osd_data *osd_data;
253 struct ceph_bvec_iter it = {
255 .iter = { .bi_size = bytes },
258 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
259 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
261 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
263 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
265 struct ceph_bvec_iter *bvec_pos)
267 struct ceph_osd_data *osd_data;
269 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
270 ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
272 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
275 * osd_req_op_extent_osd_iter - Set up an operation with an iterator buffer
276 * @osd_req: The request to set up
277 * @which: Index of the operation in which to set the iter
278 * @iter: The buffer iterator
280 void osd_req_op_extent_osd_iter(struct ceph_osd_request *osd_req,
281 unsigned int which, struct iov_iter *iter)
283 struct ceph_osd_data *osd_data;
285 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
286 ceph_osd_iter_init(osd_data, iter);
288 EXPORT_SYMBOL(osd_req_op_extent_osd_iter);
290 static void osd_req_op_cls_request_info_pagelist(
291 struct ceph_osd_request *osd_req,
292 unsigned int which, struct ceph_pagelist *pagelist)
294 struct ceph_osd_data *osd_data;
296 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
297 ceph_osd_data_pagelist_init(osd_data, pagelist);
300 void osd_req_op_cls_request_data_pagelist(
301 struct ceph_osd_request *osd_req,
302 unsigned int which, struct ceph_pagelist *pagelist)
304 struct ceph_osd_data *osd_data;
306 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
307 ceph_osd_data_pagelist_init(osd_data, pagelist);
308 osd_req->r_ops[which].cls.indata_len += pagelist->length;
309 osd_req->r_ops[which].indata_len += pagelist->length;
311 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
313 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
314 unsigned int which, struct page **pages, u64 length,
315 u32 alignment, bool pages_from_pool, bool own_pages)
317 struct ceph_osd_data *osd_data;
319 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
320 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
321 pages_from_pool, own_pages);
322 osd_req->r_ops[which].cls.indata_len += length;
323 osd_req->r_ops[which].indata_len += length;
325 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
327 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
329 struct bio_vec *bvecs, u32 num_bvecs,
332 struct ceph_osd_data *osd_data;
333 struct ceph_bvec_iter it = {
335 .iter = { .bi_size = bytes },
338 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
339 ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
340 osd_req->r_ops[which].cls.indata_len += bytes;
341 osd_req->r_ops[which].indata_len += bytes;
343 EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
345 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
346 unsigned int which, struct page **pages, u64 length,
347 u32 alignment, bool pages_from_pool, bool own_pages)
349 struct ceph_osd_data *osd_data;
351 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
352 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
353 pages_from_pool, own_pages);
355 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
357 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
359 switch (osd_data->type) {
360 case CEPH_OSD_DATA_TYPE_NONE:
362 case CEPH_OSD_DATA_TYPE_PAGES:
363 return osd_data->length;
364 case CEPH_OSD_DATA_TYPE_PAGELIST:
365 return (u64)osd_data->pagelist->length;
367 case CEPH_OSD_DATA_TYPE_BIO:
368 return (u64)osd_data->bio_length;
369 #endif /* CONFIG_BLOCK */
370 case CEPH_OSD_DATA_TYPE_BVECS:
371 return osd_data->bvec_pos.iter.bi_size;
372 case CEPH_OSD_DATA_TYPE_ITER:
373 return iov_iter_count(&osd_data->iter);
375 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
380 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
382 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
385 num_pages = calc_pages_for((u64)osd_data->alignment,
386 (u64)osd_data->length);
387 ceph_release_page_vector(osd_data->pages, num_pages);
388 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
389 ceph_pagelist_release(osd_data->pagelist);
391 ceph_osd_data_init(osd_data);
394 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
397 struct ceph_osd_req_op *op;
399 BUG_ON(which >= osd_req->r_num_ops);
400 op = &osd_req->r_ops[which];
403 case CEPH_OSD_OP_READ:
404 case CEPH_OSD_OP_SPARSE_READ:
405 case CEPH_OSD_OP_WRITE:
406 case CEPH_OSD_OP_WRITEFULL:
407 kfree(op->extent.sparse_ext);
408 ceph_osd_data_release(&op->extent.osd_data);
410 case CEPH_OSD_OP_CALL:
411 ceph_osd_data_release(&op->cls.request_info);
412 ceph_osd_data_release(&op->cls.request_data);
413 ceph_osd_data_release(&op->cls.response_data);
415 case CEPH_OSD_OP_SETXATTR:
416 case CEPH_OSD_OP_CMPXATTR:
417 ceph_osd_data_release(&op->xattr.osd_data);
419 case CEPH_OSD_OP_STAT:
420 ceph_osd_data_release(&op->raw_data_in);
422 case CEPH_OSD_OP_NOTIFY_ACK:
423 ceph_osd_data_release(&op->notify_ack.request_data);
425 case CEPH_OSD_OP_NOTIFY:
426 ceph_osd_data_release(&op->notify.request_data);
427 ceph_osd_data_release(&op->notify.response_data);
429 case CEPH_OSD_OP_LIST_WATCHERS:
430 ceph_osd_data_release(&op->list_watchers.response_data);
432 case CEPH_OSD_OP_COPY_FROM2:
433 ceph_osd_data_release(&op->copy_from.osd_data);
441 * Assumes @t is zero-initialized.
443 static void target_init(struct ceph_osd_request_target *t)
445 ceph_oid_init(&t->base_oid);
446 ceph_oloc_init(&t->base_oloc);
447 ceph_oid_init(&t->target_oid);
448 ceph_oloc_init(&t->target_oloc);
450 ceph_osds_init(&t->acting);
451 ceph_osds_init(&t->up);
455 t->osd = CEPH_HOMELESS_OSD;
458 static void target_copy(struct ceph_osd_request_target *dest,
459 const struct ceph_osd_request_target *src)
461 ceph_oid_copy(&dest->base_oid, &src->base_oid);
462 ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
463 ceph_oid_copy(&dest->target_oid, &src->target_oid);
464 ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
466 dest->pgid = src->pgid; /* struct */
467 dest->spgid = src->spgid; /* struct */
468 dest->pg_num = src->pg_num;
469 dest->pg_num_mask = src->pg_num_mask;
470 ceph_osds_copy(&dest->acting, &src->acting);
471 ceph_osds_copy(&dest->up, &src->up);
472 dest->size = src->size;
473 dest->min_size = src->min_size;
474 dest->sort_bitwise = src->sort_bitwise;
475 dest->recovery_deletes = src->recovery_deletes;
477 dest->flags = src->flags;
478 dest->used_replica = src->used_replica;
479 dest->paused = src->paused;
481 dest->epoch = src->epoch;
482 dest->last_force_resend = src->last_force_resend;
484 dest->osd = src->osd;
487 static void target_destroy(struct ceph_osd_request_target *t)
489 ceph_oid_destroy(&t->base_oid);
490 ceph_oloc_destroy(&t->base_oloc);
491 ceph_oid_destroy(&t->target_oid);
492 ceph_oloc_destroy(&t->target_oloc);
498 static void request_release_checks(struct ceph_osd_request *req)
500 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
501 WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
502 WARN_ON(!list_empty(&req->r_private_item));
506 static void ceph_osdc_release_request(struct kref *kref)
508 struct ceph_osd_request *req = container_of(kref,
509 struct ceph_osd_request, r_kref);
512 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
513 req->r_request, req->r_reply);
514 request_release_checks(req);
517 ceph_msg_put(req->r_request);
519 ceph_msg_put(req->r_reply);
521 for (which = 0; which < req->r_num_ops; which++)
522 osd_req_op_data_release(req, which);
524 target_destroy(&req->r_t);
525 ceph_put_snap_context(req->r_snapc);
528 mempool_free(req, req->r_osdc->req_mempool);
529 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
530 kmem_cache_free(ceph_osd_request_cache, req);
535 void ceph_osdc_get_request(struct ceph_osd_request *req)
537 dout("%s %p (was %d)\n", __func__, req,
538 kref_read(&req->r_kref));
539 kref_get(&req->r_kref);
541 EXPORT_SYMBOL(ceph_osdc_get_request);
543 void ceph_osdc_put_request(struct ceph_osd_request *req)
546 dout("%s %p (was %d)\n", __func__, req,
547 kref_read(&req->r_kref));
548 kref_put(&req->r_kref, ceph_osdc_release_request);
551 EXPORT_SYMBOL(ceph_osdc_put_request);
553 static void request_init(struct ceph_osd_request *req)
555 /* req only, each op is zeroed in osd_req_op_init() */
556 memset(req, 0, sizeof(*req));
558 kref_init(&req->r_kref);
559 init_completion(&req->r_completion);
560 RB_CLEAR_NODE(&req->r_node);
561 RB_CLEAR_NODE(&req->r_mc_node);
562 INIT_LIST_HEAD(&req->r_private_item);
564 target_init(&req->r_t);
567 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
568 struct ceph_snap_context *snapc,
569 unsigned int num_ops,
573 struct ceph_osd_request *req;
576 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
577 req = mempool_alloc(osdc->req_mempool, gfp_flags);
578 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
579 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
581 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
582 req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
589 req->r_mempool = use_mempool;
590 req->r_num_ops = num_ops;
591 req->r_snapid = CEPH_NOSNAP;
592 req->r_snapc = ceph_get_snap_context(snapc);
594 dout("%s req %p\n", __func__, req);
597 EXPORT_SYMBOL(ceph_osdc_alloc_request);
599 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
601 return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
604 static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
605 int num_request_data_items,
606 int num_reply_data_items)
608 struct ceph_osd_client *osdc = req->r_osdc;
609 struct ceph_msg *msg;
612 WARN_ON(req->r_request || req->r_reply);
613 WARN_ON(ceph_oid_empty(&req->r_base_oid));
614 WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
616 /* create request message */
617 msg_size = CEPH_ENCODING_START_BLK_LEN +
618 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
619 msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
620 msg_size += CEPH_ENCODING_START_BLK_LEN +
621 sizeof(struct ceph_osd_reqid); /* reqid */
622 msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
623 msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
624 msg_size += CEPH_ENCODING_START_BLK_LEN +
625 ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
626 msg_size += 4 + req->r_base_oid.name_len; /* oid */
627 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
628 msg_size += 8; /* snapid */
629 msg_size += 8; /* snap_seq */
630 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
631 msg_size += 4 + 8; /* retry_attempt, features */
634 msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
635 num_request_data_items);
637 msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
638 num_request_data_items, gfp, true);
642 memset(msg->front.iov_base, 0, msg->front.iov_len);
643 req->r_request = msg;
645 /* create reply message */
646 msg_size = OSD_OPREPLY_FRONT_LEN;
647 msg_size += req->r_base_oid.name_len;
648 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
651 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
652 num_reply_data_items);
654 msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
655 num_reply_data_items, gfp, true);
664 static bool osd_req_opcode_valid(u16 opcode)
667 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
668 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
675 static void get_num_data_items(struct ceph_osd_request *req,
676 int *num_request_data_items,
677 int *num_reply_data_items)
679 struct ceph_osd_req_op *op;
681 *num_request_data_items = 0;
682 *num_reply_data_items = 0;
684 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
687 case CEPH_OSD_OP_WRITE:
688 case CEPH_OSD_OP_WRITEFULL:
689 case CEPH_OSD_OP_SETXATTR:
690 case CEPH_OSD_OP_CMPXATTR:
691 case CEPH_OSD_OP_NOTIFY_ACK:
692 case CEPH_OSD_OP_COPY_FROM2:
693 *num_request_data_items += 1;
697 case CEPH_OSD_OP_STAT:
698 case CEPH_OSD_OP_READ:
699 case CEPH_OSD_OP_SPARSE_READ:
700 case CEPH_OSD_OP_LIST_WATCHERS:
701 *num_reply_data_items += 1;
705 case CEPH_OSD_OP_NOTIFY:
706 *num_request_data_items += 1;
707 *num_reply_data_items += 1;
709 case CEPH_OSD_OP_CALL:
710 *num_request_data_items += 2;
711 *num_reply_data_items += 1;
715 WARN_ON(!osd_req_opcode_valid(op->op));
722 * oid, oloc and OSD op opcode(s) must be filled in before this function
725 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
727 int num_request_data_items, num_reply_data_items;
729 get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
730 return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
731 num_reply_data_items);
733 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
736 * This is an osd op init function for opcodes that have no data or
737 * other information associated with them. It also serves as a
738 * common init routine for all the other init functions, below.
740 struct ceph_osd_req_op *
741 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
742 u16 opcode, u32 flags)
744 struct ceph_osd_req_op *op;
746 BUG_ON(which >= osd_req->r_num_ops);
747 BUG_ON(!osd_req_opcode_valid(opcode));
749 op = &osd_req->r_ops[which];
750 memset(op, 0, sizeof (*op));
756 EXPORT_SYMBOL(osd_req_op_init);
758 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
759 unsigned int which, u16 opcode,
760 u64 offset, u64 length,
761 u64 truncate_size, u32 truncate_seq)
763 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
765 size_t payload_len = 0;
767 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
768 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
769 opcode != CEPH_OSD_OP_TRUNCATE && opcode != CEPH_OSD_OP_SPARSE_READ);
771 op->extent.offset = offset;
772 op->extent.length = length;
773 op->extent.truncate_size = truncate_size;
774 op->extent.truncate_seq = truncate_seq;
775 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
776 payload_len += length;
778 op->indata_len = payload_len;
780 EXPORT_SYMBOL(osd_req_op_extent_init);
782 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
783 unsigned int which, u64 length)
785 struct ceph_osd_req_op *op;
788 BUG_ON(which >= osd_req->r_num_ops);
789 op = &osd_req->r_ops[which];
790 previous = op->extent.length;
792 if (length == previous)
793 return; /* Nothing to do */
794 BUG_ON(length > previous);
796 op->extent.length = length;
797 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
798 op->indata_len -= previous - length;
800 EXPORT_SYMBOL(osd_req_op_extent_update);
802 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
803 unsigned int which, u64 offset_inc)
805 struct ceph_osd_req_op *op, *prev_op;
807 BUG_ON(which + 1 >= osd_req->r_num_ops);
809 prev_op = &osd_req->r_ops[which];
810 op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
811 /* dup previous one */
812 op->indata_len = prev_op->indata_len;
813 op->outdata_len = prev_op->outdata_len;
814 op->extent = prev_op->extent;
816 op->extent.offset += offset_inc;
817 op->extent.length -= offset_inc;
819 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
820 op->indata_len -= offset_inc;
822 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
824 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
825 const char *class, const char *method)
827 struct ceph_osd_req_op *op;
828 struct ceph_pagelist *pagelist;
829 size_t payload_len = 0;
833 op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
835 pagelist = ceph_pagelist_alloc(GFP_NOFS);
839 op->cls.class_name = class;
840 size = strlen(class);
841 BUG_ON(size > (size_t) U8_MAX);
842 op->cls.class_len = size;
843 ret = ceph_pagelist_append(pagelist, class, size);
845 goto err_pagelist_free;
848 op->cls.method_name = method;
849 size = strlen(method);
850 BUG_ON(size > (size_t) U8_MAX);
851 op->cls.method_len = size;
852 ret = ceph_pagelist_append(pagelist, method, size);
854 goto err_pagelist_free;
857 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
858 op->indata_len = payload_len;
862 ceph_pagelist_release(pagelist);
865 EXPORT_SYMBOL(osd_req_op_cls_init);
867 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
868 u16 opcode, const char *name, const void *value,
869 size_t size, u8 cmp_op, u8 cmp_mode)
871 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
873 struct ceph_pagelist *pagelist;
877 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
879 pagelist = ceph_pagelist_alloc(GFP_NOFS);
883 payload_len = strlen(name);
884 op->xattr.name_len = payload_len;
885 ret = ceph_pagelist_append(pagelist, name, payload_len);
887 goto err_pagelist_free;
889 op->xattr.value_len = size;
890 ret = ceph_pagelist_append(pagelist, value, size);
892 goto err_pagelist_free;
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;
903 ceph_pagelist_release(pagelist);
906 EXPORT_SYMBOL(osd_req_op_xattr_init);
909 * @watch_opcode: CEPH_OSD_WATCH_OP_*
911 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
912 u8 watch_opcode, u64 cookie, u32 gen)
914 struct ceph_osd_req_op *op;
916 op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
917 op->watch.cookie = cookie;
918 op->watch.op = watch_opcode;
923 * prot_ver, timeout and notify payload (may be empty) should already be
924 * encoded in @request_pl
926 static void osd_req_op_notify_init(struct ceph_osd_request *req, int which,
927 u64 cookie, struct ceph_pagelist *request_pl)
929 struct ceph_osd_req_op *op;
931 op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
932 op->notify.cookie = cookie;
934 ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl);
935 op->indata_len = request_pl->length;
939 * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_*
941 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
943 u64 expected_object_size,
944 u64 expected_write_size,
947 struct ceph_osd_req_op *op;
949 op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0);
950 op->alloc_hint.expected_object_size = expected_object_size;
951 op->alloc_hint.expected_write_size = expected_write_size;
952 op->alloc_hint.flags = flags;
955 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
956 * not worth a feature bit. Set FAILOK per-op flag to make
957 * sure older osds don't trip over an unsupported opcode.
959 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
961 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
963 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
964 struct ceph_osd_data *osd_data)
966 u64 length = ceph_osd_data_length(osd_data);
968 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
969 BUG_ON(length > (u64) SIZE_MAX);
971 ceph_msg_data_add_pages(msg, osd_data->pages,
972 length, osd_data->alignment, false);
973 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
975 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
977 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
978 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
980 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
981 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
982 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_ITER) {
983 ceph_msg_data_add_iter(msg, &osd_data->iter);
985 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
989 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
990 const struct ceph_osd_req_op *src)
993 case CEPH_OSD_OP_STAT:
995 case CEPH_OSD_OP_READ:
996 case CEPH_OSD_OP_SPARSE_READ:
997 case CEPH_OSD_OP_WRITE:
998 case CEPH_OSD_OP_WRITEFULL:
999 case CEPH_OSD_OP_ZERO:
1000 case CEPH_OSD_OP_TRUNCATE:
1001 dst->extent.offset = cpu_to_le64(src->extent.offset);
1002 dst->extent.length = cpu_to_le64(src->extent.length);
1003 dst->extent.truncate_size =
1004 cpu_to_le64(src->extent.truncate_size);
1005 dst->extent.truncate_seq =
1006 cpu_to_le32(src->extent.truncate_seq);
1008 case CEPH_OSD_OP_CALL:
1009 dst->cls.class_len = src->cls.class_len;
1010 dst->cls.method_len = src->cls.method_len;
1011 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
1013 case CEPH_OSD_OP_WATCH:
1014 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
1015 dst->watch.ver = cpu_to_le64(0);
1016 dst->watch.op = src->watch.op;
1017 dst->watch.gen = cpu_to_le32(src->watch.gen);
1019 case CEPH_OSD_OP_NOTIFY_ACK:
1021 case CEPH_OSD_OP_NOTIFY:
1022 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
1024 case CEPH_OSD_OP_LIST_WATCHERS:
1026 case CEPH_OSD_OP_SETALLOCHINT:
1027 dst->alloc_hint.expected_object_size =
1028 cpu_to_le64(src->alloc_hint.expected_object_size);
1029 dst->alloc_hint.expected_write_size =
1030 cpu_to_le64(src->alloc_hint.expected_write_size);
1031 dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags);
1033 case CEPH_OSD_OP_SETXATTR:
1034 case CEPH_OSD_OP_CMPXATTR:
1035 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1036 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1037 dst->xattr.cmp_op = src->xattr.cmp_op;
1038 dst->xattr.cmp_mode = src->xattr.cmp_mode;
1040 case CEPH_OSD_OP_CREATE:
1041 case CEPH_OSD_OP_DELETE:
1043 case CEPH_OSD_OP_COPY_FROM2:
1044 dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
1045 dst->copy_from.src_version =
1046 cpu_to_le64(src->copy_from.src_version);
1047 dst->copy_from.flags = src->copy_from.flags;
1048 dst->copy_from.src_fadvise_flags =
1049 cpu_to_le32(src->copy_from.src_fadvise_flags);
1051 case CEPH_OSD_OP_ASSERT_VER:
1052 dst->assert_ver.unused = cpu_to_le64(0);
1053 dst->assert_ver.ver = cpu_to_le64(src->assert_ver.ver);
1056 pr_err("unsupported osd opcode %s\n",
1057 ceph_osd_op_name(src->op));
1063 dst->op = cpu_to_le16(src->op);
1064 dst->flags = cpu_to_le32(src->flags);
1065 dst->payload_len = cpu_to_le32(src->indata_len);
1067 return src->indata_len;
1071 * build new request AND message, calculate layout, and adjust file
1074 * if the file was recently truncated, we include information about its
1075 * old and new size so that the object can be updated appropriately. (we
1076 * avoid synchronously deleting truncated objects because it's slow.)
1078 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
1079 struct ceph_file_layout *layout,
1080 struct ceph_vino vino,
1082 unsigned int which, int num_ops,
1083 int opcode, int flags,
1084 struct ceph_snap_context *snapc,
1089 struct ceph_osd_request *req;
1095 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1096 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1097 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE &&
1098 opcode != CEPH_OSD_OP_SPARSE_READ);
1100 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1107 /* calculate max write size */
1108 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1112 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1113 osd_req_op_init(req, which, opcode, 0);
1115 u32 object_size = layout->object_size;
1116 u32 object_base = off - objoff;
1117 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1118 if (truncate_size <= object_base) {
1121 truncate_size -= object_base;
1122 if (truncate_size > object_size)
1123 truncate_size = object_size;
1126 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1127 truncate_size, truncate_seq);
1130 req->r_base_oloc.pool = layout->pool_id;
1131 req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1132 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1133 req->r_flags = flags | osdc->client->options->read_from_replica;
1135 req->r_snapid = vino.snap;
1136 if (flags & CEPH_OSD_FLAG_WRITE)
1137 req->r_data_offset = off;
1140 int num_req_ops, num_rep_ops;
1143 * If this is a multi-op write request, assume that we'll need
1144 * request ops. If it's a multi-op read then assume we'll need
1145 * reply ops. Anything else and call it -EINVAL.
1147 if (flags & CEPH_OSD_FLAG_WRITE) {
1148 num_req_ops = num_ops;
1150 } else if (flags & CEPH_OSD_FLAG_READ) {
1152 num_rep_ops = num_ops;
1158 r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_req_ops,
1161 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1169 ceph_osdc_put_request(req);
1172 EXPORT_SYMBOL(ceph_osdc_new_request);
1174 int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt)
1176 op->extent.sparse_ext_cnt = cnt;
1177 op->extent.sparse_ext = kmalloc_array(cnt,
1178 sizeof(*op->extent.sparse_ext),
1180 if (!op->extent.sparse_ext)
1184 EXPORT_SYMBOL(__ceph_alloc_sparse_ext_map);
1187 * We keep osd requests in an rbtree, sorted by ->r_tid.
1189 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1190 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1193 * Call @fn on each OSD request as long as @fn returns 0.
1195 static void for_each_request(struct ceph_osd_client *osdc,
1196 int (*fn)(struct ceph_osd_request *req, void *arg),
1199 struct rb_node *n, *p;
1201 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1202 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1204 for (p = rb_first(&osd->o_requests); p; ) {
1205 struct ceph_osd_request *req =
1206 rb_entry(p, struct ceph_osd_request, r_node);
1214 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1215 struct ceph_osd_request *req =
1216 rb_entry(p, struct ceph_osd_request, r_node);
1224 static bool osd_homeless(struct ceph_osd *osd)
1226 return osd->o_osd == CEPH_HOMELESS_OSD;
1229 static bool osd_registered(struct ceph_osd *osd)
1231 verify_osdc_locked(osd->o_osdc);
1233 return !RB_EMPTY_NODE(&osd->o_node);
1237 * Assumes @osd is zero-initialized.
1239 static void osd_init(struct ceph_osd *osd)
1241 refcount_set(&osd->o_ref, 1);
1242 RB_CLEAR_NODE(&osd->o_node);
1243 spin_lock_init(&osd->o_requests_lock);
1244 osd->o_requests = RB_ROOT;
1245 osd->o_linger_requests = RB_ROOT;
1246 osd->o_backoff_mappings = RB_ROOT;
1247 osd->o_backoffs_by_id = RB_ROOT;
1248 INIT_LIST_HEAD(&osd->o_osd_lru);
1249 INIT_LIST_HEAD(&osd->o_keepalive_item);
1250 osd->o_incarnation = 1;
1251 mutex_init(&osd->lock);
1254 static void ceph_init_sparse_read(struct ceph_sparse_read *sr)
1256 kfree(sr->sr_extent);
1257 memset(sr, '\0', sizeof(*sr));
1258 sr->sr_state = CEPH_SPARSE_READ_HDR;
1261 static void osd_cleanup(struct ceph_osd *osd)
1263 WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1264 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1265 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1266 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1267 WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1268 WARN_ON(!list_empty(&osd->o_osd_lru));
1269 WARN_ON(!list_empty(&osd->o_keepalive_item));
1271 ceph_init_sparse_read(&osd->o_sparse_read);
1273 if (osd->o_auth.authorizer) {
1274 WARN_ON(osd_homeless(osd));
1275 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1280 * Track open sessions with osds.
1282 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1284 struct ceph_osd *osd;
1286 WARN_ON(onum == CEPH_HOMELESS_OSD);
1288 osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1292 osd->o_sparse_op_idx = -1;
1294 ceph_init_sparse_read(&osd->o_sparse_read);
1296 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1301 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1303 if (refcount_inc_not_zero(&osd->o_ref)) {
1304 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1305 refcount_read(&osd->o_ref));
1308 dout("get_osd %p FAIL\n", osd);
1313 static void put_osd(struct ceph_osd *osd)
1315 dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1316 refcount_read(&osd->o_ref) - 1);
1317 if (refcount_dec_and_test(&osd->o_ref)) {
1323 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1325 static void __move_osd_to_lru(struct ceph_osd *osd)
1327 struct ceph_osd_client *osdc = osd->o_osdc;
1329 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1330 BUG_ON(!list_empty(&osd->o_osd_lru));
1332 spin_lock(&osdc->osd_lru_lock);
1333 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1334 spin_unlock(&osdc->osd_lru_lock);
1336 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1339 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1341 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1342 RB_EMPTY_ROOT(&osd->o_linger_requests))
1343 __move_osd_to_lru(osd);
1346 static void __remove_osd_from_lru(struct ceph_osd *osd)
1348 struct ceph_osd_client *osdc = osd->o_osdc;
1350 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1352 spin_lock(&osdc->osd_lru_lock);
1353 if (!list_empty(&osd->o_osd_lru))
1354 list_del_init(&osd->o_osd_lru);
1355 spin_unlock(&osdc->osd_lru_lock);
1359 * Close the connection and assign any leftover requests to the
1362 static void close_osd(struct ceph_osd *osd)
1364 struct ceph_osd_client *osdc = osd->o_osdc;
1367 verify_osdc_wrlocked(osdc);
1368 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1370 ceph_con_close(&osd->o_con);
1372 for (n = rb_first(&osd->o_requests); n; ) {
1373 struct ceph_osd_request *req =
1374 rb_entry(n, struct ceph_osd_request, r_node);
1376 n = rb_next(n); /* unlink_request() */
1378 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1379 unlink_request(osd, req);
1380 link_request(&osdc->homeless_osd, req);
1382 for (n = rb_first(&osd->o_linger_requests); n; ) {
1383 struct ceph_osd_linger_request *lreq =
1384 rb_entry(n, struct ceph_osd_linger_request, node);
1386 n = rb_next(n); /* unlink_linger() */
1388 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1390 unlink_linger(osd, lreq);
1391 link_linger(&osdc->homeless_osd, lreq);
1393 clear_backoffs(osd);
1395 __remove_osd_from_lru(osd);
1396 erase_osd(&osdc->osds, osd);
1403 static int reopen_osd(struct ceph_osd *osd)
1405 struct ceph_entity_addr *peer_addr;
1407 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1409 if (RB_EMPTY_ROOT(&osd->o_requests) &&
1410 RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1415 peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1416 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1417 !ceph_con_opened(&osd->o_con)) {
1420 dout("osd addr hasn't changed and connection never opened, "
1421 "letting msgr retry\n");
1422 /* touch each r_stamp for handle_timeout()'s benfit */
1423 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1424 struct ceph_osd_request *req =
1425 rb_entry(n, struct ceph_osd_request, r_node);
1426 req->r_stamp = jiffies;
1432 ceph_con_close(&osd->o_con);
1433 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1434 osd->o_incarnation++;
1439 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1442 struct ceph_osd *osd;
1445 verify_osdc_wrlocked(osdc);
1447 verify_osdc_locked(osdc);
1449 if (o != CEPH_HOMELESS_OSD)
1450 osd = lookup_osd(&osdc->osds, o);
1452 osd = &osdc->homeless_osd;
1455 return ERR_PTR(-EAGAIN);
1457 osd = create_osd(osdc, o);
1458 insert_osd(&osdc->osds, osd);
1459 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1460 &osdc->osdmap->osd_addr[osd->o_osd]);
1463 dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1468 * Create request <-> OSD session relation.
1470 * @req has to be assigned a tid, @osd may be homeless.
1472 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1474 verify_osd_locked(osd);
1475 WARN_ON(!req->r_tid || req->r_osd);
1476 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1479 if (!osd_homeless(osd))
1480 __remove_osd_from_lru(osd);
1482 atomic_inc(&osd->o_osdc->num_homeless);
1485 spin_lock(&osd->o_requests_lock);
1486 insert_request(&osd->o_requests, req);
1487 spin_unlock(&osd->o_requests_lock);
1491 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1493 verify_osd_locked(osd);
1494 WARN_ON(req->r_osd != osd);
1495 dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1499 spin_lock(&osd->o_requests_lock);
1500 erase_request(&osd->o_requests, req);
1501 spin_unlock(&osd->o_requests_lock);
1504 if (!osd_homeless(osd))
1505 maybe_move_osd_to_lru(osd);
1507 atomic_dec(&osd->o_osdc->num_homeless);
1510 static bool __pool_full(struct ceph_pg_pool_info *pi)
1512 return pi->flags & CEPH_POOL_FLAG_FULL;
1515 static bool have_pool_full(struct ceph_osd_client *osdc)
1519 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1520 struct ceph_pg_pool_info *pi =
1521 rb_entry(n, struct ceph_pg_pool_info, node);
1523 if (__pool_full(pi))
1530 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1532 struct ceph_pg_pool_info *pi;
1534 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1538 return __pool_full(pi);
1542 * Returns whether a request should be blocked from being sent
1543 * based on the current osdmap and osd_client settings.
1545 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1546 const struct ceph_osd_request_target *t,
1547 struct ceph_pg_pool_info *pi)
1549 bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1550 bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1551 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1554 WARN_ON(pi->id != t->target_oloc.pool);
1555 return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1556 ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1557 (osdc->osdmap->epoch < osdc->epoch_barrier);
1560 static int pick_random_replica(const struct ceph_osds *acting)
1562 int i = get_random_u32_below(acting->size);
1564 dout("%s picked osd%d, primary osd%d\n", __func__,
1565 acting->osds[i], acting->primary);
1570 * Picks the closest replica based on client's location given by
1571 * crush_location option. Prefers the primary if the locality is
1574 static int pick_closest_replica(struct ceph_osd_client *osdc,
1575 const struct ceph_osds *acting)
1577 struct ceph_options *opt = osdc->client->options;
1578 int best_i, best_locality;
1579 int i = 0, locality;
1582 locality = ceph_get_crush_locality(osdc->osdmap,
1586 (locality >= 0 && best_locality < 0) ||
1587 (locality >= 0 && best_locality >= 0 &&
1588 locality < best_locality)) {
1590 best_locality = locality;
1592 } while (++i < acting->size);
1594 dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
1595 acting->osds[best_i], best_locality, acting->primary);
1599 enum calc_target_result {
1600 CALC_TARGET_NO_ACTION = 0,
1601 CALC_TARGET_NEED_RESEND,
1602 CALC_TARGET_POOL_DNE,
1605 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1606 struct ceph_osd_request_target *t,
1609 struct ceph_pg_pool_info *pi;
1610 struct ceph_pg pgid, last_pgid;
1611 struct ceph_osds up, acting;
1612 bool is_read = t->flags & CEPH_OSD_FLAG_READ;
1613 bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
1614 bool force_resend = false;
1615 bool unpaused = false;
1616 bool legacy_change = false;
1618 bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1619 bool recovery_deletes = ceph_osdmap_flag(osdc,
1620 CEPH_OSDMAP_RECOVERY_DELETES);
1621 enum calc_target_result ct_res;
1623 t->epoch = osdc->osdmap->epoch;
1624 pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1626 t->osd = CEPH_HOMELESS_OSD;
1627 ct_res = CALC_TARGET_POOL_DNE;
1631 if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1632 if (t->last_force_resend < pi->last_force_request_resend) {
1633 t->last_force_resend = pi->last_force_request_resend;
1634 force_resend = true;
1635 } else if (t->last_force_resend == 0) {
1636 force_resend = true;
1641 ceph_oid_copy(&t->target_oid, &t->base_oid);
1642 ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1643 if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1644 if (is_read && pi->read_tier >= 0)
1645 t->target_oloc.pool = pi->read_tier;
1646 if (is_write && pi->write_tier >= 0)
1647 t->target_oloc.pool = pi->write_tier;
1649 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1651 t->osd = CEPH_HOMELESS_OSD;
1652 ct_res = CALC_TARGET_POOL_DNE;
1657 __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
1658 last_pgid.pool = pgid.pool;
1659 last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1661 ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1663 ceph_is_new_interval(&t->acting,
1675 t->recovery_deletes,
1678 force_resend = true;
1680 if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1684 legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1685 ceph_osds_changed(&t->acting, &acting,
1686 t->used_replica || any_change);
1688 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1690 if (legacy_change || force_resend || split) {
1691 t->pgid = pgid; /* struct */
1692 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1693 ceph_osds_copy(&t->acting, &acting);
1694 ceph_osds_copy(&t->up, &up);
1696 t->min_size = pi->min_size;
1697 t->pg_num = pi->pg_num;
1698 t->pg_num_mask = pi->pg_num_mask;
1699 t->sort_bitwise = sort_bitwise;
1700 t->recovery_deletes = recovery_deletes;
1702 if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
1703 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
1704 !is_write && pi->type == CEPH_POOL_TYPE_REP &&
1708 WARN_ON(!is_read || acting.osds[0] != acting.primary);
1709 if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
1710 pos = pick_random_replica(&acting);
1712 pos = pick_closest_replica(osdc, &acting);
1714 t->osd = acting.osds[pos];
1715 t->used_replica = pos > 0;
1717 t->osd = acting.primary;
1718 t->used_replica = false;
1722 if (unpaused || legacy_change || force_resend || split)
1723 ct_res = CALC_TARGET_NEED_RESEND;
1725 ct_res = CALC_TARGET_NO_ACTION;
1728 dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1729 legacy_change, force_resend, split, ct_res, t->osd);
1733 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1735 struct ceph_spg_mapping *spg;
1737 spg = kmalloc(sizeof(*spg), GFP_NOIO);
1741 RB_CLEAR_NODE(&spg->node);
1742 spg->backoffs = RB_ROOT;
1746 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1748 WARN_ON(!RB_EMPTY_NODE(&spg->node));
1749 WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1755 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1756 * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
1757 * defined only within a specific spgid; it does not pass anything to
1758 * children on split, or to another primary.
1760 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1761 RB_BYPTR, const struct ceph_spg *, node)
1763 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1765 return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1768 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1769 void **pkey, size_t *pkey_len)
1771 if (hoid->key_len) {
1773 *pkey_len = hoid->key_len;
1776 *pkey_len = hoid->oid_len;
1780 static int compare_names(const void *name1, size_t name1_len,
1781 const void *name2, size_t name2_len)
1785 ret = memcmp(name1, name2, min(name1_len, name2_len));
1787 if (name1_len < name2_len)
1789 else if (name1_len > name2_len)
1795 static int hoid_compare(const struct ceph_hobject_id *lhs,
1796 const struct ceph_hobject_id *rhs)
1798 void *effective_key1, *effective_key2;
1799 size_t effective_key1_len, effective_key2_len;
1802 if (lhs->is_max < rhs->is_max)
1804 if (lhs->is_max > rhs->is_max)
1807 if (lhs->pool < rhs->pool)
1809 if (lhs->pool > rhs->pool)
1812 if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1814 if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1817 ret = compare_names(lhs->nspace, lhs->nspace_len,
1818 rhs->nspace, rhs->nspace_len);
1822 hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1823 hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1824 ret = compare_names(effective_key1, effective_key1_len,
1825 effective_key2, effective_key2_len);
1829 ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1833 if (lhs->snapid < rhs->snapid)
1835 if (lhs->snapid > rhs->snapid)
1842 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1843 * compat stuff here.
1845 * Assumes @hoid is zero-initialized.
1847 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1853 ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1859 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1863 hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1865 if (IS_ERR(hoid->key)) {
1866 ret = PTR_ERR(hoid->key);
1871 hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1873 if (IS_ERR(hoid->oid)) {
1874 ret = PTR_ERR(hoid->oid);
1879 ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1880 ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1881 ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1883 hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1885 if (IS_ERR(hoid->nspace)) {
1886 ret = PTR_ERR(hoid->nspace);
1887 hoid->nspace = NULL;
1891 ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1893 ceph_hoid_build_hash_cache(hoid);
1900 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1902 return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1903 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1906 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1908 ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1909 ceph_encode_string(p, end, hoid->key, hoid->key_len);
1910 ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1911 ceph_encode_64(p, hoid->snapid);
1912 ceph_encode_32(p, hoid->hash);
1913 ceph_encode_8(p, hoid->is_max);
1914 ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1915 ceph_encode_64(p, hoid->pool);
1918 static void free_hoid(struct ceph_hobject_id *hoid)
1923 kfree(hoid->nspace);
1928 static struct ceph_osd_backoff *alloc_backoff(void)
1930 struct ceph_osd_backoff *backoff;
1932 backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1936 RB_CLEAR_NODE(&backoff->spg_node);
1937 RB_CLEAR_NODE(&backoff->id_node);
1941 static void free_backoff(struct ceph_osd_backoff *backoff)
1943 WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1944 WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1946 free_hoid(backoff->begin);
1947 free_hoid(backoff->end);
1952 * Within a specific spgid, backoffs are managed by ->begin hoid.
1954 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1955 RB_BYVAL, spg_node);
1957 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1958 const struct ceph_hobject_id *hoid)
1960 struct rb_node *n = root->rb_node;
1963 struct ceph_osd_backoff *cur =
1964 rb_entry(n, struct ceph_osd_backoff, spg_node);
1967 cmp = hoid_compare(hoid, cur->begin);
1970 } else if (cmp > 0) {
1971 if (hoid_compare(hoid, cur->end) < 0)
1984 * Each backoff has a unique id within its OSD session.
1986 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1988 static void clear_backoffs(struct ceph_osd *osd)
1990 while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1991 struct ceph_spg_mapping *spg =
1992 rb_entry(rb_first(&osd->o_backoff_mappings),
1993 struct ceph_spg_mapping, node);
1995 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1996 struct ceph_osd_backoff *backoff =
1997 rb_entry(rb_first(&spg->backoffs),
1998 struct ceph_osd_backoff, spg_node);
2000 erase_backoff(&spg->backoffs, backoff);
2001 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
2002 free_backoff(backoff);
2004 erase_spg_mapping(&osd->o_backoff_mappings, spg);
2005 free_spg_mapping(spg);
2010 * Set up a temporary, non-owning view into @t.
2012 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
2013 const struct ceph_osd_request_target *t)
2017 hoid->oid = t->target_oid.name;
2018 hoid->oid_len = t->target_oid.name_len;
2019 hoid->snapid = CEPH_NOSNAP;
2020 hoid->hash = t->pgid.seed;
2021 hoid->is_max = false;
2022 if (t->target_oloc.pool_ns) {
2023 hoid->nspace = t->target_oloc.pool_ns->str;
2024 hoid->nspace_len = t->target_oloc.pool_ns->len;
2026 hoid->nspace = NULL;
2027 hoid->nspace_len = 0;
2029 hoid->pool = t->target_oloc.pool;
2030 ceph_hoid_build_hash_cache(hoid);
2033 static bool should_plug_request(struct ceph_osd_request *req)
2035 struct ceph_osd *osd = req->r_osd;
2036 struct ceph_spg_mapping *spg;
2037 struct ceph_osd_backoff *backoff;
2038 struct ceph_hobject_id hoid;
2040 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
2044 hoid_fill_from_target(&hoid, &req->r_t);
2045 backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
2049 dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
2050 __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
2051 backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
2056 * Keep get_num_data_items() in sync with this function.
2058 static void setup_request_data(struct ceph_osd_request *req)
2060 struct ceph_msg *request_msg = req->r_request;
2061 struct ceph_msg *reply_msg = req->r_reply;
2062 struct ceph_osd_req_op *op;
2064 if (req->r_request->num_data_items || req->r_reply->num_data_items)
2067 WARN_ON(request_msg->data_length || reply_msg->data_length);
2068 for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
2071 case CEPH_OSD_OP_WRITE:
2072 case CEPH_OSD_OP_WRITEFULL:
2073 WARN_ON(op->indata_len != op->extent.length);
2074 ceph_osdc_msg_data_add(request_msg,
2075 &op->extent.osd_data);
2077 case CEPH_OSD_OP_SETXATTR:
2078 case CEPH_OSD_OP_CMPXATTR:
2079 WARN_ON(op->indata_len != op->xattr.name_len +
2080 op->xattr.value_len);
2081 ceph_osdc_msg_data_add(request_msg,
2082 &op->xattr.osd_data);
2084 case CEPH_OSD_OP_NOTIFY_ACK:
2085 ceph_osdc_msg_data_add(request_msg,
2086 &op->notify_ack.request_data);
2088 case CEPH_OSD_OP_COPY_FROM2:
2089 ceph_osdc_msg_data_add(request_msg,
2090 &op->copy_from.osd_data);
2094 case CEPH_OSD_OP_STAT:
2095 ceph_osdc_msg_data_add(reply_msg,
2098 case CEPH_OSD_OP_READ:
2099 case CEPH_OSD_OP_SPARSE_READ:
2100 ceph_osdc_msg_data_add(reply_msg,
2101 &op->extent.osd_data);
2103 case CEPH_OSD_OP_LIST_WATCHERS:
2104 ceph_osdc_msg_data_add(reply_msg,
2105 &op->list_watchers.response_data);
2109 case CEPH_OSD_OP_CALL:
2110 WARN_ON(op->indata_len != op->cls.class_len +
2111 op->cls.method_len +
2112 op->cls.indata_len);
2113 ceph_osdc_msg_data_add(request_msg,
2114 &op->cls.request_info);
2115 /* optional, can be NONE */
2116 ceph_osdc_msg_data_add(request_msg,
2117 &op->cls.request_data);
2118 /* optional, can be NONE */
2119 ceph_osdc_msg_data_add(reply_msg,
2120 &op->cls.response_data);
2122 case CEPH_OSD_OP_NOTIFY:
2123 ceph_osdc_msg_data_add(request_msg,
2124 &op->notify.request_data);
2125 ceph_osdc_msg_data_add(reply_msg,
2126 &op->notify.response_data);
2132 static void encode_pgid(void **p, const struct ceph_pg *pgid)
2134 ceph_encode_8(p, 1);
2135 ceph_encode_64(p, pgid->pool);
2136 ceph_encode_32(p, pgid->seed);
2137 ceph_encode_32(p, -1); /* preferred */
2140 static void encode_spgid(void **p, const struct ceph_spg *spgid)
2142 ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
2143 encode_pgid(p, &spgid->pgid);
2144 ceph_encode_8(p, spgid->shard);
2147 static void encode_oloc(void **p, void *end,
2148 const struct ceph_object_locator *oloc)
2150 ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
2151 ceph_encode_64(p, oloc->pool);
2152 ceph_encode_32(p, -1); /* preferred */
2153 ceph_encode_32(p, 0); /* key len */
2155 ceph_encode_string(p, end, oloc->pool_ns->str,
2156 oloc->pool_ns->len);
2158 ceph_encode_32(p, 0);
2161 static void encode_request_partial(struct ceph_osd_request *req,
2162 struct ceph_msg *msg)
2164 void *p = msg->front.iov_base;
2165 void *const end = p + msg->front_alloc_len;
2169 if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2170 /* snapshots aren't writeable */
2171 WARN_ON(req->r_snapid != CEPH_NOSNAP);
2173 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2174 req->r_data_offset || req->r_snapc);
2177 setup_request_data(req);
2179 encode_spgid(&p, &req->r_t.spgid); /* actual spg */
2180 ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2181 ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2182 ceph_encode_32(&p, req->r_flags);
2185 ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
2186 memset(p, 0, sizeof(struct ceph_osd_reqid));
2187 p += sizeof(struct ceph_osd_reqid);
2190 memset(p, 0, sizeof(struct ceph_blkin_trace_info));
2191 p += sizeof(struct ceph_blkin_trace_info);
2193 ceph_encode_32(&p, 0); /* client_inc, always 0 */
2194 ceph_encode_timespec64(p, &req->r_mtime);
2195 p += sizeof(struct ceph_timespec);
2197 encode_oloc(&p, end, &req->r_t.target_oloc);
2198 ceph_encode_string(&p, end, req->r_t.target_oid.name,
2199 req->r_t.target_oid.name_len);
2201 /* ops, can imply data */
2202 ceph_encode_16(&p, req->r_num_ops);
2203 for (i = 0; i < req->r_num_ops; i++) {
2204 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2205 p += sizeof(struct ceph_osd_op);
2208 ceph_encode_64(&p, req->r_snapid); /* snapid */
2210 ceph_encode_64(&p, req->r_snapc->seq);
2211 ceph_encode_32(&p, req->r_snapc->num_snaps);
2212 for (i = 0; i < req->r_snapc->num_snaps; i++)
2213 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2215 ceph_encode_64(&p, 0); /* snap_seq */
2216 ceph_encode_32(&p, 0); /* snaps len */
2219 ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2220 BUG_ON(p > end - 8); /* space for features */
2222 msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2223 /* front_len is finalized in encode_request_finish() */
2224 msg->front.iov_len = p - msg->front.iov_base;
2225 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2226 msg->hdr.data_len = cpu_to_le32(data_len);
2228 * The header "data_off" is a hint to the receiver allowing it
2229 * to align received data into its buffers such that there's no
2230 * need to re-copy it before writing it to disk (direct I/O).
2232 msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2234 dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2235 req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2238 static void encode_request_finish(struct ceph_msg *msg)
2240 void *p = msg->front.iov_base;
2241 void *const partial_end = p + msg->front.iov_len;
2242 void *const end = p + msg->front_alloc_len;
2244 if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2245 /* luminous OSD -- encode features and be done */
2247 ceph_encode_64(&p, msg->con->peer_features);
2250 char spgid[CEPH_ENCODING_START_BLK_LEN +
2251 CEPH_PGID_ENCODING_LEN + 1];
2255 char reqid[CEPH_ENCODING_START_BLK_LEN +
2256 sizeof(struct ceph_osd_reqid)];
2257 char trace[sizeof(struct ceph_blkin_trace_info)];
2259 struct ceph_timespec mtime;
2261 struct ceph_pg pgid;
2262 void *oloc, *oid, *tail;
2263 int oloc_len, oid_len, tail_len;
2267 * Pre-luminous OSD -- reencode v8 into v4 using @head
2268 * as a temporary buffer. Encode the raw PG; the rest
2269 * is just a matter of moving oloc, oid and tail blobs
2272 memcpy(&head, p, sizeof(head));
2276 p += CEPH_ENCODING_START_BLK_LEN;
2277 pgid.pool = ceph_decode_64(&p);
2278 p += 4 + 4; /* preferred, key len */
2279 len = ceph_decode_32(&p);
2280 p += len; /* nspace */
2281 oloc_len = p - oloc;
2284 len = ceph_decode_32(&p);
2289 tail_len = partial_end - p;
2291 p = msg->front.iov_base;
2292 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2293 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2294 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2295 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2297 /* reassert_version */
2298 memset(p, 0, sizeof(struct ceph_eversion));
2299 p += sizeof(struct ceph_eversion);
2302 memmove(p, oloc, oloc_len);
2305 pgid.seed = le32_to_cpu(head.hash);
2306 encode_pgid(&p, &pgid); /* raw pg */
2309 memmove(p, oid, oid_len);
2312 /* tail -- ops, snapid, snapc, retry_attempt */
2314 memmove(p, tail, tail_len);
2317 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2321 msg->front.iov_len = p - msg->front.iov_base;
2322 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2324 dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2325 le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2326 le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2327 le16_to_cpu(msg->hdr.version));
2331 * @req has to be assigned a tid and registered.
2333 static void send_request(struct ceph_osd_request *req)
2335 struct ceph_osd *osd = req->r_osd;
2337 verify_osd_locked(osd);
2338 WARN_ON(osd->o_osd != req->r_t.osd);
2341 if (should_plug_request(req))
2345 * We may have a previously queued request message hanging
2346 * around. Cancel it to avoid corrupting the msgr.
2349 ceph_msg_revoke(req->r_request);
2351 req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2352 if (req->r_attempts)
2353 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2355 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2357 encode_request_partial(req, req->r_request);
2359 dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2360 __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2361 req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2362 req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2365 req->r_t.paused = false;
2366 req->r_stamp = jiffies;
2369 req->r_sent = osd->o_incarnation;
2370 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2371 ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2374 static void maybe_request_map(struct ceph_osd_client *osdc)
2376 bool continuous = false;
2378 verify_osdc_locked(osdc);
2379 WARN_ON(!osdc->osdmap->epoch);
2381 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2382 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2383 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2384 dout("%s osdc %p continuous\n", __func__, osdc);
2387 dout("%s osdc %p onetime\n", __func__, osdc);
2390 if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2391 osdc->osdmap->epoch + 1, continuous))
2392 ceph_monc_renew_subs(&osdc->client->monc);
2395 static void complete_request(struct ceph_osd_request *req, int err);
2396 static void send_map_check(struct ceph_osd_request *req);
2398 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2400 struct ceph_osd_client *osdc = req->r_osdc;
2401 struct ceph_osd *osd;
2402 enum calc_target_result ct_res;
2404 bool need_send = false;
2405 bool promoted = false;
2407 WARN_ON(req->r_tid);
2408 dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2411 ct_res = calc_target(osdc, &req->r_t, false);
2412 if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2415 osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2417 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2421 if (osdc->abort_err) {
2422 dout("req %p abort_err %d\n", req, osdc->abort_err);
2423 err = osdc->abort_err;
2424 } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2425 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2426 osdc->epoch_barrier);
2427 req->r_t.paused = true;
2428 maybe_request_map(osdc);
2429 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2430 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2431 dout("req %p pausewr\n", req);
2432 req->r_t.paused = true;
2433 maybe_request_map(osdc);
2434 } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2435 ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2436 dout("req %p pauserd\n", req);
2437 req->r_t.paused = true;
2438 maybe_request_map(osdc);
2439 } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2440 !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2441 CEPH_OSD_FLAG_FULL_FORCE)) &&
2442 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2443 pool_full(osdc, req->r_t.base_oloc.pool))) {
2444 dout("req %p full/pool_full\n", req);
2445 if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
2448 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
2449 pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
2451 pr_warn_ratelimited("pool %lld is full or reached quota\n",
2452 req->r_t.base_oloc.pool);
2453 req->r_t.paused = true;
2454 maybe_request_map(osdc);
2456 } else if (!osd_homeless(osd)) {
2459 maybe_request_map(osdc);
2462 mutex_lock(&osd->lock);
2464 * Assign the tid atomically with send_request() to protect
2465 * multiple writes to the same object from racing with each
2466 * other, resulting in out of order ops on the OSDs.
2468 req->r_tid = atomic64_inc_return(&osdc->last_tid);
2469 link_request(osd, req);
2473 complete_request(req, err);
2474 mutex_unlock(&osd->lock);
2476 if (!err && ct_res == CALC_TARGET_POOL_DNE)
2477 send_map_check(req);
2480 downgrade_write(&osdc->lock);
2484 up_read(&osdc->lock);
2485 down_write(&osdc->lock);
2491 static void account_request(struct ceph_osd_request *req)
2493 WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2494 WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2496 req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2497 atomic_inc(&req->r_osdc->num_requests);
2499 req->r_start_stamp = jiffies;
2500 req->r_start_latency = ktime_get();
2503 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2505 ceph_osdc_get_request(req);
2506 account_request(req);
2507 __submit_request(req, wrlocked);
2510 static void finish_request(struct ceph_osd_request *req)
2512 struct ceph_osd_client *osdc = req->r_osdc;
2514 WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2515 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2517 req->r_end_latency = ktime_get();
2520 ceph_init_sparse_read(&req->r_osd->o_sparse_read);
2521 unlink_request(req->r_osd, req);
2523 atomic_dec(&osdc->num_requests);
2526 * If an OSD has failed or returned and a request has been sent
2527 * twice, it's possible to get a reply and end up here while the
2528 * request message is queued for delivery. We will ignore the
2529 * reply, so not a big deal, but better to try and catch it.
2531 ceph_msg_revoke(req->r_request);
2532 ceph_msg_revoke_incoming(req->r_reply);
2535 static void __complete_request(struct ceph_osd_request *req)
2537 dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2538 req->r_tid, req->r_callback, req->r_result);
2540 if (req->r_callback)
2541 req->r_callback(req);
2542 complete_all(&req->r_completion);
2543 ceph_osdc_put_request(req);
2546 static void complete_request_workfn(struct work_struct *work)
2548 struct ceph_osd_request *req =
2549 container_of(work, struct ceph_osd_request, r_complete_work);
2551 __complete_request(req);
2555 * This is open-coded in handle_reply().
2557 static void complete_request(struct ceph_osd_request *req, int err)
2559 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2561 req->r_result = err;
2562 finish_request(req);
2564 INIT_WORK(&req->r_complete_work, complete_request_workfn);
2565 queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
2568 static void cancel_map_check(struct ceph_osd_request *req)
2570 struct ceph_osd_client *osdc = req->r_osdc;
2571 struct ceph_osd_request *lookup_req;
2573 verify_osdc_wrlocked(osdc);
2575 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2579 WARN_ON(lookup_req != req);
2580 erase_request_mc(&osdc->map_checks, req);
2581 ceph_osdc_put_request(req);
2584 static void cancel_request(struct ceph_osd_request *req)
2586 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2588 cancel_map_check(req);
2589 finish_request(req);
2590 complete_all(&req->r_completion);
2591 ceph_osdc_put_request(req);
2594 static void abort_request(struct ceph_osd_request *req, int err)
2596 dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2598 cancel_map_check(req);
2599 complete_request(req, err);
2602 static int abort_fn(struct ceph_osd_request *req, void *arg)
2604 int err = *(int *)arg;
2606 abort_request(req, err);
2607 return 0; /* continue iteration */
2611 * Abort all in-flight requests with @err and arrange for all future
2612 * requests to be failed immediately.
2614 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2616 dout("%s osdc %p err %d\n", __func__, osdc, err);
2617 down_write(&osdc->lock);
2618 for_each_request(osdc, abort_fn, &err);
2619 osdc->abort_err = err;
2620 up_write(&osdc->lock);
2622 EXPORT_SYMBOL(ceph_osdc_abort_requests);
2624 void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
2626 down_write(&osdc->lock);
2627 osdc->abort_err = 0;
2628 up_write(&osdc->lock);
2630 EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
2632 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2634 if (likely(eb > osdc->epoch_barrier)) {
2635 dout("updating epoch_barrier from %u to %u\n",
2636 osdc->epoch_barrier, eb);
2637 osdc->epoch_barrier = eb;
2638 /* Request map if we're not to the barrier yet */
2639 if (eb > osdc->osdmap->epoch)
2640 maybe_request_map(osdc);
2644 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2646 down_read(&osdc->lock);
2647 if (unlikely(eb > osdc->epoch_barrier)) {
2648 up_read(&osdc->lock);
2649 down_write(&osdc->lock);
2650 update_epoch_barrier(osdc, eb);
2651 up_write(&osdc->lock);
2653 up_read(&osdc->lock);
2656 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2659 * We can end up releasing caps as a result of abort_request().
2660 * In that case, we probably want to ensure that the cap release message
2661 * has an updated epoch barrier in it, so set the epoch barrier prior to
2662 * aborting the first request.
2664 static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2666 struct ceph_osd_client *osdc = req->r_osdc;
2667 bool *victims = arg;
2669 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2670 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2671 pool_full(osdc, req->r_t.base_oloc.pool))) {
2673 update_epoch_barrier(osdc, osdc->osdmap->epoch);
2676 abort_request(req, -ENOSPC);
2679 return 0; /* continue iteration */
2683 * Drop all pending requests that are stalled waiting on a full condition to
2684 * clear, and complete them with ENOSPC as the return code. Set the
2685 * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2688 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2690 bool victims = false;
2692 if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2693 (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
2694 for_each_request(osdc, abort_on_full_fn, &victims);
2697 static void check_pool_dne(struct ceph_osd_request *req)
2699 struct ceph_osd_client *osdc = req->r_osdc;
2700 struct ceph_osdmap *map = osdc->osdmap;
2702 verify_osdc_wrlocked(osdc);
2703 WARN_ON(!map->epoch);
2705 if (req->r_attempts) {
2707 * We sent a request earlier, which means that
2708 * previously the pool existed, and now it does not
2709 * (i.e., it was deleted).
2711 req->r_map_dne_bound = map->epoch;
2712 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2715 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2716 req, req->r_tid, req->r_map_dne_bound, map->epoch);
2719 if (req->r_map_dne_bound) {
2720 if (map->epoch >= req->r_map_dne_bound) {
2721 /* we had a new enough map */
2722 pr_info_ratelimited("tid %llu pool does not exist\n",
2724 complete_request(req, -ENOENT);
2727 send_map_check(req);
2731 static void map_check_cb(struct ceph_mon_generic_request *greq)
2733 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2734 struct ceph_osd_request *req;
2735 u64 tid = greq->private_data;
2737 WARN_ON(greq->result || !greq->u.newest);
2739 down_write(&osdc->lock);
2740 req = lookup_request_mc(&osdc->map_checks, tid);
2742 dout("%s tid %llu dne\n", __func__, tid);
2746 dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2747 req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2748 if (!req->r_map_dne_bound)
2749 req->r_map_dne_bound = greq->u.newest;
2750 erase_request_mc(&osdc->map_checks, req);
2751 check_pool_dne(req);
2753 ceph_osdc_put_request(req);
2755 up_write(&osdc->lock);
2758 static void send_map_check(struct ceph_osd_request *req)
2760 struct ceph_osd_client *osdc = req->r_osdc;
2761 struct ceph_osd_request *lookup_req;
2764 verify_osdc_wrlocked(osdc);
2766 lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2768 WARN_ON(lookup_req != req);
2772 ceph_osdc_get_request(req);
2773 insert_request_mc(&osdc->map_checks, req);
2774 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2775 map_check_cb, req->r_tid);
2780 * lingering requests, watch/notify v2 infrastructure
2782 static void linger_release(struct kref *kref)
2784 struct ceph_osd_linger_request *lreq =
2785 container_of(kref, struct ceph_osd_linger_request, kref);
2787 dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2788 lreq->reg_req, lreq->ping_req);
2789 WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2790 WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2791 WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2792 WARN_ON(!list_empty(&lreq->scan_item));
2793 WARN_ON(!list_empty(&lreq->pending_lworks));
2796 if (lreq->request_pl)
2797 ceph_pagelist_release(lreq->request_pl);
2798 if (lreq->notify_id_pages)
2799 ceph_release_page_vector(lreq->notify_id_pages, 1);
2801 ceph_osdc_put_request(lreq->reg_req);
2802 ceph_osdc_put_request(lreq->ping_req);
2803 target_destroy(&lreq->t);
2807 static void linger_put(struct ceph_osd_linger_request *lreq)
2810 kref_put(&lreq->kref, linger_release);
2813 static struct ceph_osd_linger_request *
2814 linger_get(struct ceph_osd_linger_request *lreq)
2816 kref_get(&lreq->kref);
2820 static struct ceph_osd_linger_request *
2821 linger_alloc(struct ceph_osd_client *osdc)
2823 struct ceph_osd_linger_request *lreq;
2825 lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2829 kref_init(&lreq->kref);
2830 mutex_init(&lreq->lock);
2831 RB_CLEAR_NODE(&lreq->node);
2832 RB_CLEAR_NODE(&lreq->osdc_node);
2833 RB_CLEAR_NODE(&lreq->mc_node);
2834 INIT_LIST_HEAD(&lreq->scan_item);
2835 INIT_LIST_HEAD(&lreq->pending_lworks);
2836 init_completion(&lreq->reg_commit_wait);
2837 init_completion(&lreq->notify_finish_wait);
2840 target_init(&lreq->t);
2842 dout("%s lreq %p\n", __func__, lreq);
2846 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2847 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2848 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2851 * Create linger request <-> OSD session relation.
2853 * @lreq has to be registered, @osd may be homeless.
2855 static void link_linger(struct ceph_osd *osd,
2856 struct ceph_osd_linger_request *lreq)
2858 verify_osd_locked(osd);
2859 WARN_ON(!lreq->linger_id || lreq->osd);
2860 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2861 osd->o_osd, lreq, lreq->linger_id);
2863 if (!osd_homeless(osd))
2864 __remove_osd_from_lru(osd);
2866 atomic_inc(&osd->o_osdc->num_homeless);
2869 insert_linger(&osd->o_linger_requests, lreq);
2873 static void unlink_linger(struct ceph_osd *osd,
2874 struct ceph_osd_linger_request *lreq)
2876 verify_osd_locked(osd);
2877 WARN_ON(lreq->osd != osd);
2878 dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2879 osd->o_osd, lreq, lreq->linger_id);
2882 erase_linger(&osd->o_linger_requests, lreq);
2885 if (!osd_homeless(osd))
2886 maybe_move_osd_to_lru(osd);
2888 atomic_dec(&osd->o_osdc->num_homeless);
2891 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2893 verify_osdc_locked(lreq->osdc);
2895 return !RB_EMPTY_NODE(&lreq->osdc_node);
2898 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2900 struct ceph_osd_client *osdc = lreq->osdc;
2903 down_read(&osdc->lock);
2904 registered = __linger_registered(lreq);
2905 up_read(&osdc->lock);
2910 static void linger_register(struct ceph_osd_linger_request *lreq)
2912 struct ceph_osd_client *osdc = lreq->osdc;
2914 verify_osdc_wrlocked(osdc);
2915 WARN_ON(lreq->linger_id);
2918 lreq->linger_id = ++osdc->last_linger_id;
2919 insert_linger_osdc(&osdc->linger_requests, lreq);
2922 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2924 struct ceph_osd_client *osdc = lreq->osdc;
2926 verify_osdc_wrlocked(osdc);
2928 erase_linger_osdc(&osdc->linger_requests, lreq);
2932 static void cancel_linger_request(struct ceph_osd_request *req)
2934 struct ceph_osd_linger_request *lreq = req->r_priv;
2936 WARN_ON(!req->r_linger);
2937 cancel_request(req);
2941 struct linger_work {
2942 struct work_struct work;
2943 struct ceph_osd_linger_request *lreq;
2944 struct list_head pending_item;
2945 unsigned long queued_stamp;
2951 void *payload; /* points into @msg front */
2954 struct ceph_msg *msg; /* for ceph_msg_put() */
2962 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2965 struct linger_work *lwork;
2967 lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2971 INIT_WORK(&lwork->work, workfn);
2972 INIT_LIST_HEAD(&lwork->pending_item);
2973 lwork->lreq = linger_get(lreq);
2978 static void lwork_free(struct linger_work *lwork)
2980 struct ceph_osd_linger_request *lreq = lwork->lreq;
2982 mutex_lock(&lreq->lock);
2983 list_del(&lwork->pending_item);
2984 mutex_unlock(&lreq->lock);
2990 static void lwork_queue(struct linger_work *lwork)
2992 struct ceph_osd_linger_request *lreq = lwork->lreq;
2993 struct ceph_osd_client *osdc = lreq->osdc;
2995 verify_lreq_locked(lreq);
2996 WARN_ON(!list_empty(&lwork->pending_item));
2998 lwork->queued_stamp = jiffies;
2999 list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
3000 queue_work(osdc->notify_wq, &lwork->work);
3003 static void do_watch_notify(struct work_struct *w)
3005 struct linger_work *lwork = container_of(w, struct linger_work, work);
3006 struct ceph_osd_linger_request *lreq = lwork->lreq;
3008 if (!linger_registered(lreq)) {
3009 dout("%s lreq %p not registered\n", __func__, lreq);
3013 WARN_ON(!lreq->is_watch);
3014 dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
3015 __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
3016 lwork->notify.payload_len);
3017 lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
3018 lwork->notify.notifier_id, lwork->notify.payload,
3019 lwork->notify.payload_len);
3022 ceph_msg_put(lwork->notify.msg);
3026 static void do_watch_error(struct work_struct *w)
3028 struct linger_work *lwork = container_of(w, struct linger_work, work);
3029 struct ceph_osd_linger_request *lreq = lwork->lreq;
3031 if (!linger_registered(lreq)) {
3032 dout("%s lreq %p not registered\n", __func__, lreq);
3036 dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
3037 lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
3043 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
3045 struct linger_work *lwork;
3047 lwork = lwork_alloc(lreq, do_watch_error);
3049 pr_err("failed to allocate error-lwork\n");
3053 lwork->error.err = lreq->last_error;
3057 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
3060 if (!completion_done(&lreq->reg_commit_wait)) {
3061 lreq->reg_commit_error = (result <= 0 ? result : 0);
3062 complete_all(&lreq->reg_commit_wait);
3066 static void linger_commit_cb(struct ceph_osd_request *req)
3068 struct ceph_osd_linger_request *lreq = req->r_priv;
3070 mutex_lock(&lreq->lock);
3071 if (req != lreq->reg_req) {
3072 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
3073 __func__, lreq, lreq->linger_id, req, lreq->reg_req);
3077 dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
3078 lreq->linger_id, req->r_result);
3079 linger_reg_commit_complete(lreq, req->r_result);
3080 lreq->committed = true;
3082 if (!lreq->is_watch) {
3083 struct ceph_osd_data *osd_data =
3084 osd_req_op_data(req, 0, notify, response_data);
3085 void *p = page_address(osd_data->pages[0]);
3087 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
3088 osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
3090 /* make note of the notify_id */
3091 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
3092 lreq->notify_id = ceph_decode_64(&p);
3093 dout("lreq %p notify_id %llu\n", lreq,
3096 dout("lreq %p no notify_id\n", lreq);
3101 mutex_unlock(&lreq->lock);
3105 static int normalize_watch_error(int err)
3108 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
3109 * notification and a failure to reconnect because we raced with
3110 * the delete appear the same to the user.
3118 static void linger_reconnect_cb(struct ceph_osd_request *req)
3120 struct ceph_osd_linger_request *lreq = req->r_priv;
3122 mutex_lock(&lreq->lock);
3123 if (req != lreq->reg_req) {
3124 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
3125 __func__, lreq, lreq->linger_id, req, lreq->reg_req);
3129 dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
3130 lreq, lreq->linger_id, req->r_result, lreq->last_error);
3131 if (req->r_result < 0) {
3132 if (!lreq->last_error) {
3133 lreq->last_error = normalize_watch_error(req->r_result);
3134 queue_watch_error(lreq);
3139 mutex_unlock(&lreq->lock);
3143 static void send_linger(struct ceph_osd_linger_request *lreq)
3145 struct ceph_osd_client *osdc = lreq->osdc;
3146 struct ceph_osd_request *req;
3149 verify_osdc_wrlocked(osdc);
3150 mutex_lock(&lreq->lock);
3151 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3153 if (lreq->reg_req) {
3154 if (lreq->reg_req->r_osd)
3155 cancel_linger_request(lreq->reg_req);
3156 ceph_osdc_put_request(lreq->reg_req);
3159 req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
3162 target_copy(&req->r_t, &lreq->t);
3163 req->r_mtime = lreq->mtime;
3165 if (lreq->is_watch && lreq->committed) {
3166 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
3167 lreq->linger_id, ++lreq->register_gen);
3168 dout("lreq %p reconnect register_gen %u\n", lreq,
3169 req->r_ops[0].watch.gen);
3170 req->r_callback = linger_reconnect_cb;
3172 if (lreq->is_watch) {
3173 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
3174 lreq->linger_id, 0);
3176 lreq->notify_id = 0;
3178 refcount_inc(&lreq->request_pl->refcnt);
3179 osd_req_op_notify_init(req, 0, lreq->linger_id,
3181 ceph_osd_data_pages_init(
3182 osd_req_op_data(req, 0, notify, response_data),
3183 lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
3185 dout("lreq %p register\n", lreq);
3186 req->r_callback = linger_commit_cb;
3189 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3192 req->r_priv = linger_get(lreq);
3193 req->r_linger = true;
3194 lreq->reg_req = req;
3195 mutex_unlock(&lreq->lock);
3197 submit_request(req, true);
3200 static void linger_ping_cb(struct ceph_osd_request *req)
3202 struct ceph_osd_linger_request *lreq = req->r_priv;
3204 mutex_lock(&lreq->lock);
3205 if (req != lreq->ping_req) {
3206 dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
3207 __func__, lreq, lreq->linger_id, req, lreq->ping_req);
3211 dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3212 __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3214 if (lreq->register_gen == req->r_ops[0].watch.gen) {
3215 if (!req->r_result) {
3216 lreq->watch_valid_thru = lreq->ping_sent;
3217 } else if (!lreq->last_error) {
3218 lreq->last_error = normalize_watch_error(req->r_result);
3219 queue_watch_error(lreq);
3222 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3223 lreq->register_gen, req->r_ops[0].watch.gen);
3227 mutex_unlock(&lreq->lock);
3231 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3233 struct ceph_osd_client *osdc = lreq->osdc;
3234 struct ceph_osd_request *req;
3237 if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3238 dout("%s PAUSERD\n", __func__);
3242 lreq->ping_sent = jiffies;
3243 dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3244 __func__, lreq, lreq->linger_id, lreq->ping_sent,
3245 lreq->register_gen);
3247 if (lreq->ping_req) {
3248 if (lreq->ping_req->r_osd)
3249 cancel_linger_request(lreq->ping_req);
3250 ceph_osdc_put_request(lreq->ping_req);
3253 req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
3256 target_copy(&req->r_t, &lreq->t);
3257 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
3258 lreq->register_gen);
3259 req->r_callback = linger_ping_cb;
3261 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3264 req->r_priv = linger_get(lreq);
3265 req->r_linger = true;
3266 lreq->ping_req = req;
3268 ceph_osdc_get_request(req);
3269 account_request(req);
3270 req->r_tid = atomic64_inc_return(&osdc->last_tid);
3271 link_request(lreq->osd, req);
3275 static void linger_submit(struct ceph_osd_linger_request *lreq)
3277 struct ceph_osd_client *osdc = lreq->osdc;
3278 struct ceph_osd *osd;
3280 down_write(&osdc->lock);
3281 linger_register(lreq);
3283 calc_target(osdc, &lreq->t, false);
3284 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3285 link_linger(osd, lreq);
3288 up_write(&osdc->lock);
3291 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3293 struct ceph_osd_client *osdc = lreq->osdc;
3294 struct ceph_osd_linger_request *lookup_lreq;
3296 verify_osdc_wrlocked(osdc);
3298 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3303 WARN_ON(lookup_lreq != lreq);
3304 erase_linger_mc(&osdc->linger_map_checks, lreq);
3309 * @lreq has to be both registered and linked.
3311 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3313 if (lreq->ping_req && lreq->ping_req->r_osd)
3314 cancel_linger_request(lreq->ping_req);
3315 if (lreq->reg_req && lreq->reg_req->r_osd)
3316 cancel_linger_request(lreq->reg_req);
3317 cancel_linger_map_check(lreq);
3318 unlink_linger(lreq->osd, lreq);
3319 linger_unregister(lreq);
3322 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3324 struct ceph_osd_client *osdc = lreq->osdc;
3326 down_write(&osdc->lock);
3327 if (__linger_registered(lreq))
3328 __linger_cancel(lreq);
3329 up_write(&osdc->lock);
3332 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3334 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3336 struct ceph_osd_client *osdc = lreq->osdc;
3337 struct ceph_osdmap *map = osdc->osdmap;
3339 verify_osdc_wrlocked(osdc);
3340 WARN_ON(!map->epoch);
3342 if (lreq->register_gen) {
3343 lreq->map_dne_bound = map->epoch;
3344 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3345 lreq, lreq->linger_id);
3347 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3348 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3352 if (lreq->map_dne_bound) {
3353 if (map->epoch >= lreq->map_dne_bound) {
3354 /* we had a new enough map */
3355 pr_info("linger_id %llu pool does not exist\n",
3357 linger_reg_commit_complete(lreq, -ENOENT);
3358 __linger_cancel(lreq);
3361 send_linger_map_check(lreq);
3365 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3367 struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3368 struct ceph_osd_linger_request *lreq;
3369 u64 linger_id = greq->private_data;
3371 WARN_ON(greq->result || !greq->u.newest);
3373 down_write(&osdc->lock);
3374 lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3376 dout("%s linger_id %llu dne\n", __func__, linger_id);
3380 dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3381 __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3383 if (!lreq->map_dne_bound)
3384 lreq->map_dne_bound = greq->u.newest;
3385 erase_linger_mc(&osdc->linger_map_checks, lreq);
3386 check_linger_pool_dne(lreq);
3390 up_write(&osdc->lock);
3393 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3395 struct ceph_osd_client *osdc = lreq->osdc;
3396 struct ceph_osd_linger_request *lookup_lreq;
3399 verify_osdc_wrlocked(osdc);
3401 lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3404 WARN_ON(lookup_lreq != lreq);
3409 insert_linger_mc(&osdc->linger_map_checks, lreq);
3410 ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3411 linger_map_check_cb, lreq->linger_id);
3415 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3419 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3420 ret = wait_for_completion_killable(&lreq->reg_commit_wait);
3421 return ret ?: lreq->reg_commit_error;
3424 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
3425 unsigned long timeout)
3429 dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3430 left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
3431 ceph_timeout_jiffies(timeout));
3433 left = left ?: -ETIMEDOUT;
3435 left = lreq->notify_finish_error; /* completed */
3441 * Timeout callback, called every N seconds. When 1 or more OSD
3442 * requests has been active for more than N seconds, we send a keepalive
3443 * (tag + timestamp) to its OSD to ensure any communications channel
3444 * reset is detected.
3446 static void handle_timeout(struct work_struct *work)
3448 struct ceph_osd_client *osdc =
3449 container_of(work, struct ceph_osd_client, timeout_work.work);
3450 struct ceph_options *opts = osdc->client->options;
3451 unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3452 unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3453 LIST_HEAD(slow_osds);
3454 struct rb_node *n, *p;
3456 dout("%s osdc %p\n", __func__, osdc);
3457 down_write(&osdc->lock);
3460 * ping osds that are a bit slow. this ensures that if there
3461 * is a break in the TCP connection we will notice, and reopen
3462 * a connection with that osd (from the fault callback).
3464 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3465 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3468 for (p = rb_first(&osd->o_requests); p; ) {
3469 struct ceph_osd_request *req =
3470 rb_entry(p, struct ceph_osd_request, r_node);
3472 p = rb_next(p); /* abort_request() */
3474 if (time_before(req->r_stamp, cutoff)) {
3475 dout(" req %p tid %llu on osd%d is laggy\n",
3476 req, req->r_tid, osd->o_osd);
3479 if (opts->osd_request_timeout &&
3480 time_before(req->r_start_stamp, expiry_cutoff)) {
3481 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3482 req->r_tid, osd->o_osd);
3483 abort_request(req, -ETIMEDOUT);
3486 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3487 struct ceph_osd_linger_request *lreq =
3488 rb_entry(p, struct ceph_osd_linger_request, node);
3490 dout(" lreq %p linger_id %llu is served by osd%d\n",
3491 lreq, lreq->linger_id, osd->o_osd);
3494 mutex_lock(&lreq->lock);
3495 if (lreq->is_watch && lreq->committed && !lreq->last_error)
3496 send_linger_ping(lreq);
3497 mutex_unlock(&lreq->lock);
3501 list_move_tail(&osd->o_keepalive_item, &slow_osds);
3504 if (opts->osd_request_timeout) {
3505 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3506 struct ceph_osd_request *req =
3507 rb_entry(p, struct ceph_osd_request, r_node);
3509 p = rb_next(p); /* abort_request() */
3511 if (time_before(req->r_start_stamp, expiry_cutoff)) {
3512 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3513 req->r_tid, osdc->homeless_osd.o_osd);
3514 abort_request(req, -ETIMEDOUT);
3519 if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3520 maybe_request_map(osdc);
3522 while (!list_empty(&slow_osds)) {
3523 struct ceph_osd *osd = list_first_entry(&slow_osds,
3526 list_del_init(&osd->o_keepalive_item);
3527 ceph_con_keepalive(&osd->o_con);
3530 up_write(&osdc->lock);
3531 schedule_delayed_work(&osdc->timeout_work,
3532 osdc->client->options->osd_keepalive_timeout);
3535 static void handle_osds_timeout(struct work_struct *work)
3537 struct ceph_osd_client *osdc =
3538 container_of(work, struct ceph_osd_client,
3539 osds_timeout_work.work);
3540 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3541 struct ceph_osd *osd, *nosd;
3543 dout("%s osdc %p\n", __func__, osdc);
3544 down_write(&osdc->lock);
3545 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3546 if (time_before(jiffies, osd->lru_ttl))
3549 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3550 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3554 up_write(&osdc->lock);
3555 schedule_delayed_work(&osdc->osds_timeout_work,
3556 round_jiffies_relative(delay));
3559 static int ceph_oloc_decode(void **p, void *end,
3560 struct ceph_object_locator *oloc)
3562 u8 struct_v, struct_cv;
3567 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3568 struct_v = ceph_decode_8(p);
3569 struct_cv = ceph_decode_8(p);
3571 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3572 struct_v, struct_cv);
3575 if (struct_cv > 6) {
3576 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3577 struct_v, struct_cv);
3580 len = ceph_decode_32(p);
3581 ceph_decode_need(p, end, len, e_inval);
3582 struct_end = *p + len;
3584 oloc->pool = ceph_decode_64(p);
3585 *p += 4; /* skip preferred */
3587 len = ceph_decode_32(p);
3589 pr_warn("ceph_object_locator::key is set\n");
3593 if (struct_v >= 5) {
3594 bool changed = false;
3596 len = ceph_decode_32(p);
3598 ceph_decode_need(p, end, len, e_inval);
3599 if (!oloc->pool_ns ||
3600 ceph_compare_string(oloc->pool_ns, *p, len))
3608 /* redirect changes namespace */
3609 pr_warn("ceph_object_locator::nspace is changed\n");
3614 if (struct_v >= 6) {
3615 s64 hash = ceph_decode_64(p);
3617 pr_warn("ceph_object_locator::hash is set\n");
3632 static int ceph_redirect_decode(void **p, void *end,
3633 struct ceph_request_redirect *redir)
3635 u8 struct_v, struct_cv;
3640 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3641 struct_v = ceph_decode_8(p);
3642 struct_cv = ceph_decode_8(p);
3643 if (struct_cv > 1) {
3644 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3645 struct_v, struct_cv);
3648 len = ceph_decode_32(p);
3649 ceph_decode_need(p, end, len, e_inval);
3650 struct_end = *p + len;
3652 ret = ceph_oloc_decode(p, end, &redir->oloc);
3656 len = ceph_decode_32(p);
3658 pr_warn("ceph_request_redirect::object_name is set\n");
3672 struct MOSDOpReply {
3673 struct ceph_pg pgid;
3678 u32 outdata_len[CEPH_OSD_MAX_OPS];
3679 s32 rval[CEPH_OSD_MAX_OPS];
3681 struct ceph_eversion replay_version;
3683 struct ceph_request_redirect redirect;
3686 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3688 void *p = msg->front.iov_base;
3689 void *const end = p + msg->front.iov_len;
3690 u16 version = le16_to_cpu(msg->hdr.version);
3691 struct ceph_eversion bad_replay_version;
3697 ceph_decode_32_safe(&p, end, len, e_inval);
3698 ceph_decode_need(&p, end, len, e_inval);
3699 p += len; /* skip oid */
3701 ret = ceph_decode_pgid(&p, end, &m->pgid);
3705 ceph_decode_64_safe(&p, end, m->flags, e_inval);
3706 ceph_decode_32_safe(&p, end, m->result, e_inval);
3707 ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3708 memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3709 p += sizeof(bad_replay_version);
3710 ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3712 ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3713 if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3716 ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3718 for (i = 0; i < m->num_ops; i++) {
3719 struct ceph_osd_op *op = p;
3721 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3725 ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3726 for (i = 0; i < m->num_ops; i++)
3727 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3730 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3731 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3732 p += sizeof(m->replay_version);
3733 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3735 m->replay_version = bad_replay_version; /* struct */
3736 m->user_version = le64_to_cpu(m->replay_version.version);
3741 ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3749 ret = ceph_redirect_decode(&p, end, &m->redirect);
3753 ceph_oloc_init(&m->redirect.oloc);
3763 * Handle MOSDOpReply. Set ->r_result and call the callback if it is
3766 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3768 struct ceph_osd_client *osdc = osd->o_osdc;
3769 struct ceph_osd_request *req;
3770 struct MOSDOpReply m;
3771 u64 tid = le64_to_cpu(msg->hdr.tid);
3776 dout("%s msg %p tid %llu\n", __func__, msg, tid);
3778 down_read(&osdc->lock);
3779 if (!osd_registered(osd)) {
3780 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3781 goto out_unlock_osdc;
3783 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3785 mutex_lock(&osd->lock);
3786 req = lookup_request(&osd->o_requests, tid);
3788 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3789 goto out_unlock_session;
3792 m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3793 ret = decode_MOSDOpReply(msg, &m);
3794 m.redirect.oloc.pool_ns = NULL;
3796 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3801 dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3802 __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3803 m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3804 le64_to_cpu(m.replay_version.version), m.user_version);
3806 if (m.retry_attempt >= 0) {
3807 if (m.retry_attempt != req->r_attempts - 1) {
3808 dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3809 req, req->r_tid, m.retry_attempt,
3810 req->r_attempts - 1);
3811 goto out_unlock_session;
3814 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3817 if (!ceph_oloc_empty(&m.redirect.oloc)) {
3818 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3819 m.redirect.oloc.pool);
3820 unlink_request(osd, req);
3821 mutex_unlock(&osd->lock);
3824 * Not ceph_oloc_copy() - changing pool_ns is not
3827 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3828 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
3829 CEPH_OSD_FLAG_IGNORE_OVERLAY |
3830 CEPH_OSD_FLAG_IGNORE_CACHE;
3832 __submit_request(req, false);
3833 goto out_unlock_osdc;
3836 if (m.result == -EAGAIN) {
3837 dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
3838 unlink_request(osd, req);
3839 mutex_unlock(&osd->lock);
3842 * The object is missing on the replica or not (yet)
3843 * readable. Clear pgid to force a resend to the primary
3844 * via legacy_change.
3846 req->r_t.pgid.pool = 0;
3847 req->r_t.pgid.seed = 0;
3848 WARN_ON(!req->r_t.used_replica);
3849 req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
3850 CEPH_OSD_FLAG_LOCALIZE_READS);
3852 __submit_request(req, false);
3853 goto out_unlock_osdc;
3856 if (m.num_ops != req->r_num_ops) {
3857 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3858 req->r_num_ops, req->r_tid);
3861 for (i = 0; i < req->r_num_ops; i++) {
3862 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3863 req->r_tid, i, m.rval[i], m.outdata_len[i]);
3864 req->r_ops[i].rval = m.rval[i];
3865 req->r_ops[i].outdata_len = m.outdata_len[i];
3866 data_len += m.outdata_len[i];
3868 if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3869 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3870 le32_to_cpu(msg->hdr.data_len), req->r_tid);
3873 dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3874 req, req->r_tid, m.result, data_len);
3877 * Since we only ever request ONDISK, we should only ever get
3878 * one (type of) reply back.
3880 WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3881 req->r_version = m.user_version;
3882 req->r_result = m.result ?: data_len;
3883 finish_request(req);
3884 mutex_unlock(&osd->lock);
3885 up_read(&osdc->lock);
3887 __complete_request(req);
3891 complete_request(req, -EIO);
3893 mutex_unlock(&osd->lock);
3895 up_read(&osdc->lock);
3898 static void set_pool_was_full(struct ceph_osd_client *osdc)
3902 for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3903 struct ceph_pg_pool_info *pi =
3904 rb_entry(n, struct ceph_pg_pool_info, node);
3906 pi->was_full = __pool_full(pi);
3910 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3912 struct ceph_pg_pool_info *pi;
3914 pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3918 return pi->was_full && !__pool_full(pi);
3921 static enum calc_target_result
3922 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3924 struct ceph_osd_client *osdc = lreq->osdc;
3925 enum calc_target_result ct_res;
3927 ct_res = calc_target(osdc, &lreq->t, true);
3928 if (ct_res == CALC_TARGET_NEED_RESEND) {
3929 struct ceph_osd *osd;
3931 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3932 if (osd != lreq->osd) {
3933 unlink_linger(lreq->osd, lreq);
3934 link_linger(osd, lreq);
3942 * Requeue requests whose mapping to an OSD has changed.
3944 static void scan_requests(struct ceph_osd *osd,
3947 bool check_pool_cleared_full,
3948 struct rb_root *need_resend,
3949 struct list_head *need_resend_linger)
3951 struct ceph_osd_client *osdc = osd->o_osdc;
3953 bool force_resend_writes;
3955 for (n = rb_first(&osd->o_linger_requests); n; ) {
3956 struct ceph_osd_linger_request *lreq =
3957 rb_entry(n, struct ceph_osd_linger_request, node);
3958 enum calc_target_result ct_res;
3960 n = rb_next(n); /* recalc_linger_target() */
3962 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3964 ct_res = recalc_linger_target(lreq);
3966 case CALC_TARGET_NO_ACTION:
3967 force_resend_writes = cleared_full ||
3968 (check_pool_cleared_full &&
3969 pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3970 if (!force_resend && !force_resend_writes)
3974 case CALC_TARGET_NEED_RESEND:
3975 cancel_linger_map_check(lreq);
3977 * scan_requests() for the previous epoch(s)
3978 * may have already added it to the list, since
3979 * it's not unlinked here.
3981 if (list_empty(&lreq->scan_item))
3982 list_add_tail(&lreq->scan_item, need_resend_linger);
3984 case CALC_TARGET_POOL_DNE:
3985 list_del_init(&lreq->scan_item);
3986 check_linger_pool_dne(lreq);
3991 for (n = rb_first(&osd->o_requests); n; ) {
3992 struct ceph_osd_request *req =
3993 rb_entry(n, struct ceph_osd_request, r_node);
3994 enum calc_target_result ct_res;
3996 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3998 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3999 ct_res = calc_target(osdc, &req->r_t, false);
4001 case CALC_TARGET_NO_ACTION:
4002 force_resend_writes = cleared_full ||
4003 (check_pool_cleared_full &&
4004 pool_cleared_full(osdc, req->r_t.base_oloc.pool));
4005 if (!force_resend &&
4006 (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
4007 !force_resend_writes))
4011 case CALC_TARGET_NEED_RESEND:
4012 cancel_map_check(req);
4013 unlink_request(osd, req);
4014 insert_request(need_resend, req);
4016 case CALC_TARGET_POOL_DNE:
4017 check_pool_dne(req);
4023 static int handle_one_map(struct ceph_osd_client *osdc,
4024 void *p, void *end, bool incremental,
4025 struct rb_root *need_resend,
4026 struct list_head *need_resend_linger)
4028 struct ceph_osdmap *newmap;
4030 bool skipped_map = false;
4033 was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
4034 set_pool_was_full(osdc);
4037 newmap = osdmap_apply_incremental(&p, end,
4038 ceph_msgr2(osdc->client),
4041 newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
4043 return PTR_ERR(newmap);
4045 if (newmap != osdc->osdmap) {
4047 * Preserve ->was_full before destroying the old map.
4048 * For pools that weren't in the old map, ->was_full
4051 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
4052 struct ceph_pg_pool_info *pi =
4053 rb_entry(n, struct ceph_pg_pool_info, node);
4054 struct ceph_pg_pool_info *old_pi;
4056 old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
4058 pi->was_full = old_pi->was_full;
4060 WARN_ON(pi->was_full);
4063 if (osdc->osdmap->epoch &&
4064 osdc->osdmap->epoch + 1 < newmap->epoch) {
4065 WARN_ON(incremental);
4069 ceph_osdmap_destroy(osdc->osdmap);
4070 osdc->osdmap = newmap;
4073 was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
4074 scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
4075 need_resend, need_resend_linger);
4077 for (n = rb_first(&osdc->osds); n; ) {
4078 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4080 n = rb_next(n); /* close_osd() */
4082 scan_requests(osd, skipped_map, was_full, true, need_resend,
4083 need_resend_linger);
4084 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
4085 memcmp(&osd->o_con.peer_addr,
4086 ceph_osd_addr(osdc->osdmap, osd->o_osd),
4087 sizeof(struct ceph_entity_addr)))
4094 static void kick_requests(struct ceph_osd_client *osdc,
4095 struct rb_root *need_resend,
4096 struct list_head *need_resend_linger)
4098 struct ceph_osd_linger_request *lreq, *nlreq;
4099 enum calc_target_result ct_res;
4102 /* make sure need_resend targets reflect latest map */
4103 for (n = rb_first(need_resend); n; ) {
4104 struct ceph_osd_request *req =
4105 rb_entry(n, struct ceph_osd_request, r_node);
4109 if (req->r_t.epoch < osdc->osdmap->epoch) {
4110 ct_res = calc_target(osdc, &req->r_t, false);
4111 if (ct_res == CALC_TARGET_POOL_DNE) {
4112 erase_request(need_resend, req);
4113 check_pool_dne(req);
4118 for (n = rb_first(need_resend); n; ) {
4119 struct ceph_osd_request *req =
4120 rb_entry(n, struct ceph_osd_request, r_node);
4121 struct ceph_osd *osd;
4124 erase_request(need_resend, req); /* before link_request() */
4126 osd = lookup_create_osd(osdc, req->r_t.osd, true);
4127 link_request(osd, req);
4128 if (!req->r_linger) {
4129 if (!osd_homeless(osd) && !req->r_t.paused)
4132 cancel_linger_request(req);
4136 list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
4137 if (!osd_homeless(lreq->osd))
4140 list_del_init(&lreq->scan_item);
4145 * Process updated osd map.
4147 * The message contains any number of incremental and full maps, normally
4148 * indicating some sort of topology change in the cluster. Kick requests
4149 * off to different OSDs as needed.
4151 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
4153 void *p = msg->front.iov_base;
4154 void *const end = p + msg->front.iov_len;
4155 u32 nr_maps, maplen;
4157 struct ceph_fsid fsid;
4158 struct rb_root need_resend = RB_ROOT;
4159 LIST_HEAD(need_resend_linger);
4160 bool handled_incremental = false;
4161 bool was_pauserd, was_pausewr;
4162 bool pauserd, pausewr;
4165 dout("%s have %u\n", __func__, osdc->osdmap->epoch);
4166 down_write(&osdc->lock);
4169 ceph_decode_need(&p, end, sizeof(fsid), bad);
4170 ceph_decode_copy(&p, &fsid, sizeof(fsid));
4171 if (ceph_check_fsid(osdc->client, &fsid) < 0)
4174 was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4175 was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4176 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
4177 have_pool_full(osdc);
4179 /* incremental maps */
4180 ceph_decode_32_safe(&p, end, nr_maps, bad);
4181 dout(" %d inc maps\n", nr_maps);
4182 while (nr_maps > 0) {
4183 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
4184 epoch = ceph_decode_32(&p);
4185 maplen = ceph_decode_32(&p);
4186 ceph_decode_need(&p, end, maplen, bad);
4187 if (osdc->osdmap->epoch &&
4188 osdc->osdmap->epoch + 1 == epoch) {
4189 dout("applying incremental map %u len %d\n",
4191 err = handle_one_map(osdc, p, p + maplen, true,
4192 &need_resend, &need_resend_linger);
4195 handled_incremental = true;
4197 dout("ignoring incremental map %u len %d\n",
4203 if (handled_incremental)
4207 ceph_decode_32_safe(&p, end, nr_maps, bad);
4208 dout(" %d full maps\n", nr_maps);
4210 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
4211 epoch = ceph_decode_32(&p);
4212 maplen = ceph_decode_32(&p);
4213 ceph_decode_need(&p, end, maplen, bad);
4215 dout("skipping non-latest full map %u len %d\n",
4217 } else if (osdc->osdmap->epoch >= epoch) {
4218 dout("skipping full map %u len %d, "
4219 "older than our %u\n", epoch, maplen,
4220 osdc->osdmap->epoch);
4222 dout("taking full map %u len %d\n", epoch, maplen);
4223 err = handle_one_map(osdc, p, p + maplen, false,
4224 &need_resend, &need_resend_linger);
4234 * subscribe to subsequent osdmap updates if full to ensure
4235 * we find out when we are no longer full and stop returning
4238 pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4239 pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4240 ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
4241 have_pool_full(osdc);
4242 if (was_pauserd || was_pausewr || pauserd || pausewr ||
4243 osdc->osdmap->epoch < osdc->epoch_barrier)
4244 maybe_request_map(osdc);
4246 kick_requests(osdc, &need_resend, &need_resend_linger);
4248 ceph_osdc_abort_on_full(osdc);
4249 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
4250 osdc->osdmap->epoch);
4251 up_write(&osdc->lock);
4252 wake_up_all(&osdc->client->auth_wq);
4256 pr_err("osdc handle_map corrupt msg\n");
4258 up_write(&osdc->lock);
4262 * Resubmit requests pending on the given osd.
4264 static void kick_osd_requests(struct ceph_osd *osd)
4268 clear_backoffs(osd);
4270 for (n = rb_first(&osd->o_requests); n; ) {
4271 struct ceph_osd_request *req =
4272 rb_entry(n, struct ceph_osd_request, r_node);
4274 n = rb_next(n); /* cancel_linger_request() */
4276 if (!req->r_linger) {
4277 if (!req->r_t.paused)
4280 cancel_linger_request(req);
4283 for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4284 struct ceph_osd_linger_request *lreq =
4285 rb_entry(n, struct ceph_osd_linger_request, node);
4292 * If the osd connection drops, we need to resubmit all requests.
4294 static void osd_fault(struct ceph_connection *con)
4296 struct ceph_osd *osd = con->private;
4297 struct ceph_osd_client *osdc = osd->o_osdc;
4299 dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4301 down_write(&osdc->lock);
4302 if (!osd_registered(osd)) {
4303 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4307 if (!reopen_osd(osd))
4308 kick_osd_requests(osd);
4309 maybe_request_map(osdc);
4312 up_write(&osdc->lock);
4315 struct MOSDBackoff {
4316 struct ceph_spg spgid;
4320 struct ceph_hobject_id *begin;
4321 struct ceph_hobject_id *end;
4324 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4326 void *p = msg->front.iov_base;
4327 void *const end = p + msg->front.iov_len;
4332 ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4336 ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4340 ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4341 ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4342 ceph_decode_8_safe(&p, end, m->op, e_inval);
4343 ceph_decode_64_safe(&p, end, m->id, e_inval);
4345 m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4349 ret = decode_hoid(&p, end, m->begin);
4351 free_hoid(m->begin);
4355 m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4357 free_hoid(m->begin);
4361 ret = decode_hoid(&p, end, m->end);
4363 free_hoid(m->begin);
4374 static struct ceph_msg *create_backoff_message(
4375 const struct ceph_osd_backoff *backoff,
4378 struct ceph_msg *msg;
4382 msg_size = CEPH_ENCODING_START_BLK_LEN +
4383 CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4384 msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4385 msg_size += CEPH_ENCODING_START_BLK_LEN +
4386 hoid_encoding_size(backoff->begin);
4387 msg_size += CEPH_ENCODING_START_BLK_LEN +
4388 hoid_encoding_size(backoff->end);
4390 msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4394 p = msg->front.iov_base;
4395 end = p + msg->front_alloc_len;
4397 encode_spgid(&p, &backoff->spgid);
4398 ceph_encode_32(&p, map_epoch);
4399 ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4400 ceph_encode_64(&p, backoff->id);
4401 encode_hoid(&p, end, backoff->begin);
4402 encode_hoid(&p, end, backoff->end);
4405 msg->front.iov_len = p - msg->front.iov_base;
4406 msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4407 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4412 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4414 struct ceph_spg_mapping *spg;
4415 struct ceph_osd_backoff *backoff;
4416 struct ceph_msg *msg;
4418 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4419 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4421 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4423 spg = alloc_spg_mapping();
4425 pr_err("%s failed to allocate spg\n", __func__);
4428 spg->spgid = m->spgid; /* struct */
4429 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4432 backoff = alloc_backoff();
4434 pr_err("%s failed to allocate backoff\n", __func__);
4437 backoff->spgid = m->spgid; /* struct */
4438 backoff->id = m->id;
4439 backoff->begin = m->begin;
4440 m->begin = NULL; /* backoff now owns this */
4441 backoff->end = m->end;
4442 m->end = NULL; /* ditto */
4444 insert_backoff(&spg->backoffs, backoff);
4445 insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4448 * Ack with original backoff's epoch so that the OSD can
4449 * discard this if there was a PG split.
4451 msg = create_backoff_message(backoff, m->map_epoch);
4453 pr_err("%s failed to allocate msg\n", __func__);
4456 ceph_con_send(&osd->o_con, msg);
4459 static bool target_contained_by(const struct ceph_osd_request_target *t,
4460 const struct ceph_hobject_id *begin,
4461 const struct ceph_hobject_id *end)
4463 struct ceph_hobject_id hoid;
4466 hoid_fill_from_target(&hoid, t);
4467 cmp = hoid_compare(&hoid, begin);
4468 return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4471 static void handle_backoff_unblock(struct ceph_osd *osd,
4472 const struct MOSDBackoff *m)
4474 struct ceph_spg_mapping *spg;
4475 struct ceph_osd_backoff *backoff;
4478 dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4479 m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4481 backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4483 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4484 __func__, osd->o_osd, m->spgid.pgid.pool,
4485 m->spgid.pgid.seed, m->spgid.shard, m->id);
4489 if (hoid_compare(backoff->begin, m->begin) &&
4490 hoid_compare(backoff->end, m->end)) {
4491 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4492 __func__, osd->o_osd, m->spgid.pgid.pool,
4493 m->spgid.pgid.seed, m->spgid.shard, m->id);
4494 /* unblock it anyway... */
4497 spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4500 erase_backoff(&spg->backoffs, backoff);
4501 erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4502 free_backoff(backoff);
4504 if (RB_EMPTY_ROOT(&spg->backoffs)) {
4505 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4506 free_spg_mapping(spg);
4509 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4510 struct ceph_osd_request *req =
4511 rb_entry(n, struct ceph_osd_request, r_node);
4513 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4515 * Match against @m, not @backoff -- the PG may
4516 * have split on the OSD.
4518 if (target_contained_by(&req->r_t, m->begin, m->end)) {
4520 * If no other installed backoff applies,
4529 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4531 struct ceph_osd_client *osdc = osd->o_osdc;
4532 struct MOSDBackoff m;
4535 down_read(&osdc->lock);
4536 if (!osd_registered(osd)) {
4537 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4538 up_read(&osdc->lock);
4541 WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4543 mutex_lock(&osd->lock);
4544 ret = decode_MOSDBackoff(msg, &m);
4546 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4552 case CEPH_OSD_BACKOFF_OP_BLOCK:
4553 handle_backoff_block(osd, &m);
4555 case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4556 handle_backoff_unblock(osd, &m);
4559 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4566 mutex_unlock(&osd->lock);
4567 up_read(&osdc->lock);
4571 * Process osd watch notifications
4573 static void handle_watch_notify(struct ceph_osd_client *osdc,
4574 struct ceph_msg *msg)
4576 void *p = msg->front.iov_base;
4577 void *const end = p + msg->front.iov_len;
4578 struct ceph_osd_linger_request *lreq;
4579 struct linger_work *lwork;
4580 u8 proto_ver, opcode;
4581 u64 cookie, notify_id;
4582 u64 notifier_id = 0;
4583 s32 return_code = 0;
4584 void *payload = NULL;
4585 u32 payload_len = 0;
4587 ceph_decode_8_safe(&p, end, proto_ver, bad);
4588 ceph_decode_8_safe(&p, end, opcode, bad);
4589 ceph_decode_64_safe(&p, end, cookie, bad);
4590 p += 8; /* skip ver */
4591 ceph_decode_64_safe(&p, end, notify_id, bad);
4593 if (proto_ver >= 1) {
4594 ceph_decode_32_safe(&p, end, payload_len, bad);
4595 ceph_decode_need(&p, end, payload_len, bad);
4600 if (le16_to_cpu(msg->hdr.version) >= 2)
4601 ceph_decode_32_safe(&p, end, return_code, bad);
4603 if (le16_to_cpu(msg->hdr.version) >= 3)
4604 ceph_decode_64_safe(&p, end, notifier_id, bad);
4606 down_read(&osdc->lock);
4607 lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4609 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4611 goto out_unlock_osdc;
4614 mutex_lock(&lreq->lock);
4615 dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4616 opcode, cookie, lreq, lreq->is_watch);
4617 if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4618 if (!lreq->last_error) {
4619 lreq->last_error = -ENOTCONN;
4620 queue_watch_error(lreq);
4622 } else if (!lreq->is_watch) {
4623 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4624 if (lreq->notify_id && lreq->notify_id != notify_id) {
4625 dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4626 lreq->notify_id, notify_id);
4627 } else if (!completion_done(&lreq->notify_finish_wait)) {
4628 struct ceph_msg_data *data =
4629 msg->num_data_items ? &msg->data[0] : NULL;
4632 if (lreq->preply_pages) {
4633 WARN_ON(data->type !=
4634 CEPH_MSG_DATA_PAGES);
4635 *lreq->preply_pages = data->pages;
4636 *lreq->preply_len = data->length;
4637 data->own_pages = false;
4640 lreq->notify_finish_error = return_code;
4641 complete_all(&lreq->notify_finish_wait);
4644 /* CEPH_WATCH_EVENT_NOTIFY */
4645 lwork = lwork_alloc(lreq, do_watch_notify);
4647 pr_err("failed to allocate notify-lwork\n");
4648 goto out_unlock_lreq;
4651 lwork->notify.notify_id = notify_id;
4652 lwork->notify.notifier_id = notifier_id;
4653 lwork->notify.payload = payload;
4654 lwork->notify.payload_len = payload_len;
4655 lwork->notify.msg = ceph_msg_get(msg);
4660 mutex_unlock(&lreq->lock);
4662 up_read(&osdc->lock);
4666 pr_err("osdc handle_watch_notify corrupt msg\n");
4670 * Register request, send initial attempt.
4672 void ceph_osdc_start_request(struct ceph_osd_client *osdc,
4673 struct ceph_osd_request *req)
4675 down_read(&osdc->lock);
4676 submit_request(req, false);
4677 up_read(&osdc->lock);
4679 EXPORT_SYMBOL(ceph_osdc_start_request);
4682 * Unregister request. If @req was registered, it isn't completed:
4683 * r_result isn't set and __complete_request() isn't invoked.
4685 * If @req wasn't registered, this call may have raced with
4686 * handle_reply(), in which case r_result would already be set and
4687 * __complete_request() would be getting invoked, possibly even
4688 * concurrently with this call.
4690 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4692 struct ceph_osd_client *osdc = req->r_osdc;
4694 down_write(&osdc->lock);
4696 cancel_request(req);
4697 up_write(&osdc->lock);
4699 EXPORT_SYMBOL(ceph_osdc_cancel_request);
4702 * @timeout: in jiffies, 0 means "wait forever"
4704 static int wait_request_timeout(struct ceph_osd_request *req,
4705 unsigned long timeout)
4709 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
4710 left = wait_for_completion_killable_timeout(&req->r_completion,
4711 ceph_timeout_jiffies(timeout));
4713 left = left ?: -ETIMEDOUT;
4714 ceph_osdc_cancel_request(req);
4716 left = req->r_result; /* completed */
4723 * wait for a request to complete
4725 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4726 struct ceph_osd_request *req)
4728 return wait_request_timeout(req, 0);
4730 EXPORT_SYMBOL(ceph_osdc_wait_request);
4733 * sync - wait for all in-flight requests to flush. avoid starvation.
4735 void ceph_osdc_sync(struct ceph_osd_client *osdc)
4737 struct rb_node *n, *p;
4738 u64 last_tid = atomic64_read(&osdc->last_tid);
4741 down_read(&osdc->lock);
4742 for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4743 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4745 mutex_lock(&osd->lock);
4746 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4747 struct ceph_osd_request *req =
4748 rb_entry(p, struct ceph_osd_request, r_node);
4750 if (req->r_tid > last_tid)
4753 if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4756 ceph_osdc_get_request(req);
4757 mutex_unlock(&osd->lock);
4758 up_read(&osdc->lock);
4759 dout("%s waiting on req %p tid %llu last_tid %llu\n",
4760 __func__, req, req->r_tid, last_tid);
4761 wait_for_completion(&req->r_completion);
4762 ceph_osdc_put_request(req);
4766 mutex_unlock(&osd->lock);
4769 up_read(&osdc->lock);
4770 dout("%s done last_tid %llu\n", __func__, last_tid);
4772 EXPORT_SYMBOL(ceph_osdc_sync);
4775 * Returns a handle, caller owns a ref.
4777 struct ceph_osd_linger_request *
4778 ceph_osdc_watch(struct ceph_osd_client *osdc,
4779 struct ceph_object_id *oid,
4780 struct ceph_object_locator *oloc,
4781 rados_watchcb2_t wcb,
4782 rados_watcherrcb_t errcb,
4785 struct ceph_osd_linger_request *lreq;
4788 lreq = linger_alloc(osdc);
4790 return ERR_PTR(-ENOMEM);
4792 lreq->is_watch = true;
4794 lreq->errcb = errcb;
4796 lreq->watch_valid_thru = jiffies;
4798 ceph_oid_copy(&lreq->t.base_oid, oid);
4799 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4800 lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4801 ktime_get_real_ts64(&lreq->mtime);
4803 linger_submit(lreq);
4804 ret = linger_reg_commit_wait(lreq);
4806 linger_cancel(lreq);
4814 return ERR_PTR(ret);
4816 EXPORT_SYMBOL(ceph_osdc_watch);
4821 * Times out after mount_timeout to preserve rbd unmap behaviour
4822 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4823 * with mount_timeout").
4825 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4826 struct ceph_osd_linger_request *lreq)
4828 struct ceph_options *opts = osdc->client->options;
4829 struct ceph_osd_request *req;
4832 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4836 ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4837 ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4838 req->r_flags = CEPH_OSD_FLAG_WRITE;
4839 ktime_get_real_ts64(&req->r_mtime);
4840 osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
4841 lreq->linger_id, 0);
4843 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4847 ceph_osdc_start_request(osdc, req);
4848 linger_cancel(lreq);
4850 ret = wait_request_timeout(req, opts->mount_timeout);
4853 ceph_osdc_put_request(req);
4856 EXPORT_SYMBOL(ceph_osdc_unwatch);
4858 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4859 u64 notify_id, u64 cookie, void *payload,
4862 struct ceph_osd_req_op *op;
4863 struct ceph_pagelist *pl;
4866 op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4868 pl = ceph_pagelist_alloc(GFP_NOIO);
4872 ret = ceph_pagelist_encode_64(pl, notify_id);
4873 ret |= ceph_pagelist_encode_64(pl, cookie);
4875 ret |= ceph_pagelist_encode_32(pl, payload_len);
4876 ret |= ceph_pagelist_append(pl, payload, payload_len);
4878 ret |= ceph_pagelist_encode_32(pl, 0);
4881 ceph_pagelist_release(pl);
4885 ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4886 op->indata_len = pl->length;
4890 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4891 struct ceph_object_id *oid,
4892 struct ceph_object_locator *oloc,
4898 struct ceph_osd_request *req;
4901 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4905 ceph_oid_copy(&req->r_base_oid, oid);
4906 ceph_oloc_copy(&req->r_base_oloc, oloc);
4907 req->r_flags = CEPH_OSD_FLAG_READ;
4909 ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4914 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4918 ceph_osdc_start_request(osdc, req);
4919 ret = ceph_osdc_wait_request(osdc, req);
4922 ceph_osdc_put_request(req);
4925 EXPORT_SYMBOL(ceph_osdc_notify_ack);
4928 * @timeout: in seconds
4930 * @preply_{pages,len} are initialized both on success and error.
4931 * The caller is responsible for:
4933 * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4935 int ceph_osdc_notify(struct ceph_osd_client *osdc,
4936 struct ceph_object_id *oid,
4937 struct ceph_object_locator *oloc,
4941 struct page ***preply_pages,
4944 struct ceph_osd_linger_request *lreq;
4949 *preply_pages = NULL;
4953 lreq = linger_alloc(osdc);
4957 lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
4958 if (!lreq->request_pl) {
4963 ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
4964 ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
4965 ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
4966 ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
4973 lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
4974 if (IS_ERR(lreq->notify_id_pages)) {
4975 ret = PTR_ERR(lreq->notify_id_pages);
4976 lreq->notify_id_pages = NULL;
4980 lreq->preply_pages = preply_pages;
4981 lreq->preply_len = preply_len;
4983 ceph_oid_copy(&lreq->t.base_oid, oid);
4984 ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4985 lreq->t.flags = CEPH_OSD_FLAG_READ;
4987 linger_submit(lreq);
4988 ret = linger_reg_commit_wait(lreq);
4990 ret = linger_notify_finish_wait(lreq,
4991 msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
4993 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4995 linger_cancel(lreq);
5000 EXPORT_SYMBOL(ceph_osdc_notify);
5003 * Return the number of milliseconds since the watch was last
5004 * confirmed, or an error. If there is an error, the watch is no
5005 * longer valid, and should be destroyed with ceph_osdc_unwatch().
5007 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
5008 struct ceph_osd_linger_request *lreq)
5010 unsigned long stamp, age;
5013 down_read(&osdc->lock);
5014 mutex_lock(&lreq->lock);
5015 stamp = lreq->watch_valid_thru;
5016 if (!list_empty(&lreq->pending_lworks)) {
5017 struct linger_work *lwork =
5018 list_first_entry(&lreq->pending_lworks,
5022 if (time_before(lwork->queued_stamp, stamp))
5023 stamp = lwork->queued_stamp;
5025 age = jiffies - stamp;
5026 dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
5027 lreq, lreq->linger_id, age, lreq->last_error);
5028 /* we are truncating to msecs, so return a safe upper bound */
5029 ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
5031 mutex_unlock(&lreq->lock);
5032 up_read(&osdc->lock);
5036 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
5042 ret = ceph_start_decoding(p, end, 2, "watch_item_t",
5043 &struct_v, &struct_len);
5048 ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
5049 ceph_decode_64_safe(p, end, item->cookie, bad);
5050 ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
5052 if (struct_v >= 2) {
5053 ret = ceph_decode_entity_addr(p, end, &item->addr);
5060 dout("%s %s%llu cookie %llu addr %s\n", __func__,
5061 ENTITY_NAME(item->name), item->cookie,
5062 ceph_pr_addr(&item->addr));
5067 static int decode_watchers(void **p, void *end,
5068 struct ceph_watch_item **watchers,
5076 ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
5077 &struct_v, &struct_len);
5081 *num_watchers = ceph_decode_32(p);
5082 *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
5086 for (i = 0; i < *num_watchers; i++) {
5087 ret = decode_watcher(p, end, *watchers + i);
5098 * On success, the caller is responsible for:
5102 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
5103 struct ceph_object_id *oid,
5104 struct ceph_object_locator *oloc,
5105 struct ceph_watch_item **watchers,
5108 struct ceph_osd_request *req;
5109 struct page **pages;
5112 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5116 ceph_oid_copy(&req->r_base_oid, oid);
5117 ceph_oloc_copy(&req->r_base_oloc, oloc);
5118 req->r_flags = CEPH_OSD_FLAG_READ;
5120 pages = ceph_alloc_page_vector(1, GFP_NOIO);
5121 if (IS_ERR(pages)) {
5122 ret = PTR_ERR(pages);
5126 osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5127 ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5129 pages, PAGE_SIZE, 0, false, true);
5131 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5135 ceph_osdc_start_request(osdc, req);
5136 ret = ceph_osdc_wait_request(osdc, req);
5138 void *p = page_address(pages[0]);
5139 void *const end = p + req->r_ops[0].outdata_len;
5141 ret = decode_watchers(&p, end, watchers, num_watchers);
5145 ceph_osdc_put_request(req);
5148 EXPORT_SYMBOL(ceph_osdc_list_watchers);
5151 * Call all pending notify callbacks - for use after a watch is
5152 * unregistered, to make sure no more callbacks for it will be invoked
5154 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5156 dout("%s osdc %p\n", __func__, osdc);
5157 flush_workqueue(osdc->notify_wq);
5159 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5161 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
5163 down_read(&osdc->lock);
5164 maybe_request_map(osdc);
5165 up_read(&osdc->lock);
5167 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5170 * Execute an OSD class method on an object.
5172 * @flags: CEPH_OSD_FLAG_*
5173 * @resp_len: in/out param for reply length
5175 int ceph_osdc_call(struct ceph_osd_client *osdc,
5176 struct ceph_object_id *oid,
5177 struct ceph_object_locator *oloc,
5178 const char *class, const char *method,
5180 struct page *req_page, size_t req_len,
5181 struct page **resp_pages, size_t *resp_len)
5183 struct ceph_osd_request *req;
5186 if (req_len > PAGE_SIZE)
5189 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5193 ceph_oid_copy(&req->r_base_oid, oid);
5194 ceph_oloc_copy(&req->r_base_oloc, oloc);
5195 req->r_flags = flags;
5197 ret = osd_req_op_cls_init(req, 0, class, method);
5202 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5205 osd_req_op_cls_response_data_pages(req, 0, resp_pages,
5206 *resp_len, 0, false, false);
5208 ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
5212 ceph_osdc_start_request(osdc, req);
5213 ret = ceph_osdc_wait_request(osdc, req);
5215 ret = req->r_ops[0].rval;
5217 *resp_len = req->r_ops[0].outdata_len;
5221 ceph_osdc_put_request(req);
5224 EXPORT_SYMBOL(ceph_osdc_call);
5227 * reset all osd connections
5229 void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5233 down_write(&osdc->lock);
5234 for (n = rb_first(&osdc->osds); n; ) {
5235 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5238 if (!reopen_osd(osd))
5239 kick_osd_requests(osd);
5241 up_write(&osdc->lock);
5247 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5252 osdc->client = client;
5253 init_rwsem(&osdc->lock);
5254 osdc->osds = RB_ROOT;
5255 INIT_LIST_HEAD(&osdc->osd_lru);
5256 spin_lock_init(&osdc->osd_lru_lock);
5257 osd_init(&osdc->homeless_osd);
5258 osdc->homeless_osd.o_osdc = osdc;
5259 osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5260 osdc->last_linger_id = CEPH_LINGER_ID_START;
5261 osdc->linger_requests = RB_ROOT;
5262 osdc->map_checks = RB_ROOT;
5263 osdc->linger_map_checks = RB_ROOT;
5264 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
5265 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5268 osdc->osdmap = ceph_osdmap_alloc();
5272 osdc->req_mempool = mempool_create_slab_pool(10,
5273 ceph_osd_request_cache);
5274 if (!osdc->req_mempool)
5277 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5278 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
5281 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5282 PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
5288 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5289 if (!osdc->notify_wq)
5290 goto out_msgpool_reply;
5292 osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5293 if (!osdc->completion_wq)
5296 schedule_delayed_work(&osdc->timeout_work,
5297 osdc->client->options->osd_keepalive_timeout);
5298 schedule_delayed_work(&osdc->osds_timeout_work,
5299 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5304 destroy_workqueue(osdc->notify_wq);
5306 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5308 ceph_msgpool_destroy(&osdc->msgpool_op);
5310 mempool_destroy(osdc->req_mempool);
5312 ceph_osdmap_destroy(osdc->osdmap);
5317 void ceph_osdc_stop(struct ceph_osd_client *osdc)
5319 destroy_workqueue(osdc->completion_wq);
5320 destroy_workqueue(osdc->notify_wq);
5321 cancel_delayed_work_sync(&osdc->timeout_work);
5322 cancel_delayed_work_sync(&osdc->osds_timeout_work);
5324 down_write(&osdc->lock);
5325 while (!RB_EMPTY_ROOT(&osdc->osds)) {
5326 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5327 struct ceph_osd, o_node);
5330 up_write(&osdc->lock);
5331 WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5332 osd_cleanup(&osdc->homeless_osd);
5334 WARN_ON(!list_empty(&osdc->osd_lru));
5335 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
5336 WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5337 WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5338 WARN_ON(atomic_read(&osdc->num_requests));
5339 WARN_ON(atomic_read(&osdc->num_homeless));
5341 ceph_osdmap_destroy(osdc->osdmap);
5342 mempool_destroy(osdc->req_mempool);
5343 ceph_msgpool_destroy(&osdc->msgpool_op);
5344 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5347 int osd_req_op_copy_from_init(struct ceph_osd_request *req,
5348 u64 src_snapid, u64 src_version,
5349 struct ceph_object_id *src_oid,
5350 struct ceph_object_locator *src_oloc,
5351 u32 src_fadvise_flags,
5352 u32 dst_fadvise_flags,
5353 u32 truncate_seq, u64 truncate_size,
5356 struct ceph_osd_req_op *op;
5357 struct page **pages;
5360 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
5362 return PTR_ERR(pages);
5364 op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
5366 op->copy_from.snapid = src_snapid;
5367 op->copy_from.src_version = src_version;
5368 op->copy_from.flags = copy_from_flags;
5369 op->copy_from.src_fadvise_flags = src_fadvise_flags;
5371 p = page_address(pages[0]);
5372 end = p + PAGE_SIZE;
5373 ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
5374 encode_oloc(&p, end, src_oloc);
5375 ceph_encode_32(&p, truncate_seq);
5376 ceph_encode_64(&p, truncate_size);
5377 op->indata_len = PAGE_SIZE - (end - p);
5379 ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
5380 op->indata_len, 0, false, true);
5383 EXPORT_SYMBOL(osd_req_op_copy_from_init);
5385 int __init ceph_osdc_setup(void)
5387 size_t size = sizeof(struct ceph_osd_request) +
5388 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5390 BUG_ON(ceph_osd_request_cache);
5391 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5394 return ceph_osd_request_cache ? 0 : -ENOMEM;
5397 void ceph_osdc_cleanup(void)
5399 BUG_ON(!ceph_osd_request_cache);
5400 kmem_cache_destroy(ceph_osd_request_cache);
5401 ceph_osd_request_cache = NULL;
5405 * handle incoming message
5407 static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5409 struct ceph_osd *osd = con->private;
5410 struct ceph_osd_client *osdc = osd->o_osdc;
5411 int type = le16_to_cpu(msg->hdr.type);
5414 case CEPH_MSG_OSD_MAP:
5415 ceph_osdc_handle_map(osdc, msg);
5417 case CEPH_MSG_OSD_OPREPLY:
5418 handle_reply(osd, msg);
5420 case CEPH_MSG_OSD_BACKOFF:
5421 handle_backoff(osd, msg);
5423 case CEPH_MSG_WATCH_NOTIFY:
5424 handle_watch_notify(osdc, msg);
5428 pr_err("received unknown message type %d %s\n", type,
5429 ceph_msg_type_name(type));
5435 /* How much sparse data was requested? */
5436 static u64 sparse_data_requested(struct ceph_osd_request *req)
5440 if (req->r_flags & CEPH_OSD_FLAG_READ) {
5443 for (i = 0; i < req->r_num_ops; ++i) {
5444 struct ceph_osd_req_op *op = &req->r_ops[i];
5446 if (op->op == CEPH_OSD_OP_SPARSE_READ)
5447 len += op->extent.length;
5454 * Lookup and return message for incoming reply. Don't try to do
5455 * anything about a larger than preallocated data portion of the
5456 * message at the moment - for now, just skip the message.
5458 static struct ceph_msg *get_reply(struct ceph_connection *con,
5459 struct ceph_msg_header *hdr,
5462 struct ceph_osd *osd = con->private;
5463 struct ceph_osd_client *osdc = osd->o_osdc;
5464 struct ceph_msg *m = NULL;
5465 struct ceph_osd_request *req;
5466 int front_len = le32_to_cpu(hdr->front_len);
5467 int data_len = le32_to_cpu(hdr->data_len);
5468 u64 tid = le64_to_cpu(hdr->tid);
5471 down_read(&osdc->lock);
5472 if (!osd_registered(osd)) {
5473 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5475 goto out_unlock_osdc;
5477 WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5479 mutex_lock(&osd->lock);
5480 req = lookup_request(&osd->o_requests, tid);
5482 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5485 goto out_unlock_session;
5488 ceph_msg_revoke_incoming(req->r_reply);
5490 if (front_len > req->r_reply->front_alloc_len) {
5491 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5492 __func__, osd->o_osd, req->r_tid, front_len,
5493 req->r_reply->front_alloc_len);
5494 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5497 goto out_unlock_session;
5498 ceph_msg_put(req->r_reply);
5502 srlen = sparse_data_requested(req);
5503 if (!srlen && data_len > req->r_reply->data_length) {
5504 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5505 __func__, osd->o_osd, req->r_tid, data_len,
5506 req->r_reply->data_length);
5509 goto out_unlock_session;
5512 m = ceph_msg_get(req->r_reply);
5513 m->sparse_read = (bool)srlen;
5515 dout("get_reply tid %lld %p\n", tid, m);
5518 mutex_unlock(&osd->lock);
5520 up_read(&osdc->lock);
5524 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5527 int type = le16_to_cpu(hdr->type);
5528 u32 front_len = le32_to_cpu(hdr->front_len);
5529 u32 data_len = le32_to_cpu(hdr->data_len);
5531 m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
5536 struct page **pages;
5538 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5540 if (IS_ERR(pages)) {
5545 ceph_msg_data_add_pages(m, pages, data_len, 0, true);
5551 static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
5552 struct ceph_msg_header *hdr,
5555 struct ceph_osd *osd = con->private;
5556 int type = le16_to_cpu(hdr->type);
5560 case CEPH_MSG_OSD_MAP:
5561 case CEPH_MSG_OSD_BACKOFF:
5562 case CEPH_MSG_WATCH_NOTIFY:
5563 return alloc_msg_with_page_vector(hdr);
5564 case CEPH_MSG_OSD_OPREPLY:
5565 return get_reply(con, hdr, skip);
5567 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5575 * Wrappers to refcount containing ceph_osd struct
5577 static struct ceph_connection *osd_get_con(struct ceph_connection *con)
5579 struct ceph_osd *osd = con->private;
5585 static void osd_put_con(struct ceph_connection *con)
5587 struct ceph_osd *osd = con->private;
5596 * Note: returned pointer is the address of a structure that's
5597 * managed separately. Caller must *not* attempt to free it.
5599 static struct ceph_auth_handshake *
5600 osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
5602 struct ceph_osd *o = con->private;
5603 struct ceph_osd_client *osdc = o->o_osdc;
5604 struct ceph_auth_client *ac = osdc->client->monc.auth;
5605 struct ceph_auth_handshake *auth = &o->o_auth;
5608 ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5609 force_new, proto, NULL, NULL);
5611 return ERR_PTR(ret);
5616 static int osd_add_authorizer_challenge(struct ceph_connection *con,
5617 void *challenge_buf, int challenge_buf_len)
5619 struct ceph_osd *o = con->private;
5620 struct ceph_osd_client *osdc = o->o_osdc;
5621 struct ceph_auth_client *ac = osdc->client->monc.auth;
5623 return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
5624 challenge_buf, challenge_buf_len);
5627 static int osd_verify_authorizer_reply(struct ceph_connection *con)
5629 struct ceph_osd *o = con->private;
5630 struct ceph_osd_client *osdc = o->o_osdc;
5631 struct ceph_auth_client *ac = osdc->client->monc.auth;
5632 struct ceph_auth_handshake *auth = &o->o_auth;
5634 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5635 auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5636 NULL, NULL, NULL, NULL);
5639 static int osd_invalidate_authorizer(struct ceph_connection *con)
5641 struct ceph_osd *o = con->private;
5642 struct ceph_osd_client *osdc = o->o_osdc;
5643 struct ceph_auth_client *ac = osdc->client->monc.auth;
5645 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
5646 return ceph_monc_validate_auth(&osdc->client->monc);
5649 static int osd_get_auth_request(struct ceph_connection *con,
5650 void *buf, int *buf_len,
5651 void **authorizer, int *authorizer_len)
5653 struct ceph_osd *o = con->private;
5654 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5655 struct ceph_auth_handshake *auth = &o->o_auth;
5658 ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5663 *authorizer = auth->authorizer_buf;
5664 *authorizer_len = auth->authorizer_buf_len;
5668 static int osd_handle_auth_reply_more(struct ceph_connection *con,
5669 void *reply, int reply_len,
5670 void *buf, int *buf_len,
5671 void **authorizer, int *authorizer_len)
5673 struct ceph_osd *o = con->private;
5674 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5675 struct ceph_auth_handshake *auth = &o->o_auth;
5678 ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5683 *authorizer = auth->authorizer_buf;
5684 *authorizer_len = auth->authorizer_buf_len;
5688 static int osd_handle_auth_done(struct ceph_connection *con,
5689 u64 global_id, void *reply, int reply_len,
5690 u8 *session_key, int *session_key_len,
5691 u8 *con_secret, int *con_secret_len)
5693 struct ceph_osd *o = con->private;
5694 struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5695 struct ceph_auth_handshake *auth = &o->o_auth;
5697 return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5698 session_key, session_key_len,
5699 con_secret, con_secret_len);
5702 static int osd_handle_auth_bad_method(struct ceph_connection *con,
5703 int used_proto, int result,
5704 const int *allowed_protos, int proto_cnt,
5705 const int *allowed_modes, int mode_cnt)
5707 struct ceph_osd *o = con->private;
5708 struct ceph_mon_client *monc = &o->o_osdc->client->monc;
5711 if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
5713 allowed_protos, proto_cnt,
5714 allowed_modes, mode_cnt)) {
5715 ret = ceph_monc_validate_auth(monc);
5723 static void osd_reencode_message(struct ceph_msg *msg)
5725 int type = le16_to_cpu(msg->hdr.type);
5727 if (type == CEPH_MSG_OSD_OP)
5728 encode_request_finish(msg);
5731 static int osd_sign_message(struct ceph_msg *msg)
5733 struct ceph_osd *o = msg->con->private;
5734 struct ceph_auth_handshake *auth = &o->o_auth;
5736 return ceph_auth_sign_message(auth, msg);
5739 static int osd_check_message_signature(struct ceph_msg *msg)
5741 struct ceph_osd *o = msg->con->private;
5742 struct ceph_auth_handshake *auth = &o->o_auth;
5744 return ceph_auth_check_message_signature(auth, msg);
5747 static void advance_cursor(struct ceph_msg_data_cursor *cursor, size_t len,
5754 page = ceph_msg_data_next(cursor, &poff, &plen);
5758 zero_user_segment(page, poff, poff + plen);
5760 ceph_msg_data_advance(cursor, plen);
5764 static int prep_next_sparse_read(struct ceph_connection *con,
5765 struct ceph_msg_data_cursor *cursor)
5767 struct ceph_osd *o = con->private;
5768 struct ceph_sparse_read *sr = &o->o_sparse_read;
5769 struct ceph_osd_request *req;
5770 struct ceph_osd_req_op *op;
5772 spin_lock(&o->o_requests_lock);
5773 req = lookup_request(&o->o_requests, le64_to_cpu(con->in_msg->hdr.tid));
5775 spin_unlock(&o->o_requests_lock);
5779 if (o->o_sparse_op_idx < 0) {
5780 u64 srlen = sparse_data_requested(req);
5782 dout("%s: [%d] starting new sparse read req. srlen=0x%llx\n",
5783 __func__, o->o_osd, srlen);
5784 ceph_msg_data_cursor_init(cursor, con->in_msg, srlen);
5788 op = &req->r_ops[o->o_sparse_op_idx];
5790 WARN_ON_ONCE(op->extent.sparse_ext);
5792 /* hand back buffer we took earlier */
5793 op->extent.sparse_ext = sr->sr_extent;
5794 sr->sr_extent = NULL;
5795 op->extent.sparse_ext_cnt = sr->sr_count;
5797 dout("%s: [%d] completed extent array len %d cursor->resid %zd\n",
5798 __func__, o->o_osd, op->extent.sparse_ext_cnt, cursor->resid);
5799 /* Advance to end of data for this operation */
5800 end = ceph_sparse_ext_map_end(op);
5801 if (end < sr->sr_req_len)
5802 advance_cursor(cursor, sr->sr_req_len - end, false);
5805 ceph_init_sparse_read(sr);
5807 /* find next op in this request (if any) */
5808 while (++o->o_sparse_op_idx < req->r_num_ops) {
5809 op = &req->r_ops[o->o_sparse_op_idx];
5810 if (op->op == CEPH_OSD_OP_SPARSE_READ)
5814 /* reset for next sparse read request */
5815 spin_unlock(&o->o_requests_lock);
5816 o->o_sparse_op_idx = -1;
5819 sr->sr_req_off = op->extent.offset;
5820 sr->sr_req_len = op->extent.length;
5821 sr->sr_pos = sr->sr_req_off;
5822 dout("%s: [%d] new sparse read op at idx %d 0x%llx~0x%llx\n", __func__,
5823 o->o_osd, o->o_sparse_op_idx, sr->sr_req_off, sr->sr_req_len);
5825 /* hand off request's sparse extent map buffer */
5826 sr->sr_ext_len = op->extent.sparse_ext_cnt;
5827 op->extent.sparse_ext_cnt = 0;
5828 sr->sr_extent = op->extent.sparse_ext;
5829 op->extent.sparse_ext = NULL;
5831 spin_unlock(&o->o_requests_lock);
5836 static inline void convert_extent_map(struct ceph_sparse_read *sr)
5840 for (i = 0; i < sr->sr_count; i++) {
5841 struct ceph_sparse_extent *ext = &sr->sr_extent[i];
5843 ext->off = le64_to_cpu((__force __le64)ext->off);
5844 ext->len = le64_to_cpu((__force __le64)ext->len);
5848 static inline void convert_extent_map(struct ceph_sparse_read *sr)
5853 #define MAX_EXTENTS 4096
5855 static int osd_sparse_read(struct ceph_connection *con,
5856 struct ceph_msg_data_cursor *cursor,
5859 struct ceph_osd *o = con->private;
5860 struct ceph_sparse_read *sr = &o->o_sparse_read;
5861 u32 count = sr->sr_count;
5865 switch (sr->sr_state) {
5866 case CEPH_SPARSE_READ_HDR:
5868 ret = prep_next_sparse_read(con, cursor);
5872 /* number of extents */
5873 ret = sizeof(sr->sr_count);
5874 *pbuf = (char *)&sr->sr_count;
5875 sr->sr_state = CEPH_SPARSE_READ_EXTENTS;
5877 case CEPH_SPARSE_READ_EXTENTS:
5878 /* Convert sr_count to host-endian */
5879 count = le32_to_cpu((__force __le32)sr->sr_count);
5880 sr->sr_count = count;
5881 dout("[%d] got %u extents\n", o->o_osd, count);
5884 if (!sr->sr_extent || count > sr->sr_ext_len) {
5886 * Apply a hard cap to the number of extents.
5887 * If we have more, assume something is wrong.
5889 if (count > MAX_EXTENTS) {
5890 dout("%s: OSD returned 0x%x extents in a single reply!\n",
5895 /* no extent array provided, or too short */
5896 kfree(sr->sr_extent);
5897 sr->sr_extent = kmalloc_array(count,
5898 sizeof(*sr->sr_extent),
5902 sr->sr_ext_len = count;
5904 ret = count * sizeof(*sr->sr_extent);
5905 *pbuf = (char *)sr->sr_extent;
5906 sr->sr_state = CEPH_SPARSE_READ_DATA_LEN;
5909 /* No extents? Read data len */
5911 case CEPH_SPARSE_READ_DATA_LEN:
5912 convert_extent_map(sr);
5913 ret = sizeof(sr->sr_datalen);
5914 *pbuf = (char *)&sr->sr_datalen;
5915 sr->sr_state = CEPH_SPARSE_READ_DATA;
5917 case CEPH_SPARSE_READ_DATA:
5918 if (sr->sr_index >= count) {
5919 sr->sr_state = CEPH_SPARSE_READ_HDR;
5923 eoff = sr->sr_extent[sr->sr_index].off;
5924 elen = sr->sr_extent[sr->sr_index].len;
5926 dout("[%d] ext %d off 0x%llx len 0x%llx\n",
5927 o->o_osd, sr->sr_index, eoff, elen);
5929 if (elen > INT_MAX) {
5930 dout("Sparse read extent length too long (0x%llx)\n",
5935 /* zero out anything from sr_pos to start of extent */
5936 if (sr->sr_pos < eoff)
5937 advance_cursor(cursor, eoff - sr->sr_pos, true);
5939 /* Set position to end of extent */
5940 sr->sr_pos = eoff + elen;
5942 /* send back the new length and nullify the ptr */
5943 cursor->sr_resid = elen;
5947 /* Bump the array index */
5954 static const struct ceph_connection_operations osd_con_ops = {
5957 .sparse_read = osd_sparse_read,
5958 .alloc_msg = osd_alloc_msg,
5959 .dispatch = osd_dispatch,
5961 .reencode_message = osd_reencode_message,
5962 .get_authorizer = osd_get_authorizer,
5963 .add_authorizer_challenge = osd_add_authorizer_challenge,
5964 .verify_authorizer_reply = osd_verify_authorizer_reply,
5965 .invalidate_authorizer = osd_invalidate_authorizer,
5966 .sign_message = osd_sign_message,
5967 .check_message_signature = osd_check_message_signature,
5968 .get_auth_request = osd_get_auth_request,
5969 .handle_auth_reply_more = osd_handle_auth_reply_more,
5970 .handle_auth_done = osd_handle_auth_done,
5971 .handle_auth_bad_method = osd_handle_auth_bad_method,