1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops;
26 static void __send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
36 * Implement client access to distributed object storage cluster.
38 * All data objects are stored within a cluster/cloud of OSDs, or
39 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
40 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
41 * remote daemons serving up and coordinating consistent and safe
44 * Cluster membership and the mapping of data objects onto storage devices
45 * are described by the osd map.
47 * We keep track of pending OSD requests (read, write), resubmit
48 * requests to different OSDs when the cluster topology/data layout
49 * change, or retry the affected requests when the communications
50 * channel with an OSD is reset.
54 * calculate the mapping of a file extent onto an object, and fill out the
55 * request accordingly. shorten extent as necessary if it crosses an
58 * fill osd op in request message.
60 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
61 u64 *objnum, u64 *objoff, u64 *objlen)
67 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
71 if (*objlen < orig_len) {
73 dout(" skipping last %llu, final file extent %llu~%llu\n",
74 orig_len - *plen, off, *plen);
77 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
82 void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
83 struct page **pages, u64 length, u32 alignment,
84 bool pages_from_pool, bool own_pages)
86 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
87 osd_data->pages = pages;
88 osd_data->length = length;
89 osd_data->alignment = alignment;
90 osd_data->pages_from_pool = pages_from_pool;
91 osd_data->own_pages = own_pages;
93 EXPORT_SYMBOL(ceph_osd_data_pages_init);
95 void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
96 struct ceph_pagelist *pagelist)
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
99 osd_data->pagelist = pagelist;
101 EXPORT_SYMBOL(ceph_osd_data_pagelist_init);
104 void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
105 struct bio *bio, size_t bio_length)
107 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
109 osd_data->bio_length = bio_length;
111 EXPORT_SYMBOL(ceph_osd_data_bio_init);
112 #endif /* CONFIG_BLOCK */
117 void ceph_osdc_release_request(struct kref *kref)
120 struct ceph_osd_request *req = container_of(kref,
121 struct ceph_osd_request,
125 ceph_msg_put(req->r_request);
127 ceph_msg_revoke_incoming(req->r_reply);
128 ceph_msg_put(req->r_reply);
131 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
132 req->r_data_in.own_pages) {
133 num_pages = calc_pages_for((u64)req->r_data_in.alignment,
134 (u64)req->r_data_in.length);
135 ceph_release_page_vector(req->r_data_in.pages, num_pages);
137 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
138 req->r_data_out.own_pages) {
139 num_pages = calc_pages_for((u64)req->r_data_out.alignment,
140 (u64)req->r_data_out.length);
141 ceph_release_page_vector(req->r_data_out.pages, num_pages);
144 ceph_put_snap_context(req->r_snapc);
146 mempool_free(req, req->r_osdc->req_mempool);
150 EXPORT_SYMBOL(ceph_osdc_release_request);
152 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
153 struct ceph_snap_context *snapc,
154 unsigned int num_ops,
158 struct ceph_osd_request *req;
159 struct ceph_msg *msg;
162 msg_size = 4 + 4 + 8 + 8 + 4+8;
163 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
164 msg_size += 1 + 8 + 4 + 4; /* pg_t */
165 msg_size += 4 + MAX_OBJ_NAME_SIZE;
166 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
167 msg_size += 8; /* snapid */
168 msg_size += 8; /* snap_seq */
169 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
173 req = mempool_alloc(osdc->req_mempool, gfp_flags);
174 memset(req, 0, sizeof(*req));
176 req = kzalloc(sizeof(*req), gfp_flags);
182 req->r_mempool = use_mempool;
184 kref_init(&req->r_kref);
185 init_completion(&req->r_completion);
186 init_completion(&req->r_safe_completion);
187 RB_CLEAR_NODE(&req->r_node);
188 INIT_LIST_HEAD(&req->r_unsafe_item);
189 INIT_LIST_HEAD(&req->r_linger_item);
190 INIT_LIST_HEAD(&req->r_linger_osd);
191 INIT_LIST_HEAD(&req->r_req_lru_item);
192 INIT_LIST_HEAD(&req->r_osd_item);
194 /* create reply message */
196 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
198 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
199 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
201 ceph_osdc_put_request(req);
206 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
207 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
209 /* create request message; allow space for oid */
211 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
213 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
215 ceph_osdc_put_request(req);
219 memset(msg->front.iov_base, 0, msg->front.iov_len);
221 req->r_request = msg;
225 EXPORT_SYMBOL(ceph_osdc_alloc_request);
227 static bool osd_req_opcode_valid(u16 opcode)
230 case CEPH_OSD_OP_READ:
231 case CEPH_OSD_OP_STAT:
232 case CEPH_OSD_OP_MAPEXT:
233 case CEPH_OSD_OP_MASKTRUNC:
234 case CEPH_OSD_OP_SPARSE_READ:
235 case CEPH_OSD_OP_NOTIFY:
236 case CEPH_OSD_OP_NOTIFY_ACK:
237 case CEPH_OSD_OP_ASSERT_VER:
238 case CEPH_OSD_OP_WRITE:
239 case CEPH_OSD_OP_WRITEFULL:
240 case CEPH_OSD_OP_TRUNCATE:
241 case CEPH_OSD_OP_ZERO:
242 case CEPH_OSD_OP_DELETE:
243 case CEPH_OSD_OP_APPEND:
244 case CEPH_OSD_OP_STARTSYNC:
245 case CEPH_OSD_OP_SETTRUNC:
246 case CEPH_OSD_OP_TRIMTRUNC:
247 case CEPH_OSD_OP_TMAPUP:
248 case CEPH_OSD_OP_TMAPPUT:
249 case CEPH_OSD_OP_TMAPGET:
250 case CEPH_OSD_OP_CREATE:
251 case CEPH_OSD_OP_ROLLBACK:
252 case CEPH_OSD_OP_WATCH:
253 case CEPH_OSD_OP_OMAPGETKEYS:
254 case CEPH_OSD_OP_OMAPGETVALS:
255 case CEPH_OSD_OP_OMAPGETHEADER:
256 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
257 case CEPH_OSD_OP_OMAPSETVALS:
258 case CEPH_OSD_OP_OMAPSETHEADER:
259 case CEPH_OSD_OP_OMAPCLEAR:
260 case CEPH_OSD_OP_OMAPRMKEYS:
261 case CEPH_OSD_OP_OMAP_CMP:
262 case CEPH_OSD_OP_CLONERANGE:
263 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
264 case CEPH_OSD_OP_SRC_CMPXATTR:
265 case CEPH_OSD_OP_GETXATTR:
266 case CEPH_OSD_OP_GETXATTRS:
267 case CEPH_OSD_OP_CMPXATTR:
268 case CEPH_OSD_OP_SETXATTR:
269 case CEPH_OSD_OP_SETXATTRS:
270 case CEPH_OSD_OP_RESETXATTRS:
271 case CEPH_OSD_OP_RMXATTR:
272 case CEPH_OSD_OP_PULL:
273 case CEPH_OSD_OP_PUSH:
274 case CEPH_OSD_OP_BALANCEREADS:
275 case CEPH_OSD_OP_UNBALANCEREADS:
276 case CEPH_OSD_OP_SCRUB:
277 case CEPH_OSD_OP_SCRUB_RESERVE:
278 case CEPH_OSD_OP_SCRUB_UNRESERVE:
279 case CEPH_OSD_OP_SCRUB_STOP:
280 case CEPH_OSD_OP_SCRUB_MAP:
281 case CEPH_OSD_OP_WRLOCK:
282 case CEPH_OSD_OP_WRUNLOCK:
283 case CEPH_OSD_OP_RDLOCK:
284 case CEPH_OSD_OP_RDUNLOCK:
285 case CEPH_OSD_OP_UPLOCK:
286 case CEPH_OSD_OP_DNLOCK:
287 case CEPH_OSD_OP_CALL:
288 case CEPH_OSD_OP_PGLS:
289 case CEPH_OSD_OP_PGLS_FILTER:
297 * This is an osd op init function for opcodes that have no data or
298 * other information associated with them. It also serves as a
299 * common init routine for all the other init functions, below.
301 void osd_req_op_init(struct ceph_osd_req_op *op, u16 opcode)
303 BUG_ON(!osd_req_opcode_valid(opcode));
305 memset(op, 0, sizeof (*op));
310 void osd_req_op_extent_init(struct ceph_osd_req_op *op, u16 opcode,
311 u64 offset, u64 length,
312 u64 truncate_size, u32 truncate_seq)
314 size_t payload_len = 0;
316 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
318 osd_req_op_init(op, opcode);
320 op->extent.offset = offset;
321 op->extent.length = length;
322 op->extent.truncate_size = truncate_size;
323 op->extent.truncate_seq = truncate_seq;
324 if (opcode == CEPH_OSD_OP_WRITE)
325 payload_len += length;
327 op->payload_len = payload_len;
329 EXPORT_SYMBOL(osd_req_op_extent_init);
331 void osd_req_op_extent_update(struct ceph_osd_req_op *op, u64 length)
333 u64 previous = op->extent.length;
335 if (length == previous)
336 return; /* Nothing to do */
337 BUG_ON(length > previous);
339 op->extent.length = length;
340 op->payload_len -= previous - length;
342 EXPORT_SYMBOL(osd_req_op_extent_update);
344 void osd_req_op_cls_init(struct ceph_osd_req_op *op, u16 opcode,
345 const char *class, const char *method,
346 const void *request_data, size_t request_data_size)
348 size_t payload_len = 0;
351 BUG_ON(opcode != CEPH_OSD_OP_CALL);
353 osd_req_op_init(op, opcode);
355 op->cls.class_name = class;
356 size = strlen(class);
357 BUG_ON(size > (size_t) U8_MAX);
358 op->cls.class_len = size;
361 op->cls.method_name = method;
362 size = strlen(method);
363 BUG_ON(size > (size_t) U8_MAX);
364 op->cls.method_len = size;
367 op->cls.indata = request_data;
368 BUG_ON(request_data_size > (size_t) U32_MAX);
369 op->cls.indata_len = (u32) request_data_size;
370 payload_len += request_data_size;
372 op->cls.argc = 0; /* currently unused */
374 op->payload_len = payload_len;
376 EXPORT_SYMBOL(osd_req_op_cls_init);
378 void osd_req_op_watch_init(struct ceph_osd_req_op *op, u16 opcode,
379 u64 cookie, u64 version, int flag)
381 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
383 osd_req_op_init(op, opcode);
385 op->watch.cookie = cookie;
386 /* op->watch.ver = version; */ /* XXX 3847 */
387 op->watch.ver = cpu_to_le64(version);
388 if (opcode == CEPH_OSD_OP_WATCH && flag)
389 op->watch.flag = (u8) 1;
391 EXPORT_SYMBOL(osd_req_op_watch_init);
393 static u64 osd_req_encode_op(struct ceph_osd_request *req,
394 struct ceph_osd_op *dst,
395 struct ceph_osd_req_op *src)
397 u64 out_data_len = 0;
398 struct ceph_pagelist *pagelist;
400 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
401 pr_err("unrecognized osd opcode %d\n", src->op);
407 case CEPH_OSD_OP_STAT:
409 case CEPH_OSD_OP_READ:
410 case CEPH_OSD_OP_WRITE:
411 if (src->op == CEPH_OSD_OP_WRITE)
412 out_data_len = src->extent.length;
413 dst->extent.offset = cpu_to_le64(src->extent.offset);
414 dst->extent.length = cpu_to_le64(src->extent.length);
415 dst->extent.truncate_size =
416 cpu_to_le64(src->extent.truncate_size);
417 dst->extent.truncate_seq =
418 cpu_to_le32(src->extent.truncate_seq);
420 case CEPH_OSD_OP_CALL:
421 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
423 ceph_pagelist_init(pagelist);
425 dst->cls.class_len = src->cls.class_len;
426 dst->cls.method_len = src->cls.method_len;
427 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
428 ceph_pagelist_append(pagelist, src->cls.class_name,
430 ceph_pagelist_append(pagelist, src->cls.method_name,
431 src->cls.method_len);
432 ceph_pagelist_append(pagelist, src->cls.indata,
433 src->cls.indata_len);
435 ceph_osd_data_pagelist_init(&req->r_data_out, pagelist);
436 out_data_len = pagelist->length;
438 case CEPH_OSD_OP_STARTSYNC:
440 case CEPH_OSD_OP_NOTIFY_ACK:
441 case CEPH_OSD_OP_WATCH:
442 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
443 dst->watch.ver = cpu_to_le64(src->watch.ver);
444 dst->watch.flag = src->watch.flag;
447 pr_err("unsupported osd opcode %s\n",
448 ceph_osd_op_name(src->op));
453 dst->op = cpu_to_le16(src->op);
454 dst->payload_len = cpu_to_le32(src->payload_len);
460 * build new request AND message
463 void ceph_osdc_build_request(struct ceph_osd_request *req,
464 u64 off, unsigned int num_ops,
465 struct ceph_osd_req_op *src_ops,
466 struct ceph_snap_context *snapc, u64 snap_id,
467 struct timespec *mtime)
469 struct ceph_msg *msg = req->r_request;
470 struct ceph_osd_req_op *src_op;
473 int flags = req->r_flags;
477 req->r_num_ops = num_ops;
478 req->r_snapid = snap_id;
479 req->r_snapc = ceph_get_snap_context(snapc);
482 msg->hdr.version = cpu_to_le16(4);
484 p = msg->front.iov_base;
485 ceph_encode_32(&p, 1); /* client_inc is always 1 */
486 req->r_request_osdmap_epoch = p;
488 req->r_request_flags = p;
490 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
491 ceph_encode_timespec(p, mtime);
492 p += sizeof(struct ceph_timespec);
493 req->r_request_reassert_version = p;
494 p += sizeof(struct ceph_eversion); /* will get filled in */
497 ceph_encode_8(&p, 4);
498 ceph_encode_8(&p, 4);
499 ceph_encode_32(&p, 8 + 4 + 4);
500 req->r_request_pool = p;
502 ceph_encode_32(&p, -1); /* preferred */
503 ceph_encode_32(&p, 0); /* key len */
505 ceph_encode_8(&p, 1);
506 req->r_request_pgid = p;
508 ceph_encode_32(&p, -1); /* preferred */
511 ceph_encode_32(&p, req->r_oid_len);
512 memcpy(p, req->r_oid, req->r_oid_len);
513 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
516 /* ops--can imply data */
517 ceph_encode_16(&p, num_ops);
519 req->r_request_ops = p;
521 for (i = 0; i < num_ops; i++, src_op++) {
522 data_len += osd_req_encode_op(req, p, src_op);
523 p += sizeof(struct ceph_osd_op);
527 ceph_encode_64(&p, req->r_snapid);
528 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
529 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
531 for (i = 0; i < snapc->num_snaps; i++) {
532 ceph_encode_64(&p, req->r_snapc->snaps[i]);
536 req->r_request_attempts = p;
540 if (flags & CEPH_OSD_FLAG_WRITE) {
544 * The header "data_off" is a hint to the receiver
545 * allowing it to align received data into its
546 * buffers such that there's no need to re-copy
547 * it before writing it to disk (direct I/O).
549 data_off = (u16) (off & 0xffff);
550 req->r_request->hdr.data_off = cpu_to_le16(data_off);
552 req->r_request->hdr.data_len = cpu_to_le32(data_len);
554 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
555 msg_size = p - msg->front.iov_base;
556 msg->front.iov_len = msg_size;
557 msg->hdr.front_len = cpu_to_le32(msg_size);
559 dout("build_request msg_size was %d\n", (int)msg_size);
561 EXPORT_SYMBOL(ceph_osdc_build_request);
564 * build new request AND message, calculate layout, and adjust file
567 * if the file was recently truncated, we include information about its
568 * old and new size so that the object can be updated appropriately. (we
569 * avoid synchronously deleting truncated objects because it's slow.)
571 * if @do_sync, include a 'startsync' command so that the osd will flush
574 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
575 struct ceph_file_layout *layout,
576 struct ceph_vino vino,
577 u64 off, u64 *plen, int num_ops,
578 struct ceph_osd_req_op *ops,
579 int opcode, int flags,
580 struct ceph_snap_context *snapc,
585 struct ceph_osd_request *req;
593 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
595 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
598 return ERR_PTR(-ENOMEM);
599 req->r_flags = flags;
601 /* calculate max write size */
602 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
604 ceph_osdc_put_request(req);
608 object_size = le32_to_cpu(layout->fl_object_size);
609 object_base = off - objoff;
610 if (truncate_size <= object_base) {
613 truncate_size -= object_base;
614 if (truncate_size > object_size)
615 truncate_size = object_size;
618 osd_req_op_extent_init(&ops[0], opcode, objoff, objlen,
619 truncate_size, truncate_seq);
621 * A second op in the ops array means the caller wants to
622 * also issue a include a 'startsync' command so that the
623 * osd will flush data quickly.
626 osd_req_op_init(&ops[1], CEPH_OSD_OP_STARTSYNC);
628 req->r_file_layout = *layout; /* keep a copy */
630 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
632 req->r_oid_len = strlen(req->r_oid);
636 EXPORT_SYMBOL(ceph_osdc_new_request);
639 * We keep osd requests in an rbtree, sorted by ->r_tid.
641 static void __insert_request(struct ceph_osd_client *osdc,
642 struct ceph_osd_request *new)
644 struct rb_node **p = &osdc->requests.rb_node;
645 struct rb_node *parent = NULL;
646 struct ceph_osd_request *req = NULL;
650 req = rb_entry(parent, struct ceph_osd_request, r_node);
651 if (new->r_tid < req->r_tid)
653 else if (new->r_tid > req->r_tid)
659 rb_link_node(&new->r_node, parent, p);
660 rb_insert_color(&new->r_node, &osdc->requests);
663 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
666 struct ceph_osd_request *req;
667 struct rb_node *n = osdc->requests.rb_node;
670 req = rb_entry(n, struct ceph_osd_request, r_node);
671 if (tid < req->r_tid)
673 else if (tid > req->r_tid)
681 static struct ceph_osd_request *
682 __lookup_request_ge(struct ceph_osd_client *osdc,
685 struct ceph_osd_request *req;
686 struct rb_node *n = osdc->requests.rb_node;
689 req = rb_entry(n, struct ceph_osd_request, r_node);
690 if (tid < req->r_tid) {
694 } else if (tid > req->r_tid) {
704 * Resubmit requests pending on the given osd.
706 static void __kick_osd_requests(struct ceph_osd_client *osdc,
707 struct ceph_osd *osd)
709 struct ceph_osd_request *req, *nreq;
713 dout("__kick_osd_requests osd%d\n", osd->o_osd);
714 err = __reset_osd(osdc, osd);
718 * Build up a list of requests to resend by traversing the
719 * osd's list of requests. Requests for a given object are
720 * sent in tid order, and that is also the order they're
721 * kept on this list. Therefore all requests that are in
722 * flight will be found first, followed by all requests that
723 * have not yet been sent. And to resend requests while
724 * preserving this order we will want to put any sent
725 * requests back on the front of the osd client's unsent
728 * So we build a separate ordered list of already-sent
729 * requests for the affected osd and splice it onto the
730 * front of the osd client's unsent list. Once we've seen a
731 * request that has not yet been sent we're done. Those
732 * requests are already sitting right where they belong.
734 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
737 list_move_tail(&req->r_req_lru_item, &resend);
738 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
741 req->r_flags |= CEPH_OSD_FLAG_RETRY;
743 list_splice(&resend, &osdc->req_unsent);
746 * Linger requests are re-registered before sending, which
747 * sets up a new tid for each. We add them to the unsent
748 * list at the end to keep things in tid order.
750 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
753 * reregister request prior to unregistering linger so
754 * that r_osd is preserved.
756 BUG_ON(!list_empty(&req->r_req_lru_item));
757 __register_request(osdc, req);
758 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
759 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
760 __unregister_linger_request(osdc, req);
761 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
767 * If the osd connection drops, we need to resubmit all requests.
769 static void osd_reset(struct ceph_connection *con)
771 struct ceph_osd *osd = con->private;
772 struct ceph_osd_client *osdc;
776 dout("osd_reset osd%d\n", osd->o_osd);
778 down_read(&osdc->map_sem);
779 mutex_lock(&osdc->request_mutex);
780 __kick_osd_requests(osdc, osd);
782 mutex_unlock(&osdc->request_mutex);
783 up_read(&osdc->map_sem);
787 * Track open sessions with osds.
789 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
791 struct ceph_osd *osd;
793 osd = kzalloc(sizeof(*osd), GFP_NOFS);
797 atomic_set(&osd->o_ref, 1);
800 RB_CLEAR_NODE(&osd->o_node);
801 INIT_LIST_HEAD(&osd->o_requests);
802 INIT_LIST_HEAD(&osd->o_linger_requests);
803 INIT_LIST_HEAD(&osd->o_osd_lru);
804 osd->o_incarnation = 1;
806 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
808 INIT_LIST_HEAD(&osd->o_keepalive_item);
812 static struct ceph_osd *get_osd(struct ceph_osd *osd)
814 if (atomic_inc_not_zero(&osd->o_ref)) {
815 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
816 atomic_read(&osd->o_ref));
819 dout("get_osd %p FAIL\n", osd);
824 static void put_osd(struct ceph_osd *osd)
826 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
827 atomic_read(&osd->o_ref) - 1);
828 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
829 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
831 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
837 * remove an osd from our map
839 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
841 dout("__remove_osd %p\n", osd);
842 BUG_ON(!list_empty(&osd->o_requests));
843 rb_erase(&osd->o_node, &osdc->osds);
844 list_del_init(&osd->o_osd_lru);
845 ceph_con_close(&osd->o_con);
849 static void remove_all_osds(struct ceph_osd_client *osdc)
851 dout("%s %p\n", __func__, osdc);
852 mutex_lock(&osdc->request_mutex);
853 while (!RB_EMPTY_ROOT(&osdc->osds)) {
854 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
855 struct ceph_osd, o_node);
856 __remove_osd(osdc, osd);
858 mutex_unlock(&osdc->request_mutex);
861 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
862 struct ceph_osd *osd)
864 dout("__move_osd_to_lru %p\n", osd);
865 BUG_ON(!list_empty(&osd->o_osd_lru));
866 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
867 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
870 static void __remove_osd_from_lru(struct ceph_osd *osd)
872 dout("__remove_osd_from_lru %p\n", osd);
873 if (!list_empty(&osd->o_osd_lru))
874 list_del_init(&osd->o_osd_lru);
877 static void remove_old_osds(struct ceph_osd_client *osdc)
879 struct ceph_osd *osd, *nosd;
881 dout("__remove_old_osds %p\n", osdc);
882 mutex_lock(&osdc->request_mutex);
883 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
884 if (time_before(jiffies, osd->lru_ttl))
886 __remove_osd(osdc, osd);
888 mutex_unlock(&osdc->request_mutex);
894 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
896 struct ceph_entity_addr *peer_addr;
898 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
899 if (list_empty(&osd->o_requests) &&
900 list_empty(&osd->o_linger_requests)) {
901 __remove_osd(osdc, osd);
906 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
907 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
908 !ceph_con_opened(&osd->o_con)) {
909 struct ceph_osd_request *req;
911 dout(" osd addr hasn't changed and connection never opened,"
912 " letting msgr retry");
913 /* touch each r_stamp for handle_timeout()'s benfit */
914 list_for_each_entry(req, &osd->o_requests, r_osd_item)
915 req->r_stamp = jiffies;
920 ceph_con_close(&osd->o_con);
921 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
922 osd->o_incarnation++;
927 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
929 struct rb_node **p = &osdc->osds.rb_node;
930 struct rb_node *parent = NULL;
931 struct ceph_osd *osd = NULL;
933 dout("__insert_osd %p osd%d\n", new, new->o_osd);
936 osd = rb_entry(parent, struct ceph_osd, o_node);
937 if (new->o_osd < osd->o_osd)
939 else if (new->o_osd > osd->o_osd)
945 rb_link_node(&new->o_node, parent, p);
946 rb_insert_color(&new->o_node, &osdc->osds);
949 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
951 struct ceph_osd *osd;
952 struct rb_node *n = osdc->osds.rb_node;
955 osd = rb_entry(n, struct ceph_osd, o_node);
958 else if (o > osd->o_osd)
966 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
968 schedule_delayed_work(&osdc->timeout_work,
969 osdc->client->options->osd_keepalive_timeout * HZ);
972 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
974 cancel_delayed_work(&osdc->timeout_work);
978 * Register request, assign tid. If this is the first request, set up
981 static void __register_request(struct ceph_osd_client *osdc,
982 struct ceph_osd_request *req)
984 req->r_tid = ++osdc->last_tid;
985 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
986 dout("__register_request %p tid %lld\n", req, req->r_tid);
987 __insert_request(osdc, req);
988 ceph_osdc_get_request(req);
989 osdc->num_requests++;
990 if (osdc->num_requests == 1) {
991 dout(" first request, scheduling timeout\n");
992 __schedule_osd_timeout(osdc);
997 * called under osdc->request_mutex
999 static void __unregister_request(struct ceph_osd_client *osdc,
1000 struct ceph_osd_request *req)
1002 if (RB_EMPTY_NODE(&req->r_node)) {
1003 dout("__unregister_request %p tid %lld not registered\n",
1008 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1009 rb_erase(&req->r_node, &osdc->requests);
1010 osdc->num_requests--;
1013 /* make sure the original request isn't in flight. */
1014 ceph_msg_revoke(req->r_request);
1016 list_del_init(&req->r_osd_item);
1017 if (list_empty(&req->r_osd->o_requests) &&
1018 list_empty(&req->r_osd->o_linger_requests)) {
1019 dout("moving osd to %p lru\n", req->r_osd);
1020 __move_osd_to_lru(osdc, req->r_osd);
1022 if (list_empty(&req->r_linger_item))
1026 list_del_init(&req->r_req_lru_item);
1027 ceph_osdc_put_request(req);
1029 if (osdc->num_requests == 0) {
1030 dout(" no requests, canceling timeout\n");
1031 __cancel_osd_timeout(osdc);
1036 * Cancel a previously queued request message
1038 static void __cancel_request(struct ceph_osd_request *req)
1040 if (req->r_sent && req->r_osd) {
1041 ceph_msg_revoke(req->r_request);
1046 static void __register_linger_request(struct ceph_osd_client *osdc,
1047 struct ceph_osd_request *req)
1049 dout("__register_linger_request %p\n", req);
1050 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1052 list_add_tail(&req->r_linger_osd,
1053 &req->r_osd->o_linger_requests);
1056 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1057 struct ceph_osd_request *req)
1059 dout("__unregister_linger_request %p\n", req);
1060 list_del_init(&req->r_linger_item);
1062 list_del_init(&req->r_linger_osd);
1064 if (list_empty(&req->r_osd->o_requests) &&
1065 list_empty(&req->r_osd->o_linger_requests)) {
1066 dout("moving osd to %p lru\n", req->r_osd);
1067 __move_osd_to_lru(osdc, req->r_osd);
1069 if (list_empty(&req->r_osd_item))
1074 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1075 struct ceph_osd_request *req)
1077 mutex_lock(&osdc->request_mutex);
1078 if (req->r_linger) {
1079 __unregister_linger_request(osdc, req);
1080 ceph_osdc_put_request(req);
1082 mutex_unlock(&osdc->request_mutex);
1084 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1086 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1087 struct ceph_osd_request *req)
1089 if (!req->r_linger) {
1090 dout("set_request_linger %p\n", req);
1093 * caller is now responsible for calling
1094 * unregister_linger_request
1096 ceph_osdc_get_request(req);
1099 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1102 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1103 * (as needed), and set the request r_osd appropriately. If there is
1104 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1105 * (unsent, homeless) or leave on in-flight lru.
1107 * Return 0 if unchanged, 1 if changed, or negative on error.
1109 * Caller should hold map_sem for read and request_mutex.
1111 static int __map_request(struct ceph_osd_client *osdc,
1112 struct ceph_osd_request *req, int force_resend)
1114 struct ceph_pg pgid;
1115 int acting[CEPH_PG_MAX_SIZE];
1116 int o = -1, num = 0;
1119 dout("map_request %p tid %lld\n", req, req->r_tid);
1120 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1121 ceph_file_layout_pg_pool(req->r_file_layout));
1123 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1128 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1134 if ((!force_resend &&
1135 req->r_osd && req->r_osd->o_osd == o &&
1136 req->r_sent >= req->r_osd->o_incarnation &&
1137 req->r_num_pg_osds == num &&
1138 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1139 (req->r_osd == NULL && o == -1))
1140 return 0; /* no change */
1142 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1143 req->r_tid, pgid.pool, pgid.seed, o,
1144 req->r_osd ? req->r_osd->o_osd : -1);
1146 /* record full pg acting set */
1147 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1148 req->r_num_pg_osds = num;
1151 __cancel_request(req);
1152 list_del_init(&req->r_osd_item);
1156 req->r_osd = __lookup_osd(osdc, o);
1157 if (!req->r_osd && o >= 0) {
1159 req->r_osd = create_osd(osdc, o);
1161 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1165 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1166 __insert_osd(osdc, req->r_osd);
1168 ceph_con_open(&req->r_osd->o_con,
1169 CEPH_ENTITY_TYPE_OSD, o,
1170 &osdc->osdmap->osd_addr[o]);
1174 __remove_osd_from_lru(req->r_osd);
1175 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1176 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1178 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1180 err = 1; /* osd or pg changed */
1187 * caller should hold map_sem (for read) and request_mutex
1189 static void __send_request(struct ceph_osd_client *osdc,
1190 struct ceph_osd_request *req)
1194 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1195 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1196 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1198 /* fill in message content that changes each time we send it */
1199 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1200 put_unaligned_le32(req->r_flags, req->r_request_flags);
1201 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1202 p = req->r_request_pgid;
1203 ceph_encode_64(&p, req->r_pgid.pool);
1204 ceph_encode_32(&p, req->r_pgid.seed);
1205 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1206 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1207 sizeof(req->r_reassert_version));
1209 req->r_stamp = jiffies;
1210 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1212 ceph_msg_get(req->r_request); /* send consumes a ref */
1213 ceph_con_send(&req->r_osd->o_con, req->r_request);
1214 req->r_sent = req->r_osd->o_incarnation;
1218 * Send any requests in the queue (req_unsent).
1220 static void __send_queued(struct ceph_osd_client *osdc)
1222 struct ceph_osd_request *req, *tmp;
1224 dout("__send_queued\n");
1225 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1226 __send_request(osdc, req);
1230 * Timeout callback, called every N seconds when 1 or more osd
1231 * requests has been active for more than N seconds. When this
1232 * happens, we ping all OSDs with requests who have timed out to
1233 * ensure any communications channel reset is detected. Reset the
1234 * request timeouts another N seconds in the future as we go.
1235 * Reschedule the timeout event another N seconds in future (unless
1236 * there are no open requests).
1238 static void handle_timeout(struct work_struct *work)
1240 struct ceph_osd_client *osdc =
1241 container_of(work, struct ceph_osd_client, timeout_work.work);
1242 struct ceph_osd_request *req;
1243 struct ceph_osd *osd;
1244 unsigned long keepalive =
1245 osdc->client->options->osd_keepalive_timeout * HZ;
1246 struct list_head slow_osds;
1248 down_read(&osdc->map_sem);
1250 ceph_monc_request_next_osdmap(&osdc->client->monc);
1252 mutex_lock(&osdc->request_mutex);
1255 * ping osds that are a bit slow. this ensures that if there
1256 * is a break in the TCP connection we will notice, and reopen
1257 * a connection with that osd (from the fault callback).
1259 INIT_LIST_HEAD(&slow_osds);
1260 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1261 if (time_before(jiffies, req->r_stamp + keepalive))
1266 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1267 req->r_tid, osd->o_osd);
1268 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1270 while (!list_empty(&slow_osds)) {
1271 osd = list_entry(slow_osds.next, struct ceph_osd,
1273 list_del_init(&osd->o_keepalive_item);
1274 ceph_con_keepalive(&osd->o_con);
1277 __schedule_osd_timeout(osdc);
1278 __send_queued(osdc);
1279 mutex_unlock(&osdc->request_mutex);
1280 up_read(&osdc->map_sem);
1283 static void handle_osds_timeout(struct work_struct *work)
1285 struct ceph_osd_client *osdc =
1286 container_of(work, struct ceph_osd_client,
1287 osds_timeout_work.work);
1288 unsigned long delay =
1289 osdc->client->options->osd_idle_ttl * HZ >> 2;
1291 dout("osds timeout\n");
1292 down_read(&osdc->map_sem);
1293 remove_old_osds(osdc);
1294 up_read(&osdc->map_sem);
1296 schedule_delayed_work(&osdc->osds_timeout_work,
1297 round_jiffies_relative(delay));
1300 static void complete_request(struct ceph_osd_request *req)
1302 if (req->r_safe_callback)
1303 req->r_safe_callback(req, NULL);
1304 complete_all(&req->r_safe_completion); /* fsync waiter */
1308 * handle osd op reply. either call the callback if it is specified,
1309 * or do the completion to wake up the waiting thread.
1311 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1312 struct ceph_connection *con)
1315 struct ceph_osd_request *req;
1318 int numops, payload_len, flags;
1324 u64 reassert_version;
1326 int already_completed;
1330 tid = le64_to_cpu(msg->hdr.tid);
1331 dout("handle_reply %p tid %llu\n", msg, tid);
1333 p = msg->front.iov_base;
1334 end = p + msg->front.iov_len;
1336 ceph_decode_need(&p, end, 4, bad);
1337 object_len = ceph_decode_32(&p);
1338 ceph_decode_need(&p, end, object_len, bad);
1341 err = ceph_decode_pgid(&p, end, &pg);
1345 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1346 flags = ceph_decode_64(&p);
1347 result = ceph_decode_32(&p);
1348 reassert_epoch = ceph_decode_32(&p);
1349 reassert_version = ceph_decode_64(&p);
1350 osdmap_epoch = ceph_decode_32(&p);
1353 mutex_lock(&osdc->request_mutex);
1354 req = __lookup_request(osdc, tid);
1356 dout("handle_reply tid %llu dne\n", tid);
1359 ceph_osdc_get_request(req);
1361 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1364 ceph_decode_need(&p, end, 4, bad);
1365 numops = ceph_decode_32(&p);
1366 if (numops > CEPH_OSD_MAX_OP)
1368 if (numops != req->r_num_ops)
1371 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1372 for (i = 0; i < numops; i++) {
1373 struct ceph_osd_op *op = p;
1376 len = le32_to_cpu(op->payload_len);
1377 req->r_reply_op_len[i] = len;
1378 dout(" op %d has %d bytes\n", i, len);
1382 bytes = le32_to_cpu(msg->hdr.data_len);
1383 if (payload_len != bytes) {
1384 pr_warning("sum of op payload lens %d != data_len %d",
1385 payload_len, bytes);
1389 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1390 retry_attempt = ceph_decode_32(&p);
1391 for (i = 0; i < numops; i++)
1392 req->r_reply_op_result[i] = ceph_decode_32(&p);
1394 if (!req->r_got_reply) {
1396 req->r_result = result;
1397 dout("handle_reply result %d bytes %d\n", req->r_result,
1399 if (req->r_result == 0)
1400 req->r_result = bytes;
1402 /* in case this is a write and we need to replay, */
1403 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1404 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1406 req->r_got_reply = 1;
1407 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1408 dout("handle_reply tid %llu dup ack\n", tid);
1409 mutex_unlock(&osdc->request_mutex);
1413 dout("handle_reply tid %llu flags %d\n", tid, flags);
1415 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1416 __register_linger_request(osdc, req);
1418 /* either this is a read, or we got the safe response */
1420 (flags & CEPH_OSD_FLAG_ONDISK) ||
1421 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1422 __unregister_request(osdc, req);
1424 already_completed = req->r_completed;
1425 req->r_completed = 1;
1426 mutex_unlock(&osdc->request_mutex);
1427 if (already_completed)
1430 if (req->r_callback)
1431 req->r_callback(req, msg);
1433 complete_all(&req->r_completion);
1435 if (flags & CEPH_OSD_FLAG_ONDISK)
1436 complete_request(req);
1439 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1440 ceph_osdc_put_request(req);
1444 ceph_osdc_put_request(req);
1446 mutex_unlock(&osdc->request_mutex);
1448 pr_err("corrupt osd_op_reply got %d %d\n",
1449 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1453 static void reset_changed_osds(struct ceph_osd_client *osdc)
1455 struct rb_node *p, *n;
1457 for (p = rb_first(&osdc->osds); p; p = n) {
1458 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1461 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1462 memcmp(&osd->o_con.peer_addr,
1463 ceph_osd_addr(osdc->osdmap,
1465 sizeof(struct ceph_entity_addr)) != 0)
1466 __reset_osd(osdc, osd);
1471 * Requeue requests whose mapping to an OSD has changed. If requests map to
1472 * no osd, request a new map.
1474 * Caller should hold map_sem for read.
1476 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1478 struct ceph_osd_request *req, *nreq;
1483 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1484 mutex_lock(&osdc->request_mutex);
1485 for (p = rb_first(&osdc->requests); p; ) {
1486 req = rb_entry(p, struct ceph_osd_request, r_node);
1490 * For linger requests that have not yet been
1491 * registered, move them to the linger list; they'll
1492 * be sent to the osd in the loop below. Unregister
1493 * the request before re-registering it as a linger
1494 * request to ensure the __map_request() below
1495 * will decide it needs to be sent.
1497 if (req->r_linger && list_empty(&req->r_linger_item)) {
1498 dout("%p tid %llu restart on osd%d\n",
1500 req->r_osd ? req->r_osd->o_osd : -1);
1501 __unregister_request(osdc, req);
1502 __register_linger_request(osdc, req);
1506 err = __map_request(osdc, req, force_resend);
1508 continue; /* error */
1509 if (req->r_osd == NULL) {
1510 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1511 needmap++; /* request a newer map */
1512 } else if (err > 0) {
1513 if (!req->r_linger) {
1514 dout("%p tid %llu requeued on osd%d\n", req,
1516 req->r_osd ? req->r_osd->o_osd : -1);
1517 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1522 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1524 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1526 err = __map_request(osdc, req, force_resend);
1527 dout("__map_request returned %d\n", err);
1529 continue; /* no change and no osd was specified */
1531 continue; /* hrm! */
1532 if (req->r_osd == NULL) {
1533 dout("tid %llu maps to no valid osd\n", req->r_tid);
1534 needmap++; /* request a newer map */
1538 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1539 req->r_osd ? req->r_osd->o_osd : -1);
1540 __register_request(osdc, req);
1541 __unregister_linger_request(osdc, req);
1543 mutex_unlock(&osdc->request_mutex);
1546 dout("%d requests for down osds, need new map\n", needmap);
1547 ceph_monc_request_next_osdmap(&osdc->client->monc);
1549 reset_changed_osds(osdc);
1554 * Process updated osd map.
1556 * The message contains any number of incremental and full maps, normally
1557 * indicating some sort of topology change in the cluster. Kick requests
1558 * off to different OSDs as needed.
1560 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1562 void *p, *end, *next;
1563 u32 nr_maps, maplen;
1565 struct ceph_osdmap *newmap = NULL, *oldmap;
1567 struct ceph_fsid fsid;
1569 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1570 p = msg->front.iov_base;
1571 end = p + msg->front.iov_len;
1574 ceph_decode_need(&p, end, sizeof(fsid), bad);
1575 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1576 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1579 down_write(&osdc->map_sem);
1581 /* incremental maps */
1582 ceph_decode_32_safe(&p, end, nr_maps, bad);
1583 dout(" %d inc maps\n", nr_maps);
1584 while (nr_maps > 0) {
1585 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1586 epoch = ceph_decode_32(&p);
1587 maplen = ceph_decode_32(&p);
1588 ceph_decode_need(&p, end, maplen, bad);
1590 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1591 dout("applying incremental map %u len %d\n",
1593 newmap = osdmap_apply_incremental(&p, next,
1595 &osdc->client->msgr);
1596 if (IS_ERR(newmap)) {
1597 err = PTR_ERR(newmap);
1601 if (newmap != osdc->osdmap) {
1602 ceph_osdmap_destroy(osdc->osdmap);
1603 osdc->osdmap = newmap;
1605 kick_requests(osdc, 0);
1607 dout("ignoring incremental map %u len %d\n",
1617 ceph_decode_32_safe(&p, end, nr_maps, bad);
1618 dout(" %d full maps\n", nr_maps);
1620 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1621 epoch = ceph_decode_32(&p);
1622 maplen = ceph_decode_32(&p);
1623 ceph_decode_need(&p, end, maplen, bad);
1625 dout("skipping non-latest full map %u len %d\n",
1627 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1628 dout("skipping full map %u len %d, "
1629 "older than our %u\n", epoch, maplen,
1630 osdc->osdmap->epoch);
1632 int skipped_map = 0;
1634 dout("taking full map %u len %d\n", epoch, maplen);
1635 newmap = osdmap_decode(&p, p+maplen);
1636 if (IS_ERR(newmap)) {
1637 err = PTR_ERR(newmap);
1641 oldmap = osdc->osdmap;
1642 osdc->osdmap = newmap;
1644 if (oldmap->epoch + 1 < newmap->epoch)
1646 ceph_osdmap_destroy(oldmap);
1648 kick_requests(osdc, skipped_map);
1655 downgrade_write(&osdc->map_sem);
1656 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1659 * subscribe to subsequent osdmap updates if full to ensure
1660 * we find out when we are no longer full and stop returning
1663 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1664 ceph_monc_request_next_osdmap(&osdc->client->monc);
1666 mutex_lock(&osdc->request_mutex);
1667 __send_queued(osdc);
1668 mutex_unlock(&osdc->request_mutex);
1669 up_read(&osdc->map_sem);
1670 wake_up_all(&osdc->client->auth_wq);
1674 pr_err("osdc handle_map corrupt msg\n");
1676 up_write(&osdc->map_sem);
1681 * watch/notify callback event infrastructure
1683 * These callbacks are used both for watch and notify operations.
1685 static void __release_event(struct kref *kref)
1687 struct ceph_osd_event *event =
1688 container_of(kref, struct ceph_osd_event, kref);
1690 dout("__release_event %p\n", event);
1694 static void get_event(struct ceph_osd_event *event)
1696 kref_get(&event->kref);
1699 void ceph_osdc_put_event(struct ceph_osd_event *event)
1701 kref_put(&event->kref, __release_event);
1703 EXPORT_SYMBOL(ceph_osdc_put_event);
1705 static void __insert_event(struct ceph_osd_client *osdc,
1706 struct ceph_osd_event *new)
1708 struct rb_node **p = &osdc->event_tree.rb_node;
1709 struct rb_node *parent = NULL;
1710 struct ceph_osd_event *event = NULL;
1714 event = rb_entry(parent, struct ceph_osd_event, node);
1715 if (new->cookie < event->cookie)
1717 else if (new->cookie > event->cookie)
1718 p = &(*p)->rb_right;
1723 rb_link_node(&new->node, parent, p);
1724 rb_insert_color(&new->node, &osdc->event_tree);
1727 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1730 struct rb_node **p = &osdc->event_tree.rb_node;
1731 struct rb_node *parent = NULL;
1732 struct ceph_osd_event *event = NULL;
1736 event = rb_entry(parent, struct ceph_osd_event, node);
1737 if (cookie < event->cookie)
1739 else if (cookie > event->cookie)
1740 p = &(*p)->rb_right;
1747 static void __remove_event(struct ceph_osd_event *event)
1749 struct ceph_osd_client *osdc = event->osdc;
1751 if (!RB_EMPTY_NODE(&event->node)) {
1752 dout("__remove_event removed %p\n", event);
1753 rb_erase(&event->node, &osdc->event_tree);
1754 ceph_osdc_put_event(event);
1756 dout("__remove_event didn't remove %p\n", event);
1760 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1761 void (*event_cb)(u64, u64, u8, void *),
1762 void *data, struct ceph_osd_event **pevent)
1764 struct ceph_osd_event *event;
1766 event = kmalloc(sizeof(*event), GFP_NOIO);
1770 dout("create_event %p\n", event);
1771 event->cb = event_cb;
1772 event->one_shot = 0;
1775 INIT_LIST_HEAD(&event->osd_node);
1776 RB_CLEAR_NODE(&event->node);
1777 kref_init(&event->kref); /* one ref for us */
1778 kref_get(&event->kref); /* one ref for the caller */
1780 spin_lock(&osdc->event_lock);
1781 event->cookie = ++osdc->event_count;
1782 __insert_event(osdc, event);
1783 spin_unlock(&osdc->event_lock);
1788 EXPORT_SYMBOL(ceph_osdc_create_event);
1790 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1792 struct ceph_osd_client *osdc = event->osdc;
1794 dout("cancel_event %p\n", event);
1795 spin_lock(&osdc->event_lock);
1796 __remove_event(event);
1797 spin_unlock(&osdc->event_lock);
1798 ceph_osdc_put_event(event); /* caller's */
1800 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1803 static void do_event_work(struct work_struct *work)
1805 struct ceph_osd_event_work *event_work =
1806 container_of(work, struct ceph_osd_event_work, work);
1807 struct ceph_osd_event *event = event_work->event;
1808 u64 ver = event_work->ver;
1809 u64 notify_id = event_work->notify_id;
1810 u8 opcode = event_work->opcode;
1812 dout("do_event_work completing %p\n", event);
1813 event->cb(ver, notify_id, opcode, event->data);
1814 dout("do_event_work completed %p\n", event);
1815 ceph_osdc_put_event(event);
1821 * Process osd watch notifications
1823 static void handle_watch_notify(struct ceph_osd_client *osdc,
1824 struct ceph_msg *msg)
1828 u64 cookie, ver, notify_id;
1830 struct ceph_osd_event *event;
1831 struct ceph_osd_event_work *event_work;
1833 p = msg->front.iov_base;
1834 end = p + msg->front.iov_len;
1836 ceph_decode_8_safe(&p, end, proto_ver, bad);
1837 ceph_decode_8_safe(&p, end, opcode, bad);
1838 ceph_decode_64_safe(&p, end, cookie, bad);
1839 ceph_decode_64_safe(&p, end, ver, bad);
1840 ceph_decode_64_safe(&p, end, notify_id, bad);
1842 spin_lock(&osdc->event_lock);
1843 event = __find_event(osdc, cookie);
1845 BUG_ON(event->one_shot);
1848 spin_unlock(&osdc->event_lock);
1849 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1850 cookie, ver, event);
1852 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1854 dout("ERROR: could not allocate event_work\n");
1857 INIT_WORK(&event_work->work, do_event_work);
1858 event_work->event = event;
1859 event_work->ver = ver;
1860 event_work->notify_id = notify_id;
1861 event_work->opcode = opcode;
1862 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1863 dout("WARNING: failed to queue notify event work\n");
1871 ceph_osdc_put_event(event);
1875 pr_err("osdc handle_watch_notify corrupt msg\n");
1879 static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1880 struct ceph_osd_data *osd_data)
1882 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1883 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1884 if (osd_data->length)
1885 ceph_msg_data_set_pages(msg, osd_data->pages,
1886 osd_data->length, osd_data->alignment);
1887 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1888 BUG_ON(!osd_data->pagelist->length);
1889 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
1891 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1892 ceph_msg_data_set_bio(msg, osd_data->bio, osd_data->bio_length);
1895 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1900 * Register request, send initial attempt.
1902 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1903 struct ceph_osd_request *req,
1908 /* Set up response incoming data and request outgoing data fields */
1910 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1911 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
1913 down_read(&osdc->map_sem);
1914 mutex_lock(&osdc->request_mutex);
1915 __register_request(osdc, req);
1916 WARN_ON(req->r_sent);
1917 rc = __map_request(osdc, req, 0);
1920 dout("osdc_start_request failed map, "
1921 " will retry %lld\n", req->r_tid);
1926 if (req->r_osd == NULL) {
1927 dout("send_request %p no up osds in pg\n", req);
1928 ceph_monc_request_next_osdmap(&osdc->client->monc);
1930 __send_queued(osdc);
1934 mutex_unlock(&osdc->request_mutex);
1935 up_read(&osdc->map_sem);
1938 EXPORT_SYMBOL(ceph_osdc_start_request);
1941 * wait for a request to complete
1943 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1944 struct ceph_osd_request *req)
1948 rc = wait_for_completion_interruptible(&req->r_completion);
1950 mutex_lock(&osdc->request_mutex);
1951 __cancel_request(req);
1952 __unregister_request(osdc, req);
1953 mutex_unlock(&osdc->request_mutex);
1954 complete_request(req);
1955 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1959 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1960 return req->r_result;
1962 EXPORT_SYMBOL(ceph_osdc_wait_request);
1965 * sync - wait for all in-flight requests to flush. avoid starvation.
1967 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1969 struct ceph_osd_request *req;
1970 u64 last_tid, next_tid = 0;
1972 mutex_lock(&osdc->request_mutex);
1973 last_tid = osdc->last_tid;
1975 req = __lookup_request_ge(osdc, next_tid);
1978 if (req->r_tid > last_tid)
1981 next_tid = req->r_tid + 1;
1982 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1985 ceph_osdc_get_request(req);
1986 mutex_unlock(&osdc->request_mutex);
1987 dout("sync waiting on tid %llu (last is %llu)\n",
1988 req->r_tid, last_tid);
1989 wait_for_completion(&req->r_safe_completion);
1990 mutex_lock(&osdc->request_mutex);
1991 ceph_osdc_put_request(req);
1993 mutex_unlock(&osdc->request_mutex);
1994 dout("sync done (thru tid %llu)\n", last_tid);
1996 EXPORT_SYMBOL(ceph_osdc_sync);
2001 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2006 osdc->client = client;
2007 osdc->osdmap = NULL;
2008 init_rwsem(&osdc->map_sem);
2009 init_completion(&osdc->map_waiters);
2010 osdc->last_requested_map = 0;
2011 mutex_init(&osdc->request_mutex);
2013 osdc->osds = RB_ROOT;
2014 INIT_LIST_HEAD(&osdc->osd_lru);
2015 osdc->requests = RB_ROOT;
2016 INIT_LIST_HEAD(&osdc->req_lru);
2017 INIT_LIST_HEAD(&osdc->req_unsent);
2018 INIT_LIST_HEAD(&osdc->req_notarget);
2019 INIT_LIST_HEAD(&osdc->req_linger);
2020 osdc->num_requests = 0;
2021 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
2022 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2023 spin_lock_init(&osdc->event_lock);
2024 osdc->event_tree = RB_ROOT;
2025 osdc->event_count = 0;
2027 schedule_delayed_work(&osdc->osds_timeout_work,
2028 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2031 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2032 sizeof(struct ceph_osd_request));
2033 if (!osdc->req_mempool)
2036 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2037 OSD_OP_FRONT_LEN, 10, true,
2041 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2042 OSD_OPREPLY_FRONT_LEN, 10, true,
2047 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2048 if (IS_ERR(osdc->notify_wq)) {
2049 err = PTR_ERR(osdc->notify_wq);
2050 osdc->notify_wq = NULL;
2056 ceph_msgpool_destroy(&osdc->msgpool_op);
2058 mempool_destroy(osdc->req_mempool);
2063 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2065 flush_workqueue(osdc->notify_wq);
2066 destroy_workqueue(osdc->notify_wq);
2067 cancel_delayed_work_sync(&osdc->timeout_work);
2068 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2070 ceph_osdmap_destroy(osdc->osdmap);
2071 osdc->osdmap = NULL;
2073 remove_all_osds(osdc);
2074 mempool_destroy(osdc->req_mempool);
2075 ceph_msgpool_destroy(&osdc->msgpool_op);
2076 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2080 * Read some contiguous pages. If we cross a stripe boundary, shorten
2081 * *plen. Return number of bytes read, or error.
2083 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2084 struct ceph_vino vino, struct ceph_file_layout *layout,
2086 u32 truncate_seq, u64 truncate_size,
2087 struct page **pages, int num_pages, int page_align)
2089 struct ceph_osd_request *req;
2090 struct ceph_osd_req_op op;
2093 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2094 vino.snap, off, *plen);
2095 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1, &op,
2096 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2097 NULL, truncate_seq, truncate_size,
2100 return PTR_ERR(req);
2102 /* it may be a short read due to an object boundary */
2104 ceph_osd_data_pages_init(&req->r_data_in, pages, *plen, page_align,
2107 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2108 off, *plen, *plen, page_align);
2110 ceph_osdc_build_request(req, off, 1, &op, NULL, vino.snap, NULL);
2112 rc = ceph_osdc_start_request(osdc, req, false);
2114 rc = ceph_osdc_wait_request(osdc, req);
2116 ceph_osdc_put_request(req);
2117 dout("readpages result %d\n", rc);
2120 EXPORT_SYMBOL(ceph_osdc_readpages);
2123 * do a synchronous write on N pages
2125 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2126 struct ceph_file_layout *layout,
2127 struct ceph_snap_context *snapc,
2129 u32 truncate_seq, u64 truncate_size,
2130 struct timespec *mtime,
2131 struct page **pages, int num_pages)
2133 struct ceph_osd_request *req;
2134 struct ceph_osd_req_op op;
2136 int page_align = off & ~PAGE_MASK;
2138 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2139 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1, &op,
2141 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2142 snapc, truncate_seq, truncate_size,
2145 return PTR_ERR(req);
2147 /* it may be a short write due to an object boundary */
2148 ceph_osd_data_pages_init(&req->r_data_out, pages, len, page_align,
2150 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
2152 ceph_osdc_build_request(req, off, 1, &op, snapc, CEPH_NOSNAP, mtime);
2154 rc = ceph_osdc_start_request(osdc, req, true);
2156 rc = ceph_osdc_wait_request(osdc, req);
2158 ceph_osdc_put_request(req);
2161 dout("writepages result %d\n", rc);
2164 EXPORT_SYMBOL(ceph_osdc_writepages);
2167 * handle incoming message
2169 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2171 struct ceph_osd *osd = con->private;
2172 struct ceph_osd_client *osdc;
2173 int type = le16_to_cpu(msg->hdr.type);
2180 case CEPH_MSG_OSD_MAP:
2181 ceph_osdc_handle_map(osdc, msg);
2183 case CEPH_MSG_OSD_OPREPLY:
2184 handle_reply(osdc, msg, con);
2186 case CEPH_MSG_WATCH_NOTIFY:
2187 handle_watch_notify(osdc, msg);
2191 pr_err("received unknown message type %d %s\n", type,
2192 ceph_msg_type_name(type));
2199 * lookup and return message for incoming reply. set up reply message
2202 static struct ceph_msg *get_reply(struct ceph_connection *con,
2203 struct ceph_msg_header *hdr,
2206 struct ceph_osd *osd = con->private;
2207 struct ceph_osd_client *osdc = osd->o_osdc;
2209 struct ceph_osd_request *req;
2210 int front = le32_to_cpu(hdr->front_len);
2211 int data_len = le32_to_cpu(hdr->data_len);
2214 tid = le64_to_cpu(hdr->tid);
2215 mutex_lock(&osdc->request_mutex);
2216 req = __lookup_request(osdc, tid);
2220 dout("get_reply unknown tid %llu from osd%d\n", tid,
2225 if (req->r_reply->con)
2226 dout("%s revoking msg %p from old con %p\n", __func__,
2227 req->r_reply, req->r_reply->con);
2228 ceph_msg_revoke_incoming(req->r_reply);
2230 if (front > req->r_reply->front.iov_len) {
2231 pr_warning("get_reply front %d > preallocated %d\n",
2232 front, (int)req->r_reply->front.iov_len);
2233 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2236 ceph_msg_put(req->r_reply);
2239 m = ceph_msg_get(req->r_reply);
2242 struct ceph_osd_data *osd_data = &req->r_data_in;
2244 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2245 if (osd_data->pages &&
2246 unlikely(osd_data->length < data_len)) {
2248 pr_warning("tid %lld reply has %d bytes "
2249 "we had only %llu bytes ready\n",
2250 tid, data_len, osd_data->length);
2259 dout("get_reply tid %lld %p\n", tid, m);
2262 mutex_unlock(&osdc->request_mutex);
2267 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2268 struct ceph_msg_header *hdr,
2271 struct ceph_osd *osd = con->private;
2272 int type = le16_to_cpu(hdr->type);
2273 int front = le32_to_cpu(hdr->front_len);
2277 case CEPH_MSG_OSD_MAP:
2278 case CEPH_MSG_WATCH_NOTIFY:
2279 return ceph_msg_new(type, front, GFP_NOFS, false);
2280 case CEPH_MSG_OSD_OPREPLY:
2281 return get_reply(con, hdr, skip);
2283 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2291 * Wrappers to refcount containing ceph_osd struct
2293 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2295 struct ceph_osd *osd = con->private;
2301 static void put_osd_con(struct ceph_connection *con)
2303 struct ceph_osd *osd = con->private;
2311 * Note: returned pointer is the address of a structure that's
2312 * managed separately. Caller must *not* attempt to free it.
2314 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2315 int *proto, int force_new)
2317 struct ceph_osd *o = con->private;
2318 struct ceph_osd_client *osdc = o->o_osdc;
2319 struct ceph_auth_client *ac = osdc->client->monc.auth;
2320 struct ceph_auth_handshake *auth = &o->o_auth;
2322 if (force_new && auth->authorizer) {
2323 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2324 auth->authorizer = NULL;
2326 if (!auth->authorizer) {
2327 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2330 return ERR_PTR(ret);
2332 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2335 return ERR_PTR(ret);
2337 *proto = ac->protocol;
2343 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2345 struct ceph_osd *o = con->private;
2346 struct ceph_osd_client *osdc = o->o_osdc;
2347 struct ceph_auth_client *ac = osdc->client->monc.auth;
2349 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2352 static int invalidate_authorizer(struct ceph_connection *con)
2354 struct ceph_osd *o = con->private;
2355 struct ceph_osd_client *osdc = o->o_osdc;
2356 struct ceph_auth_client *ac = osdc->client->monc.auth;
2358 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2359 return ceph_monc_validate_auth(&osdc->client->monc);
2362 static const struct ceph_connection_operations osd_con_ops = {
2365 .dispatch = dispatch,
2366 .get_authorizer = get_authorizer,
2367 .verify_authorizer_reply = verify_authorizer_reply,
2368 .invalidate_authorizer = invalidate_authorizer,
2369 .alloc_msg = alloc_msg,