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 const struct ceph_connection_operations osd_con_ops;
27 static void __send_queued(struct ceph_osd_client *osdc);
28 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
29 static void __register_request(struct ceph_osd_client *osdc,
30 struct ceph_osd_request *req);
31 static void __unregister_linger_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
33 static void __send_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
37 * Implement client access to distributed object storage cluster.
39 * All data objects are stored within a cluster/cloud of OSDs, or
40 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
41 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
42 * remote daemons serving up and coordinating consistent and safe
45 * Cluster membership and the mapping of data objects onto storage devices
46 * are described by the osd map.
48 * We keep track of pending OSD requests (read, write), resubmit
49 * requests to different OSDs when the cluster topology/data layout
50 * change, or retry the affected requests when the communications
51 * channel with an OSD is reset.
55 * calculate the mapping of a file extent onto an object, and fill out the
56 * request accordingly. shorten extent as necessary if it crosses an
59 * fill osd op in request message.
61 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
62 u64 *objnum, u64 *objoff, u64 *objlen)
68 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
72 if (*objlen < orig_len) {
74 dout(" skipping last %llu, final file extent %llu~%llu\n",
75 orig_len - *plen, off, *plen);
78 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
83 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
85 memset(osd_data, 0, sizeof (*osd_data));
86 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
89 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
90 struct page **pages, u64 length, u32 alignment,
91 bool pages_from_pool, bool own_pages)
93 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
94 osd_data->pages = pages;
95 osd_data->length = length;
96 osd_data->alignment = alignment;
97 osd_data->pages_from_pool = pages_from_pool;
98 osd_data->own_pages = own_pages;
101 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
102 struct ceph_pagelist *pagelist)
104 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
105 osd_data->pagelist = pagelist;
109 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
110 struct bio *bio, size_t bio_length)
112 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
114 osd_data->bio_length = bio_length;
116 #endif /* CONFIG_BLOCK */
118 struct ceph_osd_data *
119 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
120 unsigned int which, bool write_request)
122 BUG_ON(which >= osd_req->r_num_ops);
124 return &osd_req->r_ops[which].extent.osd_data;
126 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
128 struct ceph_osd_data *
129 osd_req_op_cls_request_info(struct ceph_osd_request *osd_req,
132 BUG_ON(which >= osd_req->r_num_ops);
134 return &osd_req->r_ops[which].cls.request_info;
136 EXPORT_SYMBOL(osd_req_op_cls_request_info); /* ??? */
138 struct ceph_osd_data *
139 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
142 BUG_ON(which >= osd_req->r_num_ops);
144 return &osd_req->r_ops[which].cls.response_data;
146 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
148 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
149 unsigned int which, bool write_request,
150 struct page **pages, u64 length, u32 alignment,
151 bool pages_from_pool, bool own_pages)
153 struct ceph_osd_data *osd_data;
155 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request);
156 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
157 pages_from_pool, own_pages);
159 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
161 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
162 unsigned int which, bool write_request,
163 struct ceph_pagelist *pagelist)
165 struct ceph_osd_data *osd_data;
167 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request);
168 ceph_osd_data_pagelist_init(osd_data, pagelist);
170 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
173 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
174 unsigned int which, bool write_request,
175 struct bio *bio, size_t bio_length)
177 struct ceph_osd_data *osd_data;
179 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request);
180 ceph_osd_data_bio_init(osd_data, bio, bio_length);
182 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
183 #endif /* CONFIG_BLOCK */
185 static void osd_req_op_cls_request_info_pagelist(
186 struct ceph_osd_request *osd_req,
187 unsigned int which, struct ceph_pagelist *pagelist)
189 struct ceph_osd_data *osd_data;
191 osd_data = osd_req_op_cls_request_info(osd_req, which);
192 ceph_osd_data_pagelist_init(osd_data, pagelist);
195 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
196 unsigned int which, struct page **pages, u64 length,
197 u32 alignment, bool pages_from_pool, bool own_pages)
199 struct ceph_osd_data *osd_data;
201 osd_data = osd_req_op_cls_response_data(osd_req, which);
202 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
203 pages_from_pool, own_pages);
205 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
207 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
209 switch (osd_data->type) {
210 case CEPH_OSD_DATA_TYPE_NONE:
212 case CEPH_OSD_DATA_TYPE_PAGES:
213 return osd_data->length;
214 case CEPH_OSD_DATA_TYPE_PAGELIST:
215 return (u64)osd_data->pagelist->length;
217 case CEPH_OSD_DATA_TYPE_BIO:
218 return (u64)osd_data->bio_length;
219 #endif /* CONFIG_BLOCK */
221 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
227 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
229 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
232 num_pages = calc_pages_for((u64)osd_data->alignment,
233 (u64)osd_data->length);
234 ceph_release_page_vector(osd_data->pages, num_pages);
236 ceph_osd_data_init(osd_data);
239 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
242 struct ceph_osd_req_op *op;
244 BUG_ON(which >= osd_req->r_num_ops);
245 op = &osd_req->r_ops[which];
248 case CEPH_OSD_OP_READ:
249 case CEPH_OSD_OP_WRITE:
250 ceph_osd_data_release(&op->extent.osd_data);
252 case CEPH_OSD_OP_CALL:
253 ceph_osd_data_release(&op->cls.request_info);
254 ceph_osd_data_release(&op->cls.response_data);
264 void ceph_osdc_release_request(struct kref *kref)
266 struct ceph_osd_request *req;
269 req = container_of(kref, struct ceph_osd_request, r_kref);
271 ceph_msg_put(req->r_request);
273 ceph_msg_revoke_incoming(req->r_reply);
274 ceph_msg_put(req->r_reply);
277 for (which = 0; which < req->r_num_ops; which++)
278 osd_req_op_data_release(req, which);
280 ceph_put_snap_context(req->r_snapc);
282 mempool_free(req, req->r_osdc->req_mempool);
286 EXPORT_SYMBOL(ceph_osdc_release_request);
288 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
289 struct ceph_snap_context *snapc,
290 unsigned int num_ops,
294 struct ceph_osd_request *req;
295 struct ceph_msg *msg;
298 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
299 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
301 msg_size = 4 + 4 + 8 + 8 + 4+8;
302 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
303 msg_size += 1 + 8 + 4 + 4; /* pg_t */
304 msg_size += 4 + MAX_OBJ_NAME_SIZE;
305 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
306 msg_size += 8; /* snapid */
307 msg_size += 8; /* snap_seq */
308 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
312 req = mempool_alloc(osdc->req_mempool, gfp_flags);
313 memset(req, 0, sizeof(*req));
315 req = kzalloc(sizeof(*req), gfp_flags);
321 req->r_mempool = use_mempool;
322 req->r_num_ops = num_ops;
324 kref_init(&req->r_kref);
325 init_completion(&req->r_completion);
326 init_completion(&req->r_safe_completion);
327 RB_CLEAR_NODE(&req->r_node);
328 INIT_LIST_HEAD(&req->r_unsafe_item);
329 INIT_LIST_HEAD(&req->r_linger_item);
330 INIT_LIST_HEAD(&req->r_linger_osd);
331 INIT_LIST_HEAD(&req->r_req_lru_item);
332 INIT_LIST_HEAD(&req->r_osd_item);
334 /* create reply message */
336 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
338 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
339 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
341 ceph_osdc_put_request(req);
346 /* create request message; allow space for oid */
348 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
350 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
352 ceph_osdc_put_request(req);
356 memset(msg->front.iov_base, 0, msg->front.iov_len);
358 req->r_request = msg;
362 EXPORT_SYMBOL(ceph_osdc_alloc_request);
364 static bool osd_req_opcode_valid(u16 opcode)
367 case CEPH_OSD_OP_READ:
368 case CEPH_OSD_OP_STAT:
369 case CEPH_OSD_OP_MAPEXT:
370 case CEPH_OSD_OP_MASKTRUNC:
371 case CEPH_OSD_OP_SPARSE_READ:
372 case CEPH_OSD_OP_NOTIFY:
373 case CEPH_OSD_OP_NOTIFY_ACK:
374 case CEPH_OSD_OP_ASSERT_VER:
375 case CEPH_OSD_OP_WRITE:
376 case CEPH_OSD_OP_WRITEFULL:
377 case CEPH_OSD_OP_TRUNCATE:
378 case CEPH_OSD_OP_ZERO:
379 case CEPH_OSD_OP_DELETE:
380 case CEPH_OSD_OP_APPEND:
381 case CEPH_OSD_OP_STARTSYNC:
382 case CEPH_OSD_OP_SETTRUNC:
383 case CEPH_OSD_OP_TRIMTRUNC:
384 case CEPH_OSD_OP_TMAPUP:
385 case CEPH_OSD_OP_TMAPPUT:
386 case CEPH_OSD_OP_TMAPGET:
387 case CEPH_OSD_OP_CREATE:
388 case CEPH_OSD_OP_ROLLBACK:
389 case CEPH_OSD_OP_WATCH:
390 case CEPH_OSD_OP_OMAPGETKEYS:
391 case CEPH_OSD_OP_OMAPGETVALS:
392 case CEPH_OSD_OP_OMAPGETHEADER:
393 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
394 case CEPH_OSD_OP_OMAPSETVALS:
395 case CEPH_OSD_OP_OMAPSETHEADER:
396 case CEPH_OSD_OP_OMAPCLEAR:
397 case CEPH_OSD_OP_OMAPRMKEYS:
398 case CEPH_OSD_OP_OMAP_CMP:
399 case CEPH_OSD_OP_CLONERANGE:
400 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
401 case CEPH_OSD_OP_SRC_CMPXATTR:
402 case CEPH_OSD_OP_GETXATTR:
403 case CEPH_OSD_OP_GETXATTRS:
404 case CEPH_OSD_OP_CMPXATTR:
405 case CEPH_OSD_OP_SETXATTR:
406 case CEPH_OSD_OP_SETXATTRS:
407 case CEPH_OSD_OP_RESETXATTRS:
408 case CEPH_OSD_OP_RMXATTR:
409 case CEPH_OSD_OP_PULL:
410 case CEPH_OSD_OP_PUSH:
411 case CEPH_OSD_OP_BALANCEREADS:
412 case CEPH_OSD_OP_UNBALANCEREADS:
413 case CEPH_OSD_OP_SCRUB:
414 case CEPH_OSD_OP_SCRUB_RESERVE:
415 case CEPH_OSD_OP_SCRUB_UNRESERVE:
416 case CEPH_OSD_OP_SCRUB_STOP:
417 case CEPH_OSD_OP_SCRUB_MAP:
418 case CEPH_OSD_OP_WRLOCK:
419 case CEPH_OSD_OP_WRUNLOCK:
420 case CEPH_OSD_OP_RDLOCK:
421 case CEPH_OSD_OP_RDUNLOCK:
422 case CEPH_OSD_OP_UPLOCK:
423 case CEPH_OSD_OP_DNLOCK:
424 case CEPH_OSD_OP_CALL:
425 case CEPH_OSD_OP_PGLS:
426 case CEPH_OSD_OP_PGLS_FILTER:
434 * This is an osd op init function for opcodes that have no data or
435 * other information associated with them. It also serves as a
436 * common init routine for all the other init functions, below.
438 static struct ceph_osd_req_op *
439 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
442 struct ceph_osd_req_op *op;
444 BUG_ON(which >= osd_req->r_num_ops);
445 BUG_ON(!osd_req_opcode_valid(opcode));
447 op = &osd_req->r_ops[which];
448 memset(op, 0, sizeof (*op));
454 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
455 unsigned int which, u16 opcode,
456 u64 offset, u64 length,
457 u64 truncate_size, u32 truncate_seq)
459 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
460 size_t payload_len = 0;
462 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
464 op->extent.offset = offset;
465 op->extent.length = length;
466 op->extent.truncate_size = truncate_size;
467 op->extent.truncate_seq = truncate_seq;
468 if (opcode == CEPH_OSD_OP_WRITE)
469 payload_len += length;
471 op->payload_len = payload_len;
473 EXPORT_SYMBOL(osd_req_op_extent_init);
475 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
476 unsigned int which, u64 length)
478 struct ceph_osd_req_op *op;
481 BUG_ON(which >= osd_req->r_num_ops);
482 op = &osd_req->r_ops[which];
483 previous = op->extent.length;
485 if (length == previous)
486 return; /* Nothing to do */
487 BUG_ON(length > previous);
489 op->extent.length = length;
490 op->payload_len -= previous - length;
492 EXPORT_SYMBOL(osd_req_op_extent_update);
494 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
495 u16 opcode, const char *class, const char *method,
496 const void *request_data, size_t request_data_size)
498 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
499 struct ceph_pagelist *pagelist;
500 size_t payload_len = 0;
503 BUG_ON(opcode != CEPH_OSD_OP_CALL);
505 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
507 ceph_pagelist_init(pagelist);
509 op->cls.class_name = class;
510 size = strlen(class);
511 BUG_ON(size > (size_t) U8_MAX);
512 op->cls.class_len = size;
513 ceph_pagelist_append(pagelist, class, size);
516 op->cls.method_name = method;
517 size = strlen(method);
518 BUG_ON(size > (size_t) U8_MAX);
519 op->cls.method_len = size;
520 ceph_pagelist_append(pagelist, method, size);
523 op->cls.request_data = request_data;
524 BUG_ON(request_data_size > (size_t) U32_MAX);
525 op->cls.request_data_len = (u32) request_data_size;
526 ceph_pagelist_append(pagelist, request_data, request_data_size);
527 payload_len += request_data_size;
529 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
531 op->cls.argc = 0; /* currently unused */
533 op->payload_len = payload_len;
535 EXPORT_SYMBOL(osd_req_op_cls_init);
537 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
538 unsigned int which, u16 opcode,
539 u64 cookie, u64 version, int flag)
541 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
543 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
545 op->watch.cookie = cookie;
546 /* op->watch.ver = version; */ /* XXX 3847 */
547 op->watch.ver = cpu_to_le64(version);
548 if (opcode == CEPH_OSD_OP_WATCH && flag)
549 op->watch.flag = (u8)1;
551 EXPORT_SYMBOL(osd_req_op_watch_init);
553 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
554 struct ceph_osd_data *osd_data)
556 u64 length = ceph_osd_data_length(osd_data);
558 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
559 BUG_ON(length > (u64) SIZE_MAX);
561 ceph_msg_data_add_pages(msg, osd_data->pages,
562 length, osd_data->alignment);
563 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
565 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
567 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
568 ceph_msg_data_add_bio(msg, osd_data->bio, length);
571 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
575 static u64 osd_req_encode_op(struct ceph_osd_request *req,
576 struct ceph_osd_op *dst, unsigned int which)
578 struct ceph_osd_req_op *src;
579 u64 request_data_len = 0;
581 BUG_ON(which >= req->r_num_ops);
582 src = &req->r_ops[which];
583 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
584 pr_err("unrecognized osd opcode %d\n", src->op);
590 case CEPH_OSD_OP_STAT:
592 case CEPH_OSD_OP_READ:
593 case CEPH_OSD_OP_WRITE:
594 if (src->op == CEPH_OSD_OP_WRITE)
595 request_data_len = src->extent.length;
596 dst->extent.offset = cpu_to_le64(src->extent.offset);
597 dst->extent.length = cpu_to_le64(src->extent.length);
598 dst->extent.truncate_size =
599 cpu_to_le64(src->extent.truncate_size);
600 dst->extent.truncate_seq =
601 cpu_to_le32(src->extent.truncate_seq);
602 if (src->op == CEPH_OSD_OP_WRITE)
603 ceph_osdc_msg_data_add(req->r_request,
604 &src->extent.osd_data);
606 ceph_osdc_msg_data_add(req->r_reply,
607 &src->extent.osd_data);
609 case CEPH_OSD_OP_CALL:
610 dst->cls.class_len = src->cls.class_len;
611 dst->cls.method_len = src->cls.method_len;
612 dst->cls.indata_len = cpu_to_le32(src->cls.request_data_len);
613 ceph_osdc_msg_data_add(req->r_reply, &src->cls.response_data);
614 ceph_osdc_msg_data_add(req->r_request, &src->cls.request_info);
615 BUG_ON(src->cls.request_info.type !=
616 CEPH_OSD_DATA_TYPE_PAGELIST);
617 request_data_len = src->cls.request_info.pagelist->length;
619 case CEPH_OSD_OP_STARTSYNC:
621 case CEPH_OSD_OP_NOTIFY_ACK:
622 case CEPH_OSD_OP_WATCH:
623 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
624 dst->watch.ver = cpu_to_le64(src->watch.ver);
625 dst->watch.flag = src->watch.flag;
628 pr_err("unsupported osd opcode %s\n",
629 ceph_osd_op_name(src->op));
634 dst->op = cpu_to_le16(src->op);
635 dst->payload_len = cpu_to_le32(src->payload_len);
637 return request_data_len;
641 * build new request AND message, calculate layout, and adjust file
644 * if the file was recently truncated, we include information about its
645 * old and new size so that the object can be updated appropriately. (we
646 * avoid synchronously deleting truncated objects because it's slow.)
648 * if @do_sync, include a 'startsync' command so that the osd will flush
651 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
652 struct ceph_file_layout *layout,
653 struct ceph_vino vino,
654 u64 off, u64 *plen, int num_ops,
655 int opcode, int flags,
656 struct ceph_snap_context *snapc,
661 struct ceph_osd_request *req;
669 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
671 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
674 return ERR_PTR(-ENOMEM);
676 req->r_flags = flags;
678 /* calculate max write size */
679 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
681 ceph_osdc_put_request(req);
685 object_size = le32_to_cpu(layout->fl_object_size);
686 object_base = off - objoff;
687 if (truncate_size <= object_base) {
690 truncate_size -= object_base;
691 if (truncate_size > object_size)
692 truncate_size = object_size;
695 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
696 truncate_size, truncate_seq);
699 * A second op in the ops array means the caller wants to
700 * also issue a include a 'startsync' command so that the
701 * osd will flush data quickly.
704 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
706 req->r_file_layout = *layout; /* keep a copy */
708 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
710 req->r_oid_len = strlen(req->r_oid);
714 EXPORT_SYMBOL(ceph_osdc_new_request);
717 * We keep osd requests in an rbtree, sorted by ->r_tid.
719 static void __insert_request(struct ceph_osd_client *osdc,
720 struct ceph_osd_request *new)
722 struct rb_node **p = &osdc->requests.rb_node;
723 struct rb_node *parent = NULL;
724 struct ceph_osd_request *req = NULL;
728 req = rb_entry(parent, struct ceph_osd_request, r_node);
729 if (new->r_tid < req->r_tid)
731 else if (new->r_tid > req->r_tid)
737 rb_link_node(&new->r_node, parent, p);
738 rb_insert_color(&new->r_node, &osdc->requests);
741 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
744 struct ceph_osd_request *req;
745 struct rb_node *n = osdc->requests.rb_node;
748 req = rb_entry(n, struct ceph_osd_request, r_node);
749 if (tid < req->r_tid)
751 else if (tid > req->r_tid)
759 static struct ceph_osd_request *
760 __lookup_request_ge(struct ceph_osd_client *osdc,
763 struct ceph_osd_request *req;
764 struct rb_node *n = osdc->requests.rb_node;
767 req = rb_entry(n, struct ceph_osd_request, r_node);
768 if (tid < req->r_tid) {
772 } else if (tid > req->r_tid) {
782 * Resubmit requests pending on the given osd.
784 static void __kick_osd_requests(struct ceph_osd_client *osdc,
785 struct ceph_osd *osd)
787 struct ceph_osd_request *req, *nreq;
791 dout("__kick_osd_requests osd%d\n", osd->o_osd);
792 err = __reset_osd(osdc, osd);
796 * Build up a list of requests to resend by traversing the
797 * osd's list of requests. Requests for a given object are
798 * sent in tid order, and that is also the order they're
799 * kept on this list. Therefore all requests that are in
800 * flight will be found first, followed by all requests that
801 * have not yet been sent. And to resend requests while
802 * preserving this order we will want to put any sent
803 * requests back on the front of the osd client's unsent
806 * So we build a separate ordered list of already-sent
807 * requests for the affected osd and splice it onto the
808 * front of the osd client's unsent list. Once we've seen a
809 * request that has not yet been sent we're done. Those
810 * requests are already sitting right where they belong.
812 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
815 list_move_tail(&req->r_req_lru_item, &resend);
816 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
819 req->r_flags |= CEPH_OSD_FLAG_RETRY;
821 list_splice(&resend, &osdc->req_unsent);
824 * Linger requests are re-registered before sending, which
825 * sets up a new tid for each. We add them to the unsent
826 * list at the end to keep things in tid order.
828 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
831 * reregister request prior to unregistering linger so
832 * that r_osd is preserved.
834 BUG_ON(!list_empty(&req->r_req_lru_item));
835 __register_request(osdc, req);
836 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
837 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
838 __unregister_linger_request(osdc, req);
839 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
845 * If the osd connection drops, we need to resubmit all requests.
847 static void osd_reset(struct ceph_connection *con)
849 struct ceph_osd *osd = con->private;
850 struct ceph_osd_client *osdc;
854 dout("osd_reset osd%d\n", osd->o_osd);
856 down_read(&osdc->map_sem);
857 mutex_lock(&osdc->request_mutex);
858 __kick_osd_requests(osdc, osd);
860 mutex_unlock(&osdc->request_mutex);
861 up_read(&osdc->map_sem);
865 * Track open sessions with osds.
867 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
869 struct ceph_osd *osd;
871 osd = kzalloc(sizeof(*osd), GFP_NOFS);
875 atomic_set(&osd->o_ref, 1);
878 RB_CLEAR_NODE(&osd->o_node);
879 INIT_LIST_HEAD(&osd->o_requests);
880 INIT_LIST_HEAD(&osd->o_linger_requests);
881 INIT_LIST_HEAD(&osd->o_osd_lru);
882 osd->o_incarnation = 1;
884 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
886 INIT_LIST_HEAD(&osd->o_keepalive_item);
890 static struct ceph_osd *get_osd(struct ceph_osd *osd)
892 if (atomic_inc_not_zero(&osd->o_ref)) {
893 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
894 atomic_read(&osd->o_ref));
897 dout("get_osd %p FAIL\n", osd);
902 static void put_osd(struct ceph_osd *osd)
904 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
905 atomic_read(&osd->o_ref) - 1);
906 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
907 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
909 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
915 * remove an osd from our map
917 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
919 dout("__remove_osd %p\n", osd);
920 BUG_ON(!list_empty(&osd->o_requests));
921 rb_erase(&osd->o_node, &osdc->osds);
922 list_del_init(&osd->o_osd_lru);
923 ceph_con_close(&osd->o_con);
927 static void remove_all_osds(struct ceph_osd_client *osdc)
929 dout("%s %p\n", __func__, osdc);
930 mutex_lock(&osdc->request_mutex);
931 while (!RB_EMPTY_ROOT(&osdc->osds)) {
932 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
933 struct ceph_osd, o_node);
934 __remove_osd(osdc, osd);
936 mutex_unlock(&osdc->request_mutex);
939 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
940 struct ceph_osd *osd)
942 dout("__move_osd_to_lru %p\n", osd);
943 BUG_ON(!list_empty(&osd->o_osd_lru));
944 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
945 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
948 static void __remove_osd_from_lru(struct ceph_osd *osd)
950 dout("__remove_osd_from_lru %p\n", osd);
951 if (!list_empty(&osd->o_osd_lru))
952 list_del_init(&osd->o_osd_lru);
955 static void remove_old_osds(struct ceph_osd_client *osdc)
957 struct ceph_osd *osd, *nosd;
959 dout("__remove_old_osds %p\n", osdc);
960 mutex_lock(&osdc->request_mutex);
961 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
962 if (time_before(jiffies, osd->lru_ttl))
964 __remove_osd(osdc, osd);
966 mutex_unlock(&osdc->request_mutex);
972 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
974 struct ceph_entity_addr *peer_addr;
976 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
977 if (list_empty(&osd->o_requests) &&
978 list_empty(&osd->o_linger_requests)) {
979 __remove_osd(osdc, osd);
984 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
985 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
986 !ceph_con_opened(&osd->o_con)) {
987 struct ceph_osd_request *req;
989 dout(" osd addr hasn't changed and connection never opened,"
990 " letting msgr retry");
991 /* touch each r_stamp for handle_timeout()'s benfit */
992 list_for_each_entry(req, &osd->o_requests, r_osd_item)
993 req->r_stamp = jiffies;
998 ceph_con_close(&osd->o_con);
999 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1000 osd->o_incarnation++;
1005 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1007 struct rb_node **p = &osdc->osds.rb_node;
1008 struct rb_node *parent = NULL;
1009 struct ceph_osd *osd = NULL;
1011 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1014 osd = rb_entry(parent, struct ceph_osd, o_node);
1015 if (new->o_osd < osd->o_osd)
1017 else if (new->o_osd > osd->o_osd)
1018 p = &(*p)->rb_right;
1023 rb_link_node(&new->o_node, parent, p);
1024 rb_insert_color(&new->o_node, &osdc->osds);
1027 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1029 struct ceph_osd *osd;
1030 struct rb_node *n = osdc->osds.rb_node;
1033 osd = rb_entry(n, struct ceph_osd, o_node);
1036 else if (o > osd->o_osd)
1044 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1046 schedule_delayed_work(&osdc->timeout_work,
1047 osdc->client->options->osd_keepalive_timeout * HZ);
1050 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1052 cancel_delayed_work(&osdc->timeout_work);
1056 * Register request, assign tid. If this is the first request, set up
1057 * the timeout event.
1059 static void __register_request(struct ceph_osd_client *osdc,
1060 struct ceph_osd_request *req)
1062 req->r_tid = ++osdc->last_tid;
1063 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1064 dout("__register_request %p tid %lld\n", req, req->r_tid);
1065 __insert_request(osdc, req);
1066 ceph_osdc_get_request(req);
1067 osdc->num_requests++;
1068 if (osdc->num_requests == 1) {
1069 dout(" first request, scheduling timeout\n");
1070 __schedule_osd_timeout(osdc);
1075 * called under osdc->request_mutex
1077 static void __unregister_request(struct ceph_osd_client *osdc,
1078 struct ceph_osd_request *req)
1080 if (RB_EMPTY_NODE(&req->r_node)) {
1081 dout("__unregister_request %p tid %lld not registered\n",
1086 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1087 rb_erase(&req->r_node, &osdc->requests);
1088 osdc->num_requests--;
1091 /* make sure the original request isn't in flight. */
1092 ceph_msg_revoke(req->r_request);
1094 list_del_init(&req->r_osd_item);
1095 if (list_empty(&req->r_osd->o_requests) &&
1096 list_empty(&req->r_osd->o_linger_requests)) {
1097 dout("moving osd to %p lru\n", req->r_osd);
1098 __move_osd_to_lru(osdc, req->r_osd);
1100 if (list_empty(&req->r_linger_item))
1104 list_del_init(&req->r_req_lru_item);
1105 ceph_osdc_put_request(req);
1107 if (osdc->num_requests == 0) {
1108 dout(" no requests, canceling timeout\n");
1109 __cancel_osd_timeout(osdc);
1114 * Cancel a previously queued request message
1116 static void __cancel_request(struct ceph_osd_request *req)
1118 if (req->r_sent && req->r_osd) {
1119 ceph_msg_revoke(req->r_request);
1124 static void __register_linger_request(struct ceph_osd_client *osdc,
1125 struct ceph_osd_request *req)
1127 dout("__register_linger_request %p\n", req);
1128 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1130 list_add_tail(&req->r_linger_osd,
1131 &req->r_osd->o_linger_requests);
1134 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1135 struct ceph_osd_request *req)
1137 dout("__unregister_linger_request %p\n", req);
1138 list_del_init(&req->r_linger_item);
1140 list_del_init(&req->r_linger_osd);
1142 if (list_empty(&req->r_osd->o_requests) &&
1143 list_empty(&req->r_osd->o_linger_requests)) {
1144 dout("moving osd to %p lru\n", req->r_osd);
1145 __move_osd_to_lru(osdc, req->r_osd);
1147 if (list_empty(&req->r_osd_item))
1152 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1153 struct ceph_osd_request *req)
1155 mutex_lock(&osdc->request_mutex);
1156 if (req->r_linger) {
1157 __unregister_linger_request(osdc, req);
1158 ceph_osdc_put_request(req);
1160 mutex_unlock(&osdc->request_mutex);
1162 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1164 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1165 struct ceph_osd_request *req)
1167 if (!req->r_linger) {
1168 dout("set_request_linger %p\n", req);
1171 * caller is now responsible for calling
1172 * unregister_linger_request
1174 ceph_osdc_get_request(req);
1177 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1180 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1181 * (as needed), and set the request r_osd appropriately. If there is
1182 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1183 * (unsent, homeless) or leave on in-flight lru.
1185 * Return 0 if unchanged, 1 if changed, or negative on error.
1187 * Caller should hold map_sem for read and request_mutex.
1189 static int __map_request(struct ceph_osd_client *osdc,
1190 struct ceph_osd_request *req, int force_resend)
1192 struct ceph_pg pgid;
1193 int acting[CEPH_PG_MAX_SIZE];
1194 int o = -1, num = 0;
1197 dout("map_request %p tid %lld\n", req, req->r_tid);
1198 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1199 ceph_file_layout_pg_pool(req->r_file_layout));
1201 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1206 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1212 if ((!force_resend &&
1213 req->r_osd && req->r_osd->o_osd == o &&
1214 req->r_sent >= req->r_osd->o_incarnation &&
1215 req->r_num_pg_osds == num &&
1216 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1217 (req->r_osd == NULL && o == -1))
1218 return 0; /* no change */
1220 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1221 req->r_tid, pgid.pool, pgid.seed, o,
1222 req->r_osd ? req->r_osd->o_osd : -1);
1224 /* record full pg acting set */
1225 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1226 req->r_num_pg_osds = num;
1229 __cancel_request(req);
1230 list_del_init(&req->r_osd_item);
1234 req->r_osd = __lookup_osd(osdc, o);
1235 if (!req->r_osd && o >= 0) {
1237 req->r_osd = create_osd(osdc, o);
1239 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1243 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1244 __insert_osd(osdc, req->r_osd);
1246 ceph_con_open(&req->r_osd->o_con,
1247 CEPH_ENTITY_TYPE_OSD, o,
1248 &osdc->osdmap->osd_addr[o]);
1252 __remove_osd_from_lru(req->r_osd);
1253 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1254 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1256 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1258 err = 1; /* osd or pg changed */
1265 * caller should hold map_sem (for read) and request_mutex
1267 static void __send_request(struct ceph_osd_client *osdc,
1268 struct ceph_osd_request *req)
1272 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1273 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1274 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1276 /* fill in message content that changes each time we send it */
1277 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1278 put_unaligned_le32(req->r_flags, req->r_request_flags);
1279 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1280 p = req->r_request_pgid;
1281 ceph_encode_64(&p, req->r_pgid.pool);
1282 ceph_encode_32(&p, req->r_pgid.seed);
1283 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1284 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1285 sizeof(req->r_reassert_version));
1287 req->r_stamp = jiffies;
1288 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1290 ceph_msg_get(req->r_request); /* send consumes a ref */
1291 ceph_con_send(&req->r_osd->o_con, req->r_request);
1292 req->r_sent = req->r_osd->o_incarnation;
1296 * Send any requests in the queue (req_unsent).
1298 static void __send_queued(struct ceph_osd_client *osdc)
1300 struct ceph_osd_request *req, *tmp;
1302 dout("__send_queued\n");
1303 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1304 __send_request(osdc, req);
1308 * Timeout callback, called every N seconds when 1 or more osd
1309 * requests has been active for more than N seconds. When this
1310 * happens, we ping all OSDs with requests who have timed out to
1311 * ensure any communications channel reset is detected. Reset the
1312 * request timeouts another N seconds in the future as we go.
1313 * Reschedule the timeout event another N seconds in future (unless
1314 * there are no open requests).
1316 static void handle_timeout(struct work_struct *work)
1318 struct ceph_osd_client *osdc =
1319 container_of(work, struct ceph_osd_client, timeout_work.work);
1320 struct ceph_osd_request *req;
1321 struct ceph_osd *osd;
1322 unsigned long keepalive =
1323 osdc->client->options->osd_keepalive_timeout * HZ;
1324 struct list_head slow_osds;
1326 down_read(&osdc->map_sem);
1328 ceph_monc_request_next_osdmap(&osdc->client->monc);
1330 mutex_lock(&osdc->request_mutex);
1333 * ping osds that are a bit slow. this ensures that if there
1334 * is a break in the TCP connection we will notice, and reopen
1335 * a connection with that osd (from the fault callback).
1337 INIT_LIST_HEAD(&slow_osds);
1338 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1339 if (time_before(jiffies, req->r_stamp + keepalive))
1344 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1345 req->r_tid, osd->o_osd);
1346 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1348 while (!list_empty(&slow_osds)) {
1349 osd = list_entry(slow_osds.next, struct ceph_osd,
1351 list_del_init(&osd->o_keepalive_item);
1352 ceph_con_keepalive(&osd->o_con);
1355 __schedule_osd_timeout(osdc);
1356 __send_queued(osdc);
1357 mutex_unlock(&osdc->request_mutex);
1358 up_read(&osdc->map_sem);
1361 static void handle_osds_timeout(struct work_struct *work)
1363 struct ceph_osd_client *osdc =
1364 container_of(work, struct ceph_osd_client,
1365 osds_timeout_work.work);
1366 unsigned long delay =
1367 osdc->client->options->osd_idle_ttl * HZ >> 2;
1369 dout("osds timeout\n");
1370 down_read(&osdc->map_sem);
1371 remove_old_osds(osdc);
1372 up_read(&osdc->map_sem);
1374 schedule_delayed_work(&osdc->osds_timeout_work,
1375 round_jiffies_relative(delay));
1378 static void complete_request(struct ceph_osd_request *req)
1380 if (req->r_safe_callback)
1381 req->r_safe_callback(req, NULL);
1382 complete_all(&req->r_safe_completion); /* fsync waiter */
1386 * handle osd op reply. either call the callback if it is specified,
1387 * or do the completion to wake up the waiting thread.
1389 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1390 struct ceph_connection *con)
1393 struct ceph_osd_request *req;
1396 unsigned int numops;
1397 int payload_len, flags;
1403 u64 reassert_version;
1405 int already_completed;
1409 tid = le64_to_cpu(msg->hdr.tid);
1410 dout("handle_reply %p tid %llu\n", msg, tid);
1412 p = msg->front.iov_base;
1413 end = p + msg->front.iov_len;
1415 ceph_decode_need(&p, end, 4, bad);
1416 object_len = ceph_decode_32(&p);
1417 ceph_decode_need(&p, end, object_len, bad);
1420 err = ceph_decode_pgid(&p, end, &pg);
1424 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1425 flags = ceph_decode_64(&p);
1426 result = ceph_decode_32(&p);
1427 reassert_epoch = ceph_decode_32(&p);
1428 reassert_version = ceph_decode_64(&p);
1429 osdmap_epoch = ceph_decode_32(&p);
1432 mutex_lock(&osdc->request_mutex);
1433 req = __lookup_request(osdc, tid);
1435 dout("handle_reply tid %llu dne\n", tid);
1438 ceph_osdc_get_request(req);
1440 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1443 ceph_decode_need(&p, end, 4, bad);
1444 numops = ceph_decode_32(&p);
1445 if (numops > CEPH_OSD_MAX_OP)
1447 if (numops != req->r_num_ops)
1450 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1451 for (i = 0; i < numops; i++) {
1452 struct ceph_osd_op *op = p;
1455 len = le32_to_cpu(op->payload_len);
1456 req->r_reply_op_len[i] = len;
1457 dout(" op %d has %d bytes\n", i, len);
1461 bytes = le32_to_cpu(msg->hdr.data_len);
1462 if (payload_len != bytes) {
1463 pr_warning("sum of op payload lens %d != data_len %d",
1464 payload_len, bytes);
1468 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1469 retry_attempt = ceph_decode_32(&p);
1470 for (i = 0; i < numops; i++)
1471 req->r_reply_op_result[i] = ceph_decode_32(&p);
1473 if (!req->r_got_reply) {
1475 req->r_result = result;
1476 dout("handle_reply result %d bytes %d\n", req->r_result,
1478 if (req->r_result == 0)
1479 req->r_result = bytes;
1481 /* in case this is a write and we need to replay, */
1482 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1483 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1485 req->r_got_reply = 1;
1486 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1487 dout("handle_reply tid %llu dup ack\n", tid);
1488 mutex_unlock(&osdc->request_mutex);
1492 dout("handle_reply tid %llu flags %d\n", tid, flags);
1494 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1495 __register_linger_request(osdc, req);
1497 /* either this is a read, or we got the safe response */
1499 (flags & CEPH_OSD_FLAG_ONDISK) ||
1500 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1501 __unregister_request(osdc, req);
1503 already_completed = req->r_completed;
1504 req->r_completed = 1;
1505 mutex_unlock(&osdc->request_mutex);
1506 if (already_completed)
1509 if (req->r_callback)
1510 req->r_callback(req, msg);
1512 complete_all(&req->r_completion);
1514 if (flags & CEPH_OSD_FLAG_ONDISK)
1515 complete_request(req);
1518 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1519 ceph_osdc_put_request(req);
1523 ceph_osdc_put_request(req);
1525 mutex_unlock(&osdc->request_mutex);
1527 pr_err("corrupt osd_op_reply got %d %d\n",
1528 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1532 static void reset_changed_osds(struct ceph_osd_client *osdc)
1534 struct rb_node *p, *n;
1536 for (p = rb_first(&osdc->osds); p; p = n) {
1537 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1540 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1541 memcmp(&osd->o_con.peer_addr,
1542 ceph_osd_addr(osdc->osdmap,
1544 sizeof(struct ceph_entity_addr)) != 0)
1545 __reset_osd(osdc, osd);
1550 * Requeue requests whose mapping to an OSD has changed. If requests map to
1551 * no osd, request a new map.
1553 * Caller should hold map_sem for read.
1555 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1557 struct ceph_osd_request *req, *nreq;
1562 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1563 mutex_lock(&osdc->request_mutex);
1564 for (p = rb_first(&osdc->requests); p; ) {
1565 req = rb_entry(p, struct ceph_osd_request, r_node);
1569 * For linger requests that have not yet been
1570 * registered, move them to the linger list; they'll
1571 * be sent to the osd in the loop below. Unregister
1572 * the request before re-registering it as a linger
1573 * request to ensure the __map_request() below
1574 * will decide it needs to be sent.
1576 if (req->r_linger && list_empty(&req->r_linger_item)) {
1577 dout("%p tid %llu restart on osd%d\n",
1579 req->r_osd ? req->r_osd->o_osd : -1);
1580 __unregister_request(osdc, req);
1581 __register_linger_request(osdc, req);
1585 err = __map_request(osdc, req, force_resend);
1587 continue; /* error */
1588 if (req->r_osd == NULL) {
1589 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1590 needmap++; /* request a newer map */
1591 } else if (err > 0) {
1592 if (!req->r_linger) {
1593 dout("%p tid %llu requeued on osd%d\n", req,
1595 req->r_osd ? req->r_osd->o_osd : -1);
1596 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1601 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1603 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1605 err = __map_request(osdc, req, force_resend);
1606 dout("__map_request returned %d\n", err);
1608 continue; /* no change and no osd was specified */
1610 continue; /* hrm! */
1611 if (req->r_osd == NULL) {
1612 dout("tid %llu maps to no valid osd\n", req->r_tid);
1613 needmap++; /* request a newer map */
1617 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1618 req->r_osd ? req->r_osd->o_osd : -1);
1619 __register_request(osdc, req);
1620 __unregister_linger_request(osdc, req);
1622 mutex_unlock(&osdc->request_mutex);
1625 dout("%d requests for down osds, need new map\n", needmap);
1626 ceph_monc_request_next_osdmap(&osdc->client->monc);
1628 reset_changed_osds(osdc);
1633 * Process updated osd map.
1635 * The message contains any number of incremental and full maps, normally
1636 * indicating some sort of topology change in the cluster. Kick requests
1637 * off to different OSDs as needed.
1639 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1641 void *p, *end, *next;
1642 u32 nr_maps, maplen;
1644 struct ceph_osdmap *newmap = NULL, *oldmap;
1646 struct ceph_fsid fsid;
1648 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1649 p = msg->front.iov_base;
1650 end = p + msg->front.iov_len;
1653 ceph_decode_need(&p, end, sizeof(fsid), bad);
1654 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1655 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1658 down_write(&osdc->map_sem);
1660 /* incremental maps */
1661 ceph_decode_32_safe(&p, end, nr_maps, bad);
1662 dout(" %d inc maps\n", nr_maps);
1663 while (nr_maps > 0) {
1664 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1665 epoch = ceph_decode_32(&p);
1666 maplen = ceph_decode_32(&p);
1667 ceph_decode_need(&p, end, maplen, bad);
1669 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1670 dout("applying incremental map %u len %d\n",
1672 newmap = osdmap_apply_incremental(&p, next,
1674 &osdc->client->msgr);
1675 if (IS_ERR(newmap)) {
1676 err = PTR_ERR(newmap);
1680 if (newmap != osdc->osdmap) {
1681 ceph_osdmap_destroy(osdc->osdmap);
1682 osdc->osdmap = newmap;
1684 kick_requests(osdc, 0);
1686 dout("ignoring incremental map %u len %d\n",
1696 ceph_decode_32_safe(&p, end, nr_maps, bad);
1697 dout(" %d full maps\n", nr_maps);
1699 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1700 epoch = ceph_decode_32(&p);
1701 maplen = ceph_decode_32(&p);
1702 ceph_decode_need(&p, end, maplen, bad);
1704 dout("skipping non-latest full map %u len %d\n",
1706 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1707 dout("skipping full map %u len %d, "
1708 "older than our %u\n", epoch, maplen,
1709 osdc->osdmap->epoch);
1711 int skipped_map = 0;
1713 dout("taking full map %u len %d\n", epoch, maplen);
1714 newmap = osdmap_decode(&p, p+maplen);
1715 if (IS_ERR(newmap)) {
1716 err = PTR_ERR(newmap);
1720 oldmap = osdc->osdmap;
1721 osdc->osdmap = newmap;
1723 if (oldmap->epoch + 1 < newmap->epoch)
1725 ceph_osdmap_destroy(oldmap);
1727 kick_requests(osdc, skipped_map);
1734 downgrade_write(&osdc->map_sem);
1735 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1738 * subscribe to subsequent osdmap updates if full to ensure
1739 * we find out when we are no longer full and stop returning
1742 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1743 ceph_monc_request_next_osdmap(&osdc->client->monc);
1745 mutex_lock(&osdc->request_mutex);
1746 __send_queued(osdc);
1747 mutex_unlock(&osdc->request_mutex);
1748 up_read(&osdc->map_sem);
1749 wake_up_all(&osdc->client->auth_wq);
1753 pr_err("osdc handle_map corrupt msg\n");
1755 up_write(&osdc->map_sem);
1760 * watch/notify callback event infrastructure
1762 * These callbacks are used both for watch and notify operations.
1764 static void __release_event(struct kref *kref)
1766 struct ceph_osd_event *event =
1767 container_of(kref, struct ceph_osd_event, kref);
1769 dout("__release_event %p\n", event);
1773 static void get_event(struct ceph_osd_event *event)
1775 kref_get(&event->kref);
1778 void ceph_osdc_put_event(struct ceph_osd_event *event)
1780 kref_put(&event->kref, __release_event);
1782 EXPORT_SYMBOL(ceph_osdc_put_event);
1784 static void __insert_event(struct ceph_osd_client *osdc,
1785 struct ceph_osd_event *new)
1787 struct rb_node **p = &osdc->event_tree.rb_node;
1788 struct rb_node *parent = NULL;
1789 struct ceph_osd_event *event = NULL;
1793 event = rb_entry(parent, struct ceph_osd_event, node);
1794 if (new->cookie < event->cookie)
1796 else if (new->cookie > event->cookie)
1797 p = &(*p)->rb_right;
1802 rb_link_node(&new->node, parent, p);
1803 rb_insert_color(&new->node, &osdc->event_tree);
1806 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1809 struct rb_node **p = &osdc->event_tree.rb_node;
1810 struct rb_node *parent = NULL;
1811 struct ceph_osd_event *event = NULL;
1815 event = rb_entry(parent, struct ceph_osd_event, node);
1816 if (cookie < event->cookie)
1818 else if (cookie > event->cookie)
1819 p = &(*p)->rb_right;
1826 static void __remove_event(struct ceph_osd_event *event)
1828 struct ceph_osd_client *osdc = event->osdc;
1830 if (!RB_EMPTY_NODE(&event->node)) {
1831 dout("__remove_event removed %p\n", event);
1832 rb_erase(&event->node, &osdc->event_tree);
1833 ceph_osdc_put_event(event);
1835 dout("__remove_event didn't remove %p\n", event);
1839 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1840 void (*event_cb)(u64, u64, u8, void *),
1841 void *data, struct ceph_osd_event **pevent)
1843 struct ceph_osd_event *event;
1845 event = kmalloc(sizeof(*event), GFP_NOIO);
1849 dout("create_event %p\n", event);
1850 event->cb = event_cb;
1851 event->one_shot = 0;
1854 INIT_LIST_HEAD(&event->osd_node);
1855 RB_CLEAR_NODE(&event->node);
1856 kref_init(&event->kref); /* one ref for us */
1857 kref_get(&event->kref); /* one ref for the caller */
1859 spin_lock(&osdc->event_lock);
1860 event->cookie = ++osdc->event_count;
1861 __insert_event(osdc, event);
1862 spin_unlock(&osdc->event_lock);
1867 EXPORT_SYMBOL(ceph_osdc_create_event);
1869 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1871 struct ceph_osd_client *osdc = event->osdc;
1873 dout("cancel_event %p\n", event);
1874 spin_lock(&osdc->event_lock);
1875 __remove_event(event);
1876 spin_unlock(&osdc->event_lock);
1877 ceph_osdc_put_event(event); /* caller's */
1879 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1882 static void do_event_work(struct work_struct *work)
1884 struct ceph_osd_event_work *event_work =
1885 container_of(work, struct ceph_osd_event_work, work);
1886 struct ceph_osd_event *event = event_work->event;
1887 u64 ver = event_work->ver;
1888 u64 notify_id = event_work->notify_id;
1889 u8 opcode = event_work->opcode;
1891 dout("do_event_work completing %p\n", event);
1892 event->cb(ver, notify_id, opcode, event->data);
1893 dout("do_event_work completed %p\n", event);
1894 ceph_osdc_put_event(event);
1900 * Process osd watch notifications
1902 static void handle_watch_notify(struct ceph_osd_client *osdc,
1903 struct ceph_msg *msg)
1907 u64 cookie, ver, notify_id;
1909 struct ceph_osd_event *event;
1910 struct ceph_osd_event_work *event_work;
1912 p = msg->front.iov_base;
1913 end = p + msg->front.iov_len;
1915 ceph_decode_8_safe(&p, end, proto_ver, bad);
1916 ceph_decode_8_safe(&p, end, opcode, bad);
1917 ceph_decode_64_safe(&p, end, cookie, bad);
1918 ceph_decode_64_safe(&p, end, ver, bad);
1919 ceph_decode_64_safe(&p, end, notify_id, bad);
1921 spin_lock(&osdc->event_lock);
1922 event = __find_event(osdc, cookie);
1924 BUG_ON(event->one_shot);
1927 spin_unlock(&osdc->event_lock);
1928 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1929 cookie, ver, event);
1931 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1933 dout("ERROR: could not allocate event_work\n");
1936 INIT_WORK(&event_work->work, do_event_work);
1937 event_work->event = event;
1938 event_work->ver = ver;
1939 event_work->notify_id = notify_id;
1940 event_work->opcode = opcode;
1941 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1942 dout("WARNING: failed to queue notify event work\n");
1950 ceph_osdc_put_event(event);
1954 pr_err("osdc handle_watch_notify corrupt msg\n");
1959 * build new request AND message
1962 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
1963 struct ceph_snap_context *snapc, u64 snap_id,
1964 struct timespec *mtime)
1966 struct ceph_msg *msg = req->r_request;
1969 int flags = req->r_flags;
1973 req->r_snapid = snap_id;
1974 req->r_snapc = ceph_get_snap_context(snapc);
1976 /* encode request */
1977 msg->hdr.version = cpu_to_le16(4);
1979 p = msg->front.iov_base;
1980 ceph_encode_32(&p, 1); /* client_inc is always 1 */
1981 req->r_request_osdmap_epoch = p;
1983 req->r_request_flags = p;
1985 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
1986 ceph_encode_timespec(p, mtime);
1987 p += sizeof(struct ceph_timespec);
1988 req->r_request_reassert_version = p;
1989 p += sizeof(struct ceph_eversion); /* will get filled in */
1992 ceph_encode_8(&p, 4);
1993 ceph_encode_8(&p, 4);
1994 ceph_encode_32(&p, 8 + 4 + 4);
1995 req->r_request_pool = p;
1997 ceph_encode_32(&p, -1); /* preferred */
1998 ceph_encode_32(&p, 0); /* key len */
2000 ceph_encode_8(&p, 1);
2001 req->r_request_pgid = p;
2003 ceph_encode_32(&p, -1); /* preferred */
2006 ceph_encode_32(&p, req->r_oid_len);
2007 memcpy(p, req->r_oid, req->r_oid_len);
2008 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2009 p += req->r_oid_len;
2011 /* ops--can imply data */
2012 ceph_encode_16(&p, (u16)req->r_num_ops);
2014 for (i = 0; i < req->r_num_ops; i++) {
2015 data_len += osd_req_encode_op(req, p, i);
2016 p += sizeof(struct ceph_osd_op);
2020 ceph_encode_64(&p, req->r_snapid);
2021 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2022 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2024 for (i = 0; i < snapc->num_snaps; i++) {
2025 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2029 req->r_request_attempts = p;
2033 if (flags & CEPH_OSD_FLAG_WRITE) {
2037 * The header "data_off" is a hint to the receiver
2038 * allowing it to align received data into its
2039 * buffers such that there's no need to re-copy
2040 * it before writing it to disk (direct I/O).
2042 data_off = (u16) (off & 0xffff);
2043 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2045 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2047 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2048 msg_size = p - msg->front.iov_base;
2049 msg->front.iov_len = msg_size;
2050 msg->hdr.front_len = cpu_to_le32(msg_size);
2052 dout("build_request msg_size was %d\n", (int)msg_size);
2054 EXPORT_SYMBOL(ceph_osdc_build_request);
2057 * Register request, send initial attempt.
2059 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2060 struct ceph_osd_request *req,
2065 down_read(&osdc->map_sem);
2066 mutex_lock(&osdc->request_mutex);
2067 __register_request(osdc, req);
2068 WARN_ON(req->r_sent);
2069 rc = __map_request(osdc, req, 0);
2072 dout("osdc_start_request failed map, "
2073 " will retry %lld\n", req->r_tid);
2078 if (req->r_osd == NULL) {
2079 dout("send_request %p no up osds in pg\n", req);
2080 ceph_monc_request_next_osdmap(&osdc->client->monc);
2082 __send_queued(osdc);
2086 mutex_unlock(&osdc->request_mutex);
2087 up_read(&osdc->map_sem);
2090 EXPORT_SYMBOL(ceph_osdc_start_request);
2093 * wait for a request to complete
2095 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2096 struct ceph_osd_request *req)
2100 rc = wait_for_completion_interruptible(&req->r_completion);
2102 mutex_lock(&osdc->request_mutex);
2103 __cancel_request(req);
2104 __unregister_request(osdc, req);
2105 mutex_unlock(&osdc->request_mutex);
2106 complete_request(req);
2107 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
2111 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2112 return req->r_result;
2114 EXPORT_SYMBOL(ceph_osdc_wait_request);
2117 * sync - wait for all in-flight requests to flush. avoid starvation.
2119 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2121 struct ceph_osd_request *req;
2122 u64 last_tid, next_tid = 0;
2124 mutex_lock(&osdc->request_mutex);
2125 last_tid = osdc->last_tid;
2127 req = __lookup_request_ge(osdc, next_tid);
2130 if (req->r_tid > last_tid)
2133 next_tid = req->r_tid + 1;
2134 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2137 ceph_osdc_get_request(req);
2138 mutex_unlock(&osdc->request_mutex);
2139 dout("sync waiting on tid %llu (last is %llu)\n",
2140 req->r_tid, last_tid);
2141 wait_for_completion(&req->r_safe_completion);
2142 mutex_lock(&osdc->request_mutex);
2143 ceph_osdc_put_request(req);
2145 mutex_unlock(&osdc->request_mutex);
2146 dout("sync done (thru tid %llu)\n", last_tid);
2148 EXPORT_SYMBOL(ceph_osdc_sync);
2153 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2158 osdc->client = client;
2159 osdc->osdmap = NULL;
2160 init_rwsem(&osdc->map_sem);
2161 init_completion(&osdc->map_waiters);
2162 osdc->last_requested_map = 0;
2163 mutex_init(&osdc->request_mutex);
2165 osdc->osds = RB_ROOT;
2166 INIT_LIST_HEAD(&osdc->osd_lru);
2167 osdc->requests = RB_ROOT;
2168 INIT_LIST_HEAD(&osdc->req_lru);
2169 INIT_LIST_HEAD(&osdc->req_unsent);
2170 INIT_LIST_HEAD(&osdc->req_notarget);
2171 INIT_LIST_HEAD(&osdc->req_linger);
2172 osdc->num_requests = 0;
2173 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2174 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2175 spin_lock_init(&osdc->event_lock);
2176 osdc->event_tree = RB_ROOT;
2177 osdc->event_count = 0;
2179 schedule_delayed_work(&osdc->osds_timeout_work,
2180 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2183 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2184 sizeof(struct ceph_osd_request));
2185 if (!osdc->req_mempool)
2188 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2189 OSD_OP_FRONT_LEN, 10, true,
2193 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2194 OSD_OPREPLY_FRONT_LEN, 10, true,
2199 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2200 if (IS_ERR(osdc->notify_wq)) {
2201 err = PTR_ERR(osdc->notify_wq);
2202 osdc->notify_wq = NULL;
2208 ceph_msgpool_destroy(&osdc->msgpool_op);
2210 mempool_destroy(osdc->req_mempool);
2215 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2217 flush_workqueue(osdc->notify_wq);
2218 destroy_workqueue(osdc->notify_wq);
2219 cancel_delayed_work_sync(&osdc->timeout_work);
2220 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2222 ceph_osdmap_destroy(osdc->osdmap);
2223 osdc->osdmap = NULL;
2225 remove_all_osds(osdc);
2226 mempool_destroy(osdc->req_mempool);
2227 ceph_msgpool_destroy(&osdc->msgpool_op);
2228 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2232 * Read some contiguous pages. If we cross a stripe boundary, shorten
2233 * *plen. Return number of bytes read, or error.
2235 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2236 struct ceph_vino vino, struct ceph_file_layout *layout,
2238 u32 truncate_seq, u64 truncate_size,
2239 struct page **pages, int num_pages, int page_align)
2241 struct ceph_osd_request *req;
2244 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2245 vino.snap, off, *plen);
2246 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2247 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2248 NULL, truncate_seq, truncate_size,
2251 return PTR_ERR(req);
2253 /* it may be a short read due to an object boundary */
2255 osd_req_op_extent_osd_data_pages(req, 0, false,
2256 pages, *plen, page_align, false, false);
2258 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2259 off, *plen, *plen, page_align);
2261 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2263 rc = ceph_osdc_start_request(osdc, req, false);
2265 rc = ceph_osdc_wait_request(osdc, req);
2267 ceph_osdc_put_request(req);
2268 dout("readpages result %d\n", rc);
2271 EXPORT_SYMBOL(ceph_osdc_readpages);
2274 * do a synchronous write on N pages
2276 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2277 struct ceph_file_layout *layout,
2278 struct ceph_snap_context *snapc,
2280 u32 truncate_seq, u64 truncate_size,
2281 struct timespec *mtime,
2282 struct page **pages, int num_pages)
2284 struct ceph_osd_request *req;
2286 int page_align = off & ~PAGE_MASK;
2288 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2289 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2291 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2292 snapc, truncate_seq, truncate_size,
2295 return PTR_ERR(req);
2297 /* it may be a short write due to an object boundary */
2298 osd_req_op_extent_osd_data_pages(req, 0, true, pages, len, page_align,
2300 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2302 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2304 rc = ceph_osdc_start_request(osdc, req, true);
2306 rc = ceph_osdc_wait_request(osdc, req);
2308 ceph_osdc_put_request(req);
2311 dout("writepages result %d\n", rc);
2314 EXPORT_SYMBOL(ceph_osdc_writepages);
2317 * handle incoming message
2319 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2321 struct ceph_osd *osd = con->private;
2322 struct ceph_osd_client *osdc;
2323 int type = le16_to_cpu(msg->hdr.type);
2330 case CEPH_MSG_OSD_MAP:
2331 ceph_osdc_handle_map(osdc, msg);
2333 case CEPH_MSG_OSD_OPREPLY:
2334 handle_reply(osdc, msg, con);
2336 case CEPH_MSG_WATCH_NOTIFY:
2337 handle_watch_notify(osdc, msg);
2341 pr_err("received unknown message type %d %s\n", type,
2342 ceph_msg_type_name(type));
2349 * lookup and return message for incoming reply. set up reply message
2352 static struct ceph_msg *get_reply(struct ceph_connection *con,
2353 struct ceph_msg_header *hdr,
2356 struct ceph_osd *osd = con->private;
2357 struct ceph_osd_client *osdc = osd->o_osdc;
2359 struct ceph_osd_request *req;
2360 int front = le32_to_cpu(hdr->front_len);
2361 int data_len = le32_to_cpu(hdr->data_len);
2364 tid = le64_to_cpu(hdr->tid);
2365 mutex_lock(&osdc->request_mutex);
2366 req = __lookup_request(osdc, tid);
2370 dout("get_reply unknown tid %llu from osd%d\n", tid,
2375 if (req->r_reply->con)
2376 dout("%s revoking msg %p from old con %p\n", __func__,
2377 req->r_reply, req->r_reply->con);
2378 ceph_msg_revoke_incoming(req->r_reply);
2380 if (front > req->r_reply->front.iov_len) {
2381 pr_warning("get_reply front %d > preallocated %d\n",
2382 front, (int)req->r_reply->front.iov_len);
2383 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2386 ceph_msg_put(req->r_reply);
2389 m = ceph_msg_get(req->r_reply);
2392 struct ceph_osd_data *osd_data;
2395 * XXX This is assuming there is only one op containing
2396 * XXX page data. Probably OK for reads, but this
2397 * XXX ought to be done more generally.
2399 osd_data = osd_req_op_extent_osd_data(req, 0, false);
2400 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2401 if (osd_data->pages &&
2402 unlikely(osd_data->length < data_len)) {
2404 pr_warning("tid %lld reply has %d bytes "
2405 "we had only %llu bytes ready\n",
2406 tid, data_len, osd_data->length);
2415 dout("get_reply tid %lld %p\n", tid, m);
2418 mutex_unlock(&osdc->request_mutex);
2423 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2424 struct ceph_msg_header *hdr,
2427 struct ceph_osd *osd = con->private;
2428 int type = le16_to_cpu(hdr->type);
2429 int front = le32_to_cpu(hdr->front_len);
2433 case CEPH_MSG_OSD_MAP:
2434 case CEPH_MSG_WATCH_NOTIFY:
2435 return ceph_msg_new(type, front, GFP_NOFS, false);
2436 case CEPH_MSG_OSD_OPREPLY:
2437 return get_reply(con, hdr, skip);
2439 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2447 * Wrappers to refcount containing ceph_osd struct
2449 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2451 struct ceph_osd *osd = con->private;
2457 static void put_osd_con(struct ceph_connection *con)
2459 struct ceph_osd *osd = con->private;
2467 * Note: returned pointer is the address of a structure that's
2468 * managed separately. Caller must *not* attempt to free it.
2470 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2471 int *proto, int force_new)
2473 struct ceph_osd *o = con->private;
2474 struct ceph_osd_client *osdc = o->o_osdc;
2475 struct ceph_auth_client *ac = osdc->client->monc.auth;
2476 struct ceph_auth_handshake *auth = &o->o_auth;
2478 if (force_new && auth->authorizer) {
2479 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2480 auth->authorizer = NULL;
2482 if (!auth->authorizer) {
2483 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2486 return ERR_PTR(ret);
2488 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2491 return ERR_PTR(ret);
2493 *proto = ac->protocol;
2499 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2501 struct ceph_osd *o = con->private;
2502 struct ceph_osd_client *osdc = o->o_osdc;
2503 struct ceph_auth_client *ac = osdc->client->monc.auth;
2505 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2508 static int invalidate_authorizer(struct ceph_connection *con)
2510 struct ceph_osd *o = con->private;
2511 struct ceph_osd_client *osdc = o->o_osdc;
2512 struct ceph_auth_client *ac = osdc->client->monc.auth;
2514 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2515 return ceph_monc_validate_auth(&osdc->client->monc);
2518 static const struct ceph_connection_operations osd_con_ops = {
2521 .dispatch = dispatch,
2522 .get_authorizer = get_authorizer,
2523 .verify_authorizer_reply = verify_authorizer_reply,
2524 .invalidate_authorizer = invalidate_authorizer,
2525 .alloc_msg = alloc_msg,