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 do_trunc = truncate_seq && (off + *plen > truncate_size);
203 int num_op = 1 + do_sync + do_trunc;
204 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
209 req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
210 memset(req, 0, sizeof(*req));
212 req = kzalloc(sizeof(*req), GFP_NOFS);
215 return ERR_PTR(-ENOMEM);
217 err = alloc_replies(req, num_reply);
219 ceph_osdc_put_request(req);
220 return ERR_PTR(-ENOMEM);
222 req->r_num_prealloc_reply = num_reply;
225 req->r_mempool = use_mempool;
226 kref_init(&req->r_kref);
227 init_completion(&req->r_completion);
228 init_completion(&req->r_safe_completion);
229 INIT_LIST_HEAD(&req->r_unsafe_item);
230 req->r_flags = flags;
232 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
234 /* create message; allow space for oid */
237 msg_size += sizeof(u64) * snapc->num_snaps;
239 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
241 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
243 ceph_osdc_put_request(req);
244 return ERR_PTR(PTR_ERR(msg));
246 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
247 memset(msg->front.iov_base, 0, msg->front.iov_len);
248 head = msg->front.iov_base;
249 op = (void *)(head + 1);
250 p = (void *)(op + num_op);
252 req->r_request = msg;
253 req->r_snapc = ceph_get_snap_context(snapc);
255 head->client_inc = cpu_to_le32(1); /* always, for now. */
256 head->flags = cpu_to_le32(flags);
257 if (flags & CEPH_OSD_FLAG_WRITE)
258 ceph_encode_timespec(&head->mtime, mtime);
259 head->num_ops = cpu_to_le16(num_op);
260 op->op = cpu_to_le16(opcode);
262 /* calculate max write size */
263 calc_layout(osdc, vino, layout, off, plen, req);
264 req->r_file_layout = *layout; /* keep a copy */
266 if (flags & CEPH_OSD_FLAG_WRITE) {
267 req->r_request->hdr.data_off = cpu_to_le16(off);
268 req->r_request->hdr.data_len = cpu_to_le32(*plen);
269 op->payload_len = cpu_to_le32(*plen);
273 head->object_len = cpu_to_le32(req->r_oid_len);
274 memcpy(p, req->r_oid, req->r_oid_len);
280 op->op = cpu_to_le16(opcode == CEPH_OSD_OP_READ ?
281 CEPH_OSD_OP_MASKTRUNC : CEPH_OSD_OP_SETTRUNC);
282 op->trunc.truncate_seq = cpu_to_le32(truncate_seq);
283 prevofs = le64_to_cpu((op-1)->extent.offset);
284 op->trunc.truncate_size = cpu_to_le64(truncate_size -
289 op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
292 head->snap_seq = cpu_to_le64(snapc->seq);
293 head->num_snaps = cpu_to_le32(snapc->num_snaps);
294 for (i = 0; i < snapc->num_snaps; i++) {
295 put_unaligned_le64(snapc->snaps[i], p);
300 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
305 * We keep osd requests in an rbtree, sorted by ->r_tid.
307 static void __insert_request(struct ceph_osd_client *osdc,
308 struct ceph_osd_request *new)
310 struct rb_node **p = &osdc->requests.rb_node;
311 struct rb_node *parent = NULL;
312 struct ceph_osd_request *req = NULL;
316 req = rb_entry(parent, struct ceph_osd_request, r_node);
317 if (new->r_tid < req->r_tid)
319 else if (new->r_tid > req->r_tid)
325 rb_link_node(&new->r_node, parent, p);
326 rb_insert_color(&new->r_node, &osdc->requests);
329 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
332 struct ceph_osd_request *req;
333 struct rb_node *n = osdc->requests.rb_node;
336 req = rb_entry(n, struct ceph_osd_request, r_node);
337 if (tid < req->r_tid)
339 else if (tid > req->r_tid)
347 static struct ceph_osd_request *
348 __lookup_request_ge(struct ceph_osd_client *osdc,
351 struct ceph_osd_request *req;
352 struct rb_node *n = osdc->requests.rb_node;
355 req = rb_entry(n, struct ceph_osd_request, r_node);
356 if (tid < req->r_tid) {
360 } else if (tid > req->r_tid) {
371 * If the osd connection drops, we need to resubmit all requests.
373 static void osd_reset(struct ceph_connection *con)
375 struct ceph_osd *osd = con->private;
376 struct ceph_osd_client *osdc;
380 dout("osd_reset osd%d\n", osd->o_osd);
382 osd->o_incarnation++;
383 down_read(&osdc->map_sem);
384 kick_requests(osdc, osd);
385 up_read(&osdc->map_sem);
389 * Track open sessions with osds.
391 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
393 struct ceph_osd *osd;
395 osd = kzalloc(sizeof(*osd), GFP_NOFS);
399 atomic_set(&osd->o_ref, 1);
401 INIT_LIST_HEAD(&osd->o_requests);
402 osd->o_incarnation = 1;
404 ceph_con_init(osdc->client->msgr, &osd->o_con);
405 osd->o_con.private = osd;
406 osd->o_con.ops = &osd_con_ops;
407 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
412 static struct ceph_osd *get_osd(struct ceph_osd *osd)
414 if (atomic_inc_not_zero(&osd->o_ref)) {
415 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
416 atomic_read(&osd->o_ref));
419 dout("get_osd %p FAIL\n", osd);
424 static void put_osd(struct ceph_osd *osd)
426 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
427 atomic_read(&osd->o_ref) - 1);
428 if (atomic_dec_and_test(&osd->o_ref))
433 * remove an osd from our map
435 static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
437 dout("remove_osd %p\n", osd);
438 BUG_ON(!list_empty(&osd->o_requests));
439 rb_erase(&osd->o_node, &osdc->osds);
440 ceph_con_close(&osd->o_con);
447 static int reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
451 dout("reset_osd %p osd%d\n", osd, osd->o_osd);
452 if (list_empty(&osd->o_requests)) {
453 remove_osd(osdc, osd);
455 ceph_con_close(&osd->o_con);
456 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
457 osd->o_incarnation++;
462 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
464 struct rb_node **p = &osdc->osds.rb_node;
465 struct rb_node *parent = NULL;
466 struct ceph_osd *osd = NULL;
470 osd = rb_entry(parent, struct ceph_osd, o_node);
471 if (new->o_osd < osd->o_osd)
473 else if (new->o_osd > osd->o_osd)
479 rb_link_node(&new->o_node, parent, p);
480 rb_insert_color(&new->o_node, &osdc->osds);
483 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
485 struct ceph_osd *osd;
486 struct rb_node *n = osdc->osds.rb_node;
489 osd = rb_entry(n, struct ceph_osd, o_node);
492 else if (o > osd->o_osd)
502 * Register request, assign tid. If this is the first request, set up
505 static void register_request(struct ceph_osd_client *osdc,
506 struct ceph_osd_request *req)
508 mutex_lock(&osdc->request_mutex);
509 req->r_tid = ++osdc->last_tid;
510 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
512 dout("register_request %p tid %lld\n", req, req->r_tid);
513 __insert_request(osdc, req);
514 ceph_osdc_get_request(req);
515 osdc->num_requests++;
517 req->r_timeout_stamp =
518 jiffies + osdc->client->mount_args->osd_timeout*HZ;
520 if (osdc->num_requests == 1) {
521 osdc->timeout_tid = req->r_tid;
522 dout(" timeout on tid %llu at %lu\n", req->r_tid,
523 req->r_timeout_stamp);
524 schedule_delayed_work(&osdc->timeout_work,
525 round_jiffies_relative(req->r_timeout_stamp - jiffies));
527 mutex_unlock(&osdc->request_mutex);
531 * called under osdc->request_mutex
533 static void __unregister_request(struct ceph_osd_client *osdc,
534 struct ceph_osd_request *req)
536 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
537 rb_erase(&req->r_node, &osdc->requests);
538 osdc->num_requests--;
541 /* make sure the original request isn't in flight. */
542 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
544 list_del_init(&req->r_osd_item);
545 if (list_empty(&req->r_osd->o_requests))
546 remove_osd(osdc, req->r_osd);
550 ceph_osdc_put_request(req);
552 if (req->r_tid == osdc->timeout_tid) {
553 if (osdc->num_requests == 0) {
554 dout("no requests, canceling timeout\n");
555 osdc->timeout_tid = 0;
556 cancel_delayed_work(&osdc->timeout_work);
558 req = rb_entry(rb_first(&osdc->requests),
559 struct ceph_osd_request, r_node);
560 osdc->timeout_tid = req->r_tid;
561 dout("rescheduled timeout on tid %llu at %lu\n",
562 req->r_tid, req->r_timeout_stamp);
563 schedule_delayed_work(&osdc->timeout_work,
564 round_jiffies_relative(req->r_timeout_stamp -
571 * Cancel a previously queued request message
573 static void __cancel_request(struct ceph_osd_request *req)
576 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
582 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
583 * (as needed), and set the request r_osd appropriately. If there is
584 * no up osd, set r_osd to NULL.
586 * Return 0 if unchanged, 1 if changed, or negative on error.
588 * Caller should hold map_sem for read and request_mutex.
590 static int __map_osds(struct ceph_osd_client *osdc,
591 struct ceph_osd_request *req)
593 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
597 struct ceph_osd *newosd = NULL;
599 dout("map_osds %p tid %lld\n", req, req->r_tid);
600 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
601 &req->r_file_layout, osdc->osdmap);
604 pgid = reqhead->layout.ol_pgid;
607 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
609 if ((req->r_osd && req->r_osd->o_osd == o &&
610 req->r_sent >= req->r_osd->o_incarnation) ||
611 (req->r_osd == NULL && o == -1))
612 return 0; /* no change */
614 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
615 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
616 req->r_osd ? req->r_osd->o_osd : -1);
619 __cancel_request(req);
620 list_del_init(&req->r_osd_item);
621 if (list_empty(&req->r_osd->o_requests)) {
622 /* try to re-use r_osd if possible */
623 newosd = get_osd(req->r_osd);
624 remove_osd(osdc, newosd);
629 req->r_osd = __lookup_osd(osdc, o);
630 if (!req->r_osd && o >= 0) {
636 req->r_osd = create_osd(osdc);
641 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
642 req->r_osd->o_osd = o;
643 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
644 __insert_osd(osdc, req->r_osd);
646 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
650 list_add(&req->r_osd_item, &req->r_osd->o_requests);
651 err = 1; /* osd changed */
660 * caller should hold map_sem (for read) and request_mutex
662 static int __send_request(struct ceph_osd_client *osdc,
663 struct ceph_osd_request *req)
665 struct ceph_osd_request_head *reqhead;
668 err = __map_osds(osdc, req);
671 if (req->r_osd == NULL) {
672 dout("send_request %p no up osds in pg\n", req);
673 ceph_monc_request_next_osdmap(&osdc->client->monc);
677 dout("send_request %p tid %llu to osd%d flags %d\n",
678 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
680 reqhead = req->r_request->front.iov_base;
681 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
682 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
683 reqhead->reassert_version = req->r_reassert_version;
685 req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
687 ceph_msg_get(req->r_request); /* send consumes a ref */
688 ceph_con_send(&req->r_osd->o_con, req->r_request);
689 req->r_sent = req->r_osd->o_incarnation;
694 * Timeout callback, called every N seconds when 1 or more osd
695 * requests has been active for more than N seconds. When this
696 * happens, we ping all OSDs with requests who have timed out to
697 * ensure any communications channel reset is detected. Reset the
698 * request timeouts another N seconds in the future as we go.
699 * Reschedule the timeout event another N seconds in future (unless
700 * there are no open requests).
702 static void handle_timeout(struct work_struct *work)
704 struct ceph_osd_client *osdc =
705 container_of(work, struct ceph_osd_client, timeout_work.work);
706 struct ceph_osd_request *req;
707 struct ceph_osd *osd;
708 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
709 unsigned long next_timeout = timeout + jiffies;
713 down_read(&osdc->map_sem);
715 ceph_monc_request_next_osdmap(&osdc->client->monc);
717 mutex_lock(&osdc->request_mutex);
718 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
719 req = rb_entry(p, struct ceph_osd_request, r_node);
724 dout("osdc resending prev failed %lld\n", req->r_tid);
725 err = __send_request(osdc, req);
727 dout("osdc failed again on %lld\n", req->r_tid);
729 req->r_resend = false;
733 for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
734 osd = rb_entry(p, struct ceph_osd, o_node);
735 if (list_empty(&osd->o_requests))
737 req = list_first_entry(&osd->o_requests,
738 struct ceph_osd_request, r_osd_item);
739 if (time_before(jiffies, req->r_timeout_stamp))
742 dout(" tid %llu (at least) timed out on osd%d\n",
743 req->r_tid, osd->o_osd);
744 req->r_timeout_stamp = next_timeout;
745 ceph_con_keepalive(&osd->o_con);
748 if (osdc->timeout_tid)
749 schedule_delayed_work(&osdc->timeout_work,
750 round_jiffies_relative(timeout));
752 mutex_unlock(&osdc->request_mutex);
754 up_read(&osdc->map_sem);
758 * handle osd op reply. either call the callback if it is specified,
759 * or do the completion to wake up the waiting thread.
761 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
762 struct ceph_connection *con)
764 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
765 struct ceph_osd_request *req;
767 int numops, object_len, flags;
769 tid = le64_to_cpu(msg->hdr.tid);
770 if (msg->front.iov_len < sizeof(*rhead))
772 numops = le32_to_cpu(rhead->num_ops);
773 object_len = le32_to_cpu(rhead->object_len);
774 if (msg->front.iov_len != sizeof(*rhead) + object_len +
775 numops * sizeof(struct ceph_osd_op))
777 dout("handle_reply %p tid %llu\n", msg, tid);
780 mutex_lock(&osdc->request_mutex);
781 req = __lookup_request(osdc, tid);
783 dout("handle_reply tid %llu dne\n", tid);
784 mutex_unlock(&osdc->request_mutex);
787 ceph_osdc_get_request(req);
788 flags = le32_to_cpu(rhead->flags);
791 * if this connection filled our message, drop our reference now, to
792 * avoid a (safe but slower) revoke later.
794 if (req->r_con_filling_msg == con && req->r_reply == msg) {
795 dout(" got pages, dropping con_filling_msg ref %p\n", con);
796 req->r_con_filling_msg = NULL;
802 * once we see the message has been received, we don't
803 * need a ref (which is only needed for revoking
806 ceph_msg_put(req->r_reply);
810 if (!req->r_got_reply) {
813 req->r_result = le32_to_cpu(rhead->result);
814 bytes = le32_to_cpu(msg->hdr.data_len);
815 dout("handle_reply result %d bytes %d\n", req->r_result,
817 if (req->r_result == 0)
818 req->r_result = bytes;
820 /* in case this is a write and we need to replay, */
821 req->r_reassert_version = rhead->reassert_version;
823 req->r_got_reply = 1;
824 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
825 dout("handle_reply tid %llu dup ack\n", tid);
826 mutex_unlock(&osdc->request_mutex);
830 dout("handle_reply tid %llu flags %d\n", tid, flags);
832 /* either this is a read, or we got the safe response */
833 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
834 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
835 __unregister_request(osdc, req);
837 mutex_unlock(&osdc->request_mutex);
840 req->r_callback(req, msg);
842 complete(&req->r_completion);
844 if (flags & CEPH_OSD_FLAG_ONDISK) {
845 if (req->r_safe_callback)
846 req->r_safe_callback(req, msg);
847 complete(&req->r_safe_completion); /* fsync waiter */
851 ceph_osdc_put_request(req);
855 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
856 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
857 (int)sizeof(*rhead));
863 * Resubmit osd requests whose osd or osd address has changed. Request
864 * a new osd map if osds are down, or we are otherwise unable to determine
865 * how to direct a request.
867 * Close connections to down osds.
869 * If @who is specified, resubmit requests for that specific osd.
871 * Caller should hold map_sem for read and request_mutex.
873 static void kick_requests(struct ceph_osd_client *osdc,
874 struct ceph_osd *kickosd)
876 struct ceph_osd_request *req;
877 struct rb_node *p, *n;
881 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
882 mutex_lock(&osdc->request_mutex);
884 for (p = rb_first(&osdc->osds); p; p = n) {
885 struct ceph_osd *osd =
886 rb_entry(p, struct ceph_osd, o_node);
889 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
890 memcmp(&osd->o_con.peer_addr,
891 ceph_osd_addr(osdc->osdmap,
893 sizeof(struct ceph_entity_addr)) != 0)
894 reset_osd(osdc, osd);
898 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
899 req = rb_entry(p, struct ceph_osd_request, r_node);
902 dout(" r_resend set on tid %llu\n", req->r_tid);
903 __cancel_request(req);
906 if (req->r_osd && kickosd == req->r_osd) {
907 __cancel_request(req);
911 err = __map_osds(osdc, req);
913 continue; /* no change */
916 * FIXME: really, we should set the request
917 * error and fail if this isn't a 'nofail'
918 * request, but that's a fair bit more
919 * complicated to do. So retry!
921 dout(" setting r_resend on %llu\n", req->r_tid);
922 req->r_resend = true;
925 if (req->r_osd == NULL) {
926 dout("tid %llu maps to no valid osd\n", req->r_tid);
927 needmap++; /* request a newer map */
932 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
934 req->r_flags |= CEPH_OSD_FLAG_RETRY;
935 err = __send_request(osdc, req);
937 dout(" setting r_resend on %llu\n", req->r_tid);
938 req->r_resend = true;
941 mutex_unlock(&osdc->request_mutex);
944 dout("%d requests for down osds, need new map\n", needmap);
945 ceph_monc_request_next_osdmap(&osdc->client->monc);
950 * Process updated osd map.
952 * The message contains any number of incremental and full maps, normally
953 * indicating some sort of topology change in the cluster. Kick requests
954 * off to different OSDs as needed.
956 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
958 void *p, *end, *next;
961 struct ceph_osdmap *newmap = NULL, *oldmap;
963 struct ceph_fsid fsid;
965 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
966 p = msg->front.iov_base;
967 end = p + msg->front.iov_len;
970 ceph_decode_need(&p, end, sizeof(fsid), bad);
971 ceph_decode_copy(&p, &fsid, sizeof(fsid));
972 if (ceph_check_fsid(osdc->client, &fsid) < 0)
975 down_write(&osdc->map_sem);
977 /* incremental maps */
978 ceph_decode_32_safe(&p, end, nr_maps, bad);
979 dout(" %d inc maps\n", nr_maps);
980 while (nr_maps > 0) {
981 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
982 epoch = ceph_decode_32(&p);
983 maplen = ceph_decode_32(&p);
984 ceph_decode_need(&p, end, maplen, bad);
986 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
987 dout("applying incremental map %u len %d\n",
989 newmap = osdmap_apply_incremental(&p, next,
992 if (IS_ERR(newmap)) {
993 err = PTR_ERR(newmap);
997 if (newmap != osdc->osdmap) {
998 ceph_osdmap_destroy(osdc->osdmap);
999 osdc->osdmap = newmap;
1002 dout("ignoring incremental map %u len %d\n",
1012 ceph_decode_32_safe(&p, end, nr_maps, bad);
1013 dout(" %d full maps\n", nr_maps);
1015 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1016 epoch = ceph_decode_32(&p);
1017 maplen = ceph_decode_32(&p);
1018 ceph_decode_need(&p, end, maplen, bad);
1020 dout("skipping non-latest full map %u len %d\n",
1022 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1023 dout("skipping full map %u len %d, "
1024 "older than our %u\n", epoch, maplen,
1025 osdc->osdmap->epoch);
1027 dout("taking full map %u len %d\n", epoch, maplen);
1028 newmap = osdmap_decode(&p, p+maplen);
1029 if (IS_ERR(newmap)) {
1030 err = PTR_ERR(newmap);
1034 oldmap = osdc->osdmap;
1035 osdc->osdmap = newmap;
1037 ceph_osdmap_destroy(oldmap);
1044 downgrade_write(&osdc->map_sem);
1045 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1047 kick_requests(osdc, NULL);
1048 up_read(&osdc->map_sem);
1052 pr_err("osdc handle_map corrupt msg\n");
1054 up_write(&osdc->map_sem);
1060 * A read request prepares specific pages that data is to be read into.
1061 * When a message is being read off the wire, we call prepare_pages to
1063 * 0 = success, -1 failure.
1065 static int __prepare_pages(struct ceph_connection *con,
1066 struct ceph_msg_header *hdr,
1067 struct ceph_osd_request *req,
1071 struct ceph_osd *osd = con->private;
1072 struct ceph_osd_client *osdc;
1074 int data_len = le32_to_cpu(hdr->data_len);
1075 unsigned data_off = le16_to_cpu(hdr->data_off);
1077 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1084 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
1085 tid, req->r_num_pages, want);
1086 if (unlikely(req->r_num_pages < want))
1088 m->pages = req->r_pages;
1089 m->nr_pages = req->r_num_pages;
1090 ret = 0; /* success */
1092 BUG_ON(ret < 0 || m->nr_pages < want);
1098 * Register request, send initial attempt.
1100 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1101 struct ceph_osd_request *req,
1106 req->r_request->pages = req->r_pages;
1107 req->r_request->nr_pages = req->r_num_pages;
1109 register_request(osdc, req);
1111 down_read(&osdc->map_sem);
1112 mutex_lock(&osdc->request_mutex);
1114 * a racing kick_requests() may have sent the message for us
1115 * while we dropped request_mutex above, so only send now if
1116 * the request still han't been touched yet.
1118 if (req->r_sent == 0) {
1119 rc = __send_request(osdc, req);
1122 dout("osdc_start_request failed send, "
1123 " marking %lld\n", req->r_tid);
1124 req->r_resend = true;
1127 __unregister_request(osdc, req);
1131 mutex_unlock(&osdc->request_mutex);
1132 up_read(&osdc->map_sem);
1137 * wait for a request to complete
1139 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1140 struct ceph_osd_request *req)
1144 rc = wait_for_completion_interruptible(&req->r_completion);
1146 mutex_lock(&osdc->request_mutex);
1147 __cancel_request(req);
1148 __unregister_request(osdc, req);
1149 mutex_unlock(&osdc->request_mutex);
1150 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1154 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1155 return req->r_result;
1159 * sync - wait for all in-flight requests to flush. avoid starvation.
1161 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1163 struct ceph_osd_request *req;
1164 u64 last_tid, next_tid = 0;
1166 mutex_lock(&osdc->request_mutex);
1167 last_tid = osdc->last_tid;
1169 req = __lookup_request_ge(osdc, next_tid);
1172 if (req->r_tid > last_tid)
1175 next_tid = req->r_tid + 1;
1176 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1179 ceph_osdc_get_request(req);
1180 mutex_unlock(&osdc->request_mutex);
1181 dout("sync waiting on tid %llu (last is %llu)\n",
1182 req->r_tid, last_tid);
1183 wait_for_completion(&req->r_safe_completion);
1184 mutex_lock(&osdc->request_mutex);
1185 ceph_osdc_put_request(req);
1187 mutex_unlock(&osdc->request_mutex);
1188 dout("sync done (thru tid %llu)\n", last_tid);
1194 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1199 osdc->client = client;
1200 osdc->osdmap = NULL;
1201 init_rwsem(&osdc->map_sem);
1202 init_completion(&osdc->map_waiters);
1203 osdc->last_requested_map = 0;
1204 mutex_init(&osdc->request_mutex);
1205 osdc->timeout_tid = 0;
1207 osdc->osds = RB_ROOT;
1208 osdc->requests = RB_ROOT;
1209 osdc->num_requests = 0;
1210 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1213 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1214 sizeof(struct ceph_osd_request));
1215 if (!osdc->req_mempool)
1218 err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
1224 mempool_destroy(osdc->req_mempool);
1229 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1231 cancel_delayed_work_sync(&osdc->timeout_work);
1233 ceph_osdmap_destroy(osdc->osdmap);
1234 osdc->osdmap = NULL;
1236 mempool_destroy(osdc->req_mempool);
1237 ceph_msgpool_destroy(&osdc->msgpool_op);
1241 * Read some contiguous pages. If we cross a stripe boundary, shorten
1242 * *plen. Return number of bytes read, or error.
1244 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1245 struct ceph_vino vino, struct ceph_file_layout *layout,
1247 u32 truncate_seq, u64 truncate_size,
1248 struct page **pages, int num_pages)
1250 struct ceph_osd_request *req;
1253 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1254 vino.snap, off, *plen);
1255 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1256 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1257 NULL, 0, truncate_seq, truncate_size, NULL,
1260 return PTR_ERR(req);
1262 /* it may be a short read due to an object boundary */
1263 req->r_pages = pages;
1264 num_pages = calc_pages_for(off, *plen);
1265 req->r_num_pages = num_pages;
1267 dout("readpages final extent is %llu~%llu (%d pages)\n",
1268 off, *plen, req->r_num_pages);
1270 rc = ceph_osdc_start_request(osdc, req, false);
1272 rc = ceph_osdc_wait_request(osdc, req);
1274 ceph_osdc_put_request(req);
1275 dout("readpages result %d\n", rc);
1280 * do a synchronous write on N pages
1282 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1283 struct ceph_file_layout *layout,
1284 struct ceph_snap_context *snapc,
1286 u32 truncate_seq, u64 truncate_size,
1287 struct timespec *mtime,
1288 struct page **pages, int num_pages,
1289 int flags, int do_sync, bool nofail)
1291 struct ceph_osd_request *req;
1294 BUG_ON(vino.snap != CEPH_NOSNAP);
1295 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1297 flags | CEPH_OSD_FLAG_ONDISK |
1298 CEPH_OSD_FLAG_WRITE,
1300 truncate_seq, truncate_size, mtime,
1303 return PTR_ERR(req);
1305 /* it may be a short write due to an object boundary */
1306 req->r_pages = pages;
1307 req->r_num_pages = calc_pages_for(off, len);
1308 dout("writepages %llu~%llu (%d pages)\n", off, len,
1311 rc = ceph_osdc_start_request(osdc, req, nofail);
1313 rc = ceph_osdc_wait_request(osdc, req);
1315 ceph_osdc_put_request(req);
1318 dout("writepages result %d\n", rc);
1323 * handle incoming message
1325 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1327 struct ceph_osd *osd = con->private;
1328 struct ceph_osd_client *osdc;
1329 int type = le16_to_cpu(msg->hdr.type);
1336 case CEPH_MSG_OSD_MAP:
1337 ceph_osdc_handle_map(osdc, msg);
1339 case CEPH_MSG_OSD_OPREPLY:
1340 handle_reply(osdc, msg, con);
1344 pr_err("received unknown message type %d %s\n", type,
1345 ceph_msg_type_name(type));
1350 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1351 struct ceph_msg_header *hdr,
1354 struct ceph_osd *osd = con->private;
1355 struct ceph_osd_client *osdc = osd->o_osdc;
1356 int type = le16_to_cpu(hdr->type);
1357 int front = le32_to_cpu(hdr->front_len);
1358 int data_len = le32_to_cpu(hdr->data_len);
1360 struct ceph_osd_request *req;
1365 if (type != CEPH_MSG_OSD_OPREPLY)
1368 tid = le64_to_cpu(hdr->tid);
1369 mutex_lock(&osdc->request_mutex);
1370 req = __lookup_request(osdc, tid);
1374 dout("alloc_msg unknown tid %llu\n", tid);
1377 m = __get_next_reply(con, req, front);
1378 if (!m || IS_ERR(m)) {
1384 err = __prepare_pages(con, hdr, req, tid, m);
1393 mutex_unlock(&osdc->request_mutex);
1399 * Wrappers to refcount containing ceph_osd struct
1401 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1403 struct ceph_osd *osd = con->private;
1409 static void put_osd_con(struct ceph_connection *con)
1411 struct ceph_osd *osd = con->private;
1418 static int get_authorizer(struct ceph_connection *con,
1419 void **buf, int *len, int *proto,
1420 void **reply_buf, int *reply_len, int force_new)
1422 struct ceph_osd *o = con->private;
1423 struct ceph_osd_client *osdc = o->o_osdc;
1424 struct ceph_auth_client *ac = osdc->client->monc.auth;
1427 if (force_new && o->o_authorizer) {
1428 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1429 o->o_authorizer = NULL;
1431 if (o->o_authorizer == NULL) {
1432 ret = ac->ops->create_authorizer(
1433 ac, CEPH_ENTITY_TYPE_OSD,
1435 &o->o_authorizer_buf,
1436 &o->o_authorizer_buf_len,
1437 &o->o_authorizer_reply_buf,
1438 &o->o_authorizer_reply_buf_len);
1443 *proto = ac->protocol;
1444 *buf = o->o_authorizer_buf;
1445 *len = o->o_authorizer_buf_len;
1446 *reply_buf = o->o_authorizer_reply_buf;
1447 *reply_len = o->o_authorizer_reply_buf_len;
1452 static int verify_authorizer_reply(struct ceph_connection *con, int len)
1454 struct ceph_osd *o = con->private;
1455 struct ceph_osd_client *osdc = o->o_osdc;
1456 struct ceph_auth_client *ac = osdc->client->monc.auth;
1458 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1462 const static struct ceph_connection_operations osd_con_ops = {
1465 .dispatch = dispatch,
1466 .get_authorizer = get_authorizer,
1467 .verify_authorizer_reply = verify_authorizer_reply,
1468 .alloc_msg = alloc_msg,