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 int __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("%s %p osd%d\n", __func__, osd, osd->o_osd);
682 BUG_ON(!list_empty(&osd->o_requests));
683 list_del_init(&osd->o_osd_lru);
684 rb_erase(&osd->o_node, &osdc->osds);
685 RB_CLEAR_NODE(&osd->o_node);
688 static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
690 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
692 if (!RB_EMPTY_NODE(&osd->o_node)) {
693 ceph_con_close(&osd->o_con);
694 __remove_osd(osdc, osd);
699 static void remove_all_osds(struct ceph_osd_client *osdc)
701 dout("__remove_old_osds %p\n", osdc);
702 mutex_lock(&osdc->request_mutex);
703 while (!RB_EMPTY_ROOT(&osdc->osds)) {
704 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
705 struct ceph_osd, o_node);
706 remove_osd(osdc, osd);
708 mutex_unlock(&osdc->request_mutex);
711 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
712 struct ceph_osd *osd)
714 dout("__move_osd_to_lru %p\n", osd);
715 BUG_ON(!list_empty(&osd->o_osd_lru));
716 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
717 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
720 static void __remove_osd_from_lru(struct ceph_osd *osd)
722 dout("__remove_osd_from_lru %p\n", osd);
723 if (!list_empty(&osd->o_osd_lru))
724 list_del_init(&osd->o_osd_lru);
727 static void remove_old_osds(struct ceph_osd_client *osdc)
729 struct ceph_osd *osd, *nosd;
731 dout("__remove_old_osds %p\n", osdc);
732 mutex_lock(&osdc->request_mutex);
733 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
734 if (time_before(jiffies, osd->lru_ttl))
736 remove_osd(osdc, osd);
738 mutex_unlock(&osdc->request_mutex);
744 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
746 struct ceph_osd_request *req;
749 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
750 if (list_empty(&osd->o_requests) &&
751 list_empty(&osd->o_linger_requests)) {
752 remove_osd(osdc, osd);
753 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
754 &osd->o_con.peer_addr,
755 sizeof(osd->o_con.peer_addr)) == 0 &&
756 !ceph_con_opened(&osd->o_con)) {
757 dout(" osd addr hasn't changed and connection never opened,"
758 " letting msgr retry");
759 /* touch each r_stamp for handle_timeout()'s benfit */
760 list_for_each_entry(req, &osd->o_requests, r_osd_item)
761 req->r_stamp = jiffies;
764 ceph_con_close(&osd->o_con);
765 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
766 osd->o_incarnation++;
771 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
773 struct rb_node **p = &osdc->osds.rb_node;
774 struct rb_node *parent = NULL;
775 struct ceph_osd *osd = NULL;
777 dout("__insert_osd %p osd%d\n", new, new->o_osd);
780 osd = rb_entry(parent, struct ceph_osd, o_node);
781 if (new->o_osd < osd->o_osd)
783 else if (new->o_osd > osd->o_osd)
789 rb_link_node(&new->o_node, parent, p);
790 rb_insert_color(&new->o_node, &osdc->osds);
793 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
795 struct ceph_osd *osd;
796 struct rb_node *n = osdc->osds.rb_node;
799 osd = rb_entry(n, struct ceph_osd, o_node);
802 else if (o > osd->o_osd)
810 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
812 schedule_delayed_work(&osdc->timeout_work,
813 osdc->client->options->osd_keepalive_timeout * HZ);
816 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
818 cancel_delayed_work(&osdc->timeout_work);
822 * Register request, assign tid. If this is the first request, set up
825 static void __register_request(struct ceph_osd_client *osdc,
826 struct ceph_osd_request *req)
828 req->r_tid = ++osdc->last_tid;
829 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
830 dout("__register_request %p tid %lld\n", req, req->r_tid);
831 __insert_request(osdc, req);
832 ceph_osdc_get_request(req);
833 osdc->num_requests++;
834 if (osdc->num_requests == 1) {
835 dout(" first request, scheduling timeout\n");
836 __schedule_osd_timeout(osdc);
840 static void register_request(struct ceph_osd_client *osdc,
841 struct ceph_osd_request *req)
843 mutex_lock(&osdc->request_mutex);
844 __register_request(osdc, req);
845 mutex_unlock(&osdc->request_mutex);
849 * called under osdc->request_mutex
851 static void __unregister_request(struct ceph_osd_client *osdc,
852 struct ceph_osd_request *req)
854 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
855 rb_erase(&req->r_node, &osdc->requests);
856 osdc->num_requests--;
859 /* make sure the original request isn't in flight. */
860 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
862 list_del_init(&req->r_osd_item);
863 if (list_empty(&req->r_osd->o_requests) &&
864 list_empty(&req->r_osd->o_linger_requests)) {
865 dout("moving osd to %p lru\n", req->r_osd);
866 __move_osd_to_lru(osdc, req->r_osd);
868 if (list_empty(&req->r_linger_item))
872 ceph_osdc_put_request(req);
874 list_del_init(&req->r_req_lru_item);
875 if (osdc->num_requests == 0) {
876 dout(" no requests, canceling timeout\n");
877 __cancel_osd_timeout(osdc);
882 * Cancel a previously queued request message
884 static void __cancel_request(struct ceph_osd_request *req)
886 if (req->r_sent && req->r_osd) {
887 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
892 static void __register_linger_request(struct ceph_osd_client *osdc,
893 struct ceph_osd_request *req)
895 dout("__register_linger_request %p\n", req);
896 list_add_tail(&req->r_linger_item, &osdc->req_linger);
897 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
900 static void __unregister_linger_request(struct ceph_osd_client *osdc,
901 struct ceph_osd_request *req)
903 dout("__unregister_linger_request %p\n", req);
905 list_del_init(&req->r_linger_item);
906 list_del_init(&req->r_linger_osd);
908 if (list_empty(&req->r_osd->o_requests) &&
909 list_empty(&req->r_osd->o_linger_requests)) {
910 dout("moving osd to %p lru\n", req->r_osd);
911 __move_osd_to_lru(osdc, req->r_osd);
913 if (list_empty(&req->r_osd_item))
918 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
919 struct ceph_osd_request *req)
921 mutex_lock(&osdc->request_mutex);
923 __unregister_linger_request(osdc, req);
924 ceph_osdc_put_request(req);
926 mutex_unlock(&osdc->request_mutex);
928 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
930 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
931 struct ceph_osd_request *req)
933 if (!req->r_linger) {
934 dout("set_request_linger %p\n", req);
937 * caller is now responsible for calling
938 * unregister_linger_request
940 ceph_osdc_get_request(req);
943 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
946 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
947 * (as needed), and set the request r_osd appropriately. If there is
948 * no up osd, set r_osd to NULL. Move the request to the appropriate list
949 * (unsent, homeless) or leave on in-flight lru.
951 * Return 0 if unchanged, 1 if changed, or negative on error.
953 * Caller should hold map_sem for read and request_mutex.
955 static int __map_request(struct ceph_osd_client *osdc,
956 struct ceph_osd_request *req, int force_resend)
958 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
960 int acting[CEPH_PG_MAX_SIZE];
964 dout("map_request %p tid %lld\n", req, req->r_tid);
965 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
966 &req->r_file_layout, osdc->osdmap);
968 list_move(&req->r_req_lru_item, &osdc->req_notarget);
971 pgid = reqhead->layout.ol_pgid;
974 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
980 if ((!force_resend &&
981 req->r_osd && req->r_osd->o_osd == o &&
982 req->r_sent >= req->r_osd->o_incarnation &&
983 req->r_num_pg_osds == num &&
984 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
985 (req->r_osd == NULL && o == -1))
986 return 0; /* no change */
988 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
989 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
990 req->r_osd ? req->r_osd->o_osd : -1);
992 /* record full pg acting set */
993 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
994 req->r_num_pg_osds = num;
997 __cancel_request(req);
998 list_del_init(&req->r_osd_item);
1002 req->r_osd = __lookup_osd(osdc, o);
1003 if (!req->r_osd && o >= 0) {
1005 req->r_osd = create_osd(osdc);
1007 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1011 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1012 req->r_osd->o_osd = o;
1013 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
1014 __insert_osd(osdc, req->r_osd);
1016 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
1020 __remove_osd_from_lru(req->r_osd);
1021 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1022 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1024 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1026 err = 1; /* osd or pg changed */
1033 * caller should hold map_sem (for read) and request_mutex
1035 static int __send_request(struct ceph_osd_client *osdc,
1036 struct ceph_osd_request *req)
1038 struct ceph_osd_request_head *reqhead;
1040 dout("send_request %p tid %llu to osd%d flags %d\n",
1041 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1043 reqhead = req->r_request->front.iov_base;
1044 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1045 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1046 reqhead->reassert_version = req->r_reassert_version;
1048 req->r_stamp = jiffies;
1049 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1051 ceph_msg_get(req->r_request); /* send consumes a ref */
1052 ceph_con_send(&req->r_osd->o_con, req->r_request);
1053 req->r_sent = req->r_osd->o_incarnation;
1058 * Send any requests in the queue (req_unsent).
1060 static void send_queued(struct ceph_osd_client *osdc)
1062 struct ceph_osd_request *req, *tmp;
1064 dout("send_queued\n");
1065 mutex_lock(&osdc->request_mutex);
1066 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1067 __send_request(osdc, req);
1069 mutex_unlock(&osdc->request_mutex);
1073 * Timeout callback, called every N seconds when 1 or more osd
1074 * requests has been active for more than N seconds. When this
1075 * happens, we ping all OSDs with requests who have timed out to
1076 * ensure any communications channel reset is detected. Reset the
1077 * request timeouts another N seconds in the future as we go.
1078 * Reschedule the timeout event another N seconds in future (unless
1079 * there are no open requests).
1081 static void handle_timeout(struct work_struct *work)
1083 struct ceph_osd_client *osdc =
1084 container_of(work, struct ceph_osd_client, timeout_work.work);
1085 struct ceph_osd_request *req, *last_req = NULL;
1086 struct ceph_osd *osd;
1087 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
1088 unsigned long keepalive =
1089 osdc->client->options->osd_keepalive_timeout * HZ;
1090 unsigned long last_stamp = 0;
1091 struct list_head slow_osds;
1093 down_read(&osdc->map_sem);
1095 ceph_monc_request_next_osdmap(&osdc->client->monc);
1097 mutex_lock(&osdc->request_mutex);
1100 * reset osds that appear to be _really_ unresponsive. this
1101 * is a failsafe measure.. we really shouldn't be getting to
1102 * this point if the system is working properly. the monitors
1103 * should mark the osd as failed and we should find out about
1104 * it from an updated osd map.
1106 while (timeout && !list_empty(&osdc->req_lru)) {
1107 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1110 /* hasn't been long enough since we sent it? */
1111 if (time_before(jiffies, req->r_stamp + timeout))
1114 /* hasn't been long enough since it was acked? */
1115 if (req->r_request->ack_stamp == 0 ||
1116 time_before(jiffies, req->r_request->ack_stamp + timeout))
1119 BUG_ON(req == last_req && req->r_stamp == last_stamp);
1121 last_stamp = req->r_stamp;
1125 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1126 req->r_tid, osd->o_osd);
1127 __kick_osd_requests(osdc, osd);
1131 * ping osds that are a bit slow. this ensures that if there
1132 * is a break in the TCP connection we will notice, and reopen
1133 * a connection with that osd (from the fault callback).
1135 INIT_LIST_HEAD(&slow_osds);
1136 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1137 if (time_before(jiffies, req->r_stamp + keepalive))
1142 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1143 req->r_tid, osd->o_osd);
1144 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1146 while (!list_empty(&slow_osds)) {
1147 osd = list_entry(slow_osds.next, struct ceph_osd,
1149 list_del_init(&osd->o_keepalive_item);
1150 ceph_con_keepalive(&osd->o_con);
1153 __schedule_osd_timeout(osdc);
1154 mutex_unlock(&osdc->request_mutex);
1156 up_read(&osdc->map_sem);
1159 static void handle_osds_timeout(struct work_struct *work)
1161 struct ceph_osd_client *osdc =
1162 container_of(work, struct ceph_osd_client,
1163 osds_timeout_work.work);
1164 unsigned long delay =
1165 osdc->client->options->osd_idle_ttl * HZ >> 2;
1167 dout("osds timeout\n");
1168 down_read(&osdc->map_sem);
1169 remove_old_osds(osdc);
1170 up_read(&osdc->map_sem);
1172 schedule_delayed_work(&osdc->osds_timeout_work,
1173 round_jiffies_relative(delay));
1176 static void complete_request(struct ceph_osd_request *req)
1178 if (req->r_safe_callback)
1179 req->r_safe_callback(req, NULL);
1180 complete_all(&req->r_safe_completion); /* fsync waiter */
1184 * handle osd op reply. either call the callback if it is specified,
1185 * or do the completion to wake up the waiting thread.
1187 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1188 struct ceph_connection *con)
1190 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1191 struct ceph_osd_request *req;
1193 int numops, object_len, flags;
1196 tid = le64_to_cpu(msg->hdr.tid);
1197 if (msg->front.iov_len < sizeof(*rhead))
1199 numops = le32_to_cpu(rhead->num_ops);
1200 object_len = le32_to_cpu(rhead->object_len);
1201 result = le32_to_cpu(rhead->result);
1202 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1203 numops * sizeof(struct ceph_osd_op))
1205 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1207 mutex_lock(&osdc->request_mutex);
1208 req = __lookup_request(osdc, tid);
1210 dout("handle_reply tid %llu dne\n", tid);
1211 mutex_unlock(&osdc->request_mutex);
1214 ceph_osdc_get_request(req);
1215 flags = le32_to_cpu(rhead->flags);
1218 * if this connection filled our message, drop our reference now, to
1219 * avoid a (safe but slower) revoke later.
1221 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1222 dout(" dropping con_filling_msg ref %p\n", con);
1223 req->r_con_filling_msg = NULL;
1227 if (!req->r_got_reply) {
1230 req->r_result = le32_to_cpu(rhead->result);
1231 bytes = le32_to_cpu(msg->hdr.data_len);
1232 dout("handle_reply result %d bytes %d\n", req->r_result,
1234 if (req->r_result == 0)
1235 req->r_result = bytes;
1237 /* in case this is a write and we need to replay, */
1238 req->r_reassert_version = rhead->reassert_version;
1240 req->r_got_reply = 1;
1241 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1242 dout("handle_reply tid %llu dup ack\n", tid);
1243 mutex_unlock(&osdc->request_mutex);
1247 dout("handle_reply tid %llu flags %d\n", tid, flags);
1249 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1250 __register_linger_request(osdc, req);
1252 /* either this is a read, or we got the safe response */
1254 (flags & CEPH_OSD_FLAG_ONDISK) ||
1255 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1256 __unregister_request(osdc, req);
1258 mutex_unlock(&osdc->request_mutex);
1260 if (req->r_callback)
1261 req->r_callback(req, msg);
1263 complete_all(&req->r_completion);
1265 if (flags & CEPH_OSD_FLAG_ONDISK)
1266 complete_request(req);
1269 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1270 ceph_osdc_put_request(req);
1274 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1275 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1276 (int)sizeof(*rhead));
1280 static void reset_changed_osds(struct ceph_osd_client *osdc)
1282 struct rb_node *p, *n;
1284 dout("%s %p\n", __func__, osdc);
1285 for (p = rb_first(&osdc->osds); p; p = n) {
1286 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1289 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1290 memcmp(&osd->o_con.peer_addr,
1291 ceph_osd_addr(osdc->osdmap,
1293 sizeof(struct ceph_entity_addr)) != 0)
1294 __reset_osd(osdc, osd);
1299 * Requeue requests whose mapping to an OSD has changed. If requests map to
1300 * no osd, request a new map.
1302 * Caller should hold map_sem for read and request_mutex.
1304 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1306 struct ceph_osd_request *req, *nreq;
1311 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1312 mutex_lock(&osdc->request_mutex);
1313 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1314 req = rb_entry(p, struct ceph_osd_request, r_node);
1315 err = __map_request(osdc, req, force_resend);
1317 continue; /* error */
1318 if (req->r_osd == NULL) {
1319 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1320 needmap++; /* request a newer map */
1321 } else if (err > 0) {
1322 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1323 req->r_osd ? req->r_osd->o_osd : -1);
1325 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1329 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1331 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1333 err = __map_request(osdc, req, force_resend);
1335 continue; /* no change and no osd was specified */
1337 continue; /* hrm! */
1338 if (req->r_osd == NULL) {
1339 dout("tid %llu maps to no valid osd\n", req->r_tid);
1340 needmap++; /* request a newer map */
1344 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1345 req->r_osd ? req->r_osd->o_osd : -1);
1346 __unregister_linger_request(osdc, req);
1347 __register_request(osdc, req);
1349 mutex_unlock(&osdc->request_mutex);
1352 dout("%d requests for down osds, need new map\n", needmap);
1353 ceph_monc_request_next_osdmap(&osdc->client->monc);
1359 * Process updated osd map.
1361 * The message contains any number of incremental and full maps, normally
1362 * indicating some sort of topology change in the cluster. Kick requests
1363 * off to different OSDs as needed.
1365 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1367 void *p, *end, *next;
1368 u32 nr_maps, maplen;
1370 struct ceph_osdmap *newmap = NULL, *oldmap;
1372 struct ceph_fsid fsid;
1374 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1375 p = msg->front.iov_base;
1376 end = p + msg->front.iov_len;
1379 ceph_decode_need(&p, end, sizeof(fsid), bad);
1380 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1381 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1384 down_write(&osdc->map_sem);
1386 /* incremental maps */
1387 ceph_decode_32_safe(&p, end, nr_maps, bad);
1388 dout(" %d inc maps\n", nr_maps);
1389 while (nr_maps > 0) {
1390 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1391 epoch = ceph_decode_32(&p);
1392 maplen = ceph_decode_32(&p);
1393 ceph_decode_need(&p, end, maplen, bad);
1395 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1396 dout("applying incremental map %u len %d\n",
1398 newmap = osdmap_apply_incremental(&p, next,
1400 osdc->client->msgr);
1401 if (IS_ERR(newmap)) {
1402 err = PTR_ERR(newmap);
1406 if (newmap != osdc->osdmap) {
1407 ceph_osdmap_destroy(osdc->osdmap);
1408 osdc->osdmap = newmap;
1410 kick_requests(osdc, 0);
1411 reset_changed_osds(osdc);
1413 dout("ignoring incremental map %u len %d\n",
1423 ceph_decode_32_safe(&p, end, nr_maps, bad);
1424 dout(" %d full maps\n", nr_maps);
1426 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1427 epoch = ceph_decode_32(&p);
1428 maplen = ceph_decode_32(&p);
1429 ceph_decode_need(&p, end, maplen, bad);
1431 dout("skipping non-latest full map %u len %d\n",
1433 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1434 dout("skipping full map %u len %d, "
1435 "older than our %u\n", epoch, maplen,
1436 osdc->osdmap->epoch);
1438 int skipped_map = 0;
1440 dout("taking full map %u len %d\n", epoch, maplen);
1441 newmap = osdmap_decode(&p, p+maplen);
1442 if (IS_ERR(newmap)) {
1443 err = PTR_ERR(newmap);
1447 oldmap = osdc->osdmap;
1448 osdc->osdmap = newmap;
1450 if (oldmap->epoch + 1 < newmap->epoch)
1452 ceph_osdmap_destroy(oldmap);
1454 kick_requests(osdc, skipped_map);
1461 downgrade_write(&osdc->map_sem);
1462 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1465 * subscribe to subsequent osdmap updates if full to ensure
1466 * we find out when we are no longer full and stop returning
1469 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1470 ceph_monc_request_next_osdmap(&osdc->client->monc);
1473 up_read(&osdc->map_sem);
1474 wake_up_all(&osdc->client->auth_wq);
1478 pr_err("osdc handle_map corrupt msg\n");
1480 up_write(&osdc->map_sem);
1485 * watch/notify callback event infrastructure
1487 * These callbacks are used both for watch and notify operations.
1489 static void __release_event(struct kref *kref)
1491 struct ceph_osd_event *event =
1492 container_of(kref, struct ceph_osd_event, kref);
1494 dout("__release_event %p\n", event);
1498 static void get_event(struct ceph_osd_event *event)
1500 kref_get(&event->kref);
1503 void ceph_osdc_put_event(struct ceph_osd_event *event)
1505 kref_put(&event->kref, __release_event);
1507 EXPORT_SYMBOL(ceph_osdc_put_event);
1509 static void __insert_event(struct ceph_osd_client *osdc,
1510 struct ceph_osd_event *new)
1512 struct rb_node **p = &osdc->event_tree.rb_node;
1513 struct rb_node *parent = NULL;
1514 struct ceph_osd_event *event = NULL;
1518 event = rb_entry(parent, struct ceph_osd_event, node);
1519 if (new->cookie < event->cookie)
1521 else if (new->cookie > event->cookie)
1522 p = &(*p)->rb_right;
1527 rb_link_node(&new->node, parent, p);
1528 rb_insert_color(&new->node, &osdc->event_tree);
1531 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1534 struct rb_node **p = &osdc->event_tree.rb_node;
1535 struct rb_node *parent = NULL;
1536 struct ceph_osd_event *event = NULL;
1540 event = rb_entry(parent, struct ceph_osd_event, node);
1541 if (cookie < event->cookie)
1543 else if (cookie > event->cookie)
1544 p = &(*p)->rb_right;
1551 static void __remove_event(struct ceph_osd_event *event)
1553 struct ceph_osd_client *osdc = event->osdc;
1555 if (!RB_EMPTY_NODE(&event->node)) {
1556 dout("__remove_event removed %p\n", event);
1557 rb_erase(&event->node, &osdc->event_tree);
1558 ceph_osdc_put_event(event);
1560 dout("__remove_event didn't remove %p\n", event);
1564 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1565 void (*event_cb)(u64, u64, u8, void *),
1566 int one_shot, void *data,
1567 struct ceph_osd_event **pevent)
1569 struct ceph_osd_event *event;
1571 event = kmalloc(sizeof(*event), GFP_NOIO);
1575 dout("create_event %p\n", event);
1576 event->cb = event_cb;
1577 event->one_shot = one_shot;
1580 INIT_LIST_HEAD(&event->osd_node);
1581 kref_init(&event->kref); /* one ref for us */
1582 kref_get(&event->kref); /* one ref for the caller */
1583 init_completion(&event->completion);
1585 spin_lock(&osdc->event_lock);
1586 event->cookie = ++osdc->event_count;
1587 __insert_event(osdc, event);
1588 spin_unlock(&osdc->event_lock);
1593 EXPORT_SYMBOL(ceph_osdc_create_event);
1595 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1597 struct ceph_osd_client *osdc = event->osdc;
1599 dout("cancel_event %p\n", event);
1600 spin_lock(&osdc->event_lock);
1601 __remove_event(event);
1602 spin_unlock(&osdc->event_lock);
1603 ceph_osdc_put_event(event); /* caller's */
1605 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1608 static void do_event_work(struct work_struct *work)
1610 struct ceph_osd_event_work *event_work =
1611 container_of(work, struct ceph_osd_event_work, work);
1612 struct ceph_osd_event *event = event_work->event;
1613 u64 ver = event_work->ver;
1614 u64 notify_id = event_work->notify_id;
1615 u8 opcode = event_work->opcode;
1617 dout("do_event_work completing %p\n", event);
1618 event->cb(ver, notify_id, opcode, event->data);
1619 complete(&event->completion);
1620 dout("do_event_work completed %p\n", event);
1621 ceph_osdc_put_event(event);
1627 * Process osd watch notifications
1629 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1633 u64 cookie, ver, notify_id;
1635 struct ceph_osd_event *event;
1636 struct ceph_osd_event_work *event_work;
1638 p = msg->front.iov_base;
1639 end = p + msg->front.iov_len;
1641 ceph_decode_8_safe(&p, end, proto_ver, bad);
1642 ceph_decode_8_safe(&p, end, opcode, bad);
1643 ceph_decode_64_safe(&p, end, cookie, bad);
1644 ceph_decode_64_safe(&p, end, ver, bad);
1645 ceph_decode_64_safe(&p, end, notify_id, bad);
1647 spin_lock(&osdc->event_lock);
1648 event = __find_event(osdc, cookie);
1651 if (event->one_shot)
1652 __remove_event(event);
1654 spin_unlock(&osdc->event_lock);
1655 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1656 cookie, ver, event);
1658 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1660 dout("ERROR: could not allocate event_work\n");
1663 INIT_WORK(&event_work->work, do_event_work);
1664 event_work->event = event;
1665 event_work->ver = ver;
1666 event_work->notify_id = notify_id;
1667 event_work->opcode = opcode;
1668 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1669 dout("WARNING: failed to queue notify event work\n");
1677 complete(&event->completion);
1678 ceph_osdc_put_event(event);
1682 pr_err("osdc handle_watch_notify corrupt msg\n");
1686 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1690 dout("wait_event %p\n", event);
1691 err = wait_for_completion_interruptible_timeout(&event->completion,
1693 ceph_osdc_put_event(event);
1696 dout("wait_event %p returns %d\n", event, err);
1699 EXPORT_SYMBOL(ceph_osdc_wait_event);
1702 * Register request, send initial attempt.
1704 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1705 struct ceph_osd_request *req,
1710 req->r_request->pages = req->r_pages;
1711 req->r_request->nr_pages = req->r_num_pages;
1713 req->r_request->bio = req->r_bio;
1715 req->r_request->trail = req->r_trail;
1717 register_request(osdc, req);
1719 down_read(&osdc->map_sem);
1720 mutex_lock(&osdc->request_mutex);
1722 * a racing kick_requests() may have sent the message for us
1723 * while we dropped request_mutex above, so only send now if
1724 * the request still han't been touched yet.
1726 if (req->r_sent == 0) {
1727 rc = __map_request(osdc, req, 0);
1730 dout("osdc_start_request failed map, "
1731 " will retry %lld\n", req->r_tid);
1734 __unregister_request(osdc, req);
1738 if (req->r_osd == NULL) {
1739 dout("send_request %p no up osds in pg\n", req);
1740 ceph_monc_request_next_osdmap(&osdc->client->monc);
1742 rc = __send_request(osdc, req);
1745 dout("osdc_start_request failed send, "
1746 " will retry %lld\n", req->r_tid);
1749 __unregister_request(osdc, req);
1756 mutex_unlock(&osdc->request_mutex);
1757 up_read(&osdc->map_sem);
1760 EXPORT_SYMBOL(ceph_osdc_start_request);
1763 * wait for a request to complete
1765 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1766 struct ceph_osd_request *req)
1770 rc = wait_for_completion_interruptible(&req->r_completion);
1772 mutex_lock(&osdc->request_mutex);
1773 __cancel_request(req);
1774 __unregister_request(osdc, req);
1775 mutex_unlock(&osdc->request_mutex);
1776 complete_request(req);
1777 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1781 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1782 return req->r_result;
1784 EXPORT_SYMBOL(ceph_osdc_wait_request);
1787 * sync - wait for all in-flight requests to flush. avoid starvation.
1789 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1791 struct ceph_osd_request *req;
1792 u64 last_tid, next_tid = 0;
1794 mutex_lock(&osdc->request_mutex);
1795 last_tid = osdc->last_tid;
1797 req = __lookup_request_ge(osdc, next_tid);
1800 if (req->r_tid > last_tid)
1803 next_tid = req->r_tid + 1;
1804 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1807 ceph_osdc_get_request(req);
1808 mutex_unlock(&osdc->request_mutex);
1809 dout("sync waiting on tid %llu (last is %llu)\n",
1810 req->r_tid, last_tid);
1811 wait_for_completion(&req->r_safe_completion);
1812 mutex_lock(&osdc->request_mutex);
1813 ceph_osdc_put_request(req);
1815 mutex_unlock(&osdc->request_mutex);
1816 dout("sync done (thru tid %llu)\n", last_tid);
1818 EXPORT_SYMBOL(ceph_osdc_sync);
1823 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1828 osdc->client = client;
1829 osdc->osdmap = NULL;
1830 init_rwsem(&osdc->map_sem);
1831 init_completion(&osdc->map_waiters);
1832 osdc->last_requested_map = 0;
1833 mutex_init(&osdc->request_mutex);
1835 osdc->osds = RB_ROOT;
1836 INIT_LIST_HEAD(&osdc->osd_lru);
1837 osdc->requests = RB_ROOT;
1838 INIT_LIST_HEAD(&osdc->req_lru);
1839 INIT_LIST_HEAD(&osdc->req_unsent);
1840 INIT_LIST_HEAD(&osdc->req_notarget);
1841 INIT_LIST_HEAD(&osdc->req_linger);
1842 osdc->num_requests = 0;
1843 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1844 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1845 spin_lock_init(&osdc->event_lock);
1846 osdc->event_tree = RB_ROOT;
1847 osdc->event_count = 0;
1849 schedule_delayed_work(&osdc->osds_timeout_work,
1850 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1853 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1854 sizeof(struct ceph_osd_request));
1855 if (!osdc->req_mempool)
1858 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1862 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1863 OSD_OPREPLY_FRONT_LEN, 10, true,
1868 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1869 if (IS_ERR(osdc->notify_wq)) {
1870 err = PTR_ERR(osdc->notify_wq);
1871 osdc->notify_wq = NULL;
1877 ceph_msgpool_destroy(&osdc->msgpool_op);
1879 mempool_destroy(osdc->req_mempool);
1883 EXPORT_SYMBOL(ceph_osdc_init);
1885 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1887 flush_workqueue(osdc->notify_wq);
1888 destroy_workqueue(osdc->notify_wq);
1889 cancel_delayed_work_sync(&osdc->timeout_work);
1890 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1892 ceph_osdmap_destroy(osdc->osdmap);
1893 osdc->osdmap = NULL;
1895 remove_all_osds(osdc);
1896 mempool_destroy(osdc->req_mempool);
1897 ceph_msgpool_destroy(&osdc->msgpool_op);
1898 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1900 EXPORT_SYMBOL(ceph_osdc_stop);
1903 * Read some contiguous pages. If we cross a stripe boundary, shorten
1904 * *plen. Return number of bytes read, or error.
1906 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1907 struct ceph_vino vino, struct ceph_file_layout *layout,
1909 u32 truncate_seq, u64 truncate_size,
1910 struct page **pages, int num_pages, int page_align)
1912 struct ceph_osd_request *req;
1915 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1916 vino.snap, off, *plen);
1917 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1918 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1919 NULL, 0, truncate_seq, truncate_size, NULL,
1920 false, 1, page_align);
1924 /* it may be a short read due to an object boundary */
1925 req->r_pages = pages;
1927 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1928 off, *plen, req->r_num_pages, page_align);
1930 rc = ceph_osdc_start_request(osdc, req, false);
1932 rc = ceph_osdc_wait_request(osdc, req);
1934 ceph_osdc_put_request(req);
1935 dout("readpages result %d\n", rc);
1938 EXPORT_SYMBOL(ceph_osdc_readpages);
1941 * do a synchronous write on N pages
1943 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1944 struct ceph_file_layout *layout,
1945 struct ceph_snap_context *snapc,
1947 u32 truncate_seq, u64 truncate_size,
1948 struct timespec *mtime,
1949 struct page **pages, int num_pages,
1950 int flags, int do_sync, bool nofail)
1952 struct ceph_osd_request *req;
1954 int page_align = off & ~PAGE_MASK;
1956 BUG_ON(vino.snap != CEPH_NOSNAP);
1957 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1959 flags | CEPH_OSD_FLAG_ONDISK |
1960 CEPH_OSD_FLAG_WRITE,
1962 truncate_seq, truncate_size, mtime,
1963 nofail, 1, page_align);
1967 /* it may be a short write due to an object boundary */
1968 req->r_pages = pages;
1969 dout("writepages %llu~%llu (%d pages)\n", off, len,
1972 rc = ceph_osdc_start_request(osdc, req, nofail);
1974 rc = ceph_osdc_wait_request(osdc, req);
1976 ceph_osdc_put_request(req);
1979 dout("writepages result %d\n", rc);
1982 EXPORT_SYMBOL(ceph_osdc_writepages);
1985 * handle incoming message
1987 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1989 struct ceph_osd *osd = con->private;
1990 struct ceph_osd_client *osdc;
1991 int type = le16_to_cpu(msg->hdr.type);
1998 case CEPH_MSG_OSD_MAP:
1999 ceph_osdc_handle_map(osdc, msg);
2001 case CEPH_MSG_OSD_OPREPLY:
2002 handle_reply(osdc, msg, con);
2004 case CEPH_MSG_WATCH_NOTIFY:
2005 handle_watch_notify(osdc, msg);
2009 pr_err("received unknown message type %d %s\n", type,
2010 ceph_msg_type_name(type));
2017 * lookup and return message for incoming reply. set up reply message
2020 static struct ceph_msg *get_reply(struct ceph_connection *con,
2021 struct ceph_msg_header *hdr,
2024 struct ceph_osd *osd = con->private;
2025 struct ceph_osd_client *osdc = osd->o_osdc;
2027 struct ceph_osd_request *req;
2028 int front = le32_to_cpu(hdr->front_len);
2029 int data_len = le32_to_cpu(hdr->data_len);
2032 tid = le64_to_cpu(hdr->tid);
2033 mutex_lock(&osdc->request_mutex);
2034 req = __lookup_request(osdc, tid);
2038 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
2043 if (req->r_con_filling_msg) {
2044 dout("get_reply revoking msg %p from old con %p\n",
2045 req->r_reply, req->r_con_filling_msg);
2046 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
2047 ceph_con_put(req->r_con_filling_msg);
2048 req->r_con_filling_msg = NULL;
2051 if (front > req->r_reply->front.iov_len) {
2052 pr_warning("get_reply front %d > preallocated %d\n",
2053 front, (int)req->r_reply->front.iov_len);
2054 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2057 ceph_msg_put(req->r_reply);
2060 m = ceph_msg_get(req->r_reply);
2063 int want = calc_pages_for(req->r_page_alignment, data_len);
2065 if (unlikely(req->r_num_pages < want)) {
2066 pr_warning("tid %lld reply has %d bytes %d pages, we"
2067 " had only %d pages ready\n", tid, data_len,
2068 want, req->r_num_pages);
2074 m->pages = req->r_pages;
2075 m->nr_pages = req->r_num_pages;
2076 m->page_alignment = req->r_page_alignment;
2078 m->bio = req->r_bio;
2082 req->r_con_filling_msg = ceph_con_get(con);
2083 dout("get_reply tid %lld %p\n", tid, m);
2086 mutex_unlock(&osdc->request_mutex);
2091 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2092 struct ceph_msg_header *hdr,
2095 struct ceph_osd *osd = con->private;
2096 int type = le16_to_cpu(hdr->type);
2097 int front = le32_to_cpu(hdr->front_len);
2100 case CEPH_MSG_OSD_MAP:
2101 case CEPH_MSG_WATCH_NOTIFY:
2102 return ceph_msg_new(type, front, GFP_NOFS, false);
2103 case CEPH_MSG_OSD_OPREPLY:
2104 return get_reply(con, hdr, skip);
2106 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2114 * Wrappers to refcount containing ceph_osd struct
2116 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2118 struct ceph_osd *osd = con->private;
2124 static void put_osd_con(struct ceph_connection *con)
2126 struct ceph_osd *osd = con->private;
2133 static int get_authorizer(struct ceph_connection *con,
2134 void **buf, int *len, int *proto,
2135 void **reply_buf, int *reply_len, int force_new)
2137 struct ceph_osd *o = con->private;
2138 struct ceph_osd_client *osdc = o->o_osdc;
2139 struct ceph_auth_client *ac = osdc->client->monc.auth;
2142 if (force_new && o->o_authorizer) {
2143 ac->ops->destroy_authorizer(ac, o->o_authorizer);
2144 o->o_authorizer = NULL;
2146 if (o->o_authorizer == NULL) {
2147 ret = ac->ops->create_authorizer(
2148 ac, CEPH_ENTITY_TYPE_OSD,
2150 &o->o_authorizer_buf,
2151 &o->o_authorizer_buf_len,
2152 &o->o_authorizer_reply_buf,
2153 &o->o_authorizer_reply_buf_len);
2158 *proto = ac->protocol;
2159 *buf = o->o_authorizer_buf;
2160 *len = o->o_authorizer_buf_len;
2161 *reply_buf = o->o_authorizer_reply_buf;
2162 *reply_len = o->o_authorizer_reply_buf_len;
2167 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2169 struct ceph_osd *o = con->private;
2170 struct ceph_osd_client *osdc = o->o_osdc;
2171 struct ceph_auth_client *ac = osdc->client->monc.auth;
2173 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
2176 static int invalidate_authorizer(struct ceph_connection *con)
2178 struct ceph_osd *o = con->private;
2179 struct ceph_osd_client *osdc = o->o_osdc;
2180 struct ceph_auth_client *ac = osdc->client->monc.auth;
2182 if (ac->ops->invalidate_authorizer)
2183 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2185 return ceph_monc_validate_auth(&osdc->client->monc);
2188 static const struct ceph_connection_operations osd_con_ops = {
2191 .dispatch = dispatch,
2192 .get_authorizer = get_authorizer,
2193 .verify_authorizer_reply = verify_authorizer_reply,
2194 .invalidate_authorizer = invalidate_authorizer,
2195 .alloc_msg = alloc_msg,