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;
20 static int __kick_requests(struct ceph_osd_client *osdc,
21 struct ceph_osd *kickosd);
23 static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
26 * Implement client access to distributed object storage cluster.
28 * All data objects are stored within a cluster/cloud of OSDs, or
29 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
30 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
31 * remote daemons serving up and coordinating consistent and safe
34 * Cluster membership and the mapping of data objects onto storage devices
35 * are described by the osd map.
37 * We keep track of pending OSD requests (read, write), resubmit
38 * requests to different OSDs when the cluster topology/data layout
39 * change, or retry the affected requests when the communications
40 * channel with an OSD is reset.
44 * calculate the mapping of a file extent onto an object, and fill out the
45 * request accordingly. shorten extent as necessary if it crosses an
48 * fill osd op in request message.
50 static void calc_layout(struct ceph_osd_client *osdc,
51 struct ceph_vino vino, struct ceph_file_layout *layout,
53 struct ceph_osd_request *req)
55 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
56 struct ceph_osd_op *op = (void *)(reqhead + 1);
58 u64 objoff, objlen; /* extent in object */
61 reqhead->snapid = cpu_to_le64(vino.snap);
64 ceph_calc_file_object_mapping(layout, off, plen, &bno,
67 dout(" skipping last %llu, final file extent %llu~%llu\n",
68 orig_len - *plen, off, *plen);
70 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
71 req->r_oid_len = strlen(req->r_oid);
73 op->extent.offset = cpu_to_le64(objoff);
74 op->extent.length = cpu_to_le64(objlen);
75 req->r_num_pages = calc_pages_for(off, *plen);
77 dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
78 req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
84 void ceph_osdc_release_request(struct kref *kref)
86 struct ceph_osd_request *req = container_of(kref,
87 struct ceph_osd_request,
91 ceph_msg_put(req->r_request);
93 ceph_msg_put(req->r_reply);
94 if (req->r_con_filling_msg) {
95 dout("release_request revoking pages %p from con %p\n",
96 req->r_pages, req->r_con_filling_msg);
97 ceph_con_revoke_message(req->r_con_filling_msg,
99 ceph_con_put(req->r_con_filling_msg);
101 if (req->r_own_pages)
102 ceph_release_page_vector(req->r_pages,
104 ceph_put_snap_context(req->r_snapc);
106 mempool_free(req, req->r_osdc->req_mempool);
112 * build new request AND message, calculate layout, and adjust file
115 * if the file was recently truncated, we include information about its
116 * old and new size so that the object can be updated appropriately. (we
117 * avoid synchronously deleting truncated objects because it's slow.)
119 * if @do_sync, include a 'startsync' command so that the osd will flush
122 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
123 struct ceph_file_layout *layout,
124 struct ceph_vino vino,
126 int opcode, int flags,
127 struct ceph_snap_context *snapc,
131 struct timespec *mtime,
132 bool use_mempool, int num_reply)
134 struct ceph_osd_request *req;
135 struct ceph_msg *msg;
136 struct ceph_osd_request_head *head;
137 struct ceph_osd_op *op;
139 int num_op = 1 + do_sync;
140 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
144 req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
145 memset(req, 0, sizeof(*req));
147 req = kzalloc(sizeof(*req), GFP_NOFS);
150 return ERR_PTR(-ENOMEM);
153 req->r_mempool = use_mempool;
154 kref_init(&req->r_kref);
155 init_completion(&req->r_completion);
156 init_completion(&req->r_safe_completion);
157 INIT_LIST_HEAD(&req->r_unsafe_item);
158 req->r_flags = flags;
160 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
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, 0, 0, NULL);
169 ceph_osdc_put_request(req);
170 return ERR_PTR(PTR_ERR(msg));
174 /* create request message; allow space for oid */
177 msg_size += sizeof(u64) * snapc->num_snaps;
179 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
181 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
183 ceph_osdc_put_request(req);
184 return ERR_PTR(PTR_ERR(msg));
186 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
187 memset(msg->front.iov_base, 0, msg->front.iov_len);
188 head = msg->front.iov_base;
189 op = (void *)(head + 1);
190 p = (void *)(op + num_op);
192 req->r_request = msg;
193 req->r_snapc = ceph_get_snap_context(snapc);
195 head->client_inc = cpu_to_le32(1); /* always, for now. */
196 head->flags = cpu_to_le32(flags);
197 if (flags & CEPH_OSD_FLAG_WRITE)
198 ceph_encode_timespec(&head->mtime, mtime);
199 head->num_ops = cpu_to_le16(num_op);
200 op->op = cpu_to_le16(opcode);
202 /* calculate max write size */
203 calc_layout(osdc, vino, layout, off, plen, req);
204 req->r_file_layout = *layout; /* keep a copy */
206 if (flags & CEPH_OSD_FLAG_WRITE) {
207 req->r_request->hdr.data_off = cpu_to_le16(off);
208 req->r_request->hdr.data_len = cpu_to_le32(*plen);
209 op->payload_len = cpu_to_le32(*plen);
211 op->extent.truncate_size = cpu_to_le64(truncate_size);
212 op->extent.truncate_seq = cpu_to_le32(truncate_seq);
215 head->object_len = cpu_to_le32(req->r_oid_len);
216 memcpy(p, req->r_oid, req->r_oid_len);
221 op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
224 head->snap_seq = cpu_to_le64(snapc->seq);
225 head->num_snaps = cpu_to_le32(snapc->num_snaps);
226 for (i = 0; i < snapc->num_snaps; i++) {
227 put_unaligned_le64(snapc->snaps[i], p);
232 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
233 msg_size = p - msg->front.iov_base;
234 msg->front.iov_len = msg_size;
235 msg->hdr.front_len = cpu_to_le32(msg_size);
240 * We keep osd requests in an rbtree, sorted by ->r_tid.
242 static void __insert_request(struct ceph_osd_client *osdc,
243 struct ceph_osd_request *new)
245 struct rb_node **p = &osdc->requests.rb_node;
246 struct rb_node *parent = NULL;
247 struct ceph_osd_request *req = NULL;
251 req = rb_entry(parent, struct ceph_osd_request, r_node);
252 if (new->r_tid < req->r_tid)
254 else if (new->r_tid > req->r_tid)
260 rb_link_node(&new->r_node, parent, p);
261 rb_insert_color(&new->r_node, &osdc->requests);
264 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
267 struct ceph_osd_request *req;
268 struct rb_node *n = osdc->requests.rb_node;
271 req = rb_entry(n, struct ceph_osd_request, r_node);
272 if (tid < req->r_tid)
274 else if (tid > req->r_tid)
282 static struct ceph_osd_request *
283 __lookup_request_ge(struct ceph_osd_client *osdc,
286 struct ceph_osd_request *req;
287 struct rb_node *n = osdc->requests.rb_node;
290 req = rb_entry(n, struct ceph_osd_request, r_node);
291 if (tid < req->r_tid) {
295 } else if (tid > req->r_tid) {
306 * If the osd connection drops, we need to resubmit all requests.
308 static void osd_reset(struct ceph_connection *con)
310 struct ceph_osd *osd = con->private;
311 struct ceph_osd_client *osdc;
315 dout("osd_reset osd%d\n", osd->o_osd);
317 down_read(&osdc->map_sem);
318 kick_requests(osdc, osd);
319 up_read(&osdc->map_sem);
323 * Track open sessions with osds.
325 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
327 struct ceph_osd *osd;
329 osd = kzalloc(sizeof(*osd), GFP_NOFS);
333 atomic_set(&osd->o_ref, 1);
335 INIT_LIST_HEAD(&osd->o_requests);
336 INIT_LIST_HEAD(&osd->o_osd_lru);
337 osd->o_incarnation = 1;
339 ceph_con_init(osdc->client->msgr, &osd->o_con);
340 osd->o_con.private = osd;
341 osd->o_con.ops = &osd_con_ops;
342 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
344 INIT_LIST_HEAD(&osd->o_keepalive_item);
348 static struct ceph_osd *get_osd(struct ceph_osd *osd)
350 if (atomic_inc_not_zero(&osd->o_ref)) {
351 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
352 atomic_read(&osd->o_ref));
355 dout("get_osd %p FAIL\n", osd);
360 static void put_osd(struct ceph_osd *osd)
362 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
363 atomic_read(&osd->o_ref) - 1);
364 if (atomic_dec_and_test(&osd->o_ref))
369 * remove an osd from our map
371 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
373 dout("__remove_osd %p\n", osd);
374 BUG_ON(!list_empty(&osd->o_requests));
375 rb_erase(&osd->o_node, &osdc->osds);
376 list_del_init(&osd->o_osd_lru);
377 ceph_con_close(&osd->o_con);
381 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
382 struct ceph_osd *osd)
384 dout("__move_osd_to_lru %p\n", osd);
385 BUG_ON(!list_empty(&osd->o_osd_lru));
386 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
387 osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
390 static void __remove_osd_from_lru(struct ceph_osd *osd)
392 dout("__remove_osd_from_lru %p\n", osd);
393 if (!list_empty(&osd->o_osd_lru))
394 list_del_init(&osd->o_osd_lru);
397 static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
399 struct ceph_osd *osd, *nosd;
401 dout("__remove_old_osds %p\n", osdc);
402 mutex_lock(&osdc->request_mutex);
403 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
404 if (!remove_all && time_before(jiffies, osd->lru_ttl))
406 __remove_osd(osdc, osd);
408 mutex_unlock(&osdc->request_mutex);
414 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
418 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
419 if (list_empty(&osd->o_requests)) {
420 __remove_osd(osdc, osd);
422 ceph_con_close(&osd->o_con);
423 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
424 osd->o_incarnation++;
429 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
431 struct rb_node **p = &osdc->osds.rb_node;
432 struct rb_node *parent = NULL;
433 struct ceph_osd *osd = NULL;
437 osd = rb_entry(parent, struct ceph_osd, o_node);
438 if (new->o_osd < osd->o_osd)
440 else if (new->o_osd > osd->o_osd)
446 rb_link_node(&new->o_node, parent, p);
447 rb_insert_color(&new->o_node, &osdc->osds);
450 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
452 struct ceph_osd *osd;
453 struct rb_node *n = osdc->osds.rb_node;
456 osd = rb_entry(n, struct ceph_osd, o_node);
459 else if (o > osd->o_osd)
467 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
469 schedule_delayed_work(&osdc->timeout_work,
470 osdc->client->mount_args->osd_keepalive_timeout * HZ);
473 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
475 cancel_delayed_work(&osdc->timeout_work);
479 * Register request, assign tid. If this is the first request, set up
482 static void register_request(struct ceph_osd_client *osdc,
483 struct ceph_osd_request *req)
485 mutex_lock(&osdc->request_mutex);
486 req->r_tid = ++osdc->last_tid;
487 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
488 INIT_LIST_HEAD(&req->r_req_lru_item);
490 dout("register_request %p tid %lld\n", req, req->r_tid);
491 __insert_request(osdc, req);
492 ceph_osdc_get_request(req);
493 osdc->num_requests++;
495 if (osdc->num_requests == 1) {
496 dout(" first request, scheduling timeout\n");
497 __schedule_osd_timeout(osdc);
499 mutex_unlock(&osdc->request_mutex);
503 * called under osdc->request_mutex
505 static void __unregister_request(struct ceph_osd_client *osdc,
506 struct ceph_osd_request *req)
508 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
509 rb_erase(&req->r_node, &osdc->requests);
510 osdc->num_requests--;
513 /* make sure the original request isn't in flight. */
514 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
516 list_del_init(&req->r_osd_item);
517 if (list_empty(&req->r_osd->o_requests))
518 __move_osd_to_lru(osdc, req->r_osd);
522 ceph_osdc_put_request(req);
524 list_del_init(&req->r_req_lru_item);
525 if (osdc->num_requests == 0) {
526 dout(" no requests, canceling timeout\n");
527 __cancel_osd_timeout(osdc);
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);
540 list_del_init(&req->r_req_lru_item);
544 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
545 * (as needed), and set the request r_osd appropriately. If there is
546 * no up osd, set r_osd to NULL.
548 * Return 0 if unchanged, 1 if changed, or negative on error.
550 * Caller should hold map_sem for read and request_mutex.
552 static int __map_osds(struct ceph_osd_client *osdc,
553 struct ceph_osd_request *req)
555 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
560 dout("map_osds %p tid %lld\n", req, req->r_tid);
561 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
562 &req->r_file_layout, osdc->osdmap);
565 pgid = reqhead->layout.ol_pgid;
568 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
570 if ((req->r_osd && req->r_osd->o_osd == o &&
571 req->r_sent >= req->r_osd->o_incarnation) ||
572 (req->r_osd == NULL && o == -1))
573 return 0; /* no change */
575 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
576 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
577 req->r_osd ? req->r_osd->o_osd : -1);
580 __cancel_request(req);
581 list_del_init(&req->r_osd_item);
585 req->r_osd = __lookup_osd(osdc, o);
586 if (!req->r_osd && o >= 0) {
588 req->r_osd = create_osd(osdc);
592 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
593 req->r_osd->o_osd = o;
594 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
595 __insert_osd(osdc, req->r_osd);
597 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
601 __remove_osd_from_lru(req->r_osd);
602 list_add(&req->r_osd_item, &req->r_osd->o_requests);
604 err = 1; /* osd changed */
611 * caller should hold map_sem (for read) and request_mutex
613 static int __send_request(struct ceph_osd_client *osdc,
614 struct ceph_osd_request *req)
616 struct ceph_osd_request_head *reqhead;
619 err = __map_osds(osdc, req);
622 if (req->r_osd == NULL) {
623 dout("send_request %p no up osds in pg\n", req);
624 ceph_monc_request_next_osdmap(&osdc->client->monc);
628 dout("send_request %p tid %llu to osd%d flags %d\n",
629 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
631 reqhead = req->r_request->front.iov_base;
632 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
633 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
634 reqhead->reassert_version = req->r_reassert_version;
636 req->r_sent_stamp = jiffies;
637 list_move_tail(&osdc->req_lru, &req->r_req_lru_item);
639 ceph_msg_get(req->r_request); /* send consumes a ref */
640 ceph_con_send(&req->r_osd->o_con, req->r_request);
641 req->r_sent = req->r_osd->o_incarnation;
646 * Timeout callback, called every N seconds when 1 or more osd
647 * requests has been active for more than N seconds. When this
648 * happens, we ping all OSDs with requests who have timed out to
649 * ensure any communications channel reset is detected. Reset the
650 * request timeouts another N seconds in the future as we go.
651 * Reschedule the timeout event another N seconds in future (unless
652 * there are no open requests).
654 static void handle_timeout(struct work_struct *work)
656 struct ceph_osd_client *osdc =
657 container_of(work, struct ceph_osd_client, timeout_work.work);
658 struct ceph_osd_request *req, *last_req = NULL;
659 struct ceph_osd *osd;
660 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
661 unsigned long keepalive =
662 osdc->client->mount_args->osd_keepalive_timeout * HZ;
663 unsigned long last_sent = 0;
665 struct list_head slow_osds;
668 down_read(&osdc->map_sem);
670 ceph_monc_request_next_osdmap(&osdc->client->monc);
672 mutex_lock(&osdc->request_mutex);
673 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
674 req = rb_entry(p, struct ceph_osd_request, r_node);
679 dout("osdc resending prev failed %lld\n", req->r_tid);
680 err = __send_request(osdc, req);
682 dout("osdc failed again on %lld\n", req->r_tid);
684 req->r_resend = false;
690 * reset osds that appear to be _really_ unresponsive. this
691 * is a failsafe measure.. we really shouldn't be getting to
692 * this point if the system is working properly. the monitors
693 * should mark the osd as failed and we should find out about
694 * it from an updated osd map.
696 while (!list_empty(&osdc->req_lru)) {
697 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
700 if (time_before(jiffies, req->r_sent_stamp + timeout))
703 BUG_ON(req == last_req && req->r_sent_stamp == last_sent);
705 last_sent = req->r_sent_stamp;
709 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
710 req->r_tid, osd->o_osd);
711 __kick_requests(osdc, osd);
715 * ping osds that are a bit slow. this ensures that if there
716 * is a break in the TCP connection we will notice, and reopen
717 * a connection with that osd (from the fault callback).
719 INIT_LIST_HEAD(&slow_osds);
720 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
721 if (time_before(jiffies, req->r_sent_stamp + keepalive))
726 dout(" tid %llu is slow, will send keepalive on osd%d\n",
727 req->r_tid, osd->o_osd);
728 list_move_tail(&osd->o_keepalive_item, &slow_osds);
730 while (!list_empty(&slow_osds)) {
731 osd = list_entry(slow_osds.next, struct ceph_osd,
733 list_del_init(&osd->o_keepalive_item);
734 ceph_con_keepalive(&osd->o_con);
737 __schedule_osd_timeout(osdc);
738 mutex_unlock(&osdc->request_mutex);
740 up_read(&osdc->map_sem);
743 static void handle_osds_timeout(struct work_struct *work)
745 struct ceph_osd_client *osdc =
746 container_of(work, struct ceph_osd_client,
747 osds_timeout_work.work);
748 unsigned long delay =
749 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
751 dout("osds timeout\n");
752 down_read(&osdc->map_sem);
753 remove_old_osds(osdc, 0);
754 up_read(&osdc->map_sem);
756 schedule_delayed_work(&osdc->osds_timeout_work,
757 round_jiffies_relative(delay));
761 * handle osd op reply. either call the callback if it is specified,
762 * or do the completion to wake up the waiting thread.
764 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
765 struct ceph_connection *con)
767 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
768 struct ceph_osd_request *req;
770 int numops, object_len, flags;
772 tid = le64_to_cpu(msg->hdr.tid);
773 if (msg->front.iov_len < sizeof(*rhead))
775 numops = le32_to_cpu(rhead->num_ops);
776 object_len = le32_to_cpu(rhead->object_len);
777 if (msg->front.iov_len != sizeof(*rhead) + object_len +
778 numops * sizeof(struct ceph_osd_op))
780 dout("handle_reply %p tid %llu\n", msg, tid);
783 mutex_lock(&osdc->request_mutex);
784 req = __lookup_request(osdc, tid);
786 dout("handle_reply tid %llu dne\n", tid);
787 mutex_unlock(&osdc->request_mutex);
790 ceph_osdc_get_request(req);
791 flags = le32_to_cpu(rhead->flags);
794 * if this connection filled our message, drop our reference now, to
795 * avoid a (safe but slower) revoke later.
797 if (req->r_con_filling_msg == con && req->r_reply == msg) {
798 dout(" dropping con_filling_msg ref %p\n", con);
799 req->r_con_filling_msg = NULL;
803 if (!req->r_got_reply) {
806 req->r_result = le32_to_cpu(rhead->result);
807 bytes = le32_to_cpu(msg->hdr.data_len);
808 dout("handle_reply result %d bytes %d\n", req->r_result,
810 if (req->r_result == 0)
811 req->r_result = bytes;
813 /* in case this is a write and we need to replay, */
814 req->r_reassert_version = rhead->reassert_version;
816 req->r_got_reply = 1;
817 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
818 dout("handle_reply tid %llu dup ack\n", tid);
819 mutex_unlock(&osdc->request_mutex);
823 dout("handle_reply tid %llu flags %d\n", tid, flags);
825 /* either this is a read, or we got the safe response */
826 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
827 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
828 __unregister_request(osdc, req);
830 mutex_unlock(&osdc->request_mutex);
833 req->r_callback(req, msg);
835 complete(&req->r_completion);
837 if (flags & CEPH_OSD_FLAG_ONDISK) {
838 if (req->r_safe_callback)
839 req->r_safe_callback(req, msg);
840 complete(&req->r_safe_completion); /* fsync waiter */
844 ceph_osdc_put_request(req);
848 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
849 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
850 (int)sizeof(*rhead));
855 static int __kick_requests(struct ceph_osd_client *osdc,
856 struct ceph_osd *kickosd)
858 struct ceph_osd_request *req;
859 struct rb_node *p, *n;
863 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
865 __reset_osd(osdc, kickosd);
867 for (p = rb_first(&osdc->osds); p; p = n) {
868 struct ceph_osd *osd =
869 rb_entry(p, struct ceph_osd, o_node);
872 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
873 memcmp(&osd->o_con.peer_addr,
874 ceph_osd_addr(osdc->osdmap,
876 sizeof(struct ceph_entity_addr)) != 0)
877 __reset_osd(osdc, osd);
881 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
882 req = rb_entry(p, struct ceph_osd_request, r_node);
885 dout(" r_resend set on tid %llu\n", req->r_tid);
886 __cancel_request(req);
889 if (req->r_osd && kickosd == req->r_osd) {
890 __cancel_request(req);
894 err = __map_osds(osdc, req);
896 continue; /* no change */
899 * FIXME: really, we should set the request
900 * error and fail if this isn't a 'nofail'
901 * request, but that's a fair bit more
902 * complicated to do. So retry!
904 dout(" setting r_resend on %llu\n", req->r_tid);
905 req->r_resend = true;
908 if (req->r_osd == NULL) {
909 dout("tid %llu maps to no valid osd\n", req->r_tid);
910 needmap++; /* request a newer map */
915 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
917 req->r_flags |= CEPH_OSD_FLAG_RETRY;
918 err = __send_request(osdc, req);
920 dout(" setting r_resend on %llu\n", req->r_tid);
921 req->r_resend = true;
929 * Resubmit osd requests whose osd or osd address has changed. Request
930 * a new osd map if osds are down, or we are otherwise unable to determine
931 * how to direct a request.
933 * Close connections to down osds.
935 * If @who is specified, resubmit requests for that specific osd.
937 * Caller should hold map_sem for read and request_mutex.
939 static void kick_requests(struct ceph_osd_client *osdc,
940 struct ceph_osd *kickosd)
944 mutex_lock(&osdc->request_mutex);
945 needmap = __kick_requests(osdc, kickosd);
946 mutex_unlock(&osdc->request_mutex);
949 dout("%d requests for down osds, need new map\n", needmap);
950 ceph_monc_request_next_osdmap(&osdc->client->monc);
955 * Process updated osd map.
957 * The message contains any number of incremental and full maps, normally
958 * indicating some sort of topology change in the cluster. Kick requests
959 * off to different OSDs as needed.
961 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
963 void *p, *end, *next;
966 struct ceph_osdmap *newmap = NULL, *oldmap;
968 struct ceph_fsid fsid;
970 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
971 p = msg->front.iov_base;
972 end = p + msg->front.iov_len;
975 ceph_decode_need(&p, end, sizeof(fsid), bad);
976 ceph_decode_copy(&p, &fsid, sizeof(fsid));
977 if (ceph_check_fsid(osdc->client, &fsid) < 0)
980 down_write(&osdc->map_sem);
982 /* incremental maps */
983 ceph_decode_32_safe(&p, end, nr_maps, bad);
984 dout(" %d inc maps\n", nr_maps);
985 while (nr_maps > 0) {
986 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
987 epoch = ceph_decode_32(&p);
988 maplen = ceph_decode_32(&p);
989 ceph_decode_need(&p, end, maplen, bad);
991 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
992 dout("applying incremental map %u len %d\n",
994 newmap = osdmap_apply_incremental(&p, next,
997 if (IS_ERR(newmap)) {
998 err = PTR_ERR(newmap);
1002 if (newmap != osdc->osdmap) {
1003 ceph_osdmap_destroy(osdc->osdmap);
1004 osdc->osdmap = newmap;
1007 dout("ignoring incremental map %u len %d\n",
1017 ceph_decode_32_safe(&p, end, nr_maps, bad);
1018 dout(" %d full maps\n", nr_maps);
1020 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1021 epoch = ceph_decode_32(&p);
1022 maplen = ceph_decode_32(&p);
1023 ceph_decode_need(&p, end, maplen, bad);
1025 dout("skipping non-latest full map %u len %d\n",
1027 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1028 dout("skipping full map %u len %d, "
1029 "older than our %u\n", epoch, maplen,
1030 osdc->osdmap->epoch);
1032 dout("taking full map %u len %d\n", epoch, maplen);
1033 newmap = osdmap_decode(&p, p+maplen);
1034 if (IS_ERR(newmap)) {
1035 err = PTR_ERR(newmap);
1039 oldmap = osdc->osdmap;
1040 osdc->osdmap = newmap;
1042 ceph_osdmap_destroy(oldmap);
1049 downgrade_write(&osdc->map_sem);
1050 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1052 kick_requests(osdc, NULL);
1053 up_read(&osdc->map_sem);
1057 pr_err("osdc handle_map corrupt msg\n");
1059 up_write(&osdc->map_sem);
1065 * A read request prepares specific pages that data is to be read into.
1066 * When a message is being read off the wire, we call prepare_pages to
1068 * 0 = success, -1 failure.
1070 static int __prepare_pages(struct ceph_connection *con,
1071 struct ceph_msg_header *hdr,
1072 struct ceph_osd_request *req,
1076 struct ceph_osd *osd = con->private;
1077 struct ceph_osd_client *osdc;
1079 int data_len = le32_to_cpu(hdr->data_len);
1080 unsigned data_off = le16_to_cpu(hdr->data_off);
1082 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1089 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
1090 tid, req->r_num_pages, want);
1091 if (unlikely(req->r_num_pages < want))
1093 m->pages = req->r_pages;
1094 m->nr_pages = req->r_num_pages;
1095 ret = 0; /* success */
1097 BUG_ON(ret < 0 || m->nr_pages < want);
1103 * Register request, send initial attempt.
1105 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1106 struct ceph_osd_request *req,
1111 req->r_request->pages = req->r_pages;
1112 req->r_request->nr_pages = req->r_num_pages;
1114 register_request(osdc, req);
1116 down_read(&osdc->map_sem);
1117 mutex_lock(&osdc->request_mutex);
1119 * a racing kick_requests() may have sent the message for us
1120 * while we dropped request_mutex above, so only send now if
1121 * the request still han't been touched yet.
1123 if (req->r_sent == 0) {
1124 rc = __send_request(osdc, req);
1127 dout("osdc_start_request failed send, "
1128 " marking %lld\n", req->r_tid);
1129 req->r_resend = true;
1132 __unregister_request(osdc, req);
1136 mutex_unlock(&osdc->request_mutex);
1137 up_read(&osdc->map_sem);
1142 * wait for a request to complete
1144 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1145 struct ceph_osd_request *req)
1149 rc = wait_for_completion_interruptible(&req->r_completion);
1151 mutex_lock(&osdc->request_mutex);
1152 __cancel_request(req);
1153 __unregister_request(osdc, req);
1154 mutex_unlock(&osdc->request_mutex);
1155 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1159 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1160 return req->r_result;
1164 * sync - wait for all in-flight requests to flush. avoid starvation.
1166 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1168 struct ceph_osd_request *req;
1169 u64 last_tid, next_tid = 0;
1171 mutex_lock(&osdc->request_mutex);
1172 last_tid = osdc->last_tid;
1174 req = __lookup_request_ge(osdc, next_tid);
1177 if (req->r_tid > last_tid)
1180 next_tid = req->r_tid + 1;
1181 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1184 ceph_osdc_get_request(req);
1185 mutex_unlock(&osdc->request_mutex);
1186 dout("sync waiting on tid %llu (last is %llu)\n",
1187 req->r_tid, last_tid);
1188 wait_for_completion(&req->r_safe_completion);
1189 mutex_lock(&osdc->request_mutex);
1190 ceph_osdc_put_request(req);
1192 mutex_unlock(&osdc->request_mutex);
1193 dout("sync done (thru tid %llu)\n", last_tid);
1199 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1204 osdc->client = client;
1205 osdc->osdmap = NULL;
1206 init_rwsem(&osdc->map_sem);
1207 init_completion(&osdc->map_waiters);
1208 osdc->last_requested_map = 0;
1209 mutex_init(&osdc->request_mutex);
1211 osdc->osds = RB_ROOT;
1212 INIT_LIST_HEAD(&osdc->osd_lru);
1213 osdc->requests = RB_ROOT;
1214 INIT_LIST_HEAD(&osdc->req_lru);
1215 osdc->num_requests = 0;
1216 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1217 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1219 schedule_delayed_work(&osdc->osds_timeout_work,
1220 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
1223 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1224 sizeof(struct ceph_osd_request));
1225 if (!osdc->req_mempool)
1228 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true);
1231 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1232 OSD_OPREPLY_FRONT_LEN, 10, true);
1238 ceph_msgpool_destroy(&osdc->msgpool_op);
1240 mempool_destroy(osdc->req_mempool);
1245 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1247 cancel_delayed_work_sync(&osdc->timeout_work);
1248 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1250 ceph_osdmap_destroy(osdc->osdmap);
1251 osdc->osdmap = NULL;
1253 remove_old_osds(osdc, 1);
1254 mempool_destroy(osdc->req_mempool);
1255 ceph_msgpool_destroy(&osdc->msgpool_op);
1256 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1260 * Read some contiguous pages. If we cross a stripe boundary, shorten
1261 * *plen. Return number of bytes read, or error.
1263 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1264 struct ceph_vino vino, struct ceph_file_layout *layout,
1266 u32 truncate_seq, u64 truncate_size,
1267 struct page **pages, int num_pages)
1269 struct ceph_osd_request *req;
1272 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1273 vino.snap, off, *plen);
1274 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1275 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1276 NULL, 0, truncate_seq, truncate_size, NULL,
1279 return PTR_ERR(req);
1281 /* it may be a short read due to an object boundary */
1282 req->r_pages = pages;
1283 num_pages = calc_pages_for(off, *plen);
1284 req->r_num_pages = num_pages;
1286 dout("readpages final extent is %llu~%llu (%d pages)\n",
1287 off, *plen, req->r_num_pages);
1289 rc = ceph_osdc_start_request(osdc, req, false);
1291 rc = ceph_osdc_wait_request(osdc, req);
1293 ceph_osdc_put_request(req);
1294 dout("readpages result %d\n", rc);
1299 * do a synchronous write on N pages
1301 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1302 struct ceph_file_layout *layout,
1303 struct ceph_snap_context *snapc,
1305 u32 truncate_seq, u64 truncate_size,
1306 struct timespec *mtime,
1307 struct page **pages, int num_pages,
1308 int flags, int do_sync, bool nofail)
1310 struct ceph_osd_request *req;
1313 BUG_ON(vino.snap != CEPH_NOSNAP);
1314 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1316 flags | CEPH_OSD_FLAG_ONDISK |
1317 CEPH_OSD_FLAG_WRITE,
1319 truncate_seq, truncate_size, mtime,
1322 return PTR_ERR(req);
1324 /* it may be a short write due to an object boundary */
1325 req->r_pages = pages;
1326 req->r_num_pages = calc_pages_for(off, len);
1327 dout("writepages %llu~%llu (%d pages)\n", off, len,
1330 rc = ceph_osdc_start_request(osdc, req, nofail);
1332 rc = ceph_osdc_wait_request(osdc, req);
1334 ceph_osdc_put_request(req);
1337 dout("writepages result %d\n", rc);
1342 * handle incoming message
1344 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1346 struct ceph_osd *osd = con->private;
1347 struct ceph_osd_client *osdc;
1348 int type = le16_to_cpu(msg->hdr.type);
1355 case CEPH_MSG_OSD_MAP:
1356 ceph_osdc_handle_map(osdc, msg);
1358 case CEPH_MSG_OSD_OPREPLY:
1359 handle_reply(osdc, msg, con);
1363 pr_err("received unknown message type %d %s\n", type,
1364 ceph_msg_type_name(type));
1370 * lookup and return message for incoming reply
1372 static struct ceph_msg *get_reply(struct ceph_connection *con,
1373 struct ceph_msg_header *hdr,
1376 struct ceph_osd *osd = con->private;
1377 struct ceph_osd_client *osdc = osd->o_osdc;
1379 struct ceph_osd_request *req;
1380 int front = le32_to_cpu(hdr->front_len);
1381 int data_len = le32_to_cpu(hdr->data_len);
1385 tid = le64_to_cpu(hdr->tid);
1386 mutex_lock(&osdc->request_mutex);
1387 req = __lookup_request(osdc, tid);
1391 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
1396 if (req->r_con_filling_msg) {
1397 dout("get_reply revoking msg %p from old con %p\n",
1398 req->r_reply, req->r_con_filling_msg);
1399 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1400 ceph_con_put(req->r_con_filling_msg);
1403 if (front > req->r_reply->front.iov_len) {
1404 pr_warning("get_reply front %d > preallocated %d\n",
1405 front, (int)req->r_reply->front.iov_len);
1406 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL);
1409 ceph_msg_put(req->r_reply);
1412 m = ceph_msg_get(req->r_reply);
1415 err = __prepare_pages(con, hdr, req, tid, m);
1423 req->r_con_filling_msg = ceph_con_get(con);
1424 dout("get_reply tid %lld %p\n", tid, m);
1427 mutex_unlock(&osdc->request_mutex);
1432 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1433 struct ceph_msg_header *hdr,
1436 struct ceph_osd *osd = con->private;
1437 int type = le16_to_cpu(hdr->type);
1438 int front = le32_to_cpu(hdr->front_len);
1441 case CEPH_MSG_OSD_MAP:
1442 return ceph_msg_new(type, front, 0, 0, NULL);
1443 case CEPH_MSG_OSD_OPREPLY:
1444 return get_reply(con, hdr, skip);
1446 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1454 * Wrappers to refcount containing ceph_osd struct
1456 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1458 struct ceph_osd *osd = con->private;
1464 static void put_osd_con(struct ceph_connection *con)
1466 struct ceph_osd *osd = con->private;
1473 static int get_authorizer(struct ceph_connection *con,
1474 void **buf, int *len, int *proto,
1475 void **reply_buf, int *reply_len, int force_new)
1477 struct ceph_osd *o = con->private;
1478 struct ceph_osd_client *osdc = o->o_osdc;
1479 struct ceph_auth_client *ac = osdc->client->monc.auth;
1482 if (force_new && o->o_authorizer) {
1483 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1484 o->o_authorizer = NULL;
1486 if (o->o_authorizer == NULL) {
1487 ret = ac->ops->create_authorizer(
1488 ac, CEPH_ENTITY_TYPE_OSD,
1490 &o->o_authorizer_buf,
1491 &o->o_authorizer_buf_len,
1492 &o->o_authorizer_reply_buf,
1493 &o->o_authorizer_reply_buf_len);
1498 *proto = ac->protocol;
1499 *buf = o->o_authorizer_buf;
1500 *len = o->o_authorizer_buf_len;
1501 *reply_buf = o->o_authorizer_reply_buf;
1502 *reply_len = o->o_authorizer_reply_buf_len;
1507 static int verify_authorizer_reply(struct ceph_connection *con, int len)
1509 struct ceph_osd *o = con->private;
1510 struct ceph_osd_client *osdc = o->o_osdc;
1511 struct ceph_auth_client *ac = osdc->client->monc.auth;
1513 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1516 static int invalidate_authorizer(struct ceph_connection *con)
1518 struct ceph_osd *o = con->private;
1519 struct ceph_osd_client *osdc = o->o_osdc;
1520 struct ceph_auth_client *ac = osdc->client->monc.auth;
1522 if (ac->ops->invalidate_authorizer)
1523 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1525 return ceph_monc_validate_auth(&osdc->client->monc);
1528 const static struct ceph_connection_operations osd_con_ops = {
1531 .dispatch = dispatch,
1532 .get_authorizer = get_authorizer,
1533 .verify_authorizer_reply = verify_authorizer_reply,
1534 .invalidate_authorizer = invalidate_authorizer,
1535 .alloc_msg = alloc_msg,