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_REPLY_RESERVE_FRONT_LEN 512
18 const static struct ceph_connection_operations osd_con_ops;
20 static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
23 * Implement client access to distributed object storage cluster.
25 * All data objects are stored within a cluster/cloud of OSDs, or
26 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
27 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
28 * remote daemons serving up and coordinating consistent and safe
31 * Cluster membership and the mapping of data objects onto storage devices
32 * are described by the osd map.
34 * We keep track of pending OSD requests (read, write), resubmit
35 * requests to different OSDs when the cluster topology/data layout
36 * change, or retry the affected requests when the communications
37 * channel with an OSD is reset.
41 * calculate the mapping of a file extent onto an object, and fill out the
42 * request accordingly. shorten extent as necessary if it crosses an
45 * fill osd op in request message.
47 static void calc_layout(struct ceph_osd_client *osdc,
48 struct ceph_vino vino, struct ceph_file_layout *layout,
50 struct ceph_osd_request *req)
52 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
53 struct ceph_osd_op *op = (void *)(reqhead + 1);
55 u64 objoff, objlen; /* extent in object */
58 reqhead->snapid = cpu_to_le64(vino.snap);
61 ceph_calc_file_object_mapping(layout, off, plen, &bno,
64 dout(" skipping last %llu, final file extent %llu~%llu\n",
65 orig_len - *plen, off, *plen);
67 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
68 req->r_oid_len = strlen(req->r_oid);
70 op->extent.offset = cpu_to_le64(objoff);
71 op->extent.length = cpu_to_le64(objlen);
72 req->r_num_pages = calc_pages_for(off, *plen);
74 dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
75 req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
78 static void remove_replies(struct ceph_osd_request *req)
81 int max = ARRAY_SIZE(req->replies);
83 for (i=0; i<max; i++) {
85 ceph_msg_put(req->replies[i]);
92 void ceph_osdc_release_request(struct kref *kref)
94 struct ceph_osd_request *req = container_of(kref,
95 struct ceph_osd_request,
99 ceph_msg_put(req->r_request);
101 ceph_msg_put(req->r_reply);
103 if (req->r_con_filling_msg) {
104 dout("release_request revoking pages %p from con %p\n",
105 req->r_pages, req->r_con_filling_msg);
106 ceph_con_revoke_message(req->r_con_filling_msg,
108 ceph_con_put(req->r_con_filling_msg);
110 if (req->r_own_pages)
111 ceph_release_page_vector(req->r_pages,
113 ceph_put_snap_context(req->r_snapc);
115 mempool_free(req, req->r_osdc->req_mempool);
120 static int alloc_replies(struct ceph_osd_request *req, int num_reply)
123 int max = ARRAY_SIZE(req->replies);
125 BUG_ON(num_reply > max);
127 for (i=0; i<num_reply; i++) {
128 req->replies[i] = ceph_msg_new(0, OSD_REPLY_RESERVE_FRONT_LEN, 0, 0, NULL);
129 if (IS_ERR(req->replies[i])) {
131 int err = PTR_ERR(req->replies[i]);
132 for (j = 0; j<=i; j++) {
133 ceph_msg_put(req->replies[j]);
140 req->replies[i] = NULL;
148 static struct ceph_msg *__get_next_reply(struct ceph_connection *con,
149 struct ceph_osd_request *req,
152 struct ceph_msg *reply;
153 if (req->r_con_filling_msg) {
154 dout("revoking reply msg %p from old con %p\n", req->r_reply,
155 req->r_con_filling_msg);
156 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
157 ceph_con_put(req->r_con_filling_msg);
160 reply = req->replies[req->cur_reply];
161 if (!reply || front_len > OSD_REPLY_RESERVE_FRONT_LEN) {
162 /* maybe we can allocate it now? */
163 reply = ceph_msg_new(0, front_len, 0, 0, NULL);
164 if (!reply || IS_ERR(reply)) {
165 pr_err(" reply alloc failed, front_len=%d\n", front_len);
166 return ERR_PTR(-ENOMEM);
169 req->r_con_filling_msg = ceph_con_get(con);
170 req->r_reply = ceph_msg_get(reply); /* for duration of read over socket */
171 return ceph_msg_get(reply);
175 * build new request AND message, calculate layout, and adjust file
178 * if the file was recently truncated, we include information about its
179 * old and new size so that the object can be updated appropriately. (we
180 * avoid synchronously deleting truncated objects because it's slow.)
182 * if @do_sync, include a 'startsync' command so that the osd will flush
185 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
186 struct ceph_file_layout *layout,
187 struct ceph_vino vino,
189 int opcode, int flags,
190 struct ceph_snap_context *snapc,
194 struct timespec *mtime,
195 bool use_mempool, int num_reply)
197 struct ceph_osd_request *req;
198 struct ceph_msg *msg;
199 struct ceph_osd_request_head *head;
200 struct ceph_osd_op *op;
202 int num_op = 1 + do_sync;
203 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
207 req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
208 memset(req, 0, sizeof(*req));
210 req = kzalloc(sizeof(*req), GFP_NOFS);
213 return ERR_PTR(-ENOMEM);
215 err = alloc_replies(req, num_reply);
217 ceph_osdc_put_request(req);
218 return ERR_PTR(-ENOMEM);
220 req->r_num_prealloc_reply = num_reply;
223 req->r_mempool = use_mempool;
224 kref_init(&req->r_kref);
225 init_completion(&req->r_completion);
226 init_completion(&req->r_safe_completion);
227 INIT_LIST_HEAD(&req->r_unsafe_item);
228 req->r_flags = flags;
230 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
232 /* create message; allow space for oid */
235 msg_size += sizeof(u64) * snapc->num_snaps;
237 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
239 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
241 ceph_osdc_put_request(req);
242 return ERR_PTR(PTR_ERR(msg));
244 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
245 memset(msg->front.iov_base, 0, msg->front.iov_len);
246 head = msg->front.iov_base;
247 op = (void *)(head + 1);
248 p = (void *)(op + num_op);
250 req->r_request = msg;
251 req->r_snapc = ceph_get_snap_context(snapc);
253 head->client_inc = cpu_to_le32(1); /* always, for now. */
254 head->flags = cpu_to_le32(flags);
255 if (flags & CEPH_OSD_FLAG_WRITE)
256 ceph_encode_timespec(&head->mtime, mtime);
257 head->num_ops = cpu_to_le16(num_op);
258 op->op = cpu_to_le16(opcode);
260 /* calculate max write size */
261 calc_layout(osdc, vino, layout, off, plen, req);
262 req->r_file_layout = *layout; /* keep a copy */
264 if (flags & CEPH_OSD_FLAG_WRITE) {
265 req->r_request->hdr.data_off = cpu_to_le16(off);
266 req->r_request->hdr.data_len = cpu_to_le32(*plen);
267 op->payload_len = cpu_to_le32(*plen);
269 op->extent.truncate_size = cpu_to_le64(truncate_size);
270 op->extent.truncate_seq = cpu_to_le32(truncate_seq);
273 head->object_len = cpu_to_le32(req->r_oid_len);
274 memcpy(p, req->r_oid, req->r_oid_len);
279 op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
282 head->snap_seq = cpu_to_le64(snapc->seq);
283 head->num_snaps = cpu_to_le32(snapc->num_snaps);
284 for (i = 0; i < snapc->num_snaps; i++) {
285 put_unaligned_le64(snapc->snaps[i], p);
290 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
295 * We keep osd requests in an rbtree, sorted by ->r_tid.
297 static void __insert_request(struct ceph_osd_client *osdc,
298 struct ceph_osd_request *new)
300 struct rb_node **p = &osdc->requests.rb_node;
301 struct rb_node *parent = NULL;
302 struct ceph_osd_request *req = NULL;
306 req = rb_entry(parent, struct ceph_osd_request, r_node);
307 if (new->r_tid < req->r_tid)
309 else if (new->r_tid > req->r_tid)
315 rb_link_node(&new->r_node, parent, p);
316 rb_insert_color(&new->r_node, &osdc->requests);
319 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
322 struct ceph_osd_request *req;
323 struct rb_node *n = osdc->requests.rb_node;
326 req = rb_entry(n, struct ceph_osd_request, r_node);
327 if (tid < req->r_tid)
329 else if (tid > req->r_tid)
337 static struct ceph_osd_request *
338 __lookup_request_ge(struct ceph_osd_client *osdc,
341 struct ceph_osd_request *req;
342 struct rb_node *n = osdc->requests.rb_node;
345 req = rb_entry(n, struct ceph_osd_request, r_node);
346 if (tid < req->r_tid) {
350 } else if (tid > req->r_tid) {
361 * If the osd connection drops, we need to resubmit all requests.
363 static void osd_reset(struct ceph_connection *con)
365 struct ceph_osd *osd = con->private;
366 struct ceph_osd_client *osdc;
370 dout("osd_reset osd%d\n", osd->o_osd);
372 down_read(&osdc->map_sem);
373 kick_requests(osdc, osd);
374 up_read(&osdc->map_sem);
378 * Track open sessions with osds.
380 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
382 struct ceph_osd *osd;
384 osd = kzalloc(sizeof(*osd), GFP_NOFS);
388 atomic_set(&osd->o_ref, 1);
390 INIT_LIST_HEAD(&osd->o_requests);
391 INIT_LIST_HEAD(&osd->o_osd_lru);
392 osd->o_incarnation = 1;
394 ceph_con_init(osdc->client->msgr, &osd->o_con);
395 osd->o_con.private = osd;
396 osd->o_con.ops = &osd_con_ops;
397 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
402 static struct ceph_osd *get_osd(struct ceph_osd *osd)
404 if (atomic_inc_not_zero(&osd->o_ref)) {
405 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
406 atomic_read(&osd->o_ref));
409 dout("get_osd %p FAIL\n", osd);
414 static void put_osd(struct ceph_osd *osd)
416 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
417 atomic_read(&osd->o_ref) - 1);
418 if (atomic_dec_and_test(&osd->o_ref))
423 * remove an osd from our map
425 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
427 dout("__remove_osd %p\n", osd);
428 BUG_ON(!list_empty(&osd->o_requests));
429 rb_erase(&osd->o_node, &osdc->osds);
430 list_del_init(&osd->o_osd_lru);
431 ceph_con_close(&osd->o_con);
435 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
436 struct ceph_osd *osd)
438 dout("__move_osd_to_lru %p\n", osd);
439 BUG_ON(!list_empty(&osd->o_osd_lru));
440 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
441 osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
444 static void __remove_osd_from_lru(struct ceph_osd *osd)
446 dout("__remove_osd_from_lru %p\n", osd);
447 if (!list_empty(&osd->o_osd_lru))
448 list_del_init(&osd->o_osd_lru);
451 static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
453 struct ceph_osd *osd, *nosd;
455 dout("__remove_old_osds %p\n", osdc);
456 mutex_lock(&osdc->request_mutex);
457 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
458 if (!remove_all && time_before(jiffies, osd->lru_ttl))
460 __remove_osd(osdc, osd);
462 mutex_unlock(&osdc->request_mutex);
468 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
472 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
473 if (list_empty(&osd->o_requests)) {
474 __remove_osd(osdc, osd);
476 ceph_con_close(&osd->o_con);
477 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
478 osd->o_incarnation++;
483 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
485 struct rb_node **p = &osdc->osds.rb_node;
486 struct rb_node *parent = NULL;
487 struct ceph_osd *osd = NULL;
491 osd = rb_entry(parent, struct ceph_osd, o_node);
492 if (new->o_osd < osd->o_osd)
494 else if (new->o_osd > osd->o_osd)
500 rb_link_node(&new->o_node, parent, p);
501 rb_insert_color(&new->o_node, &osdc->osds);
504 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
506 struct ceph_osd *osd;
507 struct rb_node *n = osdc->osds.rb_node;
510 osd = rb_entry(n, struct ceph_osd, o_node);
513 else if (o > osd->o_osd)
523 * Register request, assign tid. If this is the first request, set up
526 static void register_request(struct ceph_osd_client *osdc,
527 struct ceph_osd_request *req)
529 mutex_lock(&osdc->request_mutex);
530 req->r_tid = ++osdc->last_tid;
531 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
533 dout("register_request %p tid %lld\n", req, req->r_tid);
534 __insert_request(osdc, req);
535 ceph_osdc_get_request(req);
536 osdc->num_requests++;
538 req->r_timeout_stamp =
539 jiffies + osdc->client->mount_args->osd_timeout*HZ;
541 if (osdc->num_requests == 1) {
542 osdc->timeout_tid = req->r_tid;
543 dout(" timeout on tid %llu at %lu\n", req->r_tid,
544 req->r_timeout_stamp);
545 schedule_delayed_work(&osdc->timeout_work,
546 round_jiffies_relative(req->r_timeout_stamp - jiffies));
548 mutex_unlock(&osdc->request_mutex);
552 * called under osdc->request_mutex
554 static void __unregister_request(struct ceph_osd_client *osdc,
555 struct ceph_osd_request *req)
557 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
558 rb_erase(&req->r_node, &osdc->requests);
559 osdc->num_requests--;
562 /* make sure the original request isn't in flight. */
563 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
565 list_del_init(&req->r_osd_item);
566 if (list_empty(&req->r_osd->o_requests))
567 __move_osd_to_lru(osdc, req->r_osd);
571 ceph_osdc_put_request(req);
573 if (req->r_tid == osdc->timeout_tid) {
574 if (osdc->num_requests == 0) {
575 dout("no requests, canceling timeout\n");
576 osdc->timeout_tid = 0;
577 cancel_delayed_work(&osdc->timeout_work);
579 req = rb_entry(rb_first(&osdc->requests),
580 struct ceph_osd_request, r_node);
581 osdc->timeout_tid = req->r_tid;
582 dout("rescheduled timeout on tid %llu at %lu\n",
583 req->r_tid, req->r_timeout_stamp);
584 schedule_delayed_work(&osdc->timeout_work,
585 round_jiffies_relative(req->r_timeout_stamp -
592 * Cancel a previously queued request message
594 static void __cancel_request(struct ceph_osd_request *req)
597 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
603 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
604 * (as needed), and set the request r_osd appropriately. If there is
605 * no up osd, set r_osd to NULL.
607 * Return 0 if unchanged, 1 if changed, or negative on error.
609 * Caller should hold map_sem for read and request_mutex.
611 static int __map_osds(struct ceph_osd_client *osdc,
612 struct ceph_osd_request *req)
614 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
618 struct ceph_osd *newosd = NULL;
620 dout("map_osds %p tid %lld\n", req, req->r_tid);
621 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
622 &req->r_file_layout, osdc->osdmap);
625 pgid = reqhead->layout.ol_pgid;
628 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
630 if ((req->r_osd && req->r_osd->o_osd == o &&
631 req->r_sent >= req->r_osd->o_incarnation) ||
632 (req->r_osd == NULL && o == -1))
633 return 0; /* no change */
635 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
636 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
637 req->r_osd ? req->r_osd->o_osd : -1);
640 __cancel_request(req);
641 list_del_init(&req->r_osd_item);
642 if (list_empty(&req->r_osd->o_requests)) {
643 /* try to re-use r_osd if possible */
644 newosd = get_osd(req->r_osd);
645 __remove_osd(osdc, newosd);
650 req->r_osd = __lookup_osd(osdc, o);
651 if (!req->r_osd && o >= 0) {
657 req->r_osd = create_osd(osdc);
662 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
663 req->r_osd->o_osd = o;
664 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
665 __insert_osd(osdc, req->r_osd);
667 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
671 __remove_osd_from_lru(req->r_osd);
672 list_add(&req->r_osd_item, &req->r_osd->o_requests);
674 err = 1; /* osd changed */
683 * caller should hold map_sem (for read) and request_mutex
685 static int __send_request(struct ceph_osd_client *osdc,
686 struct ceph_osd_request *req)
688 struct ceph_osd_request_head *reqhead;
691 err = __map_osds(osdc, req);
694 if (req->r_osd == NULL) {
695 dout("send_request %p no up osds in pg\n", req);
696 ceph_monc_request_next_osdmap(&osdc->client->monc);
700 dout("send_request %p tid %llu to osd%d flags %d\n",
701 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
703 reqhead = req->r_request->front.iov_base;
704 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
705 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
706 reqhead->reassert_version = req->r_reassert_version;
708 req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
710 ceph_msg_get(req->r_request); /* send consumes a ref */
711 ceph_con_send(&req->r_osd->o_con, req->r_request);
712 req->r_sent = req->r_osd->o_incarnation;
717 * Timeout callback, called every N seconds when 1 or more osd
718 * requests has been active for more than N seconds. When this
719 * happens, we ping all OSDs with requests who have timed out to
720 * ensure any communications channel reset is detected. Reset the
721 * request timeouts another N seconds in the future as we go.
722 * Reschedule the timeout event another N seconds in future (unless
723 * there are no open requests).
725 static void handle_timeout(struct work_struct *work)
727 struct ceph_osd_client *osdc =
728 container_of(work, struct ceph_osd_client, timeout_work.work);
729 struct ceph_osd_request *req;
730 struct ceph_osd *osd;
731 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
732 unsigned long next_timeout = timeout + jiffies;
736 down_read(&osdc->map_sem);
738 ceph_monc_request_next_osdmap(&osdc->client->monc);
740 mutex_lock(&osdc->request_mutex);
741 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
742 req = rb_entry(p, struct ceph_osd_request, r_node);
747 dout("osdc resending prev failed %lld\n", req->r_tid);
748 err = __send_request(osdc, req);
750 dout("osdc failed again on %lld\n", req->r_tid);
752 req->r_resend = false;
756 for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
757 osd = rb_entry(p, struct ceph_osd, o_node);
758 if (list_empty(&osd->o_requests))
760 req = list_first_entry(&osd->o_requests,
761 struct ceph_osd_request, r_osd_item);
762 if (time_before(jiffies, req->r_timeout_stamp))
765 dout(" tid %llu (at least) timed out on osd%d\n",
766 req->r_tid, osd->o_osd);
767 req->r_timeout_stamp = next_timeout;
768 ceph_con_keepalive(&osd->o_con);
771 if (osdc->timeout_tid)
772 schedule_delayed_work(&osdc->timeout_work,
773 round_jiffies_relative(timeout));
775 mutex_unlock(&osdc->request_mutex);
777 up_read(&osdc->map_sem);
780 static void handle_osds_timeout(struct work_struct *work)
782 struct ceph_osd_client *osdc =
783 container_of(work, struct ceph_osd_client,
784 osds_timeout_work.work);
785 unsigned long delay =
786 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
788 dout("osds timeout\n");
789 down_read(&osdc->map_sem);
790 remove_old_osds(osdc, 0);
791 up_read(&osdc->map_sem);
793 schedule_delayed_work(&osdc->osds_timeout_work,
794 round_jiffies_relative(delay));
798 * handle osd op reply. either call the callback if it is specified,
799 * or do the completion to wake up the waiting thread.
801 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
802 struct ceph_connection *con)
804 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
805 struct ceph_osd_request *req;
807 int numops, object_len, flags;
809 tid = le64_to_cpu(msg->hdr.tid);
810 if (msg->front.iov_len < sizeof(*rhead))
812 numops = le32_to_cpu(rhead->num_ops);
813 object_len = le32_to_cpu(rhead->object_len);
814 if (msg->front.iov_len != sizeof(*rhead) + object_len +
815 numops * sizeof(struct ceph_osd_op))
817 dout("handle_reply %p tid %llu\n", msg, tid);
820 mutex_lock(&osdc->request_mutex);
821 req = __lookup_request(osdc, tid);
823 dout("handle_reply tid %llu dne\n", tid);
824 mutex_unlock(&osdc->request_mutex);
827 ceph_osdc_get_request(req);
828 flags = le32_to_cpu(rhead->flags);
831 * if this connection filled our message, drop our reference now, to
832 * avoid a (safe but slower) revoke later.
834 if (req->r_con_filling_msg == con && req->r_reply == msg) {
835 dout(" got pages, dropping con_filling_msg ref %p\n", con);
836 req->r_con_filling_msg = NULL;
842 * once we see the message has been received, we don't
843 * need a ref (which is only needed for revoking
846 ceph_msg_put(req->r_reply);
850 if (!req->r_got_reply) {
853 req->r_result = le32_to_cpu(rhead->result);
854 bytes = le32_to_cpu(msg->hdr.data_len);
855 dout("handle_reply result %d bytes %d\n", req->r_result,
857 if (req->r_result == 0)
858 req->r_result = bytes;
860 /* in case this is a write and we need to replay, */
861 req->r_reassert_version = rhead->reassert_version;
863 req->r_got_reply = 1;
864 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
865 dout("handle_reply tid %llu dup ack\n", tid);
866 mutex_unlock(&osdc->request_mutex);
870 dout("handle_reply tid %llu flags %d\n", tid, flags);
872 /* either this is a read, or we got the safe response */
873 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
874 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
875 __unregister_request(osdc, req);
877 mutex_unlock(&osdc->request_mutex);
880 req->r_callback(req, msg);
882 complete(&req->r_completion);
884 if (flags & CEPH_OSD_FLAG_ONDISK) {
885 if (req->r_safe_callback)
886 req->r_safe_callback(req, msg);
887 complete(&req->r_safe_completion); /* fsync waiter */
891 ceph_osdc_put_request(req);
895 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
896 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
897 (int)sizeof(*rhead));
903 * Resubmit osd requests whose osd or osd address has changed. Request
904 * a new osd map if osds are down, or we are otherwise unable to determine
905 * how to direct a request.
907 * Close connections to down osds.
909 * If @who is specified, resubmit requests for that specific osd.
911 * Caller should hold map_sem for read and request_mutex.
913 static void kick_requests(struct ceph_osd_client *osdc,
914 struct ceph_osd *kickosd)
916 struct ceph_osd_request *req;
917 struct rb_node *p, *n;
921 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
922 mutex_lock(&osdc->request_mutex);
924 __reset_osd(osdc, kickosd);
926 for (p = rb_first(&osdc->osds); p; p = n) {
927 struct ceph_osd *osd =
928 rb_entry(p, struct ceph_osd, o_node);
931 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
932 memcmp(&osd->o_con.peer_addr,
933 ceph_osd_addr(osdc->osdmap,
935 sizeof(struct ceph_entity_addr)) != 0)
936 __reset_osd(osdc, osd);
940 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
941 req = rb_entry(p, struct ceph_osd_request, r_node);
944 dout(" r_resend set on tid %llu\n", req->r_tid);
945 __cancel_request(req);
948 if (req->r_osd && kickosd == req->r_osd) {
949 __cancel_request(req);
953 err = __map_osds(osdc, req);
955 continue; /* no change */
958 * FIXME: really, we should set the request
959 * error and fail if this isn't a 'nofail'
960 * request, but that's a fair bit more
961 * complicated to do. So retry!
963 dout(" setting r_resend on %llu\n", req->r_tid);
964 req->r_resend = true;
967 if (req->r_osd == NULL) {
968 dout("tid %llu maps to no valid osd\n", req->r_tid);
969 needmap++; /* request a newer map */
974 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
976 req->r_flags |= CEPH_OSD_FLAG_RETRY;
977 err = __send_request(osdc, req);
979 dout(" setting r_resend on %llu\n", req->r_tid);
980 req->r_resend = true;
983 mutex_unlock(&osdc->request_mutex);
986 dout("%d requests for down osds, need new map\n", needmap);
987 ceph_monc_request_next_osdmap(&osdc->client->monc);
992 * Process updated osd map.
994 * The message contains any number of incremental and full maps, normally
995 * indicating some sort of topology change in the cluster. Kick requests
996 * off to different OSDs as needed.
998 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1000 void *p, *end, *next;
1001 u32 nr_maps, maplen;
1003 struct ceph_osdmap *newmap = NULL, *oldmap;
1005 struct ceph_fsid fsid;
1007 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1008 p = msg->front.iov_base;
1009 end = p + msg->front.iov_len;
1012 ceph_decode_need(&p, end, sizeof(fsid), bad);
1013 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1014 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1017 down_write(&osdc->map_sem);
1019 /* incremental maps */
1020 ceph_decode_32_safe(&p, end, nr_maps, bad);
1021 dout(" %d inc maps\n", nr_maps);
1022 while (nr_maps > 0) {
1023 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1024 epoch = ceph_decode_32(&p);
1025 maplen = ceph_decode_32(&p);
1026 ceph_decode_need(&p, end, maplen, bad);
1028 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1029 dout("applying incremental map %u len %d\n",
1031 newmap = osdmap_apply_incremental(&p, next,
1033 osdc->client->msgr);
1034 if (IS_ERR(newmap)) {
1035 err = PTR_ERR(newmap);
1039 if (newmap != osdc->osdmap) {
1040 ceph_osdmap_destroy(osdc->osdmap);
1041 osdc->osdmap = newmap;
1044 dout("ignoring incremental map %u len %d\n",
1054 ceph_decode_32_safe(&p, end, nr_maps, bad);
1055 dout(" %d full maps\n", nr_maps);
1057 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1058 epoch = ceph_decode_32(&p);
1059 maplen = ceph_decode_32(&p);
1060 ceph_decode_need(&p, end, maplen, bad);
1062 dout("skipping non-latest full map %u len %d\n",
1064 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1065 dout("skipping full map %u len %d, "
1066 "older than our %u\n", epoch, maplen,
1067 osdc->osdmap->epoch);
1069 dout("taking full map %u len %d\n", epoch, maplen);
1070 newmap = osdmap_decode(&p, p+maplen);
1071 if (IS_ERR(newmap)) {
1072 err = PTR_ERR(newmap);
1076 oldmap = osdc->osdmap;
1077 osdc->osdmap = newmap;
1079 ceph_osdmap_destroy(oldmap);
1086 downgrade_write(&osdc->map_sem);
1087 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1089 kick_requests(osdc, NULL);
1090 up_read(&osdc->map_sem);
1094 pr_err("osdc handle_map corrupt msg\n");
1096 up_write(&osdc->map_sem);
1102 * A read request prepares specific pages that data is to be read into.
1103 * When a message is being read off the wire, we call prepare_pages to
1105 * 0 = success, -1 failure.
1107 static int __prepare_pages(struct ceph_connection *con,
1108 struct ceph_msg_header *hdr,
1109 struct ceph_osd_request *req,
1113 struct ceph_osd *osd = con->private;
1114 struct ceph_osd_client *osdc;
1116 int data_len = le32_to_cpu(hdr->data_len);
1117 unsigned data_off = le16_to_cpu(hdr->data_off);
1119 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1126 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
1127 tid, req->r_num_pages, want);
1128 if (unlikely(req->r_num_pages < want))
1130 m->pages = req->r_pages;
1131 m->nr_pages = req->r_num_pages;
1132 ret = 0; /* success */
1134 BUG_ON(ret < 0 || m->nr_pages < want);
1140 * Register request, send initial attempt.
1142 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1143 struct ceph_osd_request *req,
1148 req->r_request->pages = req->r_pages;
1149 req->r_request->nr_pages = req->r_num_pages;
1151 register_request(osdc, req);
1153 down_read(&osdc->map_sem);
1154 mutex_lock(&osdc->request_mutex);
1156 * a racing kick_requests() may have sent the message for us
1157 * while we dropped request_mutex above, so only send now if
1158 * the request still han't been touched yet.
1160 if (req->r_sent == 0) {
1161 rc = __send_request(osdc, req);
1164 dout("osdc_start_request failed send, "
1165 " marking %lld\n", req->r_tid);
1166 req->r_resend = true;
1169 __unregister_request(osdc, req);
1173 mutex_unlock(&osdc->request_mutex);
1174 up_read(&osdc->map_sem);
1179 * wait for a request to complete
1181 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1182 struct ceph_osd_request *req)
1186 rc = wait_for_completion_interruptible(&req->r_completion);
1188 mutex_lock(&osdc->request_mutex);
1189 __cancel_request(req);
1190 __unregister_request(osdc, req);
1191 mutex_unlock(&osdc->request_mutex);
1192 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1196 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1197 return req->r_result;
1201 * sync - wait for all in-flight requests to flush. avoid starvation.
1203 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1205 struct ceph_osd_request *req;
1206 u64 last_tid, next_tid = 0;
1208 mutex_lock(&osdc->request_mutex);
1209 last_tid = osdc->last_tid;
1211 req = __lookup_request_ge(osdc, next_tid);
1214 if (req->r_tid > last_tid)
1217 next_tid = req->r_tid + 1;
1218 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1221 ceph_osdc_get_request(req);
1222 mutex_unlock(&osdc->request_mutex);
1223 dout("sync waiting on tid %llu (last is %llu)\n",
1224 req->r_tid, last_tid);
1225 wait_for_completion(&req->r_safe_completion);
1226 mutex_lock(&osdc->request_mutex);
1227 ceph_osdc_put_request(req);
1229 mutex_unlock(&osdc->request_mutex);
1230 dout("sync done (thru tid %llu)\n", last_tid);
1236 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1241 osdc->client = client;
1242 osdc->osdmap = NULL;
1243 init_rwsem(&osdc->map_sem);
1244 init_completion(&osdc->map_waiters);
1245 osdc->last_requested_map = 0;
1246 mutex_init(&osdc->request_mutex);
1247 osdc->timeout_tid = 0;
1249 osdc->osds = RB_ROOT;
1250 INIT_LIST_HEAD(&osdc->osd_lru);
1251 osdc->requests = RB_ROOT;
1252 osdc->num_requests = 0;
1253 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1254 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1256 schedule_delayed_work(&osdc->osds_timeout_work,
1257 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
1260 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1261 sizeof(struct ceph_osd_request));
1262 if (!osdc->req_mempool)
1265 err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
1271 mempool_destroy(osdc->req_mempool);
1276 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1278 cancel_delayed_work_sync(&osdc->timeout_work);
1279 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1281 ceph_osdmap_destroy(osdc->osdmap);
1282 osdc->osdmap = NULL;
1284 remove_old_osds(osdc, 1);
1285 mempool_destroy(osdc->req_mempool);
1286 ceph_msgpool_destroy(&osdc->msgpool_op);
1290 * Read some contiguous pages. If we cross a stripe boundary, shorten
1291 * *plen. Return number of bytes read, or error.
1293 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1294 struct ceph_vino vino, struct ceph_file_layout *layout,
1296 u32 truncate_seq, u64 truncate_size,
1297 struct page **pages, int num_pages)
1299 struct ceph_osd_request *req;
1302 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1303 vino.snap, off, *plen);
1304 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1305 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1306 NULL, 0, truncate_seq, truncate_size, NULL,
1309 return PTR_ERR(req);
1311 /* it may be a short read due to an object boundary */
1312 req->r_pages = pages;
1313 num_pages = calc_pages_for(off, *plen);
1314 req->r_num_pages = num_pages;
1316 dout("readpages final extent is %llu~%llu (%d pages)\n",
1317 off, *plen, req->r_num_pages);
1319 rc = ceph_osdc_start_request(osdc, req, false);
1321 rc = ceph_osdc_wait_request(osdc, req);
1323 ceph_osdc_put_request(req);
1324 dout("readpages result %d\n", rc);
1329 * do a synchronous write on N pages
1331 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1332 struct ceph_file_layout *layout,
1333 struct ceph_snap_context *snapc,
1335 u32 truncate_seq, u64 truncate_size,
1336 struct timespec *mtime,
1337 struct page **pages, int num_pages,
1338 int flags, int do_sync, bool nofail)
1340 struct ceph_osd_request *req;
1343 BUG_ON(vino.snap != CEPH_NOSNAP);
1344 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1346 flags | CEPH_OSD_FLAG_ONDISK |
1347 CEPH_OSD_FLAG_WRITE,
1349 truncate_seq, truncate_size, mtime,
1352 return PTR_ERR(req);
1354 /* it may be a short write due to an object boundary */
1355 req->r_pages = pages;
1356 req->r_num_pages = calc_pages_for(off, len);
1357 dout("writepages %llu~%llu (%d pages)\n", off, len,
1360 rc = ceph_osdc_start_request(osdc, req, nofail);
1362 rc = ceph_osdc_wait_request(osdc, req);
1364 ceph_osdc_put_request(req);
1367 dout("writepages result %d\n", rc);
1372 * handle incoming message
1374 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1376 struct ceph_osd *osd = con->private;
1377 struct ceph_osd_client *osdc;
1378 int type = le16_to_cpu(msg->hdr.type);
1385 case CEPH_MSG_OSD_MAP:
1386 ceph_osdc_handle_map(osdc, msg);
1388 case CEPH_MSG_OSD_OPREPLY:
1389 handle_reply(osdc, msg, con);
1393 pr_err("received unknown message type %d %s\n", type,
1394 ceph_msg_type_name(type));
1399 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1400 struct ceph_msg_header *hdr,
1403 struct ceph_osd *osd = con->private;
1404 struct ceph_osd_client *osdc = osd->o_osdc;
1405 int type = le16_to_cpu(hdr->type);
1406 int front = le32_to_cpu(hdr->front_len);
1407 int data_len = le32_to_cpu(hdr->data_len);
1409 struct ceph_osd_request *req;
1414 if (type != CEPH_MSG_OSD_OPREPLY)
1417 tid = le64_to_cpu(hdr->tid);
1418 mutex_lock(&osdc->request_mutex);
1419 req = __lookup_request(osdc, tid);
1423 dout("alloc_msg unknown tid %llu\n", tid);
1426 m = __get_next_reply(con, req, front);
1427 if (!m || IS_ERR(m)) {
1433 err = __prepare_pages(con, hdr, req, tid, m);
1442 mutex_unlock(&osdc->request_mutex);
1448 * Wrappers to refcount containing ceph_osd struct
1450 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1452 struct ceph_osd *osd = con->private;
1458 static void put_osd_con(struct ceph_connection *con)
1460 struct ceph_osd *osd = con->private;
1467 static int get_authorizer(struct ceph_connection *con,
1468 void **buf, int *len, int *proto,
1469 void **reply_buf, int *reply_len, int force_new)
1471 struct ceph_osd *o = con->private;
1472 struct ceph_osd_client *osdc = o->o_osdc;
1473 struct ceph_auth_client *ac = osdc->client->monc.auth;
1476 if (force_new && o->o_authorizer) {
1477 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1478 o->o_authorizer = NULL;
1480 if (o->o_authorizer == NULL) {
1481 ret = ac->ops->create_authorizer(
1482 ac, CEPH_ENTITY_TYPE_OSD,
1484 &o->o_authorizer_buf,
1485 &o->o_authorizer_buf_len,
1486 &o->o_authorizer_reply_buf,
1487 &o->o_authorizer_reply_buf_len);
1492 *proto = ac->protocol;
1493 *buf = o->o_authorizer_buf;
1494 *len = o->o_authorizer_buf_len;
1495 *reply_buf = o->o_authorizer_reply_buf;
1496 *reply_len = o->o_authorizer_reply_buf_len;
1501 static int verify_authorizer_reply(struct ceph_connection *con, int len)
1503 struct ceph_osd *o = con->private;
1504 struct ceph_osd_client *osdc = o->o_osdc;
1505 struct ceph_auth_client *ac = osdc->client->monc.auth;
1507 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1510 static int invalidate_authorizer(struct ceph_connection *con)
1512 struct ceph_osd *o = con->private;
1513 struct ceph_osd_client *osdc = o->o_osdc;
1514 struct ceph_auth_client *ac = osdc->client->monc.auth;
1516 if (ac->ops->invalidate_authorizer)
1517 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1519 return ceph_monc_validate_auth(&osdc->client->monc);
1522 const static struct ceph_connection_operations osd_con_ops = {
1525 .dispatch = dispatch,
1526 .get_authorizer = get_authorizer,
1527 .verify_authorizer_reply = verify_authorizer_reply,
1528 .invalidate_authorizer = invalidate_authorizer,
1529 .alloc_msg = alloc_msg,