1 #include "ceph_debug.h"
4 #include <linux/highmem.h>
6 #include <linux/pagemap.h>
7 #include <linux/slab.h>
8 #include <linux/uaccess.h>
11 #include "osd_client.h"
12 #include "messenger.h"
16 #define OSD_OP_FRONT_LEN 4096
17 #define OSD_OPREPLY_FRONT_LEN 512
19 const static struct ceph_connection_operations osd_con_ops;
21 static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
24 * Implement client access to distributed object storage cluster.
26 * All data objects are stored within a cluster/cloud of OSDs, or
27 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
28 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
29 * remote daemons serving up and coordinating consistent and safe
32 * Cluster membership and the mapping of data objects onto storage devices
33 * are described by the osd map.
35 * We keep track of pending OSD requests (read, write), resubmit
36 * requests to different OSDs when the cluster topology/data layout
37 * change, or retry the affected requests when the communications
38 * channel with an OSD is reset.
42 * calculate the mapping of a file extent onto an object, and fill out the
43 * request accordingly. shorten extent as necessary if it crosses an
46 * fill osd op in request message.
48 static void calc_layout(struct ceph_osd_client *osdc,
49 struct ceph_vino vino, struct ceph_file_layout *layout,
51 struct ceph_osd_request *req)
53 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
54 struct ceph_osd_op *op = (void *)(reqhead + 1);
56 u64 objoff, objlen; /* extent in object */
59 reqhead->snapid = cpu_to_le64(vino.snap);
62 ceph_calc_file_object_mapping(layout, off, plen, &bno,
65 dout(" skipping last %llu, final file extent %llu~%llu\n",
66 orig_len - *plen, off, *plen);
68 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
69 req->r_oid_len = strlen(req->r_oid);
71 op->extent.offset = cpu_to_le64(objoff);
72 op->extent.length = cpu_to_le64(objlen);
73 req->r_num_pages = calc_pages_for(off, *plen);
75 dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
76 req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
82 void ceph_osdc_release_request(struct kref *kref)
84 struct ceph_osd_request *req = container_of(kref,
85 struct ceph_osd_request,
89 ceph_msg_put(req->r_request);
91 ceph_msg_put(req->r_reply);
92 if (req->r_con_filling_msg) {
93 dout("release_request revoking pages %p from con %p\n",
94 req->r_pages, req->r_con_filling_msg);
95 ceph_con_revoke_message(req->r_con_filling_msg,
97 ceph_con_put(req->r_con_filling_msg);
100 ceph_release_page_vector(req->r_pages,
102 ceph_put_snap_context(req->r_snapc);
104 mempool_free(req, req->r_osdc->req_mempool);
110 * build new request AND message, calculate layout, and adjust file
113 * if the file was recently truncated, we include information about its
114 * old and new size so that the object can be updated appropriately. (we
115 * avoid synchronously deleting truncated objects because it's slow.)
117 * if @do_sync, include a 'startsync' command so that the osd will flush
120 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
121 struct ceph_file_layout *layout,
122 struct ceph_vino vino,
124 int opcode, int flags,
125 struct ceph_snap_context *snapc,
129 struct timespec *mtime,
130 bool use_mempool, int num_reply)
132 struct ceph_osd_request *req;
133 struct ceph_msg *msg;
134 struct ceph_osd_request_head *head;
135 struct ceph_osd_op *op;
137 int num_op = 1 + do_sync;
138 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
142 req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
143 memset(req, 0, sizeof(*req));
145 req = kzalloc(sizeof(*req), GFP_NOFS);
148 return ERR_PTR(-ENOMEM);
151 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 INIT_LIST_HEAD(&req->r_unsafe_item);
156 req->r_flags = flags;
158 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
160 /* create reply message */
162 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
164 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
165 OSD_OPREPLY_FRONT_LEN, 0, 0, NULL);
167 ceph_osdc_put_request(req);
168 return ERR_PTR(PTR_ERR(msg));
172 /* create request message; allow space for oid */
175 msg_size += sizeof(u64) * snapc->num_snaps;
177 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
179 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
181 ceph_osdc_put_request(req);
182 return ERR_PTR(PTR_ERR(msg));
184 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
185 memset(msg->front.iov_base, 0, msg->front.iov_len);
186 head = msg->front.iov_base;
187 op = (void *)(head + 1);
188 p = (void *)(op + num_op);
190 req->r_request = msg;
191 req->r_snapc = ceph_get_snap_context(snapc);
193 head->client_inc = cpu_to_le32(1); /* always, for now. */
194 head->flags = cpu_to_le32(flags);
195 if (flags & CEPH_OSD_FLAG_WRITE)
196 ceph_encode_timespec(&head->mtime, mtime);
197 head->num_ops = cpu_to_le16(num_op);
198 op->op = cpu_to_le16(opcode);
200 /* calculate max write size */
201 calc_layout(osdc, vino, layout, off, plen, req);
202 req->r_file_layout = *layout; /* keep a copy */
204 if (flags & CEPH_OSD_FLAG_WRITE) {
205 req->r_request->hdr.data_off = cpu_to_le16(off);
206 req->r_request->hdr.data_len = cpu_to_le32(*plen);
207 op->payload_len = cpu_to_le32(*plen);
209 op->extent.truncate_size = cpu_to_le64(truncate_size);
210 op->extent.truncate_seq = cpu_to_le32(truncate_seq);
213 head->object_len = cpu_to_le32(req->r_oid_len);
214 memcpy(p, req->r_oid, req->r_oid_len);
219 op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
222 head->snap_seq = cpu_to_le64(snapc->seq);
223 head->num_snaps = cpu_to_le32(snapc->num_snaps);
224 for (i = 0; i < snapc->num_snaps; i++) {
225 put_unaligned_le64(snapc->snaps[i], p);
230 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
235 * We keep osd requests in an rbtree, sorted by ->r_tid.
237 static void __insert_request(struct ceph_osd_client *osdc,
238 struct ceph_osd_request *new)
240 struct rb_node **p = &osdc->requests.rb_node;
241 struct rb_node *parent = NULL;
242 struct ceph_osd_request *req = NULL;
246 req = rb_entry(parent, struct ceph_osd_request, r_node);
247 if (new->r_tid < req->r_tid)
249 else if (new->r_tid > req->r_tid)
255 rb_link_node(&new->r_node, parent, p);
256 rb_insert_color(&new->r_node, &osdc->requests);
259 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
262 struct ceph_osd_request *req;
263 struct rb_node *n = osdc->requests.rb_node;
266 req = rb_entry(n, struct ceph_osd_request, r_node);
267 if (tid < req->r_tid)
269 else if (tid > req->r_tid)
277 static struct ceph_osd_request *
278 __lookup_request_ge(struct ceph_osd_client *osdc,
281 struct ceph_osd_request *req;
282 struct rb_node *n = osdc->requests.rb_node;
285 req = rb_entry(n, struct ceph_osd_request, r_node);
286 if (tid < req->r_tid) {
290 } else if (tid > req->r_tid) {
301 * If the osd connection drops, we need to resubmit all requests.
303 static void osd_reset(struct ceph_connection *con)
305 struct ceph_osd *osd = con->private;
306 struct ceph_osd_client *osdc;
310 dout("osd_reset osd%d\n", osd->o_osd);
312 down_read(&osdc->map_sem);
313 kick_requests(osdc, osd);
314 up_read(&osdc->map_sem);
318 * Track open sessions with osds.
320 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
322 struct ceph_osd *osd;
324 osd = kzalloc(sizeof(*osd), GFP_NOFS);
328 atomic_set(&osd->o_ref, 1);
330 INIT_LIST_HEAD(&osd->o_requests);
331 INIT_LIST_HEAD(&osd->o_osd_lru);
332 osd->o_incarnation = 1;
334 ceph_con_init(osdc->client->msgr, &osd->o_con);
335 osd->o_con.private = osd;
336 osd->o_con.ops = &osd_con_ops;
337 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
342 static struct ceph_osd *get_osd(struct ceph_osd *osd)
344 if (atomic_inc_not_zero(&osd->o_ref)) {
345 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
346 atomic_read(&osd->o_ref));
349 dout("get_osd %p FAIL\n", osd);
354 static void put_osd(struct ceph_osd *osd)
356 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
357 atomic_read(&osd->o_ref) - 1);
358 if (atomic_dec_and_test(&osd->o_ref))
363 * remove an osd from our map
365 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
367 dout("__remove_osd %p\n", osd);
368 BUG_ON(!list_empty(&osd->o_requests));
369 rb_erase(&osd->o_node, &osdc->osds);
370 list_del_init(&osd->o_osd_lru);
371 ceph_con_close(&osd->o_con);
375 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
376 struct ceph_osd *osd)
378 dout("__move_osd_to_lru %p\n", osd);
379 BUG_ON(!list_empty(&osd->o_osd_lru));
380 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
381 osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
384 static void __remove_osd_from_lru(struct ceph_osd *osd)
386 dout("__remove_osd_from_lru %p\n", osd);
387 if (!list_empty(&osd->o_osd_lru))
388 list_del_init(&osd->o_osd_lru);
391 static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
393 struct ceph_osd *osd, *nosd;
395 dout("__remove_old_osds %p\n", osdc);
396 mutex_lock(&osdc->request_mutex);
397 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
398 if (!remove_all && time_before(jiffies, osd->lru_ttl))
400 __remove_osd(osdc, osd);
402 mutex_unlock(&osdc->request_mutex);
408 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
412 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
413 if (list_empty(&osd->o_requests)) {
414 __remove_osd(osdc, osd);
416 ceph_con_close(&osd->o_con);
417 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
418 osd->o_incarnation++;
423 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
425 struct rb_node **p = &osdc->osds.rb_node;
426 struct rb_node *parent = NULL;
427 struct ceph_osd *osd = NULL;
431 osd = rb_entry(parent, struct ceph_osd, o_node);
432 if (new->o_osd < osd->o_osd)
434 else if (new->o_osd > osd->o_osd)
440 rb_link_node(&new->o_node, parent, p);
441 rb_insert_color(&new->o_node, &osdc->osds);
444 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
446 struct ceph_osd *osd;
447 struct rb_node *n = osdc->osds.rb_node;
450 osd = rb_entry(n, struct ceph_osd, o_node);
453 else if (o > osd->o_osd)
463 * Register request, assign tid. If this is the first request, set up
466 static void register_request(struct ceph_osd_client *osdc,
467 struct ceph_osd_request *req)
469 mutex_lock(&osdc->request_mutex);
470 req->r_tid = ++osdc->last_tid;
471 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
473 dout("register_request %p tid %lld\n", req, req->r_tid);
474 __insert_request(osdc, req);
475 ceph_osdc_get_request(req);
476 osdc->num_requests++;
478 req->r_timeout_stamp =
479 jiffies + osdc->client->mount_args->osd_timeout*HZ;
481 if (osdc->num_requests == 1) {
482 osdc->timeout_tid = req->r_tid;
483 dout(" timeout on tid %llu at %lu\n", req->r_tid,
484 req->r_timeout_stamp);
485 schedule_delayed_work(&osdc->timeout_work,
486 round_jiffies_relative(req->r_timeout_stamp - jiffies));
488 mutex_unlock(&osdc->request_mutex);
492 * called under osdc->request_mutex
494 static void __unregister_request(struct ceph_osd_client *osdc,
495 struct ceph_osd_request *req)
497 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
498 rb_erase(&req->r_node, &osdc->requests);
499 osdc->num_requests--;
502 /* make sure the original request isn't in flight. */
503 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
505 list_del_init(&req->r_osd_item);
506 if (list_empty(&req->r_osd->o_requests))
507 __move_osd_to_lru(osdc, req->r_osd);
511 ceph_osdc_put_request(req);
513 if (req->r_tid == osdc->timeout_tid) {
514 if (osdc->num_requests == 0) {
515 dout("no requests, canceling timeout\n");
516 osdc->timeout_tid = 0;
517 cancel_delayed_work(&osdc->timeout_work);
519 req = rb_entry(rb_first(&osdc->requests),
520 struct ceph_osd_request, r_node);
521 osdc->timeout_tid = req->r_tid;
522 dout("rescheduled timeout on tid %llu at %lu\n",
523 req->r_tid, req->r_timeout_stamp);
524 schedule_delayed_work(&osdc->timeout_work,
525 round_jiffies_relative(req->r_timeout_stamp -
532 * Cancel a previously queued request message
534 static void __cancel_request(struct ceph_osd_request *req)
537 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
543 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
544 * (as needed), and set the request r_osd appropriately. If there is
545 * no up osd, set r_osd to NULL.
547 * Return 0 if unchanged, 1 if changed, or negative on error.
549 * Caller should hold map_sem for read and request_mutex.
551 static int __map_osds(struct ceph_osd_client *osdc,
552 struct ceph_osd_request *req)
554 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
559 dout("map_osds %p tid %lld\n", req, req->r_tid);
560 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
561 &req->r_file_layout, osdc->osdmap);
564 pgid = reqhead->layout.ol_pgid;
567 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
569 if ((req->r_osd && req->r_osd->o_osd == o &&
570 req->r_sent >= req->r_osd->o_incarnation) ||
571 (req->r_osd == NULL && o == -1))
572 return 0; /* no change */
574 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
575 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
576 req->r_osd ? req->r_osd->o_osd : -1);
579 __cancel_request(req);
580 list_del_init(&req->r_osd_item);
584 req->r_osd = __lookup_osd(osdc, o);
585 if (!req->r_osd && o >= 0) {
587 req->r_osd = create_osd(osdc);
591 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
592 req->r_osd->o_osd = o;
593 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
594 __insert_osd(osdc, req->r_osd);
596 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
600 __remove_osd_from_lru(req->r_osd);
601 list_add(&req->r_osd_item, &req->r_osd->o_requests);
603 err = 1; /* osd changed */
610 * caller should hold map_sem (for read) and request_mutex
612 static int __send_request(struct ceph_osd_client *osdc,
613 struct ceph_osd_request *req)
615 struct ceph_osd_request_head *reqhead;
618 err = __map_osds(osdc, req);
621 if (req->r_osd == NULL) {
622 dout("send_request %p no up osds in pg\n", req);
623 ceph_monc_request_next_osdmap(&osdc->client->monc);
627 dout("send_request %p tid %llu to osd%d flags %d\n",
628 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
630 reqhead = req->r_request->front.iov_base;
631 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
632 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
633 reqhead->reassert_version = req->r_reassert_version;
635 req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
637 ceph_msg_get(req->r_request); /* send consumes a ref */
638 ceph_con_send(&req->r_osd->o_con, req->r_request);
639 req->r_sent = req->r_osd->o_incarnation;
644 * Timeout callback, called every N seconds when 1 or more osd
645 * requests has been active for more than N seconds. When this
646 * happens, we ping all OSDs with requests who have timed out to
647 * ensure any communications channel reset is detected. Reset the
648 * request timeouts another N seconds in the future as we go.
649 * Reschedule the timeout event another N seconds in future (unless
650 * there are no open requests).
652 static void handle_timeout(struct work_struct *work)
654 struct ceph_osd_client *osdc =
655 container_of(work, struct ceph_osd_client, timeout_work.work);
656 struct ceph_osd_request *req;
657 struct ceph_osd *osd;
658 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
659 unsigned long next_timeout = timeout + jiffies;
663 down_read(&osdc->map_sem);
665 ceph_monc_request_next_osdmap(&osdc->client->monc);
667 mutex_lock(&osdc->request_mutex);
668 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
669 req = rb_entry(p, struct ceph_osd_request, r_node);
674 dout("osdc resending prev failed %lld\n", req->r_tid);
675 err = __send_request(osdc, req);
677 dout("osdc failed again on %lld\n", req->r_tid);
679 req->r_resend = false;
683 for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
684 osd = rb_entry(p, struct ceph_osd, o_node);
685 if (list_empty(&osd->o_requests))
687 req = list_first_entry(&osd->o_requests,
688 struct ceph_osd_request, r_osd_item);
689 if (time_before(jiffies, req->r_timeout_stamp))
692 dout(" tid %llu (at least) timed out on osd%d\n",
693 req->r_tid, osd->o_osd);
694 req->r_timeout_stamp = next_timeout;
695 ceph_con_keepalive(&osd->o_con);
698 if (osdc->timeout_tid)
699 schedule_delayed_work(&osdc->timeout_work,
700 round_jiffies_relative(timeout));
702 mutex_unlock(&osdc->request_mutex);
704 up_read(&osdc->map_sem);
707 static void handle_osds_timeout(struct work_struct *work)
709 struct ceph_osd_client *osdc =
710 container_of(work, struct ceph_osd_client,
711 osds_timeout_work.work);
712 unsigned long delay =
713 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
715 dout("osds timeout\n");
716 down_read(&osdc->map_sem);
717 remove_old_osds(osdc, 0);
718 up_read(&osdc->map_sem);
720 schedule_delayed_work(&osdc->osds_timeout_work,
721 round_jiffies_relative(delay));
725 * handle osd op reply. either call the callback if it is specified,
726 * or do the completion to wake up the waiting thread.
728 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
729 struct ceph_connection *con)
731 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
732 struct ceph_osd_request *req;
734 int numops, object_len, flags;
736 tid = le64_to_cpu(msg->hdr.tid);
737 if (msg->front.iov_len < sizeof(*rhead))
739 numops = le32_to_cpu(rhead->num_ops);
740 object_len = le32_to_cpu(rhead->object_len);
741 if (msg->front.iov_len != sizeof(*rhead) + object_len +
742 numops * sizeof(struct ceph_osd_op))
744 dout("handle_reply %p tid %llu\n", msg, tid);
747 mutex_lock(&osdc->request_mutex);
748 req = __lookup_request(osdc, tid);
750 dout("handle_reply tid %llu dne\n", tid);
751 mutex_unlock(&osdc->request_mutex);
754 ceph_osdc_get_request(req);
755 flags = le32_to_cpu(rhead->flags);
758 * if this connection filled our message, drop our reference now, to
759 * avoid a (safe but slower) revoke later.
761 if (req->r_con_filling_msg == con && req->r_reply == msg) {
762 dout(" dropping con_filling_msg ref %p\n", con);
763 req->r_con_filling_msg = NULL;
767 if (!req->r_got_reply) {
770 req->r_result = le32_to_cpu(rhead->result);
771 bytes = le32_to_cpu(msg->hdr.data_len);
772 dout("handle_reply result %d bytes %d\n", req->r_result,
774 if (req->r_result == 0)
775 req->r_result = bytes;
777 /* in case this is a write and we need to replay, */
778 req->r_reassert_version = rhead->reassert_version;
780 req->r_got_reply = 1;
781 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
782 dout("handle_reply tid %llu dup ack\n", tid);
783 mutex_unlock(&osdc->request_mutex);
787 dout("handle_reply tid %llu flags %d\n", tid, flags);
789 /* either this is a read, or we got the safe response */
790 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
791 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
792 __unregister_request(osdc, req);
794 mutex_unlock(&osdc->request_mutex);
797 req->r_callback(req, msg);
799 complete(&req->r_completion);
801 if (flags & CEPH_OSD_FLAG_ONDISK) {
802 if (req->r_safe_callback)
803 req->r_safe_callback(req, msg);
804 complete(&req->r_safe_completion); /* fsync waiter */
808 ceph_osdc_put_request(req);
812 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
813 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
814 (int)sizeof(*rhead));
820 * Resubmit osd requests whose osd or osd address has changed. Request
821 * a new osd map if osds are down, or we are otherwise unable to determine
822 * how to direct a request.
824 * Close connections to down osds.
826 * If @who is specified, resubmit requests for that specific osd.
828 * Caller should hold map_sem for read and request_mutex.
830 static void kick_requests(struct ceph_osd_client *osdc,
831 struct ceph_osd *kickosd)
833 struct ceph_osd_request *req;
834 struct rb_node *p, *n;
838 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
839 mutex_lock(&osdc->request_mutex);
841 __reset_osd(osdc, kickosd);
843 for (p = rb_first(&osdc->osds); p; p = n) {
844 struct ceph_osd *osd =
845 rb_entry(p, struct ceph_osd, o_node);
848 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
849 memcmp(&osd->o_con.peer_addr,
850 ceph_osd_addr(osdc->osdmap,
852 sizeof(struct ceph_entity_addr)) != 0)
853 __reset_osd(osdc, osd);
857 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
858 req = rb_entry(p, struct ceph_osd_request, r_node);
861 dout(" r_resend set on tid %llu\n", req->r_tid);
862 __cancel_request(req);
865 if (req->r_osd && kickosd == req->r_osd) {
866 __cancel_request(req);
870 err = __map_osds(osdc, req);
872 continue; /* no change */
875 * FIXME: really, we should set the request
876 * error and fail if this isn't a 'nofail'
877 * request, but that's a fair bit more
878 * complicated to do. So retry!
880 dout(" setting r_resend on %llu\n", req->r_tid);
881 req->r_resend = true;
884 if (req->r_osd == NULL) {
885 dout("tid %llu maps to no valid osd\n", req->r_tid);
886 needmap++; /* request a newer map */
891 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
893 req->r_flags |= CEPH_OSD_FLAG_RETRY;
894 err = __send_request(osdc, req);
896 dout(" setting r_resend on %llu\n", req->r_tid);
897 req->r_resend = true;
900 mutex_unlock(&osdc->request_mutex);
903 dout("%d requests for down osds, need new map\n", needmap);
904 ceph_monc_request_next_osdmap(&osdc->client->monc);
909 * Process updated osd map.
911 * The message contains any number of incremental and full maps, normally
912 * indicating some sort of topology change in the cluster. Kick requests
913 * off to different OSDs as needed.
915 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
917 void *p, *end, *next;
920 struct ceph_osdmap *newmap = NULL, *oldmap;
922 struct ceph_fsid fsid;
924 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
925 p = msg->front.iov_base;
926 end = p + msg->front.iov_len;
929 ceph_decode_need(&p, end, sizeof(fsid), bad);
930 ceph_decode_copy(&p, &fsid, sizeof(fsid));
931 if (ceph_check_fsid(osdc->client, &fsid) < 0)
934 down_write(&osdc->map_sem);
936 /* incremental maps */
937 ceph_decode_32_safe(&p, end, nr_maps, bad);
938 dout(" %d inc maps\n", nr_maps);
939 while (nr_maps > 0) {
940 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
941 epoch = ceph_decode_32(&p);
942 maplen = ceph_decode_32(&p);
943 ceph_decode_need(&p, end, maplen, bad);
945 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
946 dout("applying incremental map %u len %d\n",
948 newmap = osdmap_apply_incremental(&p, next,
951 if (IS_ERR(newmap)) {
952 err = PTR_ERR(newmap);
956 if (newmap != osdc->osdmap) {
957 ceph_osdmap_destroy(osdc->osdmap);
958 osdc->osdmap = newmap;
961 dout("ignoring incremental map %u len %d\n",
971 ceph_decode_32_safe(&p, end, nr_maps, bad);
972 dout(" %d full maps\n", nr_maps);
974 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
975 epoch = ceph_decode_32(&p);
976 maplen = ceph_decode_32(&p);
977 ceph_decode_need(&p, end, maplen, bad);
979 dout("skipping non-latest full map %u len %d\n",
981 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
982 dout("skipping full map %u len %d, "
983 "older than our %u\n", epoch, maplen,
984 osdc->osdmap->epoch);
986 dout("taking full map %u len %d\n", epoch, maplen);
987 newmap = osdmap_decode(&p, p+maplen);
988 if (IS_ERR(newmap)) {
989 err = PTR_ERR(newmap);
993 oldmap = osdc->osdmap;
994 osdc->osdmap = newmap;
996 ceph_osdmap_destroy(oldmap);
1003 downgrade_write(&osdc->map_sem);
1004 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1006 kick_requests(osdc, NULL);
1007 up_read(&osdc->map_sem);
1011 pr_err("osdc handle_map corrupt msg\n");
1013 up_write(&osdc->map_sem);
1019 * A read request prepares specific pages that data is to be read into.
1020 * When a message is being read off the wire, we call prepare_pages to
1022 * 0 = success, -1 failure.
1024 static int __prepare_pages(struct ceph_connection *con,
1025 struct ceph_msg_header *hdr,
1026 struct ceph_osd_request *req,
1030 struct ceph_osd *osd = con->private;
1031 struct ceph_osd_client *osdc;
1033 int data_len = le32_to_cpu(hdr->data_len);
1034 unsigned data_off = le16_to_cpu(hdr->data_off);
1036 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1043 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
1044 tid, req->r_num_pages, want);
1045 if (unlikely(req->r_num_pages < want))
1047 m->pages = req->r_pages;
1048 m->nr_pages = req->r_num_pages;
1049 ret = 0; /* success */
1051 BUG_ON(ret < 0 || m->nr_pages < want);
1057 * Register request, send initial attempt.
1059 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1060 struct ceph_osd_request *req,
1065 req->r_request->pages = req->r_pages;
1066 req->r_request->nr_pages = req->r_num_pages;
1068 register_request(osdc, req);
1070 down_read(&osdc->map_sem);
1071 mutex_lock(&osdc->request_mutex);
1073 * a racing kick_requests() may have sent the message for us
1074 * while we dropped request_mutex above, so only send now if
1075 * the request still han't been touched yet.
1077 if (req->r_sent == 0) {
1078 rc = __send_request(osdc, req);
1081 dout("osdc_start_request failed send, "
1082 " marking %lld\n", req->r_tid);
1083 req->r_resend = true;
1086 __unregister_request(osdc, req);
1090 mutex_unlock(&osdc->request_mutex);
1091 up_read(&osdc->map_sem);
1096 * wait for a request to complete
1098 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1099 struct ceph_osd_request *req)
1103 rc = wait_for_completion_interruptible(&req->r_completion);
1105 mutex_lock(&osdc->request_mutex);
1106 __cancel_request(req);
1107 __unregister_request(osdc, req);
1108 mutex_unlock(&osdc->request_mutex);
1109 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1113 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1114 return req->r_result;
1118 * sync - wait for all in-flight requests to flush. avoid starvation.
1120 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1122 struct ceph_osd_request *req;
1123 u64 last_tid, next_tid = 0;
1125 mutex_lock(&osdc->request_mutex);
1126 last_tid = osdc->last_tid;
1128 req = __lookup_request_ge(osdc, next_tid);
1131 if (req->r_tid > last_tid)
1134 next_tid = req->r_tid + 1;
1135 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1138 ceph_osdc_get_request(req);
1139 mutex_unlock(&osdc->request_mutex);
1140 dout("sync waiting on tid %llu (last is %llu)\n",
1141 req->r_tid, last_tid);
1142 wait_for_completion(&req->r_safe_completion);
1143 mutex_lock(&osdc->request_mutex);
1144 ceph_osdc_put_request(req);
1146 mutex_unlock(&osdc->request_mutex);
1147 dout("sync done (thru tid %llu)\n", last_tid);
1153 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1158 osdc->client = client;
1159 osdc->osdmap = NULL;
1160 init_rwsem(&osdc->map_sem);
1161 init_completion(&osdc->map_waiters);
1162 osdc->last_requested_map = 0;
1163 mutex_init(&osdc->request_mutex);
1164 osdc->timeout_tid = 0;
1166 osdc->osds = RB_ROOT;
1167 INIT_LIST_HEAD(&osdc->osd_lru);
1168 osdc->requests = RB_ROOT;
1169 osdc->num_requests = 0;
1170 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1171 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1173 schedule_delayed_work(&osdc->osds_timeout_work,
1174 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
1177 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1178 sizeof(struct ceph_osd_request));
1179 if (!osdc->req_mempool)
1182 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true);
1185 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1186 OSD_OPREPLY_FRONT_LEN, 10, true);
1192 ceph_msgpool_destroy(&osdc->msgpool_op);
1194 mempool_destroy(osdc->req_mempool);
1199 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1201 cancel_delayed_work_sync(&osdc->timeout_work);
1202 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1204 ceph_osdmap_destroy(osdc->osdmap);
1205 osdc->osdmap = NULL;
1207 remove_old_osds(osdc, 1);
1208 mempool_destroy(osdc->req_mempool);
1209 ceph_msgpool_destroy(&osdc->msgpool_op);
1210 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1214 * Read some contiguous pages. If we cross a stripe boundary, shorten
1215 * *plen. Return number of bytes read, or error.
1217 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1218 struct ceph_vino vino, struct ceph_file_layout *layout,
1220 u32 truncate_seq, u64 truncate_size,
1221 struct page **pages, int num_pages)
1223 struct ceph_osd_request *req;
1226 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1227 vino.snap, off, *plen);
1228 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1229 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1230 NULL, 0, truncate_seq, truncate_size, NULL,
1233 return PTR_ERR(req);
1235 /* it may be a short read due to an object boundary */
1236 req->r_pages = pages;
1237 num_pages = calc_pages_for(off, *plen);
1238 req->r_num_pages = num_pages;
1240 dout("readpages final extent is %llu~%llu (%d pages)\n",
1241 off, *plen, req->r_num_pages);
1243 rc = ceph_osdc_start_request(osdc, req, false);
1245 rc = ceph_osdc_wait_request(osdc, req);
1247 ceph_osdc_put_request(req);
1248 dout("readpages result %d\n", rc);
1253 * do a synchronous write on N pages
1255 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1256 struct ceph_file_layout *layout,
1257 struct ceph_snap_context *snapc,
1259 u32 truncate_seq, u64 truncate_size,
1260 struct timespec *mtime,
1261 struct page **pages, int num_pages,
1262 int flags, int do_sync, bool nofail)
1264 struct ceph_osd_request *req;
1267 BUG_ON(vino.snap != CEPH_NOSNAP);
1268 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1270 flags | CEPH_OSD_FLAG_ONDISK |
1271 CEPH_OSD_FLAG_WRITE,
1273 truncate_seq, truncate_size, mtime,
1276 return PTR_ERR(req);
1278 /* it may be a short write due to an object boundary */
1279 req->r_pages = pages;
1280 req->r_num_pages = calc_pages_for(off, len);
1281 dout("writepages %llu~%llu (%d pages)\n", off, len,
1284 rc = ceph_osdc_start_request(osdc, req, nofail);
1286 rc = ceph_osdc_wait_request(osdc, req);
1288 ceph_osdc_put_request(req);
1291 dout("writepages result %d\n", rc);
1296 * handle incoming message
1298 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1300 struct ceph_osd *osd = con->private;
1301 struct ceph_osd_client *osdc;
1302 int type = le16_to_cpu(msg->hdr.type);
1309 case CEPH_MSG_OSD_MAP:
1310 ceph_osdc_handle_map(osdc, msg);
1312 case CEPH_MSG_OSD_OPREPLY:
1313 handle_reply(osdc, msg, con);
1317 pr_err("received unknown message type %d %s\n", type,
1318 ceph_msg_type_name(type));
1324 * lookup and return message for incoming reply
1326 static struct ceph_msg *get_reply(struct ceph_connection *con,
1327 struct ceph_msg_header *hdr,
1330 struct ceph_osd *osd = con->private;
1331 struct ceph_osd_client *osdc = osd->o_osdc;
1333 struct ceph_osd_request *req;
1334 int front = le32_to_cpu(hdr->front_len);
1335 int data_len = le32_to_cpu(hdr->data_len);
1339 tid = le64_to_cpu(hdr->tid);
1340 mutex_lock(&osdc->request_mutex);
1341 req = __lookup_request(osdc, tid);
1345 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
1350 if (req->r_con_filling_msg) {
1351 dout("get_reply revoking msg %p from old con %p\n",
1352 req->r_reply, req->r_con_filling_msg);
1353 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1354 ceph_con_put(req->r_con_filling_msg);
1357 if (front > req->r_reply->front.iov_len) {
1358 pr_warning("get_reply front %d > preallocated %d\n",
1359 front, (int)req->r_reply->front.iov_len);
1360 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL);
1363 ceph_msg_put(req->r_reply);
1366 m = ceph_msg_get(req->r_reply);
1369 err = __prepare_pages(con, hdr, req, tid, m);
1377 req->r_con_filling_msg = ceph_con_get(con);
1378 dout("get_reply tid %lld %p\n", tid, m);
1381 mutex_unlock(&osdc->request_mutex);
1386 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1387 struct ceph_msg_header *hdr,
1390 struct ceph_osd *osd = con->private;
1391 int type = le16_to_cpu(hdr->type);
1392 int front = le32_to_cpu(hdr->front_len);
1395 case CEPH_MSG_OSD_MAP:
1396 return ceph_msg_new(type, front, 0, 0, NULL);
1397 case CEPH_MSG_OSD_OPREPLY:
1398 return get_reply(con, hdr, skip);
1400 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1408 * Wrappers to refcount containing ceph_osd struct
1410 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1412 struct ceph_osd *osd = con->private;
1418 static void put_osd_con(struct ceph_connection *con)
1420 struct ceph_osd *osd = con->private;
1427 static int get_authorizer(struct ceph_connection *con,
1428 void **buf, int *len, int *proto,
1429 void **reply_buf, int *reply_len, int force_new)
1431 struct ceph_osd *o = con->private;
1432 struct ceph_osd_client *osdc = o->o_osdc;
1433 struct ceph_auth_client *ac = osdc->client->monc.auth;
1436 if (force_new && o->o_authorizer) {
1437 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1438 o->o_authorizer = NULL;
1440 if (o->o_authorizer == NULL) {
1441 ret = ac->ops->create_authorizer(
1442 ac, CEPH_ENTITY_TYPE_OSD,
1444 &o->o_authorizer_buf,
1445 &o->o_authorizer_buf_len,
1446 &o->o_authorizer_reply_buf,
1447 &o->o_authorizer_reply_buf_len);
1452 *proto = ac->protocol;
1453 *buf = o->o_authorizer_buf;
1454 *len = o->o_authorizer_buf_len;
1455 *reply_buf = o->o_authorizer_reply_buf;
1456 *reply_len = o->o_authorizer_reply_buf_len;
1461 static int verify_authorizer_reply(struct ceph_connection *con, int len)
1463 struct ceph_osd *o = con->private;
1464 struct ceph_osd_client *osdc = o->o_osdc;
1465 struct ceph_auth_client *ac = osdc->client->monc.auth;
1467 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1470 static int invalidate_authorizer(struct ceph_connection *con)
1472 struct ceph_osd *o = con->private;
1473 struct ceph_osd_client *osdc = o->o_osdc;
1474 struct ceph_auth_client *ac = osdc->client->monc.auth;
1476 if (ac->ops->invalidate_authorizer)
1477 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1479 return ceph_monc_validate_auth(&osdc->client->monc);
1482 const static struct ceph_connection_operations osd_con_ops = {
1485 .dispatch = dispatch,
1486 .get_authorizer = get_authorizer,
1487 .verify_authorizer_reply = verify_authorizer_reply,
1488 .invalidate_authorizer = invalidate_authorizer,
1489 .alloc_msg = alloc_msg,