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_OP_FRONT_LEN 4096
23 #define OSD_OPREPLY_FRONT_LEN 512
25 static struct kmem_cache *ceph_osd_request_cache;
27 static const struct ceph_connection_operations osd_con_ops;
29 static void __send_queued(struct ceph_osd_client *osdc);
30 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
31 static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
33 static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
35 static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
37 static void __enqueue_request(struct ceph_osd_request *req);
38 static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
42 * Implement client access to distributed object storage cluster.
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
64 * fill osd op in request message.
66 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
67 u64 *objnum, u64 *objoff, u64 *objlen)
73 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
77 if (*objlen < orig_len) {
79 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
83 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
88 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
94 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
95 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
106 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
107 struct ceph_pagelist *pagelist)
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
114 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
115 struct bio *bio, size_t bio_length)
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
119 osd_data->bio_length = bio_length;
121 #endif /* CONFIG_BLOCK */
123 #define osd_req_op_data(oreq, whch, typ, fld) \
125 BUG_ON(whch >= (oreq)->r_num_ops); \
126 &(oreq)->r_ops[whch].typ.fld; \
129 static struct ceph_osd_data *
130 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
132 BUG_ON(which >= osd_req->r_num_ops);
134 return &osd_req->r_ops[which].raw_data_in;
137 struct ceph_osd_data *
138 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
141 return osd_req_op_data(osd_req, which, extent, osd_data);
143 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
145 struct ceph_osd_data *
146 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
149 return osd_req_op_data(osd_req, which, cls, response_data);
151 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
153 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
154 unsigned int which, struct page **pages,
155 u64 length, u32 alignment,
156 bool pages_from_pool, bool own_pages)
158 struct ceph_osd_data *osd_data;
160 osd_data = osd_req_op_raw_data_in(osd_req, which);
161 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
162 pages_from_pool, own_pages);
164 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
166 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
167 unsigned int which, struct page **pages,
168 u64 length, u32 alignment,
169 bool pages_from_pool, bool own_pages)
171 struct ceph_osd_data *osd_data;
173 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
174 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
175 pages_from_pool, own_pages);
177 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
179 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
180 unsigned int which, struct ceph_pagelist *pagelist)
182 struct ceph_osd_data *osd_data;
184 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
185 ceph_osd_data_pagelist_init(osd_data, pagelist);
187 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
190 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
191 unsigned int which, struct bio *bio, size_t bio_length)
193 struct ceph_osd_data *osd_data;
195 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
196 ceph_osd_data_bio_init(osd_data, bio, bio_length);
198 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
199 #endif /* CONFIG_BLOCK */
201 static void osd_req_op_cls_request_info_pagelist(
202 struct ceph_osd_request *osd_req,
203 unsigned int which, struct ceph_pagelist *pagelist)
205 struct ceph_osd_data *osd_data;
207 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
208 ceph_osd_data_pagelist_init(osd_data, pagelist);
211 void osd_req_op_cls_request_data_pagelist(
212 struct ceph_osd_request *osd_req,
213 unsigned int which, struct ceph_pagelist *pagelist)
215 struct ceph_osd_data *osd_data;
217 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
218 ceph_osd_data_pagelist_init(osd_data, pagelist);
220 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
222 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
223 unsigned int which, struct page **pages, u64 length,
224 u32 alignment, bool pages_from_pool, bool own_pages)
226 struct ceph_osd_data *osd_data;
228 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
229 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
230 pages_from_pool, own_pages);
232 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
234 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
235 unsigned int which, struct page **pages, u64 length,
236 u32 alignment, bool pages_from_pool, bool own_pages)
238 struct ceph_osd_data *osd_data;
240 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
241 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
242 pages_from_pool, own_pages);
244 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
246 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
248 switch (osd_data->type) {
249 case CEPH_OSD_DATA_TYPE_NONE:
251 case CEPH_OSD_DATA_TYPE_PAGES:
252 return osd_data->length;
253 case CEPH_OSD_DATA_TYPE_PAGELIST:
254 return (u64)osd_data->pagelist->length;
256 case CEPH_OSD_DATA_TYPE_BIO:
257 return (u64)osd_data->bio_length;
258 #endif /* CONFIG_BLOCK */
260 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
265 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
267 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
270 num_pages = calc_pages_for((u64)osd_data->alignment,
271 (u64)osd_data->length);
272 ceph_release_page_vector(osd_data->pages, num_pages);
274 ceph_osd_data_init(osd_data);
277 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
280 struct ceph_osd_req_op *op;
282 BUG_ON(which >= osd_req->r_num_ops);
283 op = &osd_req->r_ops[which];
286 case CEPH_OSD_OP_READ:
287 case CEPH_OSD_OP_WRITE:
288 case CEPH_OSD_OP_WRITEFULL:
289 ceph_osd_data_release(&op->extent.osd_data);
291 case CEPH_OSD_OP_CALL:
292 ceph_osd_data_release(&op->cls.request_info);
293 ceph_osd_data_release(&op->cls.request_data);
294 ceph_osd_data_release(&op->cls.response_data);
296 case CEPH_OSD_OP_SETXATTR:
297 case CEPH_OSD_OP_CMPXATTR:
298 ceph_osd_data_release(&op->xattr.osd_data);
300 case CEPH_OSD_OP_STAT:
301 ceph_osd_data_release(&op->raw_data_in);
311 static void ceph_osdc_release_request(struct kref *kref)
313 struct ceph_osd_request *req = container_of(kref,
314 struct ceph_osd_request, r_kref);
317 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
318 req->r_request, req->r_reply);
319 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
320 WARN_ON(!list_empty(&req->r_req_lru_item));
321 WARN_ON(!list_empty(&req->r_osd_item));
322 WARN_ON(!list_empty(&req->r_linger_item));
323 WARN_ON(!list_empty(&req->r_linger_osd_item));
327 ceph_msg_put(req->r_request);
329 ceph_msg_revoke_incoming(req->r_reply);
330 ceph_msg_put(req->r_reply);
333 for (which = 0; which < req->r_num_ops; which++)
334 osd_req_op_data_release(req, which);
336 ceph_put_snap_context(req->r_snapc);
338 mempool_free(req, req->r_osdc->req_mempool);
340 kmem_cache_free(ceph_osd_request_cache, req);
344 void ceph_osdc_get_request(struct ceph_osd_request *req)
346 dout("%s %p (was %d)\n", __func__, req,
347 atomic_read(&req->r_kref.refcount));
348 kref_get(&req->r_kref);
350 EXPORT_SYMBOL(ceph_osdc_get_request);
352 void ceph_osdc_put_request(struct ceph_osd_request *req)
354 dout("%s %p (was %d)\n", __func__, req,
355 atomic_read(&req->r_kref.refcount));
356 kref_put(&req->r_kref, ceph_osdc_release_request);
358 EXPORT_SYMBOL(ceph_osdc_put_request);
360 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
361 struct ceph_snap_context *snapc,
362 unsigned int num_ops,
366 struct ceph_osd_request *req;
367 struct ceph_msg *msg;
370 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
371 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
373 msg_size = 4 + 4 + 8 + 8 + 4+8;
374 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
375 msg_size += 1 + 8 + 4 + 4; /* pg_t */
376 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
377 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
378 msg_size += 8; /* snapid */
379 msg_size += 8; /* snap_seq */
380 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
384 req = mempool_alloc(osdc->req_mempool, gfp_flags);
385 memset(req, 0, sizeof(*req));
387 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
393 req->r_mempool = use_mempool;
394 req->r_num_ops = num_ops;
396 kref_init(&req->r_kref);
397 init_completion(&req->r_completion);
398 init_completion(&req->r_safe_completion);
399 RB_CLEAR_NODE(&req->r_node);
400 INIT_LIST_HEAD(&req->r_unsafe_item);
401 INIT_LIST_HEAD(&req->r_linger_item);
402 INIT_LIST_HEAD(&req->r_linger_osd_item);
403 INIT_LIST_HEAD(&req->r_req_lru_item);
404 INIT_LIST_HEAD(&req->r_osd_item);
406 req->r_base_oloc.pool = -1;
407 req->r_target_oloc.pool = -1;
409 /* create reply message */
411 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
413 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
414 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
416 ceph_osdc_put_request(req);
421 /* create request message; allow space for oid */
423 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
425 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
427 ceph_osdc_put_request(req);
431 memset(msg->front.iov_base, 0, msg->front.iov_len);
433 req->r_request = msg;
437 EXPORT_SYMBOL(ceph_osdc_alloc_request);
439 static bool osd_req_opcode_valid(u16 opcode)
442 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
443 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
451 * This is an osd op init function for opcodes that have no data or
452 * other information associated with them. It also serves as a
453 * common init routine for all the other init functions, below.
455 static struct ceph_osd_req_op *
456 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
457 u16 opcode, u32 flags)
459 struct ceph_osd_req_op *op;
461 BUG_ON(which >= osd_req->r_num_ops);
462 BUG_ON(!osd_req_opcode_valid(opcode));
464 op = &osd_req->r_ops[which];
465 memset(op, 0, sizeof (*op));
472 void osd_req_op_init(struct ceph_osd_request *osd_req,
473 unsigned int which, u16 opcode, u32 flags)
475 (void)_osd_req_op_init(osd_req, which, opcode, flags);
477 EXPORT_SYMBOL(osd_req_op_init);
479 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
480 unsigned int which, u16 opcode,
481 u64 offset, u64 length,
482 u64 truncate_size, u32 truncate_seq)
484 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
486 size_t payload_len = 0;
488 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
489 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
490 opcode != CEPH_OSD_OP_TRUNCATE);
492 op->extent.offset = offset;
493 op->extent.length = length;
494 op->extent.truncate_size = truncate_size;
495 op->extent.truncate_seq = truncate_seq;
496 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
497 payload_len += length;
499 op->payload_len = payload_len;
501 EXPORT_SYMBOL(osd_req_op_extent_init);
503 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
504 unsigned int which, u64 length)
506 struct ceph_osd_req_op *op;
509 BUG_ON(which >= osd_req->r_num_ops);
510 op = &osd_req->r_ops[which];
511 previous = op->extent.length;
513 if (length == previous)
514 return; /* Nothing to do */
515 BUG_ON(length > previous);
517 op->extent.length = length;
518 op->payload_len -= previous - length;
520 EXPORT_SYMBOL(osd_req_op_extent_update);
522 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
523 u16 opcode, const char *class, const char *method)
525 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
527 struct ceph_pagelist *pagelist;
528 size_t payload_len = 0;
531 BUG_ON(opcode != CEPH_OSD_OP_CALL);
533 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
535 ceph_pagelist_init(pagelist);
537 op->cls.class_name = class;
538 size = strlen(class);
539 BUG_ON(size > (size_t) U8_MAX);
540 op->cls.class_len = size;
541 ceph_pagelist_append(pagelist, class, size);
544 op->cls.method_name = method;
545 size = strlen(method);
546 BUG_ON(size > (size_t) U8_MAX);
547 op->cls.method_len = size;
548 ceph_pagelist_append(pagelist, method, size);
551 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
553 op->cls.argc = 0; /* currently unused */
555 op->payload_len = payload_len;
557 EXPORT_SYMBOL(osd_req_op_cls_init);
559 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
560 u16 opcode, const char *name, const void *value,
561 size_t size, u8 cmp_op, u8 cmp_mode)
563 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
565 struct ceph_pagelist *pagelist;
568 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
570 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
574 ceph_pagelist_init(pagelist);
576 payload_len = strlen(name);
577 op->xattr.name_len = payload_len;
578 ceph_pagelist_append(pagelist, name, payload_len);
580 op->xattr.value_len = size;
581 ceph_pagelist_append(pagelist, value, size);
584 op->xattr.cmp_op = cmp_op;
585 op->xattr.cmp_mode = cmp_mode;
587 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
588 op->payload_len = payload_len;
591 EXPORT_SYMBOL(osd_req_op_xattr_init);
593 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
594 unsigned int which, u16 opcode,
595 u64 cookie, u64 version, int flag)
597 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
600 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
602 op->watch.cookie = cookie;
603 op->watch.ver = version;
604 if (opcode == CEPH_OSD_OP_WATCH && flag)
605 op->watch.flag = (u8)1;
607 EXPORT_SYMBOL(osd_req_op_watch_init);
609 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
611 u64 expected_object_size,
612 u64 expected_write_size)
614 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
615 CEPH_OSD_OP_SETALLOCHINT,
618 op->alloc_hint.expected_object_size = expected_object_size;
619 op->alloc_hint.expected_write_size = expected_write_size;
622 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
623 * not worth a feature bit. Set FAILOK per-op flag to make
624 * sure older osds don't trip over an unsupported opcode.
626 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
628 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
630 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
631 struct ceph_osd_data *osd_data)
633 u64 length = ceph_osd_data_length(osd_data);
635 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
636 BUG_ON(length > (u64) SIZE_MAX);
638 ceph_msg_data_add_pages(msg, osd_data->pages,
639 length, osd_data->alignment);
640 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
642 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
644 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
645 ceph_msg_data_add_bio(msg, osd_data->bio, length);
648 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
652 static u64 osd_req_encode_op(struct ceph_osd_request *req,
653 struct ceph_osd_op *dst, unsigned int which)
655 struct ceph_osd_req_op *src;
656 struct ceph_osd_data *osd_data;
657 u64 request_data_len = 0;
660 BUG_ON(which >= req->r_num_ops);
661 src = &req->r_ops[which];
662 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
663 pr_err("unrecognized osd opcode %d\n", src->op);
669 case CEPH_OSD_OP_STAT:
670 osd_data = &src->raw_data_in;
671 ceph_osdc_msg_data_add(req->r_reply, osd_data);
673 case CEPH_OSD_OP_READ:
674 case CEPH_OSD_OP_WRITE:
675 case CEPH_OSD_OP_WRITEFULL:
676 case CEPH_OSD_OP_ZERO:
677 case CEPH_OSD_OP_TRUNCATE:
678 if (src->op == CEPH_OSD_OP_WRITE ||
679 src->op == CEPH_OSD_OP_WRITEFULL)
680 request_data_len = src->extent.length;
681 dst->extent.offset = cpu_to_le64(src->extent.offset);
682 dst->extent.length = cpu_to_le64(src->extent.length);
683 dst->extent.truncate_size =
684 cpu_to_le64(src->extent.truncate_size);
685 dst->extent.truncate_seq =
686 cpu_to_le32(src->extent.truncate_seq);
687 osd_data = &src->extent.osd_data;
688 if (src->op == CEPH_OSD_OP_WRITE ||
689 src->op == CEPH_OSD_OP_WRITEFULL)
690 ceph_osdc_msg_data_add(req->r_request, osd_data);
692 ceph_osdc_msg_data_add(req->r_reply, osd_data);
694 case CEPH_OSD_OP_CALL:
695 dst->cls.class_len = src->cls.class_len;
696 dst->cls.method_len = src->cls.method_len;
697 osd_data = &src->cls.request_info;
698 ceph_osdc_msg_data_add(req->r_request, osd_data);
699 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
700 request_data_len = osd_data->pagelist->length;
702 osd_data = &src->cls.request_data;
703 data_length = ceph_osd_data_length(osd_data);
705 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
706 dst->cls.indata_len = cpu_to_le32(data_length);
707 ceph_osdc_msg_data_add(req->r_request, osd_data);
708 src->payload_len += data_length;
709 request_data_len += data_length;
711 osd_data = &src->cls.response_data;
712 ceph_osdc_msg_data_add(req->r_reply, osd_data);
714 case CEPH_OSD_OP_STARTSYNC:
716 case CEPH_OSD_OP_NOTIFY_ACK:
717 case CEPH_OSD_OP_WATCH:
718 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
719 dst->watch.ver = cpu_to_le64(src->watch.ver);
720 dst->watch.flag = src->watch.flag;
722 case CEPH_OSD_OP_SETALLOCHINT:
723 dst->alloc_hint.expected_object_size =
724 cpu_to_le64(src->alloc_hint.expected_object_size);
725 dst->alloc_hint.expected_write_size =
726 cpu_to_le64(src->alloc_hint.expected_write_size);
728 case CEPH_OSD_OP_SETXATTR:
729 case CEPH_OSD_OP_CMPXATTR:
730 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
731 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
732 dst->xattr.cmp_op = src->xattr.cmp_op;
733 dst->xattr.cmp_mode = src->xattr.cmp_mode;
734 osd_data = &src->xattr.osd_data;
735 ceph_osdc_msg_data_add(req->r_request, osd_data);
736 request_data_len = osd_data->pagelist->length;
738 case CEPH_OSD_OP_CREATE:
739 case CEPH_OSD_OP_DELETE:
742 pr_err("unsupported osd opcode %s\n",
743 ceph_osd_op_name(src->op));
749 dst->op = cpu_to_le16(src->op);
750 dst->flags = cpu_to_le32(src->flags);
751 dst->payload_len = cpu_to_le32(src->payload_len);
753 return request_data_len;
757 * build new request AND message, calculate layout, and adjust file
760 * if the file was recently truncated, we include information about its
761 * old and new size so that the object can be updated appropriately. (we
762 * avoid synchronously deleting truncated objects because it's slow.)
764 * if @do_sync, include a 'startsync' command so that the osd will flush
767 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
768 struct ceph_file_layout *layout,
769 struct ceph_vino vino,
771 unsigned int which, int num_ops,
772 int opcode, int flags,
773 struct ceph_snap_context *snapc,
778 struct ceph_osd_request *req;
784 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
785 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
786 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
788 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
791 return ERR_PTR(-ENOMEM);
793 req->r_flags = flags;
795 /* calculate max write size */
796 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
798 ceph_osdc_put_request(req);
802 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
803 osd_req_op_init(req, which, opcode, 0);
805 u32 object_size = le32_to_cpu(layout->fl_object_size);
806 u32 object_base = off - objoff;
807 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
808 if (truncate_size <= object_base) {
811 truncate_size -= object_base;
812 if (truncate_size > object_size)
813 truncate_size = object_size;
816 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
817 truncate_size, truncate_seq);
820 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
822 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
823 "%llx.%08llx", vino.ino, objnum);
824 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
828 EXPORT_SYMBOL(ceph_osdc_new_request);
831 * We keep osd requests in an rbtree, sorted by ->r_tid.
833 static void __insert_request(struct ceph_osd_client *osdc,
834 struct ceph_osd_request *new)
836 struct rb_node **p = &osdc->requests.rb_node;
837 struct rb_node *parent = NULL;
838 struct ceph_osd_request *req = NULL;
842 req = rb_entry(parent, struct ceph_osd_request, r_node);
843 if (new->r_tid < req->r_tid)
845 else if (new->r_tid > req->r_tid)
851 rb_link_node(&new->r_node, parent, p);
852 rb_insert_color(&new->r_node, &osdc->requests);
855 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
858 struct ceph_osd_request *req;
859 struct rb_node *n = osdc->requests.rb_node;
862 req = rb_entry(n, struct ceph_osd_request, r_node);
863 if (tid < req->r_tid)
865 else if (tid > req->r_tid)
873 static struct ceph_osd_request *
874 __lookup_request_ge(struct ceph_osd_client *osdc,
877 struct ceph_osd_request *req;
878 struct rb_node *n = osdc->requests.rb_node;
881 req = rb_entry(n, struct ceph_osd_request, r_node);
882 if (tid < req->r_tid) {
886 } else if (tid > req->r_tid) {
895 static void __kick_linger_request(struct ceph_osd_request *req)
897 struct ceph_osd_client *osdc = req->r_osdc;
898 struct ceph_osd *osd = req->r_osd;
901 * Linger requests need to be resent with a new tid to avoid
902 * the dup op detection logic on the OSDs. Achieve this with
903 * a re-register dance instead of open-coding.
905 ceph_osdc_get_request(req);
906 if (!list_empty(&req->r_linger_item))
907 __unregister_linger_request(osdc, req);
909 __unregister_request(osdc, req);
910 __register_request(osdc, req);
911 ceph_osdc_put_request(req);
914 * Unless request has been registered as both normal and
915 * lingering, __unregister{,_linger}_request clears r_osd.
916 * However, here we need to preserve r_osd to make sure we
917 * requeue on the same OSD.
919 WARN_ON(req->r_osd || !osd);
922 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
923 __enqueue_request(req);
927 * Resubmit requests pending on the given osd.
929 static void __kick_osd_requests(struct ceph_osd_client *osdc,
930 struct ceph_osd *osd)
932 struct ceph_osd_request *req, *nreq;
934 LIST_HEAD(resend_linger);
937 dout("%s osd%d\n", __func__, osd->o_osd);
938 err = __reset_osd(osdc, osd);
943 * Build up a list of requests to resend by traversing the
944 * osd's list of requests. Requests for a given object are
945 * sent in tid order, and that is also the order they're
946 * kept on this list. Therefore all requests that are in
947 * flight will be found first, followed by all requests that
948 * have not yet been sent. And to resend requests while
949 * preserving this order we will want to put any sent
950 * requests back on the front of the osd client's unsent
953 * So we build a separate ordered list of already-sent
954 * requests for the affected osd and splice it onto the
955 * front of the osd client's unsent list. Once we've seen a
956 * request that has not yet been sent we're done. Those
957 * requests are already sitting right where they belong.
959 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
963 if (!req->r_linger) {
964 dout("%s requeueing %p tid %llu\n", __func__, req,
966 list_move_tail(&req->r_req_lru_item, &resend);
967 req->r_flags |= CEPH_OSD_FLAG_RETRY;
969 list_move_tail(&req->r_req_lru_item, &resend_linger);
972 list_splice(&resend, &osdc->req_unsent);
975 * Both registered and not yet registered linger requests are
976 * enqueued with a new tid on the same OSD. We add/move them
977 * to req_unsent/o_requests at the end to keep things in tid
980 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
982 WARN_ON(!list_empty(&req->r_req_lru_item));
983 __kick_linger_request(req);
986 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
987 __kick_linger_request(req);
991 * If the osd connection drops, we need to resubmit all requests.
993 static void osd_reset(struct ceph_connection *con)
995 struct ceph_osd *osd = con->private;
996 struct ceph_osd_client *osdc;
1000 dout("osd_reset osd%d\n", osd->o_osd);
1002 down_read(&osdc->map_sem);
1003 mutex_lock(&osdc->request_mutex);
1004 __kick_osd_requests(osdc, osd);
1005 __send_queued(osdc);
1006 mutex_unlock(&osdc->request_mutex);
1007 up_read(&osdc->map_sem);
1011 * Track open sessions with osds.
1013 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1015 struct ceph_osd *osd;
1017 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1021 atomic_set(&osd->o_ref, 1);
1024 RB_CLEAR_NODE(&osd->o_node);
1025 INIT_LIST_HEAD(&osd->o_requests);
1026 INIT_LIST_HEAD(&osd->o_linger_requests);
1027 INIT_LIST_HEAD(&osd->o_osd_lru);
1028 osd->o_incarnation = 1;
1030 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1032 INIT_LIST_HEAD(&osd->o_keepalive_item);
1036 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1038 if (atomic_inc_not_zero(&osd->o_ref)) {
1039 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1040 atomic_read(&osd->o_ref));
1043 dout("get_osd %p FAIL\n", osd);
1048 static void put_osd(struct ceph_osd *osd)
1050 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1051 atomic_read(&osd->o_ref) - 1);
1052 if (atomic_dec_and_test(&osd->o_ref)) {
1053 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1055 if (osd->o_auth.authorizer)
1056 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
1062 * remove an osd from our map
1064 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1066 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1067 WARN_ON(!list_empty(&osd->o_requests));
1068 WARN_ON(!list_empty(&osd->o_linger_requests));
1070 list_del_init(&osd->o_osd_lru);
1071 rb_erase(&osd->o_node, &osdc->osds);
1072 RB_CLEAR_NODE(&osd->o_node);
1075 static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1077 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1079 if (!RB_EMPTY_NODE(&osd->o_node)) {
1080 ceph_con_close(&osd->o_con);
1081 __remove_osd(osdc, osd);
1086 static void remove_all_osds(struct ceph_osd_client *osdc)
1088 dout("%s %p\n", __func__, osdc);
1089 mutex_lock(&osdc->request_mutex);
1090 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1091 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1092 struct ceph_osd, o_node);
1093 remove_osd(osdc, osd);
1095 mutex_unlock(&osdc->request_mutex);
1098 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1099 struct ceph_osd *osd)
1101 dout("%s %p\n", __func__, osd);
1102 BUG_ON(!list_empty(&osd->o_osd_lru));
1104 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1105 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1108 static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1109 struct ceph_osd *osd)
1111 dout("%s %p\n", __func__, osd);
1113 if (list_empty(&osd->o_requests) &&
1114 list_empty(&osd->o_linger_requests))
1115 __move_osd_to_lru(osdc, osd);
1118 static void __remove_osd_from_lru(struct ceph_osd *osd)
1120 dout("__remove_osd_from_lru %p\n", osd);
1121 if (!list_empty(&osd->o_osd_lru))
1122 list_del_init(&osd->o_osd_lru);
1125 static void remove_old_osds(struct ceph_osd_client *osdc)
1127 struct ceph_osd *osd, *nosd;
1129 dout("__remove_old_osds %p\n", osdc);
1130 mutex_lock(&osdc->request_mutex);
1131 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1132 if (time_before(jiffies, osd->lru_ttl))
1134 remove_osd(osdc, osd);
1136 mutex_unlock(&osdc->request_mutex);
1142 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1144 struct ceph_entity_addr *peer_addr;
1146 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1147 if (list_empty(&osd->o_requests) &&
1148 list_empty(&osd->o_linger_requests)) {
1149 remove_osd(osdc, osd);
1153 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1154 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1155 !ceph_con_opened(&osd->o_con)) {
1156 struct ceph_osd_request *req;
1158 dout("osd addr hasn't changed and connection never opened, "
1159 "letting msgr retry\n");
1160 /* touch each r_stamp for handle_timeout()'s benfit */
1161 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1162 req->r_stamp = jiffies;
1167 ceph_con_close(&osd->o_con);
1168 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1169 osd->o_incarnation++;
1174 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1176 struct rb_node **p = &osdc->osds.rb_node;
1177 struct rb_node *parent = NULL;
1178 struct ceph_osd *osd = NULL;
1180 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1183 osd = rb_entry(parent, struct ceph_osd, o_node);
1184 if (new->o_osd < osd->o_osd)
1186 else if (new->o_osd > osd->o_osd)
1187 p = &(*p)->rb_right;
1192 rb_link_node(&new->o_node, parent, p);
1193 rb_insert_color(&new->o_node, &osdc->osds);
1196 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1198 struct ceph_osd *osd;
1199 struct rb_node *n = osdc->osds.rb_node;
1202 osd = rb_entry(n, struct ceph_osd, o_node);
1205 else if (o > osd->o_osd)
1213 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1215 schedule_delayed_work(&osdc->timeout_work,
1216 osdc->client->options->osd_keepalive_timeout);
1219 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1221 cancel_delayed_work(&osdc->timeout_work);
1225 * Register request, assign tid. If this is the first request, set up
1226 * the timeout event.
1228 static void __register_request(struct ceph_osd_client *osdc,
1229 struct ceph_osd_request *req)
1231 req->r_tid = ++osdc->last_tid;
1232 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1233 dout("__register_request %p tid %lld\n", req, req->r_tid);
1234 __insert_request(osdc, req);
1235 ceph_osdc_get_request(req);
1236 osdc->num_requests++;
1237 if (osdc->num_requests == 1) {
1238 dout(" first request, scheduling timeout\n");
1239 __schedule_osd_timeout(osdc);
1244 * called under osdc->request_mutex
1246 static void __unregister_request(struct ceph_osd_client *osdc,
1247 struct ceph_osd_request *req)
1249 if (RB_EMPTY_NODE(&req->r_node)) {
1250 dout("__unregister_request %p tid %lld not registered\n",
1255 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1256 rb_erase(&req->r_node, &osdc->requests);
1257 RB_CLEAR_NODE(&req->r_node);
1258 osdc->num_requests--;
1261 /* make sure the original request isn't in flight. */
1262 ceph_msg_revoke(req->r_request);
1264 list_del_init(&req->r_osd_item);
1265 maybe_move_osd_to_lru(osdc, req->r_osd);
1266 if (list_empty(&req->r_linger_osd_item))
1270 list_del_init(&req->r_req_lru_item);
1271 ceph_osdc_put_request(req);
1273 if (osdc->num_requests == 0) {
1274 dout(" no requests, canceling timeout\n");
1275 __cancel_osd_timeout(osdc);
1280 * Cancel a previously queued request message
1282 static void __cancel_request(struct ceph_osd_request *req)
1284 if (req->r_sent && req->r_osd) {
1285 ceph_msg_revoke(req->r_request);
1290 static void __register_linger_request(struct ceph_osd_client *osdc,
1291 struct ceph_osd_request *req)
1293 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1294 WARN_ON(!req->r_linger);
1296 ceph_osdc_get_request(req);
1297 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1299 list_add_tail(&req->r_linger_osd_item,
1300 &req->r_osd->o_linger_requests);
1303 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1304 struct ceph_osd_request *req)
1306 WARN_ON(!req->r_linger);
1308 if (list_empty(&req->r_linger_item)) {
1309 dout("%s %p tid %llu not registered\n", __func__, req,
1314 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1315 list_del_init(&req->r_linger_item);
1318 list_del_init(&req->r_linger_osd_item);
1319 maybe_move_osd_to_lru(osdc, req->r_osd);
1320 if (list_empty(&req->r_osd_item))
1323 ceph_osdc_put_request(req);
1326 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1327 struct ceph_osd_request *req)
1329 if (!req->r_linger) {
1330 dout("set_request_linger %p\n", req);
1334 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1337 * Returns whether a request should be blocked from being sent
1338 * based on the current osdmap and osd_client settings.
1340 * Caller should hold map_sem for read.
1342 static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1343 struct ceph_osd_request *req)
1345 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1346 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1347 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1348 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1349 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1353 * Calculate mapping of a request to a PG. Takes tiering into account.
1355 static int __calc_request_pg(struct ceph_osdmap *osdmap,
1356 struct ceph_osd_request *req,
1357 struct ceph_pg *pg_out)
1359 bool need_check_tiering;
1361 need_check_tiering = false;
1362 if (req->r_target_oloc.pool == -1) {
1363 req->r_target_oloc = req->r_base_oloc; /* struct */
1364 need_check_tiering = true;
1366 if (req->r_target_oid.name_len == 0) {
1367 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1368 need_check_tiering = true;
1371 if (need_check_tiering &&
1372 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1373 struct ceph_pg_pool_info *pi;
1375 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
1377 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1379 req->r_target_oloc.pool = pi->read_tier;
1380 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1381 pi->write_tier >= 0)
1382 req->r_target_oloc.pool = pi->write_tier;
1384 /* !pi is caught in ceph_oloc_oid_to_pg() */
1387 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1388 &req->r_target_oid, pg_out);
1391 static void __enqueue_request(struct ceph_osd_request *req)
1393 struct ceph_osd_client *osdc = req->r_osdc;
1395 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1396 req->r_osd ? req->r_osd->o_osd : -1);
1399 __remove_osd_from_lru(req->r_osd);
1400 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1401 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1403 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1408 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1409 * (as needed), and set the request r_osd appropriately. If there is
1410 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1411 * (unsent, homeless) or leave on in-flight lru.
1413 * Return 0 if unchanged, 1 if changed, or negative on error.
1415 * Caller should hold map_sem for read and request_mutex.
1417 static int __map_request(struct ceph_osd_client *osdc,
1418 struct ceph_osd_request *req, int force_resend)
1420 struct ceph_pg pgid;
1421 int acting[CEPH_PG_MAX_SIZE];
1426 dout("map_request %p tid %lld\n", req, req->r_tid);
1428 err = __calc_request_pg(osdc->osdmap, req, &pgid);
1430 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1435 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1439 was_paused = req->r_paused;
1440 req->r_paused = __req_should_be_paused(osdc, req);
1441 if (was_paused && !req->r_paused)
1444 if ((!force_resend &&
1445 req->r_osd && req->r_osd->o_osd == o &&
1446 req->r_sent >= req->r_osd->o_incarnation &&
1447 req->r_num_pg_osds == num &&
1448 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1449 (req->r_osd == NULL && o == -1) ||
1451 return 0; /* no change */
1453 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1454 req->r_tid, pgid.pool, pgid.seed, o,
1455 req->r_osd ? req->r_osd->o_osd : -1);
1457 /* record full pg acting set */
1458 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1459 req->r_num_pg_osds = num;
1462 __cancel_request(req);
1463 list_del_init(&req->r_osd_item);
1464 list_del_init(&req->r_linger_osd_item);
1468 req->r_osd = __lookup_osd(osdc, o);
1469 if (!req->r_osd && o >= 0) {
1471 req->r_osd = create_osd(osdc, o);
1473 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1477 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1478 __insert_osd(osdc, req->r_osd);
1480 ceph_con_open(&req->r_osd->o_con,
1481 CEPH_ENTITY_TYPE_OSD, o,
1482 &osdc->osdmap->osd_addr[o]);
1485 __enqueue_request(req);
1486 err = 1; /* osd or pg changed */
1493 * caller should hold map_sem (for read) and request_mutex
1495 static void __send_request(struct ceph_osd_client *osdc,
1496 struct ceph_osd_request *req)
1500 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1501 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1502 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1504 /* fill in message content that changes each time we send it */
1505 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1506 put_unaligned_le32(req->r_flags, req->r_request_flags);
1507 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1508 p = req->r_request_pgid;
1509 ceph_encode_64(&p, req->r_pgid.pool);
1510 ceph_encode_32(&p, req->r_pgid.seed);
1511 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1512 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1513 sizeof(req->r_reassert_version));
1515 req->r_stamp = jiffies;
1516 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1518 ceph_msg_get(req->r_request); /* send consumes a ref */
1520 req->r_sent = req->r_osd->o_incarnation;
1522 ceph_con_send(&req->r_osd->o_con, req->r_request);
1526 * Send any requests in the queue (req_unsent).
1528 static void __send_queued(struct ceph_osd_client *osdc)
1530 struct ceph_osd_request *req, *tmp;
1532 dout("__send_queued\n");
1533 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1534 __send_request(osdc, req);
1538 * Caller should hold map_sem for read and request_mutex.
1540 static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1541 struct ceph_osd_request *req,
1546 __register_request(osdc, req);
1548 req->r_got_reply = 0;
1549 rc = __map_request(osdc, req, 0);
1552 dout("osdc_start_request failed map, "
1553 " will retry %lld\n", req->r_tid);
1556 __unregister_request(osdc, req);
1561 if (req->r_osd == NULL) {
1562 dout("send_request %p no up osds in pg\n", req);
1563 ceph_monc_request_next_osdmap(&osdc->client->monc);
1565 __send_queued(osdc);
1572 * Timeout callback, called every N seconds when 1 or more osd
1573 * requests has been active for more than N seconds. When this
1574 * happens, we ping all OSDs with requests who have timed out to
1575 * ensure any communications channel reset is detected. Reset the
1576 * request timeouts another N seconds in the future as we go.
1577 * Reschedule the timeout event another N seconds in future (unless
1578 * there are no open requests).
1580 static void handle_timeout(struct work_struct *work)
1582 struct ceph_osd_client *osdc =
1583 container_of(work, struct ceph_osd_client, timeout_work.work);
1584 struct ceph_options *opts = osdc->client->options;
1585 struct ceph_osd_request *req;
1586 struct ceph_osd *osd;
1587 struct list_head slow_osds;
1589 down_read(&osdc->map_sem);
1591 ceph_monc_request_next_osdmap(&osdc->client->monc);
1593 mutex_lock(&osdc->request_mutex);
1596 * ping osds that are a bit slow. this ensures that if there
1597 * is a break in the TCP connection we will notice, and reopen
1598 * a connection with that osd (from the fault callback).
1600 INIT_LIST_HEAD(&slow_osds);
1601 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1602 if (time_before(jiffies,
1603 req->r_stamp + opts->osd_keepalive_timeout))
1608 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1609 req->r_tid, osd->o_osd);
1610 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1612 while (!list_empty(&slow_osds)) {
1613 osd = list_entry(slow_osds.next, struct ceph_osd,
1615 list_del_init(&osd->o_keepalive_item);
1616 ceph_con_keepalive(&osd->o_con);
1619 __schedule_osd_timeout(osdc);
1620 __send_queued(osdc);
1621 mutex_unlock(&osdc->request_mutex);
1622 up_read(&osdc->map_sem);
1625 static void handle_osds_timeout(struct work_struct *work)
1627 struct ceph_osd_client *osdc =
1628 container_of(work, struct ceph_osd_client,
1629 osds_timeout_work.work);
1630 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
1632 dout("osds timeout\n");
1633 down_read(&osdc->map_sem);
1634 remove_old_osds(osdc);
1635 up_read(&osdc->map_sem);
1637 schedule_delayed_work(&osdc->osds_timeout_work,
1638 round_jiffies_relative(delay));
1641 static int ceph_oloc_decode(void **p, void *end,
1642 struct ceph_object_locator *oloc)
1644 u8 struct_v, struct_cv;
1649 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1650 struct_v = ceph_decode_8(p);
1651 struct_cv = ceph_decode_8(p);
1653 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1654 struct_v, struct_cv);
1657 if (struct_cv > 6) {
1658 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1659 struct_v, struct_cv);
1662 len = ceph_decode_32(p);
1663 ceph_decode_need(p, end, len, e_inval);
1664 struct_end = *p + len;
1666 oloc->pool = ceph_decode_64(p);
1667 *p += 4; /* skip preferred */
1669 len = ceph_decode_32(p);
1671 pr_warn("ceph_object_locator::key is set\n");
1675 if (struct_v >= 5) {
1676 len = ceph_decode_32(p);
1678 pr_warn("ceph_object_locator::nspace is set\n");
1683 if (struct_v >= 6) {
1684 s64 hash = ceph_decode_64(p);
1686 pr_warn("ceph_object_locator::hash is set\n");
1701 static int ceph_redirect_decode(void **p, void *end,
1702 struct ceph_request_redirect *redir)
1704 u8 struct_v, struct_cv;
1709 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1710 struct_v = ceph_decode_8(p);
1711 struct_cv = ceph_decode_8(p);
1712 if (struct_cv > 1) {
1713 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1714 struct_v, struct_cv);
1717 len = ceph_decode_32(p);
1718 ceph_decode_need(p, end, len, e_inval);
1719 struct_end = *p + len;
1721 ret = ceph_oloc_decode(p, end, &redir->oloc);
1725 len = ceph_decode_32(p);
1727 pr_warn("ceph_request_redirect::object_name is set\n");
1731 len = ceph_decode_32(p);
1732 *p += len; /* skip osd_instructions */
1744 static void complete_request(struct ceph_osd_request *req)
1746 complete_all(&req->r_safe_completion); /* fsync waiter */
1750 * handle osd op reply. either call the callback if it is specified,
1751 * or do the completion to wake up the waiting thread.
1753 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1754 struct ceph_connection *con)
1757 struct ceph_osd_request *req;
1758 struct ceph_request_redirect redir;
1761 unsigned int numops;
1762 int payload_len, flags;
1768 u64 reassert_version;
1770 int already_completed;
1774 tid = le64_to_cpu(msg->hdr.tid);
1775 dout("handle_reply %p tid %llu\n", msg, tid);
1777 p = msg->front.iov_base;
1778 end = p + msg->front.iov_len;
1780 ceph_decode_need(&p, end, 4, bad);
1781 object_len = ceph_decode_32(&p);
1782 ceph_decode_need(&p, end, object_len, bad);
1785 err = ceph_decode_pgid(&p, end, &pg);
1789 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1790 flags = ceph_decode_64(&p);
1791 result = ceph_decode_32(&p);
1792 reassert_epoch = ceph_decode_32(&p);
1793 reassert_version = ceph_decode_64(&p);
1794 osdmap_epoch = ceph_decode_32(&p);
1797 down_read(&osdc->map_sem);
1798 mutex_lock(&osdc->request_mutex);
1799 req = __lookup_request(osdc, tid);
1801 dout("handle_reply tid %llu dne\n", tid);
1804 ceph_osdc_get_request(req);
1806 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1809 ceph_decode_need(&p, end, 4, bad_put);
1810 numops = ceph_decode_32(&p);
1811 if (numops > CEPH_OSD_MAX_OP)
1813 if (numops != req->r_num_ops)
1816 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1817 for (i = 0; i < numops; i++) {
1818 struct ceph_osd_op *op = p;
1821 len = le32_to_cpu(op->payload_len);
1822 req->r_reply_op_len[i] = len;
1823 dout(" op %d has %d bytes\n", i, len);
1827 bytes = le32_to_cpu(msg->hdr.data_len);
1828 if (payload_len != bytes) {
1829 pr_warn("sum of op payload lens %d != data_len %d\n",
1830 payload_len, bytes);
1834 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1835 retry_attempt = ceph_decode_32(&p);
1836 for (i = 0; i < numops; i++)
1837 req->r_reply_op_result[i] = ceph_decode_32(&p);
1839 if (le16_to_cpu(msg->hdr.version) >= 6) {
1840 p += 8 + 4; /* skip replay_version */
1841 p += 8; /* skip user_version */
1843 err = ceph_redirect_decode(&p, end, &redir);
1847 redir.oloc.pool = -1;
1850 if (redir.oloc.pool != -1) {
1851 dout("redirect pool %lld\n", redir.oloc.pool);
1853 __unregister_request(osdc, req);
1855 req->r_target_oloc = redir.oloc; /* struct */
1858 * Start redirect requests with nofail=true. If
1859 * mapping fails, request will end up on the notarget
1860 * list, waiting for the new osdmap (which can take
1861 * a while), even though the original request mapped
1862 * successfully. In the future we might want to follow
1863 * original request's nofail setting here.
1865 err = __ceph_osdc_start_request(osdc, req, true);
1871 already_completed = req->r_got_reply;
1872 if (!req->r_got_reply) {
1873 req->r_result = result;
1874 dout("handle_reply result %d bytes %d\n", req->r_result,
1876 if (req->r_result == 0)
1877 req->r_result = bytes;
1879 /* in case this is a write and we need to replay, */
1880 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1881 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1883 req->r_got_reply = 1;
1884 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1885 dout("handle_reply tid %llu dup ack\n", tid);
1889 dout("handle_reply tid %llu flags %d\n", tid, flags);
1891 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1892 __register_linger_request(osdc, req);
1894 /* either this is a read, or we got the safe response */
1896 (flags & CEPH_OSD_FLAG_ONDISK) ||
1897 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1898 __unregister_request(osdc, req);
1900 mutex_unlock(&osdc->request_mutex);
1901 up_read(&osdc->map_sem);
1903 if (!already_completed) {
1904 if (req->r_unsafe_callback &&
1905 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1906 req->r_unsafe_callback(req, true);
1907 if (req->r_callback)
1908 req->r_callback(req, msg);
1910 complete_all(&req->r_completion);
1913 if (flags & CEPH_OSD_FLAG_ONDISK) {
1914 if (req->r_unsafe_callback && already_completed)
1915 req->r_unsafe_callback(req, false);
1916 complete_request(req);
1920 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1921 ceph_osdc_put_request(req);
1924 mutex_unlock(&osdc->request_mutex);
1925 up_read(&osdc->map_sem);
1929 req->r_result = -EIO;
1930 __unregister_request(osdc, req);
1931 if (req->r_callback)
1932 req->r_callback(req, msg);
1934 complete_all(&req->r_completion);
1935 complete_request(req);
1936 ceph_osdc_put_request(req);
1938 mutex_unlock(&osdc->request_mutex);
1939 up_read(&osdc->map_sem);
1941 pr_err("corrupt osd_op_reply got %d %d\n",
1942 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1946 static void reset_changed_osds(struct ceph_osd_client *osdc)
1948 struct rb_node *p, *n;
1950 dout("%s %p\n", __func__, osdc);
1951 for (p = rb_first(&osdc->osds); p; p = n) {
1952 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1955 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1956 memcmp(&osd->o_con.peer_addr,
1957 ceph_osd_addr(osdc->osdmap,
1959 sizeof(struct ceph_entity_addr)) != 0)
1960 __reset_osd(osdc, osd);
1965 * Requeue requests whose mapping to an OSD has changed. If requests map to
1966 * no osd, request a new map.
1968 * Caller should hold map_sem for read.
1970 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1971 bool force_resend_writes)
1973 struct ceph_osd_request *req, *nreq;
1977 bool force_resend_req;
1979 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1980 force_resend_writes ? " (force resend writes)" : "");
1981 mutex_lock(&osdc->request_mutex);
1982 for (p = rb_first(&osdc->requests); p; ) {
1983 req = rb_entry(p, struct ceph_osd_request, r_node);
1987 * For linger requests that have not yet been
1988 * registered, move them to the linger list; they'll
1989 * be sent to the osd in the loop below. Unregister
1990 * the request before re-registering it as a linger
1991 * request to ensure the __map_request() below
1992 * will decide it needs to be sent.
1994 if (req->r_linger && list_empty(&req->r_linger_item)) {
1995 dout("%p tid %llu restart on osd%d\n",
1997 req->r_osd ? req->r_osd->o_osd : -1);
1998 ceph_osdc_get_request(req);
1999 __unregister_request(osdc, req);
2000 __register_linger_request(osdc, req);
2001 ceph_osdc_put_request(req);
2005 force_resend_req = force_resend ||
2006 (force_resend_writes &&
2007 req->r_flags & CEPH_OSD_FLAG_WRITE);
2008 err = __map_request(osdc, req, force_resend_req);
2010 continue; /* error */
2011 if (req->r_osd == NULL) {
2012 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2013 needmap++; /* request a newer map */
2014 } else if (err > 0) {
2015 if (!req->r_linger) {
2016 dout("%p tid %llu requeued on osd%d\n", req,
2018 req->r_osd ? req->r_osd->o_osd : -1);
2019 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2024 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2026 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2028 err = __map_request(osdc, req,
2029 force_resend || force_resend_writes);
2030 dout("__map_request returned %d\n", err);
2032 continue; /* hrm! */
2033 if (req->r_osd == NULL || err > 0) {
2034 if (req->r_osd == NULL) {
2035 dout("lingering %p tid %llu maps to no osd\n",
2038 * A homeless lingering request makes
2039 * no sense, as it's job is to keep
2040 * a particular OSD connection open.
2041 * Request a newer map and kick the
2042 * request, knowing that it won't be
2043 * resent until we actually get a map
2044 * that can tell us where to send it.
2049 dout("kicking lingering %p tid %llu osd%d\n", req,
2050 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2051 __register_request(osdc, req);
2052 __unregister_linger_request(osdc, req);
2055 reset_changed_osds(osdc);
2056 mutex_unlock(&osdc->request_mutex);
2059 dout("%d requests for down osds, need new map\n", needmap);
2060 ceph_monc_request_next_osdmap(&osdc->client->monc);
2066 * Process updated osd map.
2068 * The message contains any number of incremental and full maps, normally
2069 * indicating some sort of topology change in the cluster. Kick requests
2070 * off to different OSDs as needed.
2072 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2074 void *p, *end, *next;
2075 u32 nr_maps, maplen;
2077 struct ceph_osdmap *newmap = NULL, *oldmap;
2079 struct ceph_fsid fsid;
2082 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2083 p = msg->front.iov_base;
2084 end = p + msg->front.iov_len;
2087 ceph_decode_need(&p, end, sizeof(fsid), bad);
2088 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2089 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2092 down_write(&osdc->map_sem);
2094 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2096 /* incremental maps */
2097 ceph_decode_32_safe(&p, end, nr_maps, bad);
2098 dout(" %d inc maps\n", nr_maps);
2099 while (nr_maps > 0) {
2100 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2101 epoch = ceph_decode_32(&p);
2102 maplen = ceph_decode_32(&p);
2103 ceph_decode_need(&p, end, maplen, bad);
2105 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2106 dout("applying incremental map %u len %d\n",
2108 newmap = osdmap_apply_incremental(&p, next,
2110 &osdc->client->msgr);
2111 if (IS_ERR(newmap)) {
2112 err = PTR_ERR(newmap);
2116 if (newmap != osdc->osdmap) {
2117 ceph_osdmap_destroy(osdc->osdmap);
2118 osdc->osdmap = newmap;
2120 was_full = was_full ||
2121 ceph_osdmap_flag(osdc->osdmap,
2123 kick_requests(osdc, 0, was_full);
2125 dout("ignoring incremental map %u len %d\n",
2135 ceph_decode_32_safe(&p, end, nr_maps, bad);
2136 dout(" %d full maps\n", nr_maps);
2138 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2139 epoch = ceph_decode_32(&p);
2140 maplen = ceph_decode_32(&p);
2141 ceph_decode_need(&p, end, maplen, bad);
2143 dout("skipping non-latest full map %u len %d\n",
2145 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2146 dout("skipping full map %u len %d, "
2147 "older than our %u\n", epoch, maplen,
2148 osdc->osdmap->epoch);
2150 int skipped_map = 0;
2152 dout("taking full map %u len %d\n", epoch, maplen);
2153 newmap = ceph_osdmap_decode(&p, p+maplen);
2154 if (IS_ERR(newmap)) {
2155 err = PTR_ERR(newmap);
2159 oldmap = osdc->osdmap;
2160 osdc->osdmap = newmap;
2162 if (oldmap->epoch + 1 < newmap->epoch)
2164 ceph_osdmap_destroy(oldmap);
2166 was_full = was_full ||
2167 ceph_osdmap_flag(osdc->osdmap,
2169 kick_requests(osdc, skipped_map, was_full);
2178 downgrade_write(&osdc->map_sem);
2179 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
2182 * subscribe to subsequent osdmap updates if full to ensure
2183 * we find out when we are no longer full and stop returning
2186 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2187 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2188 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2189 ceph_monc_request_next_osdmap(&osdc->client->monc);
2191 mutex_lock(&osdc->request_mutex);
2192 __send_queued(osdc);
2193 mutex_unlock(&osdc->request_mutex);
2194 up_read(&osdc->map_sem);
2195 wake_up_all(&osdc->client->auth_wq);
2199 pr_err("osdc handle_map corrupt msg\n");
2201 up_write(&osdc->map_sem);
2205 * watch/notify callback event infrastructure
2207 * These callbacks are used both for watch and notify operations.
2209 static void __release_event(struct kref *kref)
2211 struct ceph_osd_event *event =
2212 container_of(kref, struct ceph_osd_event, kref);
2214 dout("__release_event %p\n", event);
2218 static void get_event(struct ceph_osd_event *event)
2220 kref_get(&event->kref);
2223 void ceph_osdc_put_event(struct ceph_osd_event *event)
2225 kref_put(&event->kref, __release_event);
2227 EXPORT_SYMBOL(ceph_osdc_put_event);
2229 static void __insert_event(struct ceph_osd_client *osdc,
2230 struct ceph_osd_event *new)
2232 struct rb_node **p = &osdc->event_tree.rb_node;
2233 struct rb_node *parent = NULL;
2234 struct ceph_osd_event *event = NULL;
2238 event = rb_entry(parent, struct ceph_osd_event, node);
2239 if (new->cookie < event->cookie)
2241 else if (new->cookie > event->cookie)
2242 p = &(*p)->rb_right;
2247 rb_link_node(&new->node, parent, p);
2248 rb_insert_color(&new->node, &osdc->event_tree);
2251 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2254 struct rb_node **p = &osdc->event_tree.rb_node;
2255 struct rb_node *parent = NULL;
2256 struct ceph_osd_event *event = NULL;
2260 event = rb_entry(parent, struct ceph_osd_event, node);
2261 if (cookie < event->cookie)
2263 else if (cookie > event->cookie)
2264 p = &(*p)->rb_right;
2271 static void __remove_event(struct ceph_osd_event *event)
2273 struct ceph_osd_client *osdc = event->osdc;
2275 if (!RB_EMPTY_NODE(&event->node)) {
2276 dout("__remove_event removed %p\n", event);
2277 rb_erase(&event->node, &osdc->event_tree);
2278 ceph_osdc_put_event(event);
2280 dout("__remove_event didn't remove %p\n", event);
2284 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2285 void (*event_cb)(u64, u64, u8, void *),
2286 void *data, struct ceph_osd_event **pevent)
2288 struct ceph_osd_event *event;
2290 event = kmalloc(sizeof(*event), GFP_NOIO);
2294 dout("create_event %p\n", event);
2295 event->cb = event_cb;
2296 event->one_shot = 0;
2299 INIT_LIST_HEAD(&event->osd_node);
2300 RB_CLEAR_NODE(&event->node);
2301 kref_init(&event->kref); /* one ref for us */
2302 kref_get(&event->kref); /* one ref for the caller */
2304 spin_lock(&osdc->event_lock);
2305 event->cookie = ++osdc->event_count;
2306 __insert_event(osdc, event);
2307 spin_unlock(&osdc->event_lock);
2312 EXPORT_SYMBOL(ceph_osdc_create_event);
2314 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2316 struct ceph_osd_client *osdc = event->osdc;
2318 dout("cancel_event %p\n", event);
2319 spin_lock(&osdc->event_lock);
2320 __remove_event(event);
2321 spin_unlock(&osdc->event_lock);
2322 ceph_osdc_put_event(event); /* caller's */
2324 EXPORT_SYMBOL(ceph_osdc_cancel_event);
2327 static void do_event_work(struct work_struct *work)
2329 struct ceph_osd_event_work *event_work =
2330 container_of(work, struct ceph_osd_event_work, work);
2331 struct ceph_osd_event *event = event_work->event;
2332 u64 ver = event_work->ver;
2333 u64 notify_id = event_work->notify_id;
2334 u8 opcode = event_work->opcode;
2336 dout("do_event_work completing %p\n", event);
2337 event->cb(ver, notify_id, opcode, event->data);
2338 dout("do_event_work completed %p\n", event);
2339 ceph_osdc_put_event(event);
2345 * Process osd watch notifications
2347 static void handle_watch_notify(struct ceph_osd_client *osdc,
2348 struct ceph_msg *msg)
2352 u64 cookie, ver, notify_id;
2354 struct ceph_osd_event *event;
2355 struct ceph_osd_event_work *event_work;
2357 p = msg->front.iov_base;
2358 end = p + msg->front.iov_len;
2360 ceph_decode_8_safe(&p, end, proto_ver, bad);
2361 ceph_decode_8_safe(&p, end, opcode, bad);
2362 ceph_decode_64_safe(&p, end, cookie, bad);
2363 ceph_decode_64_safe(&p, end, ver, bad);
2364 ceph_decode_64_safe(&p, end, notify_id, bad);
2366 spin_lock(&osdc->event_lock);
2367 event = __find_event(osdc, cookie);
2369 BUG_ON(event->one_shot);
2372 spin_unlock(&osdc->event_lock);
2373 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2374 cookie, ver, event);
2376 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2378 pr_err("couldn't allocate event_work\n");
2379 ceph_osdc_put_event(event);
2382 INIT_WORK(&event_work->work, do_event_work);
2383 event_work->event = event;
2384 event_work->ver = ver;
2385 event_work->notify_id = notify_id;
2386 event_work->opcode = opcode;
2388 queue_work(osdc->notify_wq, &event_work->work);
2394 pr_err("osdc handle_watch_notify corrupt msg\n");
2398 * build new request AND message
2401 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2402 struct ceph_snap_context *snapc, u64 snap_id,
2403 struct timespec *mtime)
2405 struct ceph_msg *msg = req->r_request;
2408 int flags = req->r_flags;
2412 req->r_snapid = snap_id;
2413 req->r_snapc = ceph_get_snap_context(snapc);
2415 /* encode request */
2416 msg->hdr.version = cpu_to_le16(4);
2418 p = msg->front.iov_base;
2419 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2420 req->r_request_osdmap_epoch = p;
2422 req->r_request_flags = p;
2424 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2425 ceph_encode_timespec(p, mtime);
2426 p += sizeof(struct ceph_timespec);
2427 req->r_request_reassert_version = p;
2428 p += sizeof(struct ceph_eversion); /* will get filled in */
2431 ceph_encode_8(&p, 4);
2432 ceph_encode_8(&p, 4);
2433 ceph_encode_32(&p, 8 + 4 + 4);
2434 req->r_request_pool = p;
2436 ceph_encode_32(&p, -1); /* preferred */
2437 ceph_encode_32(&p, 0); /* key len */
2439 ceph_encode_8(&p, 1);
2440 req->r_request_pgid = p;
2442 ceph_encode_32(&p, -1); /* preferred */
2445 ceph_encode_32(&p, req->r_base_oid.name_len);
2446 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2447 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2448 req->r_base_oid.name, req->r_base_oid.name_len);
2449 p += req->r_base_oid.name_len;
2451 /* ops--can imply data */
2452 ceph_encode_16(&p, (u16)req->r_num_ops);
2454 for (i = 0; i < req->r_num_ops; i++) {
2455 data_len += osd_req_encode_op(req, p, i);
2456 p += sizeof(struct ceph_osd_op);
2460 ceph_encode_64(&p, req->r_snapid);
2461 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2462 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2464 for (i = 0; i < snapc->num_snaps; i++) {
2465 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2469 req->r_request_attempts = p;
2473 if (flags & CEPH_OSD_FLAG_WRITE) {
2477 * The header "data_off" is a hint to the receiver
2478 * allowing it to align received data into its
2479 * buffers such that there's no need to re-copy
2480 * it before writing it to disk (direct I/O).
2482 data_off = (u16) (off & 0xffff);
2483 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2485 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2487 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2488 msg_size = p - msg->front.iov_base;
2489 msg->front.iov_len = msg_size;
2490 msg->hdr.front_len = cpu_to_le32(msg_size);
2492 dout("build_request msg_size was %d\n", (int)msg_size);
2494 EXPORT_SYMBOL(ceph_osdc_build_request);
2497 * Register request, send initial attempt.
2499 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2500 struct ceph_osd_request *req,
2505 down_read(&osdc->map_sem);
2506 mutex_lock(&osdc->request_mutex);
2508 rc = __ceph_osdc_start_request(osdc, req, nofail);
2510 mutex_unlock(&osdc->request_mutex);
2511 up_read(&osdc->map_sem);
2515 EXPORT_SYMBOL(ceph_osdc_start_request);
2518 * Unregister a registered request. The request is not completed (i.e.
2519 * no callbacks or wakeups) - higher layers are supposed to know what
2520 * they are canceling.
2522 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2524 struct ceph_osd_client *osdc = req->r_osdc;
2526 mutex_lock(&osdc->request_mutex);
2528 __unregister_linger_request(osdc, req);
2529 __unregister_request(osdc, req);
2530 mutex_unlock(&osdc->request_mutex);
2532 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2534 EXPORT_SYMBOL(ceph_osdc_cancel_request);
2537 * wait for a request to complete
2539 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2540 struct ceph_osd_request *req)
2544 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2546 rc = wait_for_completion_interruptible(&req->r_completion);
2548 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2549 ceph_osdc_cancel_request(req);
2550 complete_request(req);
2554 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2556 return req->r_result;
2558 EXPORT_SYMBOL(ceph_osdc_wait_request);
2561 * sync - wait for all in-flight requests to flush. avoid starvation.
2563 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2565 struct ceph_osd_request *req;
2566 u64 last_tid, next_tid = 0;
2568 mutex_lock(&osdc->request_mutex);
2569 last_tid = osdc->last_tid;
2571 req = __lookup_request_ge(osdc, next_tid);
2574 if (req->r_tid > last_tid)
2577 next_tid = req->r_tid + 1;
2578 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2581 ceph_osdc_get_request(req);
2582 mutex_unlock(&osdc->request_mutex);
2583 dout("sync waiting on tid %llu (last is %llu)\n",
2584 req->r_tid, last_tid);
2585 wait_for_completion(&req->r_safe_completion);
2586 mutex_lock(&osdc->request_mutex);
2587 ceph_osdc_put_request(req);
2589 mutex_unlock(&osdc->request_mutex);
2590 dout("sync done (thru tid %llu)\n", last_tid);
2592 EXPORT_SYMBOL(ceph_osdc_sync);
2595 * Call all pending notify callbacks - for use after a watch is
2596 * unregistered, to make sure no more callbacks for it will be invoked
2598 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2600 flush_workqueue(osdc->notify_wq);
2602 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2608 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2613 osdc->client = client;
2614 osdc->osdmap = NULL;
2615 init_rwsem(&osdc->map_sem);
2616 init_completion(&osdc->map_waiters);
2617 osdc->last_requested_map = 0;
2618 mutex_init(&osdc->request_mutex);
2620 osdc->osds = RB_ROOT;
2621 INIT_LIST_HEAD(&osdc->osd_lru);
2622 osdc->requests = RB_ROOT;
2623 INIT_LIST_HEAD(&osdc->req_lru);
2624 INIT_LIST_HEAD(&osdc->req_unsent);
2625 INIT_LIST_HEAD(&osdc->req_notarget);
2626 INIT_LIST_HEAD(&osdc->req_linger);
2627 osdc->num_requests = 0;
2628 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2629 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2630 spin_lock_init(&osdc->event_lock);
2631 osdc->event_tree = RB_ROOT;
2632 osdc->event_count = 0;
2634 schedule_delayed_work(&osdc->osds_timeout_work,
2635 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
2638 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2639 sizeof(struct ceph_osd_request));
2640 if (!osdc->req_mempool)
2643 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2644 OSD_OP_FRONT_LEN, 10, true,
2648 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2649 OSD_OPREPLY_FRONT_LEN, 10, true,
2655 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2656 if (!osdc->notify_wq)
2657 goto out_msgpool_reply;
2662 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2664 ceph_msgpool_destroy(&osdc->msgpool_op);
2666 mempool_destroy(osdc->req_mempool);
2671 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2673 flush_workqueue(osdc->notify_wq);
2674 destroy_workqueue(osdc->notify_wq);
2675 cancel_delayed_work_sync(&osdc->timeout_work);
2676 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2678 ceph_osdmap_destroy(osdc->osdmap);
2679 osdc->osdmap = NULL;
2681 remove_all_osds(osdc);
2682 mempool_destroy(osdc->req_mempool);
2683 ceph_msgpool_destroy(&osdc->msgpool_op);
2684 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2688 * Read some contiguous pages. If we cross a stripe boundary, shorten
2689 * *plen. Return number of bytes read, or error.
2691 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2692 struct ceph_vino vino, struct ceph_file_layout *layout,
2694 u32 truncate_seq, u64 truncate_size,
2695 struct page **pages, int num_pages, int page_align)
2697 struct ceph_osd_request *req;
2700 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2701 vino.snap, off, *plen);
2702 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
2703 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2704 NULL, truncate_seq, truncate_size,
2707 return PTR_ERR(req);
2709 /* it may be a short read due to an object boundary */
2711 osd_req_op_extent_osd_data_pages(req, 0,
2712 pages, *plen, page_align, false, false);
2714 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2715 off, *plen, *plen, page_align);
2717 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2719 rc = ceph_osdc_start_request(osdc, req, false);
2721 rc = ceph_osdc_wait_request(osdc, req);
2723 ceph_osdc_put_request(req);
2724 dout("readpages result %d\n", rc);
2727 EXPORT_SYMBOL(ceph_osdc_readpages);
2730 * do a synchronous write on N pages
2732 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2733 struct ceph_file_layout *layout,
2734 struct ceph_snap_context *snapc,
2736 u32 truncate_seq, u64 truncate_size,
2737 struct timespec *mtime,
2738 struct page **pages, int num_pages)
2740 struct ceph_osd_request *req;
2742 int page_align = off & ~PAGE_MASK;
2744 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2745 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
2747 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2748 snapc, truncate_seq, truncate_size,
2751 return PTR_ERR(req);
2753 /* it may be a short write due to an object boundary */
2754 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2756 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2758 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2760 rc = ceph_osdc_start_request(osdc, req, true);
2762 rc = ceph_osdc_wait_request(osdc, req);
2764 ceph_osdc_put_request(req);
2767 dout("writepages result %d\n", rc);
2770 EXPORT_SYMBOL(ceph_osdc_writepages);
2772 int ceph_osdc_setup(void)
2774 BUG_ON(ceph_osd_request_cache);
2775 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2776 sizeof (struct ceph_osd_request),
2777 __alignof__(struct ceph_osd_request),
2780 return ceph_osd_request_cache ? 0 : -ENOMEM;
2782 EXPORT_SYMBOL(ceph_osdc_setup);
2784 void ceph_osdc_cleanup(void)
2786 BUG_ON(!ceph_osd_request_cache);
2787 kmem_cache_destroy(ceph_osd_request_cache);
2788 ceph_osd_request_cache = NULL;
2790 EXPORT_SYMBOL(ceph_osdc_cleanup);
2793 * handle incoming message
2795 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2797 struct ceph_osd *osd = con->private;
2798 struct ceph_osd_client *osdc;
2799 int type = le16_to_cpu(msg->hdr.type);
2806 case CEPH_MSG_OSD_MAP:
2807 ceph_osdc_handle_map(osdc, msg);
2809 case CEPH_MSG_OSD_OPREPLY:
2810 handle_reply(osdc, msg, con);
2812 case CEPH_MSG_WATCH_NOTIFY:
2813 handle_watch_notify(osdc, msg);
2817 pr_err("received unknown message type %d %s\n", type,
2818 ceph_msg_type_name(type));
2825 * Lookup and return message for incoming reply. Don't try to do
2826 * anything about a larger than preallocated data portion of the
2827 * message at the moment - for now, just skip the message.
2829 static struct ceph_msg *get_reply(struct ceph_connection *con,
2830 struct ceph_msg_header *hdr,
2833 struct ceph_osd *osd = con->private;
2834 struct ceph_osd_client *osdc = osd->o_osdc;
2836 struct ceph_osd_request *req;
2837 int front_len = le32_to_cpu(hdr->front_len);
2838 int data_len = le32_to_cpu(hdr->data_len);
2841 tid = le64_to_cpu(hdr->tid);
2842 mutex_lock(&osdc->request_mutex);
2843 req = __lookup_request(osdc, tid);
2845 pr_warn("%s osd%d tid %llu unknown, skipping\n",
2846 __func__, osd->o_osd, tid);
2852 if (req->r_reply->con)
2853 dout("%s revoking msg %p from old con %p\n", __func__,
2854 req->r_reply, req->r_reply->con);
2855 ceph_msg_revoke_incoming(req->r_reply);
2857 if (front_len > req->r_reply->front_alloc_len) {
2858 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2859 __func__, osd->o_osd, req->r_tid, front_len,
2860 req->r_reply->front_alloc_len);
2861 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2865 ceph_msg_put(req->r_reply);
2869 if (data_len > req->r_reply->data_length) {
2870 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2871 __func__, osd->o_osd, req->r_tid, data_len,
2872 req->r_reply->data_length);
2878 m = ceph_msg_get(req->r_reply);
2879 dout("get_reply tid %lld %p\n", tid, m);
2882 mutex_unlock(&osdc->request_mutex);
2886 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2887 struct ceph_msg_header *hdr,
2890 struct ceph_osd *osd = con->private;
2891 int type = le16_to_cpu(hdr->type);
2892 int front = le32_to_cpu(hdr->front_len);
2896 case CEPH_MSG_OSD_MAP:
2897 case CEPH_MSG_WATCH_NOTIFY:
2898 return ceph_msg_new(type, front, GFP_NOFS, false);
2899 case CEPH_MSG_OSD_OPREPLY:
2900 return get_reply(con, hdr, skip);
2902 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2910 * Wrappers to refcount containing ceph_osd struct
2912 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2914 struct ceph_osd *osd = con->private;
2920 static void put_osd_con(struct ceph_connection *con)
2922 struct ceph_osd *osd = con->private;
2930 * Note: returned pointer is the address of a structure that's
2931 * managed separately. Caller must *not* attempt to free it.
2933 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2934 int *proto, int force_new)
2936 struct ceph_osd *o = con->private;
2937 struct ceph_osd_client *osdc = o->o_osdc;
2938 struct ceph_auth_client *ac = osdc->client->monc.auth;
2939 struct ceph_auth_handshake *auth = &o->o_auth;
2941 if (force_new && auth->authorizer) {
2942 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2943 auth->authorizer = NULL;
2945 if (!auth->authorizer) {
2946 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2949 return ERR_PTR(ret);
2951 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2954 return ERR_PTR(ret);
2956 *proto = ac->protocol;
2962 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2964 struct ceph_osd *o = con->private;
2965 struct ceph_osd_client *osdc = o->o_osdc;
2966 struct ceph_auth_client *ac = osdc->client->monc.auth;
2968 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2971 static int invalidate_authorizer(struct ceph_connection *con)
2973 struct ceph_osd *o = con->private;
2974 struct ceph_osd_client *osdc = o->o_osdc;
2975 struct ceph_auth_client *ac = osdc->client->monc.auth;
2977 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2978 return ceph_monc_validate_auth(&osdc->client->monc);
2981 static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
2983 struct ceph_osd *o = con->private;
2984 struct ceph_auth_handshake *auth = &o->o_auth;
2985 return ceph_auth_sign_message(auth, msg);
2988 static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
2990 struct ceph_osd *o = con->private;
2991 struct ceph_auth_handshake *auth = &o->o_auth;
2992 return ceph_auth_check_message_signature(auth, msg);
2995 static const struct ceph_connection_operations osd_con_ops = {
2998 .dispatch = dispatch,
2999 .get_authorizer = get_authorizer,
3000 .verify_authorizer_reply = verify_authorizer_reply,
3001 .invalidate_authorizer = invalidate_authorizer,
3002 .alloc_msg = alloc_msg,
3003 .sign_message = sign_message,
3004 .check_message_signature = check_message_signature,