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)
416 struct ceph_osd_request *req;
419 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
420 if (list_empty(&osd->o_requests)) {
421 __remove_osd(osdc, osd);
422 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
423 &osd->o_con.peer_addr,
424 sizeof(osd->o_con.peer_addr)) == 0 &&
425 !ceph_con_opened(&osd->o_con)) {
426 dout(" osd addr hasn't changed and connection never opened,"
427 " letting msgr retry");
428 /* touch each r_stamp for handle_timeout()'s benfit */
429 list_for_each_entry(req, &osd->o_requests, r_osd_item)
430 req->r_stamp = jiffies;
433 ceph_con_close(&osd->o_con);
434 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
435 osd->o_incarnation++;
440 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
442 struct rb_node **p = &osdc->osds.rb_node;
443 struct rb_node *parent = NULL;
444 struct ceph_osd *osd = NULL;
448 osd = rb_entry(parent, struct ceph_osd, o_node);
449 if (new->o_osd < osd->o_osd)
451 else if (new->o_osd > osd->o_osd)
457 rb_link_node(&new->o_node, parent, p);
458 rb_insert_color(&new->o_node, &osdc->osds);
461 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
463 struct ceph_osd *osd;
464 struct rb_node *n = osdc->osds.rb_node;
467 osd = rb_entry(n, struct ceph_osd, o_node);
470 else if (o > osd->o_osd)
478 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
480 schedule_delayed_work(&osdc->timeout_work,
481 osdc->client->mount_args->osd_keepalive_timeout * HZ);
484 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
486 cancel_delayed_work(&osdc->timeout_work);
490 * Register request, assign tid. If this is the first request, set up
493 static void register_request(struct ceph_osd_client *osdc,
494 struct ceph_osd_request *req)
496 mutex_lock(&osdc->request_mutex);
497 req->r_tid = ++osdc->last_tid;
498 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
499 INIT_LIST_HEAD(&req->r_req_lru_item);
501 dout("register_request %p tid %lld\n", req, req->r_tid);
502 __insert_request(osdc, req);
503 ceph_osdc_get_request(req);
504 osdc->num_requests++;
506 if (osdc->num_requests == 1) {
507 dout(" first request, scheduling timeout\n");
508 __schedule_osd_timeout(osdc);
510 mutex_unlock(&osdc->request_mutex);
514 * called under osdc->request_mutex
516 static void __unregister_request(struct ceph_osd_client *osdc,
517 struct ceph_osd_request *req)
519 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
520 rb_erase(&req->r_node, &osdc->requests);
521 osdc->num_requests--;
524 /* make sure the original request isn't in flight. */
525 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
527 list_del_init(&req->r_osd_item);
528 if (list_empty(&req->r_osd->o_requests))
529 __move_osd_to_lru(osdc, req->r_osd);
533 ceph_osdc_put_request(req);
535 list_del_init(&req->r_req_lru_item);
536 if (osdc->num_requests == 0) {
537 dout(" no requests, canceling timeout\n");
538 __cancel_osd_timeout(osdc);
543 * Cancel a previously queued request message
545 static void __cancel_request(struct ceph_osd_request *req)
548 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
551 list_del_init(&req->r_req_lru_item);
555 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
556 * (as needed), and set the request r_osd appropriately. If there is
557 * no up osd, set r_osd to NULL.
559 * Return 0 if unchanged, 1 if changed, or negative on error.
561 * Caller should hold map_sem for read and request_mutex.
563 static int __map_osds(struct ceph_osd_client *osdc,
564 struct ceph_osd_request *req)
566 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
571 dout("map_osds %p tid %lld\n", req, req->r_tid);
572 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
573 &req->r_file_layout, osdc->osdmap);
576 pgid = reqhead->layout.ol_pgid;
579 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
581 if ((req->r_osd && req->r_osd->o_osd == o &&
582 req->r_sent >= req->r_osd->o_incarnation) ||
583 (req->r_osd == NULL && o == -1))
584 return 0; /* no change */
586 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
587 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
588 req->r_osd ? req->r_osd->o_osd : -1);
591 __cancel_request(req);
592 list_del_init(&req->r_osd_item);
596 req->r_osd = __lookup_osd(osdc, o);
597 if (!req->r_osd && o >= 0) {
599 req->r_osd = create_osd(osdc);
603 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
604 req->r_osd->o_osd = o;
605 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
606 __insert_osd(osdc, req->r_osd);
608 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
612 __remove_osd_from_lru(req->r_osd);
613 list_add(&req->r_osd_item, &req->r_osd->o_requests);
615 err = 1; /* osd changed */
622 * caller should hold map_sem (for read) and request_mutex
624 static int __send_request(struct ceph_osd_client *osdc,
625 struct ceph_osd_request *req)
627 struct ceph_osd_request_head *reqhead;
630 err = __map_osds(osdc, req);
633 if (req->r_osd == NULL) {
634 dout("send_request %p no up osds in pg\n", req);
635 ceph_monc_request_next_osdmap(&osdc->client->monc);
639 dout("send_request %p tid %llu to osd%d flags %d\n",
640 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
642 reqhead = req->r_request->front.iov_base;
643 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
644 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
645 reqhead->reassert_version = req->r_reassert_version;
647 req->r_stamp = jiffies;
648 list_move_tail(&osdc->req_lru, &req->r_req_lru_item);
650 ceph_msg_get(req->r_request); /* send consumes a ref */
651 ceph_con_send(&req->r_osd->o_con, req->r_request);
652 req->r_sent = req->r_osd->o_incarnation;
657 * Timeout callback, called every N seconds when 1 or more osd
658 * requests has been active for more than N seconds. When this
659 * happens, we ping all OSDs with requests who have timed out to
660 * ensure any communications channel reset is detected. Reset the
661 * request timeouts another N seconds in the future as we go.
662 * Reschedule the timeout event another N seconds in future (unless
663 * there are no open requests).
665 static void handle_timeout(struct work_struct *work)
667 struct ceph_osd_client *osdc =
668 container_of(work, struct ceph_osd_client, timeout_work.work);
669 struct ceph_osd_request *req, *last_req = NULL;
670 struct ceph_osd *osd;
671 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
672 unsigned long keepalive =
673 osdc->client->mount_args->osd_keepalive_timeout * HZ;
674 unsigned long last_stamp = 0;
676 struct list_head slow_osds;
679 down_read(&osdc->map_sem);
681 ceph_monc_request_next_osdmap(&osdc->client->monc);
683 mutex_lock(&osdc->request_mutex);
684 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
685 req = rb_entry(p, struct ceph_osd_request, r_node);
690 dout("osdc resending prev failed %lld\n", req->r_tid);
691 err = __send_request(osdc, req);
693 dout("osdc failed again on %lld\n", req->r_tid);
695 req->r_resend = false;
701 * reset osds that appear to be _really_ unresponsive. this
702 * is a failsafe measure.. we really shouldn't be getting to
703 * this point if the system is working properly. the monitors
704 * should mark the osd as failed and we should find out about
705 * it from an updated osd map.
707 while (!list_empty(&osdc->req_lru)) {
708 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
711 if (time_before(jiffies, req->r_stamp + timeout))
714 BUG_ON(req == last_req && req->r_stamp == last_stamp);
716 last_stamp = req->r_stamp;
720 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
721 req->r_tid, osd->o_osd);
722 __kick_requests(osdc, osd);
726 * ping osds that are a bit slow. this ensures that if there
727 * is a break in the TCP connection we will notice, and reopen
728 * a connection with that osd (from the fault callback).
730 INIT_LIST_HEAD(&slow_osds);
731 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
732 if (time_before(jiffies, req->r_stamp + keepalive))
737 dout(" tid %llu is slow, will send keepalive on osd%d\n",
738 req->r_tid, osd->o_osd);
739 list_move_tail(&osd->o_keepalive_item, &slow_osds);
741 while (!list_empty(&slow_osds)) {
742 osd = list_entry(slow_osds.next, struct ceph_osd,
744 list_del_init(&osd->o_keepalive_item);
745 ceph_con_keepalive(&osd->o_con);
748 __schedule_osd_timeout(osdc);
749 mutex_unlock(&osdc->request_mutex);
751 up_read(&osdc->map_sem);
754 static void handle_osds_timeout(struct work_struct *work)
756 struct ceph_osd_client *osdc =
757 container_of(work, struct ceph_osd_client,
758 osds_timeout_work.work);
759 unsigned long delay =
760 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
762 dout("osds timeout\n");
763 down_read(&osdc->map_sem);
764 remove_old_osds(osdc, 0);
765 up_read(&osdc->map_sem);
767 schedule_delayed_work(&osdc->osds_timeout_work,
768 round_jiffies_relative(delay));
772 * handle osd op reply. either call the callback if it is specified,
773 * or do the completion to wake up the waiting thread.
775 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
776 struct ceph_connection *con)
778 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
779 struct ceph_osd_request *req;
781 int numops, object_len, flags;
783 tid = le64_to_cpu(msg->hdr.tid);
784 if (msg->front.iov_len < sizeof(*rhead))
786 numops = le32_to_cpu(rhead->num_ops);
787 object_len = le32_to_cpu(rhead->object_len);
788 if (msg->front.iov_len != sizeof(*rhead) + object_len +
789 numops * sizeof(struct ceph_osd_op))
791 dout("handle_reply %p tid %llu\n", msg, tid);
794 mutex_lock(&osdc->request_mutex);
795 req = __lookup_request(osdc, tid);
797 dout("handle_reply tid %llu dne\n", tid);
798 mutex_unlock(&osdc->request_mutex);
801 ceph_osdc_get_request(req);
802 flags = le32_to_cpu(rhead->flags);
805 * if this connection filled our message, drop our reference now, to
806 * avoid a (safe but slower) revoke later.
808 if (req->r_con_filling_msg == con && req->r_reply == msg) {
809 dout(" dropping con_filling_msg ref %p\n", con);
810 req->r_con_filling_msg = NULL;
814 if (!req->r_got_reply) {
817 req->r_result = le32_to_cpu(rhead->result);
818 bytes = le32_to_cpu(msg->hdr.data_len);
819 dout("handle_reply result %d bytes %d\n", req->r_result,
821 if (req->r_result == 0)
822 req->r_result = bytes;
824 /* in case this is a write and we need to replay, */
825 req->r_reassert_version = rhead->reassert_version;
827 req->r_got_reply = 1;
828 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
829 dout("handle_reply tid %llu dup ack\n", tid);
830 mutex_unlock(&osdc->request_mutex);
834 dout("handle_reply tid %llu flags %d\n", tid, flags);
836 /* either this is a read, or we got the safe response */
837 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
838 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
839 __unregister_request(osdc, req);
841 mutex_unlock(&osdc->request_mutex);
844 req->r_callback(req, msg);
846 complete(&req->r_completion);
848 if (flags & CEPH_OSD_FLAG_ONDISK) {
849 if (req->r_safe_callback)
850 req->r_safe_callback(req, msg);
851 complete(&req->r_safe_completion); /* fsync waiter */
855 ceph_osdc_put_request(req);
859 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
860 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
861 (int)sizeof(*rhead));
866 static int __kick_requests(struct ceph_osd_client *osdc,
867 struct ceph_osd *kickosd)
869 struct ceph_osd_request *req;
870 struct rb_node *p, *n;
874 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
876 err = __reset_osd(osdc, kickosd);
880 for (p = rb_first(&osdc->osds); p; p = n) {
881 struct ceph_osd *osd =
882 rb_entry(p, struct ceph_osd, o_node);
885 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
886 memcmp(&osd->o_con.peer_addr,
887 ceph_osd_addr(osdc->osdmap,
889 sizeof(struct ceph_entity_addr)) != 0)
890 __reset_osd(osdc, osd);
894 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
895 req = rb_entry(p, struct ceph_osd_request, r_node);
898 dout(" r_resend set on tid %llu\n", req->r_tid);
899 __cancel_request(req);
902 if (req->r_osd && kickosd == req->r_osd) {
903 __cancel_request(req);
907 err = __map_osds(osdc, req);
909 continue; /* no change */
912 * FIXME: really, we should set the request
913 * error and fail if this isn't a 'nofail'
914 * request, but that's a fair bit more
915 * complicated to do. So retry!
917 dout(" setting r_resend on %llu\n", req->r_tid);
918 req->r_resend = true;
921 if (req->r_osd == NULL) {
922 dout("tid %llu maps to no valid osd\n", req->r_tid);
923 needmap++; /* request a newer map */
928 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
929 req->r_osd ? req->r_osd->o_osd : -1);
930 req->r_flags |= CEPH_OSD_FLAG_RETRY;
931 err = __send_request(osdc, req);
933 dout(" setting r_resend on %llu\n", req->r_tid);
934 req->r_resend = true;
942 * Resubmit osd requests whose osd or osd address has changed. Request
943 * a new osd map if osds are down, or we are otherwise unable to determine
944 * how to direct a request.
946 * Close connections to down osds.
948 * If @who is specified, resubmit requests for that specific osd.
950 * Caller should hold map_sem for read and request_mutex.
952 static void kick_requests(struct ceph_osd_client *osdc,
953 struct ceph_osd *kickosd)
957 mutex_lock(&osdc->request_mutex);
958 needmap = __kick_requests(osdc, kickosd);
959 mutex_unlock(&osdc->request_mutex);
962 dout("%d requests for down osds, need new map\n", needmap);
963 ceph_monc_request_next_osdmap(&osdc->client->monc);
968 * Process updated osd map.
970 * The message contains any number of incremental and full maps, normally
971 * indicating some sort of topology change in the cluster. Kick requests
972 * off to different OSDs as needed.
974 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
976 void *p, *end, *next;
979 struct ceph_osdmap *newmap = NULL, *oldmap;
981 struct ceph_fsid fsid;
983 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
984 p = msg->front.iov_base;
985 end = p + msg->front.iov_len;
988 ceph_decode_need(&p, end, sizeof(fsid), bad);
989 ceph_decode_copy(&p, &fsid, sizeof(fsid));
990 if (ceph_check_fsid(osdc->client, &fsid) < 0)
993 down_write(&osdc->map_sem);
995 /* incremental maps */
996 ceph_decode_32_safe(&p, end, nr_maps, bad);
997 dout(" %d inc maps\n", nr_maps);
998 while (nr_maps > 0) {
999 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1000 epoch = ceph_decode_32(&p);
1001 maplen = ceph_decode_32(&p);
1002 ceph_decode_need(&p, end, maplen, bad);
1004 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1005 dout("applying incremental map %u len %d\n",
1007 newmap = osdmap_apply_incremental(&p, next,
1009 osdc->client->msgr);
1010 if (IS_ERR(newmap)) {
1011 err = PTR_ERR(newmap);
1015 if (newmap != osdc->osdmap) {
1016 ceph_osdmap_destroy(osdc->osdmap);
1017 osdc->osdmap = newmap;
1020 dout("ignoring incremental map %u len %d\n",
1030 ceph_decode_32_safe(&p, end, nr_maps, bad);
1031 dout(" %d full maps\n", nr_maps);
1033 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1034 epoch = ceph_decode_32(&p);
1035 maplen = ceph_decode_32(&p);
1036 ceph_decode_need(&p, end, maplen, bad);
1038 dout("skipping non-latest full map %u len %d\n",
1040 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1041 dout("skipping full map %u len %d, "
1042 "older than our %u\n", epoch, maplen,
1043 osdc->osdmap->epoch);
1045 dout("taking full map %u len %d\n", epoch, maplen);
1046 newmap = osdmap_decode(&p, p+maplen);
1047 if (IS_ERR(newmap)) {
1048 err = PTR_ERR(newmap);
1052 oldmap = osdc->osdmap;
1053 osdc->osdmap = newmap;
1055 ceph_osdmap_destroy(oldmap);
1062 downgrade_write(&osdc->map_sem);
1063 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1065 kick_requests(osdc, NULL);
1066 up_read(&osdc->map_sem);
1070 pr_err("osdc handle_map corrupt msg\n");
1072 up_write(&osdc->map_sem);
1078 * A read request prepares specific pages that data is to be read into.
1079 * When a message is being read off the wire, we call prepare_pages to
1081 * 0 = success, -1 failure.
1083 static int __prepare_pages(struct ceph_connection *con,
1084 struct ceph_msg_header *hdr,
1085 struct ceph_osd_request *req,
1089 struct ceph_osd *osd = con->private;
1090 struct ceph_osd_client *osdc;
1092 int data_len = le32_to_cpu(hdr->data_len);
1093 unsigned data_off = le16_to_cpu(hdr->data_off);
1095 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1102 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
1103 tid, req->r_num_pages, want);
1104 if (unlikely(req->r_num_pages < want))
1106 m->pages = req->r_pages;
1107 m->nr_pages = req->r_num_pages;
1108 ret = 0; /* success */
1110 BUG_ON(ret < 0 || m->nr_pages < want);
1116 * Register request, send initial attempt.
1118 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1119 struct ceph_osd_request *req,
1124 req->r_request->pages = req->r_pages;
1125 req->r_request->nr_pages = req->r_num_pages;
1127 register_request(osdc, req);
1129 down_read(&osdc->map_sem);
1130 mutex_lock(&osdc->request_mutex);
1132 * a racing kick_requests() may have sent the message for us
1133 * while we dropped request_mutex above, so only send now if
1134 * the request still han't been touched yet.
1136 if (req->r_sent == 0) {
1137 rc = __send_request(osdc, req);
1140 dout("osdc_start_request failed send, "
1141 " marking %lld\n", req->r_tid);
1142 req->r_resend = true;
1145 __unregister_request(osdc, req);
1149 mutex_unlock(&osdc->request_mutex);
1150 up_read(&osdc->map_sem);
1155 * wait for a request to complete
1157 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1158 struct ceph_osd_request *req)
1162 rc = wait_for_completion_interruptible(&req->r_completion);
1164 mutex_lock(&osdc->request_mutex);
1165 __cancel_request(req);
1166 __unregister_request(osdc, req);
1167 mutex_unlock(&osdc->request_mutex);
1168 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1172 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1173 return req->r_result;
1177 * sync - wait for all in-flight requests to flush. avoid starvation.
1179 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1181 struct ceph_osd_request *req;
1182 u64 last_tid, next_tid = 0;
1184 mutex_lock(&osdc->request_mutex);
1185 last_tid = osdc->last_tid;
1187 req = __lookup_request_ge(osdc, next_tid);
1190 if (req->r_tid > last_tid)
1193 next_tid = req->r_tid + 1;
1194 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1197 ceph_osdc_get_request(req);
1198 mutex_unlock(&osdc->request_mutex);
1199 dout("sync waiting on tid %llu (last is %llu)\n",
1200 req->r_tid, last_tid);
1201 wait_for_completion(&req->r_safe_completion);
1202 mutex_lock(&osdc->request_mutex);
1203 ceph_osdc_put_request(req);
1205 mutex_unlock(&osdc->request_mutex);
1206 dout("sync done (thru tid %llu)\n", last_tid);
1212 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1217 osdc->client = client;
1218 osdc->osdmap = NULL;
1219 init_rwsem(&osdc->map_sem);
1220 init_completion(&osdc->map_waiters);
1221 osdc->last_requested_map = 0;
1222 mutex_init(&osdc->request_mutex);
1224 osdc->osds = RB_ROOT;
1225 INIT_LIST_HEAD(&osdc->osd_lru);
1226 osdc->requests = RB_ROOT;
1227 INIT_LIST_HEAD(&osdc->req_lru);
1228 osdc->num_requests = 0;
1229 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1230 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1232 schedule_delayed_work(&osdc->osds_timeout_work,
1233 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
1236 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1237 sizeof(struct ceph_osd_request));
1238 if (!osdc->req_mempool)
1241 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true);
1244 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1245 OSD_OPREPLY_FRONT_LEN, 10, true);
1251 ceph_msgpool_destroy(&osdc->msgpool_op);
1253 mempool_destroy(osdc->req_mempool);
1258 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1260 cancel_delayed_work_sync(&osdc->timeout_work);
1261 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1263 ceph_osdmap_destroy(osdc->osdmap);
1264 osdc->osdmap = NULL;
1266 remove_old_osds(osdc, 1);
1267 mempool_destroy(osdc->req_mempool);
1268 ceph_msgpool_destroy(&osdc->msgpool_op);
1269 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1273 * Read some contiguous pages. If we cross a stripe boundary, shorten
1274 * *plen. Return number of bytes read, or error.
1276 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1277 struct ceph_vino vino, struct ceph_file_layout *layout,
1279 u32 truncate_seq, u64 truncate_size,
1280 struct page **pages, int num_pages)
1282 struct ceph_osd_request *req;
1285 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1286 vino.snap, off, *plen);
1287 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1288 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1289 NULL, 0, truncate_seq, truncate_size, NULL,
1292 return PTR_ERR(req);
1294 /* it may be a short read due to an object boundary */
1295 req->r_pages = pages;
1296 num_pages = calc_pages_for(off, *plen);
1297 req->r_num_pages = num_pages;
1299 dout("readpages final extent is %llu~%llu (%d pages)\n",
1300 off, *plen, req->r_num_pages);
1302 rc = ceph_osdc_start_request(osdc, req, false);
1304 rc = ceph_osdc_wait_request(osdc, req);
1306 ceph_osdc_put_request(req);
1307 dout("readpages result %d\n", rc);
1312 * do a synchronous write on N pages
1314 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1315 struct ceph_file_layout *layout,
1316 struct ceph_snap_context *snapc,
1318 u32 truncate_seq, u64 truncate_size,
1319 struct timespec *mtime,
1320 struct page **pages, int num_pages,
1321 int flags, int do_sync, bool nofail)
1323 struct ceph_osd_request *req;
1326 BUG_ON(vino.snap != CEPH_NOSNAP);
1327 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1329 flags | CEPH_OSD_FLAG_ONDISK |
1330 CEPH_OSD_FLAG_WRITE,
1332 truncate_seq, truncate_size, mtime,
1335 return PTR_ERR(req);
1337 /* it may be a short write due to an object boundary */
1338 req->r_pages = pages;
1339 req->r_num_pages = calc_pages_for(off, len);
1340 dout("writepages %llu~%llu (%d pages)\n", off, len,
1343 rc = ceph_osdc_start_request(osdc, req, nofail);
1345 rc = ceph_osdc_wait_request(osdc, req);
1347 ceph_osdc_put_request(req);
1350 dout("writepages result %d\n", rc);
1355 * handle incoming message
1357 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1359 struct ceph_osd *osd = con->private;
1360 struct ceph_osd_client *osdc;
1361 int type = le16_to_cpu(msg->hdr.type);
1368 case CEPH_MSG_OSD_MAP:
1369 ceph_osdc_handle_map(osdc, msg);
1371 case CEPH_MSG_OSD_OPREPLY:
1372 handle_reply(osdc, msg, con);
1376 pr_err("received unknown message type %d %s\n", type,
1377 ceph_msg_type_name(type));
1383 * lookup and return message for incoming reply
1385 static struct ceph_msg *get_reply(struct ceph_connection *con,
1386 struct ceph_msg_header *hdr,
1389 struct ceph_osd *osd = con->private;
1390 struct ceph_osd_client *osdc = osd->o_osdc;
1392 struct ceph_osd_request *req;
1393 int front = le32_to_cpu(hdr->front_len);
1394 int data_len = le32_to_cpu(hdr->data_len);
1398 tid = le64_to_cpu(hdr->tid);
1399 mutex_lock(&osdc->request_mutex);
1400 req = __lookup_request(osdc, tid);
1404 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
1409 if (req->r_con_filling_msg) {
1410 dout("get_reply revoking msg %p from old con %p\n",
1411 req->r_reply, req->r_con_filling_msg);
1412 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1413 ceph_con_put(req->r_con_filling_msg);
1416 if (front > req->r_reply->front.iov_len) {
1417 pr_warning("get_reply front %d > preallocated %d\n",
1418 front, (int)req->r_reply->front.iov_len);
1419 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL);
1422 ceph_msg_put(req->r_reply);
1425 m = ceph_msg_get(req->r_reply);
1428 err = __prepare_pages(con, hdr, req, tid, m);
1436 req->r_con_filling_msg = ceph_con_get(con);
1437 dout("get_reply tid %lld %p\n", tid, m);
1440 mutex_unlock(&osdc->request_mutex);
1445 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1446 struct ceph_msg_header *hdr,
1449 struct ceph_osd *osd = con->private;
1450 int type = le16_to_cpu(hdr->type);
1451 int front = le32_to_cpu(hdr->front_len);
1454 case CEPH_MSG_OSD_MAP:
1455 return ceph_msg_new(type, front, 0, 0, NULL);
1456 case CEPH_MSG_OSD_OPREPLY:
1457 return get_reply(con, hdr, skip);
1459 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1467 * Wrappers to refcount containing ceph_osd struct
1469 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1471 struct ceph_osd *osd = con->private;
1477 static void put_osd_con(struct ceph_connection *con)
1479 struct ceph_osd *osd = con->private;
1486 static int get_authorizer(struct ceph_connection *con,
1487 void **buf, int *len, int *proto,
1488 void **reply_buf, int *reply_len, int force_new)
1490 struct ceph_osd *o = con->private;
1491 struct ceph_osd_client *osdc = o->o_osdc;
1492 struct ceph_auth_client *ac = osdc->client->monc.auth;
1495 if (force_new && o->o_authorizer) {
1496 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1497 o->o_authorizer = NULL;
1499 if (o->o_authorizer == NULL) {
1500 ret = ac->ops->create_authorizer(
1501 ac, CEPH_ENTITY_TYPE_OSD,
1503 &o->o_authorizer_buf,
1504 &o->o_authorizer_buf_len,
1505 &o->o_authorizer_reply_buf,
1506 &o->o_authorizer_reply_buf_len);
1511 *proto = ac->protocol;
1512 *buf = o->o_authorizer_buf;
1513 *len = o->o_authorizer_buf_len;
1514 *reply_buf = o->o_authorizer_reply_buf;
1515 *reply_len = o->o_authorizer_reply_buf_len;
1520 static int verify_authorizer_reply(struct ceph_connection *con, int len)
1522 struct ceph_osd *o = con->private;
1523 struct ceph_osd_client *osdc = o->o_osdc;
1524 struct ceph_auth_client *ac = osdc->client->monc.auth;
1526 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1529 static int invalidate_authorizer(struct ceph_connection *con)
1531 struct ceph_osd *o = con->private;
1532 struct ceph_osd_client *osdc = o->o_osdc;
1533 struct ceph_auth_client *ac = osdc->client->monc.auth;
1535 if (ac->ops->invalidate_authorizer)
1536 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1538 return ceph_monc_validate_auth(&osdc->client->monc);
1541 const static struct ceph_connection_operations osd_con_ops = {
1544 .dispatch = dispatch,
1545 .get_authorizer = get_authorizer,
1546 .verify_authorizer_reply = verify_authorizer_reply,
1547 .invalidate_authorizer = invalidate_authorizer,
1548 .alloc_msg = alloc_msg,