1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops;
26 static void send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
35 static int op_needs_trail(int op)
38 case CEPH_OSD_OP_GETXATTR:
39 case CEPH_OSD_OP_SETXATTR:
40 case CEPH_OSD_OP_CMPXATTR:
41 case CEPH_OSD_OP_CALL:
42 case CEPH_OSD_OP_NOTIFY:
49 static int op_has_extent(int op)
51 return (op == CEPH_OSD_OP_READ ||
52 op == CEPH_OSD_OP_WRITE);
55 void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
56 struct ceph_file_layout *layout,
58 u64 off, u64 *plen, u64 *bno,
59 struct ceph_osd_request *req,
60 struct ceph_osd_req_op *op)
62 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
64 u64 objoff, objlen; /* extent in object */
66 reqhead->snapid = cpu_to_le64(snapid);
69 ceph_calc_file_object_mapping(layout, off, plen, bno,
72 dout(" skipping last %llu, final file extent %llu~%llu\n",
73 orig_len - *plen, off, *plen);
75 if (op_has_extent(op->op)) {
76 op->extent.offset = objoff;
77 op->extent.length = objlen;
79 req->r_num_pages = calc_pages_for(off, *plen);
80 req->r_page_alignment = off & ~PAGE_MASK;
81 if (op->op == CEPH_OSD_OP_WRITE)
82 op->payload_len = *plen;
84 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
85 *bno, objoff, objlen, req->r_num_pages);
88 EXPORT_SYMBOL(ceph_calc_raw_layout);
91 * Implement client access to distributed object storage cluster.
93 * All data objects are stored within a cluster/cloud of OSDs, or
94 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
95 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
96 * remote daemons serving up and coordinating consistent and safe
99 * Cluster membership and the mapping of data objects onto storage devices
100 * are described by the osd map.
102 * We keep track of pending OSD requests (read, write), resubmit
103 * requests to different OSDs when the cluster topology/data layout
104 * change, or retry the affected requests when the communications
105 * channel with an OSD is reset.
109 * calculate the mapping of a file extent onto an object, and fill out the
110 * request accordingly. shorten extent as necessary if it crosses an
113 * fill osd op in request message.
115 static void calc_layout(struct ceph_osd_client *osdc,
116 struct ceph_vino vino,
117 struct ceph_file_layout *layout,
119 struct ceph_osd_request *req,
120 struct ceph_osd_req_op *op)
124 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
125 plen, &bno, req, op);
127 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
128 req->r_oid_len = strlen(req->r_oid);
134 void ceph_osdc_release_request(struct kref *kref)
136 struct ceph_osd_request *req = container_of(kref,
137 struct ceph_osd_request,
141 ceph_msg_put(req->r_request);
143 ceph_msg_put(req->r_reply);
144 if (req->r_con_filling_msg) {
145 dout("release_request revoking pages %p from con %p\n",
146 req->r_pages, req->r_con_filling_msg);
147 ceph_con_revoke_message(req->r_con_filling_msg,
149 ceph_con_put(req->r_con_filling_msg);
151 if (req->r_own_pages)
152 ceph_release_page_vector(req->r_pages,
158 ceph_put_snap_context(req->r_snapc);
160 ceph_pagelist_release(req->r_trail);
164 mempool_free(req, req->r_osdc->req_mempool);
168 EXPORT_SYMBOL(ceph_osdc_release_request);
170 static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
177 if (needs_trail && op_needs_trail(ops[i].op))
185 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
187 struct ceph_snap_context *snapc,
188 struct ceph_osd_req_op *ops,
194 struct ceph_osd_request *req;
195 struct ceph_msg *msg;
197 int num_op = get_num_ops(ops, &needs_trail);
198 size_t msg_size = sizeof(struct ceph_osd_request_head);
200 msg_size += num_op*sizeof(struct ceph_osd_op);
203 req = mempool_alloc(osdc->req_mempool, gfp_flags);
204 memset(req, 0, sizeof(*req));
206 req = kzalloc(sizeof(*req), gfp_flags);
212 req->r_mempool = use_mempool;
214 kref_init(&req->r_kref);
215 init_completion(&req->r_completion);
216 init_completion(&req->r_safe_completion);
217 INIT_LIST_HEAD(&req->r_unsafe_item);
218 INIT_LIST_HEAD(&req->r_linger_item);
219 INIT_LIST_HEAD(&req->r_linger_osd);
220 INIT_LIST_HEAD(&req->r_req_lru_item);
221 req->r_flags = flags;
223 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
225 /* create reply message */
227 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
229 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
230 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
232 ceph_osdc_put_request(req);
237 /* allocate space for the trailing data */
239 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
241 ceph_osdc_put_request(req);
244 ceph_pagelist_init(req->r_trail);
246 /* create request message; allow space for oid */
247 msg_size += MAX_OBJ_NAME_SIZE;
249 msg_size += sizeof(u64) * snapc->num_snaps;
251 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
253 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
255 ceph_osdc_put_request(req);
259 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
260 memset(msg->front.iov_base, 0, msg->front.iov_len);
262 req->r_request = msg;
263 req->r_pages = pages;
273 EXPORT_SYMBOL(ceph_osdc_alloc_request);
275 static void osd_req_encode_op(struct ceph_osd_request *req,
276 struct ceph_osd_op *dst,
277 struct ceph_osd_req_op *src)
279 dst->op = cpu_to_le16(src->op);
282 case CEPH_OSD_OP_READ:
283 case CEPH_OSD_OP_WRITE:
285 cpu_to_le64(src->extent.offset);
287 cpu_to_le64(src->extent.length);
288 dst->extent.truncate_size =
289 cpu_to_le64(src->extent.truncate_size);
290 dst->extent.truncate_seq =
291 cpu_to_le32(src->extent.truncate_seq);
294 case CEPH_OSD_OP_GETXATTR:
295 case CEPH_OSD_OP_SETXATTR:
296 case CEPH_OSD_OP_CMPXATTR:
297 BUG_ON(!req->r_trail);
299 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
300 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
301 dst->xattr.cmp_op = src->xattr.cmp_op;
302 dst->xattr.cmp_mode = src->xattr.cmp_mode;
303 ceph_pagelist_append(req->r_trail, src->xattr.name,
304 src->xattr.name_len);
305 ceph_pagelist_append(req->r_trail, src->xattr.val,
306 src->xattr.value_len);
308 case CEPH_OSD_OP_CALL:
309 BUG_ON(!req->r_trail);
311 dst->cls.class_len = src->cls.class_len;
312 dst->cls.method_len = src->cls.method_len;
313 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
315 ceph_pagelist_append(req->r_trail, src->cls.class_name,
317 ceph_pagelist_append(req->r_trail, src->cls.method_name,
318 src->cls.method_len);
319 ceph_pagelist_append(req->r_trail, src->cls.indata,
320 src->cls.indata_len);
322 case CEPH_OSD_OP_ROLLBACK:
323 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
325 case CEPH_OSD_OP_STARTSYNC:
327 case CEPH_OSD_OP_NOTIFY:
329 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
330 __le32 timeout = cpu_to_le32(src->watch.timeout);
332 BUG_ON(!req->r_trail);
334 ceph_pagelist_append(req->r_trail,
335 &prot_ver, sizeof(prot_ver));
336 ceph_pagelist_append(req->r_trail,
337 &timeout, sizeof(timeout));
339 case CEPH_OSD_OP_NOTIFY_ACK:
340 case CEPH_OSD_OP_WATCH:
341 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
342 dst->watch.ver = cpu_to_le64(src->watch.ver);
343 dst->watch.flag = src->watch.flag;
346 pr_err("unrecognized osd opcode %d\n", dst->op);
350 dst->payload_len = cpu_to_le32(src->payload_len);
354 * build new request AND message
357 void ceph_osdc_build_request(struct ceph_osd_request *req,
359 struct ceph_osd_req_op *src_ops,
360 struct ceph_snap_context *snapc,
361 struct timespec *mtime,
365 struct ceph_msg *msg = req->r_request;
366 struct ceph_osd_request_head *head;
367 struct ceph_osd_req_op *src_op;
368 struct ceph_osd_op *op;
370 int num_op = get_num_ops(src_ops, NULL);
371 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
372 int flags = req->r_flags;
376 head = msg->front.iov_base;
377 op = (void *)(head + 1);
378 p = (void *)(op + num_op);
380 req->r_snapc = ceph_get_snap_context(snapc);
382 head->client_inc = cpu_to_le32(1); /* always, for now. */
383 head->flags = cpu_to_le32(flags);
384 if (flags & CEPH_OSD_FLAG_WRITE)
385 ceph_encode_timespec(&head->mtime, mtime);
386 head->num_ops = cpu_to_le16(num_op);
390 head->object_len = cpu_to_le32(oid_len);
391 memcpy(p, oid, oid_len);
396 osd_req_encode_op(req, op, src_op);
402 data_len += req->r_trail->length;
405 head->snap_seq = cpu_to_le64(snapc->seq);
406 head->num_snaps = cpu_to_le32(snapc->num_snaps);
407 for (i = 0; i < snapc->num_snaps; i++) {
408 put_unaligned_le64(snapc->snaps[i], p);
413 if (flags & CEPH_OSD_FLAG_WRITE) {
414 req->r_request->hdr.data_off = cpu_to_le16(off);
415 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
416 } else if (data_len) {
417 req->r_request->hdr.data_off = 0;
418 req->r_request->hdr.data_len = cpu_to_le32(data_len);
421 req->r_request->page_alignment = req->r_page_alignment;
423 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
424 msg_size = p - msg->front.iov_base;
425 msg->front.iov_len = msg_size;
426 msg->hdr.front_len = cpu_to_le32(msg_size);
429 EXPORT_SYMBOL(ceph_osdc_build_request);
432 * build new request AND message, calculate layout, and adjust file
435 * if the file was recently truncated, we include information about its
436 * old and new size so that the object can be updated appropriately. (we
437 * avoid synchronously deleting truncated objects because it's slow.)
439 * if @do_sync, include a 'startsync' command so that the osd will flush
442 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
443 struct ceph_file_layout *layout,
444 struct ceph_vino vino,
446 int opcode, int flags,
447 struct ceph_snap_context *snapc,
451 struct timespec *mtime,
452 bool use_mempool, int num_reply,
455 struct ceph_osd_req_op ops[3];
456 struct ceph_osd_request *req;
459 ops[0].extent.truncate_seq = truncate_seq;
460 ops[0].extent.truncate_size = truncate_size;
461 ops[0].payload_len = 0;
464 ops[1].op = CEPH_OSD_OP_STARTSYNC;
465 ops[1].payload_len = 0;
470 req = ceph_osdc_alloc_request(osdc, flags,
473 GFP_NOFS, NULL, NULL);
477 /* calculate max write size */
478 calc_layout(osdc, vino, layout, off, plen, req, ops);
479 req->r_file_layout = *layout; /* keep a copy */
481 /* in case it differs from natural (file) alignment that
482 calc_layout filled in for us */
483 req->r_num_pages = calc_pages_for(page_align, *plen);
484 req->r_page_alignment = page_align;
486 ceph_osdc_build_request(req, off, plen, ops,
489 req->r_oid, req->r_oid_len);
493 EXPORT_SYMBOL(ceph_osdc_new_request);
496 * We keep osd requests in an rbtree, sorted by ->r_tid.
498 static void __insert_request(struct ceph_osd_client *osdc,
499 struct ceph_osd_request *new)
501 struct rb_node **p = &osdc->requests.rb_node;
502 struct rb_node *parent = NULL;
503 struct ceph_osd_request *req = NULL;
507 req = rb_entry(parent, struct ceph_osd_request, r_node);
508 if (new->r_tid < req->r_tid)
510 else if (new->r_tid > req->r_tid)
516 rb_link_node(&new->r_node, parent, p);
517 rb_insert_color(&new->r_node, &osdc->requests);
520 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
523 struct ceph_osd_request *req;
524 struct rb_node *n = osdc->requests.rb_node;
527 req = rb_entry(n, struct ceph_osd_request, r_node);
528 if (tid < req->r_tid)
530 else if (tid > req->r_tid)
538 static struct ceph_osd_request *
539 __lookup_request_ge(struct ceph_osd_client *osdc,
542 struct ceph_osd_request *req;
543 struct rb_node *n = osdc->requests.rb_node;
546 req = rb_entry(n, struct ceph_osd_request, r_node);
547 if (tid < req->r_tid) {
551 } else if (tid > req->r_tid) {
561 * Resubmit requests pending on the given osd.
563 static void __kick_osd_requests(struct ceph_osd_client *osdc,
564 struct ceph_osd *osd)
566 struct ceph_osd_request *req, *nreq;
569 dout("__kick_osd_requests osd%d\n", osd->o_osd);
570 err = __reset_osd(osdc, osd);
574 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
575 list_move(&req->r_req_lru_item, &osdc->req_unsent);
576 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
579 req->r_flags |= CEPH_OSD_FLAG_RETRY;
582 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
585 * reregister request prior to unregistering linger so
586 * that r_osd is preserved.
588 BUG_ON(!list_empty(&req->r_req_lru_item));
589 __register_request(osdc, req);
590 list_add(&req->r_req_lru_item, &osdc->req_unsent);
591 list_add(&req->r_osd_item, &req->r_osd->o_requests);
592 __unregister_linger_request(osdc, req);
593 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
598 static void kick_osd_requests(struct ceph_osd_client *osdc,
599 struct ceph_osd *kickosd)
601 mutex_lock(&osdc->request_mutex);
602 __kick_osd_requests(osdc, kickosd);
603 mutex_unlock(&osdc->request_mutex);
607 * If the osd connection drops, we need to resubmit all requests.
609 static void osd_reset(struct ceph_connection *con)
611 struct ceph_osd *osd = con->private;
612 struct ceph_osd_client *osdc;
616 dout("osd_reset osd%d\n", osd->o_osd);
618 down_read(&osdc->map_sem);
619 kick_osd_requests(osdc, osd);
621 up_read(&osdc->map_sem);
625 * Track open sessions with osds.
627 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
629 struct ceph_osd *osd;
631 osd = kzalloc(sizeof(*osd), GFP_NOFS);
635 atomic_set(&osd->o_ref, 1);
637 INIT_LIST_HEAD(&osd->o_requests);
638 INIT_LIST_HEAD(&osd->o_linger_requests);
639 INIT_LIST_HEAD(&osd->o_osd_lru);
640 osd->o_incarnation = 1;
642 ceph_con_init(osdc->client->msgr, &osd->o_con);
643 osd->o_con.private = osd;
644 osd->o_con.ops = &osd_con_ops;
645 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
647 INIT_LIST_HEAD(&osd->o_keepalive_item);
651 static struct ceph_osd *get_osd(struct ceph_osd *osd)
653 if (atomic_inc_not_zero(&osd->o_ref)) {
654 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
655 atomic_read(&osd->o_ref));
658 dout("get_osd %p FAIL\n", osd);
663 static void put_osd(struct ceph_osd *osd)
665 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
666 atomic_read(&osd->o_ref) - 1);
667 if (atomic_dec_and_test(&osd->o_ref)) {
668 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
670 if (osd->o_authorizer)
671 ac->ops->destroy_authorizer(ac, osd->o_authorizer);
677 * remove an osd from our map
679 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
681 dout("__remove_osd %p\n", osd);
682 BUG_ON(!list_empty(&osd->o_requests));
683 rb_erase(&osd->o_node, &osdc->osds);
684 list_del_init(&osd->o_osd_lru);
685 ceph_con_close(&osd->o_con);
689 static void remove_all_osds(struct ceph_osd_client *osdc)
691 dout("__remove_old_osds %p\n", osdc);
692 mutex_lock(&osdc->request_mutex);
693 while (!RB_EMPTY_ROOT(&osdc->osds)) {
694 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
695 struct ceph_osd, o_node);
696 __remove_osd(osdc, osd);
698 mutex_unlock(&osdc->request_mutex);
701 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
702 struct ceph_osd *osd)
704 dout("__move_osd_to_lru %p\n", osd);
705 BUG_ON(!list_empty(&osd->o_osd_lru));
706 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
707 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
710 static void __remove_osd_from_lru(struct ceph_osd *osd)
712 dout("__remove_osd_from_lru %p\n", osd);
713 if (!list_empty(&osd->o_osd_lru))
714 list_del_init(&osd->o_osd_lru);
717 static void remove_old_osds(struct ceph_osd_client *osdc)
719 struct ceph_osd *osd, *nosd;
721 dout("__remove_old_osds %p\n", osdc);
722 mutex_lock(&osdc->request_mutex);
723 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
724 if (time_before(jiffies, osd->lru_ttl))
726 __remove_osd(osdc, osd);
728 mutex_unlock(&osdc->request_mutex);
734 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
736 struct ceph_osd_request *req;
739 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
740 if (list_empty(&osd->o_requests) &&
741 list_empty(&osd->o_linger_requests)) {
742 __remove_osd(osdc, osd);
743 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
744 &osd->o_con.peer_addr,
745 sizeof(osd->o_con.peer_addr)) == 0 &&
746 !ceph_con_opened(&osd->o_con)) {
747 dout(" osd addr hasn't changed and connection never opened,"
748 " letting msgr retry");
749 /* touch each r_stamp for handle_timeout()'s benfit */
750 list_for_each_entry(req, &osd->o_requests, r_osd_item)
751 req->r_stamp = jiffies;
754 ceph_con_close(&osd->o_con);
755 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
756 osd->o_incarnation++;
761 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
763 struct rb_node **p = &osdc->osds.rb_node;
764 struct rb_node *parent = NULL;
765 struct ceph_osd *osd = NULL;
767 dout("__insert_osd %p osd%d\n", new, new->o_osd);
770 osd = rb_entry(parent, struct ceph_osd, o_node);
771 if (new->o_osd < osd->o_osd)
773 else if (new->o_osd > osd->o_osd)
779 rb_link_node(&new->o_node, parent, p);
780 rb_insert_color(&new->o_node, &osdc->osds);
783 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
785 struct ceph_osd *osd;
786 struct rb_node *n = osdc->osds.rb_node;
789 osd = rb_entry(n, struct ceph_osd, o_node);
792 else if (o > osd->o_osd)
800 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
802 schedule_delayed_work(&osdc->timeout_work,
803 osdc->client->options->osd_keepalive_timeout * HZ);
806 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
808 cancel_delayed_work(&osdc->timeout_work);
812 * Register request, assign tid. If this is the first request, set up
815 static void __register_request(struct ceph_osd_client *osdc,
816 struct ceph_osd_request *req)
818 req->r_tid = ++osdc->last_tid;
819 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
820 dout("__register_request %p tid %lld\n", req, req->r_tid);
821 __insert_request(osdc, req);
822 ceph_osdc_get_request(req);
823 osdc->num_requests++;
824 if (osdc->num_requests == 1) {
825 dout(" first request, scheduling timeout\n");
826 __schedule_osd_timeout(osdc);
830 static void register_request(struct ceph_osd_client *osdc,
831 struct ceph_osd_request *req)
833 mutex_lock(&osdc->request_mutex);
834 __register_request(osdc, req);
835 mutex_unlock(&osdc->request_mutex);
839 * called under osdc->request_mutex
841 static void __unregister_request(struct ceph_osd_client *osdc,
842 struct ceph_osd_request *req)
844 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
845 rb_erase(&req->r_node, &osdc->requests);
846 osdc->num_requests--;
849 /* make sure the original request isn't in flight. */
850 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
852 list_del_init(&req->r_osd_item);
853 if (list_empty(&req->r_osd->o_requests) &&
854 list_empty(&req->r_osd->o_linger_requests)) {
855 dout("moving osd to %p lru\n", req->r_osd);
856 __move_osd_to_lru(osdc, req->r_osd);
858 if (list_empty(&req->r_linger_item))
862 ceph_osdc_put_request(req);
864 list_del_init(&req->r_req_lru_item);
865 if (osdc->num_requests == 0) {
866 dout(" no requests, canceling timeout\n");
867 __cancel_osd_timeout(osdc);
872 * Cancel a previously queued request message
874 static void __cancel_request(struct ceph_osd_request *req)
876 if (req->r_sent && req->r_osd) {
877 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
882 static void __register_linger_request(struct ceph_osd_client *osdc,
883 struct ceph_osd_request *req)
885 dout("__register_linger_request %p\n", req);
886 list_add_tail(&req->r_linger_item, &osdc->req_linger);
887 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
890 static void __unregister_linger_request(struct ceph_osd_client *osdc,
891 struct ceph_osd_request *req)
893 dout("__unregister_linger_request %p\n", req);
895 list_del_init(&req->r_linger_item);
896 list_del_init(&req->r_linger_osd);
898 if (list_empty(&req->r_osd->o_requests) &&
899 list_empty(&req->r_osd->o_linger_requests)) {
900 dout("moving osd to %p lru\n", req->r_osd);
901 __move_osd_to_lru(osdc, req->r_osd);
903 if (list_empty(&req->r_osd_item))
908 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
909 struct ceph_osd_request *req)
911 mutex_lock(&osdc->request_mutex);
913 __unregister_linger_request(osdc, req);
914 ceph_osdc_put_request(req);
916 mutex_unlock(&osdc->request_mutex);
918 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
920 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
921 struct ceph_osd_request *req)
923 if (!req->r_linger) {
924 dout("set_request_linger %p\n", req);
927 * caller is now responsible for calling
928 * unregister_linger_request
930 ceph_osdc_get_request(req);
933 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
936 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
937 * (as needed), and set the request r_osd appropriately. If there is
938 * no up osd, set r_osd to NULL. Move the request to the appropriate list
939 * (unsent, homeless) or leave on in-flight lru.
941 * Return 0 if unchanged, 1 if changed, or negative on error.
943 * Caller should hold map_sem for read and request_mutex.
945 static int __map_request(struct ceph_osd_client *osdc,
946 struct ceph_osd_request *req, int force_resend)
948 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
950 int acting[CEPH_PG_MAX_SIZE];
954 dout("map_request %p tid %lld\n", req, req->r_tid);
955 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
956 &req->r_file_layout, osdc->osdmap);
958 list_move(&req->r_req_lru_item, &osdc->req_notarget);
961 pgid = reqhead->layout.ol_pgid;
964 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
970 if ((!force_resend &&
971 req->r_osd && req->r_osd->o_osd == o &&
972 req->r_sent >= req->r_osd->o_incarnation &&
973 req->r_num_pg_osds == num &&
974 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
975 (req->r_osd == NULL && o == -1))
976 return 0; /* no change */
978 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
979 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
980 req->r_osd ? req->r_osd->o_osd : -1);
982 /* record full pg acting set */
983 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
984 req->r_num_pg_osds = num;
987 __cancel_request(req);
988 list_del_init(&req->r_osd_item);
992 req->r_osd = __lookup_osd(osdc, o);
993 if (!req->r_osd && o >= 0) {
995 req->r_osd = create_osd(osdc);
997 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1001 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1002 req->r_osd->o_osd = o;
1003 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
1004 __insert_osd(osdc, req->r_osd);
1006 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
1010 __remove_osd_from_lru(req->r_osd);
1011 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1012 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1014 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1016 err = 1; /* osd or pg changed */
1023 * caller should hold map_sem (for read) and request_mutex
1025 static void __send_request(struct ceph_osd_client *osdc,
1026 struct ceph_osd_request *req)
1028 struct ceph_osd_request_head *reqhead;
1030 dout("send_request %p tid %llu to osd%d flags %d\n",
1031 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1033 reqhead = req->r_request->front.iov_base;
1034 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1035 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1036 reqhead->reassert_version = req->r_reassert_version;
1038 req->r_stamp = jiffies;
1039 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1041 ceph_msg_get(req->r_request); /* send consumes a ref */
1042 ceph_con_send(&req->r_osd->o_con, req->r_request);
1043 req->r_sent = req->r_osd->o_incarnation;
1047 * Send any requests in the queue (req_unsent).
1049 static void send_queued(struct ceph_osd_client *osdc)
1051 struct ceph_osd_request *req, *tmp;
1053 dout("send_queued\n");
1054 mutex_lock(&osdc->request_mutex);
1055 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1056 __send_request(osdc, req);
1058 mutex_unlock(&osdc->request_mutex);
1062 * Timeout callback, called every N seconds when 1 or more osd
1063 * requests has been active for more than N seconds. When this
1064 * happens, we ping all OSDs with requests who have timed out to
1065 * ensure any communications channel reset is detected. Reset the
1066 * request timeouts another N seconds in the future as we go.
1067 * Reschedule the timeout event another N seconds in future (unless
1068 * there are no open requests).
1070 static void handle_timeout(struct work_struct *work)
1072 struct ceph_osd_client *osdc =
1073 container_of(work, struct ceph_osd_client, timeout_work.work);
1074 struct ceph_osd_request *req, *last_req = NULL;
1075 struct ceph_osd *osd;
1076 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
1077 unsigned long keepalive =
1078 osdc->client->options->osd_keepalive_timeout * HZ;
1079 unsigned long last_stamp = 0;
1080 struct list_head slow_osds;
1082 down_read(&osdc->map_sem);
1084 ceph_monc_request_next_osdmap(&osdc->client->monc);
1086 mutex_lock(&osdc->request_mutex);
1089 * reset osds that appear to be _really_ unresponsive. this
1090 * is a failsafe measure.. we really shouldn't be getting to
1091 * this point if the system is working properly. the monitors
1092 * should mark the osd as failed and we should find out about
1093 * it from an updated osd map.
1095 while (timeout && !list_empty(&osdc->req_lru)) {
1096 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1099 /* hasn't been long enough since we sent it? */
1100 if (time_before(jiffies, req->r_stamp + timeout))
1103 /* hasn't been long enough since it was acked? */
1104 if (req->r_request->ack_stamp == 0 ||
1105 time_before(jiffies, req->r_request->ack_stamp + timeout))
1108 BUG_ON(req == last_req && req->r_stamp == last_stamp);
1110 last_stamp = req->r_stamp;
1114 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1115 req->r_tid, osd->o_osd);
1116 __kick_osd_requests(osdc, osd);
1120 * ping osds that are a bit slow. this ensures that if there
1121 * is a break in the TCP connection we will notice, and reopen
1122 * a connection with that osd (from the fault callback).
1124 INIT_LIST_HEAD(&slow_osds);
1125 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1126 if (time_before(jiffies, req->r_stamp + keepalive))
1131 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1132 req->r_tid, osd->o_osd);
1133 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1135 while (!list_empty(&slow_osds)) {
1136 osd = list_entry(slow_osds.next, struct ceph_osd,
1138 list_del_init(&osd->o_keepalive_item);
1139 ceph_con_keepalive(&osd->o_con);
1142 __schedule_osd_timeout(osdc);
1143 mutex_unlock(&osdc->request_mutex);
1145 up_read(&osdc->map_sem);
1148 static void handle_osds_timeout(struct work_struct *work)
1150 struct ceph_osd_client *osdc =
1151 container_of(work, struct ceph_osd_client,
1152 osds_timeout_work.work);
1153 unsigned long delay =
1154 osdc->client->options->osd_idle_ttl * HZ >> 2;
1156 dout("osds timeout\n");
1157 down_read(&osdc->map_sem);
1158 remove_old_osds(osdc);
1159 up_read(&osdc->map_sem);
1161 schedule_delayed_work(&osdc->osds_timeout_work,
1162 round_jiffies_relative(delay));
1165 static void complete_request(struct ceph_osd_request *req)
1167 if (req->r_safe_callback)
1168 req->r_safe_callback(req, NULL);
1169 complete_all(&req->r_safe_completion); /* fsync waiter */
1173 * handle osd op reply. either call the callback if it is specified,
1174 * or do the completion to wake up the waiting thread.
1176 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1177 struct ceph_connection *con)
1179 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1180 struct ceph_osd_request *req;
1182 int numops, object_len, flags;
1185 tid = le64_to_cpu(msg->hdr.tid);
1186 if (msg->front.iov_len < sizeof(*rhead))
1188 numops = le32_to_cpu(rhead->num_ops);
1189 object_len = le32_to_cpu(rhead->object_len);
1190 result = le32_to_cpu(rhead->result);
1191 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1192 numops * sizeof(struct ceph_osd_op))
1194 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1196 mutex_lock(&osdc->request_mutex);
1197 req = __lookup_request(osdc, tid);
1199 dout("handle_reply tid %llu dne\n", tid);
1200 mutex_unlock(&osdc->request_mutex);
1203 ceph_osdc_get_request(req);
1204 flags = le32_to_cpu(rhead->flags);
1207 * if this connection filled our message, drop our reference now, to
1208 * avoid a (safe but slower) revoke later.
1210 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1211 dout(" dropping con_filling_msg ref %p\n", con);
1212 req->r_con_filling_msg = NULL;
1216 if (!req->r_got_reply) {
1219 req->r_result = le32_to_cpu(rhead->result);
1220 bytes = le32_to_cpu(msg->hdr.data_len);
1221 dout("handle_reply result %d bytes %d\n", req->r_result,
1223 if (req->r_result == 0)
1224 req->r_result = bytes;
1226 /* in case this is a write and we need to replay, */
1227 req->r_reassert_version = rhead->reassert_version;
1229 req->r_got_reply = 1;
1230 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1231 dout("handle_reply tid %llu dup ack\n", tid);
1232 mutex_unlock(&osdc->request_mutex);
1236 dout("handle_reply tid %llu flags %d\n", tid, flags);
1238 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1239 __register_linger_request(osdc, req);
1241 /* either this is a read, or we got the safe response */
1243 (flags & CEPH_OSD_FLAG_ONDISK) ||
1244 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1245 __unregister_request(osdc, req);
1247 mutex_unlock(&osdc->request_mutex);
1249 if (req->r_callback)
1250 req->r_callback(req, msg);
1252 complete_all(&req->r_completion);
1254 if (flags & CEPH_OSD_FLAG_ONDISK)
1255 complete_request(req);
1258 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1259 ceph_osdc_put_request(req);
1263 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1264 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1265 (int)sizeof(*rhead));
1269 static void reset_changed_osds(struct ceph_osd_client *osdc)
1271 struct rb_node *p, *n;
1273 for (p = rb_first(&osdc->osds); p; p = n) {
1274 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1277 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1278 memcmp(&osd->o_con.peer_addr,
1279 ceph_osd_addr(osdc->osdmap,
1281 sizeof(struct ceph_entity_addr)) != 0)
1282 __reset_osd(osdc, osd);
1287 * Requeue requests whose mapping to an OSD has changed. If requests map to
1288 * no osd, request a new map.
1290 * Caller should hold map_sem for read and request_mutex.
1292 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1294 struct ceph_osd_request *req, *nreq;
1299 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1300 mutex_lock(&osdc->request_mutex);
1301 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1302 req = rb_entry(p, struct ceph_osd_request, r_node);
1303 err = __map_request(osdc, req, force_resend);
1305 continue; /* error */
1306 if (req->r_osd == NULL) {
1307 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1308 needmap++; /* request a newer map */
1309 } else if (err > 0) {
1310 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1311 req->r_osd ? req->r_osd->o_osd : -1);
1313 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1317 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1319 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1321 err = __map_request(osdc, req, force_resend);
1323 continue; /* no change and no osd was specified */
1325 continue; /* hrm! */
1326 if (req->r_osd == NULL) {
1327 dout("tid %llu maps to no valid osd\n", req->r_tid);
1328 needmap++; /* request a newer map */
1332 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1333 req->r_osd ? req->r_osd->o_osd : -1);
1334 __unregister_linger_request(osdc, req);
1335 __register_request(osdc, req);
1337 mutex_unlock(&osdc->request_mutex);
1340 dout("%d requests for down osds, need new map\n", needmap);
1341 ceph_monc_request_next_osdmap(&osdc->client->monc);
1347 * Process updated osd map.
1349 * The message contains any number of incremental and full maps, normally
1350 * indicating some sort of topology change in the cluster. Kick requests
1351 * off to different OSDs as needed.
1353 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1355 void *p, *end, *next;
1356 u32 nr_maps, maplen;
1358 struct ceph_osdmap *newmap = NULL, *oldmap;
1360 struct ceph_fsid fsid;
1362 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1363 p = msg->front.iov_base;
1364 end = p + msg->front.iov_len;
1367 ceph_decode_need(&p, end, sizeof(fsid), bad);
1368 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1369 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1372 down_write(&osdc->map_sem);
1374 /* incremental maps */
1375 ceph_decode_32_safe(&p, end, nr_maps, bad);
1376 dout(" %d inc maps\n", nr_maps);
1377 while (nr_maps > 0) {
1378 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1379 epoch = ceph_decode_32(&p);
1380 maplen = ceph_decode_32(&p);
1381 ceph_decode_need(&p, end, maplen, bad);
1383 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1384 dout("applying incremental map %u len %d\n",
1386 newmap = osdmap_apply_incremental(&p, next,
1388 osdc->client->msgr);
1389 if (IS_ERR(newmap)) {
1390 err = PTR_ERR(newmap);
1394 if (newmap != osdc->osdmap) {
1395 ceph_osdmap_destroy(osdc->osdmap);
1396 osdc->osdmap = newmap;
1398 kick_requests(osdc, 0);
1399 reset_changed_osds(osdc);
1401 dout("ignoring incremental map %u len %d\n",
1411 ceph_decode_32_safe(&p, end, nr_maps, bad);
1412 dout(" %d full maps\n", nr_maps);
1414 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1415 epoch = ceph_decode_32(&p);
1416 maplen = ceph_decode_32(&p);
1417 ceph_decode_need(&p, end, maplen, bad);
1419 dout("skipping non-latest full map %u len %d\n",
1421 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1422 dout("skipping full map %u len %d, "
1423 "older than our %u\n", epoch, maplen,
1424 osdc->osdmap->epoch);
1426 int skipped_map = 0;
1428 dout("taking full map %u len %d\n", epoch, maplen);
1429 newmap = osdmap_decode(&p, p+maplen);
1430 if (IS_ERR(newmap)) {
1431 err = PTR_ERR(newmap);
1435 oldmap = osdc->osdmap;
1436 osdc->osdmap = newmap;
1438 if (oldmap->epoch + 1 < newmap->epoch)
1440 ceph_osdmap_destroy(oldmap);
1442 kick_requests(osdc, skipped_map);
1449 downgrade_write(&osdc->map_sem);
1450 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1453 * subscribe to subsequent osdmap updates if full to ensure
1454 * we find out when we are no longer full and stop returning
1457 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1458 ceph_monc_request_next_osdmap(&osdc->client->monc);
1461 up_read(&osdc->map_sem);
1462 wake_up_all(&osdc->client->auth_wq);
1466 pr_err("osdc handle_map corrupt msg\n");
1468 up_write(&osdc->map_sem);
1473 * watch/notify callback event infrastructure
1475 * These callbacks are used both for watch and notify operations.
1477 static void __release_event(struct kref *kref)
1479 struct ceph_osd_event *event =
1480 container_of(kref, struct ceph_osd_event, kref);
1482 dout("__release_event %p\n", event);
1486 static void get_event(struct ceph_osd_event *event)
1488 kref_get(&event->kref);
1491 void ceph_osdc_put_event(struct ceph_osd_event *event)
1493 kref_put(&event->kref, __release_event);
1495 EXPORT_SYMBOL(ceph_osdc_put_event);
1497 static void __insert_event(struct ceph_osd_client *osdc,
1498 struct ceph_osd_event *new)
1500 struct rb_node **p = &osdc->event_tree.rb_node;
1501 struct rb_node *parent = NULL;
1502 struct ceph_osd_event *event = NULL;
1506 event = rb_entry(parent, struct ceph_osd_event, node);
1507 if (new->cookie < event->cookie)
1509 else if (new->cookie > event->cookie)
1510 p = &(*p)->rb_right;
1515 rb_link_node(&new->node, parent, p);
1516 rb_insert_color(&new->node, &osdc->event_tree);
1519 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1522 struct rb_node **p = &osdc->event_tree.rb_node;
1523 struct rb_node *parent = NULL;
1524 struct ceph_osd_event *event = NULL;
1528 event = rb_entry(parent, struct ceph_osd_event, node);
1529 if (cookie < event->cookie)
1531 else if (cookie > event->cookie)
1532 p = &(*p)->rb_right;
1539 static void __remove_event(struct ceph_osd_event *event)
1541 struct ceph_osd_client *osdc = event->osdc;
1543 if (!RB_EMPTY_NODE(&event->node)) {
1544 dout("__remove_event removed %p\n", event);
1545 rb_erase(&event->node, &osdc->event_tree);
1546 ceph_osdc_put_event(event);
1548 dout("__remove_event didn't remove %p\n", event);
1552 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1553 void (*event_cb)(u64, u64, u8, void *),
1554 int one_shot, void *data,
1555 struct ceph_osd_event **pevent)
1557 struct ceph_osd_event *event;
1559 event = kmalloc(sizeof(*event), GFP_NOIO);
1563 dout("create_event %p\n", event);
1564 event->cb = event_cb;
1565 event->one_shot = one_shot;
1568 INIT_LIST_HEAD(&event->osd_node);
1569 kref_init(&event->kref); /* one ref for us */
1570 kref_get(&event->kref); /* one ref for the caller */
1571 init_completion(&event->completion);
1573 spin_lock(&osdc->event_lock);
1574 event->cookie = ++osdc->event_count;
1575 __insert_event(osdc, event);
1576 spin_unlock(&osdc->event_lock);
1581 EXPORT_SYMBOL(ceph_osdc_create_event);
1583 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1585 struct ceph_osd_client *osdc = event->osdc;
1587 dout("cancel_event %p\n", event);
1588 spin_lock(&osdc->event_lock);
1589 __remove_event(event);
1590 spin_unlock(&osdc->event_lock);
1591 ceph_osdc_put_event(event); /* caller's */
1593 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1596 static void do_event_work(struct work_struct *work)
1598 struct ceph_osd_event_work *event_work =
1599 container_of(work, struct ceph_osd_event_work, work);
1600 struct ceph_osd_event *event = event_work->event;
1601 u64 ver = event_work->ver;
1602 u64 notify_id = event_work->notify_id;
1603 u8 opcode = event_work->opcode;
1605 dout("do_event_work completing %p\n", event);
1606 event->cb(ver, notify_id, opcode, event->data);
1607 complete(&event->completion);
1608 dout("do_event_work completed %p\n", event);
1609 ceph_osdc_put_event(event);
1615 * Process osd watch notifications
1617 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1621 u64 cookie, ver, notify_id;
1623 struct ceph_osd_event *event;
1624 struct ceph_osd_event_work *event_work;
1626 p = msg->front.iov_base;
1627 end = p + msg->front.iov_len;
1629 ceph_decode_8_safe(&p, end, proto_ver, bad);
1630 ceph_decode_8_safe(&p, end, opcode, bad);
1631 ceph_decode_64_safe(&p, end, cookie, bad);
1632 ceph_decode_64_safe(&p, end, ver, bad);
1633 ceph_decode_64_safe(&p, end, notify_id, bad);
1635 spin_lock(&osdc->event_lock);
1636 event = __find_event(osdc, cookie);
1639 if (event->one_shot)
1640 __remove_event(event);
1642 spin_unlock(&osdc->event_lock);
1643 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1644 cookie, ver, event);
1646 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1648 dout("ERROR: could not allocate event_work\n");
1651 INIT_WORK(&event_work->work, do_event_work);
1652 event_work->event = event;
1653 event_work->ver = ver;
1654 event_work->notify_id = notify_id;
1655 event_work->opcode = opcode;
1656 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1657 dout("WARNING: failed to queue notify event work\n");
1665 complete(&event->completion);
1666 ceph_osdc_put_event(event);
1670 pr_err("osdc handle_watch_notify corrupt msg\n");
1674 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1678 dout("wait_event %p\n", event);
1679 err = wait_for_completion_interruptible_timeout(&event->completion,
1681 ceph_osdc_put_event(event);
1684 dout("wait_event %p returns %d\n", event, err);
1687 EXPORT_SYMBOL(ceph_osdc_wait_event);
1690 * Register request, send initial attempt.
1692 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1693 struct ceph_osd_request *req,
1698 req->r_request->pages = req->r_pages;
1699 req->r_request->nr_pages = req->r_num_pages;
1701 req->r_request->bio = req->r_bio;
1703 req->r_request->trail = req->r_trail;
1705 register_request(osdc, req);
1707 down_read(&osdc->map_sem);
1708 mutex_lock(&osdc->request_mutex);
1710 * a racing kick_requests() may have sent the message for us
1711 * while we dropped request_mutex above, so only send now if
1712 * the request still han't been touched yet.
1714 if (req->r_sent == 0) {
1715 rc = __map_request(osdc, req, 0);
1718 dout("osdc_start_request failed map, "
1719 " will retry %lld\n", req->r_tid);
1724 if (req->r_osd == NULL) {
1725 dout("send_request %p no up osds in pg\n", req);
1726 ceph_monc_request_next_osdmap(&osdc->client->monc);
1728 __send_request(osdc, req);
1734 mutex_unlock(&osdc->request_mutex);
1735 up_read(&osdc->map_sem);
1738 EXPORT_SYMBOL(ceph_osdc_start_request);
1741 * wait for a request to complete
1743 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1744 struct ceph_osd_request *req)
1748 rc = wait_for_completion_interruptible(&req->r_completion);
1750 mutex_lock(&osdc->request_mutex);
1751 __cancel_request(req);
1752 __unregister_request(osdc, req);
1753 mutex_unlock(&osdc->request_mutex);
1754 complete_request(req);
1755 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1759 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1760 return req->r_result;
1762 EXPORT_SYMBOL(ceph_osdc_wait_request);
1765 * sync - wait for all in-flight requests to flush. avoid starvation.
1767 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1769 struct ceph_osd_request *req;
1770 u64 last_tid, next_tid = 0;
1772 mutex_lock(&osdc->request_mutex);
1773 last_tid = osdc->last_tid;
1775 req = __lookup_request_ge(osdc, next_tid);
1778 if (req->r_tid > last_tid)
1781 next_tid = req->r_tid + 1;
1782 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1785 ceph_osdc_get_request(req);
1786 mutex_unlock(&osdc->request_mutex);
1787 dout("sync waiting on tid %llu (last is %llu)\n",
1788 req->r_tid, last_tid);
1789 wait_for_completion(&req->r_safe_completion);
1790 mutex_lock(&osdc->request_mutex);
1791 ceph_osdc_put_request(req);
1793 mutex_unlock(&osdc->request_mutex);
1794 dout("sync done (thru tid %llu)\n", last_tid);
1796 EXPORT_SYMBOL(ceph_osdc_sync);
1801 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1806 osdc->client = client;
1807 osdc->osdmap = NULL;
1808 init_rwsem(&osdc->map_sem);
1809 init_completion(&osdc->map_waiters);
1810 osdc->last_requested_map = 0;
1811 mutex_init(&osdc->request_mutex);
1813 osdc->osds = RB_ROOT;
1814 INIT_LIST_HEAD(&osdc->osd_lru);
1815 osdc->requests = RB_ROOT;
1816 INIT_LIST_HEAD(&osdc->req_lru);
1817 INIT_LIST_HEAD(&osdc->req_unsent);
1818 INIT_LIST_HEAD(&osdc->req_notarget);
1819 INIT_LIST_HEAD(&osdc->req_linger);
1820 osdc->num_requests = 0;
1821 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1822 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1823 spin_lock_init(&osdc->event_lock);
1824 osdc->event_tree = RB_ROOT;
1825 osdc->event_count = 0;
1827 schedule_delayed_work(&osdc->osds_timeout_work,
1828 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1831 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1832 sizeof(struct ceph_osd_request));
1833 if (!osdc->req_mempool)
1836 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1840 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1841 OSD_OPREPLY_FRONT_LEN, 10, true,
1846 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1847 if (IS_ERR(osdc->notify_wq)) {
1848 err = PTR_ERR(osdc->notify_wq);
1849 osdc->notify_wq = NULL;
1855 ceph_msgpool_destroy(&osdc->msgpool_op);
1857 mempool_destroy(osdc->req_mempool);
1861 EXPORT_SYMBOL(ceph_osdc_init);
1863 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1865 flush_workqueue(osdc->notify_wq);
1866 destroy_workqueue(osdc->notify_wq);
1867 cancel_delayed_work_sync(&osdc->timeout_work);
1868 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1870 ceph_osdmap_destroy(osdc->osdmap);
1871 osdc->osdmap = NULL;
1873 remove_all_osds(osdc);
1874 mempool_destroy(osdc->req_mempool);
1875 ceph_msgpool_destroy(&osdc->msgpool_op);
1876 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1878 EXPORT_SYMBOL(ceph_osdc_stop);
1881 * Read some contiguous pages. If we cross a stripe boundary, shorten
1882 * *plen. Return number of bytes read, or error.
1884 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1885 struct ceph_vino vino, struct ceph_file_layout *layout,
1887 u32 truncate_seq, u64 truncate_size,
1888 struct page **pages, int num_pages, int page_align)
1890 struct ceph_osd_request *req;
1893 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1894 vino.snap, off, *plen);
1895 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1896 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1897 NULL, 0, truncate_seq, truncate_size, NULL,
1898 false, 1, page_align);
1902 /* it may be a short read due to an object boundary */
1903 req->r_pages = pages;
1905 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1906 off, *plen, req->r_num_pages, page_align);
1908 rc = ceph_osdc_start_request(osdc, req, false);
1910 rc = ceph_osdc_wait_request(osdc, req);
1912 ceph_osdc_put_request(req);
1913 dout("readpages result %d\n", rc);
1916 EXPORT_SYMBOL(ceph_osdc_readpages);
1919 * do a synchronous write on N pages
1921 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1922 struct ceph_file_layout *layout,
1923 struct ceph_snap_context *snapc,
1925 u32 truncate_seq, u64 truncate_size,
1926 struct timespec *mtime,
1927 struct page **pages, int num_pages,
1928 int flags, int do_sync, bool nofail)
1930 struct ceph_osd_request *req;
1932 int page_align = off & ~PAGE_MASK;
1934 BUG_ON(vino.snap != CEPH_NOSNAP);
1935 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1937 flags | CEPH_OSD_FLAG_ONDISK |
1938 CEPH_OSD_FLAG_WRITE,
1940 truncate_seq, truncate_size, mtime,
1941 nofail, 1, page_align);
1945 /* it may be a short write due to an object boundary */
1946 req->r_pages = pages;
1947 dout("writepages %llu~%llu (%d pages)\n", off, len,
1950 rc = ceph_osdc_start_request(osdc, req, nofail);
1952 rc = ceph_osdc_wait_request(osdc, req);
1954 ceph_osdc_put_request(req);
1957 dout("writepages result %d\n", rc);
1960 EXPORT_SYMBOL(ceph_osdc_writepages);
1963 * handle incoming message
1965 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1967 struct ceph_osd *osd = con->private;
1968 struct ceph_osd_client *osdc;
1969 int type = le16_to_cpu(msg->hdr.type);
1976 case CEPH_MSG_OSD_MAP:
1977 ceph_osdc_handle_map(osdc, msg);
1979 case CEPH_MSG_OSD_OPREPLY:
1980 handle_reply(osdc, msg, con);
1982 case CEPH_MSG_WATCH_NOTIFY:
1983 handle_watch_notify(osdc, msg);
1987 pr_err("received unknown message type %d %s\n", type,
1988 ceph_msg_type_name(type));
1995 * lookup and return message for incoming reply. set up reply message
1998 static struct ceph_msg *get_reply(struct ceph_connection *con,
1999 struct ceph_msg_header *hdr,
2002 struct ceph_osd *osd = con->private;
2003 struct ceph_osd_client *osdc = osd->o_osdc;
2005 struct ceph_osd_request *req;
2006 int front = le32_to_cpu(hdr->front_len);
2007 int data_len = le32_to_cpu(hdr->data_len);
2010 tid = le64_to_cpu(hdr->tid);
2011 mutex_lock(&osdc->request_mutex);
2012 req = __lookup_request(osdc, tid);
2016 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
2021 if (req->r_con_filling_msg) {
2022 dout("get_reply revoking msg %p from old con %p\n",
2023 req->r_reply, req->r_con_filling_msg);
2024 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
2025 ceph_con_put(req->r_con_filling_msg);
2026 req->r_con_filling_msg = NULL;
2029 if (front > req->r_reply->front.iov_len) {
2030 pr_warning("get_reply front %d > preallocated %d\n",
2031 front, (int)req->r_reply->front.iov_len);
2032 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2035 ceph_msg_put(req->r_reply);
2038 m = ceph_msg_get(req->r_reply);
2041 int want = calc_pages_for(req->r_page_alignment, data_len);
2043 if (unlikely(req->r_num_pages < want)) {
2044 pr_warning("tid %lld reply has %d bytes %d pages, we"
2045 " had only %d pages ready\n", tid, data_len,
2046 want, req->r_num_pages);
2052 m->pages = req->r_pages;
2053 m->nr_pages = req->r_num_pages;
2054 m->page_alignment = req->r_page_alignment;
2056 m->bio = req->r_bio;
2060 req->r_con_filling_msg = ceph_con_get(con);
2061 dout("get_reply tid %lld %p\n", tid, m);
2064 mutex_unlock(&osdc->request_mutex);
2069 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2070 struct ceph_msg_header *hdr,
2073 struct ceph_osd *osd = con->private;
2074 int type = le16_to_cpu(hdr->type);
2075 int front = le32_to_cpu(hdr->front_len);
2078 case CEPH_MSG_OSD_MAP:
2079 case CEPH_MSG_WATCH_NOTIFY:
2080 return ceph_msg_new(type, front, GFP_NOFS, false);
2081 case CEPH_MSG_OSD_OPREPLY:
2082 return get_reply(con, hdr, skip);
2084 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2092 * Wrappers to refcount containing ceph_osd struct
2094 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2096 struct ceph_osd *osd = con->private;
2102 static void put_osd_con(struct ceph_connection *con)
2104 struct ceph_osd *osd = con->private;
2111 static int get_authorizer(struct ceph_connection *con,
2112 void **buf, int *len, int *proto,
2113 void **reply_buf, int *reply_len, int force_new)
2115 struct ceph_osd *o = con->private;
2116 struct ceph_osd_client *osdc = o->o_osdc;
2117 struct ceph_auth_client *ac = osdc->client->monc.auth;
2120 if (force_new && o->o_authorizer) {
2121 ac->ops->destroy_authorizer(ac, o->o_authorizer);
2122 o->o_authorizer = NULL;
2124 if (o->o_authorizer == NULL) {
2125 ret = ac->ops->create_authorizer(
2126 ac, CEPH_ENTITY_TYPE_OSD,
2128 &o->o_authorizer_buf,
2129 &o->o_authorizer_buf_len,
2130 &o->o_authorizer_reply_buf,
2131 &o->o_authorizer_reply_buf_len);
2136 *proto = ac->protocol;
2137 *buf = o->o_authorizer_buf;
2138 *len = o->o_authorizer_buf_len;
2139 *reply_buf = o->o_authorizer_reply_buf;
2140 *reply_len = o->o_authorizer_reply_buf_len;
2145 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2147 struct ceph_osd *o = con->private;
2148 struct ceph_osd_client *osdc = o->o_osdc;
2149 struct ceph_auth_client *ac = osdc->client->monc.auth;
2151 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
2154 static int invalidate_authorizer(struct ceph_connection *con)
2156 struct ceph_osd *o = con->private;
2157 struct ceph_osd_client *osdc = o->o_osdc;
2158 struct ceph_auth_client *ac = osdc->client->monc.auth;
2160 if (ac->ops->invalidate_authorizer)
2161 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2163 return ceph_monc_validate_auth(&osdc->client->monc);
2166 static const struct ceph_connection_operations osd_con_ops = {
2169 .dispatch = dispatch,
2170 .get_authorizer = get_authorizer,
2171 .verify_authorizer_reply = verify_authorizer_reply,
2172 .invalidate_authorizer = invalidate_authorizer,
2173 .alloc_msg = alloc_msg,