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 static struct ceph_osd_data *
125 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
127 BUG_ON(which >= osd_req->r_num_ops);
129 return &osd_req->r_ops[which].raw_data_in;
132 struct ceph_osd_data *
133 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
136 return osd_req_op_data(osd_req, which, extent, osd_data);
138 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
140 struct ceph_osd_data *
141 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
144 return osd_req_op_data(osd_req, which, cls, response_data);
146 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
148 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
149 unsigned int which, struct page **pages,
150 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_raw_data_in(osd_req, which);
156 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
157 pages_from_pool, own_pages);
159 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
161 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
162 unsigned int which, struct page **pages,
163 u64 length, u32 alignment,
164 bool pages_from_pool, bool own_pages)
166 struct ceph_osd_data *osd_data;
168 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
169 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
170 pages_from_pool, own_pages);
172 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
174 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
175 unsigned int which, struct ceph_pagelist *pagelist)
177 struct ceph_osd_data *osd_data;
179 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
180 ceph_osd_data_pagelist_init(osd_data, pagelist);
182 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
185 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
186 unsigned int which, struct bio *bio, size_t bio_length)
188 struct ceph_osd_data *osd_data;
190 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
191 ceph_osd_data_bio_init(osd_data, bio, bio_length);
193 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
194 #endif /* CONFIG_BLOCK */
196 static void osd_req_op_cls_request_info_pagelist(
197 struct ceph_osd_request *osd_req,
198 unsigned int which, struct ceph_pagelist *pagelist)
200 struct ceph_osd_data *osd_data;
202 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
203 ceph_osd_data_pagelist_init(osd_data, pagelist);
206 void osd_req_op_cls_request_data_pagelist(
207 struct ceph_osd_request *osd_req,
208 unsigned int which, struct ceph_pagelist *pagelist)
210 struct ceph_osd_data *osd_data;
212 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
213 ceph_osd_data_pagelist_init(osd_data, pagelist);
215 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
217 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
218 unsigned int which, struct page **pages, u64 length,
219 u32 alignment, bool pages_from_pool, bool own_pages)
221 struct ceph_osd_data *osd_data;
223 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
224 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
225 pages_from_pool, own_pages);
227 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
229 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
230 unsigned int which, struct page **pages, u64 length,
231 u32 alignment, bool pages_from_pool, bool own_pages)
233 struct ceph_osd_data *osd_data;
235 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
236 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
237 pages_from_pool, own_pages);
239 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
241 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
243 switch (osd_data->type) {
244 case CEPH_OSD_DATA_TYPE_NONE:
246 case CEPH_OSD_DATA_TYPE_PAGES:
247 return osd_data->length;
248 case CEPH_OSD_DATA_TYPE_PAGELIST:
249 return (u64)osd_data->pagelist->length;
251 case CEPH_OSD_DATA_TYPE_BIO:
252 return (u64)osd_data->bio_length;
253 #endif /* CONFIG_BLOCK */
255 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
260 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
262 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
265 num_pages = calc_pages_for((u64)osd_data->alignment,
266 (u64)osd_data->length);
267 ceph_release_page_vector(osd_data->pages, num_pages);
269 ceph_osd_data_init(osd_data);
272 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
275 struct ceph_osd_req_op *op;
277 BUG_ON(which >= osd_req->r_num_ops);
278 op = &osd_req->r_ops[which];
281 case CEPH_OSD_OP_READ:
282 case CEPH_OSD_OP_WRITE:
283 ceph_osd_data_release(&op->extent.osd_data);
285 case CEPH_OSD_OP_CALL:
286 ceph_osd_data_release(&op->cls.request_info);
287 ceph_osd_data_release(&op->cls.request_data);
288 ceph_osd_data_release(&op->cls.response_data);
298 void ceph_osdc_release_request(struct kref *kref)
300 struct ceph_osd_request *req;
303 req = container_of(kref, struct ceph_osd_request, r_kref);
305 ceph_msg_put(req->r_request);
307 ceph_msg_revoke_incoming(req->r_reply);
308 ceph_msg_put(req->r_reply);
311 for (which = 0; which < req->r_num_ops; which++)
312 osd_req_op_data_release(req, which);
314 ceph_put_snap_context(req->r_snapc);
316 mempool_free(req, req->r_osdc->req_mempool);
320 EXPORT_SYMBOL(ceph_osdc_release_request);
322 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
323 struct ceph_snap_context *snapc,
324 unsigned int num_ops,
328 struct ceph_osd_request *req;
329 struct ceph_msg *msg;
332 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
333 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
335 msg_size = 4 + 4 + 8 + 8 + 4+8;
336 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
337 msg_size += 1 + 8 + 4 + 4; /* pg_t */
338 msg_size += 4 + MAX_OBJ_NAME_SIZE;
339 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
340 msg_size += 8; /* snapid */
341 msg_size += 8; /* snap_seq */
342 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
346 req = mempool_alloc(osdc->req_mempool, gfp_flags);
347 memset(req, 0, sizeof(*req));
349 req = kzalloc(sizeof(*req), gfp_flags);
355 req->r_mempool = use_mempool;
356 req->r_num_ops = num_ops;
358 kref_init(&req->r_kref);
359 init_completion(&req->r_completion);
360 init_completion(&req->r_safe_completion);
361 RB_CLEAR_NODE(&req->r_node);
362 INIT_LIST_HEAD(&req->r_unsafe_item);
363 INIT_LIST_HEAD(&req->r_linger_item);
364 INIT_LIST_HEAD(&req->r_linger_osd);
365 INIT_LIST_HEAD(&req->r_req_lru_item);
366 INIT_LIST_HEAD(&req->r_osd_item);
368 /* create reply message */
370 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
372 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
373 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
375 ceph_osdc_put_request(req);
380 /* create request message; allow space for oid */
382 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
384 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
386 ceph_osdc_put_request(req);
390 memset(msg->front.iov_base, 0, msg->front.iov_len);
392 req->r_request = msg;
396 EXPORT_SYMBOL(ceph_osdc_alloc_request);
398 static bool osd_req_opcode_valid(u16 opcode)
401 case CEPH_OSD_OP_READ:
402 case CEPH_OSD_OP_STAT:
403 case CEPH_OSD_OP_MAPEXT:
404 case CEPH_OSD_OP_MASKTRUNC:
405 case CEPH_OSD_OP_SPARSE_READ:
406 case CEPH_OSD_OP_NOTIFY:
407 case CEPH_OSD_OP_NOTIFY_ACK:
408 case CEPH_OSD_OP_ASSERT_VER:
409 case CEPH_OSD_OP_WRITE:
410 case CEPH_OSD_OP_WRITEFULL:
411 case CEPH_OSD_OP_TRUNCATE:
412 case CEPH_OSD_OP_ZERO:
413 case CEPH_OSD_OP_DELETE:
414 case CEPH_OSD_OP_APPEND:
415 case CEPH_OSD_OP_STARTSYNC:
416 case CEPH_OSD_OP_SETTRUNC:
417 case CEPH_OSD_OP_TRIMTRUNC:
418 case CEPH_OSD_OP_TMAPUP:
419 case CEPH_OSD_OP_TMAPPUT:
420 case CEPH_OSD_OP_TMAPGET:
421 case CEPH_OSD_OP_CREATE:
422 case CEPH_OSD_OP_ROLLBACK:
423 case CEPH_OSD_OP_WATCH:
424 case CEPH_OSD_OP_OMAPGETKEYS:
425 case CEPH_OSD_OP_OMAPGETVALS:
426 case CEPH_OSD_OP_OMAPGETHEADER:
427 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
428 case CEPH_OSD_OP_OMAPSETVALS:
429 case CEPH_OSD_OP_OMAPSETHEADER:
430 case CEPH_OSD_OP_OMAPCLEAR:
431 case CEPH_OSD_OP_OMAPRMKEYS:
432 case CEPH_OSD_OP_OMAP_CMP:
433 case CEPH_OSD_OP_CLONERANGE:
434 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
435 case CEPH_OSD_OP_SRC_CMPXATTR:
436 case CEPH_OSD_OP_GETXATTR:
437 case CEPH_OSD_OP_GETXATTRS:
438 case CEPH_OSD_OP_CMPXATTR:
439 case CEPH_OSD_OP_SETXATTR:
440 case CEPH_OSD_OP_SETXATTRS:
441 case CEPH_OSD_OP_RESETXATTRS:
442 case CEPH_OSD_OP_RMXATTR:
443 case CEPH_OSD_OP_PULL:
444 case CEPH_OSD_OP_PUSH:
445 case CEPH_OSD_OP_BALANCEREADS:
446 case CEPH_OSD_OP_UNBALANCEREADS:
447 case CEPH_OSD_OP_SCRUB:
448 case CEPH_OSD_OP_SCRUB_RESERVE:
449 case CEPH_OSD_OP_SCRUB_UNRESERVE:
450 case CEPH_OSD_OP_SCRUB_STOP:
451 case CEPH_OSD_OP_SCRUB_MAP:
452 case CEPH_OSD_OP_WRLOCK:
453 case CEPH_OSD_OP_WRUNLOCK:
454 case CEPH_OSD_OP_RDLOCK:
455 case CEPH_OSD_OP_RDUNLOCK:
456 case CEPH_OSD_OP_UPLOCK:
457 case CEPH_OSD_OP_DNLOCK:
458 case CEPH_OSD_OP_CALL:
459 case CEPH_OSD_OP_PGLS:
460 case CEPH_OSD_OP_PGLS_FILTER:
468 * This is an osd op init function for opcodes that have no data or
469 * other information associated with them. It also serves as a
470 * common init routine for all the other init functions, below.
472 static struct ceph_osd_req_op *
473 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
476 struct ceph_osd_req_op *op;
478 BUG_ON(which >= osd_req->r_num_ops);
479 BUG_ON(!osd_req_opcode_valid(opcode));
481 op = &osd_req->r_ops[which];
482 memset(op, 0, sizeof (*op));
488 void osd_req_op_init(struct ceph_osd_request *osd_req,
489 unsigned int which, u16 opcode)
491 (void)_osd_req_op_init(osd_req, which, opcode);
493 EXPORT_SYMBOL(osd_req_op_init);
495 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
496 unsigned int which, u16 opcode,
497 u64 offset, u64 length,
498 u64 truncate_size, u32 truncate_seq)
500 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
501 size_t payload_len = 0;
503 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
505 op->extent.offset = offset;
506 op->extent.length = length;
507 op->extent.truncate_size = truncate_size;
508 op->extent.truncate_seq = truncate_seq;
509 if (opcode == CEPH_OSD_OP_WRITE)
510 payload_len += length;
512 op->payload_len = payload_len;
514 EXPORT_SYMBOL(osd_req_op_extent_init);
516 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
517 unsigned int which, u64 length)
519 struct ceph_osd_req_op *op;
522 BUG_ON(which >= osd_req->r_num_ops);
523 op = &osd_req->r_ops[which];
524 previous = op->extent.length;
526 if (length == previous)
527 return; /* Nothing to do */
528 BUG_ON(length > previous);
530 op->extent.length = length;
531 op->payload_len -= previous - length;
533 EXPORT_SYMBOL(osd_req_op_extent_update);
535 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
536 u16 opcode, const char *class, const char *method)
538 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
539 struct ceph_pagelist *pagelist;
540 size_t payload_len = 0;
543 BUG_ON(opcode != CEPH_OSD_OP_CALL);
545 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
547 ceph_pagelist_init(pagelist);
549 op->cls.class_name = class;
550 size = strlen(class);
551 BUG_ON(size > (size_t) U8_MAX);
552 op->cls.class_len = size;
553 ceph_pagelist_append(pagelist, class, size);
556 op->cls.method_name = method;
557 size = strlen(method);
558 BUG_ON(size > (size_t) U8_MAX);
559 op->cls.method_len = size;
560 ceph_pagelist_append(pagelist, method, size);
563 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
565 op->cls.argc = 0; /* currently unused */
567 op->payload_len = payload_len;
569 EXPORT_SYMBOL(osd_req_op_cls_init);
571 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
572 unsigned int which, u16 opcode,
573 u64 cookie, u64 version, int flag)
575 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
577 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
579 op->watch.cookie = cookie;
580 op->watch.ver = version;
581 if (opcode == CEPH_OSD_OP_WATCH && flag)
582 op->watch.flag = (u8)1;
584 EXPORT_SYMBOL(osd_req_op_watch_init);
586 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
587 struct ceph_osd_data *osd_data)
589 u64 length = ceph_osd_data_length(osd_data);
591 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
592 BUG_ON(length > (u64) SIZE_MAX);
594 ceph_msg_data_add_pages(msg, osd_data->pages,
595 length, osd_data->alignment);
596 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
598 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
600 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
601 ceph_msg_data_add_bio(msg, osd_data->bio, length);
604 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
608 static u64 osd_req_encode_op(struct ceph_osd_request *req,
609 struct ceph_osd_op *dst, unsigned int which)
611 struct ceph_osd_req_op *src;
612 struct ceph_osd_data *osd_data;
613 u64 request_data_len = 0;
616 BUG_ON(which >= req->r_num_ops);
617 src = &req->r_ops[which];
618 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
619 pr_err("unrecognized osd opcode %d\n", src->op);
625 case CEPH_OSD_OP_STAT:
626 osd_data = &src->raw_data_in;
627 ceph_osdc_msg_data_add(req->r_reply, osd_data);
629 case CEPH_OSD_OP_READ:
630 case CEPH_OSD_OP_WRITE:
631 if (src->op == CEPH_OSD_OP_WRITE)
632 request_data_len = src->extent.length;
633 dst->extent.offset = cpu_to_le64(src->extent.offset);
634 dst->extent.length = cpu_to_le64(src->extent.length);
635 dst->extent.truncate_size =
636 cpu_to_le64(src->extent.truncate_size);
637 dst->extent.truncate_seq =
638 cpu_to_le32(src->extent.truncate_seq);
639 osd_data = &src->extent.osd_data;
640 if (src->op == CEPH_OSD_OP_WRITE)
641 ceph_osdc_msg_data_add(req->r_request, osd_data);
643 ceph_osdc_msg_data_add(req->r_reply, osd_data);
645 case CEPH_OSD_OP_CALL:
646 dst->cls.class_len = src->cls.class_len;
647 dst->cls.method_len = src->cls.method_len;
648 osd_data = &src->cls.request_info;
649 ceph_osdc_msg_data_add(req->r_request, osd_data);
650 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
651 request_data_len = osd_data->pagelist->length;
653 osd_data = &src->cls.request_data;
654 data_length = ceph_osd_data_length(osd_data);
656 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
657 dst->cls.indata_len = cpu_to_le32(data_length);
658 ceph_osdc_msg_data_add(req->r_request, osd_data);
659 src->payload_len += data_length;
660 request_data_len += data_length;
662 osd_data = &src->cls.response_data;
663 ceph_osdc_msg_data_add(req->r_reply, osd_data);
665 case CEPH_OSD_OP_STARTSYNC:
667 case CEPH_OSD_OP_NOTIFY_ACK:
668 case CEPH_OSD_OP_WATCH:
669 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
670 dst->watch.ver = cpu_to_le64(src->watch.ver);
671 dst->watch.flag = src->watch.flag;
674 pr_err("unsupported osd opcode %s\n",
675 ceph_osd_op_name(src->op));
680 dst->op = cpu_to_le16(src->op);
681 dst->payload_len = cpu_to_le32(src->payload_len);
683 return request_data_len;
687 * build new request AND message, calculate layout, and adjust file
690 * if the file was recently truncated, we include information about its
691 * old and new size so that the object can be updated appropriately. (we
692 * avoid synchronously deleting truncated objects because it's slow.)
694 * if @do_sync, include a 'startsync' command so that the osd will flush
697 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
698 struct ceph_file_layout *layout,
699 struct ceph_vino vino,
700 u64 off, u64 *plen, int num_ops,
701 int opcode, int flags,
702 struct ceph_snap_context *snapc,
707 struct ceph_osd_request *req;
715 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
717 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
720 return ERR_PTR(-ENOMEM);
722 req->r_flags = flags;
724 /* calculate max write size */
725 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
727 ceph_osdc_put_request(req);
731 object_size = le32_to_cpu(layout->fl_object_size);
732 object_base = off - objoff;
733 if (truncate_size <= object_base) {
736 truncate_size -= object_base;
737 if (truncate_size > object_size)
738 truncate_size = object_size;
741 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
742 truncate_size, truncate_seq);
745 * A second op in the ops array means the caller wants to
746 * also issue a include a 'startsync' command so that the
747 * osd will flush data quickly.
750 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
752 req->r_file_layout = *layout; /* keep a copy */
754 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
756 req->r_oid_len = strlen(req->r_oid);
760 EXPORT_SYMBOL(ceph_osdc_new_request);
763 * We keep osd requests in an rbtree, sorted by ->r_tid.
765 static void __insert_request(struct ceph_osd_client *osdc,
766 struct ceph_osd_request *new)
768 struct rb_node **p = &osdc->requests.rb_node;
769 struct rb_node *parent = NULL;
770 struct ceph_osd_request *req = NULL;
774 req = rb_entry(parent, struct ceph_osd_request, r_node);
775 if (new->r_tid < req->r_tid)
777 else if (new->r_tid > req->r_tid)
783 rb_link_node(&new->r_node, parent, p);
784 rb_insert_color(&new->r_node, &osdc->requests);
787 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
790 struct ceph_osd_request *req;
791 struct rb_node *n = osdc->requests.rb_node;
794 req = rb_entry(n, struct ceph_osd_request, r_node);
795 if (tid < req->r_tid)
797 else if (tid > req->r_tid)
805 static struct ceph_osd_request *
806 __lookup_request_ge(struct ceph_osd_client *osdc,
809 struct ceph_osd_request *req;
810 struct rb_node *n = osdc->requests.rb_node;
813 req = rb_entry(n, struct ceph_osd_request, r_node);
814 if (tid < req->r_tid) {
818 } else if (tid > req->r_tid) {
828 * Resubmit requests pending on the given osd.
830 static void __kick_osd_requests(struct ceph_osd_client *osdc,
831 struct ceph_osd *osd)
833 struct ceph_osd_request *req, *nreq;
837 dout("__kick_osd_requests osd%d\n", osd->o_osd);
838 err = __reset_osd(osdc, osd);
842 * Build up a list of requests to resend by traversing the
843 * osd's list of requests. Requests for a given object are
844 * sent in tid order, and that is also the order they're
845 * kept on this list. Therefore all requests that are in
846 * flight will be found first, followed by all requests that
847 * have not yet been sent. And to resend requests while
848 * preserving this order we will want to put any sent
849 * requests back on the front of the osd client's unsent
852 * So we build a separate ordered list of already-sent
853 * requests for the affected osd and splice it onto the
854 * front of the osd client's unsent list. Once we've seen a
855 * request that has not yet been sent we're done. Those
856 * requests are already sitting right where they belong.
858 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
861 list_move_tail(&req->r_req_lru_item, &resend);
862 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
865 req->r_flags |= CEPH_OSD_FLAG_RETRY;
867 list_splice(&resend, &osdc->req_unsent);
870 * Linger requests are re-registered before sending, which
871 * sets up a new tid for each. We add them to the unsent
872 * list at the end to keep things in tid order.
874 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
877 * reregister request prior to unregistering linger so
878 * that r_osd is preserved.
880 BUG_ON(!list_empty(&req->r_req_lru_item));
881 __register_request(osdc, req);
882 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
883 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
884 __unregister_linger_request(osdc, req);
885 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
891 * If the osd connection drops, we need to resubmit all requests.
893 static void osd_reset(struct ceph_connection *con)
895 struct ceph_osd *osd = con->private;
896 struct ceph_osd_client *osdc;
900 dout("osd_reset osd%d\n", osd->o_osd);
902 down_read(&osdc->map_sem);
903 mutex_lock(&osdc->request_mutex);
904 __kick_osd_requests(osdc, osd);
906 mutex_unlock(&osdc->request_mutex);
907 up_read(&osdc->map_sem);
911 * Track open sessions with osds.
913 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
915 struct ceph_osd *osd;
917 osd = kzalloc(sizeof(*osd), GFP_NOFS);
921 atomic_set(&osd->o_ref, 1);
924 RB_CLEAR_NODE(&osd->o_node);
925 INIT_LIST_HEAD(&osd->o_requests);
926 INIT_LIST_HEAD(&osd->o_linger_requests);
927 INIT_LIST_HEAD(&osd->o_osd_lru);
928 osd->o_incarnation = 1;
930 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
932 INIT_LIST_HEAD(&osd->o_keepalive_item);
936 static struct ceph_osd *get_osd(struct ceph_osd *osd)
938 if (atomic_inc_not_zero(&osd->o_ref)) {
939 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
940 atomic_read(&osd->o_ref));
943 dout("get_osd %p FAIL\n", osd);
948 static void put_osd(struct ceph_osd *osd)
950 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
951 atomic_read(&osd->o_ref) - 1);
952 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
953 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
955 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
961 * remove an osd from our map
963 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
965 dout("__remove_osd %p\n", osd);
966 BUG_ON(!list_empty(&osd->o_requests));
967 rb_erase(&osd->o_node, &osdc->osds);
968 list_del_init(&osd->o_osd_lru);
969 ceph_con_close(&osd->o_con);
973 static void remove_all_osds(struct ceph_osd_client *osdc)
975 dout("%s %p\n", __func__, osdc);
976 mutex_lock(&osdc->request_mutex);
977 while (!RB_EMPTY_ROOT(&osdc->osds)) {
978 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
979 struct ceph_osd, o_node);
980 __remove_osd(osdc, osd);
982 mutex_unlock(&osdc->request_mutex);
985 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
986 struct ceph_osd *osd)
988 dout("__move_osd_to_lru %p\n", osd);
989 BUG_ON(!list_empty(&osd->o_osd_lru));
990 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
991 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
994 static void __remove_osd_from_lru(struct ceph_osd *osd)
996 dout("__remove_osd_from_lru %p\n", osd);
997 if (!list_empty(&osd->o_osd_lru))
998 list_del_init(&osd->o_osd_lru);
1001 static void remove_old_osds(struct ceph_osd_client *osdc)
1003 struct ceph_osd *osd, *nosd;
1005 dout("__remove_old_osds %p\n", osdc);
1006 mutex_lock(&osdc->request_mutex);
1007 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1008 if (time_before(jiffies, osd->lru_ttl))
1010 __remove_osd(osdc, osd);
1012 mutex_unlock(&osdc->request_mutex);
1018 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1020 struct ceph_entity_addr *peer_addr;
1022 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1023 if (list_empty(&osd->o_requests) &&
1024 list_empty(&osd->o_linger_requests)) {
1025 __remove_osd(osdc, osd);
1030 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1031 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1032 !ceph_con_opened(&osd->o_con)) {
1033 struct ceph_osd_request *req;
1035 dout(" osd addr hasn't changed and connection never opened,"
1036 " letting msgr retry");
1037 /* touch each r_stamp for handle_timeout()'s benfit */
1038 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1039 req->r_stamp = jiffies;
1044 ceph_con_close(&osd->o_con);
1045 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1046 osd->o_incarnation++;
1051 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1053 struct rb_node **p = &osdc->osds.rb_node;
1054 struct rb_node *parent = NULL;
1055 struct ceph_osd *osd = NULL;
1057 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1060 osd = rb_entry(parent, struct ceph_osd, o_node);
1061 if (new->o_osd < osd->o_osd)
1063 else if (new->o_osd > osd->o_osd)
1064 p = &(*p)->rb_right;
1069 rb_link_node(&new->o_node, parent, p);
1070 rb_insert_color(&new->o_node, &osdc->osds);
1073 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1075 struct ceph_osd *osd;
1076 struct rb_node *n = osdc->osds.rb_node;
1079 osd = rb_entry(n, struct ceph_osd, o_node);
1082 else if (o > osd->o_osd)
1090 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1092 schedule_delayed_work(&osdc->timeout_work,
1093 osdc->client->options->osd_keepalive_timeout * HZ);
1096 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1098 cancel_delayed_work(&osdc->timeout_work);
1102 * Register request, assign tid. If this is the first request, set up
1103 * the timeout event.
1105 static void __register_request(struct ceph_osd_client *osdc,
1106 struct ceph_osd_request *req)
1108 req->r_tid = ++osdc->last_tid;
1109 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1110 dout("__register_request %p tid %lld\n", req, req->r_tid);
1111 __insert_request(osdc, req);
1112 ceph_osdc_get_request(req);
1113 osdc->num_requests++;
1114 if (osdc->num_requests == 1) {
1115 dout(" first request, scheduling timeout\n");
1116 __schedule_osd_timeout(osdc);
1121 * called under osdc->request_mutex
1123 static void __unregister_request(struct ceph_osd_client *osdc,
1124 struct ceph_osd_request *req)
1126 if (RB_EMPTY_NODE(&req->r_node)) {
1127 dout("__unregister_request %p tid %lld not registered\n",
1132 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1133 rb_erase(&req->r_node, &osdc->requests);
1134 osdc->num_requests--;
1137 /* make sure the original request isn't in flight. */
1138 ceph_msg_revoke(req->r_request);
1140 list_del_init(&req->r_osd_item);
1141 if (list_empty(&req->r_osd->o_requests) &&
1142 list_empty(&req->r_osd->o_linger_requests)) {
1143 dout("moving osd to %p lru\n", req->r_osd);
1144 __move_osd_to_lru(osdc, req->r_osd);
1146 if (list_empty(&req->r_linger_item))
1150 list_del_init(&req->r_req_lru_item);
1151 ceph_osdc_put_request(req);
1153 if (osdc->num_requests == 0) {
1154 dout(" no requests, canceling timeout\n");
1155 __cancel_osd_timeout(osdc);
1160 * Cancel a previously queued request message
1162 static void __cancel_request(struct ceph_osd_request *req)
1164 if (req->r_sent && req->r_osd) {
1165 ceph_msg_revoke(req->r_request);
1170 static void __register_linger_request(struct ceph_osd_client *osdc,
1171 struct ceph_osd_request *req)
1173 dout("__register_linger_request %p\n", req);
1174 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1176 list_add_tail(&req->r_linger_osd,
1177 &req->r_osd->o_linger_requests);
1180 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1181 struct ceph_osd_request *req)
1183 dout("__unregister_linger_request %p\n", req);
1184 list_del_init(&req->r_linger_item);
1186 list_del_init(&req->r_linger_osd);
1188 if (list_empty(&req->r_osd->o_requests) &&
1189 list_empty(&req->r_osd->o_linger_requests)) {
1190 dout("moving osd to %p lru\n", req->r_osd);
1191 __move_osd_to_lru(osdc, req->r_osd);
1193 if (list_empty(&req->r_osd_item))
1198 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1199 struct ceph_osd_request *req)
1201 mutex_lock(&osdc->request_mutex);
1202 if (req->r_linger) {
1203 __unregister_linger_request(osdc, req);
1204 ceph_osdc_put_request(req);
1206 mutex_unlock(&osdc->request_mutex);
1208 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1210 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1211 struct ceph_osd_request *req)
1213 if (!req->r_linger) {
1214 dout("set_request_linger %p\n", req);
1217 * caller is now responsible for calling
1218 * unregister_linger_request
1220 ceph_osdc_get_request(req);
1223 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1226 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1227 * (as needed), and set the request r_osd appropriately. If there is
1228 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1229 * (unsent, homeless) or leave on in-flight lru.
1231 * Return 0 if unchanged, 1 if changed, or negative on error.
1233 * Caller should hold map_sem for read and request_mutex.
1235 static int __map_request(struct ceph_osd_client *osdc,
1236 struct ceph_osd_request *req, int force_resend)
1238 struct ceph_pg pgid;
1239 int acting[CEPH_PG_MAX_SIZE];
1240 int o = -1, num = 0;
1243 dout("map_request %p tid %lld\n", req, req->r_tid);
1244 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1245 ceph_file_layout_pg_pool(req->r_file_layout));
1247 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1252 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1258 if ((!force_resend &&
1259 req->r_osd && req->r_osd->o_osd == o &&
1260 req->r_sent >= req->r_osd->o_incarnation &&
1261 req->r_num_pg_osds == num &&
1262 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1263 (req->r_osd == NULL && o == -1))
1264 return 0; /* no change */
1266 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1267 req->r_tid, pgid.pool, pgid.seed, o,
1268 req->r_osd ? req->r_osd->o_osd : -1);
1270 /* record full pg acting set */
1271 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1272 req->r_num_pg_osds = num;
1275 __cancel_request(req);
1276 list_del_init(&req->r_osd_item);
1280 req->r_osd = __lookup_osd(osdc, o);
1281 if (!req->r_osd && o >= 0) {
1283 req->r_osd = create_osd(osdc, o);
1285 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1289 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1290 __insert_osd(osdc, req->r_osd);
1292 ceph_con_open(&req->r_osd->o_con,
1293 CEPH_ENTITY_TYPE_OSD, o,
1294 &osdc->osdmap->osd_addr[o]);
1298 __remove_osd_from_lru(req->r_osd);
1299 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1300 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1302 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1304 err = 1; /* osd or pg changed */
1311 * caller should hold map_sem (for read) and request_mutex
1313 static void __send_request(struct ceph_osd_client *osdc,
1314 struct ceph_osd_request *req)
1318 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1319 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1320 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1322 /* fill in message content that changes each time we send it */
1323 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1324 put_unaligned_le32(req->r_flags, req->r_request_flags);
1325 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1326 p = req->r_request_pgid;
1327 ceph_encode_64(&p, req->r_pgid.pool);
1328 ceph_encode_32(&p, req->r_pgid.seed);
1329 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1330 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1331 sizeof(req->r_reassert_version));
1333 req->r_stamp = jiffies;
1334 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1336 ceph_msg_get(req->r_request); /* send consumes a ref */
1338 /* Mark the request unsafe if this is the first timet's being sent. */
1340 if (!req->r_sent && req->r_unsafe_callback)
1341 req->r_unsafe_callback(req, true);
1342 req->r_sent = req->r_osd->o_incarnation;
1344 ceph_con_send(&req->r_osd->o_con, req->r_request);
1348 * Send any requests in the queue (req_unsent).
1350 static void __send_queued(struct ceph_osd_client *osdc)
1352 struct ceph_osd_request *req, *tmp;
1354 dout("__send_queued\n");
1355 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1356 __send_request(osdc, req);
1360 * Timeout callback, called every N seconds when 1 or more osd
1361 * requests has been active for more than N seconds. When this
1362 * happens, we ping all OSDs with requests who have timed out to
1363 * ensure any communications channel reset is detected. Reset the
1364 * request timeouts another N seconds in the future as we go.
1365 * Reschedule the timeout event another N seconds in future (unless
1366 * there are no open requests).
1368 static void handle_timeout(struct work_struct *work)
1370 struct ceph_osd_client *osdc =
1371 container_of(work, struct ceph_osd_client, timeout_work.work);
1372 struct ceph_osd_request *req;
1373 struct ceph_osd *osd;
1374 unsigned long keepalive =
1375 osdc->client->options->osd_keepalive_timeout * HZ;
1376 struct list_head slow_osds;
1378 down_read(&osdc->map_sem);
1380 ceph_monc_request_next_osdmap(&osdc->client->monc);
1382 mutex_lock(&osdc->request_mutex);
1385 * ping osds that are a bit slow. this ensures that if there
1386 * is a break in the TCP connection we will notice, and reopen
1387 * a connection with that osd (from the fault callback).
1389 INIT_LIST_HEAD(&slow_osds);
1390 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1391 if (time_before(jiffies, req->r_stamp + keepalive))
1396 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1397 req->r_tid, osd->o_osd);
1398 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1400 while (!list_empty(&slow_osds)) {
1401 osd = list_entry(slow_osds.next, struct ceph_osd,
1403 list_del_init(&osd->o_keepalive_item);
1404 ceph_con_keepalive(&osd->o_con);
1407 __schedule_osd_timeout(osdc);
1408 __send_queued(osdc);
1409 mutex_unlock(&osdc->request_mutex);
1410 up_read(&osdc->map_sem);
1413 static void handle_osds_timeout(struct work_struct *work)
1415 struct ceph_osd_client *osdc =
1416 container_of(work, struct ceph_osd_client,
1417 osds_timeout_work.work);
1418 unsigned long delay =
1419 osdc->client->options->osd_idle_ttl * HZ >> 2;
1421 dout("osds timeout\n");
1422 down_read(&osdc->map_sem);
1423 remove_old_osds(osdc);
1424 up_read(&osdc->map_sem);
1426 schedule_delayed_work(&osdc->osds_timeout_work,
1427 round_jiffies_relative(delay));
1430 static void complete_request(struct ceph_osd_request *req)
1432 if (req->r_unsafe_callback)
1433 req->r_unsafe_callback(req, false);
1434 complete_all(&req->r_safe_completion); /* fsync waiter */
1438 * handle osd op reply. either call the callback if it is specified,
1439 * or do the completion to wake up the waiting thread.
1441 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1442 struct ceph_connection *con)
1445 struct ceph_osd_request *req;
1448 unsigned int numops;
1449 int payload_len, flags;
1455 u64 reassert_version;
1457 int already_completed;
1461 tid = le64_to_cpu(msg->hdr.tid);
1462 dout("handle_reply %p tid %llu\n", msg, tid);
1464 p = msg->front.iov_base;
1465 end = p + msg->front.iov_len;
1467 ceph_decode_need(&p, end, 4, bad);
1468 object_len = ceph_decode_32(&p);
1469 ceph_decode_need(&p, end, object_len, bad);
1472 err = ceph_decode_pgid(&p, end, &pg);
1476 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1477 flags = ceph_decode_64(&p);
1478 result = ceph_decode_32(&p);
1479 reassert_epoch = ceph_decode_32(&p);
1480 reassert_version = ceph_decode_64(&p);
1481 osdmap_epoch = ceph_decode_32(&p);
1484 mutex_lock(&osdc->request_mutex);
1485 req = __lookup_request(osdc, tid);
1487 dout("handle_reply tid %llu dne\n", tid);
1490 ceph_osdc_get_request(req);
1492 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1495 ceph_decode_need(&p, end, 4, bad);
1496 numops = ceph_decode_32(&p);
1497 if (numops > CEPH_OSD_MAX_OP)
1499 if (numops != req->r_num_ops)
1502 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1503 for (i = 0; i < numops; i++) {
1504 struct ceph_osd_op *op = p;
1507 len = le32_to_cpu(op->payload_len);
1508 req->r_reply_op_len[i] = len;
1509 dout(" op %d has %d bytes\n", i, len);
1513 bytes = le32_to_cpu(msg->hdr.data_len);
1514 if (payload_len != bytes) {
1515 pr_warning("sum of op payload lens %d != data_len %d",
1516 payload_len, bytes);
1520 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1521 retry_attempt = ceph_decode_32(&p);
1522 for (i = 0; i < numops; i++)
1523 req->r_reply_op_result[i] = ceph_decode_32(&p);
1525 if (!req->r_got_reply) {
1527 req->r_result = result;
1528 dout("handle_reply result %d bytes %d\n", req->r_result,
1530 if (req->r_result == 0)
1531 req->r_result = bytes;
1533 /* in case this is a write and we need to replay, */
1534 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1535 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1537 req->r_got_reply = 1;
1538 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1539 dout("handle_reply tid %llu dup ack\n", tid);
1540 mutex_unlock(&osdc->request_mutex);
1544 dout("handle_reply tid %llu flags %d\n", tid, flags);
1546 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1547 __register_linger_request(osdc, req);
1549 /* either this is a read, or we got the safe response */
1551 (flags & CEPH_OSD_FLAG_ONDISK) ||
1552 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1553 __unregister_request(osdc, req);
1555 already_completed = req->r_completed;
1556 req->r_completed = 1;
1557 mutex_unlock(&osdc->request_mutex);
1558 if (already_completed)
1561 if (req->r_callback)
1562 req->r_callback(req, msg);
1564 complete_all(&req->r_completion);
1566 if (flags & CEPH_OSD_FLAG_ONDISK)
1567 complete_request(req);
1570 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1571 ceph_osdc_put_request(req);
1575 ceph_osdc_put_request(req);
1577 mutex_unlock(&osdc->request_mutex);
1579 pr_err("corrupt osd_op_reply got %d %d\n",
1580 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1584 static void reset_changed_osds(struct ceph_osd_client *osdc)
1586 struct rb_node *p, *n;
1588 for (p = rb_first(&osdc->osds); p; p = n) {
1589 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1592 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1593 memcmp(&osd->o_con.peer_addr,
1594 ceph_osd_addr(osdc->osdmap,
1596 sizeof(struct ceph_entity_addr)) != 0)
1597 __reset_osd(osdc, osd);
1602 * Requeue requests whose mapping to an OSD has changed. If requests map to
1603 * no osd, request a new map.
1605 * Caller should hold map_sem for read.
1607 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1609 struct ceph_osd_request *req, *nreq;
1614 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1615 mutex_lock(&osdc->request_mutex);
1616 for (p = rb_first(&osdc->requests); p; ) {
1617 req = rb_entry(p, struct ceph_osd_request, r_node);
1621 * For linger requests that have not yet been
1622 * registered, move them to the linger list; they'll
1623 * be sent to the osd in the loop below. Unregister
1624 * the request before re-registering it as a linger
1625 * request to ensure the __map_request() below
1626 * will decide it needs to be sent.
1628 if (req->r_linger && list_empty(&req->r_linger_item)) {
1629 dout("%p tid %llu restart on osd%d\n",
1631 req->r_osd ? req->r_osd->o_osd : -1);
1632 __unregister_request(osdc, req);
1633 __register_linger_request(osdc, req);
1637 err = __map_request(osdc, req, force_resend);
1639 continue; /* error */
1640 if (req->r_osd == NULL) {
1641 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1642 needmap++; /* request a newer map */
1643 } else if (err > 0) {
1644 if (!req->r_linger) {
1645 dout("%p tid %llu requeued on osd%d\n", req,
1647 req->r_osd ? req->r_osd->o_osd : -1);
1648 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1653 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1655 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1657 err = __map_request(osdc, req, force_resend);
1658 dout("__map_request returned %d\n", err);
1660 continue; /* no change and no osd was specified */
1662 continue; /* hrm! */
1663 if (req->r_osd == NULL) {
1664 dout("tid %llu maps to no valid osd\n", req->r_tid);
1665 needmap++; /* request a newer map */
1669 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1670 req->r_osd ? req->r_osd->o_osd : -1);
1671 __register_request(osdc, req);
1672 __unregister_linger_request(osdc, req);
1674 mutex_unlock(&osdc->request_mutex);
1677 dout("%d requests for down osds, need new map\n", needmap);
1678 ceph_monc_request_next_osdmap(&osdc->client->monc);
1680 reset_changed_osds(osdc);
1685 * Process updated osd map.
1687 * The message contains any number of incremental and full maps, normally
1688 * indicating some sort of topology change in the cluster. Kick requests
1689 * off to different OSDs as needed.
1691 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1693 void *p, *end, *next;
1694 u32 nr_maps, maplen;
1696 struct ceph_osdmap *newmap = NULL, *oldmap;
1698 struct ceph_fsid fsid;
1700 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1701 p = msg->front.iov_base;
1702 end = p + msg->front.iov_len;
1705 ceph_decode_need(&p, end, sizeof(fsid), bad);
1706 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1707 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1710 down_write(&osdc->map_sem);
1712 /* incremental maps */
1713 ceph_decode_32_safe(&p, end, nr_maps, bad);
1714 dout(" %d inc maps\n", nr_maps);
1715 while (nr_maps > 0) {
1716 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1717 epoch = ceph_decode_32(&p);
1718 maplen = ceph_decode_32(&p);
1719 ceph_decode_need(&p, end, maplen, bad);
1721 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1722 dout("applying incremental map %u len %d\n",
1724 newmap = osdmap_apply_incremental(&p, next,
1726 &osdc->client->msgr);
1727 if (IS_ERR(newmap)) {
1728 err = PTR_ERR(newmap);
1732 if (newmap != osdc->osdmap) {
1733 ceph_osdmap_destroy(osdc->osdmap);
1734 osdc->osdmap = newmap;
1736 kick_requests(osdc, 0);
1738 dout("ignoring incremental map %u len %d\n",
1748 ceph_decode_32_safe(&p, end, nr_maps, bad);
1749 dout(" %d full maps\n", nr_maps);
1751 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1752 epoch = ceph_decode_32(&p);
1753 maplen = ceph_decode_32(&p);
1754 ceph_decode_need(&p, end, maplen, bad);
1756 dout("skipping non-latest full map %u len %d\n",
1758 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1759 dout("skipping full map %u len %d, "
1760 "older than our %u\n", epoch, maplen,
1761 osdc->osdmap->epoch);
1763 int skipped_map = 0;
1765 dout("taking full map %u len %d\n", epoch, maplen);
1766 newmap = osdmap_decode(&p, p+maplen);
1767 if (IS_ERR(newmap)) {
1768 err = PTR_ERR(newmap);
1772 oldmap = osdc->osdmap;
1773 osdc->osdmap = newmap;
1775 if (oldmap->epoch + 1 < newmap->epoch)
1777 ceph_osdmap_destroy(oldmap);
1779 kick_requests(osdc, skipped_map);
1786 downgrade_write(&osdc->map_sem);
1787 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1790 * subscribe to subsequent osdmap updates if full to ensure
1791 * we find out when we are no longer full and stop returning
1794 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1795 ceph_monc_request_next_osdmap(&osdc->client->monc);
1797 mutex_lock(&osdc->request_mutex);
1798 __send_queued(osdc);
1799 mutex_unlock(&osdc->request_mutex);
1800 up_read(&osdc->map_sem);
1801 wake_up_all(&osdc->client->auth_wq);
1805 pr_err("osdc handle_map corrupt msg\n");
1807 up_write(&osdc->map_sem);
1812 * watch/notify callback event infrastructure
1814 * These callbacks are used both for watch and notify operations.
1816 static void __release_event(struct kref *kref)
1818 struct ceph_osd_event *event =
1819 container_of(kref, struct ceph_osd_event, kref);
1821 dout("__release_event %p\n", event);
1825 static void get_event(struct ceph_osd_event *event)
1827 kref_get(&event->kref);
1830 void ceph_osdc_put_event(struct ceph_osd_event *event)
1832 kref_put(&event->kref, __release_event);
1834 EXPORT_SYMBOL(ceph_osdc_put_event);
1836 static void __insert_event(struct ceph_osd_client *osdc,
1837 struct ceph_osd_event *new)
1839 struct rb_node **p = &osdc->event_tree.rb_node;
1840 struct rb_node *parent = NULL;
1841 struct ceph_osd_event *event = NULL;
1845 event = rb_entry(parent, struct ceph_osd_event, node);
1846 if (new->cookie < event->cookie)
1848 else if (new->cookie > event->cookie)
1849 p = &(*p)->rb_right;
1854 rb_link_node(&new->node, parent, p);
1855 rb_insert_color(&new->node, &osdc->event_tree);
1858 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1861 struct rb_node **p = &osdc->event_tree.rb_node;
1862 struct rb_node *parent = NULL;
1863 struct ceph_osd_event *event = NULL;
1867 event = rb_entry(parent, struct ceph_osd_event, node);
1868 if (cookie < event->cookie)
1870 else if (cookie > event->cookie)
1871 p = &(*p)->rb_right;
1878 static void __remove_event(struct ceph_osd_event *event)
1880 struct ceph_osd_client *osdc = event->osdc;
1882 if (!RB_EMPTY_NODE(&event->node)) {
1883 dout("__remove_event removed %p\n", event);
1884 rb_erase(&event->node, &osdc->event_tree);
1885 ceph_osdc_put_event(event);
1887 dout("__remove_event didn't remove %p\n", event);
1891 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1892 void (*event_cb)(u64, u64, u8, void *),
1893 void *data, struct ceph_osd_event **pevent)
1895 struct ceph_osd_event *event;
1897 event = kmalloc(sizeof(*event), GFP_NOIO);
1901 dout("create_event %p\n", event);
1902 event->cb = event_cb;
1903 event->one_shot = 0;
1906 INIT_LIST_HEAD(&event->osd_node);
1907 RB_CLEAR_NODE(&event->node);
1908 kref_init(&event->kref); /* one ref for us */
1909 kref_get(&event->kref); /* one ref for the caller */
1911 spin_lock(&osdc->event_lock);
1912 event->cookie = ++osdc->event_count;
1913 __insert_event(osdc, event);
1914 spin_unlock(&osdc->event_lock);
1919 EXPORT_SYMBOL(ceph_osdc_create_event);
1921 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1923 struct ceph_osd_client *osdc = event->osdc;
1925 dout("cancel_event %p\n", event);
1926 spin_lock(&osdc->event_lock);
1927 __remove_event(event);
1928 spin_unlock(&osdc->event_lock);
1929 ceph_osdc_put_event(event); /* caller's */
1931 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1934 static void do_event_work(struct work_struct *work)
1936 struct ceph_osd_event_work *event_work =
1937 container_of(work, struct ceph_osd_event_work, work);
1938 struct ceph_osd_event *event = event_work->event;
1939 u64 ver = event_work->ver;
1940 u64 notify_id = event_work->notify_id;
1941 u8 opcode = event_work->opcode;
1943 dout("do_event_work completing %p\n", event);
1944 event->cb(ver, notify_id, opcode, event->data);
1945 dout("do_event_work completed %p\n", event);
1946 ceph_osdc_put_event(event);
1952 * Process osd watch notifications
1954 static void handle_watch_notify(struct ceph_osd_client *osdc,
1955 struct ceph_msg *msg)
1959 u64 cookie, ver, notify_id;
1961 struct ceph_osd_event *event;
1962 struct ceph_osd_event_work *event_work;
1964 p = msg->front.iov_base;
1965 end = p + msg->front.iov_len;
1967 ceph_decode_8_safe(&p, end, proto_ver, bad);
1968 ceph_decode_8_safe(&p, end, opcode, bad);
1969 ceph_decode_64_safe(&p, end, cookie, bad);
1970 ceph_decode_64_safe(&p, end, ver, bad);
1971 ceph_decode_64_safe(&p, end, notify_id, bad);
1973 spin_lock(&osdc->event_lock);
1974 event = __find_event(osdc, cookie);
1976 BUG_ON(event->one_shot);
1979 spin_unlock(&osdc->event_lock);
1980 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1981 cookie, ver, event);
1983 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1985 dout("ERROR: could not allocate event_work\n");
1988 INIT_WORK(&event_work->work, do_event_work);
1989 event_work->event = event;
1990 event_work->ver = ver;
1991 event_work->notify_id = notify_id;
1992 event_work->opcode = opcode;
1993 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1994 dout("WARNING: failed to queue notify event work\n");
2002 ceph_osdc_put_event(event);
2006 pr_err("osdc handle_watch_notify corrupt msg\n");
2011 * build new request AND message
2014 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2015 struct ceph_snap_context *snapc, u64 snap_id,
2016 struct timespec *mtime)
2018 struct ceph_msg *msg = req->r_request;
2021 int flags = req->r_flags;
2025 req->r_snapid = snap_id;
2026 req->r_snapc = ceph_get_snap_context(snapc);
2028 /* encode request */
2029 msg->hdr.version = cpu_to_le16(4);
2031 p = msg->front.iov_base;
2032 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2033 req->r_request_osdmap_epoch = p;
2035 req->r_request_flags = p;
2037 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2038 ceph_encode_timespec(p, mtime);
2039 p += sizeof(struct ceph_timespec);
2040 req->r_request_reassert_version = p;
2041 p += sizeof(struct ceph_eversion); /* will get filled in */
2044 ceph_encode_8(&p, 4);
2045 ceph_encode_8(&p, 4);
2046 ceph_encode_32(&p, 8 + 4 + 4);
2047 req->r_request_pool = p;
2049 ceph_encode_32(&p, -1); /* preferred */
2050 ceph_encode_32(&p, 0); /* key len */
2052 ceph_encode_8(&p, 1);
2053 req->r_request_pgid = p;
2055 ceph_encode_32(&p, -1); /* preferred */
2058 ceph_encode_32(&p, req->r_oid_len);
2059 memcpy(p, req->r_oid, req->r_oid_len);
2060 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2061 p += req->r_oid_len;
2063 /* ops--can imply data */
2064 ceph_encode_16(&p, (u16)req->r_num_ops);
2066 for (i = 0; i < req->r_num_ops; i++) {
2067 data_len += osd_req_encode_op(req, p, i);
2068 p += sizeof(struct ceph_osd_op);
2072 ceph_encode_64(&p, req->r_snapid);
2073 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2074 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2076 for (i = 0; i < snapc->num_snaps; i++) {
2077 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2081 req->r_request_attempts = p;
2085 if (flags & CEPH_OSD_FLAG_WRITE) {
2089 * The header "data_off" is a hint to the receiver
2090 * allowing it to align received data into its
2091 * buffers such that there's no need to re-copy
2092 * it before writing it to disk (direct I/O).
2094 data_off = (u16) (off & 0xffff);
2095 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2097 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2099 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2100 msg_size = p - msg->front.iov_base;
2101 msg->front.iov_len = msg_size;
2102 msg->hdr.front_len = cpu_to_le32(msg_size);
2104 dout("build_request msg_size was %d\n", (int)msg_size);
2106 EXPORT_SYMBOL(ceph_osdc_build_request);
2109 * Register request, send initial attempt.
2111 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2112 struct ceph_osd_request *req,
2117 down_read(&osdc->map_sem);
2118 mutex_lock(&osdc->request_mutex);
2119 __register_request(osdc, req);
2120 WARN_ON(req->r_sent);
2121 rc = __map_request(osdc, req, 0);
2124 dout("osdc_start_request failed map, "
2125 " will retry %lld\n", req->r_tid);
2130 if (req->r_osd == NULL) {
2131 dout("send_request %p no up osds in pg\n", req);
2132 ceph_monc_request_next_osdmap(&osdc->client->monc);
2134 __send_queued(osdc);
2138 mutex_unlock(&osdc->request_mutex);
2139 up_read(&osdc->map_sem);
2142 EXPORT_SYMBOL(ceph_osdc_start_request);
2145 * wait for a request to complete
2147 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2148 struct ceph_osd_request *req)
2152 rc = wait_for_completion_interruptible(&req->r_completion);
2154 mutex_lock(&osdc->request_mutex);
2155 __cancel_request(req);
2156 __unregister_request(osdc, req);
2157 mutex_unlock(&osdc->request_mutex);
2158 complete_request(req);
2159 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
2163 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2164 return req->r_result;
2166 EXPORT_SYMBOL(ceph_osdc_wait_request);
2169 * sync - wait for all in-flight requests to flush. avoid starvation.
2171 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2173 struct ceph_osd_request *req;
2174 u64 last_tid, next_tid = 0;
2176 mutex_lock(&osdc->request_mutex);
2177 last_tid = osdc->last_tid;
2179 req = __lookup_request_ge(osdc, next_tid);
2182 if (req->r_tid > last_tid)
2185 next_tid = req->r_tid + 1;
2186 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2189 ceph_osdc_get_request(req);
2190 mutex_unlock(&osdc->request_mutex);
2191 dout("sync waiting on tid %llu (last is %llu)\n",
2192 req->r_tid, last_tid);
2193 wait_for_completion(&req->r_safe_completion);
2194 mutex_lock(&osdc->request_mutex);
2195 ceph_osdc_put_request(req);
2197 mutex_unlock(&osdc->request_mutex);
2198 dout("sync done (thru tid %llu)\n", last_tid);
2200 EXPORT_SYMBOL(ceph_osdc_sync);
2205 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2210 osdc->client = client;
2211 osdc->osdmap = NULL;
2212 init_rwsem(&osdc->map_sem);
2213 init_completion(&osdc->map_waiters);
2214 osdc->last_requested_map = 0;
2215 mutex_init(&osdc->request_mutex);
2217 osdc->osds = RB_ROOT;
2218 INIT_LIST_HEAD(&osdc->osd_lru);
2219 osdc->requests = RB_ROOT;
2220 INIT_LIST_HEAD(&osdc->req_lru);
2221 INIT_LIST_HEAD(&osdc->req_unsent);
2222 INIT_LIST_HEAD(&osdc->req_notarget);
2223 INIT_LIST_HEAD(&osdc->req_linger);
2224 osdc->num_requests = 0;
2225 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2226 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2227 spin_lock_init(&osdc->event_lock);
2228 osdc->event_tree = RB_ROOT;
2229 osdc->event_count = 0;
2231 schedule_delayed_work(&osdc->osds_timeout_work,
2232 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2235 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2236 sizeof(struct ceph_osd_request));
2237 if (!osdc->req_mempool)
2240 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2241 OSD_OP_FRONT_LEN, 10, true,
2245 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2246 OSD_OPREPLY_FRONT_LEN, 10, true,
2251 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2252 if (IS_ERR(osdc->notify_wq)) {
2253 err = PTR_ERR(osdc->notify_wq);
2254 osdc->notify_wq = NULL;
2260 ceph_msgpool_destroy(&osdc->msgpool_op);
2262 mempool_destroy(osdc->req_mempool);
2267 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2269 flush_workqueue(osdc->notify_wq);
2270 destroy_workqueue(osdc->notify_wq);
2271 cancel_delayed_work_sync(&osdc->timeout_work);
2272 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2274 ceph_osdmap_destroy(osdc->osdmap);
2275 osdc->osdmap = NULL;
2277 remove_all_osds(osdc);
2278 mempool_destroy(osdc->req_mempool);
2279 ceph_msgpool_destroy(&osdc->msgpool_op);
2280 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2284 * Read some contiguous pages. If we cross a stripe boundary, shorten
2285 * *plen. Return number of bytes read, or error.
2287 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2288 struct ceph_vino vino, struct ceph_file_layout *layout,
2290 u32 truncate_seq, u64 truncate_size,
2291 struct page **pages, int num_pages, int page_align)
2293 struct ceph_osd_request *req;
2296 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2297 vino.snap, off, *plen);
2298 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2299 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2300 NULL, truncate_seq, truncate_size,
2303 return PTR_ERR(req);
2305 /* it may be a short read due to an object boundary */
2307 osd_req_op_extent_osd_data_pages(req, 0,
2308 pages, *plen, page_align, false, false);
2310 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2311 off, *plen, *plen, page_align);
2313 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2315 rc = ceph_osdc_start_request(osdc, req, false);
2317 rc = ceph_osdc_wait_request(osdc, req);
2319 ceph_osdc_put_request(req);
2320 dout("readpages result %d\n", rc);
2323 EXPORT_SYMBOL(ceph_osdc_readpages);
2326 * do a synchronous write on N pages
2328 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2329 struct ceph_file_layout *layout,
2330 struct ceph_snap_context *snapc,
2332 u32 truncate_seq, u64 truncate_size,
2333 struct timespec *mtime,
2334 struct page **pages, int num_pages)
2336 struct ceph_osd_request *req;
2338 int page_align = off & ~PAGE_MASK;
2340 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2341 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2343 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2344 snapc, truncate_seq, truncate_size,
2347 return PTR_ERR(req);
2349 /* it may be a short write due to an object boundary */
2350 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2352 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2354 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2356 rc = ceph_osdc_start_request(osdc, req, true);
2358 rc = ceph_osdc_wait_request(osdc, req);
2360 ceph_osdc_put_request(req);
2363 dout("writepages result %d\n", rc);
2366 EXPORT_SYMBOL(ceph_osdc_writepages);
2369 * handle incoming message
2371 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2373 struct ceph_osd *osd = con->private;
2374 struct ceph_osd_client *osdc;
2375 int type = le16_to_cpu(msg->hdr.type);
2382 case CEPH_MSG_OSD_MAP:
2383 ceph_osdc_handle_map(osdc, msg);
2385 case CEPH_MSG_OSD_OPREPLY:
2386 handle_reply(osdc, msg, con);
2388 case CEPH_MSG_WATCH_NOTIFY:
2389 handle_watch_notify(osdc, msg);
2393 pr_err("received unknown message type %d %s\n", type,
2394 ceph_msg_type_name(type));
2401 * lookup and return message for incoming reply. set up reply message
2404 static struct ceph_msg *get_reply(struct ceph_connection *con,
2405 struct ceph_msg_header *hdr,
2408 struct ceph_osd *osd = con->private;
2409 struct ceph_osd_client *osdc = osd->o_osdc;
2411 struct ceph_osd_request *req;
2412 int front = le32_to_cpu(hdr->front_len);
2413 int data_len = le32_to_cpu(hdr->data_len);
2416 tid = le64_to_cpu(hdr->tid);
2417 mutex_lock(&osdc->request_mutex);
2418 req = __lookup_request(osdc, tid);
2422 dout("get_reply unknown tid %llu from osd%d\n", tid,
2427 if (req->r_reply->con)
2428 dout("%s revoking msg %p from old con %p\n", __func__,
2429 req->r_reply, req->r_reply->con);
2430 ceph_msg_revoke_incoming(req->r_reply);
2432 if (front > req->r_reply->front.iov_len) {
2433 pr_warning("get_reply front %d > preallocated %d\n",
2434 front, (int)req->r_reply->front.iov_len);
2435 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2438 ceph_msg_put(req->r_reply);
2441 m = ceph_msg_get(req->r_reply);
2444 struct ceph_osd_data *osd_data;
2447 * XXX This is assuming there is only one op containing
2448 * XXX page data. Probably OK for reads, but this
2449 * XXX ought to be done more generally.
2451 osd_data = osd_req_op_extent_osd_data(req, 0);
2452 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2453 if (osd_data->pages &&
2454 unlikely(osd_data->length < data_len)) {
2456 pr_warning("tid %lld reply has %d bytes "
2457 "we had only %llu bytes ready\n",
2458 tid, data_len, osd_data->length);
2467 dout("get_reply tid %lld %p\n", tid, m);
2470 mutex_unlock(&osdc->request_mutex);
2475 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2476 struct ceph_msg_header *hdr,
2479 struct ceph_osd *osd = con->private;
2480 int type = le16_to_cpu(hdr->type);
2481 int front = le32_to_cpu(hdr->front_len);
2485 case CEPH_MSG_OSD_MAP:
2486 case CEPH_MSG_WATCH_NOTIFY:
2487 return ceph_msg_new(type, front, GFP_NOFS, false);
2488 case CEPH_MSG_OSD_OPREPLY:
2489 return get_reply(con, hdr, skip);
2491 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2499 * Wrappers to refcount containing ceph_osd struct
2501 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2503 struct ceph_osd *osd = con->private;
2509 static void put_osd_con(struct ceph_connection *con)
2511 struct ceph_osd *osd = con->private;
2519 * Note: returned pointer is the address of a structure that's
2520 * managed separately. Caller must *not* attempt to free it.
2522 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2523 int *proto, int force_new)
2525 struct ceph_osd *o = con->private;
2526 struct ceph_osd_client *osdc = o->o_osdc;
2527 struct ceph_auth_client *ac = osdc->client->monc.auth;
2528 struct ceph_auth_handshake *auth = &o->o_auth;
2530 if (force_new && auth->authorizer) {
2531 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2532 auth->authorizer = NULL;
2534 if (!auth->authorizer) {
2535 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2538 return ERR_PTR(ret);
2540 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2543 return ERR_PTR(ret);
2545 *proto = ac->protocol;
2551 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2553 struct ceph_osd *o = con->private;
2554 struct ceph_osd_client *osdc = o->o_osdc;
2555 struct ceph_auth_client *ac = osdc->client->monc.auth;
2557 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2560 static int invalidate_authorizer(struct ceph_connection *con)
2562 struct ceph_osd *o = con->private;
2563 struct ceph_osd_client *osdc = o->o_osdc;
2564 struct ceph_auth_client *ac = osdc->client->monc.auth;
2566 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2567 return ceph_monc_validate_auth(&osdc->client->monc);
2570 static const struct ceph_connection_operations osd_con_ops = {
2573 .dispatch = dispatch,
2574 .get_authorizer = get_authorizer,
2575 .verify_authorizer_reply = verify_authorizer_reply,
2576 .invalidate_authorizer = invalidate_authorizer,
2577 .alloc_msg = alloc_msg,