2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
6 #include <linux/highmem.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
12 #include <linux/bio.h>
15 #include <linux/ceph/libceph.h>
16 #include <linux/ceph/osd_client.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/pagelist.h>
22 #define OSD_OP_FRONT_LEN 4096
23 #define OSD_OPREPLY_FRONT_LEN 512
25 static const struct ceph_connection_operations osd_con_ops;
27 static void __send_queued(struct ceph_osd_client *osdc);
28 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
29 static void __register_request(struct ceph_osd_client *osdc,
30 struct ceph_osd_request *req);
31 static void __unregister_linger_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
33 static void __send_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
37 * Implement client access to distributed object storage cluster.
39 * All data objects are stored within a cluster/cloud of OSDs, or
40 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
41 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
42 * remote daemons serving up and coordinating consistent and safe
45 * Cluster membership and the mapping of data objects onto storage devices
46 * are described by the osd map.
48 * We keep track of pending OSD requests (read, write), resubmit
49 * requests to different OSDs when the cluster topology/data layout
50 * change, or retry the affected requests when the communications
51 * channel with an OSD is reset.
55 * calculate the mapping of a file extent onto an object, and fill out the
56 * request accordingly. shorten extent as necessary if it crosses an
59 * fill osd op in request message.
61 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
62 u64 *objnum, u64 *objoff, u64 *objlen)
68 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
72 if (*objlen < orig_len) {
74 dout(" skipping last %llu, final file extent %llu~%llu\n",
75 orig_len - *plen, off, *plen);
78 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
83 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
85 memset(osd_data, 0, sizeof (*osd_data));
86 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
89 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
90 struct page **pages, u64 length, u32 alignment,
91 bool pages_from_pool, bool own_pages)
93 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
94 osd_data->pages = pages;
95 osd_data->length = length;
96 osd_data->alignment = alignment;
97 osd_data->pages_from_pool = pages_from_pool;
98 osd_data->own_pages = own_pages;
101 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
102 struct ceph_pagelist *pagelist)
104 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
105 osd_data->pagelist = pagelist;
109 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
110 struct bio *bio, size_t bio_length)
112 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
114 osd_data->bio_length = bio_length;
116 #endif /* CONFIG_BLOCK */
118 struct ceph_osd_data *
119 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
122 BUG_ON(which >= osd_req->r_num_ops);
124 return &osd_req->r_ops[which].extent.osd_data;
126 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
128 struct ceph_osd_data *
129 osd_req_op_cls_request_info(struct ceph_osd_request *osd_req,
132 BUG_ON(which >= osd_req->r_num_ops);
134 return &osd_req->r_ops[which].cls.request_info;
136 EXPORT_SYMBOL(osd_req_op_cls_request_info); /* ??? */
138 struct ceph_osd_data *
139 osd_req_op_cls_request_data(struct ceph_osd_request *osd_req,
142 BUG_ON(which >= osd_req->r_num_ops);
144 return &osd_req->r_ops[which].cls.request_data;
146 EXPORT_SYMBOL(osd_req_op_cls_request_data); /* ??? */
148 struct ceph_osd_data *
149 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
152 BUG_ON(which >= osd_req->r_num_ops);
154 return &osd_req->r_ops[which].cls.response_data;
156 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
158 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
159 unsigned int which, struct page **pages,
160 u64 length, u32 alignment,
161 bool pages_from_pool, bool own_pages)
163 struct ceph_osd_data *osd_data;
165 osd_data = osd_req_op_extent_osd_data(osd_req, which);
166 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
167 pages_from_pool, own_pages);
169 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
171 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
172 unsigned int which, struct ceph_pagelist *pagelist)
174 struct ceph_osd_data *osd_data;
176 osd_data = osd_req_op_extent_osd_data(osd_req, which);
177 ceph_osd_data_pagelist_init(osd_data, pagelist);
179 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
182 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
183 unsigned int which, struct bio *bio, size_t bio_length)
185 struct ceph_osd_data *osd_data;
186 osd_data = osd_req_op_extent_osd_data(osd_req, which);
187 ceph_osd_data_bio_init(osd_data, bio, bio_length);
189 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
190 #endif /* CONFIG_BLOCK */
192 static void osd_req_op_cls_request_info_pagelist(
193 struct ceph_osd_request *osd_req,
194 unsigned int which, struct ceph_pagelist *pagelist)
196 struct ceph_osd_data *osd_data;
198 osd_data = osd_req_op_cls_request_info(osd_req, which);
199 ceph_osd_data_pagelist_init(osd_data, pagelist);
202 void osd_req_op_cls_request_data_pagelist(
203 struct ceph_osd_request *osd_req,
204 unsigned int which, struct ceph_pagelist *pagelist)
206 struct ceph_osd_data *osd_data;
208 osd_data = osd_req_op_cls_request_data(osd_req, which);
209 ceph_osd_data_pagelist_init(osd_data, pagelist);
211 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
213 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
214 unsigned int which, struct page **pages, u64 length,
215 u32 alignment, bool pages_from_pool, bool own_pages)
217 struct ceph_osd_data *osd_data;
219 osd_data = osd_req_op_cls_response_data(osd_req, which);
220 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
221 pages_from_pool, own_pages);
223 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
225 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
227 switch (osd_data->type) {
228 case CEPH_OSD_DATA_TYPE_NONE:
230 case CEPH_OSD_DATA_TYPE_PAGES:
231 return osd_data->length;
232 case CEPH_OSD_DATA_TYPE_PAGELIST:
233 return (u64)osd_data->pagelist->length;
235 case CEPH_OSD_DATA_TYPE_BIO:
236 return (u64)osd_data->bio_length;
237 #endif /* CONFIG_BLOCK */
239 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
245 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
247 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
250 num_pages = calc_pages_for((u64)osd_data->alignment,
251 (u64)osd_data->length);
252 ceph_release_page_vector(osd_data->pages, num_pages);
254 ceph_osd_data_init(osd_data);
257 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
260 struct ceph_osd_req_op *op;
262 BUG_ON(which >= osd_req->r_num_ops);
263 op = &osd_req->r_ops[which];
266 case CEPH_OSD_OP_READ:
267 case CEPH_OSD_OP_WRITE:
268 ceph_osd_data_release(&op->extent.osd_data);
270 case CEPH_OSD_OP_CALL:
271 ceph_osd_data_release(&op->cls.request_info);
272 ceph_osd_data_release(&op->cls.request_data);
273 ceph_osd_data_release(&op->cls.response_data);
283 void ceph_osdc_release_request(struct kref *kref)
285 struct ceph_osd_request *req;
288 req = container_of(kref, struct ceph_osd_request, r_kref);
290 ceph_msg_put(req->r_request);
292 ceph_msg_revoke_incoming(req->r_reply);
293 ceph_msg_put(req->r_reply);
296 for (which = 0; which < req->r_num_ops; which++)
297 osd_req_op_data_release(req, which);
299 ceph_put_snap_context(req->r_snapc);
301 mempool_free(req, req->r_osdc->req_mempool);
305 EXPORT_SYMBOL(ceph_osdc_release_request);
307 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
308 struct ceph_snap_context *snapc,
309 unsigned int num_ops,
313 struct ceph_osd_request *req;
314 struct ceph_msg *msg;
317 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
318 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
320 msg_size = 4 + 4 + 8 + 8 + 4+8;
321 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
322 msg_size += 1 + 8 + 4 + 4; /* pg_t */
323 msg_size += 4 + MAX_OBJ_NAME_SIZE;
324 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
325 msg_size += 8; /* snapid */
326 msg_size += 8; /* snap_seq */
327 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
331 req = mempool_alloc(osdc->req_mempool, gfp_flags);
332 memset(req, 0, sizeof(*req));
334 req = kzalloc(sizeof(*req), gfp_flags);
340 req->r_mempool = use_mempool;
341 req->r_num_ops = num_ops;
343 kref_init(&req->r_kref);
344 init_completion(&req->r_completion);
345 init_completion(&req->r_safe_completion);
346 RB_CLEAR_NODE(&req->r_node);
347 INIT_LIST_HEAD(&req->r_unsafe_item);
348 INIT_LIST_HEAD(&req->r_linger_item);
349 INIT_LIST_HEAD(&req->r_linger_osd);
350 INIT_LIST_HEAD(&req->r_req_lru_item);
351 INIT_LIST_HEAD(&req->r_osd_item);
353 /* create reply message */
355 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
357 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
358 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
360 ceph_osdc_put_request(req);
365 /* create request message; allow space for oid */
367 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
369 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
371 ceph_osdc_put_request(req);
375 memset(msg->front.iov_base, 0, msg->front.iov_len);
377 req->r_request = msg;
381 EXPORT_SYMBOL(ceph_osdc_alloc_request);
383 static bool osd_req_opcode_valid(u16 opcode)
386 case CEPH_OSD_OP_READ:
387 case CEPH_OSD_OP_STAT:
388 case CEPH_OSD_OP_MAPEXT:
389 case CEPH_OSD_OP_MASKTRUNC:
390 case CEPH_OSD_OP_SPARSE_READ:
391 case CEPH_OSD_OP_NOTIFY:
392 case CEPH_OSD_OP_NOTIFY_ACK:
393 case CEPH_OSD_OP_ASSERT_VER:
394 case CEPH_OSD_OP_WRITE:
395 case CEPH_OSD_OP_WRITEFULL:
396 case CEPH_OSD_OP_TRUNCATE:
397 case CEPH_OSD_OP_ZERO:
398 case CEPH_OSD_OP_DELETE:
399 case CEPH_OSD_OP_APPEND:
400 case CEPH_OSD_OP_STARTSYNC:
401 case CEPH_OSD_OP_SETTRUNC:
402 case CEPH_OSD_OP_TRIMTRUNC:
403 case CEPH_OSD_OP_TMAPUP:
404 case CEPH_OSD_OP_TMAPPUT:
405 case CEPH_OSD_OP_TMAPGET:
406 case CEPH_OSD_OP_CREATE:
407 case CEPH_OSD_OP_ROLLBACK:
408 case CEPH_OSD_OP_WATCH:
409 case CEPH_OSD_OP_OMAPGETKEYS:
410 case CEPH_OSD_OP_OMAPGETVALS:
411 case CEPH_OSD_OP_OMAPGETHEADER:
412 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
413 case CEPH_OSD_OP_OMAPSETVALS:
414 case CEPH_OSD_OP_OMAPSETHEADER:
415 case CEPH_OSD_OP_OMAPCLEAR:
416 case CEPH_OSD_OP_OMAPRMKEYS:
417 case CEPH_OSD_OP_OMAP_CMP:
418 case CEPH_OSD_OP_CLONERANGE:
419 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
420 case CEPH_OSD_OP_SRC_CMPXATTR:
421 case CEPH_OSD_OP_GETXATTR:
422 case CEPH_OSD_OP_GETXATTRS:
423 case CEPH_OSD_OP_CMPXATTR:
424 case CEPH_OSD_OP_SETXATTR:
425 case CEPH_OSD_OP_SETXATTRS:
426 case CEPH_OSD_OP_RESETXATTRS:
427 case CEPH_OSD_OP_RMXATTR:
428 case CEPH_OSD_OP_PULL:
429 case CEPH_OSD_OP_PUSH:
430 case CEPH_OSD_OP_BALANCEREADS:
431 case CEPH_OSD_OP_UNBALANCEREADS:
432 case CEPH_OSD_OP_SCRUB:
433 case CEPH_OSD_OP_SCRUB_RESERVE:
434 case CEPH_OSD_OP_SCRUB_UNRESERVE:
435 case CEPH_OSD_OP_SCRUB_STOP:
436 case CEPH_OSD_OP_SCRUB_MAP:
437 case CEPH_OSD_OP_WRLOCK:
438 case CEPH_OSD_OP_WRUNLOCK:
439 case CEPH_OSD_OP_RDLOCK:
440 case CEPH_OSD_OP_RDUNLOCK:
441 case CEPH_OSD_OP_UPLOCK:
442 case CEPH_OSD_OP_DNLOCK:
443 case CEPH_OSD_OP_CALL:
444 case CEPH_OSD_OP_PGLS:
445 case CEPH_OSD_OP_PGLS_FILTER:
453 * This is an osd op init function for opcodes that have no data or
454 * other information associated with them. It also serves as a
455 * common init routine for all the other init functions, below.
457 static struct ceph_osd_req_op *
458 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
461 struct ceph_osd_req_op *op;
463 BUG_ON(which >= osd_req->r_num_ops);
464 BUG_ON(!osd_req_opcode_valid(opcode));
466 op = &osd_req->r_ops[which];
467 memset(op, 0, sizeof (*op));
473 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
474 unsigned int which, u16 opcode,
475 u64 offset, u64 length,
476 u64 truncate_size, u32 truncate_seq)
478 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
479 size_t payload_len = 0;
481 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
483 op->extent.offset = offset;
484 op->extent.length = length;
485 op->extent.truncate_size = truncate_size;
486 op->extent.truncate_seq = truncate_seq;
487 if (opcode == CEPH_OSD_OP_WRITE)
488 payload_len += length;
490 op->payload_len = payload_len;
492 EXPORT_SYMBOL(osd_req_op_extent_init);
494 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
495 unsigned int which, u64 length)
497 struct ceph_osd_req_op *op;
500 BUG_ON(which >= osd_req->r_num_ops);
501 op = &osd_req->r_ops[which];
502 previous = op->extent.length;
504 if (length == previous)
505 return; /* Nothing to do */
506 BUG_ON(length > previous);
508 op->extent.length = length;
509 op->payload_len -= previous - length;
511 EXPORT_SYMBOL(osd_req_op_extent_update);
513 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
514 u16 opcode, const char *class, const char *method)
516 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
517 struct ceph_pagelist *pagelist;
518 size_t payload_len = 0;
521 BUG_ON(opcode != CEPH_OSD_OP_CALL);
523 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
525 ceph_pagelist_init(pagelist);
527 op->cls.class_name = class;
528 size = strlen(class);
529 BUG_ON(size > (size_t) U8_MAX);
530 op->cls.class_len = size;
531 ceph_pagelist_append(pagelist, class, size);
534 op->cls.method_name = method;
535 size = strlen(method);
536 BUG_ON(size > (size_t) U8_MAX);
537 op->cls.method_len = size;
538 ceph_pagelist_append(pagelist, method, size);
541 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
543 op->cls.argc = 0; /* currently unused */
545 op->payload_len = payload_len;
547 EXPORT_SYMBOL(osd_req_op_cls_init);
549 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
550 unsigned int which, u16 opcode,
551 u64 cookie, u64 version, int flag)
553 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
555 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
557 op->watch.cookie = cookie;
558 /* op->watch.ver = version; */ /* XXX 3847 */
559 op->watch.ver = cpu_to_le64(version);
560 if (opcode == CEPH_OSD_OP_WATCH && flag)
561 op->watch.flag = (u8)1;
563 EXPORT_SYMBOL(osd_req_op_watch_init);
565 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
566 struct ceph_osd_data *osd_data)
568 u64 length = ceph_osd_data_length(osd_data);
570 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
571 BUG_ON(length > (u64) SIZE_MAX);
573 ceph_msg_data_add_pages(msg, osd_data->pages,
574 length, osd_data->alignment);
575 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
577 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
579 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
580 ceph_msg_data_add_bio(msg, osd_data->bio, length);
583 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
587 static u64 osd_req_encode_op(struct ceph_osd_request *req,
588 struct ceph_osd_op *dst, unsigned int which)
590 struct ceph_osd_req_op *src;
591 struct ceph_osd_data *osd_data;
592 u64 request_data_len = 0;
595 BUG_ON(which >= req->r_num_ops);
596 src = &req->r_ops[which];
597 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
598 pr_err("unrecognized osd opcode %d\n", src->op);
604 case CEPH_OSD_OP_STAT:
606 case CEPH_OSD_OP_READ:
607 case CEPH_OSD_OP_WRITE:
608 if (src->op == CEPH_OSD_OP_WRITE)
609 request_data_len = src->extent.length;
610 dst->extent.offset = cpu_to_le64(src->extent.offset);
611 dst->extent.length = cpu_to_le64(src->extent.length);
612 dst->extent.truncate_size =
613 cpu_to_le64(src->extent.truncate_size);
614 dst->extent.truncate_seq =
615 cpu_to_le32(src->extent.truncate_seq);
616 osd_data = &src->extent.osd_data;
617 if (src->op == CEPH_OSD_OP_WRITE)
618 ceph_osdc_msg_data_add(req->r_request, osd_data);
620 ceph_osdc_msg_data_add(req->r_reply, osd_data);
622 case CEPH_OSD_OP_CALL:
623 dst->cls.class_len = src->cls.class_len;
624 dst->cls.method_len = src->cls.method_len;
625 osd_data = &src->cls.request_info;
626 ceph_osdc_msg_data_add(req->r_request, osd_data);
627 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
628 request_data_len = osd_data->pagelist->length;
630 osd_data = &src->cls.request_data;
631 data_length = ceph_osd_data_length(osd_data);
633 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
634 dst->cls.indata_len = cpu_to_le32(data_length);
635 ceph_osdc_msg_data_add(req->r_request, osd_data);
636 src->payload_len += data_length;
637 request_data_len += data_length;
639 osd_data = &src->cls.response_data;
640 ceph_osdc_msg_data_add(req->r_reply, osd_data);
642 case CEPH_OSD_OP_STARTSYNC:
644 case CEPH_OSD_OP_NOTIFY_ACK:
645 case CEPH_OSD_OP_WATCH:
646 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
647 dst->watch.ver = cpu_to_le64(src->watch.ver);
648 dst->watch.flag = src->watch.flag;
651 pr_err("unsupported osd opcode %s\n",
652 ceph_osd_op_name(src->op));
657 dst->op = cpu_to_le16(src->op);
658 dst->payload_len = cpu_to_le32(src->payload_len);
660 return request_data_len;
664 * build new request AND message, calculate layout, and adjust file
667 * if the file was recently truncated, we include information about its
668 * old and new size so that the object can be updated appropriately. (we
669 * avoid synchronously deleting truncated objects because it's slow.)
671 * if @do_sync, include a 'startsync' command so that the osd will flush
674 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
675 struct ceph_file_layout *layout,
676 struct ceph_vino vino,
677 u64 off, u64 *plen, int num_ops,
678 int opcode, int flags,
679 struct ceph_snap_context *snapc,
684 struct ceph_osd_request *req;
692 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
694 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
697 return ERR_PTR(-ENOMEM);
699 req->r_flags = flags;
701 /* calculate max write size */
702 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
704 ceph_osdc_put_request(req);
708 object_size = le32_to_cpu(layout->fl_object_size);
709 object_base = off - objoff;
710 if (truncate_size <= object_base) {
713 truncate_size -= object_base;
714 if (truncate_size > object_size)
715 truncate_size = object_size;
718 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
719 truncate_size, truncate_seq);
722 * A second op in the ops array means the caller wants to
723 * also issue a include a 'startsync' command so that the
724 * osd will flush data quickly.
727 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
729 req->r_file_layout = *layout; /* keep a copy */
731 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
733 req->r_oid_len = strlen(req->r_oid);
737 EXPORT_SYMBOL(ceph_osdc_new_request);
740 * We keep osd requests in an rbtree, sorted by ->r_tid.
742 static void __insert_request(struct ceph_osd_client *osdc,
743 struct ceph_osd_request *new)
745 struct rb_node **p = &osdc->requests.rb_node;
746 struct rb_node *parent = NULL;
747 struct ceph_osd_request *req = NULL;
751 req = rb_entry(parent, struct ceph_osd_request, r_node);
752 if (new->r_tid < req->r_tid)
754 else if (new->r_tid > req->r_tid)
760 rb_link_node(&new->r_node, parent, p);
761 rb_insert_color(&new->r_node, &osdc->requests);
764 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
767 struct ceph_osd_request *req;
768 struct rb_node *n = osdc->requests.rb_node;
771 req = rb_entry(n, struct ceph_osd_request, r_node);
772 if (tid < req->r_tid)
774 else if (tid > req->r_tid)
782 static struct ceph_osd_request *
783 __lookup_request_ge(struct ceph_osd_client *osdc,
786 struct ceph_osd_request *req;
787 struct rb_node *n = osdc->requests.rb_node;
790 req = rb_entry(n, struct ceph_osd_request, r_node);
791 if (tid < req->r_tid) {
795 } else if (tid > req->r_tid) {
805 * Resubmit requests pending on the given osd.
807 static void __kick_osd_requests(struct ceph_osd_client *osdc,
808 struct ceph_osd *osd)
810 struct ceph_osd_request *req, *nreq;
814 dout("__kick_osd_requests osd%d\n", osd->o_osd);
815 err = __reset_osd(osdc, osd);
819 * Build up a list of requests to resend by traversing the
820 * osd's list of requests. Requests for a given object are
821 * sent in tid order, and that is also the order they're
822 * kept on this list. Therefore all requests that are in
823 * flight will be found first, followed by all requests that
824 * have not yet been sent. And to resend requests while
825 * preserving this order we will want to put any sent
826 * requests back on the front of the osd client's unsent
829 * So we build a separate ordered list of already-sent
830 * requests for the affected osd and splice it onto the
831 * front of the osd client's unsent list. Once we've seen a
832 * request that has not yet been sent we're done. Those
833 * requests are already sitting right where they belong.
835 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
838 list_move_tail(&req->r_req_lru_item, &resend);
839 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
842 req->r_flags |= CEPH_OSD_FLAG_RETRY;
844 list_splice(&resend, &osdc->req_unsent);
847 * Linger requests are re-registered before sending, which
848 * sets up a new tid for each. We add them to the unsent
849 * list at the end to keep things in tid order.
851 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
854 * reregister request prior to unregistering linger so
855 * that r_osd is preserved.
857 BUG_ON(!list_empty(&req->r_req_lru_item));
858 __register_request(osdc, req);
859 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
860 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
861 __unregister_linger_request(osdc, req);
862 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
868 * If the osd connection drops, we need to resubmit all requests.
870 static void osd_reset(struct ceph_connection *con)
872 struct ceph_osd *osd = con->private;
873 struct ceph_osd_client *osdc;
877 dout("osd_reset osd%d\n", osd->o_osd);
879 down_read(&osdc->map_sem);
880 mutex_lock(&osdc->request_mutex);
881 __kick_osd_requests(osdc, osd);
883 mutex_unlock(&osdc->request_mutex);
884 up_read(&osdc->map_sem);
888 * Track open sessions with osds.
890 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
892 struct ceph_osd *osd;
894 osd = kzalloc(sizeof(*osd), GFP_NOFS);
898 atomic_set(&osd->o_ref, 1);
901 RB_CLEAR_NODE(&osd->o_node);
902 INIT_LIST_HEAD(&osd->o_requests);
903 INIT_LIST_HEAD(&osd->o_linger_requests);
904 INIT_LIST_HEAD(&osd->o_osd_lru);
905 osd->o_incarnation = 1;
907 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
909 INIT_LIST_HEAD(&osd->o_keepalive_item);
913 static struct ceph_osd *get_osd(struct ceph_osd *osd)
915 if (atomic_inc_not_zero(&osd->o_ref)) {
916 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
917 atomic_read(&osd->o_ref));
920 dout("get_osd %p FAIL\n", osd);
925 static void put_osd(struct ceph_osd *osd)
927 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
928 atomic_read(&osd->o_ref) - 1);
929 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
930 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
932 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
938 * remove an osd from our map
940 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
942 dout("__remove_osd %p\n", osd);
943 BUG_ON(!list_empty(&osd->o_requests));
944 rb_erase(&osd->o_node, &osdc->osds);
945 list_del_init(&osd->o_osd_lru);
946 ceph_con_close(&osd->o_con);
950 static void remove_all_osds(struct ceph_osd_client *osdc)
952 dout("%s %p\n", __func__, osdc);
953 mutex_lock(&osdc->request_mutex);
954 while (!RB_EMPTY_ROOT(&osdc->osds)) {
955 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
956 struct ceph_osd, o_node);
957 __remove_osd(osdc, osd);
959 mutex_unlock(&osdc->request_mutex);
962 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
963 struct ceph_osd *osd)
965 dout("__move_osd_to_lru %p\n", osd);
966 BUG_ON(!list_empty(&osd->o_osd_lru));
967 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
968 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
971 static void __remove_osd_from_lru(struct ceph_osd *osd)
973 dout("__remove_osd_from_lru %p\n", osd);
974 if (!list_empty(&osd->o_osd_lru))
975 list_del_init(&osd->o_osd_lru);
978 static void remove_old_osds(struct ceph_osd_client *osdc)
980 struct ceph_osd *osd, *nosd;
982 dout("__remove_old_osds %p\n", osdc);
983 mutex_lock(&osdc->request_mutex);
984 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
985 if (time_before(jiffies, osd->lru_ttl))
987 __remove_osd(osdc, osd);
989 mutex_unlock(&osdc->request_mutex);
995 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
997 struct ceph_entity_addr *peer_addr;
999 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1000 if (list_empty(&osd->o_requests) &&
1001 list_empty(&osd->o_linger_requests)) {
1002 __remove_osd(osdc, osd);
1007 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1008 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1009 !ceph_con_opened(&osd->o_con)) {
1010 struct ceph_osd_request *req;
1012 dout(" osd addr hasn't changed and connection never opened,"
1013 " letting msgr retry");
1014 /* touch each r_stamp for handle_timeout()'s benfit */
1015 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1016 req->r_stamp = jiffies;
1021 ceph_con_close(&osd->o_con);
1022 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1023 osd->o_incarnation++;
1028 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1030 struct rb_node **p = &osdc->osds.rb_node;
1031 struct rb_node *parent = NULL;
1032 struct ceph_osd *osd = NULL;
1034 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1037 osd = rb_entry(parent, struct ceph_osd, o_node);
1038 if (new->o_osd < osd->o_osd)
1040 else if (new->o_osd > osd->o_osd)
1041 p = &(*p)->rb_right;
1046 rb_link_node(&new->o_node, parent, p);
1047 rb_insert_color(&new->o_node, &osdc->osds);
1050 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1052 struct ceph_osd *osd;
1053 struct rb_node *n = osdc->osds.rb_node;
1056 osd = rb_entry(n, struct ceph_osd, o_node);
1059 else if (o > osd->o_osd)
1067 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1069 schedule_delayed_work(&osdc->timeout_work,
1070 osdc->client->options->osd_keepalive_timeout * HZ);
1073 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1075 cancel_delayed_work(&osdc->timeout_work);
1079 * Register request, assign tid. If this is the first request, set up
1080 * the timeout event.
1082 static void __register_request(struct ceph_osd_client *osdc,
1083 struct ceph_osd_request *req)
1085 req->r_tid = ++osdc->last_tid;
1086 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1087 dout("__register_request %p tid %lld\n", req, req->r_tid);
1088 __insert_request(osdc, req);
1089 ceph_osdc_get_request(req);
1090 osdc->num_requests++;
1091 if (osdc->num_requests == 1) {
1092 dout(" first request, scheduling timeout\n");
1093 __schedule_osd_timeout(osdc);
1098 * called under osdc->request_mutex
1100 static void __unregister_request(struct ceph_osd_client *osdc,
1101 struct ceph_osd_request *req)
1103 if (RB_EMPTY_NODE(&req->r_node)) {
1104 dout("__unregister_request %p tid %lld not registered\n",
1109 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1110 rb_erase(&req->r_node, &osdc->requests);
1111 osdc->num_requests--;
1114 /* make sure the original request isn't in flight. */
1115 ceph_msg_revoke(req->r_request);
1117 list_del_init(&req->r_osd_item);
1118 if (list_empty(&req->r_osd->o_requests) &&
1119 list_empty(&req->r_osd->o_linger_requests)) {
1120 dout("moving osd to %p lru\n", req->r_osd);
1121 __move_osd_to_lru(osdc, req->r_osd);
1123 if (list_empty(&req->r_linger_item))
1127 list_del_init(&req->r_req_lru_item);
1128 ceph_osdc_put_request(req);
1130 if (osdc->num_requests == 0) {
1131 dout(" no requests, canceling timeout\n");
1132 __cancel_osd_timeout(osdc);
1137 * Cancel a previously queued request message
1139 static void __cancel_request(struct ceph_osd_request *req)
1141 if (req->r_sent && req->r_osd) {
1142 ceph_msg_revoke(req->r_request);
1147 static void __register_linger_request(struct ceph_osd_client *osdc,
1148 struct ceph_osd_request *req)
1150 dout("__register_linger_request %p\n", req);
1151 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1153 list_add_tail(&req->r_linger_osd,
1154 &req->r_osd->o_linger_requests);
1157 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1158 struct ceph_osd_request *req)
1160 dout("__unregister_linger_request %p\n", req);
1161 list_del_init(&req->r_linger_item);
1163 list_del_init(&req->r_linger_osd);
1165 if (list_empty(&req->r_osd->o_requests) &&
1166 list_empty(&req->r_osd->o_linger_requests)) {
1167 dout("moving osd to %p lru\n", req->r_osd);
1168 __move_osd_to_lru(osdc, req->r_osd);
1170 if (list_empty(&req->r_osd_item))
1175 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1176 struct ceph_osd_request *req)
1178 mutex_lock(&osdc->request_mutex);
1179 if (req->r_linger) {
1180 __unregister_linger_request(osdc, req);
1181 ceph_osdc_put_request(req);
1183 mutex_unlock(&osdc->request_mutex);
1185 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1187 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1188 struct ceph_osd_request *req)
1190 if (!req->r_linger) {
1191 dout("set_request_linger %p\n", req);
1194 * caller is now responsible for calling
1195 * unregister_linger_request
1197 ceph_osdc_get_request(req);
1200 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1203 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1204 * (as needed), and set the request r_osd appropriately. If there is
1205 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1206 * (unsent, homeless) or leave on in-flight lru.
1208 * Return 0 if unchanged, 1 if changed, or negative on error.
1210 * Caller should hold map_sem for read and request_mutex.
1212 static int __map_request(struct ceph_osd_client *osdc,
1213 struct ceph_osd_request *req, int force_resend)
1215 struct ceph_pg pgid;
1216 int acting[CEPH_PG_MAX_SIZE];
1217 int o = -1, num = 0;
1220 dout("map_request %p tid %lld\n", req, req->r_tid);
1221 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1222 ceph_file_layout_pg_pool(req->r_file_layout));
1224 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1229 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1235 if ((!force_resend &&
1236 req->r_osd && req->r_osd->o_osd == o &&
1237 req->r_sent >= req->r_osd->o_incarnation &&
1238 req->r_num_pg_osds == num &&
1239 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1240 (req->r_osd == NULL && o == -1))
1241 return 0; /* no change */
1243 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1244 req->r_tid, pgid.pool, pgid.seed, o,
1245 req->r_osd ? req->r_osd->o_osd : -1);
1247 /* record full pg acting set */
1248 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1249 req->r_num_pg_osds = num;
1252 __cancel_request(req);
1253 list_del_init(&req->r_osd_item);
1257 req->r_osd = __lookup_osd(osdc, o);
1258 if (!req->r_osd && o >= 0) {
1260 req->r_osd = create_osd(osdc, o);
1262 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1266 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1267 __insert_osd(osdc, req->r_osd);
1269 ceph_con_open(&req->r_osd->o_con,
1270 CEPH_ENTITY_TYPE_OSD, o,
1271 &osdc->osdmap->osd_addr[o]);
1275 __remove_osd_from_lru(req->r_osd);
1276 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1277 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1279 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1281 err = 1; /* osd or pg changed */
1288 * caller should hold map_sem (for read) and request_mutex
1290 static void __send_request(struct ceph_osd_client *osdc,
1291 struct ceph_osd_request *req)
1295 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1296 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1297 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1299 /* fill in message content that changes each time we send it */
1300 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1301 put_unaligned_le32(req->r_flags, req->r_request_flags);
1302 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1303 p = req->r_request_pgid;
1304 ceph_encode_64(&p, req->r_pgid.pool);
1305 ceph_encode_32(&p, req->r_pgid.seed);
1306 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1307 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1308 sizeof(req->r_reassert_version));
1310 req->r_stamp = jiffies;
1311 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1313 ceph_msg_get(req->r_request); /* send consumes a ref */
1315 /* Mark the request unsafe if this is the first timet's being sent. */
1317 if (!req->r_sent && req->r_unsafe_callback)
1318 req->r_unsafe_callback(req, true);
1319 req->r_sent = req->r_osd->o_incarnation;
1321 ceph_con_send(&req->r_osd->o_con, req->r_request);
1325 * Send any requests in the queue (req_unsent).
1327 static void __send_queued(struct ceph_osd_client *osdc)
1329 struct ceph_osd_request *req, *tmp;
1331 dout("__send_queued\n");
1332 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1333 __send_request(osdc, req);
1337 * Timeout callback, called every N seconds when 1 or more osd
1338 * requests has been active for more than N seconds. When this
1339 * happens, we ping all OSDs with requests who have timed out to
1340 * ensure any communications channel reset is detected. Reset the
1341 * request timeouts another N seconds in the future as we go.
1342 * Reschedule the timeout event another N seconds in future (unless
1343 * there are no open requests).
1345 static void handle_timeout(struct work_struct *work)
1347 struct ceph_osd_client *osdc =
1348 container_of(work, struct ceph_osd_client, timeout_work.work);
1349 struct ceph_osd_request *req;
1350 struct ceph_osd *osd;
1351 unsigned long keepalive =
1352 osdc->client->options->osd_keepalive_timeout * HZ;
1353 struct list_head slow_osds;
1355 down_read(&osdc->map_sem);
1357 ceph_monc_request_next_osdmap(&osdc->client->monc);
1359 mutex_lock(&osdc->request_mutex);
1362 * ping osds that are a bit slow. this ensures that if there
1363 * is a break in the TCP connection we will notice, and reopen
1364 * a connection with that osd (from the fault callback).
1366 INIT_LIST_HEAD(&slow_osds);
1367 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1368 if (time_before(jiffies, req->r_stamp + keepalive))
1373 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1374 req->r_tid, osd->o_osd);
1375 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1377 while (!list_empty(&slow_osds)) {
1378 osd = list_entry(slow_osds.next, struct ceph_osd,
1380 list_del_init(&osd->o_keepalive_item);
1381 ceph_con_keepalive(&osd->o_con);
1384 __schedule_osd_timeout(osdc);
1385 __send_queued(osdc);
1386 mutex_unlock(&osdc->request_mutex);
1387 up_read(&osdc->map_sem);
1390 static void handle_osds_timeout(struct work_struct *work)
1392 struct ceph_osd_client *osdc =
1393 container_of(work, struct ceph_osd_client,
1394 osds_timeout_work.work);
1395 unsigned long delay =
1396 osdc->client->options->osd_idle_ttl * HZ >> 2;
1398 dout("osds timeout\n");
1399 down_read(&osdc->map_sem);
1400 remove_old_osds(osdc);
1401 up_read(&osdc->map_sem);
1403 schedule_delayed_work(&osdc->osds_timeout_work,
1404 round_jiffies_relative(delay));
1407 static void complete_request(struct ceph_osd_request *req)
1409 if (req->r_unsafe_callback)
1410 req->r_unsafe_callback(req, false);
1411 complete_all(&req->r_safe_completion); /* fsync waiter */
1415 * handle osd op reply. either call the callback if it is specified,
1416 * or do the completion to wake up the waiting thread.
1418 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1419 struct ceph_connection *con)
1422 struct ceph_osd_request *req;
1425 unsigned int numops;
1426 int payload_len, flags;
1432 u64 reassert_version;
1434 int already_completed;
1438 tid = le64_to_cpu(msg->hdr.tid);
1439 dout("handle_reply %p tid %llu\n", msg, tid);
1441 p = msg->front.iov_base;
1442 end = p + msg->front.iov_len;
1444 ceph_decode_need(&p, end, 4, bad);
1445 object_len = ceph_decode_32(&p);
1446 ceph_decode_need(&p, end, object_len, bad);
1449 err = ceph_decode_pgid(&p, end, &pg);
1453 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1454 flags = ceph_decode_64(&p);
1455 result = ceph_decode_32(&p);
1456 reassert_epoch = ceph_decode_32(&p);
1457 reassert_version = ceph_decode_64(&p);
1458 osdmap_epoch = ceph_decode_32(&p);
1461 mutex_lock(&osdc->request_mutex);
1462 req = __lookup_request(osdc, tid);
1464 dout("handle_reply tid %llu dne\n", tid);
1467 ceph_osdc_get_request(req);
1469 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1472 ceph_decode_need(&p, end, 4, bad);
1473 numops = ceph_decode_32(&p);
1474 if (numops > CEPH_OSD_MAX_OP)
1476 if (numops != req->r_num_ops)
1479 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1480 for (i = 0; i < numops; i++) {
1481 struct ceph_osd_op *op = p;
1484 len = le32_to_cpu(op->payload_len);
1485 req->r_reply_op_len[i] = len;
1486 dout(" op %d has %d bytes\n", i, len);
1490 bytes = le32_to_cpu(msg->hdr.data_len);
1491 if (payload_len != bytes) {
1492 pr_warning("sum of op payload lens %d != data_len %d",
1493 payload_len, bytes);
1497 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1498 retry_attempt = ceph_decode_32(&p);
1499 for (i = 0; i < numops; i++)
1500 req->r_reply_op_result[i] = ceph_decode_32(&p);
1502 if (!req->r_got_reply) {
1504 req->r_result = result;
1505 dout("handle_reply result %d bytes %d\n", req->r_result,
1507 if (req->r_result == 0)
1508 req->r_result = bytes;
1510 /* in case this is a write and we need to replay, */
1511 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1512 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1514 req->r_got_reply = 1;
1515 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1516 dout("handle_reply tid %llu dup ack\n", tid);
1517 mutex_unlock(&osdc->request_mutex);
1521 dout("handle_reply tid %llu flags %d\n", tid, flags);
1523 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1524 __register_linger_request(osdc, req);
1526 /* either this is a read, or we got the safe response */
1528 (flags & CEPH_OSD_FLAG_ONDISK) ||
1529 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1530 __unregister_request(osdc, req);
1532 already_completed = req->r_completed;
1533 req->r_completed = 1;
1534 mutex_unlock(&osdc->request_mutex);
1535 if (already_completed)
1538 if (req->r_callback)
1539 req->r_callback(req, msg);
1541 complete_all(&req->r_completion);
1543 if (flags & CEPH_OSD_FLAG_ONDISK)
1544 complete_request(req);
1547 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1548 ceph_osdc_put_request(req);
1552 ceph_osdc_put_request(req);
1554 mutex_unlock(&osdc->request_mutex);
1556 pr_err("corrupt osd_op_reply got %d %d\n",
1557 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1561 static void reset_changed_osds(struct ceph_osd_client *osdc)
1563 struct rb_node *p, *n;
1565 for (p = rb_first(&osdc->osds); p; p = n) {
1566 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1569 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1570 memcmp(&osd->o_con.peer_addr,
1571 ceph_osd_addr(osdc->osdmap,
1573 sizeof(struct ceph_entity_addr)) != 0)
1574 __reset_osd(osdc, osd);
1579 * Requeue requests whose mapping to an OSD has changed. If requests map to
1580 * no osd, request a new map.
1582 * Caller should hold map_sem for read.
1584 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1586 struct ceph_osd_request *req, *nreq;
1591 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1592 mutex_lock(&osdc->request_mutex);
1593 for (p = rb_first(&osdc->requests); p; ) {
1594 req = rb_entry(p, struct ceph_osd_request, r_node);
1598 * For linger requests that have not yet been
1599 * registered, move them to the linger list; they'll
1600 * be sent to the osd in the loop below. Unregister
1601 * the request before re-registering it as a linger
1602 * request to ensure the __map_request() below
1603 * will decide it needs to be sent.
1605 if (req->r_linger && list_empty(&req->r_linger_item)) {
1606 dout("%p tid %llu restart on osd%d\n",
1608 req->r_osd ? req->r_osd->o_osd : -1);
1609 __unregister_request(osdc, req);
1610 __register_linger_request(osdc, req);
1614 err = __map_request(osdc, req, force_resend);
1616 continue; /* error */
1617 if (req->r_osd == NULL) {
1618 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1619 needmap++; /* request a newer map */
1620 } else if (err > 0) {
1621 if (!req->r_linger) {
1622 dout("%p tid %llu requeued on osd%d\n", req,
1624 req->r_osd ? req->r_osd->o_osd : -1);
1625 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1630 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1632 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1634 err = __map_request(osdc, req, force_resend);
1635 dout("__map_request returned %d\n", err);
1637 continue; /* no change and no osd was specified */
1639 continue; /* hrm! */
1640 if (req->r_osd == NULL) {
1641 dout("tid %llu maps to no valid osd\n", req->r_tid);
1642 needmap++; /* request a newer map */
1646 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1647 req->r_osd ? req->r_osd->o_osd : -1);
1648 __register_request(osdc, req);
1649 __unregister_linger_request(osdc, req);
1651 mutex_unlock(&osdc->request_mutex);
1654 dout("%d requests for down osds, need new map\n", needmap);
1655 ceph_monc_request_next_osdmap(&osdc->client->monc);
1657 reset_changed_osds(osdc);
1662 * Process updated osd map.
1664 * The message contains any number of incremental and full maps, normally
1665 * indicating some sort of topology change in the cluster. Kick requests
1666 * off to different OSDs as needed.
1668 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1670 void *p, *end, *next;
1671 u32 nr_maps, maplen;
1673 struct ceph_osdmap *newmap = NULL, *oldmap;
1675 struct ceph_fsid fsid;
1677 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1678 p = msg->front.iov_base;
1679 end = p + msg->front.iov_len;
1682 ceph_decode_need(&p, end, sizeof(fsid), bad);
1683 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1684 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1687 down_write(&osdc->map_sem);
1689 /* incremental maps */
1690 ceph_decode_32_safe(&p, end, nr_maps, bad);
1691 dout(" %d inc maps\n", nr_maps);
1692 while (nr_maps > 0) {
1693 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1694 epoch = ceph_decode_32(&p);
1695 maplen = ceph_decode_32(&p);
1696 ceph_decode_need(&p, end, maplen, bad);
1698 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1699 dout("applying incremental map %u len %d\n",
1701 newmap = osdmap_apply_incremental(&p, next,
1703 &osdc->client->msgr);
1704 if (IS_ERR(newmap)) {
1705 err = PTR_ERR(newmap);
1709 if (newmap != osdc->osdmap) {
1710 ceph_osdmap_destroy(osdc->osdmap);
1711 osdc->osdmap = newmap;
1713 kick_requests(osdc, 0);
1715 dout("ignoring incremental map %u len %d\n",
1725 ceph_decode_32_safe(&p, end, nr_maps, bad);
1726 dout(" %d full maps\n", nr_maps);
1728 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1729 epoch = ceph_decode_32(&p);
1730 maplen = ceph_decode_32(&p);
1731 ceph_decode_need(&p, end, maplen, bad);
1733 dout("skipping non-latest full map %u len %d\n",
1735 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1736 dout("skipping full map %u len %d, "
1737 "older than our %u\n", epoch, maplen,
1738 osdc->osdmap->epoch);
1740 int skipped_map = 0;
1742 dout("taking full map %u len %d\n", epoch, maplen);
1743 newmap = osdmap_decode(&p, p+maplen);
1744 if (IS_ERR(newmap)) {
1745 err = PTR_ERR(newmap);
1749 oldmap = osdc->osdmap;
1750 osdc->osdmap = newmap;
1752 if (oldmap->epoch + 1 < newmap->epoch)
1754 ceph_osdmap_destroy(oldmap);
1756 kick_requests(osdc, skipped_map);
1763 downgrade_write(&osdc->map_sem);
1764 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1767 * subscribe to subsequent osdmap updates if full to ensure
1768 * we find out when we are no longer full and stop returning
1771 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1772 ceph_monc_request_next_osdmap(&osdc->client->monc);
1774 mutex_lock(&osdc->request_mutex);
1775 __send_queued(osdc);
1776 mutex_unlock(&osdc->request_mutex);
1777 up_read(&osdc->map_sem);
1778 wake_up_all(&osdc->client->auth_wq);
1782 pr_err("osdc handle_map corrupt msg\n");
1784 up_write(&osdc->map_sem);
1789 * watch/notify callback event infrastructure
1791 * These callbacks are used both for watch and notify operations.
1793 static void __release_event(struct kref *kref)
1795 struct ceph_osd_event *event =
1796 container_of(kref, struct ceph_osd_event, kref);
1798 dout("__release_event %p\n", event);
1802 static void get_event(struct ceph_osd_event *event)
1804 kref_get(&event->kref);
1807 void ceph_osdc_put_event(struct ceph_osd_event *event)
1809 kref_put(&event->kref, __release_event);
1811 EXPORT_SYMBOL(ceph_osdc_put_event);
1813 static void __insert_event(struct ceph_osd_client *osdc,
1814 struct ceph_osd_event *new)
1816 struct rb_node **p = &osdc->event_tree.rb_node;
1817 struct rb_node *parent = NULL;
1818 struct ceph_osd_event *event = NULL;
1822 event = rb_entry(parent, struct ceph_osd_event, node);
1823 if (new->cookie < event->cookie)
1825 else if (new->cookie > event->cookie)
1826 p = &(*p)->rb_right;
1831 rb_link_node(&new->node, parent, p);
1832 rb_insert_color(&new->node, &osdc->event_tree);
1835 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1838 struct rb_node **p = &osdc->event_tree.rb_node;
1839 struct rb_node *parent = NULL;
1840 struct ceph_osd_event *event = NULL;
1844 event = rb_entry(parent, struct ceph_osd_event, node);
1845 if (cookie < event->cookie)
1847 else if (cookie > event->cookie)
1848 p = &(*p)->rb_right;
1855 static void __remove_event(struct ceph_osd_event *event)
1857 struct ceph_osd_client *osdc = event->osdc;
1859 if (!RB_EMPTY_NODE(&event->node)) {
1860 dout("__remove_event removed %p\n", event);
1861 rb_erase(&event->node, &osdc->event_tree);
1862 ceph_osdc_put_event(event);
1864 dout("__remove_event didn't remove %p\n", event);
1868 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1869 void (*event_cb)(u64, u64, u8, void *),
1870 void *data, struct ceph_osd_event **pevent)
1872 struct ceph_osd_event *event;
1874 event = kmalloc(sizeof(*event), GFP_NOIO);
1878 dout("create_event %p\n", event);
1879 event->cb = event_cb;
1880 event->one_shot = 0;
1883 INIT_LIST_HEAD(&event->osd_node);
1884 RB_CLEAR_NODE(&event->node);
1885 kref_init(&event->kref); /* one ref for us */
1886 kref_get(&event->kref); /* one ref for the caller */
1888 spin_lock(&osdc->event_lock);
1889 event->cookie = ++osdc->event_count;
1890 __insert_event(osdc, event);
1891 spin_unlock(&osdc->event_lock);
1896 EXPORT_SYMBOL(ceph_osdc_create_event);
1898 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1900 struct ceph_osd_client *osdc = event->osdc;
1902 dout("cancel_event %p\n", event);
1903 spin_lock(&osdc->event_lock);
1904 __remove_event(event);
1905 spin_unlock(&osdc->event_lock);
1906 ceph_osdc_put_event(event); /* caller's */
1908 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1911 static void do_event_work(struct work_struct *work)
1913 struct ceph_osd_event_work *event_work =
1914 container_of(work, struct ceph_osd_event_work, work);
1915 struct ceph_osd_event *event = event_work->event;
1916 u64 ver = event_work->ver;
1917 u64 notify_id = event_work->notify_id;
1918 u8 opcode = event_work->opcode;
1920 dout("do_event_work completing %p\n", event);
1921 event->cb(ver, notify_id, opcode, event->data);
1922 dout("do_event_work completed %p\n", event);
1923 ceph_osdc_put_event(event);
1929 * Process osd watch notifications
1931 static void handle_watch_notify(struct ceph_osd_client *osdc,
1932 struct ceph_msg *msg)
1936 u64 cookie, ver, notify_id;
1938 struct ceph_osd_event *event;
1939 struct ceph_osd_event_work *event_work;
1941 p = msg->front.iov_base;
1942 end = p + msg->front.iov_len;
1944 ceph_decode_8_safe(&p, end, proto_ver, bad);
1945 ceph_decode_8_safe(&p, end, opcode, bad);
1946 ceph_decode_64_safe(&p, end, cookie, bad);
1947 ceph_decode_64_safe(&p, end, ver, bad);
1948 ceph_decode_64_safe(&p, end, notify_id, bad);
1950 spin_lock(&osdc->event_lock);
1951 event = __find_event(osdc, cookie);
1953 BUG_ON(event->one_shot);
1956 spin_unlock(&osdc->event_lock);
1957 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1958 cookie, ver, event);
1960 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1962 dout("ERROR: could not allocate event_work\n");
1965 INIT_WORK(&event_work->work, do_event_work);
1966 event_work->event = event;
1967 event_work->ver = ver;
1968 event_work->notify_id = notify_id;
1969 event_work->opcode = opcode;
1970 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1971 dout("WARNING: failed to queue notify event work\n");
1979 ceph_osdc_put_event(event);
1983 pr_err("osdc handle_watch_notify corrupt msg\n");
1988 * build new request AND message
1991 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
1992 struct ceph_snap_context *snapc, u64 snap_id,
1993 struct timespec *mtime)
1995 struct ceph_msg *msg = req->r_request;
1998 int flags = req->r_flags;
2002 req->r_snapid = snap_id;
2003 req->r_snapc = ceph_get_snap_context(snapc);
2005 /* encode request */
2006 msg->hdr.version = cpu_to_le16(4);
2008 p = msg->front.iov_base;
2009 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2010 req->r_request_osdmap_epoch = p;
2012 req->r_request_flags = p;
2014 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2015 ceph_encode_timespec(p, mtime);
2016 p += sizeof(struct ceph_timespec);
2017 req->r_request_reassert_version = p;
2018 p += sizeof(struct ceph_eversion); /* will get filled in */
2021 ceph_encode_8(&p, 4);
2022 ceph_encode_8(&p, 4);
2023 ceph_encode_32(&p, 8 + 4 + 4);
2024 req->r_request_pool = p;
2026 ceph_encode_32(&p, -1); /* preferred */
2027 ceph_encode_32(&p, 0); /* key len */
2029 ceph_encode_8(&p, 1);
2030 req->r_request_pgid = p;
2032 ceph_encode_32(&p, -1); /* preferred */
2035 ceph_encode_32(&p, req->r_oid_len);
2036 memcpy(p, req->r_oid, req->r_oid_len);
2037 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2038 p += req->r_oid_len;
2040 /* ops--can imply data */
2041 ceph_encode_16(&p, (u16)req->r_num_ops);
2043 for (i = 0; i < req->r_num_ops; i++) {
2044 data_len += osd_req_encode_op(req, p, i);
2045 p += sizeof(struct ceph_osd_op);
2049 ceph_encode_64(&p, req->r_snapid);
2050 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2051 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2053 for (i = 0; i < snapc->num_snaps; i++) {
2054 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2058 req->r_request_attempts = p;
2062 if (flags & CEPH_OSD_FLAG_WRITE) {
2066 * The header "data_off" is a hint to the receiver
2067 * allowing it to align received data into its
2068 * buffers such that there's no need to re-copy
2069 * it before writing it to disk (direct I/O).
2071 data_off = (u16) (off & 0xffff);
2072 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2074 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2076 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2077 msg_size = p - msg->front.iov_base;
2078 msg->front.iov_len = msg_size;
2079 msg->hdr.front_len = cpu_to_le32(msg_size);
2081 dout("build_request msg_size was %d\n", (int)msg_size);
2083 EXPORT_SYMBOL(ceph_osdc_build_request);
2086 * Register request, send initial attempt.
2088 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2089 struct ceph_osd_request *req,
2094 down_read(&osdc->map_sem);
2095 mutex_lock(&osdc->request_mutex);
2096 __register_request(osdc, req);
2097 WARN_ON(req->r_sent);
2098 rc = __map_request(osdc, req, 0);
2101 dout("osdc_start_request failed map, "
2102 " will retry %lld\n", req->r_tid);
2107 if (req->r_osd == NULL) {
2108 dout("send_request %p no up osds in pg\n", req);
2109 ceph_monc_request_next_osdmap(&osdc->client->monc);
2111 __send_queued(osdc);
2115 mutex_unlock(&osdc->request_mutex);
2116 up_read(&osdc->map_sem);
2119 EXPORT_SYMBOL(ceph_osdc_start_request);
2122 * wait for a request to complete
2124 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2125 struct ceph_osd_request *req)
2129 rc = wait_for_completion_interruptible(&req->r_completion);
2131 mutex_lock(&osdc->request_mutex);
2132 __cancel_request(req);
2133 __unregister_request(osdc, req);
2134 mutex_unlock(&osdc->request_mutex);
2135 complete_request(req);
2136 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
2140 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2141 return req->r_result;
2143 EXPORT_SYMBOL(ceph_osdc_wait_request);
2146 * sync - wait for all in-flight requests to flush. avoid starvation.
2148 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2150 struct ceph_osd_request *req;
2151 u64 last_tid, next_tid = 0;
2153 mutex_lock(&osdc->request_mutex);
2154 last_tid = osdc->last_tid;
2156 req = __lookup_request_ge(osdc, next_tid);
2159 if (req->r_tid > last_tid)
2162 next_tid = req->r_tid + 1;
2163 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2166 ceph_osdc_get_request(req);
2167 mutex_unlock(&osdc->request_mutex);
2168 dout("sync waiting on tid %llu (last is %llu)\n",
2169 req->r_tid, last_tid);
2170 wait_for_completion(&req->r_safe_completion);
2171 mutex_lock(&osdc->request_mutex);
2172 ceph_osdc_put_request(req);
2174 mutex_unlock(&osdc->request_mutex);
2175 dout("sync done (thru tid %llu)\n", last_tid);
2177 EXPORT_SYMBOL(ceph_osdc_sync);
2182 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2187 osdc->client = client;
2188 osdc->osdmap = NULL;
2189 init_rwsem(&osdc->map_sem);
2190 init_completion(&osdc->map_waiters);
2191 osdc->last_requested_map = 0;
2192 mutex_init(&osdc->request_mutex);
2194 osdc->osds = RB_ROOT;
2195 INIT_LIST_HEAD(&osdc->osd_lru);
2196 osdc->requests = RB_ROOT;
2197 INIT_LIST_HEAD(&osdc->req_lru);
2198 INIT_LIST_HEAD(&osdc->req_unsent);
2199 INIT_LIST_HEAD(&osdc->req_notarget);
2200 INIT_LIST_HEAD(&osdc->req_linger);
2201 osdc->num_requests = 0;
2202 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2203 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2204 spin_lock_init(&osdc->event_lock);
2205 osdc->event_tree = RB_ROOT;
2206 osdc->event_count = 0;
2208 schedule_delayed_work(&osdc->osds_timeout_work,
2209 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2212 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2213 sizeof(struct ceph_osd_request));
2214 if (!osdc->req_mempool)
2217 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2218 OSD_OP_FRONT_LEN, 10, true,
2222 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2223 OSD_OPREPLY_FRONT_LEN, 10, true,
2228 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2229 if (IS_ERR(osdc->notify_wq)) {
2230 err = PTR_ERR(osdc->notify_wq);
2231 osdc->notify_wq = NULL;
2237 ceph_msgpool_destroy(&osdc->msgpool_op);
2239 mempool_destroy(osdc->req_mempool);
2244 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2246 flush_workqueue(osdc->notify_wq);
2247 destroy_workqueue(osdc->notify_wq);
2248 cancel_delayed_work_sync(&osdc->timeout_work);
2249 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2251 ceph_osdmap_destroy(osdc->osdmap);
2252 osdc->osdmap = NULL;
2254 remove_all_osds(osdc);
2255 mempool_destroy(osdc->req_mempool);
2256 ceph_msgpool_destroy(&osdc->msgpool_op);
2257 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2261 * Read some contiguous pages. If we cross a stripe boundary, shorten
2262 * *plen. Return number of bytes read, or error.
2264 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2265 struct ceph_vino vino, struct ceph_file_layout *layout,
2267 u32 truncate_seq, u64 truncate_size,
2268 struct page **pages, int num_pages, int page_align)
2270 struct ceph_osd_request *req;
2273 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2274 vino.snap, off, *plen);
2275 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2276 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2277 NULL, truncate_seq, truncate_size,
2280 return PTR_ERR(req);
2282 /* it may be a short read due to an object boundary */
2284 osd_req_op_extent_osd_data_pages(req, 0,
2285 pages, *plen, page_align, false, false);
2287 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2288 off, *plen, *plen, page_align);
2290 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2292 rc = ceph_osdc_start_request(osdc, req, false);
2294 rc = ceph_osdc_wait_request(osdc, req);
2296 ceph_osdc_put_request(req);
2297 dout("readpages result %d\n", rc);
2300 EXPORT_SYMBOL(ceph_osdc_readpages);
2303 * do a synchronous write on N pages
2305 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2306 struct ceph_file_layout *layout,
2307 struct ceph_snap_context *snapc,
2309 u32 truncate_seq, u64 truncate_size,
2310 struct timespec *mtime,
2311 struct page **pages, int num_pages)
2313 struct ceph_osd_request *req;
2315 int page_align = off & ~PAGE_MASK;
2317 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2318 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2320 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2321 snapc, truncate_seq, truncate_size,
2324 return PTR_ERR(req);
2326 /* it may be a short write due to an object boundary */
2327 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2329 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2331 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2333 rc = ceph_osdc_start_request(osdc, req, true);
2335 rc = ceph_osdc_wait_request(osdc, req);
2337 ceph_osdc_put_request(req);
2340 dout("writepages result %d\n", rc);
2343 EXPORT_SYMBOL(ceph_osdc_writepages);
2346 * handle incoming message
2348 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2350 struct ceph_osd *osd = con->private;
2351 struct ceph_osd_client *osdc;
2352 int type = le16_to_cpu(msg->hdr.type);
2359 case CEPH_MSG_OSD_MAP:
2360 ceph_osdc_handle_map(osdc, msg);
2362 case CEPH_MSG_OSD_OPREPLY:
2363 handle_reply(osdc, msg, con);
2365 case CEPH_MSG_WATCH_NOTIFY:
2366 handle_watch_notify(osdc, msg);
2370 pr_err("received unknown message type %d %s\n", type,
2371 ceph_msg_type_name(type));
2378 * lookup and return message for incoming reply. set up reply message
2381 static struct ceph_msg *get_reply(struct ceph_connection *con,
2382 struct ceph_msg_header *hdr,
2385 struct ceph_osd *osd = con->private;
2386 struct ceph_osd_client *osdc = osd->o_osdc;
2388 struct ceph_osd_request *req;
2389 int front = le32_to_cpu(hdr->front_len);
2390 int data_len = le32_to_cpu(hdr->data_len);
2393 tid = le64_to_cpu(hdr->tid);
2394 mutex_lock(&osdc->request_mutex);
2395 req = __lookup_request(osdc, tid);
2399 dout("get_reply unknown tid %llu from osd%d\n", tid,
2404 if (req->r_reply->con)
2405 dout("%s revoking msg %p from old con %p\n", __func__,
2406 req->r_reply, req->r_reply->con);
2407 ceph_msg_revoke_incoming(req->r_reply);
2409 if (front > req->r_reply->front.iov_len) {
2410 pr_warning("get_reply front %d > preallocated %d\n",
2411 front, (int)req->r_reply->front.iov_len);
2412 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2415 ceph_msg_put(req->r_reply);
2418 m = ceph_msg_get(req->r_reply);
2421 struct ceph_osd_data *osd_data;
2424 * XXX This is assuming there is only one op containing
2425 * XXX page data. Probably OK for reads, but this
2426 * XXX ought to be done more generally.
2428 osd_data = osd_req_op_extent_osd_data(req, 0);
2429 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2430 if (osd_data->pages &&
2431 unlikely(osd_data->length < data_len)) {
2433 pr_warning("tid %lld reply has %d bytes "
2434 "we had only %llu bytes ready\n",
2435 tid, data_len, osd_data->length);
2444 dout("get_reply tid %lld %p\n", tid, m);
2447 mutex_unlock(&osdc->request_mutex);
2452 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2453 struct ceph_msg_header *hdr,
2456 struct ceph_osd *osd = con->private;
2457 int type = le16_to_cpu(hdr->type);
2458 int front = le32_to_cpu(hdr->front_len);
2462 case CEPH_MSG_OSD_MAP:
2463 case CEPH_MSG_WATCH_NOTIFY:
2464 return ceph_msg_new(type, front, GFP_NOFS, false);
2465 case CEPH_MSG_OSD_OPREPLY:
2466 return get_reply(con, hdr, skip);
2468 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2476 * Wrappers to refcount containing ceph_osd struct
2478 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2480 struct ceph_osd *osd = con->private;
2486 static void put_osd_con(struct ceph_connection *con)
2488 struct ceph_osd *osd = con->private;
2496 * Note: returned pointer is the address of a structure that's
2497 * managed separately. Caller must *not* attempt to free it.
2499 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2500 int *proto, int force_new)
2502 struct ceph_osd *o = con->private;
2503 struct ceph_osd_client *osdc = o->o_osdc;
2504 struct ceph_auth_client *ac = osdc->client->monc.auth;
2505 struct ceph_auth_handshake *auth = &o->o_auth;
2507 if (force_new && auth->authorizer) {
2508 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2509 auth->authorizer = NULL;
2511 if (!auth->authorizer) {
2512 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2515 return ERR_PTR(ret);
2517 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2520 return ERR_PTR(ret);
2522 *proto = ac->protocol;
2528 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2530 struct ceph_osd *o = con->private;
2531 struct ceph_osd_client *osdc = o->o_osdc;
2532 struct ceph_auth_client *ac = osdc->client->monc.auth;
2534 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2537 static int invalidate_authorizer(struct ceph_connection *con)
2539 struct ceph_osd *o = con->private;
2540 struct ceph_osd_client *osdc = o->o_osdc;
2541 struct ceph_auth_client *ac = osdc->client->monc.auth;
2543 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2544 return ceph_monc_validate_auth(&osdc->client->monc);
2547 static const struct ceph_connection_operations osd_con_ops = {
2550 .dispatch = dispatch,
2551 .get_authorizer = get_authorizer,
2552 .verify_authorizer_reply = verify_authorizer_reply,
2553 .invalidate_authorizer = invalidate_authorizer,
2554 .alloc_msg = alloc_msg,