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 #define osd_req_op_data(oreq, whch, typ, fld) \
120 BUG_ON(whch >= (oreq)->r_num_ops); \
121 &(oreq)->r_ops[whch].typ.fld; \
124 struct ceph_osd_data *
125 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
128 return osd_req_op_data(osd_req, which, extent, osd_data);
130 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
132 struct ceph_osd_data *
133 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
136 return osd_req_op_data(osd_req, which, cls, response_data);
138 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
140 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
141 unsigned int which, struct page **pages,
142 u64 length, u32 alignment,
143 bool pages_from_pool, bool own_pages)
145 struct ceph_osd_data *osd_data;
147 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
148 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
149 pages_from_pool, own_pages);
151 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
153 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
154 unsigned int which, struct ceph_pagelist *pagelist)
156 struct ceph_osd_data *osd_data;
158 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
159 ceph_osd_data_pagelist_init(osd_data, pagelist);
161 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
164 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
165 unsigned int which, struct bio *bio, size_t bio_length)
167 struct ceph_osd_data *osd_data;
169 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
170 ceph_osd_data_bio_init(osd_data, bio, bio_length);
172 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
173 #endif /* CONFIG_BLOCK */
175 static void osd_req_op_cls_request_info_pagelist(
176 struct ceph_osd_request *osd_req,
177 unsigned int which, struct ceph_pagelist *pagelist)
179 struct ceph_osd_data *osd_data;
181 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
182 ceph_osd_data_pagelist_init(osd_data, pagelist);
185 void osd_req_op_cls_request_data_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_data(osd_req, which, cls, request_data);
192 ceph_osd_data_pagelist_init(osd_data, pagelist);
194 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
196 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
197 unsigned int which, struct page **pages, u64 length,
198 u32 alignment, bool pages_from_pool, bool own_pages)
200 struct ceph_osd_data *osd_data;
202 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
203 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
204 pages_from_pool, own_pages);
206 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
208 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
210 switch (osd_data->type) {
211 case CEPH_OSD_DATA_TYPE_NONE:
213 case CEPH_OSD_DATA_TYPE_PAGES:
214 return osd_data->length;
215 case CEPH_OSD_DATA_TYPE_PAGELIST:
216 return (u64)osd_data->pagelist->length;
218 case CEPH_OSD_DATA_TYPE_BIO:
219 return (u64)osd_data->bio_length;
220 #endif /* CONFIG_BLOCK */
222 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.request_data);
255 ceph_osd_data_release(&op->cls.response_data);
265 void ceph_osdc_release_request(struct kref *kref)
267 struct ceph_osd_request *req;
270 req = container_of(kref, struct ceph_osd_request, r_kref);
272 ceph_msg_put(req->r_request);
274 ceph_msg_revoke_incoming(req->r_reply);
275 ceph_msg_put(req->r_reply);
278 for (which = 0; which < req->r_num_ops; which++)
279 osd_req_op_data_release(req, which);
281 ceph_put_snap_context(req->r_snapc);
283 mempool_free(req, req->r_osdc->req_mempool);
287 EXPORT_SYMBOL(ceph_osdc_release_request);
289 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
290 struct ceph_snap_context *snapc,
291 unsigned int num_ops,
295 struct ceph_osd_request *req;
296 struct ceph_msg *msg;
299 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
300 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
302 msg_size = 4 + 4 + 8 + 8 + 4+8;
303 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
304 msg_size += 1 + 8 + 4 + 4; /* pg_t */
305 msg_size += 4 + MAX_OBJ_NAME_SIZE;
306 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
307 msg_size += 8; /* snapid */
308 msg_size += 8; /* snap_seq */
309 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
313 req = mempool_alloc(osdc->req_mempool, gfp_flags);
314 memset(req, 0, sizeof(*req));
316 req = kzalloc(sizeof(*req), gfp_flags);
322 req->r_mempool = use_mempool;
323 req->r_num_ops = num_ops;
325 kref_init(&req->r_kref);
326 init_completion(&req->r_completion);
327 init_completion(&req->r_safe_completion);
328 RB_CLEAR_NODE(&req->r_node);
329 INIT_LIST_HEAD(&req->r_unsafe_item);
330 INIT_LIST_HEAD(&req->r_linger_item);
331 INIT_LIST_HEAD(&req->r_linger_osd);
332 INIT_LIST_HEAD(&req->r_req_lru_item);
333 INIT_LIST_HEAD(&req->r_osd_item);
335 /* create reply message */
337 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
339 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
340 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
342 ceph_osdc_put_request(req);
347 /* create request message; allow space for oid */
349 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
351 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
353 ceph_osdc_put_request(req);
357 memset(msg->front.iov_base, 0, msg->front.iov_len);
359 req->r_request = msg;
363 EXPORT_SYMBOL(ceph_osdc_alloc_request);
365 static bool osd_req_opcode_valid(u16 opcode)
368 case CEPH_OSD_OP_READ:
369 case CEPH_OSD_OP_STAT:
370 case CEPH_OSD_OP_MAPEXT:
371 case CEPH_OSD_OP_MASKTRUNC:
372 case CEPH_OSD_OP_SPARSE_READ:
373 case CEPH_OSD_OP_NOTIFY:
374 case CEPH_OSD_OP_NOTIFY_ACK:
375 case CEPH_OSD_OP_ASSERT_VER:
376 case CEPH_OSD_OP_WRITE:
377 case CEPH_OSD_OP_WRITEFULL:
378 case CEPH_OSD_OP_TRUNCATE:
379 case CEPH_OSD_OP_ZERO:
380 case CEPH_OSD_OP_DELETE:
381 case CEPH_OSD_OP_APPEND:
382 case CEPH_OSD_OP_STARTSYNC:
383 case CEPH_OSD_OP_SETTRUNC:
384 case CEPH_OSD_OP_TRIMTRUNC:
385 case CEPH_OSD_OP_TMAPUP:
386 case CEPH_OSD_OP_TMAPPUT:
387 case CEPH_OSD_OP_TMAPGET:
388 case CEPH_OSD_OP_CREATE:
389 case CEPH_OSD_OP_ROLLBACK:
390 case CEPH_OSD_OP_WATCH:
391 case CEPH_OSD_OP_OMAPGETKEYS:
392 case CEPH_OSD_OP_OMAPGETVALS:
393 case CEPH_OSD_OP_OMAPGETHEADER:
394 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
395 case CEPH_OSD_OP_OMAPSETVALS:
396 case CEPH_OSD_OP_OMAPSETHEADER:
397 case CEPH_OSD_OP_OMAPCLEAR:
398 case CEPH_OSD_OP_OMAPRMKEYS:
399 case CEPH_OSD_OP_OMAP_CMP:
400 case CEPH_OSD_OP_CLONERANGE:
401 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
402 case CEPH_OSD_OP_SRC_CMPXATTR:
403 case CEPH_OSD_OP_GETXATTR:
404 case CEPH_OSD_OP_GETXATTRS:
405 case CEPH_OSD_OP_CMPXATTR:
406 case CEPH_OSD_OP_SETXATTR:
407 case CEPH_OSD_OP_SETXATTRS:
408 case CEPH_OSD_OP_RESETXATTRS:
409 case CEPH_OSD_OP_RMXATTR:
410 case CEPH_OSD_OP_PULL:
411 case CEPH_OSD_OP_PUSH:
412 case CEPH_OSD_OP_BALANCEREADS:
413 case CEPH_OSD_OP_UNBALANCEREADS:
414 case CEPH_OSD_OP_SCRUB:
415 case CEPH_OSD_OP_SCRUB_RESERVE:
416 case CEPH_OSD_OP_SCRUB_UNRESERVE:
417 case CEPH_OSD_OP_SCRUB_STOP:
418 case CEPH_OSD_OP_SCRUB_MAP:
419 case CEPH_OSD_OP_WRLOCK:
420 case CEPH_OSD_OP_WRUNLOCK:
421 case CEPH_OSD_OP_RDLOCK:
422 case CEPH_OSD_OP_RDUNLOCK:
423 case CEPH_OSD_OP_UPLOCK:
424 case CEPH_OSD_OP_DNLOCK:
425 case CEPH_OSD_OP_CALL:
426 case CEPH_OSD_OP_PGLS:
427 case CEPH_OSD_OP_PGLS_FILTER:
435 * This is an osd op init function for opcodes that have no data or
436 * other information associated with them. It also serves as a
437 * common init routine for all the other init functions, below.
439 static struct ceph_osd_req_op *
440 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
443 struct ceph_osd_req_op *op;
445 BUG_ON(which >= osd_req->r_num_ops);
446 BUG_ON(!osd_req_opcode_valid(opcode));
448 op = &osd_req->r_ops[which];
449 memset(op, 0, sizeof (*op));
455 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
456 unsigned int which, u16 opcode,
457 u64 offset, u64 length,
458 u64 truncate_size, u32 truncate_seq)
460 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
461 size_t payload_len = 0;
463 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
465 op->extent.offset = offset;
466 op->extent.length = length;
467 op->extent.truncate_size = truncate_size;
468 op->extent.truncate_seq = truncate_seq;
469 if (opcode == CEPH_OSD_OP_WRITE)
470 payload_len += length;
472 op->payload_len = payload_len;
474 EXPORT_SYMBOL(osd_req_op_extent_init);
476 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
477 unsigned int which, u64 length)
479 struct ceph_osd_req_op *op;
482 BUG_ON(which >= osd_req->r_num_ops);
483 op = &osd_req->r_ops[which];
484 previous = op->extent.length;
486 if (length == previous)
487 return; /* Nothing to do */
488 BUG_ON(length > previous);
490 op->extent.length = length;
491 op->payload_len -= previous - length;
493 EXPORT_SYMBOL(osd_req_op_extent_update);
495 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
496 u16 opcode, const char *class, const char *method)
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 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
525 op->cls.argc = 0; /* currently unused */
527 op->payload_len = payload_len;
529 EXPORT_SYMBOL(osd_req_op_cls_init);
531 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
532 unsigned int which, u16 opcode,
533 u64 cookie, u64 version, int flag)
535 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
537 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
539 op->watch.cookie = cookie;
540 /* op->watch.ver = version; */ /* XXX 3847 */
541 op->watch.ver = cpu_to_le64(version);
542 if (opcode == CEPH_OSD_OP_WATCH && flag)
543 op->watch.flag = (u8)1;
545 EXPORT_SYMBOL(osd_req_op_watch_init);
547 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
548 struct ceph_osd_data *osd_data)
550 u64 length = ceph_osd_data_length(osd_data);
552 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
553 BUG_ON(length > (u64) SIZE_MAX);
555 ceph_msg_data_add_pages(msg, osd_data->pages,
556 length, osd_data->alignment);
557 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
559 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
561 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
562 ceph_msg_data_add_bio(msg, osd_data->bio, length);
565 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
569 static u64 osd_req_encode_op(struct ceph_osd_request *req,
570 struct ceph_osd_op *dst, unsigned int which)
572 struct ceph_osd_req_op *src;
573 struct ceph_osd_data *osd_data;
574 u64 request_data_len = 0;
577 BUG_ON(which >= req->r_num_ops);
578 src = &req->r_ops[which];
579 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
580 pr_err("unrecognized osd opcode %d\n", src->op);
586 case CEPH_OSD_OP_STAT:
588 case CEPH_OSD_OP_READ:
589 case CEPH_OSD_OP_WRITE:
590 if (src->op == CEPH_OSD_OP_WRITE)
591 request_data_len = src->extent.length;
592 dst->extent.offset = cpu_to_le64(src->extent.offset);
593 dst->extent.length = cpu_to_le64(src->extent.length);
594 dst->extent.truncate_size =
595 cpu_to_le64(src->extent.truncate_size);
596 dst->extent.truncate_seq =
597 cpu_to_le32(src->extent.truncate_seq);
598 osd_data = &src->extent.osd_data;
599 if (src->op == CEPH_OSD_OP_WRITE)
600 ceph_osdc_msg_data_add(req->r_request, osd_data);
602 ceph_osdc_msg_data_add(req->r_reply, osd_data);
604 case CEPH_OSD_OP_CALL:
605 dst->cls.class_len = src->cls.class_len;
606 dst->cls.method_len = src->cls.method_len;
607 osd_data = &src->cls.request_info;
608 ceph_osdc_msg_data_add(req->r_request, osd_data);
609 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
610 request_data_len = osd_data->pagelist->length;
612 osd_data = &src->cls.request_data;
613 data_length = ceph_osd_data_length(osd_data);
615 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
616 dst->cls.indata_len = cpu_to_le32(data_length);
617 ceph_osdc_msg_data_add(req->r_request, osd_data);
618 src->payload_len += data_length;
619 request_data_len += data_length;
621 osd_data = &src->cls.response_data;
622 ceph_osdc_msg_data_add(req->r_reply, osd_data);
624 case CEPH_OSD_OP_STARTSYNC:
626 case CEPH_OSD_OP_NOTIFY_ACK:
627 case CEPH_OSD_OP_WATCH:
628 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
629 dst->watch.ver = cpu_to_le64(src->watch.ver);
630 dst->watch.flag = src->watch.flag;
633 pr_err("unsupported osd opcode %s\n",
634 ceph_osd_op_name(src->op));
639 dst->op = cpu_to_le16(src->op);
640 dst->payload_len = cpu_to_le32(src->payload_len);
642 return request_data_len;
646 * build new request AND message, calculate layout, and adjust file
649 * if the file was recently truncated, we include information about its
650 * old and new size so that the object can be updated appropriately. (we
651 * avoid synchronously deleting truncated objects because it's slow.)
653 * if @do_sync, include a 'startsync' command so that the osd will flush
656 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
657 struct ceph_file_layout *layout,
658 struct ceph_vino vino,
659 u64 off, u64 *plen, int num_ops,
660 int opcode, int flags,
661 struct ceph_snap_context *snapc,
666 struct ceph_osd_request *req;
674 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
676 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
679 return ERR_PTR(-ENOMEM);
681 req->r_flags = flags;
683 /* calculate max write size */
684 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
686 ceph_osdc_put_request(req);
690 object_size = le32_to_cpu(layout->fl_object_size);
691 object_base = off - objoff;
692 if (truncate_size <= object_base) {
695 truncate_size -= object_base;
696 if (truncate_size > object_size)
697 truncate_size = object_size;
700 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
701 truncate_size, truncate_seq);
704 * A second op in the ops array means the caller wants to
705 * also issue a include a 'startsync' command so that the
706 * osd will flush data quickly.
709 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
711 req->r_file_layout = *layout; /* keep a copy */
713 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
715 req->r_oid_len = strlen(req->r_oid);
719 EXPORT_SYMBOL(ceph_osdc_new_request);
722 * We keep osd requests in an rbtree, sorted by ->r_tid.
724 static void __insert_request(struct ceph_osd_client *osdc,
725 struct ceph_osd_request *new)
727 struct rb_node **p = &osdc->requests.rb_node;
728 struct rb_node *parent = NULL;
729 struct ceph_osd_request *req = NULL;
733 req = rb_entry(parent, struct ceph_osd_request, r_node);
734 if (new->r_tid < req->r_tid)
736 else if (new->r_tid > req->r_tid)
742 rb_link_node(&new->r_node, parent, p);
743 rb_insert_color(&new->r_node, &osdc->requests);
746 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
749 struct ceph_osd_request *req;
750 struct rb_node *n = osdc->requests.rb_node;
753 req = rb_entry(n, struct ceph_osd_request, r_node);
754 if (tid < req->r_tid)
756 else if (tid > req->r_tid)
764 static struct ceph_osd_request *
765 __lookup_request_ge(struct ceph_osd_client *osdc,
768 struct ceph_osd_request *req;
769 struct rb_node *n = osdc->requests.rb_node;
772 req = rb_entry(n, struct ceph_osd_request, r_node);
773 if (tid < req->r_tid) {
777 } else if (tid > req->r_tid) {
787 * Resubmit requests pending on the given osd.
789 static void __kick_osd_requests(struct ceph_osd_client *osdc,
790 struct ceph_osd *osd)
792 struct ceph_osd_request *req, *nreq;
796 dout("__kick_osd_requests osd%d\n", osd->o_osd);
797 err = __reset_osd(osdc, osd);
801 * Build up a list of requests to resend by traversing the
802 * osd's list of requests. Requests for a given object are
803 * sent in tid order, and that is also the order they're
804 * kept on this list. Therefore all requests that are in
805 * flight will be found first, followed by all requests that
806 * have not yet been sent. And to resend requests while
807 * preserving this order we will want to put any sent
808 * requests back on the front of the osd client's unsent
811 * So we build a separate ordered list of already-sent
812 * requests for the affected osd and splice it onto the
813 * front of the osd client's unsent list. Once we've seen a
814 * request that has not yet been sent we're done. Those
815 * requests are already sitting right where they belong.
817 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
820 list_move_tail(&req->r_req_lru_item, &resend);
821 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
824 req->r_flags |= CEPH_OSD_FLAG_RETRY;
826 list_splice(&resend, &osdc->req_unsent);
829 * Linger requests are re-registered before sending, which
830 * sets up a new tid for each. We add them to the unsent
831 * list at the end to keep things in tid order.
833 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
836 * reregister request prior to unregistering linger so
837 * that r_osd is preserved.
839 BUG_ON(!list_empty(&req->r_req_lru_item));
840 __register_request(osdc, req);
841 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
842 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
843 __unregister_linger_request(osdc, req);
844 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
850 * If the osd connection drops, we need to resubmit all requests.
852 static void osd_reset(struct ceph_connection *con)
854 struct ceph_osd *osd = con->private;
855 struct ceph_osd_client *osdc;
859 dout("osd_reset osd%d\n", osd->o_osd);
861 down_read(&osdc->map_sem);
862 mutex_lock(&osdc->request_mutex);
863 __kick_osd_requests(osdc, osd);
865 mutex_unlock(&osdc->request_mutex);
866 up_read(&osdc->map_sem);
870 * Track open sessions with osds.
872 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
874 struct ceph_osd *osd;
876 osd = kzalloc(sizeof(*osd), GFP_NOFS);
880 atomic_set(&osd->o_ref, 1);
883 RB_CLEAR_NODE(&osd->o_node);
884 INIT_LIST_HEAD(&osd->o_requests);
885 INIT_LIST_HEAD(&osd->o_linger_requests);
886 INIT_LIST_HEAD(&osd->o_osd_lru);
887 osd->o_incarnation = 1;
889 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
891 INIT_LIST_HEAD(&osd->o_keepalive_item);
895 static struct ceph_osd *get_osd(struct ceph_osd *osd)
897 if (atomic_inc_not_zero(&osd->o_ref)) {
898 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
899 atomic_read(&osd->o_ref));
902 dout("get_osd %p FAIL\n", osd);
907 static void put_osd(struct ceph_osd *osd)
909 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
910 atomic_read(&osd->o_ref) - 1);
911 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
912 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
914 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
920 * remove an osd from our map
922 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
924 dout("__remove_osd %p\n", osd);
925 BUG_ON(!list_empty(&osd->o_requests));
926 rb_erase(&osd->o_node, &osdc->osds);
927 list_del_init(&osd->o_osd_lru);
928 ceph_con_close(&osd->o_con);
932 static void remove_all_osds(struct ceph_osd_client *osdc)
934 dout("%s %p\n", __func__, osdc);
935 mutex_lock(&osdc->request_mutex);
936 while (!RB_EMPTY_ROOT(&osdc->osds)) {
937 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
938 struct ceph_osd, o_node);
939 __remove_osd(osdc, osd);
941 mutex_unlock(&osdc->request_mutex);
944 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
945 struct ceph_osd *osd)
947 dout("__move_osd_to_lru %p\n", osd);
948 BUG_ON(!list_empty(&osd->o_osd_lru));
949 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
950 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
953 static void __remove_osd_from_lru(struct ceph_osd *osd)
955 dout("__remove_osd_from_lru %p\n", osd);
956 if (!list_empty(&osd->o_osd_lru))
957 list_del_init(&osd->o_osd_lru);
960 static void remove_old_osds(struct ceph_osd_client *osdc)
962 struct ceph_osd *osd, *nosd;
964 dout("__remove_old_osds %p\n", osdc);
965 mutex_lock(&osdc->request_mutex);
966 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
967 if (time_before(jiffies, osd->lru_ttl))
969 __remove_osd(osdc, osd);
971 mutex_unlock(&osdc->request_mutex);
977 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
979 struct ceph_entity_addr *peer_addr;
981 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
982 if (list_empty(&osd->o_requests) &&
983 list_empty(&osd->o_linger_requests)) {
984 __remove_osd(osdc, osd);
989 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
990 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
991 !ceph_con_opened(&osd->o_con)) {
992 struct ceph_osd_request *req;
994 dout(" osd addr hasn't changed and connection never opened,"
995 " letting msgr retry");
996 /* touch each r_stamp for handle_timeout()'s benfit */
997 list_for_each_entry(req, &osd->o_requests, r_osd_item)
998 req->r_stamp = jiffies;
1003 ceph_con_close(&osd->o_con);
1004 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1005 osd->o_incarnation++;
1010 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1012 struct rb_node **p = &osdc->osds.rb_node;
1013 struct rb_node *parent = NULL;
1014 struct ceph_osd *osd = NULL;
1016 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1019 osd = rb_entry(parent, struct ceph_osd, o_node);
1020 if (new->o_osd < osd->o_osd)
1022 else if (new->o_osd > osd->o_osd)
1023 p = &(*p)->rb_right;
1028 rb_link_node(&new->o_node, parent, p);
1029 rb_insert_color(&new->o_node, &osdc->osds);
1032 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1034 struct ceph_osd *osd;
1035 struct rb_node *n = osdc->osds.rb_node;
1038 osd = rb_entry(n, struct ceph_osd, o_node);
1041 else if (o > osd->o_osd)
1049 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1051 schedule_delayed_work(&osdc->timeout_work,
1052 osdc->client->options->osd_keepalive_timeout * HZ);
1055 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1057 cancel_delayed_work(&osdc->timeout_work);
1061 * Register request, assign tid. If this is the first request, set up
1062 * the timeout event.
1064 static void __register_request(struct ceph_osd_client *osdc,
1065 struct ceph_osd_request *req)
1067 req->r_tid = ++osdc->last_tid;
1068 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1069 dout("__register_request %p tid %lld\n", req, req->r_tid);
1070 __insert_request(osdc, req);
1071 ceph_osdc_get_request(req);
1072 osdc->num_requests++;
1073 if (osdc->num_requests == 1) {
1074 dout(" first request, scheduling timeout\n");
1075 __schedule_osd_timeout(osdc);
1080 * called under osdc->request_mutex
1082 static void __unregister_request(struct ceph_osd_client *osdc,
1083 struct ceph_osd_request *req)
1085 if (RB_EMPTY_NODE(&req->r_node)) {
1086 dout("__unregister_request %p tid %lld not registered\n",
1091 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1092 rb_erase(&req->r_node, &osdc->requests);
1093 osdc->num_requests--;
1096 /* make sure the original request isn't in flight. */
1097 ceph_msg_revoke(req->r_request);
1099 list_del_init(&req->r_osd_item);
1100 if (list_empty(&req->r_osd->o_requests) &&
1101 list_empty(&req->r_osd->o_linger_requests)) {
1102 dout("moving osd to %p lru\n", req->r_osd);
1103 __move_osd_to_lru(osdc, req->r_osd);
1105 if (list_empty(&req->r_linger_item))
1109 list_del_init(&req->r_req_lru_item);
1110 ceph_osdc_put_request(req);
1112 if (osdc->num_requests == 0) {
1113 dout(" no requests, canceling timeout\n");
1114 __cancel_osd_timeout(osdc);
1119 * Cancel a previously queued request message
1121 static void __cancel_request(struct ceph_osd_request *req)
1123 if (req->r_sent && req->r_osd) {
1124 ceph_msg_revoke(req->r_request);
1129 static void __register_linger_request(struct ceph_osd_client *osdc,
1130 struct ceph_osd_request *req)
1132 dout("__register_linger_request %p\n", req);
1133 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1135 list_add_tail(&req->r_linger_osd,
1136 &req->r_osd->o_linger_requests);
1139 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1140 struct ceph_osd_request *req)
1142 dout("__unregister_linger_request %p\n", req);
1143 list_del_init(&req->r_linger_item);
1145 list_del_init(&req->r_linger_osd);
1147 if (list_empty(&req->r_osd->o_requests) &&
1148 list_empty(&req->r_osd->o_linger_requests)) {
1149 dout("moving osd to %p lru\n", req->r_osd);
1150 __move_osd_to_lru(osdc, req->r_osd);
1152 if (list_empty(&req->r_osd_item))
1157 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1158 struct ceph_osd_request *req)
1160 mutex_lock(&osdc->request_mutex);
1161 if (req->r_linger) {
1162 __unregister_linger_request(osdc, req);
1163 ceph_osdc_put_request(req);
1165 mutex_unlock(&osdc->request_mutex);
1167 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1169 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1170 struct ceph_osd_request *req)
1172 if (!req->r_linger) {
1173 dout("set_request_linger %p\n", req);
1176 * caller is now responsible for calling
1177 * unregister_linger_request
1179 ceph_osdc_get_request(req);
1182 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1185 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1186 * (as needed), and set the request r_osd appropriately. If there is
1187 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1188 * (unsent, homeless) or leave on in-flight lru.
1190 * Return 0 if unchanged, 1 if changed, or negative on error.
1192 * Caller should hold map_sem for read and request_mutex.
1194 static int __map_request(struct ceph_osd_client *osdc,
1195 struct ceph_osd_request *req, int force_resend)
1197 struct ceph_pg pgid;
1198 int acting[CEPH_PG_MAX_SIZE];
1199 int o = -1, num = 0;
1202 dout("map_request %p tid %lld\n", req, req->r_tid);
1203 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1204 ceph_file_layout_pg_pool(req->r_file_layout));
1206 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1211 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1217 if ((!force_resend &&
1218 req->r_osd && req->r_osd->o_osd == o &&
1219 req->r_sent >= req->r_osd->o_incarnation &&
1220 req->r_num_pg_osds == num &&
1221 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1222 (req->r_osd == NULL && o == -1))
1223 return 0; /* no change */
1225 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1226 req->r_tid, pgid.pool, pgid.seed, o,
1227 req->r_osd ? req->r_osd->o_osd : -1);
1229 /* record full pg acting set */
1230 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1231 req->r_num_pg_osds = num;
1234 __cancel_request(req);
1235 list_del_init(&req->r_osd_item);
1239 req->r_osd = __lookup_osd(osdc, o);
1240 if (!req->r_osd && o >= 0) {
1242 req->r_osd = create_osd(osdc, o);
1244 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1248 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1249 __insert_osd(osdc, req->r_osd);
1251 ceph_con_open(&req->r_osd->o_con,
1252 CEPH_ENTITY_TYPE_OSD, o,
1253 &osdc->osdmap->osd_addr[o]);
1257 __remove_osd_from_lru(req->r_osd);
1258 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1259 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1261 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1263 err = 1; /* osd or pg changed */
1270 * caller should hold map_sem (for read) and request_mutex
1272 static void __send_request(struct ceph_osd_client *osdc,
1273 struct ceph_osd_request *req)
1277 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1278 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1279 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1281 /* fill in message content that changes each time we send it */
1282 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1283 put_unaligned_le32(req->r_flags, req->r_request_flags);
1284 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1285 p = req->r_request_pgid;
1286 ceph_encode_64(&p, req->r_pgid.pool);
1287 ceph_encode_32(&p, req->r_pgid.seed);
1288 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1289 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1290 sizeof(req->r_reassert_version));
1292 req->r_stamp = jiffies;
1293 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1295 ceph_msg_get(req->r_request); /* send consumes a ref */
1297 /* Mark the request unsafe if this is the first timet's being sent. */
1299 if (!req->r_sent && req->r_unsafe_callback)
1300 req->r_unsafe_callback(req, true);
1301 req->r_sent = req->r_osd->o_incarnation;
1303 ceph_con_send(&req->r_osd->o_con, req->r_request);
1307 * Send any requests in the queue (req_unsent).
1309 static void __send_queued(struct ceph_osd_client *osdc)
1311 struct ceph_osd_request *req, *tmp;
1313 dout("__send_queued\n");
1314 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1315 __send_request(osdc, req);
1319 * Timeout callback, called every N seconds when 1 or more osd
1320 * requests has been active for more than N seconds. When this
1321 * happens, we ping all OSDs with requests who have timed out to
1322 * ensure any communications channel reset is detected. Reset the
1323 * request timeouts another N seconds in the future as we go.
1324 * Reschedule the timeout event another N seconds in future (unless
1325 * there are no open requests).
1327 static void handle_timeout(struct work_struct *work)
1329 struct ceph_osd_client *osdc =
1330 container_of(work, struct ceph_osd_client, timeout_work.work);
1331 struct ceph_osd_request *req;
1332 struct ceph_osd *osd;
1333 unsigned long keepalive =
1334 osdc->client->options->osd_keepalive_timeout * HZ;
1335 struct list_head slow_osds;
1337 down_read(&osdc->map_sem);
1339 ceph_monc_request_next_osdmap(&osdc->client->monc);
1341 mutex_lock(&osdc->request_mutex);
1344 * ping osds that are a bit slow. this ensures that if there
1345 * is a break in the TCP connection we will notice, and reopen
1346 * a connection with that osd (from the fault callback).
1348 INIT_LIST_HEAD(&slow_osds);
1349 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1350 if (time_before(jiffies, req->r_stamp + keepalive))
1355 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1356 req->r_tid, osd->o_osd);
1357 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1359 while (!list_empty(&slow_osds)) {
1360 osd = list_entry(slow_osds.next, struct ceph_osd,
1362 list_del_init(&osd->o_keepalive_item);
1363 ceph_con_keepalive(&osd->o_con);
1366 __schedule_osd_timeout(osdc);
1367 __send_queued(osdc);
1368 mutex_unlock(&osdc->request_mutex);
1369 up_read(&osdc->map_sem);
1372 static void handle_osds_timeout(struct work_struct *work)
1374 struct ceph_osd_client *osdc =
1375 container_of(work, struct ceph_osd_client,
1376 osds_timeout_work.work);
1377 unsigned long delay =
1378 osdc->client->options->osd_idle_ttl * HZ >> 2;
1380 dout("osds timeout\n");
1381 down_read(&osdc->map_sem);
1382 remove_old_osds(osdc);
1383 up_read(&osdc->map_sem);
1385 schedule_delayed_work(&osdc->osds_timeout_work,
1386 round_jiffies_relative(delay));
1389 static void complete_request(struct ceph_osd_request *req)
1391 if (req->r_unsafe_callback)
1392 req->r_unsafe_callback(req, false);
1393 complete_all(&req->r_safe_completion); /* fsync waiter */
1397 * handle osd op reply. either call the callback if it is specified,
1398 * or do the completion to wake up the waiting thread.
1400 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1401 struct ceph_connection *con)
1404 struct ceph_osd_request *req;
1407 unsigned int numops;
1408 int payload_len, flags;
1414 u64 reassert_version;
1416 int already_completed;
1420 tid = le64_to_cpu(msg->hdr.tid);
1421 dout("handle_reply %p tid %llu\n", msg, tid);
1423 p = msg->front.iov_base;
1424 end = p + msg->front.iov_len;
1426 ceph_decode_need(&p, end, 4, bad);
1427 object_len = ceph_decode_32(&p);
1428 ceph_decode_need(&p, end, object_len, bad);
1431 err = ceph_decode_pgid(&p, end, &pg);
1435 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1436 flags = ceph_decode_64(&p);
1437 result = ceph_decode_32(&p);
1438 reassert_epoch = ceph_decode_32(&p);
1439 reassert_version = ceph_decode_64(&p);
1440 osdmap_epoch = ceph_decode_32(&p);
1443 mutex_lock(&osdc->request_mutex);
1444 req = __lookup_request(osdc, tid);
1446 dout("handle_reply tid %llu dne\n", tid);
1449 ceph_osdc_get_request(req);
1451 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1454 ceph_decode_need(&p, end, 4, bad);
1455 numops = ceph_decode_32(&p);
1456 if (numops > CEPH_OSD_MAX_OP)
1458 if (numops != req->r_num_ops)
1461 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1462 for (i = 0; i < numops; i++) {
1463 struct ceph_osd_op *op = p;
1466 len = le32_to_cpu(op->payload_len);
1467 req->r_reply_op_len[i] = len;
1468 dout(" op %d has %d bytes\n", i, len);
1472 bytes = le32_to_cpu(msg->hdr.data_len);
1473 if (payload_len != bytes) {
1474 pr_warning("sum of op payload lens %d != data_len %d",
1475 payload_len, bytes);
1479 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1480 retry_attempt = ceph_decode_32(&p);
1481 for (i = 0; i < numops; i++)
1482 req->r_reply_op_result[i] = ceph_decode_32(&p);
1484 if (!req->r_got_reply) {
1486 req->r_result = result;
1487 dout("handle_reply result %d bytes %d\n", req->r_result,
1489 if (req->r_result == 0)
1490 req->r_result = bytes;
1492 /* in case this is a write and we need to replay, */
1493 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1494 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1496 req->r_got_reply = 1;
1497 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1498 dout("handle_reply tid %llu dup ack\n", tid);
1499 mutex_unlock(&osdc->request_mutex);
1503 dout("handle_reply tid %llu flags %d\n", tid, flags);
1505 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1506 __register_linger_request(osdc, req);
1508 /* either this is a read, or we got the safe response */
1510 (flags & CEPH_OSD_FLAG_ONDISK) ||
1511 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1512 __unregister_request(osdc, req);
1514 already_completed = req->r_completed;
1515 req->r_completed = 1;
1516 mutex_unlock(&osdc->request_mutex);
1517 if (already_completed)
1520 if (req->r_callback)
1521 req->r_callback(req, msg);
1523 complete_all(&req->r_completion);
1525 if (flags & CEPH_OSD_FLAG_ONDISK)
1526 complete_request(req);
1529 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1530 ceph_osdc_put_request(req);
1534 ceph_osdc_put_request(req);
1536 mutex_unlock(&osdc->request_mutex);
1538 pr_err("corrupt osd_op_reply got %d %d\n",
1539 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1543 static void reset_changed_osds(struct ceph_osd_client *osdc)
1545 struct rb_node *p, *n;
1547 for (p = rb_first(&osdc->osds); p; p = n) {
1548 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1551 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1552 memcmp(&osd->o_con.peer_addr,
1553 ceph_osd_addr(osdc->osdmap,
1555 sizeof(struct ceph_entity_addr)) != 0)
1556 __reset_osd(osdc, osd);
1561 * Requeue requests whose mapping to an OSD has changed. If requests map to
1562 * no osd, request a new map.
1564 * Caller should hold map_sem for read.
1566 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1568 struct ceph_osd_request *req, *nreq;
1573 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1574 mutex_lock(&osdc->request_mutex);
1575 for (p = rb_first(&osdc->requests); p; ) {
1576 req = rb_entry(p, struct ceph_osd_request, r_node);
1580 * For linger requests that have not yet been
1581 * registered, move them to the linger list; they'll
1582 * be sent to the osd in the loop below. Unregister
1583 * the request before re-registering it as a linger
1584 * request to ensure the __map_request() below
1585 * will decide it needs to be sent.
1587 if (req->r_linger && list_empty(&req->r_linger_item)) {
1588 dout("%p tid %llu restart on osd%d\n",
1590 req->r_osd ? req->r_osd->o_osd : -1);
1591 __unregister_request(osdc, req);
1592 __register_linger_request(osdc, req);
1596 err = __map_request(osdc, req, force_resend);
1598 continue; /* error */
1599 if (req->r_osd == NULL) {
1600 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1601 needmap++; /* request a newer map */
1602 } else if (err > 0) {
1603 if (!req->r_linger) {
1604 dout("%p tid %llu requeued on osd%d\n", req,
1606 req->r_osd ? req->r_osd->o_osd : -1);
1607 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1612 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1614 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1616 err = __map_request(osdc, req, force_resend);
1617 dout("__map_request returned %d\n", err);
1619 continue; /* no change and no osd was specified */
1621 continue; /* hrm! */
1622 if (req->r_osd == NULL) {
1623 dout("tid %llu maps to no valid osd\n", req->r_tid);
1624 needmap++; /* request a newer map */
1628 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1629 req->r_osd ? req->r_osd->o_osd : -1);
1630 __register_request(osdc, req);
1631 __unregister_linger_request(osdc, req);
1633 mutex_unlock(&osdc->request_mutex);
1636 dout("%d requests for down osds, need new map\n", needmap);
1637 ceph_monc_request_next_osdmap(&osdc->client->monc);
1639 reset_changed_osds(osdc);
1644 * Process updated osd map.
1646 * The message contains any number of incremental and full maps, normally
1647 * indicating some sort of topology change in the cluster. Kick requests
1648 * off to different OSDs as needed.
1650 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1652 void *p, *end, *next;
1653 u32 nr_maps, maplen;
1655 struct ceph_osdmap *newmap = NULL, *oldmap;
1657 struct ceph_fsid fsid;
1659 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1660 p = msg->front.iov_base;
1661 end = p + msg->front.iov_len;
1664 ceph_decode_need(&p, end, sizeof(fsid), bad);
1665 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1666 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1669 down_write(&osdc->map_sem);
1671 /* incremental maps */
1672 ceph_decode_32_safe(&p, end, nr_maps, bad);
1673 dout(" %d inc maps\n", nr_maps);
1674 while (nr_maps > 0) {
1675 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1676 epoch = ceph_decode_32(&p);
1677 maplen = ceph_decode_32(&p);
1678 ceph_decode_need(&p, end, maplen, bad);
1680 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1681 dout("applying incremental map %u len %d\n",
1683 newmap = osdmap_apply_incremental(&p, next,
1685 &osdc->client->msgr);
1686 if (IS_ERR(newmap)) {
1687 err = PTR_ERR(newmap);
1691 if (newmap != osdc->osdmap) {
1692 ceph_osdmap_destroy(osdc->osdmap);
1693 osdc->osdmap = newmap;
1695 kick_requests(osdc, 0);
1697 dout("ignoring incremental map %u len %d\n",
1707 ceph_decode_32_safe(&p, end, nr_maps, bad);
1708 dout(" %d full maps\n", nr_maps);
1710 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1711 epoch = ceph_decode_32(&p);
1712 maplen = ceph_decode_32(&p);
1713 ceph_decode_need(&p, end, maplen, bad);
1715 dout("skipping non-latest full map %u len %d\n",
1717 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1718 dout("skipping full map %u len %d, "
1719 "older than our %u\n", epoch, maplen,
1720 osdc->osdmap->epoch);
1722 int skipped_map = 0;
1724 dout("taking full map %u len %d\n", epoch, maplen);
1725 newmap = osdmap_decode(&p, p+maplen);
1726 if (IS_ERR(newmap)) {
1727 err = PTR_ERR(newmap);
1731 oldmap = osdc->osdmap;
1732 osdc->osdmap = newmap;
1734 if (oldmap->epoch + 1 < newmap->epoch)
1736 ceph_osdmap_destroy(oldmap);
1738 kick_requests(osdc, skipped_map);
1745 downgrade_write(&osdc->map_sem);
1746 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1749 * subscribe to subsequent osdmap updates if full to ensure
1750 * we find out when we are no longer full and stop returning
1753 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1754 ceph_monc_request_next_osdmap(&osdc->client->monc);
1756 mutex_lock(&osdc->request_mutex);
1757 __send_queued(osdc);
1758 mutex_unlock(&osdc->request_mutex);
1759 up_read(&osdc->map_sem);
1760 wake_up_all(&osdc->client->auth_wq);
1764 pr_err("osdc handle_map corrupt msg\n");
1766 up_write(&osdc->map_sem);
1771 * watch/notify callback event infrastructure
1773 * These callbacks are used both for watch and notify operations.
1775 static void __release_event(struct kref *kref)
1777 struct ceph_osd_event *event =
1778 container_of(kref, struct ceph_osd_event, kref);
1780 dout("__release_event %p\n", event);
1784 static void get_event(struct ceph_osd_event *event)
1786 kref_get(&event->kref);
1789 void ceph_osdc_put_event(struct ceph_osd_event *event)
1791 kref_put(&event->kref, __release_event);
1793 EXPORT_SYMBOL(ceph_osdc_put_event);
1795 static void __insert_event(struct ceph_osd_client *osdc,
1796 struct ceph_osd_event *new)
1798 struct rb_node **p = &osdc->event_tree.rb_node;
1799 struct rb_node *parent = NULL;
1800 struct ceph_osd_event *event = NULL;
1804 event = rb_entry(parent, struct ceph_osd_event, node);
1805 if (new->cookie < event->cookie)
1807 else if (new->cookie > event->cookie)
1808 p = &(*p)->rb_right;
1813 rb_link_node(&new->node, parent, p);
1814 rb_insert_color(&new->node, &osdc->event_tree);
1817 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1820 struct rb_node **p = &osdc->event_tree.rb_node;
1821 struct rb_node *parent = NULL;
1822 struct ceph_osd_event *event = NULL;
1826 event = rb_entry(parent, struct ceph_osd_event, node);
1827 if (cookie < event->cookie)
1829 else if (cookie > event->cookie)
1830 p = &(*p)->rb_right;
1837 static void __remove_event(struct ceph_osd_event *event)
1839 struct ceph_osd_client *osdc = event->osdc;
1841 if (!RB_EMPTY_NODE(&event->node)) {
1842 dout("__remove_event removed %p\n", event);
1843 rb_erase(&event->node, &osdc->event_tree);
1844 ceph_osdc_put_event(event);
1846 dout("__remove_event didn't remove %p\n", event);
1850 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1851 void (*event_cb)(u64, u64, u8, void *),
1852 void *data, struct ceph_osd_event **pevent)
1854 struct ceph_osd_event *event;
1856 event = kmalloc(sizeof(*event), GFP_NOIO);
1860 dout("create_event %p\n", event);
1861 event->cb = event_cb;
1862 event->one_shot = 0;
1865 INIT_LIST_HEAD(&event->osd_node);
1866 RB_CLEAR_NODE(&event->node);
1867 kref_init(&event->kref); /* one ref for us */
1868 kref_get(&event->kref); /* one ref for the caller */
1870 spin_lock(&osdc->event_lock);
1871 event->cookie = ++osdc->event_count;
1872 __insert_event(osdc, event);
1873 spin_unlock(&osdc->event_lock);
1878 EXPORT_SYMBOL(ceph_osdc_create_event);
1880 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1882 struct ceph_osd_client *osdc = event->osdc;
1884 dout("cancel_event %p\n", event);
1885 spin_lock(&osdc->event_lock);
1886 __remove_event(event);
1887 spin_unlock(&osdc->event_lock);
1888 ceph_osdc_put_event(event); /* caller's */
1890 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1893 static void do_event_work(struct work_struct *work)
1895 struct ceph_osd_event_work *event_work =
1896 container_of(work, struct ceph_osd_event_work, work);
1897 struct ceph_osd_event *event = event_work->event;
1898 u64 ver = event_work->ver;
1899 u64 notify_id = event_work->notify_id;
1900 u8 opcode = event_work->opcode;
1902 dout("do_event_work completing %p\n", event);
1903 event->cb(ver, notify_id, opcode, event->data);
1904 dout("do_event_work completed %p\n", event);
1905 ceph_osdc_put_event(event);
1911 * Process osd watch notifications
1913 static void handle_watch_notify(struct ceph_osd_client *osdc,
1914 struct ceph_msg *msg)
1918 u64 cookie, ver, notify_id;
1920 struct ceph_osd_event *event;
1921 struct ceph_osd_event_work *event_work;
1923 p = msg->front.iov_base;
1924 end = p + msg->front.iov_len;
1926 ceph_decode_8_safe(&p, end, proto_ver, bad);
1927 ceph_decode_8_safe(&p, end, opcode, bad);
1928 ceph_decode_64_safe(&p, end, cookie, bad);
1929 ceph_decode_64_safe(&p, end, ver, bad);
1930 ceph_decode_64_safe(&p, end, notify_id, bad);
1932 spin_lock(&osdc->event_lock);
1933 event = __find_event(osdc, cookie);
1935 BUG_ON(event->one_shot);
1938 spin_unlock(&osdc->event_lock);
1939 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1940 cookie, ver, event);
1942 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1944 dout("ERROR: could not allocate event_work\n");
1947 INIT_WORK(&event_work->work, do_event_work);
1948 event_work->event = event;
1949 event_work->ver = ver;
1950 event_work->notify_id = notify_id;
1951 event_work->opcode = opcode;
1952 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1953 dout("WARNING: failed to queue notify event work\n");
1961 ceph_osdc_put_event(event);
1965 pr_err("osdc handle_watch_notify corrupt msg\n");
1970 * build new request AND message
1973 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
1974 struct ceph_snap_context *snapc, u64 snap_id,
1975 struct timespec *mtime)
1977 struct ceph_msg *msg = req->r_request;
1980 int flags = req->r_flags;
1984 req->r_snapid = snap_id;
1985 req->r_snapc = ceph_get_snap_context(snapc);
1987 /* encode request */
1988 msg->hdr.version = cpu_to_le16(4);
1990 p = msg->front.iov_base;
1991 ceph_encode_32(&p, 1); /* client_inc is always 1 */
1992 req->r_request_osdmap_epoch = p;
1994 req->r_request_flags = p;
1996 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
1997 ceph_encode_timespec(p, mtime);
1998 p += sizeof(struct ceph_timespec);
1999 req->r_request_reassert_version = p;
2000 p += sizeof(struct ceph_eversion); /* will get filled in */
2003 ceph_encode_8(&p, 4);
2004 ceph_encode_8(&p, 4);
2005 ceph_encode_32(&p, 8 + 4 + 4);
2006 req->r_request_pool = p;
2008 ceph_encode_32(&p, -1); /* preferred */
2009 ceph_encode_32(&p, 0); /* key len */
2011 ceph_encode_8(&p, 1);
2012 req->r_request_pgid = p;
2014 ceph_encode_32(&p, -1); /* preferred */
2017 ceph_encode_32(&p, req->r_oid_len);
2018 memcpy(p, req->r_oid, req->r_oid_len);
2019 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2020 p += req->r_oid_len;
2022 /* ops--can imply data */
2023 ceph_encode_16(&p, (u16)req->r_num_ops);
2025 for (i = 0; i < req->r_num_ops; i++) {
2026 data_len += osd_req_encode_op(req, p, i);
2027 p += sizeof(struct ceph_osd_op);
2031 ceph_encode_64(&p, req->r_snapid);
2032 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2033 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2035 for (i = 0; i < snapc->num_snaps; i++) {
2036 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2040 req->r_request_attempts = p;
2044 if (flags & CEPH_OSD_FLAG_WRITE) {
2048 * The header "data_off" is a hint to the receiver
2049 * allowing it to align received data into its
2050 * buffers such that there's no need to re-copy
2051 * it before writing it to disk (direct I/O).
2053 data_off = (u16) (off & 0xffff);
2054 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2056 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2058 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2059 msg_size = p - msg->front.iov_base;
2060 msg->front.iov_len = msg_size;
2061 msg->hdr.front_len = cpu_to_le32(msg_size);
2063 dout("build_request msg_size was %d\n", (int)msg_size);
2065 EXPORT_SYMBOL(ceph_osdc_build_request);
2068 * Register request, send initial attempt.
2070 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2071 struct ceph_osd_request *req,
2076 down_read(&osdc->map_sem);
2077 mutex_lock(&osdc->request_mutex);
2078 __register_request(osdc, req);
2079 WARN_ON(req->r_sent);
2080 rc = __map_request(osdc, req, 0);
2083 dout("osdc_start_request failed map, "
2084 " will retry %lld\n", req->r_tid);
2089 if (req->r_osd == NULL) {
2090 dout("send_request %p no up osds in pg\n", req);
2091 ceph_monc_request_next_osdmap(&osdc->client->monc);
2093 __send_queued(osdc);
2097 mutex_unlock(&osdc->request_mutex);
2098 up_read(&osdc->map_sem);
2101 EXPORT_SYMBOL(ceph_osdc_start_request);
2104 * wait for a request to complete
2106 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2107 struct ceph_osd_request *req)
2111 rc = wait_for_completion_interruptible(&req->r_completion);
2113 mutex_lock(&osdc->request_mutex);
2114 __cancel_request(req);
2115 __unregister_request(osdc, req);
2116 mutex_unlock(&osdc->request_mutex);
2117 complete_request(req);
2118 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
2122 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2123 return req->r_result;
2125 EXPORT_SYMBOL(ceph_osdc_wait_request);
2128 * sync - wait for all in-flight requests to flush. avoid starvation.
2130 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2132 struct ceph_osd_request *req;
2133 u64 last_tid, next_tid = 0;
2135 mutex_lock(&osdc->request_mutex);
2136 last_tid = osdc->last_tid;
2138 req = __lookup_request_ge(osdc, next_tid);
2141 if (req->r_tid > last_tid)
2144 next_tid = req->r_tid + 1;
2145 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2148 ceph_osdc_get_request(req);
2149 mutex_unlock(&osdc->request_mutex);
2150 dout("sync waiting on tid %llu (last is %llu)\n",
2151 req->r_tid, last_tid);
2152 wait_for_completion(&req->r_safe_completion);
2153 mutex_lock(&osdc->request_mutex);
2154 ceph_osdc_put_request(req);
2156 mutex_unlock(&osdc->request_mutex);
2157 dout("sync done (thru tid %llu)\n", last_tid);
2159 EXPORT_SYMBOL(ceph_osdc_sync);
2164 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2169 osdc->client = client;
2170 osdc->osdmap = NULL;
2171 init_rwsem(&osdc->map_sem);
2172 init_completion(&osdc->map_waiters);
2173 osdc->last_requested_map = 0;
2174 mutex_init(&osdc->request_mutex);
2176 osdc->osds = RB_ROOT;
2177 INIT_LIST_HEAD(&osdc->osd_lru);
2178 osdc->requests = RB_ROOT;
2179 INIT_LIST_HEAD(&osdc->req_lru);
2180 INIT_LIST_HEAD(&osdc->req_unsent);
2181 INIT_LIST_HEAD(&osdc->req_notarget);
2182 INIT_LIST_HEAD(&osdc->req_linger);
2183 osdc->num_requests = 0;
2184 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2185 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2186 spin_lock_init(&osdc->event_lock);
2187 osdc->event_tree = RB_ROOT;
2188 osdc->event_count = 0;
2190 schedule_delayed_work(&osdc->osds_timeout_work,
2191 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2194 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2195 sizeof(struct ceph_osd_request));
2196 if (!osdc->req_mempool)
2199 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2200 OSD_OP_FRONT_LEN, 10, true,
2204 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2205 OSD_OPREPLY_FRONT_LEN, 10, true,
2210 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2211 if (IS_ERR(osdc->notify_wq)) {
2212 err = PTR_ERR(osdc->notify_wq);
2213 osdc->notify_wq = NULL;
2219 ceph_msgpool_destroy(&osdc->msgpool_op);
2221 mempool_destroy(osdc->req_mempool);
2226 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2228 flush_workqueue(osdc->notify_wq);
2229 destroy_workqueue(osdc->notify_wq);
2230 cancel_delayed_work_sync(&osdc->timeout_work);
2231 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2233 ceph_osdmap_destroy(osdc->osdmap);
2234 osdc->osdmap = NULL;
2236 remove_all_osds(osdc);
2237 mempool_destroy(osdc->req_mempool);
2238 ceph_msgpool_destroy(&osdc->msgpool_op);
2239 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2243 * Read some contiguous pages. If we cross a stripe boundary, shorten
2244 * *plen. Return number of bytes read, or error.
2246 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2247 struct ceph_vino vino, struct ceph_file_layout *layout,
2249 u32 truncate_seq, u64 truncate_size,
2250 struct page **pages, int num_pages, int page_align)
2252 struct ceph_osd_request *req;
2255 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2256 vino.snap, off, *plen);
2257 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2258 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2259 NULL, truncate_seq, truncate_size,
2262 return PTR_ERR(req);
2264 /* it may be a short read due to an object boundary */
2266 osd_req_op_extent_osd_data_pages(req, 0,
2267 pages, *plen, page_align, false, false);
2269 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2270 off, *plen, *plen, page_align);
2272 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2274 rc = ceph_osdc_start_request(osdc, req, false);
2276 rc = ceph_osdc_wait_request(osdc, req);
2278 ceph_osdc_put_request(req);
2279 dout("readpages result %d\n", rc);
2282 EXPORT_SYMBOL(ceph_osdc_readpages);
2285 * do a synchronous write on N pages
2287 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2288 struct ceph_file_layout *layout,
2289 struct ceph_snap_context *snapc,
2291 u32 truncate_seq, u64 truncate_size,
2292 struct timespec *mtime,
2293 struct page **pages, int num_pages)
2295 struct ceph_osd_request *req;
2297 int page_align = off & ~PAGE_MASK;
2299 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2300 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2302 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2303 snapc, truncate_seq, truncate_size,
2306 return PTR_ERR(req);
2308 /* it may be a short write due to an object boundary */
2309 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2311 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2313 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2315 rc = ceph_osdc_start_request(osdc, req, true);
2317 rc = ceph_osdc_wait_request(osdc, req);
2319 ceph_osdc_put_request(req);
2322 dout("writepages result %d\n", rc);
2325 EXPORT_SYMBOL(ceph_osdc_writepages);
2328 * handle incoming message
2330 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2332 struct ceph_osd *osd = con->private;
2333 struct ceph_osd_client *osdc;
2334 int type = le16_to_cpu(msg->hdr.type);
2341 case CEPH_MSG_OSD_MAP:
2342 ceph_osdc_handle_map(osdc, msg);
2344 case CEPH_MSG_OSD_OPREPLY:
2345 handle_reply(osdc, msg, con);
2347 case CEPH_MSG_WATCH_NOTIFY:
2348 handle_watch_notify(osdc, msg);
2352 pr_err("received unknown message type %d %s\n", type,
2353 ceph_msg_type_name(type));
2360 * lookup and return message for incoming reply. set up reply message
2363 static struct ceph_msg *get_reply(struct ceph_connection *con,
2364 struct ceph_msg_header *hdr,
2367 struct ceph_osd *osd = con->private;
2368 struct ceph_osd_client *osdc = osd->o_osdc;
2370 struct ceph_osd_request *req;
2371 int front = le32_to_cpu(hdr->front_len);
2372 int data_len = le32_to_cpu(hdr->data_len);
2375 tid = le64_to_cpu(hdr->tid);
2376 mutex_lock(&osdc->request_mutex);
2377 req = __lookup_request(osdc, tid);
2381 dout("get_reply unknown tid %llu from osd%d\n", tid,
2386 if (req->r_reply->con)
2387 dout("%s revoking msg %p from old con %p\n", __func__,
2388 req->r_reply, req->r_reply->con);
2389 ceph_msg_revoke_incoming(req->r_reply);
2391 if (front > req->r_reply->front.iov_len) {
2392 pr_warning("get_reply front %d > preallocated %d\n",
2393 front, (int)req->r_reply->front.iov_len);
2394 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2397 ceph_msg_put(req->r_reply);
2400 m = ceph_msg_get(req->r_reply);
2403 struct ceph_osd_data *osd_data;
2406 * XXX This is assuming there is only one op containing
2407 * XXX page data. Probably OK for reads, but this
2408 * XXX ought to be done more generally.
2410 osd_data = osd_req_op_extent_osd_data(req, 0);
2411 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2412 if (osd_data->pages &&
2413 unlikely(osd_data->length < data_len)) {
2415 pr_warning("tid %lld reply has %d bytes "
2416 "we had only %llu bytes ready\n",
2417 tid, data_len, osd_data->length);
2426 dout("get_reply tid %lld %p\n", tid, m);
2429 mutex_unlock(&osdc->request_mutex);
2434 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2435 struct ceph_msg_header *hdr,
2438 struct ceph_osd *osd = con->private;
2439 int type = le16_to_cpu(hdr->type);
2440 int front = le32_to_cpu(hdr->front_len);
2444 case CEPH_MSG_OSD_MAP:
2445 case CEPH_MSG_WATCH_NOTIFY:
2446 return ceph_msg_new(type, front, GFP_NOFS, false);
2447 case CEPH_MSG_OSD_OPREPLY:
2448 return get_reply(con, hdr, skip);
2450 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2458 * Wrappers to refcount containing ceph_osd struct
2460 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2462 struct ceph_osd *osd = con->private;
2468 static void put_osd_con(struct ceph_connection *con)
2470 struct ceph_osd *osd = con->private;
2478 * Note: returned pointer is the address of a structure that's
2479 * managed separately. Caller must *not* attempt to free it.
2481 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2482 int *proto, int force_new)
2484 struct ceph_osd *o = con->private;
2485 struct ceph_osd_client *osdc = o->o_osdc;
2486 struct ceph_auth_client *ac = osdc->client->monc.auth;
2487 struct ceph_auth_handshake *auth = &o->o_auth;
2489 if (force_new && auth->authorizer) {
2490 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2491 auth->authorizer = NULL;
2493 if (!auth->authorizer) {
2494 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2497 return ERR_PTR(ret);
2499 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2502 return ERR_PTR(ret);
2504 *proto = ac->protocol;
2510 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2512 struct ceph_osd *o = con->private;
2513 struct ceph_osd_client *osdc = o->o_osdc;
2514 struct ceph_auth_client *ac = osdc->client->monc.auth;
2516 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2519 static int invalidate_authorizer(struct ceph_connection *con)
2521 struct ceph_osd *o = con->private;
2522 struct ceph_osd_client *osdc = o->o_osdc;
2523 struct ceph_auth_client *ac = osdc->client->monc.auth;
2525 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2526 return ceph_monc_validate_auth(&osdc->client->monc);
2529 static const struct ceph_connection_operations osd_con_ops = {
2532 .dispatch = dispatch,
2533 .get_authorizer = get_authorizer,
2534 .verify_authorizer_reply = verify_authorizer_reply,
2535 .invalidate_authorizer = invalidate_authorizer,
2536 .alloc_msg = alloc_msg,