2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
6 #include <linux/highmem.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
12 #include <linux/bio.h>
15 #include <linux/ceph/libceph.h>
16 #include <linux/ceph/osd_client.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/pagelist.h>
22 #define OSD_OP_FRONT_LEN 4096
23 #define OSD_OPREPLY_FRONT_LEN 512
25 static const struct ceph_connection_operations osd_con_ops;
27 static void __send_queued(struct ceph_osd_client *osdc);
28 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
29 static void __register_request(struct ceph_osd_client *osdc,
30 struct ceph_osd_request *req);
31 static void __unregister_linger_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
33 static void __send_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
37 * Implement client access to distributed object storage cluster.
39 * All data objects are stored within a cluster/cloud of OSDs, or
40 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
41 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
42 * remote daemons serving up and coordinating consistent and safe
45 * Cluster membership and the mapping of data objects onto storage devices
46 * are described by the osd map.
48 * We keep track of pending OSD requests (read, write), resubmit
49 * requests to different OSDs when the cluster topology/data layout
50 * change, or retry the affected requests when the communications
51 * channel with an OSD is reset.
55 * calculate the mapping of a file extent onto an object, and fill out the
56 * request accordingly. shorten extent as necessary if it crosses an
59 * fill osd op in request message.
61 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
62 u64 *objnum, u64 *objoff, u64 *objlen)
68 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
72 if (*objlen < orig_len) {
74 dout(" skipping last %llu, final file extent %llu~%llu\n",
75 orig_len - *plen, off, *plen);
78 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
83 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
85 memset(osd_data, 0, sizeof (*osd_data));
86 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
89 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
90 struct page **pages, u64 length, u32 alignment,
91 bool pages_from_pool, bool own_pages)
93 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
94 osd_data->pages = pages;
95 osd_data->length = length;
96 osd_data->alignment = alignment;
97 osd_data->pages_from_pool = pages_from_pool;
98 osd_data->own_pages = own_pages;
101 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
102 struct ceph_pagelist *pagelist)
104 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
105 osd_data->pagelist = pagelist;
109 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
110 struct bio *bio, size_t bio_length)
112 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
114 osd_data->bio_length = bio_length;
116 #endif /* CONFIG_BLOCK */
118 struct ceph_osd_data *
119 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
120 unsigned int which, bool write_request)
122 BUG_ON(which >= osd_req->r_num_ops);
124 return &osd_req->r_ops[which].extent.osd_data;
126 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
128 struct ceph_osd_data *
129 osd_req_op_cls_request_info(struct ceph_osd_request *osd_req,
132 BUG_ON(which >= osd_req->r_num_ops);
134 return &osd_req->r_ops[which].cls.request_info;
136 EXPORT_SYMBOL(osd_req_op_cls_request_info); /* ??? */
138 struct ceph_osd_data *
139 osd_req_op_cls_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, bool write_request,
160 struct page **pages, 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, write_request);
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, bool write_request,
173 struct ceph_pagelist *pagelist)
175 struct ceph_osd_data *osd_data;
177 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request);
178 ceph_osd_data_pagelist_init(osd_data, pagelist);
180 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
183 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
184 unsigned int which, bool write_request,
185 struct bio *bio, size_t bio_length)
187 struct ceph_osd_data *osd_data;
189 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request);
190 ceph_osd_data_bio_init(osd_data, bio, bio_length);
192 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
193 #endif /* CONFIG_BLOCK */
195 static void osd_req_op_cls_request_info_pagelist(
196 struct ceph_osd_request *osd_req,
197 unsigned int which, struct ceph_pagelist *pagelist)
199 struct ceph_osd_data *osd_data;
201 osd_data = osd_req_op_cls_request_info(osd_req, which);
202 ceph_osd_data_pagelist_init(osd_data, pagelist);
205 void osd_req_op_cls_request_data_pagelist(
206 struct ceph_osd_request *osd_req,
207 unsigned int which, struct ceph_pagelist *pagelist)
209 struct ceph_osd_data *osd_data;
211 osd_data = osd_req_op_cls_request_data(osd_req, which);
212 ceph_osd_data_pagelist_init(osd_data, pagelist);
214 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
216 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
217 unsigned int which, struct page **pages, u64 length,
218 u32 alignment, bool pages_from_pool, bool own_pages)
220 struct ceph_osd_data *osd_data;
222 osd_data = osd_req_op_cls_response_data(osd_req, which);
223 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
224 pages_from_pool, own_pages);
226 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
228 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
230 switch (osd_data->type) {
231 case CEPH_OSD_DATA_TYPE_NONE:
233 case CEPH_OSD_DATA_TYPE_PAGES:
234 return osd_data->length;
235 case CEPH_OSD_DATA_TYPE_PAGELIST:
236 return (u64)osd_data->pagelist->length;
238 case CEPH_OSD_DATA_TYPE_BIO:
239 return (u64)osd_data->bio_length;
240 #endif /* CONFIG_BLOCK */
242 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
248 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
250 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
253 num_pages = calc_pages_for((u64)osd_data->alignment,
254 (u64)osd_data->length);
255 ceph_release_page_vector(osd_data->pages, num_pages);
257 ceph_osd_data_init(osd_data);
260 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
263 struct ceph_osd_req_op *op;
265 BUG_ON(which >= osd_req->r_num_ops);
266 op = &osd_req->r_ops[which];
269 case CEPH_OSD_OP_READ:
270 case CEPH_OSD_OP_WRITE:
271 ceph_osd_data_release(&op->extent.osd_data);
273 case CEPH_OSD_OP_CALL:
274 ceph_osd_data_release(&op->cls.request_info);
275 ceph_osd_data_release(&op->cls.request_data);
276 ceph_osd_data_release(&op->cls.response_data);
286 void ceph_osdc_release_request(struct kref *kref)
288 struct ceph_osd_request *req;
291 req = container_of(kref, struct ceph_osd_request, r_kref);
293 ceph_msg_put(req->r_request);
295 ceph_msg_revoke_incoming(req->r_reply);
296 ceph_msg_put(req->r_reply);
299 for (which = 0; which < req->r_num_ops; which++)
300 osd_req_op_data_release(req, which);
302 ceph_put_snap_context(req->r_snapc);
304 mempool_free(req, req->r_osdc->req_mempool);
308 EXPORT_SYMBOL(ceph_osdc_release_request);
310 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
311 struct ceph_snap_context *snapc,
312 unsigned int num_ops,
316 struct ceph_osd_request *req;
317 struct ceph_msg *msg;
320 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
321 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
323 msg_size = 4 + 4 + 8 + 8 + 4+8;
324 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
325 msg_size += 1 + 8 + 4 + 4; /* pg_t */
326 msg_size += 4 + MAX_OBJ_NAME_SIZE;
327 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
328 msg_size += 8; /* snapid */
329 msg_size += 8; /* snap_seq */
330 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
334 req = mempool_alloc(osdc->req_mempool, gfp_flags);
335 memset(req, 0, sizeof(*req));
337 req = kzalloc(sizeof(*req), gfp_flags);
343 req->r_mempool = use_mempool;
344 req->r_num_ops = num_ops;
346 kref_init(&req->r_kref);
347 init_completion(&req->r_completion);
348 init_completion(&req->r_safe_completion);
349 RB_CLEAR_NODE(&req->r_node);
350 INIT_LIST_HEAD(&req->r_unsafe_item);
351 INIT_LIST_HEAD(&req->r_linger_item);
352 INIT_LIST_HEAD(&req->r_linger_osd);
353 INIT_LIST_HEAD(&req->r_req_lru_item);
354 INIT_LIST_HEAD(&req->r_osd_item);
356 /* create reply message */
358 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
360 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
361 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
363 ceph_osdc_put_request(req);
368 /* create request message; allow space for oid */
370 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
372 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
374 ceph_osdc_put_request(req);
378 memset(msg->front.iov_base, 0, msg->front.iov_len);
380 req->r_request = msg;
384 EXPORT_SYMBOL(ceph_osdc_alloc_request);
386 static bool osd_req_opcode_valid(u16 opcode)
389 case CEPH_OSD_OP_READ:
390 case CEPH_OSD_OP_STAT:
391 case CEPH_OSD_OP_MAPEXT:
392 case CEPH_OSD_OP_MASKTRUNC:
393 case CEPH_OSD_OP_SPARSE_READ:
394 case CEPH_OSD_OP_NOTIFY:
395 case CEPH_OSD_OP_NOTIFY_ACK:
396 case CEPH_OSD_OP_ASSERT_VER:
397 case CEPH_OSD_OP_WRITE:
398 case CEPH_OSD_OP_WRITEFULL:
399 case CEPH_OSD_OP_TRUNCATE:
400 case CEPH_OSD_OP_ZERO:
401 case CEPH_OSD_OP_DELETE:
402 case CEPH_OSD_OP_APPEND:
403 case CEPH_OSD_OP_STARTSYNC:
404 case CEPH_OSD_OP_SETTRUNC:
405 case CEPH_OSD_OP_TRIMTRUNC:
406 case CEPH_OSD_OP_TMAPUP:
407 case CEPH_OSD_OP_TMAPPUT:
408 case CEPH_OSD_OP_TMAPGET:
409 case CEPH_OSD_OP_CREATE:
410 case CEPH_OSD_OP_ROLLBACK:
411 case CEPH_OSD_OP_WATCH:
412 case CEPH_OSD_OP_OMAPGETKEYS:
413 case CEPH_OSD_OP_OMAPGETVALS:
414 case CEPH_OSD_OP_OMAPGETHEADER:
415 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
416 case CEPH_OSD_OP_OMAPSETVALS:
417 case CEPH_OSD_OP_OMAPSETHEADER:
418 case CEPH_OSD_OP_OMAPCLEAR:
419 case CEPH_OSD_OP_OMAPRMKEYS:
420 case CEPH_OSD_OP_OMAP_CMP:
421 case CEPH_OSD_OP_CLONERANGE:
422 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
423 case CEPH_OSD_OP_SRC_CMPXATTR:
424 case CEPH_OSD_OP_GETXATTR:
425 case CEPH_OSD_OP_GETXATTRS:
426 case CEPH_OSD_OP_CMPXATTR:
427 case CEPH_OSD_OP_SETXATTR:
428 case CEPH_OSD_OP_SETXATTRS:
429 case CEPH_OSD_OP_RESETXATTRS:
430 case CEPH_OSD_OP_RMXATTR:
431 case CEPH_OSD_OP_PULL:
432 case CEPH_OSD_OP_PUSH:
433 case CEPH_OSD_OP_BALANCEREADS:
434 case CEPH_OSD_OP_UNBALANCEREADS:
435 case CEPH_OSD_OP_SCRUB:
436 case CEPH_OSD_OP_SCRUB_RESERVE:
437 case CEPH_OSD_OP_SCRUB_UNRESERVE:
438 case CEPH_OSD_OP_SCRUB_STOP:
439 case CEPH_OSD_OP_SCRUB_MAP:
440 case CEPH_OSD_OP_WRLOCK:
441 case CEPH_OSD_OP_WRUNLOCK:
442 case CEPH_OSD_OP_RDLOCK:
443 case CEPH_OSD_OP_RDUNLOCK:
444 case CEPH_OSD_OP_UPLOCK:
445 case CEPH_OSD_OP_DNLOCK:
446 case CEPH_OSD_OP_CALL:
447 case CEPH_OSD_OP_PGLS:
448 case CEPH_OSD_OP_PGLS_FILTER:
456 * This is an osd op init function for opcodes that have no data or
457 * other information associated with them. It also serves as a
458 * common init routine for all the other init functions, below.
460 static struct ceph_osd_req_op *
461 osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
464 struct ceph_osd_req_op *op;
466 BUG_ON(which >= osd_req->r_num_ops);
467 BUG_ON(!osd_req_opcode_valid(opcode));
469 op = &osd_req->r_ops[which];
470 memset(op, 0, sizeof (*op));
476 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
477 unsigned int which, u16 opcode,
478 u64 offset, u64 length,
479 u64 truncate_size, u32 truncate_seq)
481 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
482 size_t payload_len = 0;
484 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
486 op->extent.offset = offset;
487 op->extent.length = length;
488 op->extent.truncate_size = truncate_size;
489 op->extent.truncate_seq = truncate_seq;
490 if (opcode == CEPH_OSD_OP_WRITE)
491 payload_len += length;
493 op->payload_len = payload_len;
495 EXPORT_SYMBOL(osd_req_op_extent_init);
497 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
498 unsigned int which, u64 length)
500 struct ceph_osd_req_op *op;
503 BUG_ON(which >= osd_req->r_num_ops);
504 op = &osd_req->r_ops[which];
505 previous = op->extent.length;
507 if (length == previous)
508 return; /* Nothing to do */
509 BUG_ON(length > previous);
511 op->extent.length = length;
512 op->payload_len -= previous - length;
514 EXPORT_SYMBOL(osd_req_op_extent_update);
516 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
517 u16 opcode, const char *class, const char *method)
519 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
520 struct ceph_pagelist *pagelist;
521 size_t payload_len = 0;
524 BUG_ON(opcode != CEPH_OSD_OP_CALL);
526 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
528 ceph_pagelist_init(pagelist);
530 op->cls.class_name = class;
531 size = strlen(class);
532 BUG_ON(size > (size_t) U8_MAX);
533 op->cls.class_len = size;
534 ceph_pagelist_append(pagelist, class, size);
537 op->cls.method_name = method;
538 size = strlen(method);
539 BUG_ON(size > (size_t) U8_MAX);
540 op->cls.method_len = size;
541 ceph_pagelist_append(pagelist, method, size);
544 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
546 op->cls.argc = 0; /* currently unused */
548 op->payload_len = payload_len;
550 EXPORT_SYMBOL(osd_req_op_cls_init);
552 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
553 unsigned int which, u16 opcode,
554 u64 cookie, u64 version, int flag)
556 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
558 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
560 op->watch.cookie = cookie;
561 /* op->watch.ver = version; */ /* XXX 3847 */
562 op->watch.ver = cpu_to_le64(version);
563 if (opcode == CEPH_OSD_OP_WATCH && flag)
564 op->watch.flag = (u8)1;
566 EXPORT_SYMBOL(osd_req_op_watch_init);
568 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
569 struct ceph_osd_data *osd_data)
571 u64 length = ceph_osd_data_length(osd_data);
573 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
574 BUG_ON(length > (u64) SIZE_MAX);
576 ceph_msg_data_add_pages(msg, osd_data->pages,
577 length, osd_data->alignment);
578 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
580 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
582 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
583 ceph_msg_data_add_bio(msg, osd_data->bio, length);
586 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
590 static u64 osd_req_encode_op(struct ceph_osd_request *req,
591 struct ceph_osd_op *dst, unsigned int which)
593 struct ceph_osd_req_op *src;
594 struct ceph_osd_data *osd_data;
595 u64 request_data_len = 0;
598 BUG_ON(which >= req->r_num_ops);
599 src = &req->r_ops[which];
600 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
601 pr_err("unrecognized osd opcode %d\n", src->op);
607 case CEPH_OSD_OP_STAT:
609 case CEPH_OSD_OP_READ:
610 case CEPH_OSD_OP_WRITE:
611 if (src->op == CEPH_OSD_OP_WRITE)
612 request_data_len = src->extent.length;
613 dst->extent.offset = cpu_to_le64(src->extent.offset);
614 dst->extent.length = cpu_to_le64(src->extent.length);
615 dst->extent.truncate_size =
616 cpu_to_le64(src->extent.truncate_size);
617 dst->extent.truncate_seq =
618 cpu_to_le32(src->extent.truncate_seq);
619 osd_data = &src->extent.osd_data;
620 if (src->op == CEPH_OSD_OP_WRITE)
621 ceph_osdc_msg_data_add(req->r_request, osd_data);
623 ceph_osdc_msg_data_add(req->r_reply, osd_data);
625 case CEPH_OSD_OP_CALL:
626 dst->cls.class_len = src->cls.class_len;
627 dst->cls.method_len = src->cls.method_len;
628 osd_data = &src->cls.request_info;
629 ceph_osdc_msg_data_add(req->r_request, osd_data);
630 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
631 request_data_len = osd_data->pagelist->length;
633 osd_data = &src->cls.request_data;
634 data_length = ceph_osd_data_length(osd_data);
636 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
637 dst->cls.indata_len = cpu_to_le32(data_length);
638 ceph_osdc_msg_data_add(req->r_request, osd_data);
639 src->payload_len += data_length;
640 request_data_len += data_length;
642 osd_data = &src->cls.response_data;
643 ceph_osdc_msg_data_add(req->r_reply, osd_data);
645 case CEPH_OSD_OP_STARTSYNC:
647 case CEPH_OSD_OP_NOTIFY_ACK:
648 case CEPH_OSD_OP_WATCH:
649 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
650 dst->watch.ver = cpu_to_le64(src->watch.ver);
651 dst->watch.flag = src->watch.flag;
654 pr_err("unsupported osd opcode %s\n",
655 ceph_osd_op_name(src->op));
660 dst->op = cpu_to_le16(src->op);
661 dst->payload_len = cpu_to_le32(src->payload_len);
663 return request_data_len;
667 * build new request AND message, calculate layout, and adjust file
670 * if the file was recently truncated, we include information about its
671 * old and new size so that the object can be updated appropriately. (we
672 * avoid synchronously deleting truncated objects because it's slow.)
674 * if @do_sync, include a 'startsync' command so that the osd will flush
677 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
678 struct ceph_file_layout *layout,
679 struct ceph_vino vino,
680 u64 off, u64 *plen, int num_ops,
681 int opcode, int flags,
682 struct ceph_snap_context *snapc,
687 struct ceph_osd_request *req;
695 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
697 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
700 return ERR_PTR(-ENOMEM);
702 req->r_flags = flags;
704 /* calculate max write size */
705 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
707 ceph_osdc_put_request(req);
711 object_size = le32_to_cpu(layout->fl_object_size);
712 object_base = off - objoff;
713 if (truncate_size <= object_base) {
716 truncate_size -= object_base;
717 if (truncate_size > object_size)
718 truncate_size = object_size;
721 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
722 truncate_size, truncate_seq);
725 * A second op in the ops array means the caller wants to
726 * also issue a include a 'startsync' command so that the
727 * osd will flush data quickly.
730 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
732 req->r_file_layout = *layout; /* keep a copy */
734 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
736 req->r_oid_len = strlen(req->r_oid);
740 EXPORT_SYMBOL(ceph_osdc_new_request);
743 * We keep osd requests in an rbtree, sorted by ->r_tid.
745 static void __insert_request(struct ceph_osd_client *osdc,
746 struct ceph_osd_request *new)
748 struct rb_node **p = &osdc->requests.rb_node;
749 struct rb_node *parent = NULL;
750 struct ceph_osd_request *req = NULL;
754 req = rb_entry(parent, struct ceph_osd_request, r_node);
755 if (new->r_tid < req->r_tid)
757 else if (new->r_tid > req->r_tid)
763 rb_link_node(&new->r_node, parent, p);
764 rb_insert_color(&new->r_node, &osdc->requests);
767 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
770 struct ceph_osd_request *req;
771 struct rb_node *n = osdc->requests.rb_node;
774 req = rb_entry(n, struct ceph_osd_request, r_node);
775 if (tid < req->r_tid)
777 else if (tid > req->r_tid)
785 static struct ceph_osd_request *
786 __lookup_request_ge(struct ceph_osd_client *osdc,
789 struct ceph_osd_request *req;
790 struct rb_node *n = osdc->requests.rb_node;
793 req = rb_entry(n, struct ceph_osd_request, r_node);
794 if (tid < req->r_tid) {
798 } else if (tid > req->r_tid) {
808 * Resubmit requests pending on the given osd.
810 static void __kick_osd_requests(struct ceph_osd_client *osdc,
811 struct ceph_osd *osd)
813 struct ceph_osd_request *req, *nreq;
817 dout("__kick_osd_requests osd%d\n", osd->o_osd);
818 err = __reset_osd(osdc, osd);
822 * Build up a list of requests to resend by traversing the
823 * osd's list of requests. Requests for a given object are
824 * sent in tid order, and that is also the order they're
825 * kept on this list. Therefore all requests that are in
826 * flight will be found first, followed by all requests that
827 * have not yet been sent. And to resend requests while
828 * preserving this order we will want to put any sent
829 * requests back on the front of the osd client's unsent
832 * So we build a separate ordered list of already-sent
833 * requests for the affected osd and splice it onto the
834 * front of the osd client's unsent list. Once we've seen a
835 * request that has not yet been sent we're done. Those
836 * requests are already sitting right where they belong.
838 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
841 list_move_tail(&req->r_req_lru_item, &resend);
842 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
845 req->r_flags |= CEPH_OSD_FLAG_RETRY;
847 list_splice(&resend, &osdc->req_unsent);
850 * Linger requests are re-registered before sending, which
851 * sets up a new tid for each. We add them to the unsent
852 * list at the end to keep things in tid order.
854 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
857 * reregister request prior to unregistering linger so
858 * that r_osd is preserved.
860 BUG_ON(!list_empty(&req->r_req_lru_item));
861 __register_request(osdc, req);
862 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
863 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
864 __unregister_linger_request(osdc, req);
865 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
871 * If the osd connection drops, we need to resubmit all requests.
873 static void osd_reset(struct ceph_connection *con)
875 struct ceph_osd *osd = con->private;
876 struct ceph_osd_client *osdc;
880 dout("osd_reset osd%d\n", osd->o_osd);
882 down_read(&osdc->map_sem);
883 mutex_lock(&osdc->request_mutex);
884 __kick_osd_requests(osdc, osd);
886 mutex_unlock(&osdc->request_mutex);
887 up_read(&osdc->map_sem);
891 * Track open sessions with osds.
893 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
895 struct ceph_osd *osd;
897 osd = kzalloc(sizeof(*osd), GFP_NOFS);
901 atomic_set(&osd->o_ref, 1);
904 RB_CLEAR_NODE(&osd->o_node);
905 INIT_LIST_HEAD(&osd->o_requests);
906 INIT_LIST_HEAD(&osd->o_linger_requests);
907 INIT_LIST_HEAD(&osd->o_osd_lru);
908 osd->o_incarnation = 1;
910 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
912 INIT_LIST_HEAD(&osd->o_keepalive_item);
916 static struct ceph_osd *get_osd(struct ceph_osd *osd)
918 if (atomic_inc_not_zero(&osd->o_ref)) {
919 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
920 atomic_read(&osd->o_ref));
923 dout("get_osd %p FAIL\n", osd);
928 static void put_osd(struct ceph_osd *osd)
930 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
931 atomic_read(&osd->o_ref) - 1);
932 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
933 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
935 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
941 * remove an osd from our map
943 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
945 dout("__remove_osd %p\n", osd);
946 BUG_ON(!list_empty(&osd->o_requests));
947 rb_erase(&osd->o_node, &osdc->osds);
948 list_del_init(&osd->o_osd_lru);
949 ceph_con_close(&osd->o_con);
953 static void remove_all_osds(struct ceph_osd_client *osdc)
955 dout("%s %p\n", __func__, osdc);
956 mutex_lock(&osdc->request_mutex);
957 while (!RB_EMPTY_ROOT(&osdc->osds)) {
958 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
959 struct ceph_osd, o_node);
960 __remove_osd(osdc, osd);
962 mutex_unlock(&osdc->request_mutex);
965 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
966 struct ceph_osd *osd)
968 dout("__move_osd_to_lru %p\n", osd);
969 BUG_ON(!list_empty(&osd->o_osd_lru));
970 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
971 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
974 static void __remove_osd_from_lru(struct ceph_osd *osd)
976 dout("__remove_osd_from_lru %p\n", osd);
977 if (!list_empty(&osd->o_osd_lru))
978 list_del_init(&osd->o_osd_lru);
981 static void remove_old_osds(struct ceph_osd_client *osdc)
983 struct ceph_osd *osd, *nosd;
985 dout("__remove_old_osds %p\n", osdc);
986 mutex_lock(&osdc->request_mutex);
987 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
988 if (time_before(jiffies, osd->lru_ttl))
990 __remove_osd(osdc, osd);
992 mutex_unlock(&osdc->request_mutex);
998 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1000 struct ceph_entity_addr *peer_addr;
1002 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1003 if (list_empty(&osd->o_requests) &&
1004 list_empty(&osd->o_linger_requests)) {
1005 __remove_osd(osdc, osd);
1010 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1011 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1012 !ceph_con_opened(&osd->o_con)) {
1013 struct ceph_osd_request *req;
1015 dout(" osd addr hasn't changed and connection never opened,"
1016 " letting msgr retry");
1017 /* touch each r_stamp for handle_timeout()'s benfit */
1018 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1019 req->r_stamp = jiffies;
1024 ceph_con_close(&osd->o_con);
1025 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1026 osd->o_incarnation++;
1031 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1033 struct rb_node **p = &osdc->osds.rb_node;
1034 struct rb_node *parent = NULL;
1035 struct ceph_osd *osd = NULL;
1037 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1040 osd = rb_entry(parent, struct ceph_osd, o_node);
1041 if (new->o_osd < osd->o_osd)
1043 else if (new->o_osd > osd->o_osd)
1044 p = &(*p)->rb_right;
1049 rb_link_node(&new->o_node, parent, p);
1050 rb_insert_color(&new->o_node, &osdc->osds);
1053 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1055 struct ceph_osd *osd;
1056 struct rb_node *n = osdc->osds.rb_node;
1059 osd = rb_entry(n, struct ceph_osd, o_node);
1062 else if (o > osd->o_osd)
1070 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1072 schedule_delayed_work(&osdc->timeout_work,
1073 osdc->client->options->osd_keepalive_timeout * HZ);
1076 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1078 cancel_delayed_work(&osdc->timeout_work);
1082 * Register request, assign tid. If this is the first request, set up
1083 * the timeout event.
1085 static void __register_request(struct ceph_osd_client *osdc,
1086 struct ceph_osd_request *req)
1088 req->r_tid = ++osdc->last_tid;
1089 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1090 dout("__register_request %p tid %lld\n", req, req->r_tid);
1091 __insert_request(osdc, req);
1092 ceph_osdc_get_request(req);
1093 osdc->num_requests++;
1094 if (osdc->num_requests == 1) {
1095 dout(" first request, scheduling timeout\n");
1096 __schedule_osd_timeout(osdc);
1101 * called under osdc->request_mutex
1103 static void __unregister_request(struct ceph_osd_client *osdc,
1104 struct ceph_osd_request *req)
1106 if (RB_EMPTY_NODE(&req->r_node)) {
1107 dout("__unregister_request %p tid %lld not registered\n",
1112 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1113 rb_erase(&req->r_node, &osdc->requests);
1114 osdc->num_requests--;
1117 /* make sure the original request isn't in flight. */
1118 ceph_msg_revoke(req->r_request);
1120 list_del_init(&req->r_osd_item);
1121 if (list_empty(&req->r_osd->o_requests) &&
1122 list_empty(&req->r_osd->o_linger_requests)) {
1123 dout("moving osd to %p lru\n", req->r_osd);
1124 __move_osd_to_lru(osdc, req->r_osd);
1126 if (list_empty(&req->r_linger_item))
1130 list_del_init(&req->r_req_lru_item);
1131 ceph_osdc_put_request(req);
1133 if (osdc->num_requests == 0) {
1134 dout(" no requests, canceling timeout\n");
1135 __cancel_osd_timeout(osdc);
1140 * Cancel a previously queued request message
1142 static void __cancel_request(struct ceph_osd_request *req)
1144 if (req->r_sent && req->r_osd) {
1145 ceph_msg_revoke(req->r_request);
1150 static void __register_linger_request(struct ceph_osd_client *osdc,
1151 struct ceph_osd_request *req)
1153 dout("__register_linger_request %p\n", req);
1154 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1156 list_add_tail(&req->r_linger_osd,
1157 &req->r_osd->o_linger_requests);
1160 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1161 struct ceph_osd_request *req)
1163 dout("__unregister_linger_request %p\n", req);
1164 list_del_init(&req->r_linger_item);
1166 list_del_init(&req->r_linger_osd);
1168 if (list_empty(&req->r_osd->o_requests) &&
1169 list_empty(&req->r_osd->o_linger_requests)) {
1170 dout("moving osd to %p lru\n", req->r_osd);
1171 __move_osd_to_lru(osdc, req->r_osd);
1173 if (list_empty(&req->r_osd_item))
1178 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1179 struct ceph_osd_request *req)
1181 mutex_lock(&osdc->request_mutex);
1182 if (req->r_linger) {
1183 __unregister_linger_request(osdc, req);
1184 ceph_osdc_put_request(req);
1186 mutex_unlock(&osdc->request_mutex);
1188 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1190 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1191 struct ceph_osd_request *req)
1193 if (!req->r_linger) {
1194 dout("set_request_linger %p\n", req);
1197 * caller is now responsible for calling
1198 * unregister_linger_request
1200 ceph_osdc_get_request(req);
1203 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1206 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1207 * (as needed), and set the request r_osd appropriately. If there is
1208 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1209 * (unsent, homeless) or leave on in-flight lru.
1211 * Return 0 if unchanged, 1 if changed, or negative on error.
1213 * Caller should hold map_sem for read and request_mutex.
1215 static int __map_request(struct ceph_osd_client *osdc,
1216 struct ceph_osd_request *req, int force_resend)
1218 struct ceph_pg pgid;
1219 int acting[CEPH_PG_MAX_SIZE];
1220 int o = -1, num = 0;
1223 dout("map_request %p tid %lld\n", req, req->r_tid);
1224 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1225 ceph_file_layout_pg_pool(req->r_file_layout));
1227 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1232 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1238 if ((!force_resend &&
1239 req->r_osd && req->r_osd->o_osd == o &&
1240 req->r_sent >= req->r_osd->o_incarnation &&
1241 req->r_num_pg_osds == num &&
1242 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1243 (req->r_osd == NULL && o == -1))
1244 return 0; /* no change */
1246 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1247 req->r_tid, pgid.pool, pgid.seed, o,
1248 req->r_osd ? req->r_osd->o_osd : -1);
1250 /* record full pg acting set */
1251 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1252 req->r_num_pg_osds = num;
1255 __cancel_request(req);
1256 list_del_init(&req->r_osd_item);
1260 req->r_osd = __lookup_osd(osdc, o);
1261 if (!req->r_osd && o >= 0) {
1263 req->r_osd = create_osd(osdc, o);
1265 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1269 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1270 __insert_osd(osdc, req->r_osd);
1272 ceph_con_open(&req->r_osd->o_con,
1273 CEPH_ENTITY_TYPE_OSD, o,
1274 &osdc->osdmap->osd_addr[o]);
1278 __remove_osd_from_lru(req->r_osd);
1279 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1280 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1282 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1284 err = 1; /* osd or pg changed */
1291 * caller should hold map_sem (for read) and request_mutex
1293 static void __send_request(struct ceph_osd_client *osdc,
1294 struct ceph_osd_request *req)
1298 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1299 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1300 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1302 /* fill in message content that changes each time we send it */
1303 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1304 put_unaligned_le32(req->r_flags, req->r_request_flags);
1305 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1306 p = req->r_request_pgid;
1307 ceph_encode_64(&p, req->r_pgid.pool);
1308 ceph_encode_32(&p, req->r_pgid.seed);
1309 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1310 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1311 sizeof(req->r_reassert_version));
1313 req->r_stamp = jiffies;
1314 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1316 ceph_msg_get(req->r_request); /* send consumes a ref */
1318 /* Mark the request unsafe if this is the first timet's being sent. */
1320 if (!req->r_sent && req->r_unsafe_callback)
1321 req->r_unsafe_callback(req, true);
1322 req->r_sent = req->r_osd->o_incarnation;
1324 ceph_con_send(&req->r_osd->o_con, req->r_request);
1328 * Send any requests in the queue (req_unsent).
1330 static void __send_queued(struct ceph_osd_client *osdc)
1332 struct ceph_osd_request *req, *tmp;
1334 dout("__send_queued\n");
1335 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1336 __send_request(osdc, req);
1340 * Timeout callback, called every N seconds when 1 or more osd
1341 * requests has been active for more than N seconds. When this
1342 * happens, we ping all OSDs with requests who have timed out to
1343 * ensure any communications channel reset is detected. Reset the
1344 * request timeouts another N seconds in the future as we go.
1345 * Reschedule the timeout event another N seconds in future (unless
1346 * there are no open requests).
1348 static void handle_timeout(struct work_struct *work)
1350 struct ceph_osd_client *osdc =
1351 container_of(work, struct ceph_osd_client, timeout_work.work);
1352 struct ceph_osd_request *req;
1353 struct ceph_osd *osd;
1354 unsigned long keepalive =
1355 osdc->client->options->osd_keepalive_timeout * HZ;
1356 struct list_head slow_osds;
1358 down_read(&osdc->map_sem);
1360 ceph_monc_request_next_osdmap(&osdc->client->monc);
1362 mutex_lock(&osdc->request_mutex);
1365 * ping osds that are a bit slow. this ensures that if there
1366 * is a break in the TCP connection we will notice, and reopen
1367 * a connection with that osd (from the fault callback).
1369 INIT_LIST_HEAD(&slow_osds);
1370 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1371 if (time_before(jiffies, req->r_stamp + keepalive))
1376 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1377 req->r_tid, osd->o_osd);
1378 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1380 while (!list_empty(&slow_osds)) {
1381 osd = list_entry(slow_osds.next, struct ceph_osd,
1383 list_del_init(&osd->o_keepalive_item);
1384 ceph_con_keepalive(&osd->o_con);
1387 __schedule_osd_timeout(osdc);
1388 __send_queued(osdc);
1389 mutex_unlock(&osdc->request_mutex);
1390 up_read(&osdc->map_sem);
1393 static void handle_osds_timeout(struct work_struct *work)
1395 struct ceph_osd_client *osdc =
1396 container_of(work, struct ceph_osd_client,
1397 osds_timeout_work.work);
1398 unsigned long delay =
1399 osdc->client->options->osd_idle_ttl * HZ >> 2;
1401 dout("osds timeout\n");
1402 down_read(&osdc->map_sem);
1403 remove_old_osds(osdc);
1404 up_read(&osdc->map_sem);
1406 schedule_delayed_work(&osdc->osds_timeout_work,
1407 round_jiffies_relative(delay));
1410 static void complete_request(struct ceph_osd_request *req)
1412 if (req->r_unsafe_callback)
1413 req->r_unsafe_callback(req, false);
1414 complete_all(&req->r_safe_completion); /* fsync waiter */
1418 * handle osd op reply. either call the callback if it is specified,
1419 * or do the completion to wake up the waiting thread.
1421 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1422 struct ceph_connection *con)
1425 struct ceph_osd_request *req;
1428 unsigned int numops;
1429 int payload_len, flags;
1435 u64 reassert_version;
1437 int already_completed;
1441 tid = le64_to_cpu(msg->hdr.tid);
1442 dout("handle_reply %p tid %llu\n", msg, tid);
1444 p = msg->front.iov_base;
1445 end = p + msg->front.iov_len;
1447 ceph_decode_need(&p, end, 4, bad);
1448 object_len = ceph_decode_32(&p);
1449 ceph_decode_need(&p, end, object_len, bad);
1452 err = ceph_decode_pgid(&p, end, &pg);
1456 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1457 flags = ceph_decode_64(&p);
1458 result = ceph_decode_32(&p);
1459 reassert_epoch = ceph_decode_32(&p);
1460 reassert_version = ceph_decode_64(&p);
1461 osdmap_epoch = ceph_decode_32(&p);
1464 mutex_lock(&osdc->request_mutex);
1465 req = __lookup_request(osdc, tid);
1467 dout("handle_reply tid %llu dne\n", tid);
1470 ceph_osdc_get_request(req);
1472 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1475 ceph_decode_need(&p, end, 4, bad);
1476 numops = ceph_decode_32(&p);
1477 if (numops > CEPH_OSD_MAX_OP)
1479 if (numops != req->r_num_ops)
1482 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1483 for (i = 0; i < numops; i++) {
1484 struct ceph_osd_op *op = p;
1487 len = le32_to_cpu(op->payload_len);
1488 req->r_reply_op_len[i] = len;
1489 dout(" op %d has %d bytes\n", i, len);
1493 bytes = le32_to_cpu(msg->hdr.data_len);
1494 if (payload_len != bytes) {
1495 pr_warning("sum of op payload lens %d != data_len %d",
1496 payload_len, bytes);
1500 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1501 retry_attempt = ceph_decode_32(&p);
1502 for (i = 0; i < numops; i++)
1503 req->r_reply_op_result[i] = ceph_decode_32(&p);
1505 if (!req->r_got_reply) {
1507 req->r_result = result;
1508 dout("handle_reply result %d bytes %d\n", req->r_result,
1510 if (req->r_result == 0)
1511 req->r_result = bytes;
1513 /* in case this is a write and we need to replay, */
1514 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1515 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1517 req->r_got_reply = 1;
1518 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1519 dout("handle_reply tid %llu dup ack\n", tid);
1520 mutex_unlock(&osdc->request_mutex);
1524 dout("handle_reply tid %llu flags %d\n", tid, flags);
1526 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1527 __register_linger_request(osdc, req);
1529 /* either this is a read, or we got the safe response */
1531 (flags & CEPH_OSD_FLAG_ONDISK) ||
1532 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1533 __unregister_request(osdc, req);
1535 already_completed = req->r_completed;
1536 req->r_completed = 1;
1537 mutex_unlock(&osdc->request_mutex);
1538 if (already_completed)
1541 if (req->r_callback)
1542 req->r_callback(req, msg);
1544 complete_all(&req->r_completion);
1546 if (flags & CEPH_OSD_FLAG_ONDISK)
1547 complete_request(req);
1550 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1551 ceph_osdc_put_request(req);
1555 ceph_osdc_put_request(req);
1557 mutex_unlock(&osdc->request_mutex);
1559 pr_err("corrupt osd_op_reply got %d %d\n",
1560 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1564 static void reset_changed_osds(struct ceph_osd_client *osdc)
1566 struct rb_node *p, *n;
1568 for (p = rb_first(&osdc->osds); p; p = n) {
1569 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1572 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1573 memcmp(&osd->o_con.peer_addr,
1574 ceph_osd_addr(osdc->osdmap,
1576 sizeof(struct ceph_entity_addr)) != 0)
1577 __reset_osd(osdc, osd);
1582 * Requeue requests whose mapping to an OSD has changed. If requests map to
1583 * no osd, request a new map.
1585 * Caller should hold map_sem for read.
1587 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1589 struct ceph_osd_request *req, *nreq;
1594 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1595 mutex_lock(&osdc->request_mutex);
1596 for (p = rb_first(&osdc->requests); p; ) {
1597 req = rb_entry(p, struct ceph_osd_request, r_node);
1601 * For linger requests that have not yet been
1602 * registered, move them to the linger list; they'll
1603 * be sent to the osd in the loop below. Unregister
1604 * the request before re-registering it as a linger
1605 * request to ensure the __map_request() below
1606 * will decide it needs to be sent.
1608 if (req->r_linger && list_empty(&req->r_linger_item)) {
1609 dout("%p tid %llu restart on osd%d\n",
1611 req->r_osd ? req->r_osd->o_osd : -1);
1612 __unregister_request(osdc, req);
1613 __register_linger_request(osdc, req);
1617 err = __map_request(osdc, req, force_resend);
1619 continue; /* error */
1620 if (req->r_osd == NULL) {
1621 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1622 needmap++; /* request a newer map */
1623 } else if (err > 0) {
1624 if (!req->r_linger) {
1625 dout("%p tid %llu requeued on osd%d\n", req,
1627 req->r_osd ? req->r_osd->o_osd : -1);
1628 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1633 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1635 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1637 err = __map_request(osdc, req, force_resend);
1638 dout("__map_request returned %d\n", err);
1640 continue; /* no change and no osd was specified */
1642 continue; /* hrm! */
1643 if (req->r_osd == NULL) {
1644 dout("tid %llu maps to no valid osd\n", req->r_tid);
1645 needmap++; /* request a newer map */
1649 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1650 req->r_osd ? req->r_osd->o_osd : -1);
1651 __register_request(osdc, req);
1652 __unregister_linger_request(osdc, req);
1654 mutex_unlock(&osdc->request_mutex);
1657 dout("%d requests for down osds, need new map\n", needmap);
1658 ceph_monc_request_next_osdmap(&osdc->client->monc);
1660 reset_changed_osds(osdc);
1665 * Process updated osd map.
1667 * The message contains any number of incremental and full maps, normally
1668 * indicating some sort of topology change in the cluster. Kick requests
1669 * off to different OSDs as needed.
1671 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1673 void *p, *end, *next;
1674 u32 nr_maps, maplen;
1676 struct ceph_osdmap *newmap = NULL, *oldmap;
1678 struct ceph_fsid fsid;
1680 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1681 p = msg->front.iov_base;
1682 end = p + msg->front.iov_len;
1685 ceph_decode_need(&p, end, sizeof(fsid), bad);
1686 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1687 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1690 down_write(&osdc->map_sem);
1692 /* incremental maps */
1693 ceph_decode_32_safe(&p, end, nr_maps, bad);
1694 dout(" %d inc maps\n", nr_maps);
1695 while (nr_maps > 0) {
1696 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1697 epoch = ceph_decode_32(&p);
1698 maplen = ceph_decode_32(&p);
1699 ceph_decode_need(&p, end, maplen, bad);
1701 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1702 dout("applying incremental map %u len %d\n",
1704 newmap = osdmap_apply_incremental(&p, next,
1706 &osdc->client->msgr);
1707 if (IS_ERR(newmap)) {
1708 err = PTR_ERR(newmap);
1712 if (newmap != osdc->osdmap) {
1713 ceph_osdmap_destroy(osdc->osdmap);
1714 osdc->osdmap = newmap;
1716 kick_requests(osdc, 0);
1718 dout("ignoring incremental map %u len %d\n",
1728 ceph_decode_32_safe(&p, end, nr_maps, bad);
1729 dout(" %d full maps\n", nr_maps);
1731 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1732 epoch = ceph_decode_32(&p);
1733 maplen = ceph_decode_32(&p);
1734 ceph_decode_need(&p, end, maplen, bad);
1736 dout("skipping non-latest full map %u len %d\n",
1738 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1739 dout("skipping full map %u len %d, "
1740 "older than our %u\n", epoch, maplen,
1741 osdc->osdmap->epoch);
1743 int skipped_map = 0;
1745 dout("taking full map %u len %d\n", epoch, maplen);
1746 newmap = osdmap_decode(&p, p+maplen);
1747 if (IS_ERR(newmap)) {
1748 err = PTR_ERR(newmap);
1752 oldmap = osdc->osdmap;
1753 osdc->osdmap = newmap;
1755 if (oldmap->epoch + 1 < newmap->epoch)
1757 ceph_osdmap_destroy(oldmap);
1759 kick_requests(osdc, skipped_map);
1766 downgrade_write(&osdc->map_sem);
1767 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1770 * subscribe to subsequent osdmap updates if full to ensure
1771 * we find out when we are no longer full and stop returning
1774 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1775 ceph_monc_request_next_osdmap(&osdc->client->monc);
1777 mutex_lock(&osdc->request_mutex);
1778 __send_queued(osdc);
1779 mutex_unlock(&osdc->request_mutex);
1780 up_read(&osdc->map_sem);
1781 wake_up_all(&osdc->client->auth_wq);
1785 pr_err("osdc handle_map corrupt msg\n");
1787 up_write(&osdc->map_sem);
1792 * watch/notify callback event infrastructure
1794 * These callbacks are used both for watch and notify operations.
1796 static void __release_event(struct kref *kref)
1798 struct ceph_osd_event *event =
1799 container_of(kref, struct ceph_osd_event, kref);
1801 dout("__release_event %p\n", event);
1805 static void get_event(struct ceph_osd_event *event)
1807 kref_get(&event->kref);
1810 void ceph_osdc_put_event(struct ceph_osd_event *event)
1812 kref_put(&event->kref, __release_event);
1814 EXPORT_SYMBOL(ceph_osdc_put_event);
1816 static void __insert_event(struct ceph_osd_client *osdc,
1817 struct ceph_osd_event *new)
1819 struct rb_node **p = &osdc->event_tree.rb_node;
1820 struct rb_node *parent = NULL;
1821 struct ceph_osd_event *event = NULL;
1825 event = rb_entry(parent, struct ceph_osd_event, node);
1826 if (new->cookie < event->cookie)
1828 else if (new->cookie > event->cookie)
1829 p = &(*p)->rb_right;
1834 rb_link_node(&new->node, parent, p);
1835 rb_insert_color(&new->node, &osdc->event_tree);
1838 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1841 struct rb_node **p = &osdc->event_tree.rb_node;
1842 struct rb_node *parent = NULL;
1843 struct ceph_osd_event *event = NULL;
1847 event = rb_entry(parent, struct ceph_osd_event, node);
1848 if (cookie < event->cookie)
1850 else if (cookie > event->cookie)
1851 p = &(*p)->rb_right;
1858 static void __remove_event(struct ceph_osd_event *event)
1860 struct ceph_osd_client *osdc = event->osdc;
1862 if (!RB_EMPTY_NODE(&event->node)) {
1863 dout("__remove_event removed %p\n", event);
1864 rb_erase(&event->node, &osdc->event_tree);
1865 ceph_osdc_put_event(event);
1867 dout("__remove_event didn't remove %p\n", event);
1871 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1872 void (*event_cb)(u64, u64, u8, void *),
1873 void *data, struct ceph_osd_event **pevent)
1875 struct ceph_osd_event *event;
1877 event = kmalloc(sizeof(*event), GFP_NOIO);
1881 dout("create_event %p\n", event);
1882 event->cb = event_cb;
1883 event->one_shot = 0;
1886 INIT_LIST_HEAD(&event->osd_node);
1887 RB_CLEAR_NODE(&event->node);
1888 kref_init(&event->kref); /* one ref for us */
1889 kref_get(&event->kref); /* one ref for the caller */
1891 spin_lock(&osdc->event_lock);
1892 event->cookie = ++osdc->event_count;
1893 __insert_event(osdc, event);
1894 spin_unlock(&osdc->event_lock);
1899 EXPORT_SYMBOL(ceph_osdc_create_event);
1901 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1903 struct ceph_osd_client *osdc = event->osdc;
1905 dout("cancel_event %p\n", event);
1906 spin_lock(&osdc->event_lock);
1907 __remove_event(event);
1908 spin_unlock(&osdc->event_lock);
1909 ceph_osdc_put_event(event); /* caller's */
1911 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1914 static void do_event_work(struct work_struct *work)
1916 struct ceph_osd_event_work *event_work =
1917 container_of(work, struct ceph_osd_event_work, work);
1918 struct ceph_osd_event *event = event_work->event;
1919 u64 ver = event_work->ver;
1920 u64 notify_id = event_work->notify_id;
1921 u8 opcode = event_work->opcode;
1923 dout("do_event_work completing %p\n", event);
1924 event->cb(ver, notify_id, opcode, event->data);
1925 dout("do_event_work completed %p\n", event);
1926 ceph_osdc_put_event(event);
1932 * Process osd watch notifications
1934 static void handle_watch_notify(struct ceph_osd_client *osdc,
1935 struct ceph_msg *msg)
1939 u64 cookie, ver, notify_id;
1941 struct ceph_osd_event *event;
1942 struct ceph_osd_event_work *event_work;
1944 p = msg->front.iov_base;
1945 end = p + msg->front.iov_len;
1947 ceph_decode_8_safe(&p, end, proto_ver, bad);
1948 ceph_decode_8_safe(&p, end, opcode, bad);
1949 ceph_decode_64_safe(&p, end, cookie, bad);
1950 ceph_decode_64_safe(&p, end, ver, bad);
1951 ceph_decode_64_safe(&p, end, notify_id, bad);
1953 spin_lock(&osdc->event_lock);
1954 event = __find_event(osdc, cookie);
1956 BUG_ON(event->one_shot);
1959 spin_unlock(&osdc->event_lock);
1960 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1961 cookie, ver, event);
1963 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1965 dout("ERROR: could not allocate event_work\n");
1968 INIT_WORK(&event_work->work, do_event_work);
1969 event_work->event = event;
1970 event_work->ver = ver;
1971 event_work->notify_id = notify_id;
1972 event_work->opcode = opcode;
1973 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1974 dout("WARNING: failed to queue notify event work\n");
1982 ceph_osdc_put_event(event);
1986 pr_err("osdc handle_watch_notify corrupt msg\n");
1991 * build new request AND message
1994 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
1995 struct ceph_snap_context *snapc, u64 snap_id,
1996 struct timespec *mtime)
1998 struct ceph_msg *msg = req->r_request;
2001 int flags = req->r_flags;
2005 req->r_snapid = snap_id;
2006 req->r_snapc = ceph_get_snap_context(snapc);
2008 /* encode request */
2009 msg->hdr.version = cpu_to_le16(4);
2011 p = msg->front.iov_base;
2012 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2013 req->r_request_osdmap_epoch = p;
2015 req->r_request_flags = p;
2017 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2018 ceph_encode_timespec(p, mtime);
2019 p += sizeof(struct ceph_timespec);
2020 req->r_request_reassert_version = p;
2021 p += sizeof(struct ceph_eversion); /* will get filled in */
2024 ceph_encode_8(&p, 4);
2025 ceph_encode_8(&p, 4);
2026 ceph_encode_32(&p, 8 + 4 + 4);
2027 req->r_request_pool = p;
2029 ceph_encode_32(&p, -1); /* preferred */
2030 ceph_encode_32(&p, 0); /* key len */
2032 ceph_encode_8(&p, 1);
2033 req->r_request_pgid = p;
2035 ceph_encode_32(&p, -1); /* preferred */
2038 ceph_encode_32(&p, req->r_oid_len);
2039 memcpy(p, req->r_oid, req->r_oid_len);
2040 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2041 p += req->r_oid_len;
2043 /* ops--can imply data */
2044 ceph_encode_16(&p, (u16)req->r_num_ops);
2046 for (i = 0; i < req->r_num_ops; i++) {
2047 data_len += osd_req_encode_op(req, p, i);
2048 p += sizeof(struct ceph_osd_op);
2052 ceph_encode_64(&p, req->r_snapid);
2053 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2054 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2056 for (i = 0; i < snapc->num_snaps; i++) {
2057 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2061 req->r_request_attempts = p;
2065 if (flags & CEPH_OSD_FLAG_WRITE) {
2069 * The header "data_off" is a hint to the receiver
2070 * allowing it to align received data into its
2071 * buffers such that there's no need to re-copy
2072 * it before writing it to disk (direct I/O).
2074 data_off = (u16) (off & 0xffff);
2075 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2077 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2079 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2080 msg_size = p - msg->front.iov_base;
2081 msg->front.iov_len = msg_size;
2082 msg->hdr.front_len = cpu_to_le32(msg_size);
2084 dout("build_request msg_size was %d\n", (int)msg_size);
2086 EXPORT_SYMBOL(ceph_osdc_build_request);
2089 * Register request, send initial attempt.
2091 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2092 struct ceph_osd_request *req,
2097 down_read(&osdc->map_sem);
2098 mutex_lock(&osdc->request_mutex);
2099 __register_request(osdc, req);
2100 WARN_ON(req->r_sent);
2101 rc = __map_request(osdc, req, 0);
2104 dout("osdc_start_request failed map, "
2105 " will retry %lld\n", req->r_tid);
2110 if (req->r_osd == NULL) {
2111 dout("send_request %p no up osds in pg\n", req);
2112 ceph_monc_request_next_osdmap(&osdc->client->monc);
2114 __send_queued(osdc);
2118 mutex_unlock(&osdc->request_mutex);
2119 up_read(&osdc->map_sem);
2122 EXPORT_SYMBOL(ceph_osdc_start_request);
2125 * wait for a request to complete
2127 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2128 struct ceph_osd_request *req)
2132 rc = wait_for_completion_interruptible(&req->r_completion);
2134 mutex_lock(&osdc->request_mutex);
2135 __cancel_request(req);
2136 __unregister_request(osdc, req);
2137 mutex_unlock(&osdc->request_mutex);
2138 complete_request(req);
2139 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
2143 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2144 return req->r_result;
2146 EXPORT_SYMBOL(ceph_osdc_wait_request);
2149 * sync - wait for all in-flight requests to flush. avoid starvation.
2151 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2153 struct ceph_osd_request *req;
2154 u64 last_tid, next_tid = 0;
2156 mutex_lock(&osdc->request_mutex);
2157 last_tid = osdc->last_tid;
2159 req = __lookup_request_ge(osdc, next_tid);
2162 if (req->r_tid > last_tid)
2165 next_tid = req->r_tid + 1;
2166 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2169 ceph_osdc_get_request(req);
2170 mutex_unlock(&osdc->request_mutex);
2171 dout("sync waiting on tid %llu (last is %llu)\n",
2172 req->r_tid, last_tid);
2173 wait_for_completion(&req->r_safe_completion);
2174 mutex_lock(&osdc->request_mutex);
2175 ceph_osdc_put_request(req);
2177 mutex_unlock(&osdc->request_mutex);
2178 dout("sync done (thru tid %llu)\n", last_tid);
2180 EXPORT_SYMBOL(ceph_osdc_sync);
2185 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2190 osdc->client = client;
2191 osdc->osdmap = NULL;
2192 init_rwsem(&osdc->map_sem);
2193 init_completion(&osdc->map_waiters);
2194 osdc->last_requested_map = 0;
2195 mutex_init(&osdc->request_mutex);
2197 osdc->osds = RB_ROOT;
2198 INIT_LIST_HEAD(&osdc->osd_lru);
2199 osdc->requests = RB_ROOT;
2200 INIT_LIST_HEAD(&osdc->req_lru);
2201 INIT_LIST_HEAD(&osdc->req_unsent);
2202 INIT_LIST_HEAD(&osdc->req_notarget);
2203 INIT_LIST_HEAD(&osdc->req_linger);
2204 osdc->num_requests = 0;
2205 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2206 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2207 spin_lock_init(&osdc->event_lock);
2208 osdc->event_tree = RB_ROOT;
2209 osdc->event_count = 0;
2211 schedule_delayed_work(&osdc->osds_timeout_work,
2212 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2215 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2216 sizeof(struct ceph_osd_request));
2217 if (!osdc->req_mempool)
2220 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2221 OSD_OP_FRONT_LEN, 10, true,
2225 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2226 OSD_OPREPLY_FRONT_LEN, 10, true,
2231 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2232 if (IS_ERR(osdc->notify_wq)) {
2233 err = PTR_ERR(osdc->notify_wq);
2234 osdc->notify_wq = NULL;
2240 ceph_msgpool_destroy(&osdc->msgpool_op);
2242 mempool_destroy(osdc->req_mempool);
2247 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2249 flush_workqueue(osdc->notify_wq);
2250 destroy_workqueue(osdc->notify_wq);
2251 cancel_delayed_work_sync(&osdc->timeout_work);
2252 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2254 ceph_osdmap_destroy(osdc->osdmap);
2255 osdc->osdmap = NULL;
2257 remove_all_osds(osdc);
2258 mempool_destroy(osdc->req_mempool);
2259 ceph_msgpool_destroy(&osdc->msgpool_op);
2260 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2264 * Read some contiguous pages. If we cross a stripe boundary, shorten
2265 * *plen. Return number of bytes read, or error.
2267 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2268 struct ceph_vino vino, struct ceph_file_layout *layout,
2270 u32 truncate_seq, u64 truncate_size,
2271 struct page **pages, int num_pages, int page_align)
2273 struct ceph_osd_request *req;
2276 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2277 vino.snap, off, *plen);
2278 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2279 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2280 NULL, truncate_seq, truncate_size,
2283 return PTR_ERR(req);
2285 /* it may be a short read due to an object boundary */
2287 osd_req_op_extent_osd_data_pages(req, 0, false,
2288 pages, *plen, page_align, false, false);
2290 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2291 off, *plen, *plen, page_align);
2293 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2295 rc = ceph_osdc_start_request(osdc, req, false);
2297 rc = ceph_osdc_wait_request(osdc, req);
2299 ceph_osdc_put_request(req);
2300 dout("readpages result %d\n", rc);
2303 EXPORT_SYMBOL(ceph_osdc_readpages);
2306 * do a synchronous write on N pages
2308 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2309 struct ceph_file_layout *layout,
2310 struct ceph_snap_context *snapc,
2312 u32 truncate_seq, u64 truncate_size,
2313 struct timespec *mtime,
2314 struct page **pages, int num_pages)
2316 struct ceph_osd_request *req;
2318 int page_align = off & ~PAGE_MASK;
2320 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2321 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2323 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2324 snapc, truncate_seq, truncate_size,
2327 return PTR_ERR(req);
2329 /* it may be a short write due to an object boundary */
2330 osd_req_op_extent_osd_data_pages(req, 0, true, pages, len, page_align,
2332 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2334 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2336 rc = ceph_osdc_start_request(osdc, req, true);
2338 rc = ceph_osdc_wait_request(osdc, req);
2340 ceph_osdc_put_request(req);
2343 dout("writepages result %d\n", rc);
2346 EXPORT_SYMBOL(ceph_osdc_writepages);
2349 * handle incoming message
2351 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2353 struct ceph_osd *osd = con->private;
2354 struct ceph_osd_client *osdc;
2355 int type = le16_to_cpu(msg->hdr.type);
2362 case CEPH_MSG_OSD_MAP:
2363 ceph_osdc_handle_map(osdc, msg);
2365 case CEPH_MSG_OSD_OPREPLY:
2366 handle_reply(osdc, msg, con);
2368 case CEPH_MSG_WATCH_NOTIFY:
2369 handle_watch_notify(osdc, msg);
2373 pr_err("received unknown message type %d %s\n", type,
2374 ceph_msg_type_name(type));
2381 * lookup and return message for incoming reply. set up reply message
2384 static struct ceph_msg *get_reply(struct ceph_connection *con,
2385 struct ceph_msg_header *hdr,
2388 struct ceph_osd *osd = con->private;
2389 struct ceph_osd_client *osdc = osd->o_osdc;
2391 struct ceph_osd_request *req;
2392 int front = le32_to_cpu(hdr->front_len);
2393 int data_len = le32_to_cpu(hdr->data_len);
2396 tid = le64_to_cpu(hdr->tid);
2397 mutex_lock(&osdc->request_mutex);
2398 req = __lookup_request(osdc, tid);
2402 dout("get_reply unknown tid %llu from osd%d\n", tid,
2407 if (req->r_reply->con)
2408 dout("%s revoking msg %p from old con %p\n", __func__,
2409 req->r_reply, req->r_reply->con);
2410 ceph_msg_revoke_incoming(req->r_reply);
2412 if (front > req->r_reply->front.iov_len) {
2413 pr_warning("get_reply front %d > preallocated %d\n",
2414 front, (int)req->r_reply->front.iov_len);
2415 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2418 ceph_msg_put(req->r_reply);
2421 m = ceph_msg_get(req->r_reply);
2424 struct ceph_osd_data *osd_data;
2427 * XXX This is assuming there is only one op containing
2428 * XXX page data. Probably OK for reads, but this
2429 * XXX ought to be done more generally.
2431 osd_data = osd_req_op_extent_osd_data(req, 0, false);
2432 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2433 if (osd_data->pages &&
2434 unlikely(osd_data->length < data_len)) {
2436 pr_warning("tid %lld reply has %d bytes "
2437 "we had only %llu bytes ready\n",
2438 tid, data_len, osd_data->length);
2447 dout("get_reply tid %lld %p\n", tid, m);
2450 mutex_unlock(&osdc->request_mutex);
2455 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2456 struct ceph_msg_header *hdr,
2459 struct ceph_osd *osd = con->private;
2460 int type = le16_to_cpu(hdr->type);
2461 int front = le32_to_cpu(hdr->front_len);
2465 case CEPH_MSG_OSD_MAP:
2466 case CEPH_MSG_WATCH_NOTIFY:
2467 return ceph_msg_new(type, front, GFP_NOFS, false);
2468 case CEPH_MSG_OSD_OPREPLY:
2469 return get_reply(con, hdr, skip);
2471 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2479 * Wrappers to refcount containing ceph_osd struct
2481 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2483 struct ceph_osd *osd = con->private;
2489 static void put_osd_con(struct ceph_connection *con)
2491 struct ceph_osd *osd = con->private;
2499 * Note: returned pointer is the address of a structure that's
2500 * managed separately. Caller must *not* attempt to free it.
2502 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2503 int *proto, int force_new)
2505 struct ceph_osd *o = con->private;
2506 struct ceph_osd_client *osdc = o->o_osdc;
2507 struct ceph_auth_client *ac = osdc->client->monc.auth;
2508 struct ceph_auth_handshake *auth = &o->o_auth;
2510 if (force_new && auth->authorizer) {
2511 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2512 auth->authorizer = NULL;
2514 if (!auth->authorizer) {
2515 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2518 return ERR_PTR(ret);
2520 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2523 return ERR_PTR(ret);
2525 *proto = ac->protocol;
2531 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2533 struct ceph_osd *o = con->private;
2534 struct ceph_osd_client *osdc = o->o_osdc;
2535 struct ceph_auth_client *ac = osdc->client->monc.auth;
2537 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2540 static int invalidate_authorizer(struct ceph_connection *con)
2542 struct ceph_osd *o = con->private;
2543 struct ceph_osd_client *osdc = o->o_osdc;
2544 struct ceph_auth_client *ac = osdc->client->monc.auth;
2546 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2547 return ceph_monc_validate_auth(&osdc->client->monc);
2550 static const struct ceph_connection_operations osd_con_ops = {
2553 .dispatch = dispatch,
2554 .get_authorizer = get_authorizer,
2555 .verify_authorizer_reply = verify_authorizer_reply,
2556 .invalidate_authorizer = invalidate_authorizer,
2557 .alloc_msg = alloc_msg,