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);
85 void ceph_osdc_release_request(struct kref *kref)
88 struct ceph_osd_request *req = container_of(kref,
89 struct ceph_osd_request,
93 ceph_msg_put(req->r_request);
95 ceph_msg_revoke_incoming(req->r_reply);
96 ceph_msg_put(req->r_reply);
99 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
100 req->r_data_in.own_pages) {
101 num_pages = calc_pages_for((u64)req->r_data_in.alignment,
102 (u64)req->r_data_in.length);
103 ceph_release_page_vector(req->r_data_in.pages, num_pages);
105 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
106 req->r_data_out.own_pages) {
107 num_pages = calc_pages_for((u64)req->r_data_out.alignment,
108 (u64)req->r_data_out.length);
109 ceph_release_page_vector(req->r_data_out.pages, num_pages);
112 ceph_put_snap_context(req->r_snapc);
114 mempool_free(req, req->r_osdc->req_mempool);
118 EXPORT_SYMBOL(ceph_osdc_release_request);
120 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
121 struct ceph_snap_context *snapc,
122 unsigned int num_ops,
126 struct ceph_osd_request *req;
127 struct ceph_msg *msg;
130 msg_size = 4 + 4 + 8 + 8 + 4+8;
131 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
132 msg_size += 1 + 8 + 4 + 4; /* pg_t */
133 msg_size += 4 + MAX_OBJ_NAME_SIZE;
134 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
135 msg_size += 8; /* snapid */
136 msg_size += 8; /* snap_seq */
137 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
141 req = mempool_alloc(osdc->req_mempool, gfp_flags);
142 memset(req, 0, sizeof(*req));
144 req = kzalloc(sizeof(*req), gfp_flags);
150 req->r_mempool = use_mempool;
152 kref_init(&req->r_kref);
153 init_completion(&req->r_completion);
154 init_completion(&req->r_safe_completion);
155 RB_CLEAR_NODE(&req->r_node);
156 INIT_LIST_HEAD(&req->r_unsafe_item);
157 INIT_LIST_HEAD(&req->r_linger_item);
158 INIT_LIST_HEAD(&req->r_linger_osd);
159 INIT_LIST_HEAD(&req->r_req_lru_item);
160 INIT_LIST_HEAD(&req->r_osd_item);
162 /* create reply message */
164 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
166 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
167 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
169 ceph_osdc_put_request(req);
174 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
175 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
177 /* create request message; allow space for oid */
179 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
181 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
183 ceph_osdc_put_request(req);
187 memset(msg->front.iov_base, 0, msg->front.iov_len);
189 req->r_request = msg;
193 EXPORT_SYMBOL(ceph_osdc_alloc_request);
195 static bool osd_req_opcode_valid(u16 opcode)
198 case CEPH_OSD_OP_READ:
199 case CEPH_OSD_OP_STAT:
200 case CEPH_OSD_OP_MAPEXT:
201 case CEPH_OSD_OP_MASKTRUNC:
202 case CEPH_OSD_OP_SPARSE_READ:
203 case CEPH_OSD_OP_NOTIFY:
204 case CEPH_OSD_OP_NOTIFY_ACK:
205 case CEPH_OSD_OP_ASSERT_VER:
206 case CEPH_OSD_OP_WRITE:
207 case CEPH_OSD_OP_WRITEFULL:
208 case CEPH_OSD_OP_TRUNCATE:
209 case CEPH_OSD_OP_ZERO:
210 case CEPH_OSD_OP_DELETE:
211 case CEPH_OSD_OP_APPEND:
212 case CEPH_OSD_OP_STARTSYNC:
213 case CEPH_OSD_OP_SETTRUNC:
214 case CEPH_OSD_OP_TRIMTRUNC:
215 case CEPH_OSD_OP_TMAPUP:
216 case CEPH_OSD_OP_TMAPPUT:
217 case CEPH_OSD_OP_TMAPGET:
218 case CEPH_OSD_OP_CREATE:
219 case CEPH_OSD_OP_ROLLBACK:
220 case CEPH_OSD_OP_WATCH:
221 case CEPH_OSD_OP_OMAPGETKEYS:
222 case CEPH_OSD_OP_OMAPGETVALS:
223 case CEPH_OSD_OP_OMAPGETHEADER:
224 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
225 case CEPH_OSD_OP_OMAPSETVALS:
226 case CEPH_OSD_OP_OMAPSETHEADER:
227 case CEPH_OSD_OP_OMAPCLEAR:
228 case CEPH_OSD_OP_OMAPRMKEYS:
229 case CEPH_OSD_OP_OMAP_CMP:
230 case CEPH_OSD_OP_CLONERANGE:
231 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
232 case CEPH_OSD_OP_SRC_CMPXATTR:
233 case CEPH_OSD_OP_GETXATTR:
234 case CEPH_OSD_OP_GETXATTRS:
235 case CEPH_OSD_OP_CMPXATTR:
236 case CEPH_OSD_OP_SETXATTR:
237 case CEPH_OSD_OP_SETXATTRS:
238 case CEPH_OSD_OP_RESETXATTRS:
239 case CEPH_OSD_OP_RMXATTR:
240 case CEPH_OSD_OP_PULL:
241 case CEPH_OSD_OP_PUSH:
242 case CEPH_OSD_OP_BALANCEREADS:
243 case CEPH_OSD_OP_UNBALANCEREADS:
244 case CEPH_OSD_OP_SCRUB:
245 case CEPH_OSD_OP_SCRUB_RESERVE:
246 case CEPH_OSD_OP_SCRUB_UNRESERVE:
247 case CEPH_OSD_OP_SCRUB_STOP:
248 case CEPH_OSD_OP_SCRUB_MAP:
249 case CEPH_OSD_OP_WRLOCK:
250 case CEPH_OSD_OP_WRUNLOCK:
251 case CEPH_OSD_OP_RDLOCK:
252 case CEPH_OSD_OP_RDUNLOCK:
253 case CEPH_OSD_OP_UPLOCK:
254 case CEPH_OSD_OP_DNLOCK:
255 case CEPH_OSD_OP_CALL:
256 case CEPH_OSD_OP_PGLS:
257 case CEPH_OSD_OP_PGLS_FILTER:
265 * This is an osd op init function for opcodes that have no data or
266 * other information associated with them. It also serves as a
267 * common init routine for all the other init functions, below.
269 void osd_req_op_init(struct ceph_osd_req_op *op, u16 opcode)
271 BUG_ON(!osd_req_opcode_valid(opcode));
273 memset(op, 0, sizeof (*op));
278 void osd_req_op_extent_init(struct ceph_osd_req_op *op, u16 opcode,
279 u64 offset, u64 length,
280 u64 truncate_size, u32 truncate_seq)
282 size_t payload_len = 0;
284 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
286 osd_req_op_init(op, opcode);
288 op->extent.offset = offset;
289 op->extent.length = length;
290 op->extent.truncate_size = truncate_size;
291 op->extent.truncate_seq = truncate_seq;
292 if (opcode == CEPH_OSD_OP_WRITE)
293 payload_len += length;
295 op->payload_len = payload_len;
297 EXPORT_SYMBOL(osd_req_op_extent_init);
299 void osd_req_op_extent_update(struct ceph_osd_req_op *op, u64 length)
301 u64 previous = op->extent.length;
303 if (length == previous)
304 return; /* Nothing to do */
305 BUG_ON(length > previous);
307 op->extent.length = length;
308 op->payload_len -= previous - length;
310 EXPORT_SYMBOL(osd_req_op_extent_update);
312 void osd_req_op_cls_init(struct ceph_osd_req_op *op, u16 opcode,
313 const char *class, const char *method,
314 const void *request_data, size_t request_data_size)
316 size_t payload_len = 0;
319 BUG_ON(opcode != CEPH_OSD_OP_CALL);
321 osd_req_op_init(op, opcode);
323 op->cls.class_name = class;
324 size = strlen(class);
325 BUG_ON(size > (size_t) U8_MAX);
326 op->cls.class_len = size;
329 op->cls.method_name = method;
330 size = strlen(method);
331 BUG_ON(size > (size_t) U8_MAX);
332 op->cls.method_len = size;
335 op->cls.indata = request_data;
336 BUG_ON(request_data_size > (size_t) U32_MAX);
337 op->cls.indata_len = (u32) request_data_size;
338 payload_len += request_data_size;
340 op->cls.argc = 0; /* currently unused */
342 op->payload_len = payload_len;
344 EXPORT_SYMBOL(osd_req_op_cls_init);
346 void osd_req_op_watch_init(struct ceph_osd_req_op *op, u16 opcode,
347 u64 cookie, u64 version, int flag)
349 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
351 osd_req_op_init(op, opcode);
353 op->watch.cookie = cookie;
354 /* op->watch.ver = version; */ /* XXX 3847 */
355 op->watch.ver = cpu_to_le64(version);
356 if (opcode == CEPH_OSD_OP_WATCH && flag)
357 op->watch.flag = (u8) 1;
359 EXPORT_SYMBOL(osd_req_op_watch_init);
361 static u64 osd_req_encode_op(struct ceph_osd_request *req,
362 struct ceph_osd_op *dst,
363 struct ceph_osd_req_op *src)
365 u64 out_data_len = 0;
366 struct ceph_pagelist *pagelist;
368 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
369 pr_err("unrecognized osd opcode %d\n", src->op);
375 case CEPH_OSD_OP_STAT:
377 case CEPH_OSD_OP_READ:
378 case CEPH_OSD_OP_WRITE:
379 if (src->op == CEPH_OSD_OP_WRITE)
380 out_data_len = src->extent.length;
381 dst->extent.offset = cpu_to_le64(src->extent.offset);
382 dst->extent.length = cpu_to_le64(src->extent.length);
383 dst->extent.truncate_size =
384 cpu_to_le64(src->extent.truncate_size);
385 dst->extent.truncate_seq =
386 cpu_to_le32(src->extent.truncate_seq);
388 case CEPH_OSD_OP_CALL:
389 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
391 ceph_pagelist_init(pagelist);
393 dst->cls.class_len = src->cls.class_len;
394 dst->cls.method_len = src->cls.method_len;
395 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
396 ceph_pagelist_append(pagelist, src->cls.class_name,
398 ceph_pagelist_append(pagelist, src->cls.method_name,
399 src->cls.method_len);
400 ceph_pagelist_append(pagelist, src->cls.indata,
401 src->cls.indata_len);
403 req->r_data_out.type = CEPH_OSD_DATA_TYPE_PAGELIST;
404 req->r_data_out.pagelist = pagelist;
405 out_data_len = pagelist->length;
407 case CEPH_OSD_OP_STARTSYNC:
409 case CEPH_OSD_OP_NOTIFY_ACK:
410 case CEPH_OSD_OP_WATCH:
411 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
412 dst->watch.ver = cpu_to_le64(src->watch.ver);
413 dst->watch.flag = src->watch.flag;
416 pr_err("unsupported osd opcode %s\n",
417 ceph_osd_op_name(src->op));
422 dst->op = cpu_to_le16(src->op);
423 dst->payload_len = cpu_to_le32(src->payload_len);
429 * build new request AND message
432 void ceph_osdc_build_request(struct ceph_osd_request *req,
433 u64 off, unsigned int num_ops,
434 struct ceph_osd_req_op *src_ops,
435 struct ceph_snap_context *snapc, u64 snap_id,
436 struct timespec *mtime)
438 struct ceph_msg *msg = req->r_request;
439 struct ceph_osd_req_op *src_op;
442 int flags = req->r_flags;
446 req->r_num_ops = num_ops;
447 req->r_snapid = snap_id;
448 req->r_snapc = ceph_get_snap_context(snapc);
451 msg->hdr.version = cpu_to_le16(4);
453 p = msg->front.iov_base;
454 ceph_encode_32(&p, 1); /* client_inc is always 1 */
455 req->r_request_osdmap_epoch = p;
457 req->r_request_flags = p;
459 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
460 ceph_encode_timespec(p, mtime);
461 p += sizeof(struct ceph_timespec);
462 req->r_request_reassert_version = p;
463 p += sizeof(struct ceph_eversion); /* will get filled in */
466 ceph_encode_8(&p, 4);
467 ceph_encode_8(&p, 4);
468 ceph_encode_32(&p, 8 + 4 + 4);
469 req->r_request_pool = p;
471 ceph_encode_32(&p, -1); /* preferred */
472 ceph_encode_32(&p, 0); /* key len */
474 ceph_encode_8(&p, 1);
475 req->r_request_pgid = p;
477 ceph_encode_32(&p, -1); /* preferred */
480 ceph_encode_32(&p, req->r_oid_len);
481 memcpy(p, req->r_oid, req->r_oid_len);
482 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
485 /* ops--can imply data */
486 ceph_encode_16(&p, num_ops);
488 req->r_request_ops = p;
490 for (i = 0; i < num_ops; i++, src_op++) {
491 data_len += osd_req_encode_op(req, p, src_op);
492 p += sizeof(struct ceph_osd_op);
496 ceph_encode_64(&p, req->r_snapid);
497 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
498 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
500 for (i = 0; i < snapc->num_snaps; i++) {
501 ceph_encode_64(&p, req->r_snapc->snaps[i]);
505 req->r_request_attempts = p;
509 if (flags & CEPH_OSD_FLAG_WRITE) {
513 * The header "data_off" is a hint to the receiver
514 * allowing it to align received data into its
515 * buffers such that there's no need to re-copy
516 * it before writing it to disk (direct I/O).
518 data_off = (u16) (off & 0xffff);
519 req->r_request->hdr.data_off = cpu_to_le16(data_off);
521 req->r_request->hdr.data_len = cpu_to_le32(data_len);
523 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
524 msg_size = p - msg->front.iov_base;
525 msg->front.iov_len = msg_size;
526 msg->hdr.front_len = cpu_to_le32(msg_size);
528 dout("build_request msg_size was %d\n", (int)msg_size);
530 EXPORT_SYMBOL(ceph_osdc_build_request);
533 * build new request AND message, calculate layout, and adjust file
536 * if the file was recently truncated, we include information about its
537 * old and new size so that the object can be updated appropriately. (we
538 * avoid synchronously deleting truncated objects because it's slow.)
540 * if @do_sync, include a 'startsync' command so that the osd will flush
543 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
544 struct ceph_file_layout *layout,
545 struct ceph_vino vino,
546 u64 off, u64 *plen, int num_ops,
547 struct ceph_osd_req_op *ops,
548 int opcode, int flags,
549 struct ceph_snap_context *snapc,
554 struct ceph_osd_request *req;
562 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
564 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
567 return ERR_PTR(-ENOMEM);
568 req->r_flags = flags;
570 /* calculate max write size */
571 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
573 ceph_osdc_put_request(req);
577 object_size = le32_to_cpu(layout->fl_object_size);
578 object_base = off - objoff;
579 if (truncate_size <= object_base) {
582 truncate_size -= object_base;
583 if (truncate_size > object_size)
584 truncate_size = object_size;
587 osd_req_op_extent_init(&ops[0], opcode, objoff, objlen,
588 truncate_size, truncate_seq);
590 * A second op in the ops array means the caller wants to
591 * also issue a include a 'startsync' command so that the
592 * osd will flush data quickly.
595 osd_req_op_init(&ops[1], CEPH_OSD_OP_STARTSYNC);
597 req->r_file_layout = *layout; /* keep a copy */
599 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
601 req->r_oid_len = strlen(req->r_oid);
605 EXPORT_SYMBOL(ceph_osdc_new_request);
608 * We keep osd requests in an rbtree, sorted by ->r_tid.
610 static void __insert_request(struct ceph_osd_client *osdc,
611 struct ceph_osd_request *new)
613 struct rb_node **p = &osdc->requests.rb_node;
614 struct rb_node *parent = NULL;
615 struct ceph_osd_request *req = NULL;
619 req = rb_entry(parent, struct ceph_osd_request, r_node);
620 if (new->r_tid < req->r_tid)
622 else if (new->r_tid > req->r_tid)
628 rb_link_node(&new->r_node, parent, p);
629 rb_insert_color(&new->r_node, &osdc->requests);
632 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
635 struct ceph_osd_request *req;
636 struct rb_node *n = osdc->requests.rb_node;
639 req = rb_entry(n, struct ceph_osd_request, r_node);
640 if (tid < req->r_tid)
642 else if (tid > req->r_tid)
650 static struct ceph_osd_request *
651 __lookup_request_ge(struct ceph_osd_client *osdc,
654 struct ceph_osd_request *req;
655 struct rb_node *n = osdc->requests.rb_node;
658 req = rb_entry(n, struct ceph_osd_request, r_node);
659 if (tid < req->r_tid) {
663 } else if (tid > req->r_tid) {
673 * Resubmit requests pending on the given osd.
675 static void __kick_osd_requests(struct ceph_osd_client *osdc,
676 struct ceph_osd *osd)
678 struct ceph_osd_request *req, *nreq;
682 dout("__kick_osd_requests osd%d\n", osd->o_osd);
683 err = __reset_osd(osdc, osd);
687 * Build up a list of requests to resend by traversing the
688 * osd's list of requests. Requests for a given object are
689 * sent in tid order, and that is also the order they're
690 * kept on this list. Therefore all requests that are in
691 * flight will be found first, followed by all requests that
692 * have not yet been sent. And to resend requests while
693 * preserving this order we will want to put any sent
694 * requests back on the front of the osd client's unsent
697 * So we build a separate ordered list of already-sent
698 * requests for the affected osd and splice it onto the
699 * front of the osd client's unsent list. Once we've seen a
700 * request that has not yet been sent we're done. Those
701 * requests are already sitting right where they belong.
703 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
706 list_move_tail(&req->r_req_lru_item, &resend);
707 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
710 req->r_flags |= CEPH_OSD_FLAG_RETRY;
712 list_splice(&resend, &osdc->req_unsent);
715 * Linger requests are re-registered before sending, which
716 * sets up a new tid for each. We add them to the unsent
717 * list at the end to keep things in tid order.
719 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
722 * reregister request prior to unregistering linger so
723 * that r_osd is preserved.
725 BUG_ON(!list_empty(&req->r_req_lru_item));
726 __register_request(osdc, req);
727 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
728 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
729 __unregister_linger_request(osdc, req);
730 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
736 * If the osd connection drops, we need to resubmit all requests.
738 static void osd_reset(struct ceph_connection *con)
740 struct ceph_osd *osd = con->private;
741 struct ceph_osd_client *osdc;
745 dout("osd_reset osd%d\n", osd->o_osd);
747 down_read(&osdc->map_sem);
748 mutex_lock(&osdc->request_mutex);
749 __kick_osd_requests(osdc, osd);
751 mutex_unlock(&osdc->request_mutex);
752 up_read(&osdc->map_sem);
756 * Track open sessions with osds.
758 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
760 struct ceph_osd *osd;
762 osd = kzalloc(sizeof(*osd), GFP_NOFS);
766 atomic_set(&osd->o_ref, 1);
769 RB_CLEAR_NODE(&osd->o_node);
770 INIT_LIST_HEAD(&osd->o_requests);
771 INIT_LIST_HEAD(&osd->o_linger_requests);
772 INIT_LIST_HEAD(&osd->o_osd_lru);
773 osd->o_incarnation = 1;
775 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
777 INIT_LIST_HEAD(&osd->o_keepalive_item);
781 static struct ceph_osd *get_osd(struct ceph_osd *osd)
783 if (atomic_inc_not_zero(&osd->o_ref)) {
784 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
785 atomic_read(&osd->o_ref));
788 dout("get_osd %p FAIL\n", osd);
793 static void put_osd(struct ceph_osd *osd)
795 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
796 atomic_read(&osd->o_ref) - 1);
797 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
798 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
800 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
806 * remove an osd from our map
808 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
810 dout("__remove_osd %p\n", osd);
811 BUG_ON(!list_empty(&osd->o_requests));
812 rb_erase(&osd->o_node, &osdc->osds);
813 list_del_init(&osd->o_osd_lru);
814 ceph_con_close(&osd->o_con);
818 static void remove_all_osds(struct ceph_osd_client *osdc)
820 dout("%s %p\n", __func__, osdc);
821 mutex_lock(&osdc->request_mutex);
822 while (!RB_EMPTY_ROOT(&osdc->osds)) {
823 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
824 struct ceph_osd, o_node);
825 __remove_osd(osdc, osd);
827 mutex_unlock(&osdc->request_mutex);
830 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
831 struct ceph_osd *osd)
833 dout("__move_osd_to_lru %p\n", osd);
834 BUG_ON(!list_empty(&osd->o_osd_lru));
835 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
836 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
839 static void __remove_osd_from_lru(struct ceph_osd *osd)
841 dout("__remove_osd_from_lru %p\n", osd);
842 if (!list_empty(&osd->o_osd_lru))
843 list_del_init(&osd->o_osd_lru);
846 static void remove_old_osds(struct ceph_osd_client *osdc)
848 struct ceph_osd *osd, *nosd;
850 dout("__remove_old_osds %p\n", osdc);
851 mutex_lock(&osdc->request_mutex);
852 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
853 if (time_before(jiffies, osd->lru_ttl))
855 __remove_osd(osdc, osd);
857 mutex_unlock(&osdc->request_mutex);
863 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
865 struct ceph_entity_addr *peer_addr;
867 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
868 if (list_empty(&osd->o_requests) &&
869 list_empty(&osd->o_linger_requests)) {
870 __remove_osd(osdc, osd);
875 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
876 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
877 !ceph_con_opened(&osd->o_con)) {
878 struct ceph_osd_request *req;
880 dout(" osd addr hasn't changed and connection never opened,"
881 " letting msgr retry");
882 /* touch each r_stamp for handle_timeout()'s benfit */
883 list_for_each_entry(req, &osd->o_requests, r_osd_item)
884 req->r_stamp = jiffies;
889 ceph_con_close(&osd->o_con);
890 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
891 osd->o_incarnation++;
896 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
898 struct rb_node **p = &osdc->osds.rb_node;
899 struct rb_node *parent = NULL;
900 struct ceph_osd *osd = NULL;
902 dout("__insert_osd %p osd%d\n", new, new->o_osd);
905 osd = rb_entry(parent, struct ceph_osd, o_node);
906 if (new->o_osd < osd->o_osd)
908 else if (new->o_osd > osd->o_osd)
914 rb_link_node(&new->o_node, parent, p);
915 rb_insert_color(&new->o_node, &osdc->osds);
918 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
920 struct ceph_osd *osd;
921 struct rb_node *n = osdc->osds.rb_node;
924 osd = rb_entry(n, struct ceph_osd, o_node);
927 else if (o > osd->o_osd)
935 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
937 schedule_delayed_work(&osdc->timeout_work,
938 osdc->client->options->osd_keepalive_timeout * HZ);
941 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
943 cancel_delayed_work(&osdc->timeout_work);
947 * Register request, assign tid. If this is the first request, set up
950 static void __register_request(struct ceph_osd_client *osdc,
951 struct ceph_osd_request *req)
953 req->r_tid = ++osdc->last_tid;
954 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
955 dout("__register_request %p tid %lld\n", req, req->r_tid);
956 __insert_request(osdc, req);
957 ceph_osdc_get_request(req);
958 osdc->num_requests++;
959 if (osdc->num_requests == 1) {
960 dout(" first request, scheduling timeout\n");
961 __schedule_osd_timeout(osdc);
966 * called under osdc->request_mutex
968 static void __unregister_request(struct ceph_osd_client *osdc,
969 struct ceph_osd_request *req)
971 if (RB_EMPTY_NODE(&req->r_node)) {
972 dout("__unregister_request %p tid %lld not registered\n",
977 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
978 rb_erase(&req->r_node, &osdc->requests);
979 osdc->num_requests--;
982 /* make sure the original request isn't in flight. */
983 ceph_msg_revoke(req->r_request);
985 list_del_init(&req->r_osd_item);
986 if (list_empty(&req->r_osd->o_requests) &&
987 list_empty(&req->r_osd->o_linger_requests)) {
988 dout("moving osd to %p lru\n", req->r_osd);
989 __move_osd_to_lru(osdc, req->r_osd);
991 if (list_empty(&req->r_linger_item))
995 list_del_init(&req->r_req_lru_item);
996 ceph_osdc_put_request(req);
998 if (osdc->num_requests == 0) {
999 dout(" no requests, canceling timeout\n");
1000 __cancel_osd_timeout(osdc);
1005 * Cancel a previously queued request message
1007 static void __cancel_request(struct ceph_osd_request *req)
1009 if (req->r_sent && req->r_osd) {
1010 ceph_msg_revoke(req->r_request);
1015 static void __register_linger_request(struct ceph_osd_client *osdc,
1016 struct ceph_osd_request *req)
1018 dout("__register_linger_request %p\n", req);
1019 list_add_tail(&req->r_linger_item, &osdc->req_linger);
1021 list_add_tail(&req->r_linger_osd,
1022 &req->r_osd->o_linger_requests);
1025 static void __unregister_linger_request(struct ceph_osd_client *osdc,
1026 struct ceph_osd_request *req)
1028 dout("__unregister_linger_request %p\n", req);
1029 list_del_init(&req->r_linger_item);
1031 list_del_init(&req->r_linger_osd);
1033 if (list_empty(&req->r_osd->o_requests) &&
1034 list_empty(&req->r_osd->o_linger_requests)) {
1035 dout("moving osd to %p lru\n", req->r_osd);
1036 __move_osd_to_lru(osdc, req->r_osd);
1038 if (list_empty(&req->r_osd_item))
1043 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1044 struct ceph_osd_request *req)
1046 mutex_lock(&osdc->request_mutex);
1047 if (req->r_linger) {
1048 __unregister_linger_request(osdc, req);
1049 ceph_osdc_put_request(req);
1051 mutex_unlock(&osdc->request_mutex);
1053 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1055 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1056 struct ceph_osd_request *req)
1058 if (!req->r_linger) {
1059 dout("set_request_linger %p\n", req);
1062 * caller is now responsible for calling
1063 * unregister_linger_request
1065 ceph_osdc_get_request(req);
1068 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1071 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1072 * (as needed), and set the request r_osd appropriately. If there is
1073 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1074 * (unsent, homeless) or leave on in-flight lru.
1076 * Return 0 if unchanged, 1 if changed, or negative on error.
1078 * Caller should hold map_sem for read and request_mutex.
1080 static int __map_request(struct ceph_osd_client *osdc,
1081 struct ceph_osd_request *req, int force_resend)
1083 struct ceph_pg pgid;
1084 int acting[CEPH_PG_MAX_SIZE];
1085 int o = -1, num = 0;
1088 dout("map_request %p tid %lld\n", req, req->r_tid);
1089 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1090 ceph_file_layout_pg_pool(req->r_file_layout));
1092 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1097 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1103 if ((!force_resend &&
1104 req->r_osd && req->r_osd->o_osd == o &&
1105 req->r_sent >= req->r_osd->o_incarnation &&
1106 req->r_num_pg_osds == num &&
1107 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1108 (req->r_osd == NULL && o == -1))
1109 return 0; /* no change */
1111 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1112 req->r_tid, pgid.pool, pgid.seed, o,
1113 req->r_osd ? req->r_osd->o_osd : -1);
1115 /* record full pg acting set */
1116 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1117 req->r_num_pg_osds = num;
1120 __cancel_request(req);
1121 list_del_init(&req->r_osd_item);
1125 req->r_osd = __lookup_osd(osdc, o);
1126 if (!req->r_osd && o >= 0) {
1128 req->r_osd = create_osd(osdc, o);
1130 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1134 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1135 __insert_osd(osdc, req->r_osd);
1137 ceph_con_open(&req->r_osd->o_con,
1138 CEPH_ENTITY_TYPE_OSD, o,
1139 &osdc->osdmap->osd_addr[o]);
1143 __remove_osd_from_lru(req->r_osd);
1144 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1145 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1147 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1149 err = 1; /* osd or pg changed */
1156 * caller should hold map_sem (for read) and request_mutex
1158 static void __send_request(struct ceph_osd_client *osdc,
1159 struct ceph_osd_request *req)
1163 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1164 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1165 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1167 /* fill in message content that changes each time we send it */
1168 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1169 put_unaligned_le32(req->r_flags, req->r_request_flags);
1170 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1171 p = req->r_request_pgid;
1172 ceph_encode_64(&p, req->r_pgid.pool);
1173 ceph_encode_32(&p, req->r_pgid.seed);
1174 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1175 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1176 sizeof(req->r_reassert_version));
1178 req->r_stamp = jiffies;
1179 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1181 ceph_msg_get(req->r_request); /* send consumes a ref */
1182 ceph_con_send(&req->r_osd->o_con, req->r_request);
1183 req->r_sent = req->r_osd->o_incarnation;
1187 * Send any requests in the queue (req_unsent).
1189 static void __send_queued(struct ceph_osd_client *osdc)
1191 struct ceph_osd_request *req, *tmp;
1193 dout("__send_queued\n");
1194 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
1195 __send_request(osdc, req);
1199 * Timeout callback, called every N seconds when 1 or more osd
1200 * requests has been active for more than N seconds. When this
1201 * happens, we ping all OSDs with requests who have timed out to
1202 * ensure any communications channel reset is detected. Reset the
1203 * request timeouts another N seconds in the future as we go.
1204 * Reschedule the timeout event another N seconds in future (unless
1205 * there are no open requests).
1207 static void handle_timeout(struct work_struct *work)
1209 struct ceph_osd_client *osdc =
1210 container_of(work, struct ceph_osd_client, timeout_work.work);
1211 struct ceph_osd_request *req;
1212 struct ceph_osd *osd;
1213 unsigned long keepalive =
1214 osdc->client->options->osd_keepalive_timeout * HZ;
1215 struct list_head slow_osds;
1217 down_read(&osdc->map_sem);
1219 ceph_monc_request_next_osdmap(&osdc->client->monc);
1221 mutex_lock(&osdc->request_mutex);
1224 * ping osds that are a bit slow. this ensures that if there
1225 * is a break in the TCP connection we will notice, and reopen
1226 * a connection with that osd (from the fault callback).
1228 INIT_LIST_HEAD(&slow_osds);
1229 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1230 if (time_before(jiffies, req->r_stamp + keepalive))
1235 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1236 req->r_tid, osd->o_osd);
1237 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1239 while (!list_empty(&slow_osds)) {
1240 osd = list_entry(slow_osds.next, struct ceph_osd,
1242 list_del_init(&osd->o_keepalive_item);
1243 ceph_con_keepalive(&osd->o_con);
1246 __schedule_osd_timeout(osdc);
1247 __send_queued(osdc);
1248 mutex_unlock(&osdc->request_mutex);
1249 up_read(&osdc->map_sem);
1252 static void handle_osds_timeout(struct work_struct *work)
1254 struct ceph_osd_client *osdc =
1255 container_of(work, struct ceph_osd_client,
1256 osds_timeout_work.work);
1257 unsigned long delay =
1258 osdc->client->options->osd_idle_ttl * HZ >> 2;
1260 dout("osds timeout\n");
1261 down_read(&osdc->map_sem);
1262 remove_old_osds(osdc);
1263 up_read(&osdc->map_sem);
1265 schedule_delayed_work(&osdc->osds_timeout_work,
1266 round_jiffies_relative(delay));
1269 static void complete_request(struct ceph_osd_request *req)
1271 if (req->r_safe_callback)
1272 req->r_safe_callback(req, NULL);
1273 complete_all(&req->r_safe_completion); /* fsync waiter */
1277 * handle osd op reply. either call the callback if it is specified,
1278 * or do the completion to wake up the waiting thread.
1280 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1281 struct ceph_connection *con)
1284 struct ceph_osd_request *req;
1287 int numops, payload_len, flags;
1293 u64 reassert_version;
1295 int already_completed;
1298 tid = le64_to_cpu(msg->hdr.tid);
1299 dout("handle_reply %p tid %llu\n", msg, tid);
1301 p = msg->front.iov_base;
1302 end = p + msg->front.iov_len;
1304 ceph_decode_need(&p, end, 4, bad);
1305 object_len = ceph_decode_32(&p);
1306 ceph_decode_need(&p, end, object_len, bad);
1309 err = ceph_decode_pgid(&p, end, &pg);
1313 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1314 flags = ceph_decode_64(&p);
1315 result = ceph_decode_32(&p);
1316 reassert_epoch = ceph_decode_32(&p);
1317 reassert_version = ceph_decode_64(&p);
1318 osdmap_epoch = ceph_decode_32(&p);
1321 mutex_lock(&osdc->request_mutex);
1322 req = __lookup_request(osdc, tid);
1324 dout("handle_reply tid %llu dne\n", tid);
1327 ceph_osdc_get_request(req);
1329 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1332 ceph_decode_need(&p, end, 4, bad);
1333 numops = ceph_decode_32(&p);
1334 if (numops > CEPH_OSD_MAX_OP)
1336 if (numops != req->r_num_ops)
1339 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1340 for (i = 0; i < numops; i++) {
1341 struct ceph_osd_op *op = p;
1344 len = le32_to_cpu(op->payload_len);
1345 req->r_reply_op_len[i] = len;
1346 dout(" op %d has %d bytes\n", i, len);
1350 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1351 pr_warning("sum of op payload lens %d != data_len %d",
1352 payload_len, le32_to_cpu(msg->hdr.data_len));
1356 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1357 retry_attempt = ceph_decode_32(&p);
1358 for (i = 0; i < numops; i++)
1359 req->r_reply_op_result[i] = ceph_decode_32(&p);
1361 if (!req->r_got_reply) {
1364 req->r_result = result;
1365 bytes = le32_to_cpu(msg->hdr.data_len);
1366 dout("handle_reply result %d bytes %d\n", req->r_result,
1368 if (req->r_result == 0)
1369 req->r_result = bytes;
1371 /* in case this is a write and we need to replay, */
1372 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1373 req->r_reassert_version.version = cpu_to_le64(reassert_version);
1375 req->r_got_reply = 1;
1376 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1377 dout("handle_reply tid %llu dup ack\n", tid);
1378 mutex_unlock(&osdc->request_mutex);
1382 dout("handle_reply tid %llu flags %d\n", tid, flags);
1384 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1385 __register_linger_request(osdc, req);
1387 /* either this is a read, or we got the safe response */
1389 (flags & CEPH_OSD_FLAG_ONDISK) ||
1390 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1391 __unregister_request(osdc, req);
1393 already_completed = req->r_completed;
1394 req->r_completed = 1;
1395 mutex_unlock(&osdc->request_mutex);
1396 if (already_completed)
1399 if (req->r_callback)
1400 req->r_callback(req, msg);
1402 complete_all(&req->r_completion);
1404 if (flags & CEPH_OSD_FLAG_ONDISK)
1405 complete_request(req);
1408 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1409 ceph_osdc_put_request(req);
1413 ceph_osdc_put_request(req);
1415 mutex_unlock(&osdc->request_mutex);
1417 pr_err("corrupt osd_op_reply got %d %d\n",
1418 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
1422 static void reset_changed_osds(struct ceph_osd_client *osdc)
1424 struct rb_node *p, *n;
1426 for (p = rb_first(&osdc->osds); p; p = n) {
1427 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1430 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1431 memcmp(&osd->o_con.peer_addr,
1432 ceph_osd_addr(osdc->osdmap,
1434 sizeof(struct ceph_entity_addr)) != 0)
1435 __reset_osd(osdc, osd);
1440 * Requeue requests whose mapping to an OSD has changed. If requests map to
1441 * no osd, request a new map.
1443 * Caller should hold map_sem for read.
1445 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1447 struct ceph_osd_request *req, *nreq;
1452 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1453 mutex_lock(&osdc->request_mutex);
1454 for (p = rb_first(&osdc->requests); p; ) {
1455 req = rb_entry(p, struct ceph_osd_request, r_node);
1459 * For linger requests that have not yet been
1460 * registered, move them to the linger list; they'll
1461 * be sent to the osd in the loop below. Unregister
1462 * the request before re-registering it as a linger
1463 * request to ensure the __map_request() below
1464 * will decide it needs to be sent.
1466 if (req->r_linger && list_empty(&req->r_linger_item)) {
1467 dout("%p tid %llu restart on osd%d\n",
1469 req->r_osd ? req->r_osd->o_osd : -1);
1470 __unregister_request(osdc, req);
1471 __register_linger_request(osdc, req);
1475 err = __map_request(osdc, req, force_resend);
1477 continue; /* error */
1478 if (req->r_osd == NULL) {
1479 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1480 needmap++; /* request a newer map */
1481 } else if (err > 0) {
1482 if (!req->r_linger) {
1483 dout("%p tid %llu requeued on osd%d\n", req,
1485 req->r_osd ? req->r_osd->o_osd : -1);
1486 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1491 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1493 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1495 err = __map_request(osdc, req, force_resend);
1496 dout("__map_request returned %d\n", err);
1498 continue; /* no change and no osd was specified */
1500 continue; /* hrm! */
1501 if (req->r_osd == NULL) {
1502 dout("tid %llu maps to no valid osd\n", req->r_tid);
1503 needmap++; /* request a newer map */
1507 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1508 req->r_osd ? req->r_osd->o_osd : -1);
1509 __register_request(osdc, req);
1510 __unregister_linger_request(osdc, req);
1512 mutex_unlock(&osdc->request_mutex);
1515 dout("%d requests for down osds, need new map\n", needmap);
1516 ceph_monc_request_next_osdmap(&osdc->client->monc);
1518 reset_changed_osds(osdc);
1523 * Process updated osd map.
1525 * The message contains any number of incremental and full maps, normally
1526 * indicating some sort of topology change in the cluster. Kick requests
1527 * off to different OSDs as needed.
1529 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1531 void *p, *end, *next;
1532 u32 nr_maps, maplen;
1534 struct ceph_osdmap *newmap = NULL, *oldmap;
1536 struct ceph_fsid fsid;
1538 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1539 p = msg->front.iov_base;
1540 end = p + msg->front.iov_len;
1543 ceph_decode_need(&p, end, sizeof(fsid), bad);
1544 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1545 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1548 down_write(&osdc->map_sem);
1550 /* incremental maps */
1551 ceph_decode_32_safe(&p, end, nr_maps, bad);
1552 dout(" %d inc maps\n", nr_maps);
1553 while (nr_maps > 0) {
1554 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1555 epoch = ceph_decode_32(&p);
1556 maplen = ceph_decode_32(&p);
1557 ceph_decode_need(&p, end, maplen, bad);
1559 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1560 dout("applying incremental map %u len %d\n",
1562 newmap = osdmap_apply_incremental(&p, next,
1564 &osdc->client->msgr);
1565 if (IS_ERR(newmap)) {
1566 err = PTR_ERR(newmap);
1570 if (newmap != osdc->osdmap) {
1571 ceph_osdmap_destroy(osdc->osdmap);
1572 osdc->osdmap = newmap;
1574 kick_requests(osdc, 0);
1576 dout("ignoring incremental map %u len %d\n",
1586 ceph_decode_32_safe(&p, end, nr_maps, bad);
1587 dout(" %d full maps\n", nr_maps);
1589 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1590 epoch = ceph_decode_32(&p);
1591 maplen = ceph_decode_32(&p);
1592 ceph_decode_need(&p, end, maplen, bad);
1594 dout("skipping non-latest full map %u len %d\n",
1596 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1597 dout("skipping full map %u len %d, "
1598 "older than our %u\n", epoch, maplen,
1599 osdc->osdmap->epoch);
1601 int skipped_map = 0;
1603 dout("taking full map %u len %d\n", epoch, maplen);
1604 newmap = osdmap_decode(&p, p+maplen);
1605 if (IS_ERR(newmap)) {
1606 err = PTR_ERR(newmap);
1610 oldmap = osdc->osdmap;
1611 osdc->osdmap = newmap;
1613 if (oldmap->epoch + 1 < newmap->epoch)
1615 ceph_osdmap_destroy(oldmap);
1617 kick_requests(osdc, skipped_map);
1624 downgrade_write(&osdc->map_sem);
1625 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1628 * subscribe to subsequent osdmap updates if full to ensure
1629 * we find out when we are no longer full and stop returning
1632 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1633 ceph_monc_request_next_osdmap(&osdc->client->monc);
1635 mutex_lock(&osdc->request_mutex);
1636 __send_queued(osdc);
1637 mutex_unlock(&osdc->request_mutex);
1638 up_read(&osdc->map_sem);
1639 wake_up_all(&osdc->client->auth_wq);
1643 pr_err("osdc handle_map corrupt msg\n");
1645 up_write(&osdc->map_sem);
1650 * watch/notify callback event infrastructure
1652 * These callbacks are used both for watch and notify operations.
1654 static void __release_event(struct kref *kref)
1656 struct ceph_osd_event *event =
1657 container_of(kref, struct ceph_osd_event, kref);
1659 dout("__release_event %p\n", event);
1663 static void get_event(struct ceph_osd_event *event)
1665 kref_get(&event->kref);
1668 void ceph_osdc_put_event(struct ceph_osd_event *event)
1670 kref_put(&event->kref, __release_event);
1672 EXPORT_SYMBOL(ceph_osdc_put_event);
1674 static void __insert_event(struct ceph_osd_client *osdc,
1675 struct ceph_osd_event *new)
1677 struct rb_node **p = &osdc->event_tree.rb_node;
1678 struct rb_node *parent = NULL;
1679 struct ceph_osd_event *event = NULL;
1683 event = rb_entry(parent, struct ceph_osd_event, node);
1684 if (new->cookie < event->cookie)
1686 else if (new->cookie > event->cookie)
1687 p = &(*p)->rb_right;
1692 rb_link_node(&new->node, parent, p);
1693 rb_insert_color(&new->node, &osdc->event_tree);
1696 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1699 struct rb_node **p = &osdc->event_tree.rb_node;
1700 struct rb_node *parent = NULL;
1701 struct ceph_osd_event *event = NULL;
1705 event = rb_entry(parent, struct ceph_osd_event, node);
1706 if (cookie < event->cookie)
1708 else if (cookie > event->cookie)
1709 p = &(*p)->rb_right;
1716 static void __remove_event(struct ceph_osd_event *event)
1718 struct ceph_osd_client *osdc = event->osdc;
1720 if (!RB_EMPTY_NODE(&event->node)) {
1721 dout("__remove_event removed %p\n", event);
1722 rb_erase(&event->node, &osdc->event_tree);
1723 ceph_osdc_put_event(event);
1725 dout("__remove_event didn't remove %p\n", event);
1729 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1730 void (*event_cb)(u64, u64, u8, void *),
1731 void *data, struct ceph_osd_event **pevent)
1733 struct ceph_osd_event *event;
1735 event = kmalloc(sizeof(*event), GFP_NOIO);
1739 dout("create_event %p\n", event);
1740 event->cb = event_cb;
1741 event->one_shot = 0;
1744 INIT_LIST_HEAD(&event->osd_node);
1745 RB_CLEAR_NODE(&event->node);
1746 kref_init(&event->kref); /* one ref for us */
1747 kref_get(&event->kref); /* one ref for the caller */
1749 spin_lock(&osdc->event_lock);
1750 event->cookie = ++osdc->event_count;
1751 __insert_event(osdc, event);
1752 spin_unlock(&osdc->event_lock);
1757 EXPORT_SYMBOL(ceph_osdc_create_event);
1759 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1761 struct ceph_osd_client *osdc = event->osdc;
1763 dout("cancel_event %p\n", event);
1764 spin_lock(&osdc->event_lock);
1765 __remove_event(event);
1766 spin_unlock(&osdc->event_lock);
1767 ceph_osdc_put_event(event); /* caller's */
1769 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1772 static void do_event_work(struct work_struct *work)
1774 struct ceph_osd_event_work *event_work =
1775 container_of(work, struct ceph_osd_event_work, work);
1776 struct ceph_osd_event *event = event_work->event;
1777 u64 ver = event_work->ver;
1778 u64 notify_id = event_work->notify_id;
1779 u8 opcode = event_work->opcode;
1781 dout("do_event_work completing %p\n", event);
1782 event->cb(ver, notify_id, opcode, event->data);
1783 dout("do_event_work completed %p\n", event);
1784 ceph_osdc_put_event(event);
1790 * Process osd watch notifications
1792 static void handle_watch_notify(struct ceph_osd_client *osdc,
1793 struct ceph_msg *msg)
1797 u64 cookie, ver, notify_id;
1799 struct ceph_osd_event *event;
1800 struct ceph_osd_event_work *event_work;
1802 p = msg->front.iov_base;
1803 end = p + msg->front.iov_len;
1805 ceph_decode_8_safe(&p, end, proto_ver, bad);
1806 ceph_decode_8_safe(&p, end, opcode, bad);
1807 ceph_decode_64_safe(&p, end, cookie, bad);
1808 ceph_decode_64_safe(&p, end, ver, bad);
1809 ceph_decode_64_safe(&p, end, notify_id, bad);
1811 spin_lock(&osdc->event_lock);
1812 event = __find_event(osdc, cookie);
1814 BUG_ON(event->one_shot);
1817 spin_unlock(&osdc->event_lock);
1818 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1819 cookie, ver, event);
1821 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1823 dout("ERROR: could not allocate event_work\n");
1826 INIT_WORK(&event_work->work, do_event_work);
1827 event_work->event = event;
1828 event_work->ver = ver;
1829 event_work->notify_id = notify_id;
1830 event_work->opcode = opcode;
1831 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1832 dout("WARNING: failed to queue notify event work\n");
1840 ceph_osdc_put_event(event);
1844 pr_err("osdc handle_watch_notify corrupt msg\n");
1848 static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1849 struct ceph_osd_data *osd_data)
1851 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1852 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1853 if (osd_data->length)
1854 ceph_msg_data_set_pages(msg, osd_data->pages,
1855 osd_data->length, osd_data->alignment);
1856 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1857 BUG_ON(!osd_data->pagelist->length);
1858 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
1860 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1861 ceph_msg_data_set_bio(msg, osd_data->bio, osd_data->bio_length);
1864 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1869 * Register request, send initial attempt.
1871 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1872 struct ceph_osd_request *req,
1877 /* Set up response incoming data and request outgoing data fields */
1879 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1880 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
1882 down_read(&osdc->map_sem);
1883 mutex_lock(&osdc->request_mutex);
1884 __register_request(osdc, req);
1885 WARN_ON(req->r_sent);
1886 rc = __map_request(osdc, req, 0);
1889 dout("osdc_start_request failed map, "
1890 " will retry %lld\n", req->r_tid);
1895 if (req->r_osd == NULL) {
1896 dout("send_request %p no up osds in pg\n", req);
1897 ceph_monc_request_next_osdmap(&osdc->client->monc);
1899 __send_queued(osdc);
1903 mutex_unlock(&osdc->request_mutex);
1904 up_read(&osdc->map_sem);
1907 EXPORT_SYMBOL(ceph_osdc_start_request);
1910 * wait for a request to complete
1912 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1913 struct ceph_osd_request *req)
1917 rc = wait_for_completion_interruptible(&req->r_completion);
1919 mutex_lock(&osdc->request_mutex);
1920 __cancel_request(req);
1921 __unregister_request(osdc, req);
1922 mutex_unlock(&osdc->request_mutex);
1923 complete_request(req);
1924 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1928 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1929 return req->r_result;
1931 EXPORT_SYMBOL(ceph_osdc_wait_request);
1934 * sync - wait for all in-flight requests to flush. avoid starvation.
1936 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1938 struct ceph_osd_request *req;
1939 u64 last_tid, next_tid = 0;
1941 mutex_lock(&osdc->request_mutex);
1942 last_tid = osdc->last_tid;
1944 req = __lookup_request_ge(osdc, next_tid);
1947 if (req->r_tid > last_tid)
1950 next_tid = req->r_tid + 1;
1951 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1954 ceph_osdc_get_request(req);
1955 mutex_unlock(&osdc->request_mutex);
1956 dout("sync waiting on tid %llu (last is %llu)\n",
1957 req->r_tid, last_tid);
1958 wait_for_completion(&req->r_safe_completion);
1959 mutex_lock(&osdc->request_mutex);
1960 ceph_osdc_put_request(req);
1962 mutex_unlock(&osdc->request_mutex);
1963 dout("sync done (thru tid %llu)\n", last_tid);
1965 EXPORT_SYMBOL(ceph_osdc_sync);
1970 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1975 osdc->client = client;
1976 osdc->osdmap = NULL;
1977 init_rwsem(&osdc->map_sem);
1978 init_completion(&osdc->map_waiters);
1979 osdc->last_requested_map = 0;
1980 mutex_init(&osdc->request_mutex);
1982 osdc->osds = RB_ROOT;
1983 INIT_LIST_HEAD(&osdc->osd_lru);
1984 osdc->requests = RB_ROOT;
1985 INIT_LIST_HEAD(&osdc->req_lru);
1986 INIT_LIST_HEAD(&osdc->req_unsent);
1987 INIT_LIST_HEAD(&osdc->req_notarget);
1988 INIT_LIST_HEAD(&osdc->req_linger);
1989 osdc->num_requests = 0;
1990 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1991 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1992 spin_lock_init(&osdc->event_lock);
1993 osdc->event_tree = RB_ROOT;
1994 osdc->event_count = 0;
1996 schedule_delayed_work(&osdc->osds_timeout_work,
1997 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
2000 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2001 sizeof(struct ceph_osd_request));
2002 if (!osdc->req_mempool)
2005 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2006 OSD_OP_FRONT_LEN, 10, true,
2010 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
2011 OSD_OPREPLY_FRONT_LEN, 10, true,
2016 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2017 if (IS_ERR(osdc->notify_wq)) {
2018 err = PTR_ERR(osdc->notify_wq);
2019 osdc->notify_wq = NULL;
2025 ceph_msgpool_destroy(&osdc->msgpool_op);
2027 mempool_destroy(osdc->req_mempool);
2032 void ceph_osdc_stop(struct ceph_osd_client *osdc)
2034 flush_workqueue(osdc->notify_wq);
2035 destroy_workqueue(osdc->notify_wq);
2036 cancel_delayed_work_sync(&osdc->timeout_work);
2037 cancel_delayed_work_sync(&osdc->osds_timeout_work);
2039 ceph_osdmap_destroy(osdc->osdmap);
2040 osdc->osdmap = NULL;
2042 remove_all_osds(osdc);
2043 mempool_destroy(osdc->req_mempool);
2044 ceph_msgpool_destroy(&osdc->msgpool_op);
2045 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
2049 * Read some contiguous pages. If we cross a stripe boundary, shorten
2050 * *plen. Return number of bytes read, or error.
2052 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2053 struct ceph_vino vino, struct ceph_file_layout *layout,
2055 u32 truncate_seq, u64 truncate_size,
2056 struct page **pages, int num_pages, int page_align)
2058 struct ceph_osd_request *req;
2059 struct ceph_osd_data *osd_data;
2060 struct ceph_osd_req_op op;
2063 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2064 vino.snap, off, *plen);
2065 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1, &op,
2066 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2067 NULL, truncate_seq, truncate_size,
2070 return PTR_ERR(req);
2072 /* it may be a short read due to an object boundary */
2074 osd_data = &req->r_data_in;
2075 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2076 osd_data->pages = pages;
2077 osd_data->length = *plen;
2078 osd_data->alignment = page_align;
2080 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2081 off, *plen, osd_data->length, page_align);
2083 ceph_osdc_build_request(req, off, 1, &op, NULL, vino.snap, NULL);
2085 rc = ceph_osdc_start_request(osdc, req, false);
2087 rc = ceph_osdc_wait_request(osdc, req);
2089 ceph_osdc_put_request(req);
2090 dout("readpages result %d\n", rc);
2093 EXPORT_SYMBOL(ceph_osdc_readpages);
2096 * do a synchronous write on N pages
2098 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2099 struct ceph_file_layout *layout,
2100 struct ceph_snap_context *snapc,
2102 u32 truncate_seq, u64 truncate_size,
2103 struct timespec *mtime,
2104 struct page **pages, int num_pages)
2106 struct ceph_osd_request *req;
2107 struct ceph_osd_data *osd_data;
2108 struct ceph_osd_req_op op;
2110 int page_align = off & ~PAGE_MASK;
2112 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2113 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1, &op,
2115 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2116 snapc, truncate_seq, truncate_size,
2119 return PTR_ERR(req);
2121 /* it may be a short write due to an object boundary */
2122 osd_data = &req->r_data_out;
2123 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2124 osd_data->pages = pages;
2125 osd_data->length = len;
2126 osd_data->alignment = page_align;
2127 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
2129 ceph_osdc_build_request(req, off, 1, &op, snapc, CEPH_NOSNAP, mtime);
2131 rc = ceph_osdc_start_request(osdc, req, true);
2133 rc = ceph_osdc_wait_request(osdc, req);
2135 ceph_osdc_put_request(req);
2138 dout("writepages result %d\n", rc);
2141 EXPORT_SYMBOL(ceph_osdc_writepages);
2144 * handle incoming message
2146 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2148 struct ceph_osd *osd = con->private;
2149 struct ceph_osd_client *osdc;
2150 int type = le16_to_cpu(msg->hdr.type);
2157 case CEPH_MSG_OSD_MAP:
2158 ceph_osdc_handle_map(osdc, msg);
2160 case CEPH_MSG_OSD_OPREPLY:
2161 handle_reply(osdc, msg, con);
2163 case CEPH_MSG_WATCH_NOTIFY:
2164 handle_watch_notify(osdc, msg);
2168 pr_err("received unknown message type %d %s\n", type,
2169 ceph_msg_type_name(type));
2176 * lookup and return message for incoming reply. set up reply message
2179 static struct ceph_msg *get_reply(struct ceph_connection *con,
2180 struct ceph_msg_header *hdr,
2183 struct ceph_osd *osd = con->private;
2184 struct ceph_osd_client *osdc = osd->o_osdc;
2186 struct ceph_osd_request *req;
2187 int front = le32_to_cpu(hdr->front_len);
2188 int data_len = le32_to_cpu(hdr->data_len);
2191 tid = le64_to_cpu(hdr->tid);
2192 mutex_lock(&osdc->request_mutex);
2193 req = __lookup_request(osdc, tid);
2197 dout("get_reply unknown tid %llu from osd%d\n", tid,
2202 if (req->r_reply->con)
2203 dout("%s revoking msg %p from old con %p\n", __func__,
2204 req->r_reply, req->r_reply->con);
2205 ceph_msg_revoke_incoming(req->r_reply);
2207 if (front > req->r_reply->front.iov_len) {
2208 pr_warning("get_reply front %d > preallocated %d\n",
2209 front, (int)req->r_reply->front.iov_len);
2210 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2213 ceph_msg_put(req->r_reply);
2216 m = ceph_msg_get(req->r_reply);
2219 struct ceph_osd_data *osd_data = &req->r_data_in;
2221 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
2222 if (osd_data->pages &&
2223 unlikely(osd_data->length < data_len)) {
2225 pr_warning("tid %lld reply has %d bytes "
2226 "we had only %llu bytes ready\n",
2227 tid, data_len, osd_data->length);
2236 dout("get_reply tid %lld %p\n", tid, m);
2239 mutex_unlock(&osdc->request_mutex);
2244 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2245 struct ceph_msg_header *hdr,
2248 struct ceph_osd *osd = con->private;
2249 int type = le16_to_cpu(hdr->type);
2250 int front = le32_to_cpu(hdr->front_len);
2254 case CEPH_MSG_OSD_MAP:
2255 case CEPH_MSG_WATCH_NOTIFY:
2256 return ceph_msg_new(type, front, GFP_NOFS, false);
2257 case CEPH_MSG_OSD_OPREPLY:
2258 return get_reply(con, hdr, skip);
2260 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2268 * Wrappers to refcount containing ceph_osd struct
2270 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2272 struct ceph_osd *osd = con->private;
2278 static void put_osd_con(struct ceph_connection *con)
2280 struct ceph_osd *osd = con->private;
2288 * Note: returned pointer is the address of a structure that's
2289 * managed separately. Caller must *not* attempt to free it.
2291 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2292 int *proto, int force_new)
2294 struct ceph_osd *o = con->private;
2295 struct ceph_osd_client *osdc = o->o_osdc;
2296 struct ceph_auth_client *ac = osdc->client->monc.auth;
2297 struct ceph_auth_handshake *auth = &o->o_auth;
2299 if (force_new && auth->authorizer) {
2300 ceph_auth_destroy_authorizer(ac, auth->authorizer);
2301 auth->authorizer = NULL;
2303 if (!auth->authorizer) {
2304 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2307 return ERR_PTR(ret);
2309 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2312 return ERR_PTR(ret);
2314 *proto = ac->protocol;
2320 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2322 struct ceph_osd *o = con->private;
2323 struct ceph_osd_client *osdc = o->o_osdc;
2324 struct ceph_auth_client *ac = osdc->client->monc.auth;
2326 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2329 static int invalidate_authorizer(struct ceph_connection *con)
2331 struct ceph_osd *o = con->private;
2332 struct ceph_osd_client *osdc = o->o_osdc;
2333 struct ceph_auth_client *ac = osdc->client->monc.auth;
2335 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2336 return ceph_monc_validate_auth(&osdc->client->monc);
2339 static const struct ceph_connection_operations osd_con_ops = {
2342 .dispatch = dispatch,
2343 .get_authorizer = get_authorizer,
2344 .verify_authorizer_reply = verify_authorizer_reply,
2345 .invalidate_authorizer = invalidate_authorizer,
2346 .alloc_msg = alloc_msg,