2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
6 #include <linux/highmem.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
12 #include <linux/bio.h>
15 #include <linux/ceph/libceph.h>
16 #include <linux/ceph/osd_client.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/pagelist.h>
22 #define OSD_OP_FRONT_LEN 4096
23 #define OSD_OPREPLY_FRONT_LEN 512
25 static const struct ceph_connection_operations osd_con_ops;
27 static void __send_queued(struct ceph_osd_client *osdc);
28 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
29 static void __register_request(struct ceph_osd_client *osdc,
30 struct ceph_osd_request *req);
31 static void __unregister_linger_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
33 static void __send_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
37 * Implement client access to distributed object storage cluster.
39 * All data objects are stored within a cluster/cloud of OSDs, or
40 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
41 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
42 * remote daemons serving up and coordinating consistent and safe
45 * Cluster membership and the mapping of data objects onto storage devices
46 * are described by the osd map.
48 * We keep track of pending OSD requests (read, write), resubmit
49 * requests to different OSDs when the cluster topology/data layout
50 * change, or retry the affected requests when the communications
51 * channel with an OSD is reset.
55 * calculate the mapping of a file extent onto an object, and fill out the
56 * request accordingly. shorten extent as necessary if it crosses an
59 * fill osd op in request message.
61 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
62 u64 *objnum, u64 *objoff, u64 *objlen)
68 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
72 if (*objlen < orig_len) {
74 dout(" skipping last %llu, final file extent %llu~%llu\n",
75 orig_len - *plen, off, *plen);
78 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
83 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
85 memset(osd_data, 0, sizeof (*osd_data));
86 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
89 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
90 struct page **pages, u64 length, u32 alignment,
91 bool pages_from_pool, bool own_pages)
93 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
94 osd_data->pages = pages;
95 osd_data->length = length;
96 osd_data->alignment = alignment;
97 osd_data->pages_from_pool = pages_from_pool;
98 osd_data->own_pages = own_pages;
101 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
102 struct ceph_pagelist *pagelist)
104 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
105 osd_data->pagelist = pagelist;
109 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
110 struct bio *bio, size_t bio_length)
112 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
114 osd_data->bio_length = bio_length;
116 #endif /* CONFIG_BLOCK */
118 #define osd_req_op_data(oreq, whch, typ, fld) \
120 BUG_ON(whch >= (oreq)->r_num_ops); \
121 &(oreq)->r_ops[whch].typ.fld; \
124 static struct ceph_osd_data *
125 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
127 BUG_ON(which >= osd_req->r_num_ops);
129 return &osd_req->r_ops[which].raw_data_in;
132 struct ceph_osd_data *
133 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
136 return osd_req_op_data(osd_req, which, extent, osd_data);
138 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
140 struct ceph_osd_data *
141 osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
144 return osd_req_op_data(osd_req, which, cls, response_data);
146 EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
148 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
149 unsigned int which, struct page **pages,
150 u64 length, u32 alignment,
151 bool pages_from_pool, bool own_pages)
153 struct ceph_osd_data *osd_data;
155 osd_data = osd_req_op_raw_data_in(osd_req, which);
156 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
157 pages_from_pool, own_pages);
159 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
161 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
162 unsigned int which, struct page **pages,
163 u64 length, u32 alignment,
164 bool pages_from_pool, bool own_pages)
166 struct ceph_osd_data *osd_data;
168 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
169 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
170 pages_from_pool, own_pages);
172 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
174 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
175 unsigned int which, struct ceph_pagelist *pagelist)
177 struct ceph_osd_data *osd_data;
179 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
180 ceph_osd_data_pagelist_init(osd_data, pagelist);
182 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
185 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
186 unsigned int which, struct bio *bio, size_t bio_length)
188 struct ceph_osd_data *osd_data;
190 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
191 ceph_osd_data_bio_init(osd_data, bio, bio_length);
193 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
194 #endif /* CONFIG_BLOCK */
196 static void osd_req_op_cls_request_info_pagelist(
197 struct ceph_osd_request *osd_req,
198 unsigned int which, struct ceph_pagelist *pagelist)
200 struct ceph_osd_data *osd_data;
202 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
203 ceph_osd_data_pagelist_init(osd_data, pagelist);
206 void osd_req_op_cls_request_data_pagelist(
207 struct ceph_osd_request *osd_req,
208 unsigned int which, struct ceph_pagelist *pagelist)
210 struct ceph_osd_data *osd_data;
212 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
213 ceph_osd_data_pagelist_init(osd_data, pagelist);
215 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
217 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
218 unsigned int which, struct page **pages, u64 length,
219 u32 alignment, bool pages_from_pool, bool own_pages)
221 struct ceph_osd_data *osd_data;
223 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
224 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
225 pages_from_pool, own_pages);
227 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
229 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
231 switch (osd_data->type) {
232 case CEPH_OSD_DATA_TYPE_NONE:
234 case CEPH_OSD_DATA_TYPE_PAGES:
235 return osd_data->length;
236 case CEPH_OSD_DATA_TYPE_PAGELIST:
237 return (u64)osd_data->pagelist->length;
239 case CEPH_OSD_DATA_TYPE_BIO:
240 return (u64)osd_data->bio_length;
241 #endif /* CONFIG_BLOCK */
243 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_init(struct ceph_osd_request *osd_req,
477 unsigned int which, u16 opcode)
479 (void)_osd_req_op_init(osd_req, which, opcode);
481 EXPORT_SYMBOL(osd_req_op_init);
483 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
484 unsigned int which, u16 opcode,
485 u64 offset, u64 length,
486 u64 truncate_size, u32 truncate_seq)
488 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
489 size_t payload_len = 0;
491 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
493 op->extent.offset = offset;
494 op->extent.length = length;
495 op->extent.truncate_size = truncate_size;
496 op->extent.truncate_seq = truncate_seq;
497 if (opcode == CEPH_OSD_OP_WRITE)
498 payload_len += length;
500 op->payload_len = payload_len;
502 EXPORT_SYMBOL(osd_req_op_extent_init);
504 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
505 unsigned int which, u64 length)
507 struct ceph_osd_req_op *op;
510 BUG_ON(which >= osd_req->r_num_ops);
511 op = &osd_req->r_ops[which];
512 previous = op->extent.length;
514 if (length == previous)
515 return; /* Nothing to do */
516 BUG_ON(length > previous);
518 op->extent.length = length;
519 op->payload_len -= previous - length;
521 EXPORT_SYMBOL(osd_req_op_extent_update);
523 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
524 u16 opcode, const char *class, const char *method)
526 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
527 struct ceph_pagelist *pagelist;
528 size_t payload_len = 0;
531 BUG_ON(opcode != CEPH_OSD_OP_CALL);
533 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
535 ceph_pagelist_init(pagelist);
537 op->cls.class_name = class;
538 size = strlen(class);
539 BUG_ON(size > (size_t) U8_MAX);
540 op->cls.class_len = size;
541 ceph_pagelist_append(pagelist, class, size);
544 op->cls.method_name = method;
545 size = strlen(method);
546 BUG_ON(size > (size_t) U8_MAX);
547 op->cls.method_len = size;
548 ceph_pagelist_append(pagelist, method, size);
551 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
553 op->cls.argc = 0; /* currently unused */
555 op->payload_len = payload_len;
557 EXPORT_SYMBOL(osd_req_op_cls_init);
559 void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
560 unsigned int which, u16 opcode,
561 u64 cookie, u64 version, int flag)
563 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
565 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
567 op->watch.cookie = cookie;
568 /* op->watch.ver = version; */ /* XXX 3847 */
569 op->watch.ver = cpu_to_le64(version);
570 if (opcode == CEPH_OSD_OP_WATCH && flag)
571 op->watch.flag = (u8)1;
573 EXPORT_SYMBOL(osd_req_op_watch_init);
575 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
576 struct ceph_osd_data *osd_data)
578 u64 length = ceph_osd_data_length(osd_data);
580 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
581 BUG_ON(length > (u64) SIZE_MAX);
583 ceph_msg_data_add_pages(msg, osd_data->pages,
584 length, osd_data->alignment);
585 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
587 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
589 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
590 ceph_msg_data_add_bio(msg, osd_data->bio, length);
593 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
597 static u64 osd_req_encode_op(struct ceph_osd_request *req,
598 struct ceph_osd_op *dst, unsigned int which)
600 struct ceph_osd_req_op *src;
601 struct ceph_osd_data *osd_data;
602 u64 request_data_len = 0;
605 BUG_ON(which >= req->r_num_ops);
606 src = &req->r_ops[which];
607 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
608 pr_err("unrecognized osd opcode %d\n", src->op);
614 case CEPH_OSD_OP_STAT:
615 osd_data = &src->raw_data_in;
616 ceph_osdc_msg_data_add(req->r_reply, osd_data);
618 case CEPH_OSD_OP_READ:
619 case CEPH_OSD_OP_WRITE:
620 if (src->op == CEPH_OSD_OP_WRITE)
621 request_data_len = src->extent.length;
622 dst->extent.offset = cpu_to_le64(src->extent.offset);
623 dst->extent.length = cpu_to_le64(src->extent.length);
624 dst->extent.truncate_size =
625 cpu_to_le64(src->extent.truncate_size);
626 dst->extent.truncate_seq =
627 cpu_to_le32(src->extent.truncate_seq);
628 osd_data = &src->extent.osd_data;
629 if (src->op == CEPH_OSD_OP_WRITE)
630 ceph_osdc_msg_data_add(req->r_request, osd_data);
632 ceph_osdc_msg_data_add(req->r_reply, osd_data);
634 case CEPH_OSD_OP_CALL:
635 dst->cls.class_len = src->cls.class_len;
636 dst->cls.method_len = src->cls.method_len;
637 osd_data = &src->cls.request_info;
638 ceph_osdc_msg_data_add(req->r_request, osd_data);
639 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
640 request_data_len = osd_data->pagelist->length;
642 osd_data = &src->cls.request_data;
643 data_length = ceph_osd_data_length(osd_data);
645 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
646 dst->cls.indata_len = cpu_to_le32(data_length);
647 ceph_osdc_msg_data_add(req->r_request, osd_data);
648 src->payload_len += data_length;
649 request_data_len += data_length;
651 osd_data = &src->cls.response_data;
652 ceph_osdc_msg_data_add(req->r_reply, osd_data);
654 case CEPH_OSD_OP_STARTSYNC:
656 case CEPH_OSD_OP_NOTIFY_ACK:
657 case CEPH_OSD_OP_WATCH:
658 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
659 dst->watch.ver = cpu_to_le64(src->watch.ver);
660 dst->watch.flag = src->watch.flag;
663 pr_err("unsupported osd opcode %s\n",
664 ceph_osd_op_name(src->op));
669 dst->op = cpu_to_le16(src->op);
670 dst->payload_len = cpu_to_le32(src->payload_len);
672 return request_data_len;
676 * build new request AND message, calculate layout, and adjust file
679 * if the file was recently truncated, we include information about its
680 * old and new size so that the object can be updated appropriately. (we
681 * avoid synchronously deleting truncated objects because it's slow.)
683 * if @do_sync, include a 'startsync' command so that the osd will flush
686 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
687 struct ceph_file_layout *layout,
688 struct ceph_vino vino,
689 u64 off, u64 *plen, int num_ops,
690 int opcode, int flags,
691 struct ceph_snap_context *snapc,
696 struct ceph_osd_request *req;
704 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
706 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
709 return ERR_PTR(-ENOMEM);
711 req->r_flags = flags;
713 /* calculate max write size */
714 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
716 ceph_osdc_put_request(req);
720 object_size = le32_to_cpu(layout->fl_object_size);
721 object_base = off - objoff;
722 if (truncate_size <= object_base) {
725 truncate_size -= object_base;
726 if (truncate_size > object_size)
727 truncate_size = object_size;
730 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
731 truncate_size, truncate_seq);
734 * A second op in the ops array means the caller wants to
735 * also issue a include a 'startsync' command so that the
736 * osd will flush data quickly.
739 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
741 req->r_file_layout = *layout; /* keep a copy */
743 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
745 req->r_oid_len = strlen(req->r_oid);
749 EXPORT_SYMBOL(ceph_osdc_new_request);
752 * We keep osd requests in an rbtree, sorted by ->r_tid.
754 static void __insert_request(struct ceph_osd_client *osdc,
755 struct ceph_osd_request *new)
757 struct rb_node **p = &osdc->requests.rb_node;
758 struct rb_node *parent = NULL;
759 struct ceph_osd_request *req = NULL;
763 req = rb_entry(parent, struct ceph_osd_request, r_node);
764 if (new->r_tid < req->r_tid)
766 else if (new->r_tid > req->r_tid)
772 rb_link_node(&new->r_node, parent, p);
773 rb_insert_color(&new->r_node, &osdc->requests);
776 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
779 struct ceph_osd_request *req;
780 struct rb_node *n = osdc->requests.rb_node;
783 req = rb_entry(n, struct ceph_osd_request, r_node);
784 if (tid < req->r_tid)
786 else if (tid > req->r_tid)
794 static struct ceph_osd_request *
795 __lookup_request_ge(struct ceph_osd_client *osdc,
798 struct ceph_osd_request *req;
799 struct rb_node *n = osdc->requests.rb_node;
802 req = rb_entry(n, struct ceph_osd_request, r_node);
803 if (tid < req->r_tid) {
807 } else if (tid > req->r_tid) {
817 * Resubmit requests pending on the given osd.
819 static void __kick_osd_requests(struct ceph_osd_client *osdc,
820 struct ceph_osd *osd)
822 struct ceph_osd_request *req, *nreq;
826 dout("__kick_osd_requests osd%d\n", osd->o_osd);
827 err = __reset_osd(osdc, osd);
831 * Build up a list of requests to resend by traversing the
832 * osd's list of requests. Requests for a given object are
833 * sent in tid order, and that is also the order they're
834 * kept on this list. Therefore all requests that are in
835 * flight will be found first, followed by all requests that
836 * have not yet been sent. And to resend requests while
837 * preserving this order we will want to put any sent
838 * requests back on the front of the osd client's unsent
841 * So we build a separate ordered list of already-sent
842 * requests for the affected osd and splice it onto the
843 * front of the osd client's unsent list. Once we've seen a
844 * request that has not yet been sent we're done. Those
845 * requests are already sitting right where they belong.
847 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
850 list_move_tail(&req->r_req_lru_item, &resend);
851 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
854 req->r_flags |= CEPH_OSD_FLAG_RETRY;
856 list_splice(&resend, &osdc->req_unsent);
859 * Linger requests are re-registered before sending, which
860 * sets up a new tid for each. We add them to the unsent
861 * list at the end to keep things in tid order.
863 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
866 * reregister request prior to unregistering linger so
867 * that r_osd is preserved.
869 BUG_ON(!list_empty(&req->r_req_lru_item));
870 __register_request(osdc, req);
871 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
872 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
873 __unregister_linger_request(osdc, req);
874 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
880 * If the osd connection drops, we need to resubmit all requests.
882 static void osd_reset(struct ceph_connection *con)
884 struct ceph_osd *osd = con->private;
885 struct ceph_osd_client *osdc;
889 dout("osd_reset osd%d\n", osd->o_osd);
891 down_read(&osdc->map_sem);
892 mutex_lock(&osdc->request_mutex);
893 __kick_osd_requests(osdc, osd);
895 mutex_unlock(&osdc->request_mutex);
896 up_read(&osdc->map_sem);
900 * Track open sessions with osds.
902 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
904 struct ceph_osd *osd;
906 osd = kzalloc(sizeof(*osd), GFP_NOFS);
910 atomic_set(&osd->o_ref, 1);
913 RB_CLEAR_NODE(&osd->o_node);
914 INIT_LIST_HEAD(&osd->o_requests);
915 INIT_LIST_HEAD(&osd->o_linger_requests);
916 INIT_LIST_HEAD(&osd->o_osd_lru);
917 osd->o_incarnation = 1;
919 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
921 INIT_LIST_HEAD(&osd->o_keepalive_item);
925 static struct ceph_osd *get_osd(struct ceph_osd *osd)
927 if (atomic_inc_not_zero(&osd->o_ref)) {
928 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
929 atomic_read(&osd->o_ref));
932 dout("get_osd %p FAIL\n", osd);
937 static void put_osd(struct ceph_osd *osd)
939 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
940 atomic_read(&osd->o_ref) - 1);
941 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
942 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
944 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
950 * remove an osd from our map
952 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
954 dout("__remove_osd %p\n", osd);
955 BUG_ON(!list_empty(&osd->o_requests));
956 rb_erase(&osd->o_node, &osdc->osds);
957 list_del_init(&osd->o_osd_lru);
958 ceph_con_close(&osd->o_con);
962 static void remove_all_osds(struct ceph_osd_client *osdc)
964 dout("%s %p\n", __func__, osdc);
965 mutex_lock(&osdc->request_mutex);
966 while (!RB_EMPTY_ROOT(&osdc->osds)) {
967 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
968 struct ceph_osd, o_node);
969 __remove_osd(osdc, osd);
971 mutex_unlock(&osdc->request_mutex);
974 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
975 struct ceph_osd *osd)
977 dout("__move_osd_to_lru %p\n", osd);
978 BUG_ON(!list_empty(&osd->o_osd_lru));
979 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
980 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
983 static void __remove_osd_from_lru(struct ceph_osd *osd)
985 dout("__remove_osd_from_lru %p\n", osd);
986 if (!list_empty(&osd->o_osd_lru))
987 list_del_init(&osd->o_osd_lru);
990 static void remove_old_osds(struct ceph_osd_client *osdc)
992 struct ceph_osd *osd, *nosd;
994 dout("__remove_old_osds %p\n", osdc);
995 mutex_lock(&osdc->request_mutex);
996 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
997 if (time_before(jiffies, osd->lru_ttl))
999 __remove_osd(osdc, osd);
1001 mutex_unlock(&osdc->request_mutex);
1007 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1009 struct ceph_entity_addr *peer_addr;
1011 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1012 if (list_empty(&osd->o_requests) &&
1013 list_empty(&osd->o_linger_requests)) {
1014 __remove_osd(osdc, osd);
1019 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1020 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1021 !ceph_con_opened(&osd->o_con)) {
1022 struct ceph_osd_request *req;
1024 dout(" osd addr hasn't changed and connection never opened,"
1025 " letting msgr retry");
1026 /* touch each r_stamp for handle_timeout()'s benfit */
1027 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1028 req->r_stamp = jiffies;
1033 ceph_con_close(&osd->o_con);
1034 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1035 osd->o_incarnation++;
1040 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1042 struct rb_node **p = &osdc->osds.rb_node;
1043 struct rb_node *parent = NULL;
1044 struct ceph_osd *osd = NULL;
1046 dout("__insert_osd %p osd%d\n", new, new->o_osd);
1049 osd = rb_entry(parent, struct ceph_osd, o_node);
1050 if (new->o_osd < osd->o_osd)
1052 else if (new->o_osd > osd->o_osd)
1053 p = &(*p)->rb_right;
1058 rb_link_node(&new->o_node, parent, p);
1059 rb_insert_color(&new->o_node, &osdc->osds);
1062 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1064 struct ceph_osd *osd;
1065 struct rb_node *n = osdc->osds.rb_node;
1068 osd = rb_entry(n, struct ceph_osd, o_node);
1071 else if (o > osd->o_osd)
1079 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1081 schedule_delayed_work(&osdc->timeout_work,
1082 osdc->client->options->osd_keepalive_timeout * HZ);
1085 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1087 cancel_delayed_work(&osdc->timeout_work);
1091 * Register request, assign tid. If this is the first request, set up
1092 * the timeout event.
1094 static void __register_request(struct ceph_osd_client *osdc,
1095 struct ceph_osd_request *req)
1097 req->r_tid = ++osdc->last_tid;
1098 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1099 dout("__register_request %p tid %lld\n", req, req->r_tid);
1100 __insert_request(osdc, req);
1101 ceph_osdc_get_request(req);
1102 osdc->num_requests++;
1103 if (osdc->num_requests == 1) {
1104 dout(" first request, scheduling timeout\n");
1105 __schedule_osd_timeout(osdc);
1110 * called under osdc->request_mutex
1112 static void __unregister_request(struct ceph_osd_client *osdc,
1113 struct ceph_osd_request *req)
1115 if (RB_EMPTY_NODE(&req->r_node)) {
1116 dout("__unregister_request %p tid %lld not registered\n",
1121 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1122 rb_erase(&req->r_node, &osdc->requests);
1123 osdc->num_requests--;
1126 /* make sure the original request isn't in flight. */
1127 ceph_msg_revoke(req->r_request);
1129 list_del_init(&req->r_osd_item);
1130 if (list_empty(&req->r_osd->o_requests) &&
1131 list_empty(&req->r_osd->o_linger_requests)) {
1132 dout("moving osd to %p lru\n", req->r_osd);
1133 __move_osd_to_lru(osdc, req->r_osd);
1135 if (list_empty(&req->r_linger_item))
1139 list_del_init(&req->r_req_lru_item);
1140 ceph_osdc_put_request(req);
1142 if (osdc->num_requests == 0) {
1143 dout(" no requests, canceling timeout\n");
1144 __cancel_osd_timeout(osdc);
1149 * Cancel a previously queued request message
1151 static void __cancel_request(struct ceph_osd_request *req)
1153 if (req->r_sent && req->r_osd) {
1154 ceph_msg_revoke(req->r_request);
1159 static void __register_linger_request(struct ceph_osd_client *osdc,
1160 struct ceph_osd_request *req)
1162 dout("__register_linger_request %p\n", req);
1163 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1165 list_add_tail(&req->r_linger_osd,
1166 &req->r_osd->o_linger_requests);
1169 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1170 struct ceph_osd_request *req)
1172 dout("__unregister_linger_request %p\n", req);
1173 list_del_init(&req->r_linger_item);
1175 list_del_init(&req->r_linger_osd);
1177 if (list_empty(&req->r_osd->o_requests) &&
1178 list_empty(&req->r_osd->o_linger_requests)) {
1179 dout("moving osd to %p lru\n", req->r_osd);
1180 __move_osd_to_lru(osdc, req->r_osd);
1182 if (list_empty(&req->r_osd_item))
1187 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1188 struct ceph_osd_request *req)
1190 mutex_lock(&osdc->request_mutex);
1191 if (req->r_linger) {
1192 __unregister_linger_request(osdc, req);
1193 ceph_osdc_put_request(req);
1195 mutex_unlock(&osdc->request_mutex);
1197 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1199 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1200 struct ceph_osd_request *req)
1202 if (!req->r_linger) {
1203 dout("set_request_linger %p\n", req);
1206 * caller is now responsible for calling
1207 * unregister_linger_request
1209 ceph_osdc_get_request(req);
1212 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1215 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1216 * (as needed), and set the request r_osd appropriately. If there is
1217 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1218 * (unsent, homeless) or leave on in-flight lru.
1220 * Return 0 if unchanged, 1 if changed, or negative on error.
1222 * Caller should hold map_sem for read and request_mutex.
1224 static int __map_request(struct ceph_osd_client *osdc,
1225 struct ceph_osd_request *req, int force_resend)
1227 struct ceph_pg pgid;
1228 int acting[CEPH_PG_MAX_SIZE];
1229 int o = -1, num = 0;
1232 dout("map_request %p tid %lld\n", req, req->r_tid);
1233 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1234 ceph_file_layout_pg_pool(req->r_file_layout));
1236 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1241 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1247 if ((!force_resend &&
1248 req->r_osd && req->r_osd->o_osd == o &&
1249 req->r_sent >= req->r_osd->o_incarnation &&
1250 req->r_num_pg_osds == num &&
1251 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1252 (req->r_osd == NULL && o == -1))
1253 return 0; /* no change */
1255 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1256 req->r_tid, pgid.pool, pgid.seed, o,
1257 req->r_osd ? req->r_osd->o_osd : -1);
1259 /* record full pg acting set */
1260 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1261 req->r_num_pg_osds = num;
1264 __cancel_request(req);
1265 list_del_init(&req->r_osd_item);
1269 req->r_osd = __lookup_osd(osdc, o);
1270 if (!req->r_osd && o >= 0) {
1272 req->r_osd = create_osd(osdc, o);
1274 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1278 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1279 __insert_osd(osdc, req->r_osd);
1281 ceph_con_open(&req->r_osd->o_con,
1282 CEPH_ENTITY_TYPE_OSD, o,
1283 &osdc->osdmap->osd_addr[o]);
1287 __remove_osd_from_lru(req->r_osd);
1288 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1289 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1291 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1293 err = 1; /* osd or pg changed */
1300 * caller should hold map_sem (for read) and request_mutex
1302 static void __send_request(struct ceph_osd_client *osdc,
1303 struct ceph_osd_request *req)
1307 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1308 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1309 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1311 /* fill in message content that changes each time we send it */
1312 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1313 put_unaligned_le32(req->r_flags, req->r_request_flags);
1314 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1315 p = req->r_request_pgid;
1316 ceph_encode_64(&p, req->r_pgid.pool);
1317 ceph_encode_32(&p, req->r_pgid.seed);
1318 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1319 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1320 sizeof(req->r_reassert_version));
1322 req->r_stamp = jiffies;
1323 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1325 ceph_msg_get(req->r_request); /* send consumes a ref */
1327 /* Mark the request unsafe if this is the first timet's being sent. */
1329 if (!req->r_sent && req->r_unsafe_callback)
1330 req->r_unsafe_callback(req, true);
1331 req->r_sent = req->r_osd->o_incarnation;
1333 ceph_con_send(&req->r_osd->o_con, req->r_request);
1337 * Send any requests in the queue (req_unsent).
1339 static void __send_queued(struct ceph_osd_client *osdc)
1341 struct ceph_osd_request *req, *tmp;
1343 dout("__send_queued\n");
1344 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1345 __send_request(osdc, req);
1349 * Timeout callback, called every N seconds when 1 or more osd
1350 * requests has been active for more than N seconds. When this
1351 * happens, we ping all OSDs with requests who have timed out to
1352 * ensure any communications channel reset is detected. Reset the
1353 * request timeouts another N seconds in the future as we go.
1354 * Reschedule the timeout event another N seconds in future (unless
1355 * there are no open requests).
1357 static void handle_timeout(struct work_struct *work)
1359 struct ceph_osd_client *osdc =
1360 container_of(work, struct ceph_osd_client, timeout_work.work);
1361 struct ceph_osd_request *req;
1362 struct ceph_osd *osd;
1363 unsigned long keepalive =
1364 osdc->client->options->osd_keepalive_timeout * HZ;
1365 struct list_head slow_osds;
1367 down_read(&osdc->map_sem);
1369 ceph_monc_request_next_osdmap(&osdc->client->monc);
1371 mutex_lock(&osdc->request_mutex);
1374 * ping osds that are a bit slow. this ensures that if there
1375 * is a break in the TCP connection we will notice, and reopen
1376 * a connection with that osd (from the fault callback).
1378 INIT_LIST_HEAD(&slow_osds);
1379 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1380 if (time_before(jiffies, req->r_stamp + keepalive))
1385 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1386 req->r_tid, osd->o_osd);
1387 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1389 while (!list_empty(&slow_osds)) {
1390 osd = list_entry(slow_osds.next, struct ceph_osd,
1392 list_del_init(&osd->o_keepalive_item);
1393 ceph_con_keepalive(&osd->o_con);
1396 __schedule_osd_timeout(osdc);
1397 __send_queued(osdc);
1398 mutex_unlock(&osdc->request_mutex);
1399 up_read(&osdc->map_sem);
1402 static void handle_osds_timeout(struct work_struct *work)
1404 struct ceph_osd_client *osdc =
1405 container_of(work, struct ceph_osd_client,
1406 osds_timeout_work.work);
1407 unsigned long delay =
1408 osdc->client->options->osd_idle_ttl * HZ >> 2;
1410 dout("osds timeout\n");
1411 down_read(&osdc->map_sem);
1412 remove_old_osds(osdc);
1413 up_read(&osdc->map_sem);
1415 schedule_delayed_work(&osdc->osds_timeout_work,
1416 round_jiffies_relative(delay));
1419 static void complete_request(struct ceph_osd_request *req)
1421 if (req->r_unsafe_callback)
1422 req->r_unsafe_callback(req, false);
1423 complete_all(&req->r_safe_completion); /* fsync waiter */
1427 * handle osd op reply. either call the callback if it is specified,
1428 * or do the completion to wake up the waiting thread.
1430 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1431 struct ceph_connection *con)
1434 struct ceph_osd_request *req;
1437 unsigned int numops;
1438 int payload_len, flags;
1444 u64 reassert_version;
1446 int already_completed;
1450 tid = le64_to_cpu(msg->hdr.tid);
1451 dout("handle_reply %p tid %llu\n", msg, tid);
1453 p = msg->front.iov_base;
1454 end = p + msg->front.iov_len;
1456 ceph_decode_need(&p, end, 4, bad);
1457 object_len = ceph_decode_32(&p);
1458 ceph_decode_need(&p, end, object_len, bad);
1461 err = ceph_decode_pgid(&p, end, &pg);
1465 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1466 flags = ceph_decode_64(&p);
1467 result = ceph_decode_32(&p);
1468 reassert_epoch = ceph_decode_32(&p);
1469 reassert_version = ceph_decode_64(&p);
1470 osdmap_epoch = ceph_decode_32(&p);
1473 mutex_lock(&osdc->request_mutex);
1474 req = __lookup_request(osdc, tid);
1476 dout("handle_reply tid %llu dne\n", tid);
1479 ceph_osdc_get_request(req);
1481 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1484 ceph_decode_need(&p, end, 4, bad);
1485 numops = ceph_decode_32(&p);
1486 if (numops > CEPH_OSD_MAX_OP)
1488 if (numops != req->r_num_ops)
1491 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1492 for (i = 0; i < numops; i++) {
1493 struct ceph_osd_op *op = p;
1496 len = le32_to_cpu(op->payload_len);
1497 req->r_reply_op_len[i] = len;
1498 dout(" op %d has %d bytes\n", i, len);
1502 bytes = le32_to_cpu(msg->hdr.data_len);
1503 if (payload_len != bytes) {
1504 pr_warning("sum of op payload lens %d != data_len %d",
1505 payload_len, bytes);
1509 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1510 retry_attempt = ceph_decode_32(&p);
1511 for (i = 0; i < numops; i++)
1512 req->r_reply_op_result[i] = ceph_decode_32(&p);
1514 if (!req->r_got_reply) {
1516 req->r_result = result;
1517 dout("handle_reply result %d bytes %d\n", req->r_result,
1519 if (req->r_result == 0)
1520 req->r_result = bytes;
1522 /* in case this is a write and we need to replay, */
1523 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1524 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1526 req->r_got_reply = 1;
1527 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1528 dout("handle_reply tid %llu dup ack\n", tid);
1529 mutex_unlock(&osdc->request_mutex);
1533 dout("handle_reply tid %llu flags %d\n", tid, flags);
1535 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1536 __register_linger_request(osdc, req);
1538 /* either this is a read, or we got the safe response */
1540 (flags & CEPH_OSD_FLAG_ONDISK) ||
1541 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1542 __unregister_request(osdc, req);
1544 already_completed = req->r_completed;
1545 req->r_completed = 1;
1546 mutex_unlock(&osdc->request_mutex);
1547 if (already_completed)
1550 if (req->r_callback)
1551 req->r_callback(req, msg);
1553 complete_all(&req->r_completion);
1555 if (flags & CEPH_OSD_FLAG_ONDISK)
1556 complete_request(req);
1559 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1560 ceph_osdc_put_request(req);
1564 ceph_osdc_put_request(req);
1566 mutex_unlock(&osdc->request_mutex);
1568 pr_err("corrupt osd_op_reply got %d %d\n",
1569 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1573 static void reset_changed_osds(struct ceph_osd_client *osdc)
1575 struct rb_node *p, *n;
1577 for (p = rb_first(&osdc->osds); p; p = n) {
1578 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1581 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1582 memcmp(&osd->o_con.peer_addr,
1583 ceph_osd_addr(osdc->osdmap,
1585 sizeof(struct ceph_entity_addr)) != 0)
1586 __reset_osd(osdc, osd);
1591 * Requeue requests whose mapping to an OSD has changed. If requests map to
1592 * no osd, request a new map.
1594 * Caller should hold map_sem for read.
1596 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1598 struct ceph_osd_request *req, *nreq;
1603 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1604 mutex_lock(&osdc->request_mutex);
1605 for (p = rb_first(&osdc->requests); p; ) {
1606 req = rb_entry(p, struct ceph_osd_request, r_node);
1610 * For linger requests that have not yet been
1611 * registered, move them to the linger list; they'll
1612 * be sent to the osd in the loop below. Unregister
1613 * the request before re-registering it as a linger
1614 * request to ensure the __map_request() below
1615 * will decide it needs to be sent.
1617 if (req->r_linger && list_empty(&req->r_linger_item)) {
1618 dout("%p tid %llu restart on osd%d\n",
1620 req->r_osd ? req->r_osd->o_osd : -1);
1621 __unregister_request(osdc, req);
1622 __register_linger_request(osdc, req);
1626 err = __map_request(osdc, req, force_resend);
1628 continue; /* error */
1629 if (req->r_osd == NULL) {
1630 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1631 needmap++; /* request a newer map */
1632 } else if (err > 0) {
1633 if (!req->r_linger) {
1634 dout("%p tid %llu requeued on osd%d\n", req,
1636 req->r_osd ? req->r_osd->o_osd : -1);
1637 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1642 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1644 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1646 err = __map_request(osdc, req, force_resend);
1647 dout("__map_request returned %d\n", err);
1649 continue; /* no change and no osd was specified */
1651 continue; /* hrm! */
1652 if (req->r_osd == NULL) {
1653 dout("tid %llu maps to no valid osd\n", req->r_tid);
1654 needmap++; /* request a newer map */
1658 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1659 req->r_osd ? req->r_osd->o_osd : -1);
1660 __register_request(osdc, req);
1661 __unregister_linger_request(osdc, req);
1663 mutex_unlock(&osdc->request_mutex);
1666 dout("%d requests for down osds, need new map\n", needmap);
1667 ceph_monc_request_next_osdmap(&osdc->client->monc);
1669 reset_changed_osds(osdc);
1674 * Process updated osd map.
1676 * The message contains any number of incremental and full maps, normally
1677 * indicating some sort of topology change in the cluster. Kick requests
1678 * off to different OSDs as needed.
1680 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1682 void *p, *end, *next;
1683 u32 nr_maps, maplen;
1685 struct ceph_osdmap *newmap = NULL, *oldmap;
1687 struct ceph_fsid fsid;
1689 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1690 p = msg->front.iov_base;
1691 end = p + msg->front.iov_len;
1694 ceph_decode_need(&p, end, sizeof(fsid), bad);
1695 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1696 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1699 down_write(&osdc->map_sem);
1701 /* incremental maps */
1702 ceph_decode_32_safe(&p, end, nr_maps, bad);
1703 dout(" %d inc maps\n", nr_maps);
1704 while (nr_maps > 0) {
1705 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1706 epoch = ceph_decode_32(&p);
1707 maplen = ceph_decode_32(&p);
1708 ceph_decode_need(&p, end, maplen, bad);
1710 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1711 dout("applying incremental map %u len %d\n",
1713 newmap = osdmap_apply_incremental(&p, next,
1715 &osdc->client->msgr);
1716 if (IS_ERR(newmap)) {
1717 err = PTR_ERR(newmap);
1721 if (newmap != osdc->osdmap) {
1722 ceph_osdmap_destroy(osdc->osdmap);
1723 osdc->osdmap = newmap;
1725 kick_requests(osdc, 0);
1727 dout("ignoring incremental map %u len %d\n",
1737 ceph_decode_32_safe(&p, end, nr_maps, bad);
1738 dout(" %d full maps\n", nr_maps);
1740 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1741 epoch = ceph_decode_32(&p);
1742 maplen = ceph_decode_32(&p);
1743 ceph_decode_need(&p, end, maplen, bad);
1745 dout("skipping non-latest full map %u len %d\n",
1747 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1748 dout("skipping full map %u len %d, "
1749 "older than our %u\n", epoch, maplen,
1750 osdc->osdmap->epoch);
1752 int skipped_map = 0;
1754 dout("taking full map %u len %d\n", epoch, maplen);
1755 newmap = osdmap_decode(&p, p+maplen);
1756 if (IS_ERR(newmap)) {
1757 err = PTR_ERR(newmap);
1761 oldmap = osdc->osdmap;
1762 osdc->osdmap = newmap;
1764 if (oldmap->epoch + 1 < newmap->epoch)
1766 ceph_osdmap_destroy(oldmap);
1768 kick_requests(osdc, skipped_map);
1775 downgrade_write(&osdc->map_sem);
1776 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1779 * subscribe to subsequent osdmap updates if full to ensure
1780 * we find out when we are no longer full and stop returning
1783 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1784 ceph_monc_request_next_osdmap(&osdc->client->monc);
1786 mutex_lock(&osdc->request_mutex);
1787 __send_queued(osdc);
1788 mutex_unlock(&osdc->request_mutex);
1789 up_read(&osdc->map_sem);
1790 wake_up_all(&osdc->client->auth_wq);
1794 pr_err("osdc handle_map corrupt msg\n");
1796 up_write(&osdc->map_sem);
1801 * watch/notify callback event infrastructure
1803 * These callbacks are used both for watch and notify operations.
1805 static void __release_event(struct kref *kref)
1807 struct ceph_osd_event *event =
1808 container_of(kref, struct ceph_osd_event, kref);
1810 dout("__release_event %p\n", event);
1814 static void get_event(struct ceph_osd_event *event)
1816 kref_get(&event->kref);
1819 void ceph_osdc_put_event(struct ceph_osd_event *event)
1821 kref_put(&event->kref, __release_event);
1823 EXPORT_SYMBOL(ceph_osdc_put_event);
1825 static void __insert_event(struct ceph_osd_client *osdc,
1826 struct ceph_osd_event *new)
1828 struct rb_node **p = &osdc->event_tree.rb_node;
1829 struct rb_node *parent = NULL;
1830 struct ceph_osd_event *event = NULL;
1834 event = rb_entry(parent, struct ceph_osd_event, node);
1835 if (new->cookie < event->cookie)
1837 else if (new->cookie > event->cookie)
1838 p = &(*p)->rb_right;
1843 rb_link_node(&new->node, parent, p);
1844 rb_insert_color(&new->node, &osdc->event_tree);
1847 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1850 struct rb_node **p = &osdc->event_tree.rb_node;
1851 struct rb_node *parent = NULL;
1852 struct ceph_osd_event *event = NULL;
1856 event = rb_entry(parent, struct ceph_osd_event, node);
1857 if (cookie < event->cookie)
1859 else if (cookie > event->cookie)
1860 p = &(*p)->rb_right;
1867 static void __remove_event(struct ceph_osd_event *event)
1869 struct ceph_osd_client *osdc = event->osdc;
1871 if (!RB_EMPTY_NODE(&event->node)) {
1872 dout("__remove_event removed %p\n", event);
1873 rb_erase(&event->node, &osdc->event_tree);
1874 ceph_osdc_put_event(event);
1876 dout("__remove_event didn't remove %p\n", event);
1880 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1881 void (*event_cb)(u64, u64, u8, void *),
1882 void *data, struct ceph_osd_event **pevent)
1884 struct ceph_osd_event *event;
1886 event = kmalloc(sizeof(*event), GFP_NOIO);
1890 dout("create_event %p\n", event);
1891 event->cb = event_cb;
1892 event->one_shot = 0;
1895 INIT_LIST_HEAD(&event->osd_node);
1896 RB_CLEAR_NODE(&event->node);
1897 kref_init(&event->kref); /* one ref for us */
1898 kref_get(&event->kref); /* one ref for the caller */
1900 spin_lock(&osdc->event_lock);
1901 event->cookie = ++osdc->event_count;
1902 __insert_event(osdc, event);
1903 spin_unlock(&osdc->event_lock);
1908 EXPORT_SYMBOL(ceph_osdc_create_event);
1910 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1912 struct ceph_osd_client *osdc = event->osdc;
1914 dout("cancel_event %p\n", event);
1915 spin_lock(&osdc->event_lock);
1916 __remove_event(event);
1917 spin_unlock(&osdc->event_lock);
1918 ceph_osdc_put_event(event); /* caller's */
1920 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1923 static void do_event_work(struct work_struct *work)
1925 struct ceph_osd_event_work *event_work =
1926 container_of(work, struct ceph_osd_event_work, work);
1927 struct ceph_osd_event *event = event_work->event;
1928 u64 ver = event_work->ver;
1929 u64 notify_id = event_work->notify_id;
1930 u8 opcode = event_work->opcode;
1932 dout("do_event_work completing %p\n", event);
1933 event->cb(ver, notify_id, opcode, event->data);
1934 dout("do_event_work completed %p\n", event);
1935 ceph_osdc_put_event(event);
1941 * Process osd watch notifications
1943 static void handle_watch_notify(struct ceph_osd_client *osdc,
1944 struct ceph_msg *msg)
1948 u64 cookie, ver, notify_id;
1950 struct ceph_osd_event *event;
1951 struct ceph_osd_event_work *event_work;
1953 p = msg->front.iov_base;
1954 end = p + msg->front.iov_len;
1956 ceph_decode_8_safe(&p, end, proto_ver, bad);
1957 ceph_decode_8_safe(&p, end, opcode, bad);
1958 ceph_decode_64_safe(&p, end, cookie, bad);
1959 ceph_decode_64_safe(&p, end, ver, bad);
1960 ceph_decode_64_safe(&p, end, notify_id, bad);
1962 spin_lock(&osdc->event_lock);
1963 event = __find_event(osdc, cookie);
1965 BUG_ON(event->one_shot);
1968 spin_unlock(&osdc->event_lock);
1969 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1970 cookie, ver, event);
1972 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1974 dout("ERROR: could not allocate event_work\n");
1977 INIT_WORK(&event_work->work, do_event_work);
1978 event_work->event = event;
1979 event_work->ver = ver;
1980 event_work->notify_id = notify_id;
1981 event_work->opcode = opcode;
1982 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1983 dout("WARNING: failed to queue notify event work\n");
1991 ceph_osdc_put_event(event);
1995 pr_err("osdc handle_watch_notify corrupt msg\n");
2000 * build new request AND message
2003 void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2004 struct ceph_snap_context *snapc, u64 snap_id,
2005 struct timespec *mtime)
2007 struct ceph_msg *msg = req->r_request;
2010 int flags = req->r_flags;
2014 req->r_snapid = snap_id;
2015 req->r_snapc = ceph_get_snap_context(snapc);
2017 /* encode request */
2018 msg->hdr.version = cpu_to_le16(4);
2020 p = msg->front.iov_base;
2021 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2022 req->r_request_osdmap_epoch = p;
2024 req->r_request_flags = p;
2026 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2027 ceph_encode_timespec(p, mtime);
2028 p += sizeof(struct ceph_timespec);
2029 req->r_request_reassert_version = p;
2030 p += sizeof(struct ceph_eversion); /* will get filled in */
2033 ceph_encode_8(&p, 4);
2034 ceph_encode_8(&p, 4);
2035 ceph_encode_32(&p, 8 + 4 + 4);
2036 req->r_request_pool = p;
2038 ceph_encode_32(&p, -1); /* preferred */
2039 ceph_encode_32(&p, 0); /* key len */
2041 ceph_encode_8(&p, 1);
2042 req->r_request_pgid = p;
2044 ceph_encode_32(&p, -1); /* preferred */
2047 ceph_encode_32(&p, req->r_oid_len);
2048 memcpy(p, req->r_oid, req->r_oid_len);
2049 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2050 p += req->r_oid_len;
2052 /* ops--can imply data */
2053 ceph_encode_16(&p, (u16)req->r_num_ops);
2055 for (i = 0; i < req->r_num_ops; i++) {
2056 data_len += osd_req_encode_op(req, p, i);
2057 p += sizeof(struct ceph_osd_op);
2061 ceph_encode_64(&p, req->r_snapid);
2062 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2063 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2065 for (i = 0; i < snapc->num_snaps; i++) {
2066 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2070 req->r_request_attempts = p;
2074 if (flags & CEPH_OSD_FLAG_WRITE) {
2078 * The header "data_off" is a hint to the receiver
2079 * allowing it to align received data into its
2080 * buffers such that there's no need to re-copy
2081 * it before writing it to disk (direct I/O).
2083 data_off = (u16) (off & 0xffff);
2084 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2086 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2088 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2089 msg_size = p - msg->front.iov_base;
2090 msg->front.iov_len = msg_size;
2091 msg->hdr.front_len = cpu_to_le32(msg_size);
2093 dout("build_request msg_size was %d\n", (int)msg_size);
2095 EXPORT_SYMBOL(ceph_osdc_build_request);
2098 * Register request, send initial attempt.
2100 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2101 struct ceph_osd_request *req,
2106 down_read(&osdc->map_sem);
2107 mutex_lock(&osdc->request_mutex);
2108 __register_request(osdc, req);
2109 WARN_ON(req->r_sent);
2110 rc = __map_request(osdc, req, 0);
2113 dout("osdc_start_request failed map, "
2114 " will retry %lld\n", req->r_tid);
2119 if (req->r_osd == NULL) {
2120 dout("send_request %p no up osds in pg\n", req);
2121 ceph_monc_request_next_osdmap(&osdc->client->monc);
2123 __send_queued(osdc);
2127 mutex_unlock(&osdc->request_mutex);
2128 up_read(&osdc->map_sem);
2131 EXPORT_SYMBOL(ceph_osdc_start_request);
2134 * wait for a request to complete
2136 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2137 struct ceph_osd_request *req)
2141 rc = wait_for_completion_interruptible(&req->r_completion);
2143 mutex_lock(&osdc->request_mutex);
2144 __cancel_request(req);
2145 __unregister_request(osdc, req);
2146 mutex_unlock(&osdc->request_mutex);
2147 complete_request(req);
2148 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
2152 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2153 return req->r_result;
2155 EXPORT_SYMBOL(ceph_osdc_wait_request);
2158 * sync - wait for all in-flight requests to flush. avoid starvation.
2160 void ceph_osdc_sync(struct ceph_osd_client *osdc)
2162 struct ceph_osd_request *req;
2163 u64 last_tid, next_tid = 0;
2165 mutex_lock(&osdc->request_mutex);
2166 last_tid = osdc->last_tid;
2168 req = __lookup_request_ge(osdc, next_tid);
2171 if (req->r_tid > last_tid)
2174 next_tid = req->r_tid + 1;
2175 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2178 ceph_osdc_get_request(req);
2179 mutex_unlock(&osdc->request_mutex);
2180 dout("sync waiting on tid %llu (last is %llu)\n",
2181 req->r_tid, last_tid);
2182 wait_for_completion(&req->r_safe_completion);
2183 mutex_lock(&osdc->request_mutex);
2184 ceph_osdc_put_request(req);
2186 mutex_unlock(&osdc->request_mutex);
2187 dout("sync done (thru tid %llu)\n", last_tid);
2189 EXPORT_SYMBOL(ceph_osdc_sync);
2194 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2199 osdc->client = client;
2200 osdc->osdmap = NULL;
2201 init_rwsem(&osdc->map_sem);
2202 init_completion(&osdc->map_waiters);
2203 osdc->last_requested_map = 0;
2204 mutex_init(&osdc->request_mutex);
2206 osdc->osds = RB_ROOT;
2207 INIT_LIST_HEAD(&osdc->osd_lru);
2208 osdc->requests = RB_ROOT;
2209 INIT_LIST_HEAD(&osdc->req_lru);
2210 INIT_LIST_HEAD(&osdc->req_unsent);
2211 INIT_LIST_HEAD(&osdc->req_notarget);
2212 INIT_LIST_HEAD(&osdc->req_linger);
2213 osdc->num_requests = 0;
2214 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2215 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2216 spin_lock_init(&osdc->event_lock);
2217 osdc->event_tree = RB_ROOT;
2218 osdc->event_count = 0;
2220 schedule_delayed_work(&osdc->osds_timeout_work,
2221 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2224 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2225 sizeof(struct ceph_osd_request));
2226 if (!osdc->req_mempool)
2229 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2230 OSD_OP_FRONT_LEN, 10, true,
2234 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2235 OSD_OPREPLY_FRONT_LEN, 10, true,
2240 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2241 if (IS_ERR(osdc->notify_wq)) {
2242 err = PTR_ERR(osdc->notify_wq);
2243 osdc->notify_wq = NULL;
2249 ceph_msgpool_destroy(&osdc->msgpool_op);
2251 mempool_destroy(osdc->req_mempool);
2256 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2258 flush_workqueue(osdc->notify_wq);
2259 destroy_workqueue(osdc->notify_wq);
2260 cancel_delayed_work_sync(&osdc->timeout_work);
2261 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2263 ceph_osdmap_destroy(osdc->osdmap);
2264 osdc->osdmap = NULL;
2266 remove_all_osds(osdc);
2267 mempool_destroy(osdc->req_mempool);
2268 ceph_msgpool_destroy(&osdc->msgpool_op);
2269 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2273 * Read some contiguous pages. If we cross a stripe boundary, shorten
2274 * *plen. Return number of bytes read, or error.
2276 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2277 struct ceph_vino vino, struct ceph_file_layout *layout,
2279 u32 truncate_seq, u64 truncate_size,
2280 struct page **pages, int num_pages, int page_align)
2282 struct ceph_osd_request *req;
2285 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2286 vino.snap, off, *plen);
2287 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
2288 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2289 NULL, truncate_seq, truncate_size,
2292 return PTR_ERR(req);
2294 /* it may be a short read due to an object boundary */
2296 osd_req_op_extent_osd_data_pages(req, 0,
2297 pages, *plen, page_align, false, false);
2299 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2300 off, *plen, *plen, page_align);
2302 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
2304 rc = ceph_osdc_start_request(osdc, req, false);
2306 rc = ceph_osdc_wait_request(osdc, req);
2308 ceph_osdc_put_request(req);
2309 dout("readpages result %d\n", rc);
2312 EXPORT_SYMBOL(ceph_osdc_readpages);
2315 * do a synchronous write on N pages
2317 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2318 struct ceph_file_layout *layout,
2319 struct ceph_snap_context *snapc,
2321 u32 truncate_seq, u64 truncate_size,
2322 struct timespec *mtime,
2323 struct page **pages, int num_pages)
2325 struct ceph_osd_request *req;
2327 int page_align = off & ~PAGE_MASK;
2329 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2330 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
2332 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2333 snapc, truncate_seq, truncate_size,
2336 return PTR_ERR(req);
2338 /* it may be a short write due to an object boundary */
2339 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
2341 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2343 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
2345 rc = ceph_osdc_start_request(osdc, req, true);
2347 rc = ceph_osdc_wait_request(osdc, req);
2349 ceph_osdc_put_request(req);
2352 dout("writepages result %d\n", rc);
2355 EXPORT_SYMBOL(ceph_osdc_writepages);
2358 * handle incoming message
2360 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2362 struct ceph_osd *osd = con->private;
2363 struct ceph_osd_client *osdc;
2364 int type = le16_to_cpu(msg->hdr.type);
2371 case CEPH_MSG_OSD_MAP:
2372 ceph_osdc_handle_map(osdc, msg);
2374 case CEPH_MSG_OSD_OPREPLY:
2375 handle_reply(osdc, msg, con);
2377 case CEPH_MSG_WATCH_NOTIFY:
2378 handle_watch_notify(osdc, msg);
2382 pr_err("received unknown message type %d %s\n", type,
2383 ceph_msg_type_name(type));
2390 * lookup and return message for incoming reply. set up reply message
2393 static struct ceph_msg *get_reply(struct ceph_connection *con,
2394 struct ceph_msg_header *hdr,
2397 struct ceph_osd *osd = con->private;
2398 struct ceph_osd_client *osdc = osd->o_osdc;
2400 struct ceph_osd_request *req;
2401 int front = le32_to_cpu(hdr->front_len);
2402 int data_len = le32_to_cpu(hdr->data_len);
2405 tid = le64_to_cpu(hdr->tid);
2406 mutex_lock(&osdc->request_mutex);
2407 req = __lookup_request(osdc, tid);
2411 dout("get_reply unknown tid %llu from osd%d\n", tid,
2416 if (req->r_reply->con)
2417 dout("%s revoking msg %p from old con %p\n", __func__,
2418 req->r_reply, req->r_reply->con);
2419 ceph_msg_revoke_incoming(req->r_reply);
2421 if (front > req->r_reply->front.iov_len) {
2422 pr_warning("get_reply front %d > preallocated %d\n",
2423 front, (int)req->r_reply->front.iov_len);
2424 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2427 ceph_msg_put(req->r_reply);
2430 m = ceph_msg_get(req->r_reply);
2433 struct ceph_osd_data *osd_data;
2436 * XXX This is assuming there is only one op containing
2437 * XXX page data. Probably OK for reads, but this
2438 * XXX ought to be done more generally.
2440 osd_data = osd_req_op_extent_osd_data(req, 0);
2441 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2442 if (osd_data->pages &&
2443 unlikely(osd_data->length < data_len)) {
2445 pr_warning("tid %lld reply has %d bytes "
2446 "we had only %llu bytes ready\n",
2447 tid, data_len, osd_data->length);
2456 dout("get_reply tid %lld %p\n", tid, m);
2459 mutex_unlock(&osdc->request_mutex);
2464 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2465 struct ceph_msg_header *hdr,
2468 struct ceph_osd *osd = con->private;
2469 int type = le16_to_cpu(hdr->type);
2470 int front = le32_to_cpu(hdr->front_len);
2474 case CEPH_MSG_OSD_MAP:
2475 case CEPH_MSG_WATCH_NOTIFY:
2476 return ceph_msg_new(type, front, GFP_NOFS, false);
2477 case CEPH_MSG_OSD_OPREPLY:
2478 return get_reply(con, hdr, skip);
2480 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2488 * Wrappers to refcount containing ceph_osd struct
2490 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2492 struct ceph_osd *osd = con->private;
2498 static void put_osd_con(struct ceph_connection *con)
2500 struct ceph_osd *osd = con->private;
2508 * Note: returned pointer is the address of a structure that's
2509 * managed separately. Caller must *not* attempt to free it.
2511 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2512 int *proto, int force_new)
2514 struct ceph_osd *o = con->private;
2515 struct ceph_osd_client *osdc = o->o_osdc;
2516 struct ceph_auth_client *ac = osdc->client->monc.auth;
2517 struct ceph_auth_handshake *auth = &o->o_auth;
2519 if (force_new && auth->authorizer) {
2520 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2521 auth->authorizer = NULL;
2523 if (!auth->authorizer) {
2524 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2527 return ERR_PTR(ret);
2529 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2532 return ERR_PTR(ret);
2534 *proto = ac->protocol;
2540 static int verify_authorizer_reply(struct ceph_connection *con, int len)
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 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2549 static int invalidate_authorizer(struct ceph_connection *con)
2551 struct ceph_osd *o = con->private;
2552 struct ceph_osd_client *osdc = o->o_osdc;
2553 struct ceph_auth_client *ac = osdc->client->monc.auth;
2555 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2556 return ceph_monc_validate_auth(&osdc->client->monc);
2559 static const struct ceph_connection_operations osd_con_ops = {
2562 .dispatch = dispatch,
2563 .get_authorizer = get_authorizer,
2564 .verify_authorizer_reply = verify_authorizer_reply,
2565 .invalidate_authorizer = invalidate_authorizer,
2566 .alloc_msg = alloc_msg,