2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
6 #include <linux/highmem.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
12 #include <linux/bio.h>
15 #include <linux/ceph/libceph.h>
16 #include <linux/ceph/osd_client.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/pagelist.h>
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static struct kmem_cache *ceph_osd_request_cache;
26 static const struct ceph_connection_operations osd_con_ops;
28 static void __send_queued(struct ceph_osd_client *osdc);
29 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
30 static void __register_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __unregister_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
34 static void __unregister_linger_request(struct ceph_osd_client *osdc,
35 struct ceph_osd_request *req);
36 static void __enqueue_request(struct ceph_osd_request *req);
37 static void __send_request(struct ceph_osd_client *osdc,
38 struct ceph_osd_request *req);
41 * Implement client access to distributed object storage cluster.
43 * All data objects are stored within a cluster/cloud of OSDs, or
44 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
45 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
46 * remote daemons serving up and coordinating consistent and safe
49 * Cluster membership and the mapping of data objects onto storage devices
50 * are described by the osd map.
52 * We keep track of pending OSD requests (read, write), resubmit
53 * requests to different OSDs when the cluster topology/data layout
54 * change, or retry the affected requests when the communications
55 * channel with an OSD is reset.
59 * calculate the mapping of a file extent onto an object, and fill out the
60 * request accordingly. shorten extent as necessary if it crosses an
63 * fill osd op in request message.
65 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
66 u64 *objnum, u64 *objoff, u64 *objlen)
72 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
76 if (*objlen < orig_len) {
78 dout(" skipping last %llu, final file extent %llu~%llu\n",
79 orig_len - *plen, off, *plen);
82 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
87 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89 memset(osd_data, 0, sizeof (*osd_data));
90 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
93 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
94 struct page **pages, u64 length, u32 alignment,
95 bool pages_from_pool, bool own_pages)
97 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
98 osd_data->pages = pages;
99 osd_data->length = length;
100 osd_data->alignment = alignment;
101 osd_data->pages_from_pool = pages_from_pool;
102 osd_data->own_pages = own_pages;
105 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
106 struct ceph_pagelist *pagelist)
108 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
109 osd_data->pagelist = pagelist;
113 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
114 struct bio *bio, size_t bio_length)
116 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
118 osd_data->bio_length = bio_length;
120 #endif /* CONFIG_BLOCK */
122 #define osd_req_op_data(oreq, whch, typ, fld) \
124 struct ceph_osd_request *__oreq = (oreq); \
125 unsigned int __whch = (whch); \
126 BUG_ON(__whch >= __oreq->r_num_ops); \
127 &__oreq->r_ops[__whch].typ.fld; \
130 static struct ceph_osd_data *
131 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
133 BUG_ON(which >= osd_req->r_num_ops);
135 return &osd_req->r_ops[which].raw_data_in;
138 struct ceph_osd_data *
139 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
142 return osd_req_op_data(osd_req, which, extent, osd_data);
144 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
146 struct ceph_osd_data *
147 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
150 return osd_req_op_data(osd_req, which, cls, response_data);
152 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
154 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
155 unsigned int which, struct page **pages,
156 u64 length, u32 alignment,
157 bool pages_from_pool, bool own_pages)
159 struct ceph_osd_data *osd_data;
161 osd_data = osd_req_op_raw_data_in(osd_req, which);
162 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
163 pages_from_pool, own_pages);
165 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
167 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
168 unsigned int which, struct page **pages,
169 u64 length, u32 alignment,
170 bool pages_from_pool, bool own_pages)
172 struct ceph_osd_data *osd_data;
174 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
175 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
176 pages_from_pool, own_pages);
178 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
180 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
181 unsigned int which, struct ceph_pagelist *pagelist)
183 struct ceph_osd_data *osd_data;
185 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
186 ceph_osd_data_pagelist_init(osd_data, pagelist);
188 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
191 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
192 unsigned int which, struct bio *bio, size_t bio_length)
194 struct ceph_osd_data *osd_data;
196 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
197 ceph_osd_data_bio_init(osd_data, bio, bio_length);
199 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
200 #endif /* CONFIG_BLOCK */
202 static void osd_req_op_cls_request_info_pagelist(
203 struct ceph_osd_request *osd_req,
204 unsigned int which, struct ceph_pagelist *pagelist)
206 struct ceph_osd_data *osd_data;
208 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
209 ceph_osd_data_pagelist_init(osd_data, pagelist);
212 void osd_req_op_cls_request_data_pagelist(
213 struct ceph_osd_request *osd_req,
214 unsigned int which, struct ceph_pagelist *pagelist)
216 struct ceph_osd_data *osd_data;
218 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
219 ceph_osd_data_pagelist_init(osd_data, pagelist);
221 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
223 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
224 unsigned int which, struct page **pages, u64 length,
225 u32 alignment, bool pages_from_pool, bool own_pages)
227 struct ceph_osd_data *osd_data;
229 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
230 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
231 pages_from_pool, own_pages);
233 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
235 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
236 unsigned int which, struct page **pages, u64 length,
237 u32 alignment, bool pages_from_pool, bool own_pages)
239 struct ceph_osd_data *osd_data;
241 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
242 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
243 pages_from_pool, own_pages);
245 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
247 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
249 switch (osd_data->type) {
250 case CEPH_OSD_DATA_TYPE_NONE:
252 case CEPH_OSD_DATA_TYPE_PAGES:
253 return osd_data->length;
254 case CEPH_OSD_DATA_TYPE_PAGELIST:
255 return (u64)osd_data->pagelist->length;
257 case CEPH_OSD_DATA_TYPE_BIO:
258 return (u64)osd_data->bio_length;
259 #endif /* CONFIG_BLOCK */
261 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
266 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
268 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
271 num_pages = calc_pages_for((u64)osd_data->alignment,
272 (u64)osd_data->length);
273 ceph_release_page_vector(osd_data->pages, num_pages);
275 ceph_osd_data_init(osd_data);
278 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
281 struct ceph_osd_req_op *op;
283 BUG_ON(which >= osd_req->r_num_ops);
284 op = &osd_req->r_ops[which];
287 case CEPH_OSD_OP_READ:
288 case CEPH_OSD_OP_WRITE:
289 case CEPH_OSD_OP_WRITEFULL:
290 ceph_osd_data_release(&op->extent.osd_data);
292 case CEPH_OSD_OP_CALL:
293 ceph_osd_data_release(&op->cls.request_info);
294 ceph_osd_data_release(&op->cls.request_data);
295 ceph_osd_data_release(&op->cls.response_data);
297 case CEPH_OSD_OP_SETXATTR:
298 case CEPH_OSD_OP_CMPXATTR:
299 ceph_osd_data_release(&op->xattr.osd_data);
301 case CEPH_OSD_OP_STAT:
302 ceph_osd_data_release(&op->raw_data_in);
312 static void ceph_osdc_release_request(struct kref *kref)
314 struct ceph_osd_request *req = container_of(kref,
315 struct ceph_osd_request, r_kref);
318 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
319 req->r_request, req->r_reply);
320 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
321 WARN_ON(!list_empty(&req->r_req_lru_item));
322 WARN_ON(!list_empty(&req->r_osd_item));
323 WARN_ON(!list_empty(&req->r_linger_item));
324 WARN_ON(!list_empty(&req->r_linger_osd_item));
328 ceph_msg_put(req->r_request);
330 ceph_msg_revoke_incoming(req->r_reply);
331 ceph_msg_put(req->r_reply);
334 for (which = 0; which < req->r_num_ops; which++)
335 osd_req_op_data_release(req, which);
337 ceph_oid_destroy(&req->r_base_oid);
338 ceph_oid_destroy(&req->r_target_oid);
339 ceph_put_snap_context(req->r_snapc);
342 mempool_free(req, req->r_osdc->req_mempool);
343 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
344 kmem_cache_free(ceph_osd_request_cache, req);
349 void ceph_osdc_get_request(struct ceph_osd_request *req)
351 dout("%s %p (was %d)\n", __func__, req,
352 atomic_read(&req->r_kref.refcount));
353 kref_get(&req->r_kref);
355 EXPORT_SYMBOL(ceph_osdc_get_request);
357 void ceph_osdc_put_request(struct ceph_osd_request *req)
360 dout("%s %p (was %d)\n", __func__, req,
361 atomic_read(&req->r_kref.refcount));
362 kref_put(&req->r_kref, ceph_osdc_release_request);
365 EXPORT_SYMBOL(ceph_osdc_put_request);
367 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
368 struct ceph_snap_context *snapc,
369 unsigned int num_ops,
373 struct ceph_osd_request *req;
376 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
377 req = mempool_alloc(osdc->req_mempool, gfp_flags);
378 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
379 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
381 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
382 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
388 /* req only, each op is zeroed in _osd_req_op_init() */
389 memset(req, 0, sizeof(*req));
392 req->r_mempool = use_mempool;
393 req->r_num_ops = num_ops;
394 req->r_snapid = CEPH_NOSNAP;
395 req->r_snapc = ceph_get_snap_context(snapc);
397 kref_init(&req->r_kref);
398 init_completion(&req->r_completion);
399 init_completion(&req->r_safe_completion);
400 RB_CLEAR_NODE(&req->r_node);
401 INIT_LIST_HEAD(&req->r_unsafe_item);
402 INIT_LIST_HEAD(&req->r_linger_item);
403 INIT_LIST_HEAD(&req->r_linger_osd_item);
404 INIT_LIST_HEAD(&req->r_req_lru_item);
405 INIT_LIST_HEAD(&req->r_osd_item);
407 ceph_oid_init(&req->r_base_oid);
408 req->r_base_oloc.pool = -1;
409 ceph_oid_init(&req->r_target_oid);
410 req->r_target_oloc.pool = -1;
412 dout("%s req %p\n", __func__, req);
415 EXPORT_SYMBOL(ceph_osdc_alloc_request);
417 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
419 struct ceph_osd_client *osdc = req->r_osdc;
420 struct ceph_msg *msg;
423 WARN_ON(ceph_oid_empty(&req->r_base_oid));
425 /* create request message */
426 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
427 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
428 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
429 msg_size += 1 + 8 + 4 + 4; /* pgid */
430 msg_size += 4 + req->r_base_oid.name_len; /* oid */
431 msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
432 msg_size += 8; /* snapid */
433 msg_size += 8; /* snap_seq */
434 msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
435 msg_size += 4; /* retry_attempt */
438 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
440 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
444 memset(msg->front.iov_base, 0, msg->front.iov_len);
445 req->r_request = msg;
447 /* create reply message */
448 msg_size = OSD_OPREPLY_FRONT_LEN;
449 msg_size += req->r_base_oid.name_len;
450 msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
453 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
455 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
463 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
465 static bool osd_req_opcode_valid(u16 opcode)
468 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
469 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
477 * This is an osd op init function for opcodes that have no data or
478 * other information associated with them. It also serves as a
479 * common init routine for all the other init functions, below.
481 static struct ceph_osd_req_op *
482 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
483 u16 opcode, u32 flags)
485 struct ceph_osd_req_op *op;
487 BUG_ON(which >= osd_req->r_num_ops);
488 BUG_ON(!osd_req_opcode_valid(opcode));
490 op = &osd_req->r_ops[which];
491 memset(op, 0, sizeof (*op));
498 void osd_req_op_init(struct ceph_osd_request *osd_req,
499 unsigned int which, u16 opcode, u32 flags)
501 (void)_osd_req_op_init(osd_req, which, opcode, flags);
503 EXPORT_SYMBOL(osd_req_op_init);
505 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
506 unsigned int which, u16 opcode,
507 u64 offset, u64 length,
508 u64 truncate_size, u32 truncate_seq)
510 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
512 size_t payload_len = 0;
514 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
515 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
516 opcode != CEPH_OSD_OP_TRUNCATE);
518 op->extent.offset = offset;
519 op->extent.length = length;
520 op->extent.truncate_size = truncate_size;
521 op->extent.truncate_seq = truncate_seq;
522 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
523 payload_len += length;
525 op->indata_len = payload_len;
527 EXPORT_SYMBOL(osd_req_op_extent_init);
529 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
530 unsigned int which, u64 length)
532 struct ceph_osd_req_op *op;
535 BUG_ON(which >= osd_req->r_num_ops);
536 op = &osd_req->r_ops[which];
537 previous = op->extent.length;
539 if (length == previous)
540 return; /* Nothing to do */
541 BUG_ON(length > previous);
543 op->extent.length = length;
544 op->indata_len -= previous - length;
546 EXPORT_SYMBOL(osd_req_op_extent_update);
548 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
549 unsigned int which, u64 offset_inc)
551 struct ceph_osd_req_op *op, *prev_op;
553 BUG_ON(which + 1 >= osd_req->r_num_ops);
555 prev_op = &osd_req->r_ops[which];
556 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
557 /* dup previous one */
558 op->indata_len = prev_op->indata_len;
559 op->outdata_len = prev_op->outdata_len;
560 op->extent = prev_op->extent;
562 op->extent.offset += offset_inc;
563 op->extent.length -= offset_inc;
565 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
566 op->indata_len -= offset_inc;
568 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
570 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
571 u16 opcode, const char *class, const char *method)
573 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
575 struct ceph_pagelist *pagelist;
576 size_t payload_len = 0;
579 BUG_ON(opcode != CEPH_OSD_OP_CALL);
581 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
583 ceph_pagelist_init(pagelist);
585 op->cls.class_name = class;
586 size = strlen(class);
587 BUG_ON(size > (size_t) U8_MAX);
588 op->cls.class_len = size;
589 ceph_pagelist_append(pagelist, class, size);
592 op->cls.method_name = method;
593 size = strlen(method);
594 BUG_ON(size > (size_t) U8_MAX);
595 op->cls.method_len = size;
596 ceph_pagelist_append(pagelist, method, size);
599 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
601 op->cls.argc = 0; /* currently unused */
603 op->indata_len = payload_len;
605 EXPORT_SYMBOL(osd_req_op_cls_init);
607 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
608 u16 opcode, const char *name, const void *value,
609 size_t size, u8 cmp_op, u8 cmp_mode)
611 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
613 struct ceph_pagelist *pagelist;
616 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
618 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
622 ceph_pagelist_init(pagelist);
624 payload_len = strlen(name);
625 op->xattr.name_len = payload_len;
626 ceph_pagelist_append(pagelist, name, payload_len);
628 op->xattr.value_len = size;
629 ceph_pagelist_append(pagelist, value, size);
632 op->xattr.cmp_op = cmp_op;
633 op->xattr.cmp_mode = cmp_mode;
635 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
636 op->indata_len = payload_len;
639 EXPORT_SYMBOL(osd_req_op_xattr_init);
641 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
642 unsigned int which, u16 opcode,
643 u64 cookie, u64 version, int flag)
645 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
648 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
650 op->watch.cookie = cookie;
651 op->watch.ver = version;
652 if (opcode == CEPH_OSD_OP_WATCH && flag)
653 op->watch.flag = (u8)1;
655 EXPORT_SYMBOL(osd_req_op_watch_init);
657 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
659 u64 expected_object_size,
660 u64 expected_write_size)
662 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
663 CEPH_OSD_OP_SETALLOCHINT,
666 op->alloc_hint.expected_object_size = expected_object_size;
667 op->alloc_hint.expected_write_size = expected_write_size;
670 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
671 * not worth a feature bit. Set FAILOK per-op flag to make
672 * sure older osds don't trip over an unsupported opcode.
674 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
676 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
678 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
679 struct ceph_osd_data *osd_data)
681 u64 length = ceph_osd_data_length(osd_data);
683 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
684 BUG_ON(length > (u64) SIZE_MAX);
686 ceph_msg_data_add_pages(msg, osd_data->pages,
687 length, osd_data->alignment);
688 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
690 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
692 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
693 ceph_msg_data_add_bio(msg, osd_data->bio, length);
696 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
700 static u64 osd_req_encode_op(struct ceph_osd_request *req,
701 struct ceph_osd_op *dst, unsigned int which)
703 struct ceph_osd_req_op *src;
704 struct ceph_osd_data *osd_data;
705 u64 request_data_len = 0;
708 BUG_ON(which >= req->r_num_ops);
709 src = &req->r_ops[which];
710 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
711 pr_err("unrecognized osd opcode %d\n", src->op);
717 case CEPH_OSD_OP_STAT:
718 osd_data = &src->raw_data_in;
719 ceph_osdc_msg_data_add(req->r_reply, osd_data);
721 case CEPH_OSD_OP_READ:
722 case CEPH_OSD_OP_WRITE:
723 case CEPH_OSD_OP_WRITEFULL:
724 case CEPH_OSD_OP_ZERO:
725 case CEPH_OSD_OP_TRUNCATE:
726 if (src->op == CEPH_OSD_OP_WRITE ||
727 src->op == CEPH_OSD_OP_WRITEFULL)
728 request_data_len = src->extent.length;
729 dst->extent.offset = cpu_to_le64(src->extent.offset);
730 dst->extent.length = cpu_to_le64(src->extent.length);
731 dst->extent.truncate_size =
732 cpu_to_le64(src->extent.truncate_size);
733 dst->extent.truncate_seq =
734 cpu_to_le32(src->extent.truncate_seq);
735 osd_data = &src->extent.osd_data;
736 if (src->op == CEPH_OSD_OP_WRITE ||
737 src->op == CEPH_OSD_OP_WRITEFULL)
738 ceph_osdc_msg_data_add(req->r_request, osd_data);
740 ceph_osdc_msg_data_add(req->r_reply, osd_data);
742 case CEPH_OSD_OP_CALL:
743 dst->cls.class_len = src->cls.class_len;
744 dst->cls.method_len = src->cls.method_len;
745 osd_data = &src->cls.request_info;
746 ceph_osdc_msg_data_add(req->r_request, osd_data);
747 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
748 request_data_len = osd_data->pagelist->length;
750 osd_data = &src->cls.request_data;
751 data_length = ceph_osd_data_length(osd_data);
753 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
754 dst->cls.indata_len = cpu_to_le32(data_length);
755 ceph_osdc_msg_data_add(req->r_request, osd_data);
756 src->indata_len += data_length;
757 request_data_len += data_length;
759 osd_data = &src->cls.response_data;
760 ceph_osdc_msg_data_add(req->r_reply, osd_data);
762 case CEPH_OSD_OP_STARTSYNC:
764 case CEPH_OSD_OP_NOTIFY_ACK:
765 case CEPH_OSD_OP_WATCH:
766 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
767 dst->watch.ver = cpu_to_le64(src->watch.ver);
768 dst->watch.flag = src->watch.flag;
770 case CEPH_OSD_OP_SETALLOCHINT:
771 dst->alloc_hint.expected_object_size =
772 cpu_to_le64(src->alloc_hint.expected_object_size);
773 dst->alloc_hint.expected_write_size =
774 cpu_to_le64(src->alloc_hint.expected_write_size);
776 case CEPH_OSD_OP_SETXATTR:
777 case CEPH_OSD_OP_CMPXATTR:
778 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
779 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
780 dst->xattr.cmp_op = src->xattr.cmp_op;
781 dst->xattr.cmp_mode = src->xattr.cmp_mode;
782 osd_data = &src->xattr.osd_data;
783 ceph_osdc_msg_data_add(req->r_request, osd_data);
784 request_data_len = osd_data->pagelist->length;
786 case CEPH_OSD_OP_CREATE:
787 case CEPH_OSD_OP_DELETE:
790 pr_err("unsupported osd opcode %s\n",
791 ceph_osd_op_name(src->op));
797 dst->op = cpu_to_le16(src->op);
798 dst->flags = cpu_to_le32(src->flags);
799 dst->payload_len = cpu_to_le32(src->indata_len);
801 return request_data_len;
805 * build new request AND message, calculate layout, and adjust file
808 * if the file was recently truncated, we include information about its
809 * old and new size so that the object can be updated appropriately. (we
810 * avoid synchronously deleting truncated objects because it's slow.)
812 * if @do_sync, include a 'startsync' command so that the osd will flush
815 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
816 struct ceph_file_layout *layout,
817 struct ceph_vino vino,
819 unsigned int which, int num_ops,
820 int opcode, int flags,
821 struct ceph_snap_context *snapc,
826 struct ceph_osd_request *req;
832 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
833 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
834 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
836 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
843 req->r_flags = flags;
845 /* calculate max write size */
846 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
850 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
851 osd_req_op_init(req, which, opcode, 0);
853 u32 object_size = le32_to_cpu(layout->fl_object_size);
854 u32 object_base = off - objoff;
855 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
856 if (truncate_size <= object_base) {
859 truncate_size -= object_base;
860 if (truncate_size > object_size)
861 truncate_size = object_size;
864 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
865 truncate_size, truncate_seq);
868 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
869 ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
871 r = ceph_osdc_alloc_messages(req, GFP_NOFS);
878 ceph_osdc_put_request(req);
881 EXPORT_SYMBOL(ceph_osdc_new_request);
884 * We keep osd requests in an rbtree, sorted by ->r_tid.
886 static void __insert_request(struct ceph_osd_client *osdc,
887 struct ceph_osd_request *new)
889 struct rb_node **p = &osdc->requests.rb_node;
890 struct rb_node *parent = NULL;
891 struct ceph_osd_request *req = NULL;
895 req = rb_entry(parent, struct ceph_osd_request, r_node);
896 if (new->r_tid < req->r_tid)
898 else if (new->r_tid > req->r_tid)
904 rb_link_node(&new->r_node, parent, p);
905 rb_insert_color(&new->r_node, &osdc->requests);
908 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
911 struct ceph_osd_request *req;
912 struct rb_node *n = osdc->requests.rb_node;
915 req = rb_entry(n, struct ceph_osd_request, r_node);
916 if (tid < req->r_tid)
918 else if (tid > req->r_tid)
926 static struct ceph_osd_request *
927 __lookup_request_ge(struct ceph_osd_client *osdc,
930 struct ceph_osd_request *req;
931 struct rb_node *n = osdc->requests.rb_node;
934 req = rb_entry(n, struct ceph_osd_request, r_node);
935 if (tid < req->r_tid) {
939 } else if (tid > req->r_tid) {
948 static void __kick_linger_request(struct ceph_osd_request *req)
950 struct ceph_osd_client *osdc = req->r_osdc;
951 struct ceph_osd *osd = req->r_osd;
954 * Linger requests need to be resent with a new tid to avoid
955 * the dup op detection logic on the OSDs. Achieve this with
956 * a re-register dance instead of open-coding.
958 ceph_osdc_get_request(req);
959 if (!list_empty(&req->r_linger_item))
960 __unregister_linger_request(osdc, req);
962 __unregister_request(osdc, req);
963 __register_request(osdc, req);
964 ceph_osdc_put_request(req);
967 * Unless request has been registered as both normal and
968 * lingering, __unregister{,_linger}_request clears r_osd.
969 * However, here we need to preserve r_osd to make sure we
970 * requeue on the same OSD.
972 WARN_ON(req->r_osd || !osd);
975 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
976 __enqueue_request(req);
980 * Resubmit requests pending on the given osd.
982 static void __kick_osd_requests(struct ceph_osd_client *osdc,
983 struct ceph_osd *osd)
985 struct ceph_osd_request *req, *nreq;
987 LIST_HEAD(resend_linger);
990 dout("%s osd%d\n", __func__, osd->o_osd);
991 err = __reset_osd(osdc, osd);
996 * Build up a list of requests to resend by traversing the
997 * osd's list of requests. Requests for a given object are
998 * sent in tid order, and that is also the order they're
999 * kept on this list. Therefore all requests that are in
1000 * flight will be found first, followed by all requests that
1001 * have not yet been sent. And to resend requests while
1002 * preserving this order we will want to put any sent
1003 * requests back on the front of the osd client's unsent
1006 * So we build a separate ordered list of already-sent
1007 * requests for the affected osd and splice it onto the
1008 * front of the osd client's unsent list. Once we've seen a
1009 * request that has not yet been sent we're done. Those
1010 * requests are already sitting right where they belong.
1012 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
1016 if (!req->r_linger) {
1017 dout("%s requeueing %p tid %llu\n", __func__, req,
1019 list_move_tail(&req->r_req_lru_item, &resend);
1020 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1022 list_move_tail(&req->r_req_lru_item, &resend_linger);
1025 list_splice(&resend, &osdc->req_unsent);
1028 * Both registered and not yet registered linger requests are
1029 * enqueued with a new tid on the same OSD. We add/move them
1030 * to req_unsent/o_requests at the end to keep things in tid
1033 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
1034 r_linger_osd_item) {
1035 WARN_ON(!list_empty(&req->r_req_lru_item));
1036 __kick_linger_request(req);
1039 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1040 __kick_linger_request(req);
1044 * If the osd connection drops, we need to resubmit all requests.
1046 static void osd_reset(struct ceph_connection *con)
1048 struct ceph_osd *osd = con->private;
1049 struct ceph_osd_client *osdc;
1053 dout("osd_reset osd%d\n", osd->o_osd);
1055 down_read(&osdc->map_sem);
1056 mutex_lock(&osdc->request_mutex);
1057 __kick_osd_requests(osdc, osd);
1058 __send_queued(osdc);
1059 mutex_unlock(&osdc->request_mutex);
1060 up_read(&osdc->map_sem);
1064 * Track open sessions with osds.
1066 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1068 struct ceph_osd *osd;
1070 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1074 atomic_set(&osd->o_ref, 1);
1077 RB_CLEAR_NODE(&osd->o_node);
1078 INIT_LIST_HEAD(&osd->o_requests);
1079 INIT_LIST_HEAD(&osd->o_linger_requests);
1080 INIT_LIST_HEAD(&osd->o_osd_lru);
1081 osd->o_incarnation = 1;
1083 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1085 INIT_LIST_HEAD(&osd->o_keepalive_item);
1089 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1091 if (atomic_inc_not_zero(&osd->o_ref)) {
1092 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1093 atomic_read(&osd->o_ref));
1096 dout("get_osd %p FAIL\n", osd);
1101 static void put_osd(struct ceph_osd *osd)
1103 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1104 atomic_read(&osd->o_ref) - 1);
1105 if (atomic_dec_and_test(&osd->o_ref)) {
1106 if (osd->o_auth.authorizer)
1107 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1113 * remove an osd from our map
1115 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1117 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1118 WARN_ON(!list_empty(&osd->o_requests));
1119 WARN_ON(!list_empty(&osd->o_linger_requests));
1121 list_del_init(&osd->o_osd_lru);
1122 rb_erase(&osd->o_node, &osdc->osds);
1123 RB_CLEAR_NODE(&osd->o_node);
1126 static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1128 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1130 if (!RB_EMPTY_NODE(&osd->o_node)) {
1131 ceph_con_close(&osd->o_con);
1132 __remove_osd(osdc, osd);
1137 static void remove_all_osds(struct ceph_osd_client *osdc)
1139 dout("%s %p\n", __func__, osdc);
1140 mutex_lock(&osdc->request_mutex);
1141 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1142 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1143 struct ceph_osd, o_node);
1144 remove_osd(osdc, osd);
1146 mutex_unlock(&osdc->request_mutex);
1149 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1150 struct ceph_osd *osd)
1152 dout("%s %p\n", __func__, osd);
1153 BUG_ON(!list_empty(&osd->o_osd_lru));
1155 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1156 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1159 static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1160 struct ceph_osd *osd)
1162 dout("%s %p\n", __func__, osd);
1164 if (list_empty(&osd->o_requests) &&
1165 list_empty(&osd->o_linger_requests))
1166 __move_osd_to_lru(osdc, osd);
1169 static void __remove_osd_from_lru(struct ceph_osd *osd)
1171 dout("__remove_osd_from_lru %p\n", osd);
1172 if (!list_empty(&osd->o_osd_lru))
1173 list_del_init(&osd->o_osd_lru);
1176 static void remove_old_osds(struct ceph_osd_client *osdc)
1178 struct ceph_osd *osd, *nosd;
1180 dout("__remove_old_osds %p\n", osdc);
1181 mutex_lock(&osdc->request_mutex);
1182 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1183 if (time_before(jiffies, osd->lru_ttl))
1185 remove_osd(osdc, osd);
1187 mutex_unlock(&osdc->request_mutex);
1193 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1195 struct ceph_entity_addr *peer_addr;
1197 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1198 if (list_empty(&osd->o_requests) &&
1199 list_empty(&osd->o_linger_requests)) {
1200 remove_osd(osdc, osd);
1204 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1205 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1206 !ceph_con_opened(&osd->o_con)) {
1207 struct ceph_osd_request *req;
1209 dout("osd addr hasn't changed and connection never opened, "
1210 "letting msgr retry\n");
1211 /* touch each r_stamp for handle_timeout()'s benfit */
1212 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1213 req->r_stamp = jiffies;
1218 ceph_con_close(&osd->o_con);
1219 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1220 osd->o_incarnation++;
1225 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1227 struct rb_node **p = &osdc->osds.rb_node;
1228 struct rb_node *parent = NULL;
1229 struct ceph_osd *osd = NULL;
1231 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1234 osd = rb_entry(parent, struct ceph_osd, o_node);
1235 if (new->o_osd < osd->o_osd)
1237 else if (new->o_osd > osd->o_osd)
1238 p = &(*p)->rb_right;
1243 rb_link_node(&new->o_node, parent, p);
1244 rb_insert_color(&new->o_node, &osdc->osds);
1247 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1249 struct ceph_osd *osd;
1250 struct rb_node *n = osdc->osds.rb_node;
1253 osd = rb_entry(n, struct ceph_osd, o_node);
1256 else if (o > osd->o_osd)
1264 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1266 schedule_delayed_work(&osdc->timeout_work,
1267 osdc->client->options->osd_keepalive_timeout);
1270 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1272 cancel_delayed_work(&osdc->timeout_work);
1276 * Register request, assign tid. If this is the first request, set up
1277 * the timeout event.
1279 static void __register_request(struct ceph_osd_client *osdc,
1280 struct ceph_osd_request *req)
1282 req->r_tid = ++osdc->last_tid;
1283 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1284 dout("__register_request %p tid %lld\n", req, req->r_tid);
1285 __insert_request(osdc, req);
1286 ceph_osdc_get_request(req);
1287 osdc->num_requests++;
1288 if (osdc->num_requests == 1) {
1289 dout(" first request, scheduling timeout\n");
1290 __schedule_osd_timeout(osdc);
1295 * called under osdc->request_mutex
1297 static void __unregister_request(struct ceph_osd_client *osdc,
1298 struct ceph_osd_request *req)
1300 if (RB_EMPTY_NODE(&req->r_node)) {
1301 dout("__unregister_request %p tid %lld not registered\n",
1306 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1307 rb_erase(&req->r_node, &osdc->requests);
1308 RB_CLEAR_NODE(&req->r_node);
1309 osdc->num_requests--;
1312 /* make sure the original request isn't in flight. */
1313 ceph_msg_revoke(req->r_request);
1315 list_del_init(&req->r_osd_item);
1316 maybe_move_osd_to_lru(osdc, req->r_osd);
1317 if (list_empty(&req->r_linger_osd_item))
1321 list_del_init(&req->r_req_lru_item);
1322 ceph_osdc_put_request(req);
1324 if (osdc->num_requests == 0) {
1325 dout(" no requests, canceling timeout\n");
1326 __cancel_osd_timeout(osdc);
1331 * Cancel a previously queued request message
1333 static void __cancel_request(struct ceph_osd_request *req)
1335 if (req->r_sent && req->r_osd) {
1336 ceph_msg_revoke(req->r_request);
1341 static void __register_linger_request(struct ceph_osd_client *osdc,
1342 struct ceph_osd_request *req)
1344 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1345 WARN_ON(!req->r_linger);
1347 ceph_osdc_get_request(req);
1348 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1350 list_add_tail(&req->r_linger_osd_item,
1351 &req->r_osd->o_linger_requests);
1354 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1355 struct ceph_osd_request *req)
1357 WARN_ON(!req->r_linger);
1359 if (list_empty(&req->r_linger_item)) {
1360 dout("%s %p tid %llu not registered\n", __func__, req,
1365 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1366 list_del_init(&req->r_linger_item);
1369 list_del_init(&req->r_linger_osd_item);
1370 maybe_move_osd_to_lru(osdc, req->r_osd);
1371 if (list_empty(&req->r_osd_item))
1374 ceph_osdc_put_request(req);
1377 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1378 struct ceph_osd_request *req)
1380 if (!req->r_linger) {
1381 dout("set_request_linger %p\n", req);
1385 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1388 * Returns whether a request should be blocked from being sent
1389 * based on the current osdmap and osd_client settings.
1391 * Caller should hold map_sem for read.
1393 static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1394 struct ceph_osd_request *req)
1396 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1397 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1398 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1399 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1400 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1404 * Calculate mapping of a request to a PG. Takes tiering into account.
1406 static int __calc_request_pg(struct ceph_osdmap *osdmap,
1407 struct ceph_osd_request *req,
1408 struct ceph_pg *pg_out)
1410 bool need_check_tiering;
1412 need_check_tiering = false;
1413 if (req->r_target_oloc.pool == -1) {
1414 req->r_target_oloc = req->r_base_oloc; /* struct */
1415 need_check_tiering = true;
1417 if (ceph_oid_empty(&req->r_target_oid)) {
1418 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1419 need_check_tiering = true;
1422 if (need_check_tiering &&
1423 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1424 struct ceph_pg_pool_info *pi;
1426 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
1428 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1430 req->r_target_oloc.pool = pi->read_tier;
1431 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1432 pi->write_tier >= 0)
1433 req->r_target_oloc.pool = pi->write_tier;
1435 /* !pi is caught in ceph_oloc_oid_to_pg() */
1438 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1439 &req->r_target_oid, pg_out);
1442 static void __enqueue_request(struct ceph_osd_request *req)
1444 struct ceph_osd_client *osdc = req->r_osdc;
1446 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1447 req->r_osd ? req->r_osd->o_osd : -1);
1450 __remove_osd_from_lru(req->r_osd);
1451 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1452 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1454 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1459 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1460 * (as needed), and set the request r_osd appropriately. If there is
1461 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1462 * (unsent, homeless) or leave on in-flight lru.
1464 * Return 0 if unchanged, 1 if changed, or negative on error.
1466 * Caller should hold map_sem for read and request_mutex.
1468 static int __map_request(struct ceph_osd_client *osdc,
1469 struct ceph_osd_request *req, int force_resend)
1471 struct ceph_pg pgid;
1472 int acting[CEPH_PG_MAX_SIZE];
1477 dout("map_request %p tid %lld\n", req, req->r_tid);
1479 err = __calc_request_pg(osdc->osdmap, req, &pgid);
1481 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1486 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1490 was_paused = req->r_paused;
1491 req->r_paused = __req_should_be_paused(osdc, req);
1492 if (was_paused && !req->r_paused)
1495 if ((!force_resend &&
1496 req->r_osd && req->r_osd->o_osd == o &&
1497 req->r_sent >= req->r_osd->o_incarnation &&
1498 req->r_num_pg_osds == num &&
1499 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1500 (req->r_osd == NULL && o == -1) ||
1502 return 0; /* no change */
1504 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1505 req->r_tid, pgid.pool, pgid.seed, o,
1506 req->r_osd ? req->r_osd->o_osd : -1);
1508 /* record full pg acting set */
1509 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1510 req->r_num_pg_osds = num;
1513 __cancel_request(req);
1514 list_del_init(&req->r_osd_item);
1515 list_del_init(&req->r_linger_osd_item);
1519 req->r_osd = __lookup_osd(osdc, o);
1520 if (!req->r_osd && o >= 0) {
1522 req->r_osd = create_osd(osdc, o);
1524 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1528 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1529 __insert_osd(osdc, req->r_osd);
1531 ceph_con_open(&req->r_osd->o_con,
1532 CEPH_ENTITY_TYPE_OSD, o,
1533 &osdc->osdmap->osd_addr[o]);
1536 __enqueue_request(req);
1537 err = 1; /* osd or pg changed */
1544 * caller should hold map_sem (for read) and request_mutex
1546 static void __send_request(struct ceph_osd_client *osdc,
1547 struct ceph_osd_request *req)
1551 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1552 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1553 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1555 /* fill in message content that changes each time we send it */
1556 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1557 put_unaligned_le32(req->r_flags, req->r_request_flags);
1558 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1559 p = req->r_request_pgid;
1560 ceph_encode_64(&p, req->r_pgid.pool);
1561 ceph_encode_32(&p, req->r_pgid.seed);
1562 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1563 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1564 sizeof(req->r_reassert_version));
1566 req->r_stamp = jiffies;
1567 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1569 ceph_msg_get(req->r_request); /* send consumes a ref */
1571 req->r_sent = req->r_osd->o_incarnation;
1573 ceph_con_send(&req->r_osd->o_con, req->r_request);
1577 * Send any requests in the queue (req_unsent).
1579 static void __send_queued(struct ceph_osd_client *osdc)
1581 struct ceph_osd_request *req, *tmp;
1583 dout("__send_queued\n");
1584 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1585 __send_request(osdc, req);
1589 * Caller should hold map_sem for read and request_mutex.
1591 static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1592 struct ceph_osd_request *req,
1597 __register_request(osdc, req);
1599 req->r_got_reply = 0;
1600 rc = __map_request(osdc, req, 0);
1603 dout("osdc_start_request failed map, "
1604 " will retry %lld\n", req->r_tid);
1607 __unregister_request(osdc, req);
1612 if (req->r_osd == NULL) {
1613 dout("send_request %p no up osds in pg\n", req);
1614 ceph_monc_request_next_osdmap(&osdc->client->monc);
1616 __send_queued(osdc);
1623 * Timeout callback, called every N seconds when 1 or more osd
1624 * requests has been active for more than N seconds. When this
1625 * happens, we ping all OSDs with requests who have timed out to
1626 * ensure any communications channel reset is detected. Reset the
1627 * request timeouts another N seconds in the future as we go.
1628 * Reschedule the timeout event another N seconds in future (unless
1629 * there are no open requests).
1631 static void handle_timeout(struct work_struct *work)
1633 struct ceph_osd_client *osdc =
1634 container_of(work, struct ceph_osd_client, timeout_work.work);
1635 struct ceph_options *opts = osdc->client->options;
1636 struct ceph_osd_request *req;
1637 struct ceph_osd *osd;
1638 struct list_head slow_osds;
1640 down_read(&osdc->map_sem);
1642 ceph_monc_request_next_osdmap(&osdc->client->monc);
1644 mutex_lock(&osdc->request_mutex);
1647 * ping osds that are a bit slow. this ensures that if there
1648 * is a break in the TCP connection we will notice, and reopen
1649 * a connection with that osd (from the fault callback).
1651 INIT_LIST_HEAD(&slow_osds);
1652 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1653 if (time_before(jiffies,
1654 req->r_stamp + opts->osd_keepalive_timeout))
1659 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1660 req->r_tid, osd->o_osd);
1661 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1663 while (!list_empty(&slow_osds)) {
1664 osd = list_entry(slow_osds.next, struct ceph_osd,
1666 list_del_init(&osd->o_keepalive_item);
1667 ceph_con_keepalive(&osd->o_con);
1670 __schedule_osd_timeout(osdc);
1671 __send_queued(osdc);
1672 mutex_unlock(&osdc->request_mutex);
1673 up_read(&osdc->map_sem);
1676 static void handle_osds_timeout(struct work_struct *work)
1678 struct ceph_osd_client *osdc =
1679 container_of(work, struct ceph_osd_client,
1680 osds_timeout_work.work);
1681 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
1683 dout("osds timeout\n");
1684 down_read(&osdc->map_sem);
1685 remove_old_osds(osdc);
1686 up_read(&osdc->map_sem);
1688 schedule_delayed_work(&osdc->osds_timeout_work,
1689 round_jiffies_relative(delay));
1692 static int ceph_oloc_decode(void **p, void *end,
1693 struct ceph_object_locator *oloc)
1695 u8 struct_v, struct_cv;
1700 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1701 struct_v = ceph_decode_8(p);
1702 struct_cv = ceph_decode_8(p);
1704 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1705 struct_v, struct_cv);
1708 if (struct_cv > 6) {
1709 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1710 struct_v, struct_cv);
1713 len = ceph_decode_32(p);
1714 ceph_decode_need(p, end, len, e_inval);
1715 struct_end = *p + len;
1717 oloc->pool = ceph_decode_64(p);
1718 *p += 4; /* skip preferred */
1720 len = ceph_decode_32(p);
1722 pr_warn("ceph_object_locator::key is set\n");
1726 if (struct_v >= 5) {
1727 len = ceph_decode_32(p);
1729 pr_warn("ceph_object_locator::nspace is set\n");
1734 if (struct_v >= 6) {
1735 s64 hash = ceph_decode_64(p);
1737 pr_warn("ceph_object_locator::hash is set\n");
1752 static int ceph_redirect_decode(void **p, void *end,
1753 struct ceph_request_redirect *redir)
1755 u8 struct_v, struct_cv;
1760 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1761 struct_v = ceph_decode_8(p);
1762 struct_cv = ceph_decode_8(p);
1763 if (struct_cv > 1) {
1764 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1765 struct_v, struct_cv);
1768 len = ceph_decode_32(p);
1769 ceph_decode_need(p, end, len, e_inval);
1770 struct_end = *p + len;
1772 ret = ceph_oloc_decode(p, end, &redir->oloc);
1776 len = ceph_decode_32(p);
1778 pr_warn("ceph_request_redirect::object_name is set\n");
1782 len = ceph_decode_32(p);
1783 *p += len; /* skip osd_instructions */
1795 static void complete_request(struct ceph_osd_request *req)
1797 complete_all(&req->r_safe_completion); /* fsync waiter */
1801 * handle osd op reply. either call the callback if it is specified,
1802 * or do the completion to wake up the waiting thread.
1804 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1807 struct ceph_osd_request *req;
1808 struct ceph_request_redirect redir;
1811 unsigned int numops;
1812 int payload_len, flags;
1818 u64 reassert_version;
1820 int already_completed;
1825 tid = le64_to_cpu(msg->hdr.tid);
1826 dout("handle_reply %p tid %llu\n", msg, tid);
1828 p = msg->front.iov_base;
1829 end = p + msg->front.iov_len;
1831 ceph_decode_need(&p, end, 4, bad);
1832 object_len = ceph_decode_32(&p);
1833 ceph_decode_need(&p, end, object_len, bad);
1836 err = ceph_decode_pgid(&p, end, &pg);
1840 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1841 flags = ceph_decode_64(&p);
1842 result = ceph_decode_32(&p);
1843 reassert_epoch = ceph_decode_32(&p);
1844 reassert_version = ceph_decode_64(&p);
1845 osdmap_epoch = ceph_decode_32(&p);
1848 down_read(&osdc->map_sem);
1849 mutex_lock(&osdc->request_mutex);
1850 req = __lookup_request(osdc, tid);
1852 dout("handle_reply tid %llu dne\n", tid);
1855 ceph_osdc_get_request(req);
1857 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1860 ceph_decode_need(&p, end, 4, bad_put);
1861 numops = ceph_decode_32(&p);
1862 if (numops > CEPH_OSD_MAX_OPS)
1864 if (numops != req->r_num_ops)
1867 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1868 for (i = 0; i < numops; i++) {
1869 struct ceph_osd_op *op = p;
1872 len = le32_to_cpu(op->payload_len);
1873 req->r_ops[i].outdata_len = len;
1874 dout(" op %d has %d bytes\n", i, len);
1878 bytes = le32_to_cpu(msg->hdr.data_len);
1879 if (payload_len != bytes) {
1880 pr_warn("sum of op payload lens %d != data_len %d\n",
1881 payload_len, bytes);
1885 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1886 retry_attempt = ceph_decode_32(&p);
1887 for (i = 0; i < numops; i++)
1888 req->r_ops[i].rval = ceph_decode_32(&p);
1890 if (le16_to_cpu(msg->hdr.version) >= 6) {
1891 p += 8 + 4; /* skip replay_version */
1892 p += 8; /* skip user_version */
1894 if (le16_to_cpu(msg->hdr.version) >= 7)
1895 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1903 err = ceph_redirect_decode(&p, end, &redir);
1907 redir.oloc.pool = -1;
1910 if (redir.oloc.pool != -1) {
1911 dout("redirect pool %lld\n", redir.oloc.pool);
1913 __unregister_request(osdc, req);
1915 req->r_target_oloc = redir.oloc; /* struct */
1918 * Start redirect requests with nofail=true. If
1919 * mapping fails, request will end up on the notarget
1920 * list, waiting for the new osdmap (which can take
1921 * a while), even though the original request mapped
1922 * successfully. In the future we might want to follow
1923 * original request's nofail setting here.
1925 err = __ceph_osdc_start_request(osdc, req, true);
1931 already_completed = req->r_got_reply;
1932 if (!req->r_got_reply) {
1933 req->r_result = result;
1934 dout("handle_reply result %d bytes %d\n", req->r_result,
1936 if (req->r_result == 0)
1937 req->r_result = bytes;
1939 /* in case this is a write and we need to replay, */
1940 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1941 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1943 req->r_got_reply = 1;
1944 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1945 dout("handle_reply tid %llu dup ack\n", tid);
1949 dout("handle_reply tid %llu flags %d\n", tid, flags);
1951 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1952 __register_linger_request(osdc, req);
1954 /* either this is a read, or we got the safe response */
1956 (flags & CEPH_OSD_FLAG_ONDISK) ||
1957 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1958 __unregister_request(osdc, req);
1960 mutex_unlock(&osdc->request_mutex);
1961 up_read(&osdc->map_sem);
1963 if (!already_completed) {
1964 if (req->r_unsafe_callback &&
1965 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1966 req->r_unsafe_callback(req, true);
1967 if (req->r_callback)
1968 req->r_callback(req, msg);
1970 complete_all(&req->r_completion);
1973 if (flags & CEPH_OSD_FLAG_ONDISK) {
1974 if (req->r_unsafe_callback && already_completed)
1975 req->r_unsafe_callback(req, false);
1976 complete_request(req);
1980 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1981 ceph_osdc_put_request(req);
1984 mutex_unlock(&osdc->request_mutex);
1985 up_read(&osdc->map_sem);
1989 req->r_result = -EIO;
1990 __unregister_request(osdc, req);
1991 if (req->r_callback)
1992 req->r_callback(req, msg);
1994 complete_all(&req->r_completion);
1995 complete_request(req);
1996 ceph_osdc_put_request(req);
1998 mutex_unlock(&osdc->request_mutex);
1999 up_read(&osdc->map_sem);
2001 pr_err("corrupt osd_op_reply got %d %d\n",
2002 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
2006 static void reset_changed_osds(struct ceph_osd_client *osdc)
2008 struct rb_node *p, *n;
2010 dout("%s %p\n", __func__, osdc);
2011 for (p = rb_first(&osdc->osds); p; p = n) {
2012 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
2015 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2016 memcmp(&osd->o_con.peer_addr,
2017 ceph_osd_addr(osdc->osdmap,
2019 sizeof(struct ceph_entity_addr)) != 0)
2020 __reset_osd(osdc, osd);
2025 * Requeue requests whose mapping to an OSD has changed. If requests map to
2026 * no osd, request a new map.
2028 * Caller should hold map_sem for read.
2030 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2031 bool force_resend_writes)
2033 struct ceph_osd_request *req, *nreq;
2037 bool force_resend_req;
2039 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2040 force_resend_writes ? " (force resend writes)" : "");
2041 mutex_lock(&osdc->request_mutex);
2042 for (p = rb_first(&osdc->requests); p; ) {
2043 req = rb_entry(p, struct ceph_osd_request, r_node);
2047 * For linger requests that have not yet been
2048 * registered, move them to the linger list; they'll
2049 * be sent to the osd in the loop below. Unregister
2050 * the request before re-registering it as a linger
2051 * request to ensure the __map_request() below
2052 * will decide it needs to be sent.
2054 if (req->r_linger && list_empty(&req->r_linger_item)) {
2055 dout("%p tid %llu restart on osd%d\n",
2057 req->r_osd ? req->r_osd->o_osd : -1);
2058 ceph_osdc_get_request(req);
2059 __unregister_request(osdc, req);
2060 __register_linger_request(osdc, req);
2061 ceph_osdc_put_request(req);
2065 force_resend_req = force_resend ||
2066 (force_resend_writes &&
2067 req->r_flags & CEPH_OSD_FLAG_WRITE);
2068 err = __map_request(osdc, req, force_resend_req);
2070 continue; /* error */
2071 if (req->r_osd == NULL) {
2072 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2073 needmap++; /* request a newer map */
2074 } else if (err > 0) {
2075 if (!req->r_linger) {
2076 dout("%p tid %llu requeued on osd%d\n", req,
2078 req->r_osd ? req->r_osd->o_osd : -1);
2079 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2084 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2086 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2088 err = __map_request(osdc, req,
2089 force_resend || force_resend_writes);
2090 dout("__map_request returned %d\n", err);
2092 continue; /* hrm! */
2093 if (req->r_osd == NULL || err > 0) {
2094 if (req->r_osd == NULL) {
2095 dout("lingering %p tid %llu maps to no osd\n",
2098 * A homeless lingering request makes
2099 * no sense, as it's job is to keep
2100 * a particular OSD connection open.
2101 * Request a newer map and kick the
2102 * request, knowing that it won't be
2103 * resent until we actually get a map
2104 * that can tell us where to send it.
2109 dout("kicking lingering %p tid %llu osd%d\n", req,
2110 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2111 __register_request(osdc, req);
2112 __unregister_linger_request(osdc, req);
2115 reset_changed_osds(osdc);
2116 mutex_unlock(&osdc->request_mutex);
2119 dout("%d requests for down osds, need new map\n", needmap);
2120 ceph_monc_request_next_osdmap(&osdc->client->monc);
2126 * Process updated osd map.
2128 * The message contains any number of incremental and full maps, normally
2129 * indicating some sort of topology change in the cluster. Kick requests
2130 * off to different OSDs as needed.
2132 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2134 void *p, *end, *next;
2135 u32 nr_maps, maplen;
2137 struct ceph_osdmap *newmap = NULL, *oldmap;
2139 struct ceph_fsid fsid;
2142 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2143 p = msg->front.iov_base;
2144 end = p + msg->front.iov_len;
2147 ceph_decode_need(&p, end, sizeof(fsid), bad);
2148 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2149 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2152 down_write(&osdc->map_sem);
2154 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2156 /* incremental maps */
2157 ceph_decode_32_safe(&p, end, nr_maps, bad);
2158 dout(" %d inc maps\n", nr_maps);
2159 while (nr_maps > 0) {
2160 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2161 epoch = ceph_decode_32(&p);
2162 maplen = ceph_decode_32(&p);
2163 ceph_decode_need(&p, end, maplen, bad);
2165 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2166 dout("applying incremental map %u len %d\n",
2168 newmap = osdmap_apply_incremental(&p, next,
2170 &osdc->client->msgr);
2171 if (IS_ERR(newmap)) {
2172 err = PTR_ERR(newmap);
2176 if (newmap != osdc->osdmap) {
2177 ceph_osdmap_destroy(osdc->osdmap);
2178 osdc->osdmap = newmap;
2180 was_full = was_full ||
2181 ceph_osdmap_flag(osdc->osdmap,
2183 kick_requests(osdc, 0, was_full);
2185 dout("ignoring incremental map %u len %d\n",
2195 ceph_decode_32_safe(&p, end, nr_maps, bad);
2196 dout(" %d full maps\n", nr_maps);
2198 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2199 epoch = ceph_decode_32(&p);
2200 maplen = ceph_decode_32(&p);
2201 ceph_decode_need(&p, end, maplen, bad);
2203 dout("skipping non-latest full map %u len %d\n",
2205 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2206 dout("skipping full map %u len %d, "
2207 "older than our %u\n", epoch, maplen,
2208 osdc->osdmap->epoch);
2210 int skipped_map = 0;
2212 dout("taking full map %u len %d\n", epoch, maplen);
2213 newmap = ceph_osdmap_decode(&p, p+maplen);
2214 if (IS_ERR(newmap)) {
2215 err = PTR_ERR(newmap);
2219 oldmap = osdc->osdmap;
2220 osdc->osdmap = newmap;
2222 if (oldmap->epoch + 1 < newmap->epoch)
2224 ceph_osdmap_destroy(oldmap);
2226 was_full = was_full ||
2227 ceph_osdmap_flag(osdc->osdmap,
2229 kick_requests(osdc, skipped_map, was_full);
2238 downgrade_write(&osdc->map_sem);
2239 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2240 osdc->osdmap->epoch);
2243 * subscribe to subsequent osdmap updates if full to ensure
2244 * we find out when we are no longer full and stop returning
2247 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2248 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2249 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2250 ceph_monc_request_next_osdmap(&osdc->client->monc);
2252 mutex_lock(&osdc->request_mutex);
2253 __send_queued(osdc);
2254 mutex_unlock(&osdc->request_mutex);
2255 up_read(&osdc->map_sem);
2256 wake_up_all(&osdc->client->auth_wq);
2260 pr_err("osdc handle_map corrupt msg\n");
2262 up_write(&osdc->map_sem);
2266 * watch/notify callback event infrastructure
2268 * These callbacks are used both for watch and notify operations.
2270 static void __release_event(struct kref *kref)
2272 struct ceph_osd_event *event =
2273 container_of(kref, struct ceph_osd_event, kref);
2275 dout("__release_event %p\n", event);
2279 static void get_event(struct ceph_osd_event *event)
2281 kref_get(&event->kref);
2284 void ceph_osdc_put_event(struct ceph_osd_event *event)
2286 kref_put(&event->kref, __release_event);
2288 EXPORT_SYMBOL(ceph_osdc_put_event);
2290 static void __insert_event(struct ceph_osd_client *osdc,
2291 struct ceph_osd_event *new)
2293 struct rb_node **p = &osdc->event_tree.rb_node;
2294 struct rb_node *parent = NULL;
2295 struct ceph_osd_event *event = NULL;
2299 event = rb_entry(parent, struct ceph_osd_event, node);
2300 if (new->cookie < event->cookie)
2302 else if (new->cookie > event->cookie)
2303 p = &(*p)->rb_right;
2308 rb_link_node(&new->node, parent, p);
2309 rb_insert_color(&new->node, &osdc->event_tree);
2312 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2315 struct rb_node **p = &osdc->event_tree.rb_node;
2316 struct rb_node *parent = NULL;
2317 struct ceph_osd_event *event = NULL;
2321 event = rb_entry(parent, struct ceph_osd_event, node);
2322 if (cookie < event->cookie)
2324 else if (cookie > event->cookie)
2325 p = &(*p)->rb_right;
2332 static void __remove_event(struct ceph_osd_event *event)
2334 struct ceph_osd_client *osdc = event->osdc;
2336 if (!RB_EMPTY_NODE(&event->node)) {
2337 dout("__remove_event removed %p\n", event);
2338 rb_erase(&event->node, &osdc->event_tree);
2339 ceph_osdc_put_event(event);
2341 dout("__remove_event didn't remove %p\n", event);
2345 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2346 void (*event_cb)(u64, u64, u8, void *),
2347 void *data, struct ceph_osd_event **pevent)
2349 struct ceph_osd_event *event;
2351 event = kmalloc(sizeof(*event), GFP_NOIO);
2355 dout("create_event %p\n", event);
2356 event->cb = event_cb;
2357 event->one_shot = 0;
2360 INIT_LIST_HEAD(&event->osd_node);
2361 RB_CLEAR_NODE(&event->node);
2362 kref_init(&event->kref); /* one ref for us */
2363 kref_get(&event->kref); /* one ref for the caller */
2365 spin_lock(&osdc->event_lock);
2366 event->cookie = ++osdc->event_count;
2367 __insert_event(osdc, event);
2368 spin_unlock(&osdc->event_lock);
2373 EXPORT_SYMBOL(ceph_osdc_create_event);
2375 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2377 struct ceph_osd_client *osdc = event->osdc;
2379 dout("cancel_event %p\n", event);
2380 spin_lock(&osdc->event_lock);
2381 __remove_event(event);
2382 spin_unlock(&osdc->event_lock);
2383 ceph_osdc_put_event(event); /* caller's */
2385 EXPORT_SYMBOL(ceph_osdc_cancel_event);
2388 static void do_event_work(struct work_struct *work)
2390 struct ceph_osd_event_work *event_work =
2391 container_of(work, struct ceph_osd_event_work, work);
2392 struct ceph_osd_event *event = event_work->event;
2393 u64 ver = event_work->ver;
2394 u64 notify_id = event_work->notify_id;
2395 u8 opcode = event_work->opcode;
2397 dout("do_event_work completing %p\n", event);
2398 event->cb(ver, notify_id, opcode, event->data);
2399 dout("do_event_work completed %p\n", event);
2400 ceph_osdc_put_event(event);
2406 * Process osd watch notifications
2408 static void handle_watch_notify(struct ceph_osd_client *osdc,
2409 struct ceph_msg *msg)
2413 u64 cookie, ver, notify_id;
2415 struct ceph_osd_event *event;
2416 struct ceph_osd_event_work *event_work;
2418 p = msg->front.iov_base;
2419 end = p + msg->front.iov_len;
2421 ceph_decode_8_safe(&p, end, proto_ver, bad);
2422 ceph_decode_8_safe(&p, end, opcode, bad);
2423 ceph_decode_64_safe(&p, end, cookie, bad);
2424 ceph_decode_64_safe(&p, end, ver, bad);
2425 ceph_decode_64_safe(&p, end, notify_id, bad);
2427 spin_lock(&osdc->event_lock);
2428 event = __find_event(osdc, cookie);
2430 BUG_ON(event->one_shot);
2433 spin_unlock(&osdc->event_lock);
2434 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2435 cookie, ver, event);
2437 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2439 pr_err("couldn't allocate event_work\n");
2440 ceph_osdc_put_event(event);
2443 INIT_WORK(&event_work->work, do_event_work);
2444 event_work->event = event;
2445 event_work->ver = ver;
2446 event_work->notify_id = notify_id;
2447 event_work->opcode = opcode;
2449 queue_work(osdc->notify_wq, &event_work->work);
2455 pr_err("osdc handle_watch_notify corrupt msg\n");
2459 * build new request AND message
2462 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2463 struct ceph_snap_context *snapc, u64 snap_id,
2464 struct timespec *mtime)
2466 struct ceph_msg *msg = req->r_request;
2469 int flags = req->r_flags;
2473 req->r_snapid = snap_id;
2474 WARN_ON(snapc != req->r_snapc);
2476 /* encode request */
2477 msg->hdr.version = cpu_to_le16(4);
2479 p = msg->front.iov_base;
2480 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2481 req->r_request_osdmap_epoch = p;
2483 req->r_request_flags = p;
2485 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2486 ceph_encode_timespec(p, mtime);
2487 p += sizeof(struct ceph_timespec);
2488 req->r_request_reassert_version = p;
2489 p += sizeof(struct ceph_eversion); /* will get filled in */
2492 ceph_encode_8(&p, 4);
2493 ceph_encode_8(&p, 4);
2494 ceph_encode_32(&p, 8 + 4 + 4);
2495 req->r_request_pool = p;
2497 ceph_encode_32(&p, -1); /* preferred */
2498 ceph_encode_32(&p, 0); /* key len */
2500 ceph_encode_8(&p, 1);
2501 req->r_request_pgid = p;
2503 ceph_encode_32(&p, -1); /* preferred */
2506 ceph_encode_32(&p, req->r_base_oid.name_len);
2507 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2508 dout("oid %*pE len %d\n", req->r_base_oid.name_len,
2509 req->r_base_oid.name, req->r_base_oid.name_len);
2510 p += req->r_base_oid.name_len;
2512 /* ops--can imply data */
2513 ceph_encode_16(&p, (u16)req->r_num_ops);
2515 for (i = 0; i < req->r_num_ops; i++) {
2516 data_len += osd_req_encode_op(req, p, i);
2517 p += sizeof(struct ceph_osd_op);
2521 ceph_encode_64(&p, req->r_snapid);
2522 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2523 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2525 for (i = 0; i < req->r_snapc->num_snaps; i++) {
2526 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2530 req->r_request_attempts = p;
2534 if (flags & CEPH_OSD_FLAG_WRITE) {
2538 * The header "data_off" is a hint to the receiver
2539 * allowing it to align received data into its
2540 * buffers such that there's no need to re-copy
2541 * it before writing it to disk (direct I/O).
2543 data_off = (u16) (off & 0xffff);
2544 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2546 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2548 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2549 msg_size = p - msg->front.iov_base;
2550 msg->front.iov_len = msg_size;
2551 msg->hdr.front_len = cpu_to_le32(msg_size);
2553 dout("build_request msg_size was %d\n", (int)msg_size);
2555 EXPORT_SYMBOL(ceph_osdc_build_request);
2558 * Register request, send initial attempt.
2560 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2561 struct ceph_osd_request *req,
2566 down_read(&osdc->map_sem);
2567 mutex_lock(&osdc->request_mutex);
2569 rc = __ceph_osdc_start_request(osdc, req, nofail);
2571 mutex_unlock(&osdc->request_mutex);
2572 up_read(&osdc->map_sem);
2576 EXPORT_SYMBOL(ceph_osdc_start_request);
2579 * Unregister a registered request. The request is not completed (i.e.
2580 * no callbacks or wakeups) - higher layers are supposed to know what
2581 * they are canceling.
2583 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2585 struct ceph_osd_client *osdc = req->r_osdc;
2587 mutex_lock(&osdc->request_mutex);
2589 __unregister_linger_request(osdc, req);
2590 __unregister_request(osdc, req);
2591 mutex_unlock(&osdc->request_mutex);
2593 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2595 EXPORT_SYMBOL(ceph_osdc_cancel_request);
2598 * wait for a request to complete
2600 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2601 struct ceph_osd_request *req)
2605 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2607 rc = wait_for_completion_interruptible(&req->r_completion);
2609 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2610 ceph_osdc_cancel_request(req);
2611 complete_request(req);
2615 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2617 return req->r_result;
2619 EXPORT_SYMBOL(ceph_osdc_wait_request);
2622 * sync - wait for all in-flight requests to flush. avoid starvation.
2624 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2626 struct ceph_osd_request *req;
2627 u64 last_tid, next_tid = 0;
2629 mutex_lock(&osdc->request_mutex);
2630 last_tid = osdc->last_tid;
2632 req = __lookup_request_ge(osdc, next_tid);
2635 if (req->r_tid > last_tid)
2638 next_tid = req->r_tid + 1;
2639 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2642 ceph_osdc_get_request(req);
2643 mutex_unlock(&osdc->request_mutex);
2644 dout("sync waiting on tid %llu (last is %llu)\n",
2645 req->r_tid, last_tid);
2646 wait_for_completion(&req->r_safe_completion);
2647 mutex_lock(&osdc->request_mutex);
2648 ceph_osdc_put_request(req);
2650 mutex_unlock(&osdc->request_mutex);
2651 dout("sync done (thru tid %llu)\n", last_tid);
2653 EXPORT_SYMBOL(ceph_osdc_sync);
2656 * Call all pending notify callbacks - for use after a watch is
2657 * unregistered, to make sure no more callbacks for it will be invoked
2659 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2661 flush_workqueue(osdc->notify_wq);
2663 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2669 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2674 osdc->client = client;
2675 osdc->osdmap = NULL;
2676 init_rwsem(&osdc->map_sem);
2677 init_completion(&osdc->map_waiters);
2678 osdc->last_requested_map = 0;
2679 mutex_init(&osdc->request_mutex);
2681 osdc->osds = RB_ROOT;
2682 INIT_LIST_HEAD(&osdc->osd_lru);
2683 osdc->requests = RB_ROOT;
2684 INIT_LIST_HEAD(&osdc->req_lru);
2685 INIT_LIST_HEAD(&osdc->req_unsent);
2686 INIT_LIST_HEAD(&osdc->req_notarget);
2687 INIT_LIST_HEAD(&osdc->req_linger);
2688 osdc->num_requests = 0;
2689 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2690 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2691 spin_lock_init(&osdc->event_lock);
2692 osdc->event_tree = RB_ROOT;
2693 osdc->event_count = 0;
2695 schedule_delayed_work(&osdc->osds_timeout_work,
2696 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
2699 osdc->req_mempool = mempool_create_slab_pool(10,
2700 ceph_osd_request_cache);
2701 if (!osdc->req_mempool)
2704 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2705 PAGE_SIZE, 10, true, "osd_op");
2708 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2709 PAGE_SIZE, 10, true, "osd_op_reply");
2714 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2715 if (!osdc->notify_wq)
2716 goto out_msgpool_reply;
2721 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2723 ceph_msgpool_destroy(&osdc->msgpool_op);
2725 mempool_destroy(osdc->req_mempool);
2730 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2732 flush_workqueue(osdc->notify_wq);
2733 destroy_workqueue(osdc->notify_wq);
2734 cancel_delayed_work_sync(&osdc->timeout_work);
2735 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2737 ceph_osdmap_destroy(osdc->osdmap);
2738 osdc->osdmap = NULL;
2740 remove_all_osds(osdc);
2741 mempool_destroy(osdc->req_mempool);
2742 ceph_msgpool_destroy(&osdc->msgpool_op);
2743 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2747 * Read some contiguous pages. If we cross a stripe boundary, shorten
2748 * *plen. Return number of bytes read, or error.
2750 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2751 struct ceph_vino vino, struct ceph_file_layout *layout,
2753 u32 truncate_seq, u64 truncate_size,
2754 struct page **pages, int num_pages, int page_align)
2756 struct ceph_osd_request *req;
2759 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2760 vino.snap, off, *plen);
2761 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
2762 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2763 NULL, truncate_seq, truncate_size,
2766 return PTR_ERR(req);
2768 /* it may be a short read due to an object boundary */
2770 osd_req_op_extent_osd_data_pages(req, 0,
2771 pages, *plen, page_align, false, false);
2773 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2774 off, *plen, *plen, page_align);
2776 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2778 rc = ceph_osdc_start_request(osdc, req, false);
2780 rc = ceph_osdc_wait_request(osdc, req);
2782 ceph_osdc_put_request(req);
2783 dout("readpages result %d\n", rc);
2786 EXPORT_SYMBOL(ceph_osdc_readpages);
2789 * do a synchronous write on N pages
2791 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2792 struct ceph_file_layout *layout,
2793 struct ceph_snap_context *snapc,
2795 u32 truncate_seq, u64 truncate_size,
2796 struct timespec *mtime,
2797 struct page **pages, int num_pages)
2799 struct ceph_osd_request *req;
2801 int page_align = off & ~PAGE_MASK;
2803 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2804 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
2806 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2807 snapc, truncate_seq, truncate_size,
2810 return PTR_ERR(req);
2812 /* it may be a short write due to an object boundary */
2813 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2815 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2817 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2819 rc = ceph_osdc_start_request(osdc, req, true);
2821 rc = ceph_osdc_wait_request(osdc, req);
2823 ceph_osdc_put_request(req);
2826 dout("writepages result %d\n", rc);
2829 EXPORT_SYMBOL(ceph_osdc_writepages);
2831 int ceph_osdc_setup(void)
2833 size_t size = sizeof(struct ceph_osd_request) +
2834 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2836 BUG_ON(ceph_osd_request_cache);
2837 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2840 return ceph_osd_request_cache ? 0 : -ENOMEM;
2842 EXPORT_SYMBOL(ceph_osdc_setup);
2844 void ceph_osdc_cleanup(void)
2846 BUG_ON(!ceph_osd_request_cache);
2847 kmem_cache_destroy(ceph_osd_request_cache);
2848 ceph_osd_request_cache = NULL;
2850 EXPORT_SYMBOL(ceph_osdc_cleanup);
2853 * handle incoming message
2855 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2857 struct ceph_osd *osd = con->private;
2858 struct ceph_osd_client *osdc;
2859 int type = le16_to_cpu(msg->hdr.type);
2866 case CEPH_MSG_OSD_MAP:
2867 ceph_osdc_handle_map(osdc, msg);
2869 case CEPH_MSG_OSD_OPREPLY:
2870 handle_reply(osdc, msg);
2872 case CEPH_MSG_WATCH_NOTIFY:
2873 handle_watch_notify(osdc, msg);
2877 pr_err("received unknown message type %d %s\n", type,
2878 ceph_msg_type_name(type));
2885 * Lookup and return message for incoming reply. Don't try to do
2886 * anything about a larger than preallocated data portion of the
2887 * message at the moment - for now, just skip the message.
2889 static struct ceph_msg *get_reply(struct ceph_connection *con,
2890 struct ceph_msg_header *hdr,
2893 struct ceph_osd *osd = con->private;
2894 struct ceph_osd_client *osdc = osd->o_osdc;
2896 struct ceph_osd_request *req;
2897 int front_len = le32_to_cpu(hdr->front_len);
2898 int data_len = le32_to_cpu(hdr->data_len);
2901 tid = le64_to_cpu(hdr->tid);
2902 mutex_lock(&osdc->request_mutex);
2903 req = __lookup_request(osdc, tid);
2905 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2912 ceph_msg_revoke_incoming(req->r_reply);
2914 if (front_len > req->r_reply->front_alloc_len) {
2915 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2916 __func__, osd->o_osd, req->r_tid, front_len,
2917 req->r_reply->front_alloc_len);
2918 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2922 ceph_msg_put(req->r_reply);
2926 if (data_len > req->r_reply->data_length) {
2927 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2928 __func__, osd->o_osd, req->r_tid, data_len,
2929 req->r_reply->data_length);
2935 m = ceph_msg_get(req->r_reply);
2936 dout("get_reply tid %lld %p\n", tid, m);
2939 mutex_unlock(&osdc->request_mutex);
2943 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2944 struct ceph_msg_header *hdr,
2947 struct ceph_osd *osd = con->private;
2948 int type = le16_to_cpu(hdr->type);
2949 int front = le32_to_cpu(hdr->front_len);
2953 case CEPH_MSG_OSD_MAP:
2954 case CEPH_MSG_WATCH_NOTIFY:
2955 return ceph_msg_new(type, front, GFP_NOFS, false);
2956 case CEPH_MSG_OSD_OPREPLY:
2957 return get_reply(con, hdr, skip);
2959 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2967 * Wrappers to refcount containing ceph_osd struct
2969 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2971 struct ceph_osd *osd = con->private;
2977 static void put_osd_con(struct ceph_connection *con)
2979 struct ceph_osd *osd = con->private;
2987 * Note: returned pointer is the address of a structure that's
2988 * managed separately. Caller must *not* attempt to free it.
2990 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2991 int *proto, int force_new)
2993 struct ceph_osd *o = con->private;
2994 struct ceph_osd_client *osdc = o->o_osdc;
2995 struct ceph_auth_client *ac = osdc->client->monc.auth;
2996 struct ceph_auth_handshake *auth = &o->o_auth;
2998 if (force_new && auth->authorizer) {
2999 ceph_auth_destroy_authorizer(auth->authorizer);
3000 auth->authorizer = NULL;
3002 if (!auth->authorizer) {
3003 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3006 return ERR_PTR(ret);
3008 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
3011 return ERR_PTR(ret);
3013 *proto = ac->protocol;
3019 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3021 struct ceph_osd *o = con->private;
3022 struct ceph_osd_client *osdc = o->o_osdc;
3023 struct ceph_auth_client *ac = osdc->client->monc.auth;
3025 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
3028 static int invalidate_authorizer(struct ceph_connection *con)
3030 struct ceph_osd *o = con->private;
3031 struct ceph_osd_client *osdc = o->o_osdc;
3032 struct ceph_auth_client *ac = osdc->client->monc.auth;
3034 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
3035 return ceph_monc_validate_auth(&osdc->client->monc);
3038 static int osd_sign_message(struct ceph_msg *msg)
3040 struct ceph_osd *o = msg->con->private;
3041 struct ceph_auth_handshake *auth = &o->o_auth;
3043 return ceph_auth_sign_message(auth, msg);
3046 static int osd_check_message_signature(struct ceph_msg *msg)
3048 struct ceph_osd *o = msg->con->private;
3049 struct ceph_auth_handshake *auth = &o->o_auth;
3051 return ceph_auth_check_message_signature(auth, msg);
3054 static const struct ceph_connection_operations osd_con_ops = {
3057 .dispatch = dispatch,
3058 .get_authorizer = get_authorizer,
3059 .verify_authorizer_reply = verify_authorizer_reply,
3060 .invalidate_authorizer = invalidate_authorizer,
3061 .alloc_msg = alloc_msg,
3062 .sign_message = osd_sign_message,
3063 .check_message_signature = osd_check_message_signature,