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 int 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 */
67 reqhead->snapid = cpu_to_le64(snapid);
70 r = ceph_calc_file_object_mapping(layout, off, plen, bno,
75 dout(" skipping last %llu, final file extent %llu~%llu\n",
76 orig_len - *plen, off, *plen);
78 if (op_has_extent(op->op)) {
79 op->extent.offset = objoff;
80 op->extent.length = objlen;
82 req->r_num_pages = calc_pages_for(off, *plen);
83 req->r_page_alignment = off & ~PAGE_MASK;
84 if (op->op == CEPH_OSD_OP_WRITE)
85 op->payload_len = *plen;
87 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
88 *bno, objoff, objlen, req->r_num_pages);
91 EXPORT_SYMBOL(ceph_calc_raw_layout);
94 * Implement client access to distributed object storage cluster.
96 * All data objects are stored within a cluster/cloud of OSDs, or
97 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
98 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
99 * remote daemons serving up and coordinating consistent and safe
102 * Cluster membership and the mapping of data objects onto storage devices
103 * are described by the osd map.
105 * We keep track of pending OSD requests (read, write), resubmit
106 * requests to different OSDs when the cluster topology/data layout
107 * change, or retry the affected requests when the communications
108 * channel with an OSD is reset.
112 * calculate the mapping of a file extent onto an object, and fill out the
113 * request accordingly. shorten extent as necessary if it crosses an
116 * fill osd op in request message.
118 static int calc_layout(struct ceph_osd_client *osdc,
119 struct ceph_vino vino,
120 struct ceph_file_layout *layout,
122 struct ceph_osd_request *req,
123 struct ceph_osd_req_op *op)
128 r = ceph_calc_raw_layout(osdc, layout, vino.snap, off,
129 plen, &bno, req, op);
133 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
134 req->r_oid_len = strlen(req->r_oid);
142 void ceph_osdc_release_request(struct kref *kref)
144 struct ceph_osd_request *req = container_of(kref,
145 struct ceph_osd_request,
149 ceph_msg_put(req->r_request);
150 if (req->r_con_filling_msg) {
151 dout("%s revoking pages %p from con %p\n", __func__,
152 req->r_pages, req->r_con_filling_msg);
153 ceph_msg_revoke_incoming(req->r_reply);
154 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
157 ceph_msg_put(req->r_reply);
158 if (req->r_own_pages)
159 ceph_release_page_vector(req->r_pages,
165 ceph_put_snap_context(req->r_snapc);
167 ceph_pagelist_release(req->r_trail);
171 mempool_free(req, req->r_osdc->req_mempool);
175 EXPORT_SYMBOL(ceph_osdc_release_request);
177 static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
184 if (needs_trail && op_needs_trail(ops[i].op))
192 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
194 struct ceph_snap_context *snapc,
195 struct ceph_osd_req_op *ops,
201 struct ceph_osd_request *req;
202 struct ceph_msg *msg;
204 int num_op = get_num_ops(ops, &needs_trail);
205 size_t msg_size = sizeof(struct ceph_osd_request_head);
207 msg_size += num_op*sizeof(struct ceph_osd_op);
210 req = mempool_alloc(osdc->req_mempool, gfp_flags);
211 memset(req, 0, sizeof(*req));
213 req = kzalloc(sizeof(*req), gfp_flags);
219 req->r_mempool = use_mempool;
221 kref_init(&req->r_kref);
222 init_completion(&req->r_completion);
223 init_completion(&req->r_safe_completion);
224 rb_init_node(&req->r_node);
225 INIT_LIST_HEAD(&req->r_unsafe_item);
226 INIT_LIST_HEAD(&req->r_linger_item);
227 INIT_LIST_HEAD(&req->r_linger_osd);
228 INIT_LIST_HEAD(&req->r_req_lru_item);
229 INIT_LIST_HEAD(&req->r_osd_item);
231 req->r_flags = flags;
233 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
235 /* create reply message */
237 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
239 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
240 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
242 ceph_osdc_put_request(req);
247 /* allocate space for the trailing data */
249 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
251 ceph_osdc_put_request(req);
254 ceph_pagelist_init(req->r_trail);
257 /* create request message; allow space for oid */
258 msg_size += MAX_OBJ_NAME_SIZE;
260 msg_size += sizeof(u64) * snapc->num_snaps;
262 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
264 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
266 ceph_osdc_put_request(req);
270 memset(msg->front.iov_base, 0, msg->front.iov_len);
272 req->r_request = msg;
273 req->r_pages = pages;
283 EXPORT_SYMBOL(ceph_osdc_alloc_request);
285 static void osd_req_encode_op(struct ceph_osd_request *req,
286 struct ceph_osd_op *dst,
287 struct ceph_osd_req_op *src)
289 dst->op = cpu_to_le16(src->op);
292 case CEPH_OSD_OP_READ:
293 case CEPH_OSD_OP_WRITE:
295 cpu_to_le64(src->extent.offset);
297 cpu_to_le64(src->extent.length);
298 dst->extent.truncate_size =
299 cpu_to_le64(src->extent.truncate_size);
300 dst->extent.truncate_seq =
301 cpu_to_le32(src->extent.truncate_seq);
304 case CEPH_OSD_OP_GETXATTR:
305 case CEPH_OSD_OP_SETXATTR:
306 case CEPH_OSD_OP_CMPXATTR:
307 BUG_ON(!req->r_trail);
309 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
310 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
311 dst->xattr.cmp_op = src->xattr.cmp_op;
312 dst->xattr.cmp_mode = src->xattr.cmp_mode;
313 ceph_pagelist_append(req->r_trail, src->xattr.name,
314 src->xattr.name_len);
315 ceph_pagelist_append(req->r_trail, src->xattr.val,
316 src->xattr.value_len);
318 case CEPH_OSD_OP_CALL:
319 BUG_ON(!req->r_trail);
321 dst->cls.class_len = src->cls.class_len;
322 dst->cls.method_len = src->cls.method_len;
323 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
325 ceph_pagelist_append(req->r_trail, src->cls.class_name,
327 ceph_pagelist_append(req->r_trail, src->cls.method_name,
328 src->cls.method_len);
329 ceph_pagelist_append(req->r_trail, src->cls.indata,
330 src->cls.indata_len);
332 case CEPH_OSD_OP_ROLLBACK:
333 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
335 case CEPH_OSD_OP_STARTSYNC:
337 case CEPH_OSD_OP_NOTIFY:
339 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
340 __le32 timeout = cpu_to_le32(src->watch.timeout);
342 BUG_ON(!req->r_trail);
344 ceph_pagelist_append(req->r_trail,
345 &prot_ver, sizeof(prot_ver));
346 ceph_pagelist_append(req->r_trail,
347 &timeout, sizeof(timeout));
349 case CEPH_OSD_OP_NOTIFY_ACK:
350 case CEPH_OSD_OP_WATCH:
351 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
352 dst->watch.ver = cpu_to_le64(src->watch.ver);
353 dst->watch.flag = src->watch.flag;
356 pr_err("unrecognized osd opcode %d\n", dst->op);
360 dst->payload_len = cpu_to_le32(src->payload_len);
364 * build new request AND message
367 void ceph_osdc_build_request(struct ceph_osd_request *req,
369 struct ceph_osd_req_op *src_ops,
370 struct ceph_snap_context *snapc,
371 struct timespec *mtime,
375 struct ceph_msg *msg = req->r_request;
376 struct ceph_osd_request_head *head;
377 struct ceph_osd_req_op *src_op;
378 struct ceph_osd_op *op;
380 int num_op = get_num_ops(src_ops, NULL);
381 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
382 int flags = req->r_flags;
386 head = msg->front.iov_base;
387 op = (void *)(head + 1);
388 p = (void *)(op + num_op);
390 req->r_snapc = ceph_get_snap_context(snapc);
392 head->client_inc = cpu_to_le32(1); /* always, for now. */
393 head->flags = cpu_to_le32(flags);
394 if (flags & CEPH_OSD_FLAG_WRITE)
395 ceph_encode_timespec(&head->mtime, mtime);
396 head->num_ops = cpu_to_le16(num_op);
400 head->object_len = cpu_to_le32(oid_len);
401 memcpy(p, oid, oid_len);
406 osd_req_encode_op(req, op, src_op);
412 data_len += req->r_trail->length;
415 head->snap_seq = cpu_to_le64(snapc->seq);
416 head->num_snaps = cpu_to_le32(snapc->num_snaps);
417 for (i = 0; i < snapc->num_snaps; i++) {
418 put_unaligned_le64(snapc->snaps[i], p);
423 if (flags & CEPH_OSD_FLAG_WRITE) {
424 req->r_request->hdr.data_off = cpu_to_le16(off);
425 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
426 } else if (data_len) {
427 req->r_request->hdr.data_off = 0;
428 req->r_request->hdr.data_len = cpu_to_le32(data_len);
431 req->r_request->page_alignment = req->r_page_alignment;
433 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
434 msg_size = p - msg->front.iov_base;
435 msg->front.iov_len = msg_size;
436 msg->hdr.front_len = cpu_to_le32(msg_size);
439 EXPORT_SYMBOL(ceph_osdc_build_request);
442 * build new request AND message, calculate layout, and adjust file
445 * if the file was recently truncated, we include information about its
446 * old and new size so that the object can be updated appropriately. (we
447 * avoid synchronously deleting truncated objects because it's slow.)
449 * if @do_sync, include a 'startsync' command so that the osd will flush
452 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
453 struct ceph_file_layout *layout,
454 struct ceph_vino vino,
456 int opcode, int flags,
457 struct ceph_snap_context *snapc,
461 struct timespec *mtime,
462 bool use_mempool, int num_reply,
465 struct ceph_osd_req_op ops[3];
466 struct ceph_osd_request *req;
470 ops[0].extent.truncate_seq = truncate_seq;
471 ops[0].extent.truncate_size = truncate_size;
472 ops[0].payload_len = 0;
475 ops[1].op = CEPH_OSD_OP_STARTSYNC;
476 ops[1].payload_len = 0;
481 req = ceph_osdc_alloc_request(osdc, flags,
484 GFP_NOFS, NULL, NULL);
486 return ERR_PTR(-ENOMEM);
488 /* calculate max write size */
489 r = calc_layout(osdc, vino, layout, off, plen, req, ops);
492 req->r_file_layout = *layout; /* keep a copy */
494 /* in case it differs from natural (file) alignment that
495 calc_layout filled in for us */
496 req->r_num_pages = calc_pages_for(page_align, *plen);
497 req->r_page_alignment = page_align;
499 ceph_osdc_build_request(req, off, plen, ops,
502 req->r_oid, req->r_oid_len);
506 EXPORT_SYMBOL(ceph_osdc_new_request);
509 * We keep osd requests in an rbtree, sorted by ->r_tid.
511 static void __insert_request(struct ceph_osd_client *osdc,
512 struct ceph_osd_request *new)
514 struct rb_node **p = &osdc->requests.rb_node;
515 struct rb_node *parent = NULL;
516 struct ceph_osd_request *req = NULL;
520 req = rb_entry(parent, struct ceph_osd_request, r_node);
521 if (new->r_tid < req->r_tid)
523 else if (new->r_tid > req->r_tid)
529 rb_link_node(&new->r_node, parent, p);
530 rb_insert_color(&new->r_node, &osdc->requests);
533 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
536 struct ceph_osd_request *req;
537 struct rb_node *n = osdc->requests.rb_node;
540 req = rb_entry(n, struct ceph_osd_request, r_node);
541 if (tid < req->r_tid)
543 else if (tid > req->r_tid)
551 static struct ceph_osd_request *
552 __lookup_request_ge(struct ceph_osd_client *osdc,
555 struct ceph_osd_request *req;
556 struct rb_node *n = osdc->requests.rb_node;
559 req = rb_entry(n, struct ceph_osd_request, r_node);
560 if (tid < req->r_tid) {
564 } else if (tid > req->r_tid) {
574 * Resubmit requests pending on the given osd.
576 static void __kick_osd_requests(struct ceph_osd_client *osdc,
577 struct ceph_osd *osd)
579 struct ceph_osd_request *req, *nreq;
582 dout("__kick_osd_requests osd%d\n", osd->o_osd);
583 err = __reset_osd(osdc, osd);
587 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
588 list_move(&req->r_req_lru_item, &osdc->req_unsent);
589 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
592 req->r_flags |= CEPH_OSD_FLAG_RETRY;
595 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
598 * reregister request prior to unregistering linger so
599 * that r_osd is preserved.
601 BUG_ON(!list_empty(&req->r_req_lru_item));
602 __register_request(osdc, req);
603 list_add(&req->r_req_lru_item, &osdc->req_unsent);
604 list_add(&req->r_osd_item, &req->r_osd->o_requests);
605 __unregister_linger_request(osdc, req);
606 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
611 static void kick_osd_requests(struct ceph_osd_client *osdc,
612 struct ceph_osd *kickosd)
614 mutex_lock(&osdc->request_mutex);
615 __kick_osd_requests(osdc, kickosd);
616 mutex_unlock(&osdc->request_mutex);
620 * If the osd connection drops, we need to resubmit all requests.
622 static void osd_reset(struct ceph_connection *con)
624 struct ceph_osd *osd = con->private;
625 struct ceph_osd_client *osdc;
629 dout("osd_reset osd%d\n", osd->o_osd);
631 down_read(&osdc->map_sem);
632 kick_osd_requests(osdc, osd);
634 up_read(&osdc->map_sem);
638 * Track open sessions with osds.
640 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
642 struct ceph_osd *osd;
644 osd = kzalloc(sizeof(*osd), GFP_NOFS);
648 atomic_set(&osd->o_ref, 1);
651 INIT_LIST_HEAD(&osd->o_requests);
652 INIT_LIST_HEAD(&osd->o_linger_requests);
653 INIT_LIST_HEAD(&osd->o_osd_lru);
654 osd->o_incarnation = 1;
656 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
658 INIT_LIST_HEAD(&osd->o_keepalive_item);
662 static struct ceph_osd *get_osd(struct ceph_osd *osd)
664 if (atomic_inc_not_zero(&osd->o_ref)) {
665 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
666 atomic_read(&osd->o_ref));
669 dout("get_osd %p FAIL\n", osd);
674 static void put_osd(struct ceph_osd *osd)
676 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
677 atomic_read(&osd->o_ref) - 1);
678 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
679 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
681 if (ac->ops && ac->ops->destroy_authorizer)
682 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
688 * remove an osd from our map
690 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
692 dout("__remove_osd %p\n", osd);
693 BUG_ON(!list_empty(&osd->o_requests));
694 rb_erase(&osd->o_node, &osdc->osds);
695 list_del_init(&osd->o_osd_lru);
696 ceph_con_close(&osd->o_con);
700 static void remove_all_osds(struct ceph_osd_client *osdc)
702 dout("%s %p\n", __func__, osdc);
703 mutex_lock(&osdc->request_mutex);
704 while (!RB_EMPTY_ROOT(&osdc->osds)) {
705 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
706 struct ceph_osd, o_node);
707 __remove_osd(osdc, osd);
709 mutex_unlock(&osdc->request_mutex);
712 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
713 struct ceph_osd *osd)
715 dout("__move_osd_to_lru %p\n", osd);
716 BUG_ON(!list_empty(&osd->o_osd_lru));
717 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
718 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
721 static void __remove_osd_from_lru(struct ceph_osd *osd)
723 dout("__remove_osd_from_lru %p\n", osd);
724 if (!list_empty(&osd->o_osd_lru))
725 list_del_init(&osd->o_osd_lru);
728 static void remove_old_osds(struct ceph_osd_client *osdc)
730 struct ceph_osd *osd, *nosd;
732 dout("__remove_old_osds %p\n", osdc);
733 mutex_lock(&osdc->request_mutex);
734 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
735 if (time_before(jiffies, osd->lru_ttl))
737 __remove_osd(osdc, osd);
739 mutex_unlock(&osdc->request_mutex);
745 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
747 struct ceph_osd_request *req;
750 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
751 if (list_empty(&osd->o_requests) &&
752 list_empty(&osd->o_linger_requests)) {
753 __remove_osd(osdc, osd);
754 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
755 &osd->o_con.peer_addr,
756 sizeof(osd->o_con.peer_addr)) == 0 &&
757 !ceph_con_opened(&osd->o_con)) {
758 dout(" osd addr hasn't changed and connection never opened,"
759 " letting msgr retry");
760 /* touch each r_stamp for handle_timeout()'s benfit */
761 list_for_each_entry(req, &osd->o_requests, r_osd_item)
762 req->r_stamp = jiffies;
765 ceph_con_close(&osd->o_con);
766 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
767 &osdc->osdmap->osd_addr[osd->o_osd]);
768 osd->o_incarnation++;
773 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
775 struct rb_node **p = &osdc->osds.rb_node;
776 struct rb_node *parent = NULL;
777 struct ceph_osd *osd = NULL;
779 dout("__insert_osd %p osd%d\n", new, new->o_osd);
782 osd = rb_entry(parent, struct ceph_osd, o_node);
783 if (new->o_osd < osd->o_osd)
785 else if (new->o_osd > osd->o_osd)
791 rb_link_node(&new->o_node, parent, p);
792 rb_insert_color(&new->o_node, &osdc->osds);
795 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
797 struct ceph_osd *osd;
798 struct rb_node *n = osdc->osds.rb_node;
801 osd = rb_entry(n, struct ceph_osd, o_node);
804 else if (o > osd->o_osd)
812 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
814 schedule_delayed_work(&osdc->timeout_work,
815 osdc->client->options->osd_keepalive_timeout * HZ);
818 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
820 cancel_delayed_work(&osdc->timeout_work);
824 * Register request, assign tid. If this is the first request, set up
827 static void __register_request(struct ceph_osd_client *osdc,
828 struct ceph_osd_request *req)
830 req->r_tid = ++osdc->last_tid;
831 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
832 dout("__register_request %p tid %lld\n", req, req->r_tid);
833 __insert_request(osdc, req);
834 ceph_osdc_get_request(req);
835 osdc->num_requests++;
836 if (osdc->num_requests == 1) {
837 dout(" first request, scheduling timeout\n");
838 __schedule_osd_timeout(osdc);
842 static void register_request(struct ceph_osd_client *osdc,
843 struct ceph_osd_request *req)
845 mutex_lock(&osdc->request_mutex);
846 __register_request(osdc, req);
847 mutex_unlock(&osdc->request_mutex);
851 * called under osdc->request_mutex
853 static void __unregister_request(struct ceph_osd_client *osdc,
854 struct ceph_osd_request *req)
856 if (RB_EMPTY_NODE(&req->r_node)) {
857 dout("__unregister_request %p tid %lld not registered\n",
862 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
863 rb_erase(&req->r_node, &osdc->requests);
864 osdc->num_requests--;
867 /* make sure the original request isn't in flight. */
868 ceph_msg_revoke(req->r_request);
870 list_del_init(&req->r_osd_item);
871 if (list_empty(&req->r_osd->o_requests) &&
872 list_empty(&req->r_osd->o_linger_requests)) {
873 dout("moving osd to %p lru\n", req->r_osd);
874 __move_osd_to_lru(osdc, req->r_osd);
876 if (list_empty(&req->r_linger_item))
880 ceph_osdc_put_request(req);
882 list_del_init(&req->r_req_lru_item);
883 if (osdc->num_requests == 0) {
884 dout(" no requests, canceling timeout\n");
885 __cancel_osd_timeout(osdc);
890 * Cancel a previously queued request message
892 static void __cancel_request(struct ceph_osd_request *req)
894 if (req->r_sent && req->r_osd) {
895 ceph_msg_revoke(req->r_request);
900 static void __register_linger_request(struct ceph_osd_client *osdc,
901 struct ceph_osd_request *req)
903 dout("__register_linger_request %p\n", req);
904 list_add_tail(&req->r_linger_item, &osdc->req_linger);
906 list_add_tail(&req->r_linger_osd,
907 &req->r_osd->o_linger_requests);
910 static void __unregister_linger_request(struct ceph_osd_client *osdc,
911 struct ceph_osd_request *req)
913 dout("__unregister_linger_request %p\n", req);
915 list_del_init(&req->r_linger_item);
916 list_del_init(&req->r_linger_osd);
918 if (list_empty(&req->r_osd->o_requests) &&
919 list_empty(&req->r_osd->o_linger_requests)) {
920 dout("moving osd to %p lru\n", req->r_osd);
921 __move_osd_to_lru(osdc, req->r_osd);
923 if (list_empty(&req->r_osd_item))
928 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
929 struct ceph_osd_request *req)
931 mutex_lock(&osdc->request_mutex);
933 __unregister_linger_request(osdc, req);
934 ceph_osdc_put_request(req);
936 mutex_unlock(&osdc->request_mutex);
938 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
940 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
941 struct ceph_osd_request *req)
943 if (!req->r_linger) {
944 dout("set_request_linger %p\n", req);
947 * caller is now responsible for calling
948 * unregister_linger_request
950 ceph_osdc_get_request(req);
953 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
956 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
957 * (as needed), and set the request r_osd appropriately. If there is
958 * no up osd, set r_osd to NULL. Move the request to the appropriate list
959 * (unsent, homeless) or leave on in-flight lru.
961 * Return 0 if unchanged, 1 if changed, or negative on error.
963 * Caller should hold map_sem for read and request_mutex.
965 static int __map_request(struct ceph_osd_client *osdc,
966 struct ceph_osd_request *req, int force_resend)
968 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
970 int acting[CEPH_PG_MAX_SIZE];
974 dout("map_request %p tid %lld\n", req, req->r_tid);
975 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
976 &req->r_file_layout, osdc->osdmap);
978 list_move(&req->r_req_lru_item, &osdc->req_notarget);
981 pgid = reqhead->layout.ol_pgid;
984 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
990 if ((!force_resend &&
991 req->r_osd && req->r_osd->o_osd == o &&
992 req->r_sent >= req->r_osd->o_incarnation &&
993 req->r_num_pg_osds == num &&
994 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
995 (req->r_osd == NULL && o == -1))
996 return 0; /* no change */
998 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
999 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
1000 req->r_osd ? req->r_osd->o_osd : -1);
1002 /* record full pg acting set */
1003 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1004 req->r_num_pg_osds = num;
1007 __cancel_request(req);
1008 list_del_init(&req->r_osd_item);
1012 req->r_osd = __lookup_osd(osdc, o);
1013 if (!req->r_osd && o >= 0) {
1015 req->r_osd = create_osd(osdc, o);
1017 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1021 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1022 __insert_osd(osdc, req->r_osd);
1024 ceph_con_open(&req->r_osd->o_con,
1025 CEPH_ENTITY_TYPE_OSD, o,
1026 &osdc->osdmap->osd_addr[o]);
1030 __remove_osd_from_lru(req->r_osd);
1031 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1032 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1034 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1036 err = 1; /* osd or pg changed */
1043 * caller should hold map_sem (for read) and request_mutex
1045 static void __send_request(struct ceph_osd_client *osdc,
1046 struct ceph_osd_request *req)
1048 struct ceph_osd_request_head *reqhead;
1050 dout("send_request %p tid %llu to osd%d flags %d\n",
1051 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1053 reqhead = req->r_request->front.iov_base;
1054 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1055 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1056 reqhead->reassert_version = req->r_reassert_version;
1058 req->r_stamp = jiffies;
1059 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1061 ceph_msg_get(req->r_request); /* send consumes a ref */
1062 ceph_con_send(&req->r_osd->o_con, req->r_request);
1063 req->r_sent = req->r_osd->o_incarnation;
1067 * Send any requests in the queue (req_unsent).
1069 static void send_queued(struct ceph_osd_client *osdc)
1071 struct ceph_osd_request *req, *tmp;
1073 dout("send_queued\n");
1074 mutex_lock(&osdc->request_mutex);
1075 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1076 __send_request(osdc, req);
1078 mutex_unlock(&osdc->request_mutex);
1082 * Timeout callback, called every N seconds when 1 or more osd
1083 * requests has been active for more than N seconds. When this
1084 * happens, we ping all OSDs with requests who have timed out to
1085 * ensure any communications channel reset is detected. Reset the
1086 * request timeouts another N seconds in the future as we go.
1087 * Reschedule the timeout event another N seconds in future (unless
1088 * there are no open requests).
1090 static void handle_timeout(struct work_struct *work)
1092 struct ceph_osd_client *osdc =
1093 container_of(work, struct ceph_osd_client, timeout_work.work);
1094 struct ceph_osd_request *req, *last_req = NULL;
1095 struct ceph_osd *osd;
1096 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
1097 unsigned long keepalive =
1098 osdc->client->options->osd_keepalive_timeout * HZ;
1099 unsigned long last_stamp = 0;
1100 struct list_head slow_osds;
1102 down_read(&osdc->map_sem);
1104 ceph_monc_request_next_osdmap(&osdc->client->monc);
1106 mutex_lock(&osdc->request_mutex);
1109 * reset osds that appear to be _really_ unresponsive. this
1110 * is a failsafe measure.. we really shouldn't be getting to
1111 * this point if the system is working properly. the monitors
1112 * should mark the osd as failed and we should find out about
1113 * it from an updated osd map.
1115 while (timeout && !list_empty(&osdc->req_lru)) {
1116 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1119 /* hasn't been long enough since we sent it? */
1120 if (time_before(jiffies, req->r_stamp + timeout))
1123 /* hasn't been long enough since it was acked? */
1124 if (req->r_request->ack_stamp == 0 ||
1125 time_before(jiffies, req->r_request->ack_stamp + timeout))
1128 BUG_ON(req == last_req && req->r_stamp == last_stamp);
1130 last_stamp = req->r_stamp;
1134 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1135 req->r_tid, osd->o_osd);
1136 __kick_osd_requests(osdc, osd);
1140 * ping osds that are a bit slow. this ensures that if there
1141 * is a break in the TCP connection we will notice, and reopen
1142 * a connection with that osd (from the fault callback).
1144 INIT_LIST_HEAD(&slow_osds);
1145 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1146 if (time_before(jiffies, req->r_stamp + keepalive))
1151 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1152 req->r_tid, osd->o_osd);
1153 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1155 while (!list_empty(&slow_osds)) {
1156 osd = list_entry(slow_osds.next, struct ceph_osd,
1158 list_del_init(&osd->o_keepalive_item);
1159 ceph_con_keepalive(&osd->o_con);
1162 __schedule_osd_timeout(osdc);
1163 mutex_unlock(&osdc->request_mutex);
1165 up_read(&osdc->map_sem);
1168 static void handle_osds_timeout(struct work_struct *work)
1170 struct ceph_osd_client *osdc =
1171 container_of(work, struct ceph_osd_client,
1172 osds_timeout_work.work);
1173 unsigned long delay =
1174 osdc->client->options->osd_idle_ttl * HZ >> 2;
1176 dout("osds timeout\n");
1177 down_read(&osdc->map_sem);
1178 remove_old_osds(osdc);
1179 up_read(&osdc->map_sem);
1181 schedule_delayed_work(&osdc->osds_timeout_work,
1182 round_jiffies_relative(delay));
1185 static void complete_request(struct ceph_osd_request *req)
1187 if (req->r_safe_callback)
1188 req->r_safe_callback(req, NULL);
1189 complete_all(&req->r_safe_completion); /* fsync waiter */
1193 * handle osd op reply. either call the callback if it is specified,
1194 * or do the completion to wake up the waiting thread.
1196 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1197 struct ceph_connection *con)
1199 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1200 struct ceph_osd_request *req;
1202 int numops, object_len, flags;
1205 tid = le64_to_cpu(msg->hdr.tid);
1206 if (msg->front.iov_len < sizeof(*rhead))
1208 numops = le32_to_cpu(rhead->num_ops);
1209 object_len = le32_to_cpu(rhead->object_len);
1210 result = le32_to_cpu(rhead->result);
1211 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1212 numops * sizeof(struct ceph_osd_op))
1214 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1216 mutex_lock(&osdc->request_mutex);
1217 req = __lookup_request(osdc, tid);
1219 dout("handle_reply tid %llu dne\n", tid);
1220 mutex_unlock(&osdc->request_mutex);
1223 ceph_osdc_get_request(req);
1224 flags = le32_to_cpu(rhead->flags);
1227 * if this connection filled our message, drop our reference now, to
1228 * avoid a (safe but slower) revoke later.
1230 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1231 dout(" dropping con_filling_msg ref %p\n", con);
1232 req->r_con_filling_msg = NULL;
1236 if (!req->r_got_reply) {
1239 req->r_result = le32_to_cpu(rhead->result);
1240 bytes = le32_to_cpu(msg->hdr.data_len);
1241 dout("handle_reply result %d bytes %d\n", req->r_result,
1243 if (req->r_result == 0)
1244 req->r_result = bytes;
1246 /* in case this is a write and we need to replay, */
1247 req->r_reassert_version = rhead->reassert_version;
1249 req->r_got_reply = 1;
1250 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1251 dout("handle_reply tid %llu dup ack\n", tid);
1252 mutex_unlock(&osdc->request_mutex);
1256 dout("handle_reply tid %llu flags %d\n", tid, flags);
1258 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1259 __register_linger_request(osdc, req);
1261 /* either this is a read, or we got the safe response */
1263 (flags & CEPH_OSD_FLAG_ONDISK) ||
1264 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1265 __unregister_request(osdc, req);
1267 mutex_unlock(&osdc->request_mutex);
1269 if (req->r_callback)
1270 req->r_callback(req, msg);
1272 complete_all(&req->r_completion);
1274 if (flags & CEPH_OSD_FLAG_ONDISK)
1275 complete_request(req);
1278 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1279 ceph_osdc_put_request(req);
1283 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1284 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1285 (int)sizeof(*rhead));
1289 static void reset_changed_osds(struct ceph_osd_client *osdc)
1291 struct rb_node *p, *n;
1293 for (p = rb_first(&osdc->osds); p; p = n) {
1294 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1297 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1298 memcmp(&osd->o_con.peer_addr,
1299 ceph_osd_addr(osdc->osdmap,
1301 sizeof(struct ceph_entity_addr)) != 0)
1302 __reset_osd(osdc, osd);
1307 * Requeue requests whose mapping to an OSD has changed. If requests map to
1308 * no osd, request a new map.
1310 * Caller should hold map_sem for read and request_mutex.
1312 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1314 struct ceph_osd_request *req, *nreq;
1319 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1320 mutex_lock(&osdc->request_mutex);
1321 for (p = rb_first(&osdc->requests); p; ) {
1322 req = rb_entry(p, struct ceph_osd_request, r_node);
1324 err = __map_request(osdc, req, force_resend);
1326 continue; /* error */
1327 if (req->r_osd == NULL) {
1328 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1329 needmap++; /* request a newer map */
1330 } else if (err > 0) {
1331 if (!req->r_linger) {
1332 dout("%p tid %llu requeued on osd%d\n", req,
1334 req->r_osd ? req->r_osd->o_osd : -1);
1335 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1338 if (req->r_linger && list_empty(&req->r_linger_item)) {
1340 * register as a linger so that we will
1341 * re-submit below and get a new tid
1343 dout("%p tid %llu restart on osd%d\n",
1345 req->r_osd ? req->r_osd->o_osd : -1);
1346 __register_linger_request(osdc, req);
1347 __unregister_request(osdc, req);
1351 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1353 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1355 err = __map_request(osdc, req, force_resend);
1357 continue; /* no change and no osd was specified */
1359 continue; /* hrm! */
1360 if (req->r_osd == NULL) {
1361 dout("tid %llu maps to no valid osd\n", req->r_tid);
1362 needmap++; /* request a newer map */
1366 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1367 req->r_osd ? req->r_osd->o_osd : -1);
1368 __unregister_linger_request(osdc, req);
1369 __register_request(osdc, req);
1371 mutex_unlock(&osdc->request_mutex);
1374 dout("%d requests for down osds, need new map\n", needmap);
1375 ceph_monc_request_next_osdmap(&osdc->client->monc);
1381 * Process updated osd map.
1383 * The message contains any number of incremental and full maps, normally
1384 * indicating some sort of topology change in the cluster. Kick requests
1385 * off to different OSDs as needed.
1387 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1389 void *p, *end, *next;
1390 u32 nr_maps, maplen;
1392 struct ceph_osdmap *newmap = NULL, *oldmap;
1394 struct ceph_fsid fsid;
1396 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1397 p = msg->front.iov_base;
1398 end = p + msg->front.iov_len;
1401 ceph_decode_need(&p, end, sizeof(fsid), bad);
1402 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1403 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1406 down_write(&osdc->map_sem);
1408 /* incremental maps */
1409 ceph_decode_32_safe(&p, end, nr_maps, bad);
1410 dout(" %d inc maps\n", nr_maps);
1411 while (nr_maps > 0) {
1412 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1413 epoch = ceph_decode_32(&p);
1414 maplen = ceph_decode_32(&p);
1415 ceph_decode_need(&p, end, maplen, bad);
1417 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1418 dout("applying incremental map %u len %d\n",
1420 newmap = osdmap_apply_incremental(&p, next,
1422 &osdc->client->msgr);
1423 if (IS_ERR(newmap)) {
1424 err = PTR_ERR(newmap);
1428 if (newmap != osdc->osdmap) {
1429 ceph_osdmap_destroy(osdc->osdmap);
1430 osdc->osdmap = newmap;
1432 kick_requests(osdc, 0);
1433 reset_changed_osds(osdc);
1435 dout("ignoring incremental map %u len %d\n",
1445 ceph_decode_32_safe(&p, end, nr_maps, bad);
1446 dout(" %d full maps\n", nr_maps);
1448 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1449 epoch = ceph_decode_32(&p);
1450 maplen = ceph_decode_32(&p);
1451 ceph_decode_need(&p, end, maplen, bad);
1453 dout("skipping non-latest full map %u len %d\n",
1455 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1456 dout("skipping full map %u len %d, "
1457 "older than our %u\n", epoch, maplen,
1458 osdc->osdmap->epoch);
1460 int skipped_map = 0;
1462 dout("taking full map %u len %d\n", epoch, maplen);
1463 newmap = osdmap_decode(&p, p+maplen);
1464 if (IS_ERR(newmap)) {
1465 err = PTR_ERR(newmap);
1469 oldmap = osdc->osdmap;
1470 osdc->osdmap = newmap;
1472 if (oldmap->epoch + 1 < newmap->epoch)
1474 ceph_osdmap_destroy(oldmap);
1476 kick_requests(osdc, skipped_map);
1483 downgrade_write(&osdc->map_sem);
1484 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1487 * subscribe to subsequent osdmap updates if full to ensure
1488 * we find out when we are no longer full and stop returning
1491 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1492 ceph_monc_request_next_osdmap(&osdc->client->monc);
1495 up_read(&osdc->map_sem);
1496 wake_up_all(&osdc->client->auth_wq);
1500 pr_err("osdc handle_map corrupt msg\n");
1502 up_write(&osdc->map_sem);
1507 * watch/notify callback event infrastructure
1509 * These callbacks are used both for watch and notify operations.
1511 static void __release_event(struct kref *kref)
1513 struct ceph_osd_event *event =
1514 container_of(kref, struct ceph_osd_event, kref);
1516 dout("__release_event %p\n", event);
1520 static void get_event(struct ceph_osd_event *event)
1522 kref_get(&event->kref);
1525 void ceph_osdc_put_event(struct ceph_osd_event *event)
1527 kref_put(&event->kref, __release_event);
1529 EXPORT_SYMBOL(ceph_osdc_put_event);
1531 static void __insert_event(struct ceph_osd_client *osdc,
1532 struct ceph_osd_event *new)
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 (new->cookie < event->cookie)
1543 else if (new->cookie > event->cookie)
1544 p = &(*p)->rb_right;
1549 rb_link_node(&new->node, parent, p);
1550 rb_insert_color(&new->node, &osdc->event_tree);
1553 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1556 struct rb_node **p = &osdc->event_tree.rb_node;
1557 struct rb_node *parent = NULL;
1558 struct ceph_osd_event *event = NULL;
1562 event = rb_entry(parent, struct ceph_osd_event, node);
1563 if (cookie < event->cookie)
1565 else if (cookie > event->cookie)
1566 p = &(*p)->rb_right;
1573 static void __remove_event(struct ceph_osd_event *event)
1575 struct ceph_osd_client *osdc = event->osdc;
1577 if (!RB_EMPTY_NODE(&event->node)) {
1578 dout("__remove_event removed %p\n", event);
1579 rb_erase(&event->node, &osdc->event_tree);
1580 ceph_osdc_put_event(event);
1582 dout("__remove_event didn't remove %p\n", event);
1586 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1587 void (*event_cb)(u64, u64, u8, void *),
1588 int one_shot, void *data,
1589 struct ceph_osd_event **pevent)
1591 struct ceph_osd_event *event;
1593 event = kmalloc(sizeof(*event), GFP_NOIO);
1597 dout("create_event %p\n", event);
1598 event->cb = event_cb;
1599 event->one_shot = one_shot;
1602 INIT_LIST_HEAD(&event->osd_node);
1603 kref_init(&event->kref); /* one ref for us */
1604 kref_get(&event->kref); /* one ref for the caller */
1605 init_completion(&event->completion);
1607 spin_lock(&osdc->event_lock);
1608 event->cookie = ++osdc->event_count;
1609 __insert_event(osdc, event);
1610 spin_unlock(&osdc->event_lock);
1615 EXPORT_SYMBOL(ceph_osdc_create_event);
1617 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1619 struct ceph_osd_client *osdc = event->osdc;
1621 dout("cancel_event %p\n", event);
1622 spin_lock(&osdc->event_lock);
1623 __remove_event(event);
1624 spin_unlock(&osdc->event_lock);
1625 ceph_osdc_put_event(event); /* caller's */
1627 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1630 static void do_event_work(struct work_struct *work)
1632 struct ceph_osd_event_work *event_work =
1633 container_of(work, struct ceph_osd_event_work, work);
1634 struct ceph_osd_event *event = event_work->event;
1635 u64 ver = event_work->ver;
1636 u64 notify_id = event_work->notify_id;
1637 u8 opcode = event_work->opcode;
1639 dout("do_event_work completing %p\n", event);
1640 event->cb(ver, notify_id, opcode, event->data);
1641 complete(&event->completion);
1642 dout("do_event_work completed %p\n", event);
1643 ceph_osdc_put_event(event);
1649 * Process osd watch notifications
1651 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1655 u64 cookie, ver, notify_id;
1657 struct ceph_osd_event *event;
1658 struct ceph_osd_event_work *event_work;
1660 p = msg->front.iov_base;
1661 end = p + msg->front.iov_len;
1663 ceph_decode_8_safe(&p, end, proto_ver, bad);
1664 ceph_decode_8_safe(&p, end, opcode, bad);
1665 ceph_decode_64_safe(&p, end, cookie, bad);
1666 ceph_decode_64_safe(&p, end, ver, bad);
1667 ceph_decode_64_safe(&p, end, notify_id, bad);
1669 spin_lock(&osdc->event_lock);
1670 event = __find_event(osdc, cookie);
1673 if (event->one_shot)
1674 __remove_event(event);
1676 spin_unlock(&osdc->event_lock);
1677 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1678 cookie, ver, event);
1680 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1682 dout("ERROR: could not allocate event_work\n");
1685 INIT_WORK(&event_work->work, do_event_work);
1686 event_work->event = event;
1687 event_work->ver = ver;
1688 event_work->notify_id = notify_id;
1689 event_work->opcode = opcode;
1690 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1691 dout("WARNING: failed to queue notify event work\n");
1699 complete(&event->completion);
1700 ceph_osdc_put_event(event);
1704 pr_err("osdc handle_watch_notify corrupt msg\n");
1708 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1712 dout("wait_event %p\n", event);
1713 err = wait_for_completion_interruptible_timeout(&event->completion,
1715 ceph_osdc_put_event(event);
1718 dout("wait_event %p returns %d\n", event, err);
1721 EXPORT_SYMBOL(ceph_osdc_wait_event);
1724 * Register request, send initial attempt.
1726 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1727 struct ceph_osd_request *req,
1732 req->r_request->pages = req->r_pages;
1733 req->r_request->nr_pages = req->r_num_pages;
1735 req->r_request->bio = req->r_bio;
1737 req->r_request->trail = req->r_trail;
1739 register_request(osdc, req);
1741 down_read(&osdc->map_sem);
1742 mutex_lock(&osdc->request_mutex);
1744 * a racing kick_requests() may have sent the message for us
1745 * while we dropped request_mutex above, so only send now if
1746 * the request still han't been touched yet.
1748 if (req->r_sent == 0) {
1749 rc = __map_request(osdc, req, 0);
1752 dout("osdc_start_request failed map, "
1753 " will retry %lld\n", req->r_tid);
1758 if (req->r_osd == NULL) {
1759 dout("send_request %p no up osds in pg\n", req);
1760 ceph_monc_request_next_osdmap(&osdc->client->monc);
1762 __send_request(osdc, req);
1768 mutex_unlock(&osdc->request_mutex);
1769 up_read(&osdc->map_sem);
1772 EXPORT_SYMBOL(ceph_osdc_start_request);
1775 * wait for a request to complete
1777 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1778 struct ceph_osd_request *req)
1782 rc = wait_for_completion_interruptible(&req->r_completion);
1784 mutex_lock(&osdc->request_mutex);
1785 __cancel_request(req);
1786 __unregister_request(osdc, req);
1787 mutex_unlock(&osdc->request_mutex);
1788 complete_request(req);
1789 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1793 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1794 return req->r_result;
1796 EXPORT_SYMBOL(ceph_osdc_wait_request);
1799 * sync - wait for all in-flight requests to flush. avoid starvation.
1801 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1803 struct ceph_osd_request *req;
1804 u64 last_tid, next_tid = 0;
1806 mutex_lock(&osdc->request_mutex);
1807 last_tid = osdc->last_tid;
1809 req = __lookup_request_ge(osdc, next_tid);
1812 if (req->r_tid > last_tid)
1815 next_tid = req->r_tid + 1;
1816 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1819 ceph_osdc_get_request(req);
1820 mutex_unlock(&osdc->request_mutex);
1821 dout("sync waiting on tid %llu (last is %llu)\n",
1822 req->r_tid, last_tid);
1823 wait_for_completion(&req->r_safe_completion);
1824 mutex_lock(&osdc->request_mutex);
1825 ceph_osdc_put_request(req);
1827 mutex_unlock(&osdc->request_mutex);
1828 dout("sync done (thru tid %llu)\n", last_tid);
1830 EXPORT_SYMBOL(ceph_osdc_sync);
1835 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1840 osdc->client = client;
1841 osdc->osdmap = NULL;
1842 init_rwsem(&osdc->map_sem);
1843 init_completion(&osdc->map_waiters);
1844 osdc->last_requested_map = 0;
1845 mutex_init(&osdc->request_mutex);
1847 osdc->osds = RB_ROOT;
1848 INIT_LIST_HEAD(&osdc->osd_lru);
1849 osdc->requests = RB_ROOT;
1850 INIT_LIST_HEAD(&osdc->req_lru);
1851 INIT_LIST_HEAD(&osdc->req_unsent);
1852 INIT_LIST_HEAD(&osdc->req_notarget);
1853 INIT_LIST_HEAD(&osdc->req_linger);
1854 osdc->num_requests = 0;
1855 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1856 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1857 spin_lock_init(&osdc->event_lock);
1858 osdc->event_tree = RB_ROOT;
1859 osdc->event_count = 0;
1861 schedule_delayed_work(&osdc->osds_timeout_work,
1862 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1865 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1866 sizeof(struct ceph_osd_request));
1867 if (!osdc->req_mempool)
1870 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1871 OSD_OP_FRONT_LEN, 10, true,
1875 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1876 OSD_OPREPLY_FRONT_LEN, 10, true,
1881 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1882 if (IS_ERR(osdc->notify_wq)) {
1883 err = PTR_ERR(osdc->notify_wq);
1884 osdc->notify_wq = NULL;
1890 ceph_msgpool_destroy(&osdc->msgpool_op);
1892 mempool_destroy(osdc->req_mempool);
1896 EXPORT_SYMBOL(ceph_osdc_init);
1898 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1900 flush_workqueue(osdc->notify_wq);
1901 destroy_workqueue(osdc->notify_wq);
1902 cancel_delayed_work_sync(&osdc->timeout_work);
1903 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1905 ceph_osdmap_destroy(osdc->osdmap);
1906 osdc->osdmap = NULL;
1908 remove_all_osds(osdc);
1909 mempool_destroy(osdc->req_mempool);
1910 ceph_msgpool_destroy(&osdc->msgpool_op);
1911 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1913 EXPORT_SYMBOL(ceph_osdc_stop);
1916 * Read some contiguous pages. If we cross a stripe boundary, shorten
1917 * *plen. Return number of bytes read, or error.
1919 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1920 struct ceph_vino vino, struct ceph_file_layout *layout,
1922 u32 truncate_seq, u64 truncate_size,
1923 struct page **pages, int num_pages, int page_align)
1925 struct ceph_osd_request *req;
1928 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1929 vino.snap, off, *plen);
1930 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1931 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1932 NULL, 0, truncate_seq, truncate_size, NULL,
1933 false, 1, page_align);
1935 return PTR_ERR(req);
1937 /* it may be a short read due to an object boundary */
1938 req->r_pages = pages;
1940 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1941 off, *plen, req->r_num_pages, page_align);
1943 rc = ceph_osdc_start_request(osdc, req, false);
1945 rc = ceph_osdc_wait_request(osdc, req);
1947 ceph_osdc_put_request(req);
1948 dout("readpages result %d\n", rc);
1951 EXPORT_SYMBOL(ceph_osdc_readpages);
1954 * do a synchronous write on N pages
1956 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1957 struct ceph_file_layout *layout,
1958 struct ceph_snap_context *snapc,
1960 u32 truncate_seq, u64 truncate_size,
1961 struct timespec *mtime,
1962 struct page **pages, int num_pages,
1963 int flags, int do_sync, bool nofail)
1965 struct ceph_osd_request *req;
1967 int page_align = off & ~PAGE_MASK;
1969 BUG_ON(vino.snap != CEPH_NOSNAP);
1970 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1972 flags | CEPH_OSD_FLAG_ONDISK |
1973 CEPH_OSD_FLAG_WRITE,
1975 truncate_seq, truncate_size, mtime,
1976 nofail, 1, page_align);
1978 return PTR_ERR(req);
1980 /* it may be a short write due to an object boundary */
1981 req->r_pages = pages;
1982 dout("writepages %llu~%llu (%d pages)\n", off, len,
1985 rc = ceph_osdc_start_request(osdc, req, nofail);
1987 rc = ceph_osdc_wait_request(osdc, req);
1989 ceph_osdc_put_request(req);
1992 dout("writepages result %d\n", rc);
1995 EXPORT_SYMBOL(ceph_osdc_writepages);
1998 * handle incoming message
2000 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2002 struct ceph_osd *osd = con->private;
2003 struct ceph_osd_client *osdc;
2004 int type = le16_to_cpu(msg->hdr.type);
2011 case CEPH_MSG_OSD_MAP:
2012 ceph_osdc_handle_map(osdc, msg);
2014 case CEPH_MSG_OSD_OPREPLY:
2015 handle_reply(osdc, msg, con);
2017 case CEPH_MSG_WATCH_NOTIFY:
2018 handle_watch_notify(osdc, msg);
2022 pr_err("received unknown message type %d %s\n", type,
2023 ceph_msg_type_name(type));
2030 * lookup and return message for incoming reply. set up reply message
2033 static struct ceph_msg *get_reply(struct ceph_connection *con,
2034 struct ceph_msg_header *hdr,
2037 struct ceph_osd *osd = con->private;
2038 struct ceph_osd_client *osdc = osd->o_osdc;
2040 struct ceph_osd_request *req;
2041 int front = le32_to_cpu(hdr->front_len);
2042 int data_len = le32_to_cpu(hdr->data_len);
2045 tid = le64_to_cpu(hdr->tid);
2046 mutex_lock(&osdc->request_mutex);
2047 req = __lookup_request(osdc, tid);
2051 dout("get_reply unknown tid %llu from osd%d\n", tid,
2056 if (req->r_con_filling_msg) {
2057 dout("%s revoking msg %p from old con %p\n", __func__,
2058 req->r_reply, req->r_con_filling_msg);
2059 ceph_msg_revoke_incoming(req->r_reply);
2060 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2061 req->r_con_filling_msg = NULL;
2064 if (front > req->r_reply->front.iov_len) {
2065 pr_warning("get_reply front %d > preallocated %d\n",
2066 front, (int)req->r_reply->front.iov_len);
2067 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2070 ceph_msg_put(req->r_reply);
2073 m = ceph_msg_get(req->r_reply);
2076 int want = calc_pages_for(req->r_page_alignment, data_len);
2078 if (unlikely(req->r_num_pages < want)) {
2079 pr_warning("tid %lld reply has %d bytes %d pages, we"
2080 " had only %d pages ready\n", tid, data_len,
2081 want, req->r_num_pages);
2087 m->pages = req->r_pages;
2088 m->nr_pages = req->r_num_pages;
2089 m->page_alignment = req->r_page_alignment;
2091 m->bio = req->r_bio;
2095 req->r_con_filling_msg = con->ops->get(con);
2096 dout("get_reply tid %lld %p\n", tid, m);
2099 mutex_unlock(&osdc->request_mutex);
2104 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2105 struct ceph_msg_header *hdr,
2108 struct ceph_osd *osd = con->private;
2109 int type = le16_to_cpu(hdr->type);
2110 int front = le32_to_cpu(hdr->front_len);
2114 case CEPH_MSG_OSD_MAP:
2115 case CEPH_MSG_WATCH_NOTIFY:
2116 return ceph_msg_new(type, front, GFP_NOFS, false);
2117 case CEPH_MSG_OSD_OPREPLY:
2118 return get_reply(con, hdr, skip);
2120 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2128 * Wrappers to refcount containing ceph_osd struct
2130 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2132 struct ceph_osd *osd = con->private;
2138 static void put_osd_con(struct ceph_connection *con)
2140 struct ceph_osd *osd = con->private;
2148 * Note: returned pointer is the address of a structure that's
2149 * managed separately. Caller must *not* attempt to free it.
2151 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2152 int *proto, int force_new)
2154 struct ceph_osd *o = con->private;
2155 struct ceph_osd_client *osdc = o->o_osdc;
2156 struct ceph_auth_client *ac = osdc->client->monc.auth;
2157 struct ceph_auth_handshake *auth = &o->o_auth;
2159 if (force_new && auth->authorizer) {
2160 if (ac->ops && ac->ops->destroy_authorizer)
2161 ac->ops->destroy_authorizer(ac, auth->authorizer);
2162 auth->authorizer = NULL;
2164 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2165 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2168 return ERR_PTR(ret);
2170 *proto = ac->protocol;
2176 static int verify_authorizer_reply(struct ceph_connection *con, int len)
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;
2183 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2184 * XXX which do we do: succeed or fail?
2186 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2189 static int invalidate_authorizer(struct ceph_connection *con)
2191 struct ceph_osd *o = con->private;
2192 struct ceph_osd_client *osdc = o->o_osdc;
2193 struct ceph_auth_client *ac = osdc->client->monc.auth;
2195 if (ac->ops && ac->ops->invalidate_authorizer)
2196 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2198 return ceph_monc_validate_auth(&osdc->client->monc);
2201 static const struct ceph_connection_operations osd_con_ops = {
2204 .dispatch = dispatch,
2205 .get_authorizer = get_authorizer,
2206 .verify_authorizer_reply = verify_authorizer_reply,
2207 .invalidate_authorizer = invalidate_authorizer,
2208 .alloc_msg = alloc_msg,