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 ceph_osd_data_release(&op->extent.osd_data);
290 case CEPH_OSD_OP_CALL:
291 ceph_osd_data_release(&op->cls.request_info);
292 ceph_osd_data_release(&op->cls.request_data);
293 ceph_osd_data_release(&op->cls.response_data);
295 case CEPH_OSD_OP_SETXATTR:
296 case CEPH_OSD_OP_CMPXATTR:
297 ceph_osd_data_release(&op->xattr.osd_data);
299 case CEPH_OSD_OP_STAT:
300 ceph_osd_data_release(&op->raw_data_in);
310 static void ceph_osdc_release_request(struct kref *kref)
312 struct ceph_osd_request *req = container_of(kref,
313 struct ceph_osd_request, r_kref);
316 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
317 req->r_request, req->r_reply);
318 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
319 WARN_ON(!list_empty(&req->r_req_lru_item));
320 WARN_ON(!list_empty(&req->r_osd_item));
321 WARN_ON(!list_empty(&req->r_linger_item));
322 WARN_ON(!list_empty(&req->r_linger_osd_item));
326 ceph_msg_put(req->r_request);
328 ceph_msg_revoke_incoming(req->r_reply);
329 ceph_msg_put(req->r_reply);
332 for (which = 0; which < req->r_num_ops; which++)
333 osd_req_op_data_release(req, which);
335 ceph_put_snap_context(req->r_snapc);
337 mempool_free(req, req->r_osdc->req_mempool);
339 kmem_cache_free(ceph_osd_request_cache, req);
343 void ceph_osdc_get_request(struct ceph_osd_request *req)
345 dout("%s %p (was %d)\n", __func__, req,
346 atomic_read(&req->r_kref.refcount));
347 kref_get(&req->r_kref);
349 EXPORT_SYMBOL(ceph_osdc_get_request);
351 void ceph_osdc_put_request(struct ceph_osd_request *req)
353 dout("%s %p (was %d)\n", __func__, req,
354 atomic_read(&req->r_kref.refcount));
355 kref_put(&req->r_kref, ceph_osdc_release_request);
357 EXPORT_SYMBOL(ceph_osdc_put_request);
359 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
360 struct ceph_snap_context *snapc,
361 unsigned int num_ops,
365 struct ceph_osd_request *req;
366 struct ceph_msg *msg;
369 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
370 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
372 msg_size = 4 + 4 + 8 + 8 + 4+8;
373 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
374 msg_size += 1 + 8 + 4 + 4; /* pg_t */
375 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
376 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
377 msg_size += 8; /* snapid */
378 msg_size += 8; /* snap_seq */
379 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
383 req = mempool_alloc(osdc->req_mempool, gfp_flags);
384 memset(req, 0, sizeof(*req));
386 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
392 req->r_mempool = use_mempool;
393 req->r_num_ops = num_ops;
395 kref_init(&req->r_kref);
396 init_completion(&req->r_completion);
397 init_completion(&req->r_safe_completion);
398 RB_CLEAR_NODE(&req->r_node);
399 INIT_LIST_HEAD(&req->r_unsafe_item);
400 INIT_LIST_HEAD(&req->r_linger_item);
401 INIT_LIST_HEAD(&req->r_linger_osd_item);
402 INIT_LIST_HEAD(&req->r_req_lru_item);
403 INIT_LIST_HEAD(&req->r_osd_item);
405 req->r_base_oloc.pool = -1;
406 req->r_target_oloc.pool = -1;
408 /* create reply message */
410 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
412 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
413 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
415 ceph_osdc_put_request(req);
420 /* create request message; allow space for oid */
422 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
424 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
426 ceph_osdc_put_request(req);
430 memset(msg->front.iov_base, 0, msg->front.iov_len);
432 req->r_request = msg;
436 EXPORT_SYMBOL(ceph_osdc_alloc_request);
438 static bool osd_req_opcode_valid(u16 opcode)
441 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
442 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
450 * This is an osd op init function for opcodes that have no data or
451 * other information associated with them. It also serves as a
452 * common init routine for all the other init functions, below.
454 static struct ceph_osd_req_op *
455 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
458 struct ceph_osd_req_op *op;
460 BUG_ON(which >= osd_req->r_num_ops);
461 BUG_ON(!osd_req_opcode_valid(opcode));
463 op = &osd_req->r_ops[which];
464 memset(op, 0, sizeof (*op));
470 void osd_req_op_init(struct ceph_osd_request *osd_req,
471 unsigned int which, u16 opcode)
473 (void)_osd_req_op_init(osd_req, which, opcode);
475 EXPORT_SYMBOL(osd_req_op_init);
477 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
478 unsigned int which, u16 opcode,
479 u64 offset, u64 length,
480 u64 truncate_size, u32 truncate_seq)
482 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
483 size_t payload_len = 0;
485 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
486 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE);
488 op->extent.offset = offset;
489 op->extent.length = length;
490 op->extent.truncate_size = truncate_size;
491 op->extent.truncate_seq = truncate_seq;
492 if (opcode == CEPH_OSD_OP_WRITE)
493 payload_len += length;
495 op->payload_len = payload_len;
497 EXPORT_SYMBOL(osd_req_op_extent_init);
499 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
500 unsigned int which, u64 length)
502 struct ceph_osd_req_op *op;
505 BUG_ON(which >= osd_req->r_num_ops);
506 op = &osd_req->r_ops[which];
507 previous = op->extent.length;
509 if (length == previous)
510 return; /* Nothing to do */
511 BUG_ON(length > previous);
513 op->extent.length = length;
514 op->payload_len -= previous - length;
516 EXPORT_SYMBOL(osd_req_op_extent_update);
518 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
519 u16 opcode, const char *class, const char *method)
521 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
522 struct ceph_pagelist *pagelist;
523 size_t payload_len = 0;
526 BUG_ON(opcode != CEPH_OSD_OP_CALL);
528 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
530 ceph_pagelist_init(pagelist);
532 op->cls.class_name = class;
533 size = strlen(class);
534 BUG_ON(size > (size_t) U8_MAX);
535 op->cls.class_len = size;
536 ceph_pagelist_append(pagelist, class, size);
539 op->cls.method_name = method;
540 size = strlen(method);
541 BUG_ON(size > (size_t) U8_MAX);
542 op->cls.method_len = size;
543 ceph_pagelist_append(pagelist, method, size);
546 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
548 op->cls.argc = 0; /* currently unused */
550 op->payload_len = payload_len;
552 EXPORT_SYMBOL(osd_req_op_cls_init);
554 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
555 u16 opcode, const char *name, const void *value,
556 size_t size, u8 cmp_op, u8 cmp_mode)
558 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
559 struct ceph_pagelist *pagelist;
562 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
564 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
568 ceph_pagelist_init(pagelist);
570 payload_len = strlen(name);
571 op->xattr.name_len = payload_len;
572 ceph_pagelist_append(pagelist, name, payload_len);
574 op->xattr.value_len = size;
575 ceph_pagelist_append(pagelist, value, size);
578 op->xattr.cmp_op = cmp_op;
579 op->xattr.cmp_mode = cmp_mode;
581 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
582 op->payload_len = payload_len;
585 EXPORT_SYMBOL(osd_req_op_xattr_init);
587 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
588 unsigned int which, u16 opcode,
589 u64 cookie, u64 version, int flag)
591 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
593 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
595 op->watch.cookie = cookie;
596 op->watch.ver = version;
597 if (opcode == CEPH_OSD_OP_WATCH && flag)
598 op->watch.flag = (u8)1;
600 EXPORT_SYMBOL(osd_req_op_watch_init);
602 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
604 u64 expected_object_size,
605 u64 expected_write_size)
607 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
608 CEPH_OSD_OP_SETALLOCHINT);
610 op->alloc_hint.expected_object_size = expected_object_size;
611 op->alloc_hint.expected_write_size = expected_write_size;
614 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
615 * not worth a feature bit. Set FAILOK per-op flag to make
616 * sure older osds don't trip over an unsupported opcode.
618 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
620 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
622 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
623 struct ceph_osd_data *osd_data)
625 u64 length = ceph_osd_data_length(osd_data);
627 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
628 BUG_ON(length > (u64) SIZE_MAX);
630 ceph_msg_data_add_pages(msg, osd_data->pages,
631 length, osd_data->alignment);
632 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
634 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
636 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
637 ceph_msg_data_add_bio(msg, osd_data->bio, length);
640 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
644 static u64 osd_req_encode_op(struct ceph_osd_request *req,
645 struct ceph_osd_op *dst, unsigned int which)
647 struct ceph_osd_req_op *src;
648 struct ceph_osd_data *osd_data;
649 u64 request_data_len = 0;
652 BUG_ON(which >= req->r_num_ops);
653 src = &req->r_ops[which];
654 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
655 pr_err("unrecognized osd opcode %d\n", src->op);
661 case CEPH_OSD_OP_STAT:
662 osd_data = &src->raw_data_in;
663 ceph_osdc_msg_data_add(req->r_reply, osd_data);
665 case CEPH_OSD_OP_READ:
666 case CEPH_OSD_OP_WRITE:
667 case CEPH_OSD_OP_ZERO:
668 case CEPH_OSD_OP_TRUNCATE:
669 if (src->op == CEPH_OSD_OP_WRITE)
670 request_data_len = src->extent.length;
671 dst->extent.offset = cpu_to_le64(src->extent.offset);
672 dst->extent.length = cpu_to_le64(src->extent.length);
673 dst->extent.truncate_size =
674 cpu_to_le64(src->extent.truncate_size);
675 dst->extent.truncate_seq =
676 cpu_to_le32(src->extent.truncate_seq);
677 osd_data = &src->extent.osd_data;
678 if (src->op == CEPH_OSD_OP_WRITE)
679 ceph_osdc_msg_data_add(req->r_request, osd_data);
681 ceph_osdc_msg_data_add(req->r_reply, osd_data);
683 case CEPH_OSD_OP_CALL:
684 dst->cls.class_len = src->cls.class_len;
685 dst->cls.method_len = src->cls.method_len;
686 osd_data = &src->cls.request_info;
687 ceph_osdc_msg_data_add(req->r_request, osd_data);
688 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
689 request_data_len = osd_data->pagelist->length;
691 osd_data = &src->cls.request_data;
692 data_length = ceph_osd_data_length(osd_data);
694 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
695 dst->cls.indata_len = cpu_to_le32(data_length);
696 ceph_osdc_msg_data_add(req->r_request, osd_data);
697 src->payload_len += data_length;
698 request_data_len += data_length;
700 osd_data = &src->cls.response_data;
701 ceph_osdc_msg_data_add(req->r_reply, osd_data);
703 case CEPH_OSD_OP_STARTSYNC:
705 case CEPH_OSD_OP_NOTIFY_ACK:
706 case CEPH_OSD_OP_WATCH:
707 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
708 dst->watch.ver = cpu_to_le64(src->watch.ver);
709 dst->watch.flag = src->watch.flag;
711 case CEPH_OSD_OP_SETALLOCHINT:
712 dst->alloc_hint.expected_object_size =
713 cpu_to_le64(src->alloc_hint.expected_object_size);
714 dst->alloc_hint.expected_write_size =
715 cpu_to_le64(src->alloc_hint.expected_write_size);
717 case CEPH_OSD_OP_SETXATTR:
718 case CEPH_OSD_OP_CMPXATTR:
719 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
720 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
721 dst->xattr.cmp_op = src->xattr.cmp_op;
722 dst->xattr.cmp_mode = src->xattr.cmp_mode;
723 osd_data = &src->xattr.osd_data;
724 ceph_osdc_msg_data_add(req->r_request, osd_data);
725 request_data_len = osd_data->pagelist->length;
727 case CEPH_OSD_OP_CREATE:
728 case CEPH_OSD_OP_DELETE:
731 pr_err("unsupported osd opcode %s\n",
732 ceph_osd_op_name(src->op));
738 dst->op = cpu_to_le16(src->op);
739 dst->flags = cpu_to_le32(src->flags);
740 dst->payload_len = cpu_to_le32(src->payload_len);
742 return request_data_len;
746 * build new request AND message, calculate layout, and adjust file
749 * if the file was recently truncated, we include information about its
750 * old and new size so that the object can be updated appropriately. (we
751 * avoid synchronously deleting truncated objects because it's slow.)
753 * if @do_sync, include a 'startsync' command so that the osd will flush
756 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
757 struct ceph_file_layout *layout,
758 struct ceph_vino vino,
760 unsigned int which, int num_ops,
761 int opcode, int flags,
762 struct ceph_snap_context *snapc,
767 struct ceph_osd_request *req;
773 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
774 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
775 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
777 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
780 return ERR_PTR(-ENOMEM);
782 req->r_flags = flags;
784 /* calculate max write size */
785 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
787 ceph_osdc_put_request(req);
791 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
792 osd_req_op_init(req, which, opcode);
794 u32 object_size = le32_to_cpu(layout->fl_object_size);
795 u32 object_base = off - objoff;
796 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
797 if (truncate_size <= object_base) {
800 truncate_size -= object_base;
801 if (truncate_size > object_size)
802 truncate_size = object_size;
805 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
806 truncate_size, truncate_seq);
809 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
811 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
812 "%llx.%08llx", vino.ino, objnum);
813 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
817 EXPORT_SYMBOL(ceph_osdc_new_request);
820 * We keep osd requests in an rbtree, sorted by ->r_tid.
822 static void __insert_request(struct ceph_osd_client *osdc,
823 struct ceph_osd_request *new)
825 struct rb_node **p = &osdc->requests.rb_node;
826 struct rb_node *parent = NULL;
827 struct ceph_osd_request *req = NULL;
831 req = rb_entry(parent, struct ceph_osd_request, r_node);
832 if (new->r_tid < req->r_tid)
834 else if (new->r_tid > req->r_tid)
840 rb_link_node(&new->r_node, parent, p);
841 rb_insert_color(&new->r_node, &osdc->requests);
844 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
847 struct ceph_osd_request *req;
848 struct rb_node *n = osdc->requests.rb_node;
851 req = rb_entry(n, struct ceph_osd_request, r_node);
852 if (tid < req->r_tid)
854 else if (tid > req->r_tid)
862 static struct ceph_osd_request *
863 __lookup_request_ge(struct ceph_osd_client *osdc,
866 struct ceph_osd_request *req;
867 struct rb_node *n = osdc->requests.rb_node;
870 req = rb_entry(n, struct ceph_osd_request, r_node);
871 if (tid < req->r_tid) {
875 } else if (tid > req->r_tid) {
884 static void __kick_linger_request(struct ceph_osd_request *req)
886 struct ceph_osd_client *osdc = req->r_osdc;
887 struct ceph_osd *osd = req->r_osd;
890 * Linger requests need to be resent with a new tid to avoid
891 * the dup op detection logic on the OSDs. Achieve this with
892 * a re-register dance instead of open-coding.
894 ceph_osdc_get_request(req);
895 if (!list_empty(&req->r_linger_item))
896 __unregister_linger_request(osdc, req);
898 __unregister_request(osdc, req);
899 __register_request(osdc, req);
900 ceph_osdc_put_request(req);
903 * Unless request has been registered as both normal and
904 * lingering, __unregister{,_linger}_request clears r_osd.
905 * However, here we need to preserve r_osd to make sure we
906 * requeue on the same OSD.
908 WARN_ON(req->r_osd || !osd);
911 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
912 __enqueue_request(req);
916 * Resubmit requests pending on the given osd.
918 static void __kick_osd_requests(struct ceph_osd_client *osdc,
919 struct ceph_osd *osd)
921 struct ceph_osd_request *req, *nreq;
923 LIST_HEAD(resend_linger);
926 dout("%s osd%d\n", __func__, osd->o_osd);
927 err = __reset_osd(osdc, osd);
932 * Build up a list of requests to resend by traversing the
933 * osd's list of requests. Requests for a given object are
934 * sent in tid order, and that is also the order they're
935 * kept on this list. Therefore all requests that are in
936 * flight will be found first, followed by all requests that
937 * have not yet been sent. And to resend requests while
938 * preserving this order we will want to put any sent
939 * requests back on the front of the osd client's unsent
942 * So we build a separate ordered list of already-sent
943 * requests for the affected osd and splice it onto the
944 * front of the osd client's unsent list. Once we've seen a
945 * request that has not yet been sent we're done. Those
946 * requests are already sitting right where they belong.
948 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
952 if (!req->r_linger) {
953 dout("%s requeueing %p tid %llu\n", __func__, req,
955 list_move_tail(&req->r_req_lru_item, &resend);
956 req->r_flags |= CEPH_OSD_FLAG_RETRY;
958 list_move_tail(&req->r_req_lru_item, &resend_linger);
961 list_splice(&resend, &osdc->req_unsent);
964 * Both registered and not yet registered linger requests are
965 * enqueued with a new tid on the same OSD. We add/move them
966 * to req_unsent/o_requests at the end to keep things in tid
969 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
971 WARN_ON(!list_empty(&req->r_req_lru_item));
972 __kick_linger_request(req);
975 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
976 __kick_linger_request(req);
980 * If the osd connection drops, we need to resubmit all requests.
982 static void osd_reset(struct ceph_connection *con)
984 struct ceph_osd *osd = con->private;
985 struct ceph_osd_client *osdc;
989 dout("osd_reset osd%d\n", osd->o_osd);
991 down_read(&osdc->map_sem);
992 mutex_lock(&osdc->request_mutex);
993 __kick_osd_requests(osdc, osd);
995 mutex_unlock(&osdc->request_mutex);
996 up_read(&osdc->map_sem);
1000 * Track open sessions with osds.
1002 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1004 struct ceph_osd *osd;
1006 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1010 atomic_set(&osd->o_ref, 1);
1013 RB_CLEAR_NODE(&osd->o_node);
1014 INIT_LIST_HEAD(&osd->o_requests);
1015 INIT_LIST_HEAD(&osd->o_linger_requests);
1016 INIT_LIST_HEAD(&osd->o_osd_lru);
1017 osd->o_incarnation = 1;
1019 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1021 INIT_LIST_HEAD(&osd->o_keepalive_item);
1025 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1027 if (atomic_inc_not_zero(&osd->o_ref)) {
1028 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1029 atomic_read(&osd->o_ref));
1032 dout("get_osd %p FAIL\n", osd);
1037 static void put_osd(struct ceph_osd *osd)
1039 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1040 atomic_read(&osd->o_ref) - 1);
1041 if (atomic_dec_and_test(&osd->o_ref)) {
1042 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1044 if (osd->o_auth.authorizer)
1045 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
1051 * remove an osd from our map
1053 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1055 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1056 WARN_ON(!list_empty(&osd->o_requests));
1057 WARN_ON(!list_empty(&osd->o_linger_requests));
1059 list_del_init(&osd->o_osd_lru);
1060 rb_erase(&osd->o_node, &osdc->osds);
1061 RB_CLEAR_NODE(&osd->o_node);
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);
1068 if (!RB_EMPTY_NODE(&osd->o_node)) {
1069 ceph_con_close(&osd->o_con);
1070 __remove_osd(osdc, osd);
1075 static void remove_all_osds(struct ceph_osd_client *osdc)
1077 dout("%s %p\n", __func__, osdc);
1078 mutex_lock(&osdc->request_mutex);
1079 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1080 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1081 struct ceph_osd, o_node);
1082 remove_osd(osdc, osd);
1084 mutex_unlock(&osdc->request_mutex);
1087 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1088 struct ceph_osd *osd)
1090 dout("%s %p\n", __func__, osd);
1091 BUG_ON(!list_empty(&osd->o_osd_lru));
1093 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1094 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
1097 static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1098 struct ceph_osd *osd)
1100 dout("%s %p\n", __func__, osd);
1102 if (list_empty(&osd->o_requests) &&
1103 list_empty(&osd->o_linger_requests))
1104 __move_osd_to_lru(osdc, osd);
1107 static void __remove_osd_from_lru(struct ceph_osd *osd)
1109 dout("__remove_osd_from_lru %p\n", osd);
1110 if (!list_empty(&osd->o_osd_lru))
1111 list_del_init(&osd->o_osd_lru);
1114 static void remove_old_osds(struct ceph_osd_client *osdc)
1116 struct ceph_osd *osd, *nosd;
1118 dout("__remove_old_osds %p\n", osdc);
1119 mutex_lock(&osdc->request_mutex);
1120 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1121 if (time_before(jiffies, osd->lru_ttl))
1123 remove_osd(osdc, osd);
1125 mutex_unlock(&osdc->request_mutex);
1131 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1133 struct ceph_entity_addr *peer_addr;
1135 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1136 if (list_empty(&osd->o_requests) &&
1137 list_empty(&osd->o_linger_requests)) {
1138 remove_osd(osdc, osd);
1142 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1143 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1144 !ceph_con_opened(&osd->o_con)) {
1145 struct ceph_osd_request *req;
1147 dout("osd addr hasn't changed and connection never opened, "
1148 "letting msgr retry\n");
1149 /* touch each r_stamp for handle_timeout()'s benfit */
1150 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1151 req->r_stamp = jiffies;
1156 ceph_con_close(&osd->o_con);
1157 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1158 osd->o_incarnation++;
1163 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1165 struct rb_node **p = &osdc->osds.rb_node;
1166 struct rb_node *parent = NULL;
1167 struct ceph_osd *osd = NULL;
1169 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1172 osd = rb_entry(parent, struct ceph_osd, o_node);
1173 if (new->o_osd < osd->o_osd)
1175 else if (new->o_osd > osd->o_osd)
1176 p = &(*p)->rb_right;
1181 rb_link_node(&new->o_node, parent, p);
1182 rb_insert_color(&new->o_node, &osdc->osds);
1185 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1187 struct ceph_osd *osd;
1188 struct rb_node *n = osdc->osds.rb_node;
1191 osd = rb_entry(n, struct ceph_osd, o_node);
1194 else if (o > osd->o_osd)
1202 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1204 schedule_delayed_work(&osdc->timeout_work,
1205 osdc->client->options->osd_keepalive_timeout * HZ);
1208 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1210 cancel_delayed_work(&osdc->timeout_work);
1214 * Register request, assign tid. If this is the first request, set up
1215 * the timeout event.
1217 static void __register_request(struct ceph_osd_client *osdc,
1218 struct ceph_osd_request *req)
1220 req->r_tid = ++osdc->last_tid;
1221 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1222 dout("__register_request %p tid %lld\n", req, req->r_tid);
1223 __insert_request(osdc, req);
1224 ceph_osdc_get_request(req);
1225 osdc->num_requests++;
1226 if (osdc->num_requests == 1) {
1227 dout(" first request, scheduling timeout\n");
1228 __schedule_osd_timeout(osdc);
1233 * called under osdc->request_mutex
1235 static void __unregister_request(struct ceph_osd_client *osdc,
1236 struct ceph_osd_request *req)
1238 if (RB_EMPTY_NODE(&req->r_node)) {
1239 dout("__unregister_request %p tid %lld not registered\n",
1244 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1245 rb_erase(&req->r_node, &osdc->requests);
1246 RB_CLEAR_NODE(&req->r_node);
1247 osdc->num_requests--;
1250 /* make sure the original request isn't in flight. */
1251 ceph_msg_revoke(req->r_request);
1253 list_del_init(&req->r_osd_item);
1254 maybe_move_osd_to_lru(osdc, req->r_osd);
1255 if (list_empty(&req->r_linger_osd_item))
1259 list_del_init(&req->r_req_lru_item);
1260 ceph_osdc_put_request(req);
1262 if (osdc->num_requests == 0) {
1263 dout(" no requests, canceling timeout\n");
1264 __cancel_osd_timeout(osdc);
1269 * Cancel a previously queued request message
1271 static void __cancel_request(struct ceph_osd_request *req)
1273 if (req->r_sent && req->r_osd) {
1274 ceph_msg_revoke(req->r_request);
1279 static void __register_linger_request(struct ceph_osd_client *osdc,
1280 struct ceph_osd_request *req)
1282 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1283 WARN_ON(!req->r_linger);
1285 ceph_osdc_get_request(req);
1286 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1288 list_add_tail(&req->r_linger_osd_item,
1289 &req->r_osd->o_linger_requests);
1292 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1293 struct ceph_osd_request *req)
1295 WARN_ON(!req->r_linger);
1297 if (list_empty(&req->r_linger_item)) {
1298 dout("%s %p tid %llu not registered\n", __func__, req,
1303 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1304 list_del_init(&req->r_linger_item);
1307 list_del_init(&req->r_linger_osd_item);
1308 maybe_move_osd_to_lru(osdc, req->r_osd);
1309 if (list_empty(&req->r_osd_item))
1312 ceph_osdc_put_request(req);
1315 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1316 struct ceph_osd_request *req)
1318 if (!req->r_linger) {
1319 dout("set_request_linger %p\n", req);
1323 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1326 * Returns whether a request should be blocked from being sent
1327 * based on the current osdmap and osd_client settings.
1329 * Caller should hold map_sem for read.
1331 static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1332 struct ceph_osd_request *req)
1334 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1335 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1336 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1337 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1338 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1342 * Calculate mapping of a request to a PG. Takes tiering into account.
1344 static int __calc_request_pg(struct ceph_osdmap *osdmap,
1345 struct ceph_osd_request *req,
1346 struct ceph_pg *pg_out)
1348 bool need_check_tiering;
1350 need_check_tiering = false;
1351 if (req->r_target_oloc.pool == -1) {
1352 req->r_target_oloc = req->r_base_oloc; /* struct */
1353 need_check_tiering = true;
1355 if (req->r_target_oid.name_len == 0) {
1356 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1357 need_check_tiering = true;
1360 if (need_check_tiering &&
1361 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1362 struct ceph_pg_pool_info *pi;
1364 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
1366 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1368 req->r_target_oloc.pool = pi->read_tier;
1369 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1370 pi->write_tier >= 0)
1371 req->r_target_oloc.pool = pi->write_tier;
1373 /* !pi is caught in ceph_oloc_oid_to_pg() */
1376 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1377 &req->r_target_oid, pg_out);
1380 static void __enqueue_request(struct ceph_osd_request *req)
1382 struct ceph_osd_client *osdc = req->r_osdc;
1384 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1385 req->r_osd ? req->r_osd->o_osd : -1);
1388 __remove_osd_from_lru(req->r_osd);
1389 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1390 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1392 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1397 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1398 * (as needed), and set the request r_osd appropriately. If there is
1399 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1400 * (unsent, homeless) or leave on in-flight lru.
1402 * Return 0 if unchanged, 1 if changed, or negative on error.
1404 * Caller should hold map_sem for read and request_mutex.
1406 static int __map_request(struct ceph_osd_client *osdc,
1407 struct ceph_osd_request *req, int force_resend)
1409 struct ceph_pg pgid;
1410 int acting[CEPH_PG_MAX_SIZE];
1415 dout("map_request %p tid %lld\n", req, req->r_tid);
1417 err = __calc_request_pg(osdc->osdmap, req, &pgid);
1419 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1424 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1428 was_paused = req->r_paused;
1429 req->r_paused = __req_should_be_paused(osdc, req);
1430 if (was_paused && !req->r_paused)
1433 if ((!force_resend &&
1434 req->r_osd && req->r_osd->o_osd == o &&
1435 req->r_sent >= req->r_osd->o_incarnation &&
1436 req->r_num_pg_osds == num &&
1437 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1438 (req->r_osd == NULL && o == -1) ||
1440 return 0; /* no change */
1442 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1443 req->r_tid, pgid.pool, pgid.seed, o,
1444 req->r_osd ? req->r_osd->o_osd : -1);
1446 /* record full pg acting set */
1447 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1448 req->r_num_pg_osds = num;
1451 __cancel_request(req);
1452 list_del_init(&req->r_osd_item);
1453 list_del_init(&req->r_linger_osd_item);
1457 req->r_osd = __lookup_osd(osdc, o);
1458 if (!req->r_osd && o >= 0) {
1460 req->r_osd = create_osd(osdc, o);
1462 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1466 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1467 __insert_osd(osdc, req->r_osd);
1469 ceph_con_open(&req->r_osd->o_con,
1470 CEPH_ENTITY_TYPE_OSD, o,
1471 &osdc->osdmap->osd_addr[o]);
1474 __enqueue_request(req);
1475 err = 1; /* osd or pg changed */
1482 * caller should hold map_sem (for read) and request_mutex
1484 static void __send_request(struct ceph_osd_client *osdc,
1485 struct ceph_osd_request *req)
1489 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1490 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1491 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1493 /* fill in message content that changes each time we send it */
1494 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1495 put_unaligned_le32(req->r_flags, req->r_request_flags);
1496 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1497 p = req->r_request_pgid;
1498 ceph_encode_64(&p, req->r_pgid.pool);
1499 ceph_encode_32(&p, req->r_pgid.seed);
1500 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1501 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1502 sizeof(req->r_reassert_version));
1504 req->r_stamp = jiffies;
1505 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1507 ceph_msg_get(req->r_request); /* send consumes a ref */
1509 req->r_sent = req->r_osd->o_incarnation;
1511 ceph_con_send(&req->r_osd->o_con, req->r_request);
1515 * Send any requests in the queue (req_unsent).
1517 static void __send_queued(struct ceph_osd_client *osdc)
1519 struct ceph_osd_request *req, *tmp;
1521 dout("__send_queued\n");
1522 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1523 __send_request(osdc, req);
1527 * Caller should hold map_sem for read and request_mutex.
1529 static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1530 struct ceph_osd_request *req,
1535 __register_request(osdc, req);
1537 req->r_got_reply = 0;
1538 rc = __map_request(osdc, req, 0);
1541 dout("osdc_start_request failed map, "
1542 " will retry %lld\n", req->r_tid);
1545 __unregister_request(osdc, req);
1550 if (req->r_osd == NULL) {
1551 dout("send_request %p no up osds in pg\n", req);
1552 ceph_monc_request_next_osdmap(&osdc->client->monc);
1554 __send_queued(osdc);
1561 * Timeout callback, called every N seconds when 1 or more osd
1562 * requests has been active for more than N seconds. When this
1563 * happens, we ping all OSDs with requests who have timed out to
1564 * ensure any communications channel reset is detected. Reset the
1565 * request timeouts another N seconds in the future as we go.
1566 * Reschedule the timeout event another N seconds in future (unless
1567 * there are no open requests).
1569 static void handle_timeout(struct work_struct *work)
1571 struct ceph_osd_client *osdc =
1572 container_of(work, struct ceph_osd_client, timeout_work.work);
1573 struct ceph_osd_request *req;
1574 struct ceph_osd *osd;
1575 unsigned long keepalive =
1576 osdc->client->options->osd_keepalive_timeout * HZ;
1577 struct list_head slow_osds;
1579 down_read(&osdc->map_sem);
1581 ceph_monc_request_next_osdmap(&osdc->client->monc);
1583 mutex_lock(&osdc->request_mutex);
1586 * ping osds that are a bit slow. this ensures that if there
1587 * is a break in the TCP connection we will notice, and reopen
1588 * a connection with that osd (from the fault callback).
1590 INIT_LIST_HEAD(&slow_osds);
1591 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1592 if (time_before(jiffies, req->r_stamp + keepalive))
1597 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1598 req->r_tid, osd->o_osd);
1599 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1601 while (!list_empty(&slow_osds)) {
1602 osd = list_entry(slow_osds.next, struct ceph_osd,
1604 list_del_init(&osd->o_keepalive_item);
1605 ceph_con_keepalive(&osd->o_con);
1608 __schedule_osd_timeout(osdc);
1609 __send_queued(osdc);
1610 mutex_unlock(&osdc->request_mutex);
1611 up_read(&osdc->map_sem);
1614 static void handle_osds_timeout(struct work_struct *work)
1616 struct ceph_osd_client *osdc =
1617 container_of(work, struct ceph_osd_client,
1618 osds_timeout_work.work);
1619 unsigned long delay =
1620 osdc->client->options->osd_idle_ttl * HZ >> 2;
1622 dout("osds timeout\n");
1623 down_read(&osdc->map_sem);
1624 remove_old_osds(osdc);
1625 up_read(&osdc->map_sem);
1627 schedule_delayed_work(&osdc->osds_timeout_work,
1628 round_jiffies_relative(delay));
1631 static int ceph_oloc_decode(void **p, void *end,
1632 struct ceph_object_locator *oloc)
1634 u8 struct_v, struct_cv;
1639 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1640 struct_v = ceph_decode_8(p);
1641 struct_cv = ceph_decode_8(p);
1643 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1644 struct_v, struct_cv);
1647 if (struct_cv > 6) {
1648 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1649 struct_v, struct_cv);
1652 len = ceph_decode_32(p);
1653 ceph_decode_need(p, end, len, e_inval);
1654 struct_end = *p + len;
1656 oloc->pool = ceph_decode_64(p);
1657 *p += 4; /* skip preferred */
1659 len = ceph_decode_32(p);
1661 pr_warn("ceph_object_locator::key is set\n");
1665 if (struct_v >= 5) {
1666 len = ceph_decode_32(p);
1668 pr_warn("ceph_object_locator::nspace is set\n");
1673 if (struct_v >= 6) {
1674 s64 hash = ceph_decode_64(p);
1676 pr_warn("ceph_object_locator::hash is set\n");
1691 static int ceph_redirect_decode(void **p, void *end,
1692 struct ceph_request_redirect *redir)
1694 u8 struct_v, struct_cv;
1699 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1700 struct_v = ceph_decode_8(p);
1701 struct_cv = ceph_decode_8(p);
1702 if (struct_cv > 1) {
1703 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1704 struct_v, struct_cv);
1707 len = ceph_decode_32(p);
1708 ceph_decode_need(p, end, len, e_inval);
1709 struct_end = *p + len;
1711 ret = ceph_oloc_decode(p, end, &redir->oloc);
1715 len = ceph_decode_32(p);
1717 pr_warn("ceph_request_redirect::object_name is set\n");
1721 len = ceph_decode_32(p);
1722 *p += len; /* skip osd_instructions */
1734 static void complete_request(struct ceph_osd_request *req)
1736 complete_all(&req->r_safe_completion); /* fsync waiter */
1740 * handle osd op reply. either call the callback if it is specified,
1741 * or do the completion to wake up the waiting thread.
1743 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1744 struct ceph_connection *con)
1747 struct ceph_osd_request *req;
1748 struct ceph_request_redirect redir;
1751 unsigned int numops;
1752 int payload_len, flags;
1758 u64 reassert_version;
1760 int already_completed;
1764 tid = le64_to_cpu(msg->hdr.tid);
1765 dout("handle_reply %p tid %llu\n", msg, tid);
1767 p = msg->front.iov_base;
1768 end = p + msg->front.iov_len;
1770 ceph_decode_need(&p, end, 4, bad);
1771 object_len = ceph_decode_32(&p);
1772 ceph_decode_need(&p, end, object_len, bad);
1775 err = ceph_decode_pgid(&p, end, &pg);
1779 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1780 flags = ceph_decode_64(&p);
1781 result = ceph_decode_32(&p);
1782 reassert_epoch = ceph_decode_32(&p);
1783 reassert_version = ceph_decode_64(&p);
1784 osdmap_epoch = ceph_decode_32(&p);
1787 down_read(&osdc->map_sem);
1788 mutex_lock(&osdc->request_mutex);
1789 req = __lookup_request(osdc, tid);
1791 dout("handle_reply tid %llu dne\n", tid);
1794 ceph_osdc_get_request(req);
1796 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1799 ceph_decode_need(&p, end, 4, bad_put);
1800 numops = ceph_decode_32(&p);
1801 if (numops > CEPH_OSD_MAX_OP)
1803 if (numops != req->r_num_ops)
1806 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1807 for (i = 0; i < numops; i++) {
1808 struct ceph_osd_op *op = p;
1811 len = le32_to_cpu(op->payload_len);
1812 req->r_reply_op_len[i] = len;
1813 dout(" op %d has %d bytes\n", i, len);
1817 bytes = le32_to_cpu(msg->hdr.data_len);
1818 if (payload_len != bytes) {
1819 pr_warn("sum of op payload lens %d != data_len %d\n",
1820 payload_len, bytes);
1824 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1825 retry_attempt = ceph_decode_32(&p);
1826 for (i = 0; i < numops; i++)
1827 req->r_reply_op_result[i] = ceph_decode_32(&p);
1829 if (le16_to_cpu(msg->hdr.version) >= 6) {
1830 p += 8 + 4; /* skip replay_version */
1831 p += 8; /* skip user_version */
1833 err = ceph_redirect_decode(&p, end, &redir);
1837 redir.oloc.pool = -1;
1840 if (redir.oloc.pool != -1) {
1841 dout("redirect pool %lld\n", redir.oloc.pool);
1843 __unregister_request(osdc, req);
1845 req->r_target_oloc = redir.oloc; /* struct */
1848 * Start redirect requests with nofail=true. If
1849 * mapping fails, request will end up on the notarget
1850 * list, waiting for the new osdmap (which can take
1851 * a while), even though the original request mapped
1852 * successfully. In the future we might want to follow
1853 * original request's nofail setting here.
1855 err = __ceph_osdc_start_request(osdc, req, true);
1861 already_completed = req->r_got_reply;
1862 if (!req->r_got_reply) {
1863 req->r_result = result;
1864 dout("handle_reply result %d bytes %d\n", req->r_result,
1866 if (req->r_result == 0)
1867 req->r_result = bytes;
1869 /* in case this is a write and we need to replay, */
1870 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1871 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1873 req->r_got_reply = 1;
1874 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1875 dout("handle_reply tid %llu dup ack\n", tid);
1879 dout("handle_reply tid %llu flags %d\n", tid, flags);
1881 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1882 __register_linger_request(osdc, req);
1884 /* either this is a read, or we got the safe response */
1886 (flags & CEPH_OSD_FLAG_ONDISK) ||
1887 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1888 __unregister_request(osdc, req);
1890 mutex_unlock(&osdc->request_mutex);
1891 up_read(&osdc->map_sem);
1893 if (!already_completed) {
1894 if (req->r_unsafe_callback &&
1895 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1896 req->r_unsafe_callback(req, true);
1897 if (req->r_callback)
1898 req->r_callback(req, msg);
1900 complete_all(&req->r_completion);
1903 if (flags & CEPH_OSD_FLAG_ONDISK) {
1904 if (req->r_unsafe_callback && already_completed)
1905 req->r_unsafe_callback(req, false);
1906 complete_request(req);
1910 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1911 ceph_osdc_put_request(req);
1914 mutex_unlock(&osdc->request_mutex);
1915 up_read(&osdc->map_sem);
1919 req->r_result = -EIO;
1920 __unregister_request(osdc, req);
1921 if (req->r_callback)
1922 req->r_callback(req, msg);
1924 complete_all(&req->r_completion);
1925 complete_request(req);
1926 ceph_osdc_put_request(req);
1928 mutex_unlock(&osdc->request_mutex);
1929 up_read(&osdc->map_sem);
1931 pr_err("corrupt osd_op_reply got %d %d\n",
1932 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1936 static void reset_changed_osds(struct ceph_osd_client *osdc)
1938 struct rb_node *p, *n;
1940 dout("%s %p\n", __func__, osdc);
1941 for (p = rb_first(&osdc->osds); p; p = n) {
1942 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1945 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1946 memcmp(&osd->o_con.peer_addr,
1947 ceph_osd_addr(osdc->osdmap,
1949 sizeof(struct ceph_entity_addr)) != 0)
1950 __reset_osd(osdc, osd);
1955 * Requeue requests whose mapping to an OSD has changed. If requests map to
1956 * no osd, request a new map.
1958 * Caller should hold map_sem for read.
1960 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1961 bool force_resend_writes)
1963 struct ceph_osd_request *req, *nreq;
1967 bool force_resend_req;
1969 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1970 force_resend_writes ? " (force resend writes)" : "");
1971 mutex_lock(&osdc->request_mutex);
1972 for (p = rb_first(&osdc->requests); p; ) {
1973 req = rb_entry(p, struct ceph_osd_request, r_node);
1977 * For linger requests that have not yet been
1978 * registered, move them to the linger list; they'll
1979 * be sent to the osd in the loop below. Unregister
1980 * the request before re-registering it as a linger
1981 * request to ensure the __map_request() below
1982 * will decide it needs to be sent.
1984 if (req->r_linger && list_empty(&req->r_linger_item)) {
1985 dout("%p tid %llu restart on osd%d\n",
1987 req->r_osd ? req->r_osd->o_osd : -1);
1988 ceph_osdc_get_request(req);
1989 __unregister_request(osdc, req);
1990 __register_linger_request(osdc, req);
1991 ceph_osdc_put_request(req);
1995 force_resend_req = force_resend ||
1996 (force_resend_writes &&
1997 req->r_flags & CEPH_OSD_FLAG_WRITE);
1998 err = __map_request(osdc, req, force_resend_req);
2000 continue; /* error */
2001 if (req->r_osd == NULL) {
2002 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2003 needmap++; /* request a newer map */
2004 } else if (err > 0) {
2005 if (!req->r_linger) {
2006 dout("%p tid %llu requeued on osd%d\n", req,
2008 req->r_osd ? req->r_osd->o_osd : -1);
2009 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2014 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2016 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2018 err = __map_request(osdc, req,
2019 force_resend || force_resend_writes);
2020 dout("__map_request returned %d\n", err);
2022 continue; /* hrm! */
2023 if (req->r_osd == NULL || err > 0) {
2024 if (req->r_osd == NULL) {
2025 dout("lingering %p tid %llu maps to no osd\n",
2028 * A homeless lingering request makes
2029 * no sense, as it's job is to keep
2030 * a particular OSD connection open.
2031 * Request a newer map and kick the
2032 * request, knowing that it won't be
2033 * resent until we actually get a map
2034 * that can tell us where to send it.
2039 dout("kicking lingering %p tid %llu osd%d\n", req,
2040 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2041 __register_request(osdc, req);
2042 __unregister_linger_request(osdc, req);
2045 reset_changed_osds(osdc);
2046 mutex_unlock(&osdc->request_mutex);
2049 dout("%d requests for down osds, need new map\n", needmap);
2050 ceph_monc_request_next_osdmap(&osdc->client->monc);
2056 * Process updated osd map.
2058 * The message contains any number of incremental and full maps, normally
2059 * indicating some sort of topology change in the cluster. Kick requests
2060 * off to different OSDs as needed.
2062 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2064 void *p, *end, *next;
2065 u32 nr_maps, maplen;
2067 struct ceph_osdmap *newmap = NULL, *oldmap;
2069 struct ceph_fsid fsid;
2072 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2073 p = msg->front.iov_base;
2074 end = p + msg->front.iov_len;
2077 ceph_decode_need(&p, end, sizeof(fsid), bad);
2078 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2079 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2082 down_write(&osdc->map_sem);
2084 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2086 /* incremental maps */
2087 ceph_decode_32_safe(&p, end, nr_maps, bad);
2088 dout(" %d inc maps\n", nr_maps);
2089 while (nr_maps > 0) {
2090 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2091 epoch = ceph_decode_32(&p);
2092 maplen = ceph_decode_32(&p);
2093 ceph_decode_need(&p, end, maplen, bad);
2095 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2096 dout("applying incremental map %u len %d\n",
2098 newmap = osdmap_apply_incremental(&p, next,
2100 &osdc->client->msgr);
2101 if (IS_ERR(newmap)) {
2102 err = PTR_ERR(newmap);
2106 if (newmap != osdc->osdmap) {
2107 ceph_osdmap_destroy(osdc->osdmap);
2108 osdc->osdmap = newmap;
2110 was_full = was_full ||
2111 ceph_osdmap_flag(osdc->osdmap,
2113 kick_requests(osdc, 0, was_full);
2115 dout("ignoring incremental map %u len %d\n",
2125 ceph_decode_32_safe(&p, end, nr_maps, bad);
2126 dout(" %d full maps\n", nr_maps);
2128 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2129 epoch = ceph_decode_32(&p);
2130 maplen = ceph_decode_32(&p);
2131 ceph_decode_need(&p, end, maplen, bad);
2133 dout("skipping non-latest full map %u len %d\n",
2135 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2136 dout("skipping full map %u len %d, "
2137 "older than our %u\n", epoch, maplen,
2138 osdc->osdmap->epoch);
2140 int skipped_map = 0;
2142 dout("taking full map %u len %d\n", epoch, maplen);
2143 newmap = ceph_osdmap_decode(&p, p+maplen);
2144 if (IS_ERR(newmap)) {
2145 err = PTR_ERR(newmap);
2149 oldmap = osdc->osdmap;
2150 osdc->osdmap = newmap;
2152 if (oldmap->epoch + 1 < newmap->epoch)
2154 ceph_osdmap_destroy(oldmap);
2156 was_full = was_full ||
2157 ceph_osdmap_flag(osdc->osdmap,
2159 kick_requests(osdc, skipped_map, was_full);
2168 downgrade_write(&osdc->map_sem);
2169 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
2172 * subscribe to subsequent osdmap updates if full to ensure
2173 * we find out when we are no longer full and stop returning
2176 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2177 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2178 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2179 ceph_monc_request_next_osdmap(&osdc->client->monc);
2181 mutex_lock(&osdc->request_mutex);
2182 __send_queued(osdc);
2183 mutex_unlock(&osdc->request_mutex);
2184 up_read(&osdc->map_sem);
2185 wake_up_all(&osdc->client->auth_wq);
2189 pr_err("osdc handle_map corrupt msg\n");
2191 up_write(&osdc->map_sem);
2195 * watch/notify callback event infrastructure
2197 * These callbacks are used both for watch and notify operations.
2199 static void __release_event(struct kref *kref)
2201 struct ceph_osd_event *event =
2202 container_of(kref, struct ceph_osd_event, kref);
2204 dout("__release_event %p\n", event);
2208 static void get_event(struct ceph_osd_event *event)
2210 kref_get(&event->kref);
2213 void ceph_osdc_put_event(struct ceph_osd_event *event)
2215 kref_put(&event->kref, __release_event);
2217 EXPORT_SYMBOL(ceph_osdc_put_event);
2219 static void __insert_event(struct ceph_osd_client *osdc,
2220 struct ceph_osd_event *new)
2222 struct rb_node **p = &osdc->event_tree.rb_node;
2223 struct rb_node *parent = NULL;
2224 struct ceph_osd_event *event = NULL;
2228 event = rb_entry(parent, struct ceph_osd_event, node);
2229 if (new->cookie < event->cookie)
2231 else if (new->cookie > event->cookie)
2232 p = &(*p)->rb_right;
2237 rb_link_node(&new->node, parent, p);
2238 rb_insert_color(&new->node, &osdc->event_tree);
2241 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2244 struct rb_node **p = &osdc->event_tree.rb_node;
2245 struct rb_node *parent = NULL;
2246 struct ceph_osd_event *event = NULL;
2250 event = rb_entry(parent, struct ceph_osd_event, node);
2251 if (cookie < event->cookie)
2253 else if (cookie > event->cookie)
2254 p = &(*p)->rb_right;
2261 static void __remove_event(struct ceph_osd_event *event)
2263 struct ceph_osd_client *osdc = event->osdc;
2265 if (!RB_EMPTY_NODE(&event->node)) {
2266 dout("__remove_event removed %p\n", event);
2267 rb_erase(&event->node, &osdc->event_tree);
2268 ceph_osdc_put_event(event);
2270 dout("__remove_event didn't remove %p\n", event);
2274 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2275 void (*event_cb)(u64, u64, u8, void *),
2276 void *data, struct ceph_osd_event **pevent)
2278 struct ceph_osd_event *event;
2280 event = kmalloc(sizeof(*event), GFP_NOIO);
2284 dout("create_event %p\n", event);
2285 event->cb = event_cb;
2286 event->one_shot = 0;
2289 INIT_LIST_HEAD(&event->osd_node);
2290 RB_CLEAR_NODE(&event->node);
2291 kref_init(&event->kref); /* one ref for us */
2292 kref_get(&event->kref); /* one ref for the caller */
2294 spin_lock(&osdc->event_lock);
2295 event->cookie = ++osdc->event_count;
2296 __insert_event(osdc, event);
2297 spin_unlock(&osdc->event_lock);
2302 EXPORT_SYMBOL(ceph_osdc_create_event);
2304 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2306 struct ceph_osd_client *osdc = event->osdc;
2308 dout("cancel_event %p\n", event);
2309 spin_lock(&osdc->event_lock);
2310 __remove_event(event);
2311 spin_unlock(&osdc->event_lock);
2312 ceph_osdc_put_event(event); /* caller's */
2314 EXPORT_SYMBOL(ceph_osdc_cancel_event);
2317 static void do_event_work(struct work_struct *work)
2319 struct ceph_osd_event_work *event_work =
2320 container_of(work, struct ceph_osd_event_work, work);
2321 struct ceph_osd_event *event = event_work->event;
2322 u64 ver = event_work->ver;
2323 u64 notify_id = event_work->notify_id;
2324 u8 opcode = event_work->opcode;
2326 dout("do_event_work completing %p\n", event);
2327 event->cb(ver, notify_id, opcode, event->data);
2328 dout("do_event_work completed %p\n", event);
2329 ceph_osdc_put_event(event);
2335 * Process osd watch notifications
2337 static void handle_watch_notify(struct ceph_osd_client *osdc,
2338 struct ceph_msg *msg)
2342 u64 cookie, ver, notify_id;
2344 struct ceph_osd_event *event;
2345 struct ceph_osd_event_work *event_work;
2347 p = msg->front.iov_base;
2348 end = p + msg->front.iov_len;
2350 ceph_decode_8_safe(&p, end, proto_ver, bad);
2351 ceph_decode_8_safe(&p, end, opcode, bad);
2352 ceph_decode_64_safe(&p, end, cookie, bad);
2353 ceph_decode_64_safe(&p, end, ver, bad);
2354 ceph_decode_64_safe(&p, end, notify_id, bad);
2356 spin_lock(&osdc->event_lock);
2357 event = __find_event(osdc, cookie);
2359 BUG_ON(event->one_shot);
2362 spin_unlock(&osdc->event_lock);
2363 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2364 cookie, ver, event);
2366 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2368 pr_err("couldn't allocate event_work\n");
2369 ceph_osdc_put_event(event);
2372 INIT_WORK(&event_work->work, do_event_work);
2373 event_work->event = event;
2374 event_work->ver = ver;
2375 event_work->notify_id = notify_id;
2376 event_work->opcode = opcode;
2378 queue_work(osdc->notify_wq, &event_work->work);
2384 pr_err("osdc handle_watch_notify corrupt msg\n");
2388 * build new request AND message
2391 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2392 struct ceph_snap_context *snapc, u64 snap_id,
2393 struct timespec *mtime)
2395 struct ceph_msg *msg = req->r_request;
2398 int flags = req->r_flags;
2402 req->r_snapid = snap_id;
2403 req->r_snapc = ceph_get_snap_context(snapc);
2405 /* encode request */
2406 msg->hdr.version = cpu_to_le16(4);
2408 p = msg->front.iov_base;
2409 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2410 req->r_request_osdmap_epoch = p;
2412 req->r_request_flags = p;
2414 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2415 ceph_encode_timespec(p, mtime);
2416 p += sizeof(struct ceph_timespec);
2417 req->r_request_reassert_version = p;
2418 p += sizeof(struct ceph_eversion); /* will get filled in */
2421 ceph_encode_8(&p, 4);
2422 ceph_encode_8(&p, 4);
2423 ceph_encode_32(&p, 8 + 4 + 4);
2424 req->r_request_pool = p;
2426 ceph_encode_32(&p, -1); /* preferred */
2427 ceph_encode_32(&p, 0); /* key len */
2429 ceph_encode_8(&p, 1);
2430 req->r_request_pgid = p;
2432 ceph_encode_32(&p, -1); /* preferred */
2435 ceph_encode_32(&p, req->r_base_oid.name_len);
2436 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2437 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2438 req->r_base_oid.name, req->r_base_oid.name_len);
2439 p += req->r_base_oid.name_len;
2441 /* ops--can imply data */
2442 ceph_encode_16(&p, (u16)req->r_num_ops);
2444 for (i = 0; i < req->r_num_ops; i++) {
2445 data_len += osd_req_encode_op(req, p, i);
2446 p += sizeof(struct ceph_osd_op);
2450 ceph_encode_64(&p, req->r_snapid);
2451 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2452 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2454 for (i = 0; i < snapc->num_snaps; i++) {
2455 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2459 req->r_request_attempts = p;
2463 if (flags & CEPH_OSD_FLAG_WRITE) {
2467 * The header "data_off" is a hint to the receiver
2468 * allowing it to align received data into its
2469 * buffers such that there's no need to re-copy
2470 * it before writing it to disk (direct I/O).
2472 data_off = (u16) (off & 0xffff);
2473 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2475 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2477 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2478 msg_size = p - msg->front.iov_base;
2479 msg->front.iov_len = msg_size;
2480 msg->hdr.front_len = cpu_to_le32(msg_size);
2482 dout("build_request msg_size was %d\n", (int)msg_size);
2484 EXPORT_SYMBOL(ceph_osdc_build_request);
2487 * Register request, send initial attempt.
2489 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2490 struct ceph_osd_request *req,
2495 down_read(&osdc->map_sem);
2496 mutex_lock(&osdc->request_mutex);
2498 rc = __ceph_osdc_start_request(osdc, req, nofail);
2500 mutex_unlock(&osdc->request_mutex);
2501 up_read(&osdc->map_sem);
2505 EXPORT_SYMBOL(ceph_osdc_start_request);
2508 * Unregister a registered request. The request is not completed (i.e.
2509 * no callbacks or wakeups) - higher layers are supposed to know what
2510 * they are canceling.
2512 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2514 struct ceph_osd_client *osdc = req->r_osdc;
2516 mutex_lock(&osdc->request_mutex);
2518 __unregister_linger_request(osdc, req);
2519 __unregister_request(osdc, req);
2520 mutex_unlock(&osdc->request_mutex);
2522 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2524 EXPORT_SYMBOL(ceph_osdc_cancel_request);
2527 * wait for a request to complete
2529 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2530 struct ceph_osd_request *req)
2534 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2536 rc = wait_for_completion_interruptible(&req->r_completion);
2538 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2539 ceph_osdc_cancel_request(req);
2540 complete_request(req);
2544 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2546 return req->r_result;
2548 EXPORT_SYMBOL(ceph_osdc_wait_request);
2551 * sync - wait for all in-flight requests to flush. avoid starvation.
2553 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2555 struct ceph_osd_request *req;
2556 u64 last_tid, next_tid = 0;
2558 mutex_lock(&osdc->request_mutex);
2559 last_tid = osdc->last_tid;
2561 req = __lookup_request_ge(osdc, next_tid);
2564 if (req->r_tid > last_tid)
2567 next_tid = req->r_tid + 1;
2568 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2571 ceph_osdc_get_request(req);
2572 mutex_unlock(&osdc->request_mutex);
2573 dout("sync waiting on tid %llu (last is %llu)\n",
2574 req->r_tid, last_tid);
2575 wait_for_completion(&req->r_safe_completion);
2576 mutex_lock(&osdc->request_mutex);
2577 ceph_osdc_put_request(req);
2579 mutex_unlock(&osdc->request_mutex);
2580 dout("sync done (thru tid %llu)\n", last_tid);
2582 EXPORT_SYMBOL(ceph_osdc_sync);
2585 * Call all pending notify callbacks - for use after a watch is
2586 * unregistered, to make sure no more callbacks for it will be invoked
2588 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2590 flush_workqueue(osdc->notify_wq);
2592 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2598 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2603 osdc->client = client;
2604 osdc->osdmap = NULL;
2605 init_rwsem(&osdc->map_sem);
2606 init_completion(&osdc->map_waiters);
2607 osdc->last_requested_map = 0;
2608 mutex_init(&osdc->request_mutex);
2610 osdc->osds = RB_ROOT;
2611 INIT_LIST_HEAD(&osdc->osd_lru);
2612 osdc->requests = RB_ROOT;
2613 INIT_LIST_HEAD(&osdc->req_lru);
2614 INIT_LIST_HEAD(&osdc->req_unsent);
2615 INIT_LIST_HEAD(&osdc->req_notarget);
2616 INIT_LIST_HEAD(&osdc->req_linger);
2617 osdc->num_requests = 0;
2618 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2619 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2620 spin_lock_init(&osdc->event_lock);
2621 osdc->event_tree = RB_ROOT;
2622 osdc->event_count = 0;
2624 schedule_delayed_work(&osdc->osds_timeout_work,
2625 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2628 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2629 sizeof(struct ceph_osd_request));
2630 if (!osdc->req_mempool)
2633 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2634 OSD_OP_FRONT_LEN, 10, true,
2638 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2639 OSD_OPREPLY_FRONT_LEN, 10, true,
2645 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2646 if (!osdc->notify_wq)
2647 goto out_msgpool_reply;
2652 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2654 ceph_msgpool_destroy(&osdc->msgpool_op);
2656 mempool_destroy(osdc->req_mempool);
2661 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2663 flush_workqueue(osdc->notify_wq);
2664 destroy_workqueue(osdc->notify_wq);
2665 cancel_delayed_work_sync(&osdc->timeout_work);
2666 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2668 ceph_osdmap_destroy(osdc->osdmap);
2669 osdc->osdmap = NULL;
2671 remove_all_osds(osdc);
2672 mempool_destroy(osdc->req_mempool);
2673 ceph_msgpool_destroy(&osdc->msgpool_op);
2674 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2678 * Read some contiguous pages. If we cross a stripe boundary, shorten
2679 * *plen. Return number of bytes read, or error.
2681 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2682 struct ceph_vino vino, struct ceph_file_layout *layout,
2684 u32 truncate_seq, u64 truncate_size,
2685 struct page **pages, int num_pages, int page_align)
2687 struct ceph_osd_request *req;
2690 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2691 vino.snap, off, *plen);
2692 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
2693 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2694 NULL, truncate_seq, truncate_size,
2697 return PTR_ERR(req);
2699 /* it may be a short read due to an object boundary */
2701 osd_req_op_extent_osd_data_pages(req, 0,
2702 pages, *plen, page_align, false, false);
2704 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2705 off, *plen, *plen, page_align);
2707 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2709 rc = ceph_osdc_start_request(osdc, req, false);
2711 rc = ceph_osdc_wait_request(osdc, req);
2713 ceph_osdc_put_request(req);
2714 dout("readpages result %d\n", rc);
2717 EXPORT_SYMBOL(ceph_osdc_readpages);
2720 * do a synchronous write on N pages
2722 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2723 struct ceph_file_layout *layout,
2724 struct ceph_snap_context *snapc,
2726 u32 truncate_seq, u64 truncate_size,
2727 struct timespec *mtime,
2728 struct page **pages, int num_pages)
2730 struct ceph_osd_request *req;
2732 int page_align = off & ~PAGE_MASK;
2734 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2735 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
2737 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2738 snapc, truncate_seq, truncate_size,
2741 return PTR_ERR(req);
2743 /* it may be a short write due to an object boundary */
2744 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2746 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2748 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2750 rc = ceph_osdc_start_request(osdc, req, true);
2752 rc = ceph_osdc_wait_request(osdc, req);
2754 ceph_osdc_put_request(req);
2757 dout("writepages result %d\n", rc);
2760 EXPORT_SYMBOL(ceph_osdc_writepages);
2762 int ceph_osdc_setup(void)
2764 BUG_ON(ceph_osd_request_cache);
2765 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2766 sizeof (struct ceph_osd_request),
2767 __alignof__(struct ceph_osd_request),
2770 return ceph_osd_request_cache ? 0 : -ENOMEM;
2772 EXPORT_SYMBOL(ceph_osdc_setup);
2774 void ceph_osdc_cleanup(void)
2776 BUG_ON(!ceph_osd_request_cache);
2777 kmem_cache_destroy(ceph_osd_request_cache);
2778 ceph_osd_request_cache = NULL;
2780 EXPORT_SYMBOL(ceph_osdc_cleanup);
2783 * handle incoming message
2785 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2787 struct ceph_osd *osd = con->private;
2788 struct ceph_osd_client *osdc;
2789 int type = le16_to_cpu(msg->hdr.type);
2796 case CEPH_MSG_OSD_MAP:
2797 ceph_osdc_handle_map(osdc, msg);
2799 case CEPH_MSG_OSD_OPREPLY:
2800 handle_reply(osdc, msg, con);
2802 case CEPH_MSG_WATCH_NOTIFY:
2803 handle_watch_notify(osdc, msg);
2807 pr_err("received unknown message type %d %s\n", type,
2808 ceph_msg_type_name(type));
2815 * lookup and return message for incoming reply. set up reply message
2818 static struct ceph_msg *get_reply(struct ceph_connection *con,
2819 struct ceph_msg_header *hdr,
2822 struct ceph_osd *osd = con->private;
2823 struct ceph_osd_client *osdc = osd->o_osdc;
2825 struct ceph_osd_request *req;
2826 int front_len = le32_to_cpu(hdr->front_len);
2827 int data_len = le32_to_cpu(hdr->data_len);
2830 tid = le64_to_cpu(hdr->tid);
2831 mutex_lock(&osdc->request_mutex);
2832 req = __lookup_request(osdc, tid);
2836 dout("get_reply unknown tid %llu from osd%d\n", tid,
2841 if (req->r_reply->con)
2842 dout("%s revoking msg %p from old con %p\n", __func__,
2843 req->r_reply, req->r_reply->con);
2844 ceph_msg_revoke_incoming(req->r_reply);
2846 if (front_len > req->r_reply->front_alloc_len) {
2847 pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2848 front_len, req->r_reply->front_alloc_len,
2849 (unsigned int)con->peer_name.type,
2850 le64_to_cpu(con->peer_name.num));
2851 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2855 ceph_msg_put(req->r_reply);
2858 m = ceph_msg_get(req->r_reply);
2861 struct ceph_osd_data *osd_data;
2864 * XXX This is assuming there is only one op containing
2865 * XXX page data. Probably OK for reads, but this
2866 * XXX ought to be done more generally.
2868 osd_data = osd_req_op_extent_osd_data(req, 0);
2869 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2870 if (osd_data->pages &&
2871 unlikely(osd_data->length < data_len)) {
2873 pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
2874 tid, data_len, osd_data->length);
2883 dout("get_reply tid %lld %p\n", tid, m);
2886 mutex_unlock(&osdc->request_mutex);
2891 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2892 struct ceph_msg_header *hdr,
2895 struct ceph_osd *osd = con->private;
2896 int type = le16_to_cpu(hdr->type);
2897 int front = le32_to_cpu(hdr->front_len);
2901 case CEPH_MSG_OSD_MAP:
2902 case CEPH_MSG_WATCH_NOTIFY:
2903 return ceph_msg_new(type, front, GFP_NOFS, false);
2904 case CEPH_MSG_OSD_OPREPLY:
2905 return get_reply(con, hdr, skip);
2907 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2915 * Wrappers to refcount containing ceph_osd struct
2917 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2919 struct ceph_osd *osd = con->private;
2925 static void put_osd_con(struct ceph_connection *con)
2927 struct ceph_osd *osd = con->private;
2935 * Note: returned pointer is the address of a structure that's
2936 * managed separately. Caller must *not* attempt to free it.
2938 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2939 int *proto, int force_new)
2941 struct ceph_osd *o = con->private;
2942 struct ceph_osd_client *osdc = o->o_osdc;
2943 struct ceph_auth_client *ac = osdc->client->monc.auth;
2944 struct ceph_auth_handshake *auth = &o->o_auth;
2946 if (force_new && auth->authorizer) {
2947 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2948 auth->authorizer = NULL;
2950 if (!auth->authorizer) {
2951 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2954 return ERR_PTR(ret);
2956 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2959 return ERR_PTR(ret);
2961 *proto = ac->protocol;
2967 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2969 struct ceph_osd *o = con->private;
2970 struct ceph_osd_client *osdc = o->o_osdc;
2971 struct ceph_auth_client *ac = osdc->client->monc.auth;
2973 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2976 static int invalidate_authorizer(struct ceph_connection *con)
2978 struct ceph_osd *o = con->private;
2979 struct ceph_osd_client *osdc = o->o_osdc;
2980 struct ceph_auth_client *ac = osdc->client->monc.auth;
2982 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2983 return ceph_monc_validate_auth(&osdc->client->monc);
2986 static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
2988 struct ceph_osd *o = con->private;
2989 struct ceph_auth_handshake *auth = &o->o_auth;
2990 return ceph_auth_sign_message(auth, msg);
2993 static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
2995 struct ceph_osd *o = con->private;
2996 struct ceph_auth_handshake *auth = &o->o_auth;
2997 return ceph_auth_check_message_signature(auth, msg);
3000 static const struct ceph_connection_operations osd_con_ops = {
3003 .dispatch = dispatch,
3004 .get_authorizer = get_authorizer,
3005 .verify_authorizer_reply = verify_authorizer_reply,
3006 .invalidate_authorizer = invalidate_authorizer,
3007 .alloc_msg = alloc_msg,
3008 .sign_message = sign_message,
3009 .check_message_signature = check_message_signature,