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);
307 static void ceph_osdc_release_request(struct kref *kref)
309 struct ceph_osd_request *req = container_of(kref,
310 struct ceph_osd_request, r_kref);
313 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
314 req->r_request, req->r_reply);
315 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
316 WARN_ON(!list_empty(&req->r_req_lru_item));
317 WARN_ON(!list_empty(&req->r_osd_item));
318 WARN_ON(!list_empty(&req->r_linger_item));
319 WARN_ON(!list_empty(&req->r_linger_osd_item));
323 ceph_msg_put(req->r_request);
325 ceph_msg_revoke_incoming(req->r_reply);
326 ceph_msg_put(req->r_reply);
329 for (which = 0; which < req->r_num_ops; which++)
330 osd_req_op_data_release(req, which);
332 ceph_put_snap_context(req->r_snapc);
334 mempool_free(req, req->r_osdc->req_mempool);
336 kmem_cache_free(ceph_osd_request_cache, req);
340 void ceph_osdc_get_request(struct ceph_osd_request *req)
342 dout("%s %p (was %d)\n", __func__, req,
343 atomic_read(&req->r_kref.refcount));
344 kref_get(&req->r_kref);
346 EXPORT_SYMBOL(ceph_osdc_get_request);
348 void ceph_osdc_put_request(struct ceph_osd_request *req)
350 dout("%s %p (was %d)\n", __func__, req,
351 atomic_read(&req->r_kref.refcount));
352 kref_put(&req->r_kref, ceph_osdc_release_request);
354 EXPORT_SYMBOL(ceph_osdc_put_request);
356 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
357 struct ceph_snap_context *snapc,
358 unsigned int num_ops,
362 struct ceph_osd_request *req;
363 struct ceph_msg *msg;
366 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
367 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
369 msg_size = 4 + 4 + 8 + 8 + 4+8;
370 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
371 msg_size += 1 + 8 + 4 + 4; /* pg_t */
372 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
373 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
374 msg_size += 8; /* snapid */
375 msg_size += 8; /* snap_seq */
376 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
380 req = mempool_alloc(osdc->req_mempool, gfp_flags);
381 memset(req, 0, sizeof(*req));
383 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
389 req->r_mempool = use_mempool;
390 req->r_num_ops = num_ops;
392 kref_init(&req->r_kref);
393 init_completion(&req->r_completion);
394 init_completion(&req->r_safe_completion);
395 RB_CLEAR_NODE(&req->r_node);
396 INIT_LIST_HEAD(&req->r_unsafe_item);
397 INIT_LIST_HEAD(&req->r_linger_item);
398 INIT_LIST_HEAD(&req->r_linger_osd_item);
399 INIT_LIST_HEAD(&req->r_req_lru_item);
400 INIT_LIST_HEAD(&req->r_osd_item);
402 req->r_base_oloc.pool = -1;
403 req->r_target_oloc.pool = -1;
405 /* create reply message */
407 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
409 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
410 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
412 ceph_osdc_put_request(req);
417 /* create request message; allow space for oid */
419 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
421 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
423 ceph_osdc_put_request(req);
427 memset(msg->front.iov_base, 0, msg->front.iov_len);
429 req->r_request = msg;
433 EXPORT_SYMBOL(ceph_osdc_alloc_request);
435 static bool osd_req_opcode_valid(u16 opcode)
438 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
439 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
447 * This is an osd op init function for opcodes that have no data or
448 * other information associated with them. It also serves as a
449 * common init routine for all the other init functions, below.
451 static struct ceph_osd_req_op *
452 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
455 struct ceph_osd_req_op *op;
457 BUG_ON(which >= osd_req->r_num_ops);
458 BUG_ON(!osd_req_opcode_valid(opcode));
460 op = &osd_req->r_ops[which];
461 memset(op, 0, sizeof (*op));
467 void osd_req_op_init(struct ceph_osd_request *osd_req,
468 unsigned int which, u16 opcode)
470 (void)_osd_req_op_init(osd_req, which, opcode);
472 EXPORT_SYMBOL(osd_req_op_init);
474 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
475 unsigned int which, u16 opcode,
476 u64 offset, u64 length,
477 u64 truncate_size, u32 truncate_seq)
479 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
480 size_t payload_len = 0;
482 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
483 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE);
485 op->extent.offset = offset;
486 op->extent.length = length;
487 op->extent.truncate_size = truncate_size;
488 op->extent.truncate_seq = truncate_seq;
489 if (opcode == CEPH_OSD_OP_WRITE)
490 payload_len += length;
492 op->payload_len = payload_len;
494 EXPORT_SYMBOL(osd_req_op_extent_init);
496 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
497 unsigned int which, u64 length)
499 struct ceph_osd_req_op *op;
502 BUG_ON(which >= osd_req->r_num_ops);
503 op = &osd_req->r_ops[which];
504 previous = op->extent.length;
506 if (length == previous)
507 return; /* Nothing to do */
508 BUG_ON(length > previous);
510 op->extent.length = length;
511 op->payload_len -= previous - length;
513 EXPORT_SYMBOL(osd_req_op_extent_update);
515 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
516 u16 opcode, const char *class, const char *method)
518 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
519 struct ceph_pagelist *pagelist;
520 size_t payload_len = 0;
523 BUG_ON(opcode != CEPH_OSD_OP_CALL);
525 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
527 ceph_pagelist_init(pagelist);
529 op->cls.class_name = class;
530 size = strlen(class);
531 BUG_ON(size > (size_t) U8_MAX);
532 op->cls.class_len = size;
533 ceph_pagelist_append(pagelist, class, size);
536 op->cls.method_name = method;
537 size = strlen(method);
538 BUG_ON(size > (size_t) U8_MAX);
539 op->cls.method_len = size;
540 ceph_pagelist_append(pagelist, method, size);
543 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
545 op->cls.argc = 0; /* currently unused */
547 op->payload_len = payload_len;
549 EXPORT_SYMBOL(osd_req_op_cls_init);
551 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
552 u16 opcode, const char *name, const void *value,
553 size_t size, u8 cmp_op, u8 cmp_mode)
555 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
556 struct ceph_pagelist *pagelist;
559 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
561 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
565 ceph_pagelist_init(pagelist);
567 payload_len = strlen(name);
568 op->xattr.name_len = payload_len;
569 ceph_pagelist_append(pagelist, name, payload_len);
571 op->xattr.value_len = size;
572 ceph_pagelist_append(pagelist, value, size);
575 op->xattr.cmp_op = cmp_op;
576 op->xattr.cmp_mode = cmp_mode;
578 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
579 op->payload_len = payload_len;
582 EXPORT_SYMBOL(osd_req_op_xattr_init);
584 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
585 unsigned int which, u16 opcode,
586 u64 cookie, u64 version, int flag)
588 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
590 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
592 op->watch.cookie = cookie;
593 op->watch.ver = version;
594 if (opcode == CEPH_OSD_OP_WATCH && flag)
595 op->watch.flag = (u8)1;
597 EXPORT_SYMBOL(osd_req_op_watch_init);
599 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
601 u64 expected_object_size,
602 u64 expected_write_size)
604 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
605 CEPH_OSD_OP_SETALLOCHINT);
607 op->alloc_hint.expected_object_size = expected_object_size;
608 op->alloc_hint.expected_write_size = expected_write_size;
611 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
612 * not worth a feature bit. Set FAILOK per-op flag to make
613 * sure older osds don't trip over an unsupported opcode.
615 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
617 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
619 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
620 struct ceph_osd_data *osd_data)
622 u64 length = ceph_osd_data_length(osd_data);
624 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
625 BUG_ON(length > (u64) SIZE_MAX);
627 ceph_msg_data_add_pages(msg, osd_data->pages,
628 length, osd_data->alignment);
629 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
631 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
633 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
634 ceph_msg_data_add_bio(msg, osd_data->bio, length);
637 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
641 static u64 osd_req_encode_op(struct ceph_osd_request *req,
642 struct ceph_osd_op *dst, unsigned int which)
644 struct ceph_osd_req_op *src;
645 struct ceph_osd_data *osd_data;
646 u64 request_data_len = 0;
649 BUG_ON(which >= req->r_num_ops);
650 src = &req->r_ops[which];
651 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
652 pr_err("unrecognized osd opcode %d\n", src->op);
658 case CEPH_OSD_OP_STAT:
659 osd_data = &src->raw_data_in;
660 ceph_osdc_msg_data_add(req->r_reply, osd_data);
662 case CEPH_OSD_OP_READ:
663 case CEPH_OSD_OP_WRITE:
664 case CEPH_OSD_OP_ZERO:
665 case CEPH_OSD_OP_TRUNCATE:
666 if (src->op == CEPH_OSD_OP_WRITE)
667 request_data_len = src->extent.length;
668 dst->extent.offset = cpu_to_le64(src->extent.offset);
669 dst->extent.length = cpu_to_le64(src->extent.length);
670 dst->extent.truncate_size =
671 cpu_to_le64(src->extent.truncate_size);
672 dst->extent.truncate_seq =
673 cpu_to_le32(src->extent.truncate_seq);
674 osd_data = &src->extent.osd_data;
675 if (src->op == CEPH_OSD_OP_WRITE)
676 ceph_osdc_msg_data_add(req->r_request, osd_data);
678 ceph_osdc_msg_data_add(req->r_reply, osd_data);
680 case CEPH_OSD_OP_CALL:
681 dst->cls.class_len = src->cls.class_len;
682 dst->cls.method_len = src->cls.method_len;
683 osd_data = &src->cls.request_info;
684 ceph_osdc_msg_data_add(req->r_request, osd_data);
685 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
686 request_data_len = osd_data->pagelist->length;
688 osd_data = &src->cls.request_data;
689 data_length = ceph_osd_data_length(osd_data);
691 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
692 dst->cls.indata_len = cpu_to_le32(data_length);
693 ceph_osdc_msg_data_add(req->r_request, osd_data);
694 src->payload_len += data_length;
695 request_data_len += data_length;
697 osd_data = &src->cls.response_data;
698 ceph_osdc_msg_data_add(req->r_reply, osd_data);
700 case CEPH_OSD_OP_STARTSYNC:
702 case CEPH_OSD_OP_NOTIFY_ACK:
703 case CEPH_OSD_OP_WATCH:
704 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
705 dst->watch.ver = cpu_to_le64(src->watch.ver);
706 dst->watch.flag = src->watch.flag;
708 case CEPH_OSD_OP_SETALLOCHINT:
709 dst->alloc_hint.expected_object_size =
710 cpu_to_le64(src->alloc_hint.expected_object_size);
711 dst->alloc_hint.expected_write_size =
712 cpu_to_le64(src->alloc_hint.expected_write_size);
714 case CEPH_OSD_OP_SETXATTR:
715 case CEPH_OSD_OP_CMPXATTR:
716 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
717 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
718 dst->xattr.cmp_op = src->xattr.cmp_op;
719 dst->xattr.cmp_mode = src->xattr.cmp_mode;
720 osd_data = &src->xattr.osd_data;
721 ceph_osdc_msg_data_add(req->r_request, osd_data);
722 request_data_len = osd_data->pagelist->length;
724 case CEPH_OSD_OP_CREATE:
725 case CEPH_OSD_OP_DELETE:
728 pr_err("unsupported osd opcode %s\n",
729 ceph_osd_op_name(src->op));
735 dst->op = cpu_to_le16(src->op);
736 dst->flags = cpu_to_le32(src->flags);
737 dst->payload_len = cpu_to_le32(src->payload_len);
739 return request_data_len;
743 * build new request AND message, calculate layout, and adjust file
746 * if the file was recently truncated, we include information about its
747 * old and new size so that the object can be updated appropriately. (we
748 * avoid synchronously deleting truncated objects because it's slow.)
750 * if @do_sync, include a 'startsync' command so that the osd will flush
753 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
754 struct ceph_file_layout *layout,
755 struct ceph_vino vino,
757 unsigned int which, int num_ops,
758 int opcode, int flags,
759 struct ceph_snap_context *snapc,
764 struct ceph_osd_request *req;
770 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
771 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
772 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
774 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
777 return ERR_PTR(-ENOMEM);
779 req->r_flags = flags;
781 /* calculate max write size */
782 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
784 ceph_osdc_put_request(req);
788 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
789 osd_req_op_init(req, which, opcode);
791 u32 object_size = le32_to_cpu(layout->fl_object_size);
792 u32 object_base = off - objoff;
793 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
794 if (truncate_size <= object_base) {
797 truncate_size -= object_base;
798 if (truncate_size > object_size)
799 truncate_size = object_size;
802 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
803 truncate_size, truncate_seq);
806 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
808 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
809 "%llx.%08llx", vino.ino, objnum);
810 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
814 EXPORT_SYMBOL(ceph_osdc_new_request);
817 * We keep osd requests in an rbtree, sorted by ->r_tid.
819 static void __insert_request(struct ceph_osd_client *osdc,
820 struct ceph_osd_request *new)
822 struct rb_node **p = &osdc->requests.rb_node;
823 struct rb_node *parent = NULL;
824 struct ceph_osd_request *req = NULL;
828 req = rb_entry(parent, struct ceph_osd_request, r_node);
829 if (new->r_tid < req->r_tid)
831 else if (new->r_tid > req->r_tid)
837 rb_link_node(&new->r_node, parent, p);
838 rb_insert_color(&new->r_node, &osdc->requests);
841 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
844 struct ceph_osd_request *req;
845 struct rb_node *n = osdc->requests.rb_node;
848 req = rb_entry(n, struct ceph_osd_request, r_node);
849 if (tid < req->r_tid)
851 else if (tid > req->r_tid)
859 static struct ceph_osd_request *
860 __lookup_request_ge(struct ceph_osd_client *osdc,
863 struct ceph_osd_request *req;
864 struct rb_node *n = osdc->requests.rb_node;
867 req = rb_entry(n, struct ceph_osd_request, r_node);
868 if (tid < req->r_tid) {
872 } else if (tid > req->r_tid) {
881 static void __kick_linger_request(struct ceph_osd_request *req)
883 struct ceph_osd_client *osdc = req->r_osdc;
884 struct ceph_osd *osd = req->r_osd;
887 * Linger requests need to be resent with a new tid to avoid
888 * the dup op detection logic on the OSDs. Achieve this with
889 * a re-register dance instead of open-coding.
891 ceph_osdc_get_request(req);
892 if (!list_empty(&req->r_linger_item))
893 __unregister_linger_request(osdc, req);
895 __unregister_request(osdc, req);
896 __register_request(osdc, req);
897 ceph_osdc_put_request(req);
900 * Unless request has been registered as both normal and
901 * lingering, __unregister{,_linger}_request clears r_osd.
902 * However, here we need to preserve r_osd to make sure we
903 * requeue on the same OSD.
905 WARN_ON(req->r_osd || !osd);
908 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
909 __enqueue_request(req);
913 * Resubmit requests pending on the given osd.
915 static void __kick_osd_requests(struct ceph_osd_client *osdc,
916 struct ceph_osd *osd)
918 struct ceph_osd_request *req, *nreq;
920 LIST_HEAD(resend_linger);
923 dout("%s osd%d\n", __func__, osd->o_osd);
924 err = __reset_osd(osdc, osd);
929 * Build up a list of requests to resend by traversing the
930 * osd's list of requests. Requests for a given object are
931 * sent in tid order, and that is also the order they're
932 * kept on this list. Therefore all requests that are in
933 * flight will be found first, followed by all requests that
934 * have not yet been sent. And to resend requests while
935 * preserving this order we will want to put any sent
936 * requests back on the front of the osd client's unsent
939 * So we build a separate ordered list of already-sent
940 * requests for the affected osd and splice it onto the
941 * front of the osd client's unsent list. Once we've seen a
942 * request that has not yet been sent we're done. Those
943 * requests are already sitting right where they belong.
945 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
949 if (!req->r_linger) {
950 dout("%s requeueing %p tid %llu\n", __func__, req,
952 list_move_tail(&req->r_req_lru_item, &resend);
953 req->r_flags |= CEPH_OSD_FLAG_RETRY;
955 list_move_tail(&req->r_req_lru_item, &resend_linger);
958 list_splice(&resend, &osdc->req_unsent);
961 * Both registered and not yet registered linger requests are
962 * enqueued with a new tid on the same OSD. We add/move them
963 * to req_unsent/o_requests at the end to keep things in tid
966 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
968 WARN_ON(!list_empty(&req->r_req_lru_item));
969 __kick_linger_request(req);
972 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
973 __kick_linger_request(req);
977 * If the osd connection drops, we need to resubmit all requests.
979 static void osd_reset(struct ceph_connection *con)
981 struct ceph_osd *osd = con->private;
982 struct ceph_osd_client *osdc;
986 dout("osd_reset osd%d\n", osd->o_osd);
988 down_read(&osdc->map_sem);
989 mutex_lock(&osdc->request_mutex);
990 __kick_osd_requests(osdc, osd);
992 mutex_unlock(&osdc->request_mutex);
993 up_read(&osdc->map_sem);
997 * Track open sessions with osds.
999 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1001 struct ceph_osd *osd;
1003 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1007 atomic_set(&osd->o_ref, 1);
1010 RB_CLEAR_NODE(&osd->o_node);
1011 INIT_LIST_HEAD(&osd->o_requests);
1012 INIT_LIST_HEAD(&osd->o_linger_requests);
1013 INIT_LIST_HEAD(&osd->o_osd_lru);
1014 osd->o_incarnation = 1;
1016 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1018 INIT_LIST_HEAD(&osd->o_keepalive_item);
1022 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1024 if (atomic_inc_not_zero(&osd->o_ref)) {
1025 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1026 atomic_read(&osd->o_ref));
1029 dout("get_osd %p FAIL\n", osd);
1034 static void put_osd(struct ceph_osd *osd)
1036 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1037 atomic_read(&osd->o_ref) - 1);
1038 if (atomic_dec_and_test(&osd->o_ref)) {
1039 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1041 if (osd->o_auth.authorizer)
1042 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
1048 * remove an osd from our map
1050 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1052 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1053 WARN_ON(!list_empty(&osd->o_requests));
1054 WARN_ON(!list_empty(&osd->o_linger_requests));
1056 list_del_init(&osd->o_osd_lru);
1057 rb_erase(&osd->o_node, &osdc->osds);
1058 RB_CLEAR_NODE(&osd->o_node);
1061 static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1063 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1065 if (!RB_EMPTY_NODE(&osd->o_node)) {
1066 ceph_con_close(&osd->o_con);
1067 __remove_osd(osdc, osd);
1072 static void remove_all_osds(struct ceph_osd_client *osdc)
1074 dout("%s %p\n", __func__, osdc);
1075 mutex_lock(&osdc->request_mutex);
1076 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1077 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1078 struct ceph_osd, o_node);
1079 remove_osd(osdc, osd);
1081 mutex_unlock(&osdc->request_mutex);
1084 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1085 struct ceph_osd *osd)
1087 dout("%s %p\n", __func__, osd);
1088 BUG_ON(!list_empty(&osd->o_osd_lru));
1090 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1091 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
1094 static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1095 struct ceph_osd *osd)
1097 dout("%s %p\n", __func__, osd);
1099 if (list_empty(&osd->o_requests) &&
1100 list_empty(&osd->o_linger_requests))
1101 __move_osd_to_lru(osdc, osd);
1104 static void __remove_osd_from_lru(struct ceph_osd *osd)
1106 dout("__remove_osd_from_lru %p\n", osd);
1107 if (!list_empty(&osd->o_osd_lru))
1108 list_del_init(&osd->o_osd_lru);
1111 static void remove_old_osds(struct ceph_osd_client *osdc)
1113 struct ceph_osd *osd, *nosd;
1115 dout("__remove_old_osds %p\n", osdc);
1116 mutex_lock(&osdc->request_mutex);
1117 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1118 if (time_before(jiffies, osd->lru_ttl))
1120 remove_osd(osdc, osd);
1122 mutex_unlock(&osdc->request_mutex);
1128 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1130 struct ceph_entity_addr *peer_addr;
1132 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1133 if (list_empty(&osd->o_requests) &&
1134 list_empty(&osd->o_linger_requests)) {
1135 remove_osd(osdc, osd);
1139 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1140 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1141 !ceph_con_opened(&osd->o_con)) {
1142 struct ceph_osd_request *req;
1144 dout("osd addr hasn't changed and connection never opened, "
1145 "letting msgr retry\n");
1146 /* touch each r_stamp for handle_timeout()'s benfit */
1147 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1148 req->r_stamp = jiffies;
1153 ceph_con_close(&osd->o_con);
1154 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1155 osd->o_incarnation++;
1160 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1162 struct rb_node **p = &osdc->osds.rb_node;
1163 struct rb_node *parent = NULL;
1164 struct ceph_osd *osd = NULL;
1166 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1169 osd = rb_entry(parent, struct ceph_osd, o_node);
1170 if (new->o_osd < osd->o_osd)
1172 else if (new->o_osd > osd->o_osd)
1173 p = &(*p)->rb_right;
1178 rb_link_node(&new->o_node, parent, p);
1179 rb_insert_color(&new->o_node, &osdc->osds);
1182 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1184 struct ceph_osd *osd;
1185 struct rb_node *n = osdc->osds.rb_node;
1188 osd = rb_entry(n, struct ceph_osd, o_node);
1191 else if (o > osd->o_osd)
1199 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1201 schedule_delayed_work(&osdc->timeout_work,
1202 osdc->client->options->osd_keepalive_timeout * HZ);
1205 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1207 cancel_delayed_work(&osdc->timeout_work);
1211 * Register request, assign tid. If this is the first request, set up
1212 * the timeout event.
1214 static void __register_request(struct ceph_osd_client *osdc,
1215 struct ceph_osd_request *req)
1217 req->r_tid = ++osdc->last_tid;
1218 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1219 dout("__register_request %p tid %lld\n", req, req->r_tid);
1220 __insert_request(osdc, req);
1221 ceph_osdc_get_request(req);
1222 osdc->num_requests++;
1223 if (osdc->num_requests == 1) {
1224 dout(" first request, scheduling timeout\n");
1225 __schedule_osd_timeout(osdc);
1230 * called under osdc->request_mutex
1232 static void __unregister_request(struct ceph_osd_client *osdc,
1233 struct ceph_osd_request *req)
1235 if (RB_EMPTY_NODE(&req->r_node)) {
1236 dout("__unregister_request %p tid %lld not registered\n",
1241 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1242 rb_erase(&req->r_node, &osdc->requests);
1243 RB_CLEAR_NODE(&req->r_node);
1244 osdc->num_requests--;
1247 /* make sure the original request isn't in flight. */
1248 ceph_msg_revoke(req->r_request);
1250 list_del_init(&req->r_osd_item);
1251 maybe_move_osd_to_lru(osdc, req->r_osd);
1252 if (list_empty(&req->r_linger_osd_item))
1256 list_del_init(&req->r_req_lru_item);
1257 ceph_osdc_put_request(req);
1259 if (osdc->num_requests == 0) {
1260 dout(" no requests, canceling timeout\n");
1261 __cancel_osd_timeout(osdc);
1266 * Cancel a previously queued request message
1268 static void __cancel_request(struct ceph_osd_request *req)
1270 if (req->r_sent && req->r_osd) {
1271 ceph_msg_revoke(req->r_request);
1276 static void __register_linger_request(struct ceph_osd_client *osdc,
1277 struct ceph_osd_request *req)
1279 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1280 WARN_ON(!req->r_linger);
1282 ceph_osdc_get_request(req);
1283 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1285 list_add_tail(&req->r_linger_osd_item,
1286 &req->r_osd->o_linger_requests);
1289 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1290 struct ceph_osd_request *req)
1292 WARN_ON(!req->r_linger);
1294 if (list_empty(&req->r_linger_item)) {
1295 dout("%s %p tid %llu not registered\n", __func__, req,
1300 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1301 list_del_init(&req->r_linger_item);
1304 list_del_init(&req->r_linger_osd_item);
1305 maybe_move_osd_to_lru(osdc, req->r_osd);
1306 if (list_empty(&req->r_osd_item))
1309 ceph_osdc_put_request(req);
1312 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1313 struct ceph_osd_request *req)
1315 if (!req->r_linger) {
1316 dout("set_request_linger %p\n", req);
1320 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1323 * Returns whether a request should be blocked from being sent
1324 * based on the current osdmap and osd_client settings.
1326 * Caller should hold map_sem for read.
1328 static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1329 struct ceph_osd_request *req)
1331 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1332 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1333 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1334 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1335 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1339 * Calculate mapping of a request to a PG. Takes tiering into account.
1341 static int __calc_request_pg(struct ceph_osdmap *osdmap,
1342 struct ceph_osd_request *req,
1343 struct ceph_pg *pg_out)
1345 bool need_check_tiering;
1347 need_check_tiering = false;
1348 if (req->r_target_oloc.pool == -1) {
1349 req->r_target_oloc = req->r_base_oloc; /* struct */
1350 need_check_tiering = true;
1352 if (req->r_target_oid.name_len == 0) {
1353 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1354 need_check_tiering = true;
1357 if (need_check_tiering &&
1358 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1359 struct ceph_pg_pool_info *pi;
1361 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
1363 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1365 req->r_target_oloc.pool = pi->read_tier;
1366 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1367 pi->write_tier >= 0)
1368 req->r_target_oloc.pool = pi->write_tier;
1370 /* !pi is caught in ceph_oloc_oid_to_pg() */
1373 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1374 &req->r_target_oid, pg_out);
1377 static void __enqueue_request(struct ceph_osd_request *req)
1379 struct ceph_osd_client *osdc = req->r_osdc;
1381 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1382 req->r_osd ? req->r_osd->o_osd : -1);
1385 __remove_osd_from_lru(req->r_osd);
1386 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1387 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1389 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1394 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1395 * (as needed), and set the request r_osd appropriately. If there is
1396 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1397 * (unsent, homeless) or leave on in-flight lru.
1399 * Return 0 if unchanged, 1 if changed, or negative on error.
1401 * Caller should hold map_sem for read and request_mutex.
1403 static int __map_request(struct ceph_osd_client *osdc,
1404 struct ceph_osd_request *req, int force_resend)
1406 struct ceph_pg pgid;
1407 int acting[CEPH_PG_MAX_SIZE];
1412 dout("map_request %p tid %lld\n", req, req->r_tid);
1414 err = __calc_request_pg(osdc->osdmap, req, &pgid);
1416 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1421 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1425 was_paused = req->r_paused;
1426 req->r_paused = __req_should_be_paused(osdc, req);
1427 if (was_paused && !req->r_paused)
1430 if ((!force_resend &&
1431 req->r_osd && req->r_osd->o_osd == o &&
1432 req->r_sent >= req->r_osd->o_incarnation &&
1433 req->r_num_pg_osds == num &&
1434 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1435 (req->r_osd == NULL && o == -1) ||
1437 return 0; /* no change */
1439 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1440 req->r_tid, pgid.pool, pgid.seed, o,
1441 req->r_osd ? req->r_osd->o_osd : -1);
1443 /* record full pg acting set */
1444 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1445 req->r_num_pg_osds = num;
1448 __cancel_request(req);
1449 list_del_init(&req->r_osd_item);
1450 list_del_init(&req->r_linger_osd_item);
1454 req->r_osd = __lookup_osd(osdc, o);
1455 if (!req->r_osd && o >= 0) {
1457 req->r_osd = create_osd(osdc, o);
1459 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1463 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1464 __insert_osd(osdc, req->r_osd);
1466 ceph_con_open(&req->r_osd->o_con,
1467 CEPH_ENTITY_TYPE_OSD, o,
1468 &osdc->osdmap->osd_addr[o]);
1471 __enqueue_request(req);
1472 err = 1; /* osd or pg changed */
1479 * caller should hold map_sem (for read) and request_mutex
1481 static void __send_request(struct ceph_osd_client *osdc,
1482 struct ceph_osd_request *req)
1486 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1487 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1488 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1490 /* fill in message content that changes each time we send it */
1491 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1492 put_unaligned_le32(req->r_flags, req->r_request_flags);
1493 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1494 p = req->r_request_pgid;
1495 ceph_encode_64(&p, req->r_pgid.pool);
1496 ceph_encode_32(&p, req->r_pgid.seed);
1497 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1498 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1499 sizeof(req->r_reassert_version));
1501 req->r_stamp = jiffies;
1502 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1504 ceph_msg_get(req->r_request); /* send consumes a ref */
1506 req->r_sent = req->r_osd->o_incarnation;
1508 ceph_con_send(&req->r_osd->o_con, req->r_request);
1512 * Send any requests in the queue (req_unsent).
1514 static void __send_queued(struct ceph_osd_client *osdc)
1516 struct ceph_osd_request *req, *tmp;
1518 dout("__send_queued\n");
1519 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1520 __send_request(osdc, req);
1524 * Caller should hold map_sem for read and request_mutex.
1526 static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1527 struct ceph_osd_request *req,
1532 __register_request(osdc, req);
1534 req->r_got_reply = 0;
1535 rc = __map_request(osdc, req, 0);
1538 dout("osdc_start_request failed map, "
1539 " will retry %lld\n", req->r_tid);
1542 __unregister_request(osdc, req);
1547 if (req->r_osd == NULL) {
1548 dout("send_request %p no up osds in pg\n", req);
1549 ceph_monc_request_next_osdmap(&osdc->client->monc);
1551 __send_queued(osdc);
1558 * Timeout callback, called every N seconds when 1 or more osd
1559 * requests has been active for more than N seconds. When this
1560 * happens, we ping all OSDs with requests who have timed out to
1561 * ensure any communications channel reset is detected. Reset the
1562 * request timeouts another N seconds in the future as we go.
1563 * Reschedule the timeout event another N seconds in future (unless
1564 * there are no open requests).
1566 static void handle_timeout(struct work_struct *work)
1568 struct ceph_osd_client *osdc =
1569 container_of(work, struct ceph_osd_client, timeout_work.work);
1570 struct ceph_osd_request *req;
1571 struct ceph_osd *osd;
1572 unsigned long keepalive =
1573 osdc->client->options->osd_keepalive_timeout * HZ;
1574 struct list_head slow_osds;
1576 down_read(&osdc->map_sem);
1578 ceph_monc_request_next_osdmap(&osdc->client->monc);
1580 mutex_lock(&osdc->request_mutex);
1583 * ping osds that are a bit slow. this ensures that if there
1584 * is a break in the TCP connection we will notice, and reopen
1585 * a connection with that osd (from the fault callback).
1587 INIT_LIST_HEAD(&slow_osds);
1588 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1589 if (time_before(jiffies, req->r_stamp + keepalive))
1594 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1595 req->r_tid, osd->o_osd);
1596 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1598 while (!list_empty(&slow_osds)) {
1599 osd = list_entry(slow_osds.next, struct ceph_osd,
1601 list_del_init(&osd->o_keepalive_item);
1602 ceph_con_keepalive(&osd->o_con);
1605 __schedule_osd_timeout(osdc);
1606 __send_queued(osdc);
1607 mutex_unlock(&osdc->request_mutex);
1608 up_read(&osdc->map_sem);
1611 static void handle_osds_timeout(struct work_struct *work)
1613 struct ceph_osd_client *osdc =
1614 container_of(work, struct ceph_osd_client,
1615 osds_timeout_work.work);
1616 unsigned long delay =
1617 osdc->client->options->osd_idle_ttl * HZ >> 2;
1619 dout("osds timeout\n");
1620 down_read(&osdc->map_sem);
1621 remove_old_osds(osdc);
1622 up_read(&osdc->map_sem);
1624 schedule_delayed_work(&osdc->osds_timeout_work,
1625 round_jiffies_relative(delay));
1628 static int ceph_oloc_decode(void **p, void *end,
1629 struct ceph_object_locator *oloc)
1631 u8 struct_v, struct_cv;
1636 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1637 struct_v = ceph_decode_8(p);
1638 struct_cv = ceph_decode_8(p);
1640 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1641 struct_v, struct_cv);
1644 if (struct_cv > 6) {
1645 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1646 struct_v, struct_cv);
1649 len = ceph_decode_32(p);
1650 ceph_decode_need(p, end, len, e_inval);
1651 struct_end = *p + len;
1653 oloc->pool = ceph_decode_64(p);
1654 *p += 4; /* skip preferred */
1656 len = ceph_decode_32(p);
1658 pr_warn("ceph_object_locator::key is set\n");
1662 if (struct_v >= 5) {
1663 len = ceph_decode_32(p);
1665 pr_warn("ceph_object_locator::nspace is set\n");
1670 if (struct_v >= 6) {
1671 s64 hash = ceph_decode_64(p);
1673 pr_warn("ceph_object_locator::hash is set\n");
1688 static int ceph_redirect_decode(void **p, void *end,
1689 struct ceph_request_redirect *redir)
1691 u8 struct_v, struct_cv;
1696 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1697 struct_v = ceph_decode_8(p);
1698 struct_cv = ceph_decode_8(p);
1699 if (struct_cv > 1) {
1700 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1701 struct_v, struct_cv);
1704 len = ceph_decode_32(p);
1705 ceph_decode_need(p, end, len, e_inval);
1706 struct_end = *p + len;
1708 ret = ceph_oloc_decode(p, end, &redir->oloc);
1712 len = ceph_decode_32(p);
1714 pr_warn("ceph_request_redirect::object_name is set\n");
1718 len = ceph_decode_32(p);
1719 *p += len; /* skip osd_instructions */
1731 static void complete_request(struct ceph_osd_request *req)
1733 complete_all(&req->r_safe_completion); /* fsync waiter */
1737 * handle osd op reply. either call the callback if it is specified,
1738 * or do the completion to wake up the waiting thread.
1740 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1741 struct ceph_connection *con)
1744 struct ceph_osd_request *req;
1745 struct ceph_request_redirect redir;
1748 unsigned int numops;
1749 int payload_len, flags;
1755 u64 reassert_version;
1757 int already_completed;
1761 tid = le64_to_cpu(msg->hdr.tid);
1762 dout("handle_reply %p tid %llu\n", msg, tid);
1764 p = msg->front.iov_base;
1765 end = p + msg->front.iov_len;
1767 ceph_decode_need(&p, end, 4, bad);
1768 object_len = ceph_decode_32(&p);
1769 ceph_decode_need(&p, end, object_len, bad);
1772 err = ceph_decode_pgid(&p, end, &pg);
1776 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1777 flags = ceph_decode_64(&p);
1778 result = ceph_decode_32(&p);
1779 reassert_epoch = ceph_decode_32(&p);
1780 reassert_version = ceph_decode_64(&p);
1781 osdmap_epoch = ceph_decode_32(&p);
1784 down_read(&osdc->map_sem);
1785 mutex_lock(&osdc->request_mutex);
1786 req = __lookup_request(osdc, tid);
1788 dout("handle_reply tid %llu dne\n", tid);
1791 ceph_osdc_get_request(req);
1793 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1796 ceph_decode_need(&p, end, 4, bad_put);
1797 numops = ceph_decode_32(&p);
1798 if (numops > CEPH_OSD_MAX_OP)
1800 if (numops != req->r_num_ops)
1803 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1804 for (i = 0; i < numops; i++) {
1805 struct ceph_osd_op *op = p;
1808 len = le32_to_cpu(op->payload_len);
1809 req->r_reply_op_len[i] = len;
1810 dout(" op %d has %d bytes\n", i, len);
1814 bytes = le32_to_cpu(msg->hdr.data_len);
1815 if (payload_len != bytes) {
1816 pr_warn("sum of op payload lens %d != data_len %d\n",
1817 payload_len, bytes);
1821 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1822 retry_attempt = ceph_decode_32(&p);
1823 for (i = 0; i < numops; i++)
1824 req->r_reply_op_result[i] = ceph_decode_32(&p);
1826 if (le16_to_cpu(msg->hdr.version) >= 6) {
1827 p += 8 + 4; /* skip replay_version */
1828 p += 8; /* skip user_version */
1830 err = ceph_redirect_decode(&p, end, &redir);
1834 redir.oloc.pool = -1;
1837 if (redir.oloc.pool != -1) {
1838 dout("redirect pool %lld\n", redir.oloc.pool);
1840 __unregister_request(osdc, req);
1842 req->r_target_oloc = redir.oloc; /* struct */
1845 * Start redirect requests with nofail=true. If
1846 * mapping fails, request will end up on the notarget
1847 * list, waiting for the new osdmap (which can take
1848 * a while), even though the original request mapped
1849 * successfully. In the future we might want to follow
1850 * original request's nofail setting here.
1852 err = __ceph_osdc_start_request(osdc, req, true);
1858 already_completed = req->r_got_reply;
1859 if (!req->r_got_reply) {
1860 req->r_result = result;
1861 dout("handle_reply result %d bytes %d\n", req->r_result,
1863 if (req->r_result == 0)
1864 req->r_result = bytes;
1866 /* in case this is a write and we need to replay, */
1867 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1868 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1870 req->r_got_reply = 1;
1871 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1872 dout("handle_reply tid %llu dup ack\n", tid);
1876 dout("handle_reply tid %llu flags %d\n", tid, flags);
1878 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1879 __register_linger_request(osdc, req);
1881 /* either this is a read, or we got the safe response */
1883 (flags & CEPH_OSD_FLAG_ONDISK) ||
1884 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1885 __unregister_request(osdc, req);
1887 mutex_unlock(&osdc->request_mutex);
1888 up_read(&osdc->map_sem);
1890 if (!already_completed) {
1891 if (req->r_unsafe_callback &&
1892 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1893 req->r_unsafe_callback(req, true);
1894 if (req->r_callback)
1895 req->r_callback(req, msg);
1897 complete_all(&req->r_completion);
1900 if (flags & CEPH_OSD_FLAG_ONDISK) {
1901 if (req->r_unsafe_callback && already_completed)
1902 req->r_unsafe_callback(req, false);
1903 complete_request(req);
1907 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1908 ceph_osdc_put_request(req);
1911 mutex_unlock(&osdc->request_mutex);
1912 up_read(&osdc->map_sem);
1916 req->r_result = -EIO;
1917 __unregister_request(osdc, req);
1918 if (req->r_callback)
1919 req->r_callback(req, msg);
1921 complete_all(&req->r_completion);
1922 complete_request(req);
1923 ceph_osdc_put_request(req);
1925 mutex_unlock(&osdc->request_mutex);
1926 up_read(&osdc->map_sem);
1928 pr_err("corrupt osd_op_reply got %d %d\n",
1929 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1933 static void reset_changed_osds(struct ceph_osd_client *osdc)
1935 struct rb_node *p, *n;
1937 dout("%s %p\n", __func__, osdc);
1938 for (p = rb_first(&osdc->osds); p; p = n) {
1939 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1942 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1943 memcmp(&osd->o_con.peer_addr,
1944 ceph_osd_addr(osdc->osdmap,
1946 sizeof(struct ceph_entity_addr)) != 0)
1947 __reset_osd(osdc, osd);
1952 * Requeue requests whose mapping to an OSD has changed. If requests map to
1953 * no osd, request a new map.
1955 * Caller should hold map_sem for read.
1957 static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1958 bool force_resend_writes)
1960 struct ceph_osd_request *req, *nreq;
1964 bool force_resend_req;
1966 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1967 force_resend_writes ? " (force resend writes)" : "");
1968 mutex_lock(&osdc->request_mutex);
1969 for (p = rb_first(&osdc->requests); p; ) {
1970 req = rb_entry(p, struct ceph_osd_request, r_node);
1974 * For linger requests that have not yet been
1975 * registered, move them to the linger list; they'll
1976 * be sent to the osd in the loop below. Unregister
1977 * the request before re-registering it as a linger
1978 * request to ensure the __map_request() below
1979 * will decide it needs to be sent.
1981 if (req->r_linger && list_empty(&req->r_linger_item)) {
1982 dout("%p tid %llu restart on osd%d\n",
1984 req->r_osd ? req->r_osd->o_osd : -1);
1985 ceph_osdc_get_request(req);
1986 __unregister_request(osdc, req);
1987 __register_linger_request(osdc, req);
1988 ceph_osdc_put_request(req);
1992 force_resend_req = force_resend ||
1993 (force_resend_writes &&
1994 req->r_flags & CEPH_OSD_FLAG_WRITE);
1995 err = __map_request(osdc, req, force_resend_req);
1997 continue; /* error */
1998 if (req->r_osd == NULL) {
1999 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2000 needmap++; /* request a newer map */
2001 } else if (err > 0) {
2002 if (!req->r_linger) {
2003 dout("%p tid %llu requeued on osd%d\n", req,
2005 req->r_osd ? req->r_osd->o_osd : -1);
2006 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2011 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2013 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2015 err = __map_request(osdc, req,
2016 force_resend || force_resend_writes);
2017 dout("__map_request returned %d\n", err);
2019 continue; /* hrm! */
2020 if (req->r_osd == NULL || err > 0) {
2021 if (req->r_osd == NULL) {
2022 dout("lingering %p tid %llu maps to no osd\n",
2025 * A homeless lingering request makes
2026 * no sense, as it's job is to keep
2027 * a particular OSD connection open.
2028 * Request a newer map and kick the
2029 * request, knowing that it won't be
2030 * resent until we actually get a map
2031 * that can tell us where to send it.
2036 dout("kicking lingering %p tid %llu osd%d\n", req,
2037 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2038 __register_request(osdc, req);
2039 __unregister_linger_request(osdc, req);
2042 reset_changed_osds(osdc);
2043 mutex_unlock(&osdc->request_mutex);
2046 dout("%d requests for down osds, need new map\n", needmap);
2047 ceph_monc_request_next_osdmap(&osdc->client->monc);
2053 * Process updated osd map.
2055 * The message contains any number of incremental and full maps, normally
2056 * indicating some sort of topology change in the cluster. Kick requests
2057 * off to different OSDs as needed.
2059 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2061 void *p, *end, *next;
2062 u32 nr_maps, maplen;
2064 struct ceph_osdmap *newmap = NULL, *oldmap;
2066 struct ceph_fsid fsid;
2069 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2070 p = msg->front.iov_base;
2071 end = p + msg->front.iov_len;
2074 ceph_decode_need(&p, end, sizeof(fsid), bad);
2075 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2076 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2079 down_write(&osdc->map_sem);
2081 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2083 /* incremental maps */
2084 ceph_decode_32_safe(&p, end, nr_maps, bad);
2085 dout(" %d inc maps\n", nr_maps);
2086 while (nr_maps > 0) {
2087 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2088 epoch = ceph_decode_32(&p);
2089 maplen = ceph_decode_32(&p);
2090 ceph_decode_need(&p, end, maplen, bad);
2092 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2093 dout("applying incremental map %u len %d\n",
2095 newmap = osdmap_apply_incremental(&p, next,
2097 &osdc->client->msgr);
2098 if (IS_ERR(newmap)) {
2099 err = PTR_ERR(newmap);
2103 if (newmap != osdc->osdmap) {
2104 ceph_osdmap_destroy(osdc->osdmap);
2105 osdc->osdmap = newmap;
2107 was_full = was_full ||
2108 ceph_osdmap_flag(osdc->osdmap,
2110 kick_requests(osdc, 0, was_full);
2112 dout("ignoring incremental map %u len %d\n",
2122 ceph_decode_32_safe(&p, end, nr_maps, bad);
2123 dout(" %d full maps\n", nr_maps);
2125 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2126 epoch = ceph_decode_32(&p);
2127 maplen = ceph_decode_32(&p);
2128 ceph_decode_need(&p, end, maplen, bad);
2130 dout("skipping non-latest full map %u len %d\n",
2132 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2133 dout("skipping full map %u len %d, "
2134 "older than our %u\n", epoch, maplen,
2135 osdc->osdmap->epoch);
2137 int skipped_map = 0;
2139 dout("taking full map %u len %d\n", epoch, maplen);
2140 newmap = ceph_osdmap_decode(&p, p+maplen);
2141 if (IS_ERR(newmap)) {
2142 err = PTR_ERR(newmap);
2146 oldmap = osdc->osdmap;
2147 osdc->osdmap = newmap;
2149 if (oldmap->epoch + 1 < newmap->epoch)
2151 ceph_osdmap_destroy(oldmap);
2153 was_full = was_full ||
2154 ceph_osdmap_flag(osdc->osdmap,
2156 kick_requests(osdc, skipped_map, was_full);
2165 downgrade_write(&osdc->map_sem);
2166 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
2169 * subscribe to subsequent osdmap updates if full to ensure
2170 * we find out when we are no longer full and stop returning
2173 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2174 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2175 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2176 ceph_monc_request_next_osdmap(&osdc->client->monc);
2178 mutex_lock(&osdc->request_mutex);
2179 __send_queued(osdc);
2180 mutex_unlock(&osdc->request_mutex);
2181 up_read(&osdc->map_sem);
2182 wake_up_all(&osdc->client->auth_wq);
2186 pr_err("osdc handle_map corrupt msg\n");
2188 up_write(&osdc->map_sem);
2192 * watch/notify callback event infrastructure
2194 * These callbacks are used both for watch and notify operations.
2196 static void __release_event(struct kref *kref)
2198 struct ceph_osd_event *event =
2199 container_of(kref, struct ceph_osd_event, kref);
2201 dout("__release_event %p\n", event);
2205 static void get_event(struct ceph_osd_event *event)
2207 kref_get(&event->kref);
2210 void ceph_osdc_put_event(struct ceph_osd_event *event)
2212 kref_put(&event->kref, __release_event);
2214 EXPORT_SYMBOL(ceph_osdc_put_event);
2216 static void __insert_event(struct ceph_osd_client *osdc,
2217 struct ceph_osd_event *new)
2219 struct rb_node **p = &osdc->event_tree.rb_node;
2220 struct rb_node *parent = NULL;
2221 struct ceph_osd_event *event = NULL;
2225 event = rb_entry(parent, struct ceph_osd_event, node);
2226 if (new->cookie < event->cookie)
2228 else if (new->cookie > event->cookie)
2229 p = &(*p)->rb_right;
2234 rb_link_node(&new->node, parent, p);
2235 rb_insert_color(&new->node, &osdc->event_tree);
2238 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2241 struct rb_node **p = &osdc->event_tree.rb_node;
2242 struct rb_node *parent = NULL;
2243 struct ceph_osd_event *event = NULL;
2247 event = rb_entry(parent, struct ceph_osd_event, node);
2248 if (cookie < event->cookie)
2250 else if (cookie > event->cookie)
2251 p = &(*p)->rb_right;
2258 static void __remove_event(struct ceph_osd_event *event)
2260 struct ceph_osd_client *osdc = event->osdc;
2262 if (!RB_EMPTY_NODE(&event->node)) {
2263 dout("__remove_event removed %p\n", event);
2264 rb_erase(&event->node, &osdc->event_tree);
2265 ceph_osdc_put_event(event);
2267 dout("__remove_event didn't remove %p\n", event);
2271 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2272 void (*event_cb)(u64, u64, u8, void *),
2273 void *data, struct ceph_osd_event **pevent)
2275 struct ceph_osd_event *event;
2277 event = kmalloc(sizeof(*event), GFP_NOIO);
2281 dout("create_event %p\n", event);
2282 event->cb = event_cb;
2283 event->one_shot = 0;
2286 INIT_LIST_HEAD(&event->osd_node);
2287 RB_CLEAR_NODE(&event->node);
2288 kref_init(&event->kref); /* one ref for us */
2289 kref_get(&event->kref); /* one ref for the caller */
2291 spin_lock(&osdc->event_lock);
2292 event->cookie = ++osdc->event_count;
2293 __insert_event(osdc, event);
2294 spin_unlock(&osdc->event_lock);
2299 EXPORT_SYMBOL(ceph_osdc_create_event);
2301 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2303 struct ceph_osd_client *osdc = event->osdc;
2305 dout("cancel_event %p\n", event);
2306 spin_lock(&osdc->event_lock);
2307 __remove_event(event);
2308 spin_unlock(&osdc->event_lock);
2309 ceph_osdc_put_event(event); /* caller's */
2311 EXPORT_SYMBOL(ceph_osdc_cancel_event);
2314 static void do_event_work(struct work_struct *work)
2316 struct ceph_osd_event_work *event_work =
2317 container_of(work, struct ceph_osd_event_work, work);
2318 struct ceph_osd_event *event = event_work->event;
2319 u64 ver = event_work->ver;
2320 u64 notify_id = event_work->notify_id;
2321 u8 opcode = event_work->opcode;
2323 dout("do_event_work completing %p\n", event);
2324 event->cb(ver, notify_id, opcode, event->data);
2325 dout("do_event_work completed %p\n", event);
2326 ceph_osdc_put_event(event);
2332 * Process osd watch notifications
2334 static void handle_watch_notify(struct ceph_osd_client *osdc,
2335 struct ceph_msg *msg)
2339 u64 cookie, ver, notify_id;
2341 struct ceph_osd_event *event;
2342 struct ceph_osd_event_work *event_work;
2344 p = msg->front.iov_base;
2345 end = p + msg->front.iov_len;
2347 ceph_decode_8_safe(&p, end, proto_ver, bad);
2348 ceph_decode_8_safe(&p, end, opcode, bad);
2349 ceph_decode_64_safe(&p, end, cookie, bad);
2350 ceph_decode_64_safe(&p, end, ver, bad);
2351 ceph_decode_64_safe(&p, end, notify_id, bad);
2353 spin_lock(&osdc->event_lock);
2354 event = __find_event(osdc, cookie);
2356 BUG_ON(event->one_shot);
2359 spin_unlock(&osdc->event_lock);
2360 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2361 cookie, ver, event);
2363 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2365 pr_err("couldn't allocate event_work\n");
2366 ceph_osdc_put_event(event);
2369 INIT_WORK(&event_work->work, do_event_work);
2370 event_work->event = event;
2371 event_work->ver = ver;
2372 event_work->notify_id = notify_id;
2373 event_work->opcode = opcode;
2375 queue_work(osdc->notify_wq, &event_work->work);
2381 pr_err("osdc handle_watch_notify corrupt msg\n");
2385 * build new request AND message
2388 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2389 struct ceph_snap_context *snapc, u64 snap_id,
2390 struct timespec *mtime)
2392 struct ceph_msg *msg = req->r_request;
2395 int flags = req->r_flags;
2399 req->r_snapid = snap_id;
2400 req->r_snapc = ceph_get_snap_context(snapc);
2402 /* encode request */
2403 msg->hdr.version = cpu_to_le16(4);
2405 p = msg->front.iov_base;
2406 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2407 req->r_request_osdmap_epoch = p;
2409 req->r_request_flags = p;
2411 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2412 ceph_encode_timespec(p, mtime);
2413 p += sizeof(struct ceph_timespec);
2414 req->r_request_reassert_version = p;
2415 p += sizeof(struct ceph_eversion); /* will get filled in */
2418 ceph_encode_8(&p, 4);
2419 ceph_encode_8(&p, 4);
2420 ceph_encode_32(&p, 8 + 4 + 4);
2421 req->r_request_pool = p;
2423 ceph_encode_32(&p, -1); /* preferred */
2424 ceph_encode_32(&p, 0); /* key len */
2426 ceph_encode_8(&p, 1);
2427 req->r_request_pgid = p;
2429 ceph_encode_32(&p, -1); /* preferred */
2432 ceph_encode_32(&p, req->r_base_oid.name_len);
2433 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2434 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2435 req->r_base_oid.name, req->r_base_oid.name_len);
2436 p += req->r_base_oid.name_len;
2438 /* ops--can imply data */
2439 ceph_encode_16(&p, (u16)req->r_num_ops);
2441 for (i = 0; i < req->r_num_ops; i++) {
2442 data_len += osd_req_encode_op(req, p, i);
2443 p += sizeof(struct ceph_osd_op);
2447 ceph_encode_64(&p, req->r_snapid);
2448 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2449 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2451 for (i = 0; i < snapc->num_snaps; i++) {
2452 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2456 req->r_request_attempts = p;
2460 if (flags & CEPH_OSD_FLAG_WRITE) {
2464 * The header "data_off" is a hint to the receiver
2465 * allowing it to align received data into its
2466 * buffers such that there's no need to re-copy
2467 * it before writing it to disk (direct I/O).
2469 data_off = (u16) (off & 0xffff);
2470 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2472 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2474 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2475 msg_size = p - msg->front.iov_base;
2476 msg->front.iov_len = msg_size;
2477 msg->hdr.front_len = cpu_to_le32(msg_size);
2479 dout("build_request msg_size was %d\n", (int)msg_size);
2481 EXPORT_SYMBOL(ceph_osdc_build_request);
2484 * Register request, send initial attempt.
2486 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2487 struct ceph_osd_request *req,
2492 down_read(&osdc->map_sem);
2493 mutex_lock(&osdc->request_mutex);
2495 rc = __ceph_osdc_start_request(osdc, req, nofail);
2497 mutex_unlock(&osdc->request_mutex);
2498 up_read(&osdc->map_sem);
2502 EXPORT_SYMBOL(ceph_osdc_start_request);
2505 * Unregister a registered request. The request is not completed (i.e.
2506 * no callbacks or wakeups) - higher layers are supposed to know what
2507 * they are canceling.
2509 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2511 struct ceph_osd_client *osdc = req->r_osdc;
2513 mutex_lock(&osdc->request_mutex);
2515 __unregister_linger_request(osdc, req);
2516 __unregister_request(osdc, req);
2517 mutex_unlock(&osdc->request_mutex);
2519 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2521 EXPORT_SYMBOL(ceph_osdc_cancel_request);
2524 * wait for a request to complete
2526 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2527 struct ceph_osd_request *req)
2531 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2533 rc = wait_for_completion_interruptible(&req->r_completion);
2535 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2536 ceph_osdc_cancel_request(req);
2537 complete_request(req);
2541 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2543 return req->r_result;
2545 EXPORT_SYMBOL(ceph_osdc_wait_request);
2548 * sync - wait for all in-flight requests to flush. avoid starvation.
2550 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2552 struct ceph_osd_request *req;
2553 u64 last_tid, next_tid = 0;
2555 mutex_lock(&osdc->request_mutex);
2556 last_tid = osdc->last_tid;
2558 req = __lookup_request_ge(osdc, next_tid);
2561 if (req->r_tid > last_tid)
2564 next_tid = req->r_tid + 1;
2565 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2568 ceph_osdc_get_request(req);
2569 mutex_unlock(&osdc->request_mutex);
2570 dout("sync waiting on tid %llu (last is %llu)\n",
2571 req->r_tid, last_tid);
2572 wait_for_completion(&req->r_safe_completion);
2573 mutex_lock(&osdc->request_mutex);
2574 ceph_osdc_put_request(req);
2576 mutex_unlock(&osdc->request_mutex);
2577 dout("sync done (thru tid %llu)\n", last_tid);
2579 EXPORT_SYMBOL(ceph_osdc_sync);
2582 * Call all pending notify callbacks - for use after a watch is
2583 * unregistered, to make sure no more callbacks for it will be invoked
2585 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2587 flush_workqueue(osdc->notify_wq);
2589 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2595 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2600 osdc->client = client;
2601 osdc->osdmap = NULL;
2602 init_rwsem(&osdc->map_sem);
2603 init_completion(&osdc->map_waiters);
2604 osdc->last_requested_map = 0;
2605 mutex_init(&osdc->request_mutex);
2607 osdc->osds = RB_ROOT;
2608 INIT_LIST_HEAD(&osdc->osd_lru);
2609 osdc->requests = RB_ROOT;
2610 INIT_LIST_HEAD(&osdc->req_lru);
2611 INIT_LIST_HEAD(&osdc->req_unsent);
2612 INIT_LIST_HEAD(&osdc->req_notarget);
2613 INIT_LIST_HEAD(&osdc->req_linger);
2614 osdc->num_requests = 0;
2615 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2616 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2617 spin_lock_init(&osdc->event_lock);
2618 osdc->event_tree = RB_ROOT;
2619 osdc->event_count = 0;
2621 schedule_delayed_work(&osdc->osds_timeout_work,
2622 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2625 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2626 sizeof(struct ceph_osd_request));
2627 if (!osdc->req_mempool)
2630 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2631 OSD_OP_FRONT_LEN, 10, true,
2635 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2636 OSD_OPREPLY_FRONT_LEN, 10, true,
2642 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2643 if (!osdc->notify_wq)
2644 goto out_msgpool_reply;
2649 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2651 ceph_msgpool_destroy(&osdc->msgpool_op);
2653 mempool_destroy(osdc->req_mempool);
2658 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2660 flush_workqueue(osdc->notify_wq);
2661 destroy_workqueue(osdc->notify_wq);
2662 cancel_delayed_work_sync(&osdc->timeout_work);
2663 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2665 ceph_osdmap_destroy(osdc->osdmap);
2666 osdc->osdmap = NULL;
2668 remove_all_osds(osdc);
2669 mempool_destroy(osdc->req_mempool);
2670 ceph_msgpool_destroy(&osdc->msgpool_op);
2671 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2675 * Read some contiguous pages. If we cross a stripe boundary, shorten
2676 * *plen. Return number of bytes read, or error.
2678 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2679 struct ceph_vino vino, struct ceph_file_layout *layout,
2681 u32 truncate_seq, u64 truncate_size,
2682 struct page **pages, int num_pages, int page_align)
2684 struct ceph_osd_request *req;
2687 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2688 vino.snap, off, *plen);
2689 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
2690 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2691 NULL, truncate_seq, truncate_size,
2694 return PTR_ERR(req);
2696 /* it may be a short read due to an object boundary */
2698 osd_req_op_extent_osd_data_pages(req, 0,
2699 pages, *plen, page_align, false, false);
2701 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2702 off, *plen, *plen, page_align);
2704 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2706 rc = ceph_osdc_start_request(osdc, req, false);
2708 rc = ceph_osdc_wait_request(osdc, req);
2710 ceph_osdc_put_request(req);
2711 dout("readpages result %d\n", rc);
2714 EXPORT_SYMBOL(ceph_osdc_readpages);
2717 * do a synchronous write on N pages
2719 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2720 struct ceph_file_layout *layout,
2721 struct ceph_snap_context *snapc,
2723 u32 truncate_seq, u64 truncate_size,
2724 struct timespec *mtime,
2725 struct page **pages, int num_pages)
2727 struct ceph_osd_request *req;
2729 int page_align = off & ~PAGE_MASK;
2731 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2732 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
2734 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2735 snapc, truncate_seq, truncate_size,
2738 return PTR_ERR(req);
2740 /* it may be a short write due to an object boundary */
2741 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2743 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2745 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2747 rc = ceph_osdc_start_request(osdc, req, true);
2749 rc = ceph_osdc_wait_request(osdc, req);
2751 ceph_osdc_put_request(req);
2754 dout("writepages result %d\n", rc);
2757 EXPORT_SYMBOL(ceph_osdc_writepages);
2759 int ceph_osdc_setup(void)
2761 BUG_ON(ceph_osd_request_cache);
2762 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2763 sizeof (struct ceph_osd_request),
2764 __alignof__(struct ceph_osd_request),
2767 return ceph_osd_request_cache ? 0 : -ENOMEM;
2769 EXPORT_SYMBOL(ceph_osdc_setup);
2771 void ceph_osdc_cleanup(void)
2773 BUG_ON(!ceph_osd_request_cache);
2774 kmem_cache_destroy(ceph_osd_request_cache);
2775 ceph_osd_request_cache = NULL;
2777 EXPORT_SYMBOL(ceph_osdc_cleanup);
2780 * handle incoming message
2782 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2784 struct ceph_osd *osd = con->private;
2785 struct ceph_osd_client *osdc;
2786 int type = le16_to_cpu(msg->hdr.type);
2793 case CEPH_MSG_OSD_MAP:
2794 ceph_osdc_handle_map(osdc, msg);
2796 case CEPH_MSG_OSD_OPREPLY:
2797 handle_reply(osdc, msg, con);
2799 case CEPH_MSG_WATCH_NOTIFY:
2800 handle_watch_notify(osdc, msg);
2804 pr_err("received unknown message type %d %s\n", type,
2805 ceph_msg_type_name(type));
2812 * lookup and return message for incoming reply. set up reply message
2815 static struct ceph_msg *get_reply(struct ceph_connection *con,
2816 struct ceph_msg_header *hdr,
2819 struct ceph_osd *osd = con->private;
2820 struct ceph_osd_client *osdc = osd->o_osdc;
2822 struct ceph_osd_request *req;
2823 int front_len = le32_to_cpu(hdr->front_len);
2824 int data_len = le32_to_cpu(hdr->data_len);
2827 tid = le64_to_cpu(hdr->tid);
2828 mutex_lock(&osdc->request_mutex);
2829 req = __lookup_request(osdc, tid);
2833 dout("get_reply unknown tid %llu from osd%d\n", tid,
2838 if (req->r_reply->con)
2839 dout("%s revoking msg %p from old con %p\n", __func__,
2840 req->r_reply, req->r_reply->con);
2841 ceph_msg_revoke_incoming(req->r_reply);
2843 if (front_len > req->r_reply->front_alloc_len) {
2844 pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2845 front_len, req->r_reply->front_alloc_len,
2846 (unsigned int)con->peer_name.type,
2847 le64_to_cpu(con->peer_name.num));
2848 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2852 ceph_msg_put(req->r_reply);
2855 m = ceph_msg_get(req->r_reply);
2858 struct ceph_osd_data *osd_data;
2861 * XXX This is assuming there is only one op containing
2862 * XXX page data. Probably OK for reads, but this
2863 * XXX ought to be done more generally.
2865 osd_data = osd_req_op_extent_osd_data(req, 0);
2866 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2867 if (osd_data->pages &&
2868 unlikely(osd_data->length < data_len)) {
2870 pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
2871 tid, data_len, osd_data->length);
2880 dout("get_reply tid %lld %p\n", tid, m);
2883 mutex_unlock(&osdc->request_mutex);
2888 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2889 struct ceph_msg_header *hdr,
2892 struct ceph_osd *osd = con->private;
2893 int type = le16_to_cpu(hdr->type);
2894 int front = le32_to_cpu(hdr->front_len);
2898 case CEPH_MSG_OSD_MAP:
2899 case CEPH_MSG_WATCH_NOTIFY:
2900 return ceph_msg_new(type, front, GFP_NOFS, false);
2901 case CEPH_MSG_OSD_OPREPLY:
2902 return get_reply(con, hdr, skip);
2904 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2912 * Wrappers to refcount containing ceph_osd struct
2914 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2916 struct ceph_osd *osd = con->private;
2922 static void put_osd_con(struct ceph_connection *con)
2924 struct ceph_osd *osd = con->private;
2932 * Note: returned pointer is the address of a structure that's
2933 * managed separately. Caller must *not* attempt to free it.
2935 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2936 int *proto, int force_new)
2938 struct ceph_osd *o = con->private;
2939 struct ceph_osd_client *osdc = o->o_osdc;
2940 struct ceph_auth_client *ac = osdc->client->monc.auth;
2941 struct ceph_auth_handshake *auth = &o->o_auth;
2943 if (force_new && auth->authorizer) {
2944 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2945 auth->authorizer = NULL;
2947 if (!auth->authorizer) {
2948 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2951 return ERR_PTR(ret);
2953 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2956 return ERR_PTR(ret);
2958 *proto = ac->protocol;
2964 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2966 struct ceph_osd *o = con->private;
2967 struct ceph_osd_client *osdc = o->o_osdc;
2968 struct ceph_auth_client *ac = osdc->client->monc.auth;
2970 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2973 static int invalidate_authorizer(struct ceph_connection *con)
2975 struct ceph_osd *o = con->private;
2976 struct ceph_osd_client *osdc = o->o_osdc;
2977 struct ceph_auth_client *ac = osdc->client->monc.auth;
2979 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2980 return ceph_monc_validate_auth(&osdc->client->monc);
2983 static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
2985 struct ceph_osd *o = con->private;
2986 struct ceph_auth_handshake *auth = &o->o_auth;
2987 return ceph_auth_sign_message(auth, msg);
2990 static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
2992 struct ceph_osd *o = con->private;
2993 struct ceph_auth_handshake *auth = &o->o_auth;
2994 return ceph_auth_check_message_signature(auth, msg);
2997 static const struct ceph_connection_operations osd_con_ops = {
3000 .dispatch = dispatch,
3001 .get_authorizer = get_authorizer,
3002 .verify_authorizer_reply = verify_authorizer_reply,
3003 .invalidate_authorizer = invalidate_authorizer,
3004 .alloc_msg = alloc_msg,
3005 .sign_message = sign_message,
3006 .check_message_signature = check_message_signature,