1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops;
26 static void send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
35 static int op_needs_trail(int op)
38 case CEPH_OSD_OP_GETXATTR:
39 case CEPH_OSD_OP_SETXATTR:
40 case CEPH_OSD_OP_CMPXATTR:
41 case CEPH_OSD_OP_CALL:
42 case CEPH_OSD_OP_NOTIFY:
49 static int op_has_extent(int op)
51 return (op == CEPH_OSD_OP_READ ||
52 op == CEPH_OSD_OP_WRITE);
55 void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
56 struct ceph_file_layout *layout,
58 u64 off, u64 *plen, u64 *bno,
59 struct ceph_osd_request *req,
60 struct ceph_osd_req_op *op)
62 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
64 u64 objoff, objlen; /* extent in object */
66 reqhead->snapid = cpu_to_le64(snapid);
69 ceph_calc_file_object_mapping(layout, off, plen, bno,
72 dout(" skipping last %llu, final file extent %llu~%llu\n",
73 orig_len - *plen, off, *plen);
75 if (op_has_extent(op->op)) {
76 op->extent.offset = objoff;
77 op->extent.length = objlen;
79 req->r_num_pages = calc_pages_for(off, *plen);
80 req->r_page_alignment = off & ~PAGE_MASK;
81 if (op->op == CEPH_OSD_OP_WRITE)
82 op->payload_len = *plen;
84 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
85 *bno, objoff, objlen, req->r_num_pages);
88 EXPORT_SYMBOL(ceph_calc_raw_layout);
91 * Implement client access to distributed object storage cluster.
93 * All data objects are stored within a cluster/cloud of OSDs, or
94 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
95 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
96 * remote daemons serving up and coordinating consistent and safe
99 * Cluster membership and the mapping of data objects onto storage devices
100 * are described by the osd map.
102 * We keep track of pending OSD requests (read, write), resubmit
103 * requests to different OSDs when the cluster topology/data layout
104 * change, or retry the affected requests when the communications
105 * channel with an OSD is reset.
109 * calculate the mapping of a file extent onto an object, and fill out the
110 * request accordingly. shorten extent as necessary if it crosses an
113 * fill osd op in request message.
115 static void calc_layout(struct ceph_osd_client *osdc,
116 struct ceph_vino vino,
117 struct ceph_file_layout *layout,
119 struct ceph_osd_request *req,
120 struct ceph_osd_req_op *op)
124 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
125 plen, &bno, req, op);
127 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
128 req->r_oid_len = strlen(req->r_oid);
134 void ceph_osdc_release_request(struct kref *kref)
136 struct ceph_osd_request *req = container_of(kref,
137 struct ceph_osd_request,
141 ceph_msg_put(req->r_request);
142 if (req->r_con_filling_msg) {
143 dout("%s revoking pages %p from con %p\n", __func__,
144 req->r_pages, req->r_con_filling_msg);
145 ceph_msg_revoke_incoming(req->r_reply);
146 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
149 ceph_msg_put(req->r_reply);
150 if (req->r_own_pages)
151 ceph_release_page_vector(req->r_pages,
157 ceph_put_snap_context(req->r_snapc);
159 ceph_pagelist_release(req->r_trail);
163 mempool_free(req, req->r_osdc->req_mempool);
167 EXPORT_SYMBOL(ceph_osdc_release_request);
169 static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
176 if (needs_trail && op_needs_trail(ops[i].op))
184 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
186 struct ceph_snap_context *snapc,
187 struct ceph_osd_req_op *ops,
193 struct ceph_osd_request *req;
194 struct ceph_msg *msg;
196 int num_op = get_num_ops(ops, &needs_trail);
197 size_t msg_size = sizeof(struct ceph_osd_request_head);
199 msg_size += num_op*sizeof(struct ceph_osd_op);
202 req = mempool_alloc(osdc->req_mempool, gfp_flags);
203 memset(req, 0, sizeof(*req));
205 req = kzalloc(sizeof(*req), gfp_flags);
211 req->r_mempool = use_mempool;
213 kref_init(&req->r_kref);
214 init_completion(&req->r_completion);
215 init_completion(&req->r_safe_completion);
216 rb_init_node(&req->r_node);
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 INIT_LIST_HEAD(&req->r_osd_item);
223 req->r_flags = flags;
225 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
227 /* create reply message */
229 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
231 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
232 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
234 ceph_osdc_put_request(req);
239 /* allocate space for the trailing data */
241 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
243 ceph_osdc_put_request(req);
246 ceph_pagelist_init(req->r_trail);
249 /* create request message; allow space for oid */
250 msg_size += MAX_OBJ_NAME_SIZE;
252 msg_size += sizeof(u64) * snapc->num_snaps;
254 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
256 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
258 ceph_osdc_put_request(req);
262 memset(msg->front.iov_base, 0, msg->front.iov_len);
264 req->r_request = msg;
265 req->r_pages = pages;
275 EXPORT_SYMBOL(ceph_osdc_alloc_request);
277 static void osd_req_encode_op(struct ceph_osd_request *req,
278 struct ceph_osd_op *dst,
279 struct ceph_osd_req_op *src)
281 dst->op = cpu_to_le16(src->op);
284 case CEPH_OSD_OP_READ:
285 case CEPH_OSD_OP_WRITE:
287 cpu_to_le64(src->extent.offset);
289 cpu_to_le64(src->extent.length);
290 dst->extent.truncate_size =
291 cpu_to_le64(src->extent.truncate_size);
292 dst->extent.truncate_seq =
293 cpu_to_le32(src->extent.truncate_seq);
296 case CEPH_OSD_OP_GETXATTR:
297 case CEPH_OSD_OP_SETXATTR:
298 case CEPH_OSD_OP_CMPXATTR:
299 BUG_ON(!req->r_trail);
301 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
302 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
303 dst->xattr.cmp_op = src->xattr.cmp_op;
304 dst->xattr.cmp_mode = src->xattr.cmp_mode;
305 ceph_pagelist_append(req->r_trail, src->xattr.name,
306 src->xattr.name_len);
307 ceph_pagelist_append(req->r_trail, src->xattr.val,
308 src->xattr.value_len);
310 case CEPH_OSD_OP_CALL:
311 BUG_ON(!req->r_trail);
313 dst->cls.class_len = src->cls.class_len;
314 dst->cls.method_len = src->cls.method_len;
315 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
317 ceph_pagelist_append(req->r_trail, src->cls.class_name,
319 ceph_pagelist_append(req->r_trail, src->cls.method_name,
320 src->cls.method_len);
321 ceph_pagelist_append(req->r_trail, src->cls.indata,
322 src->cls.indata_len);
324 case CEPH_OSD_OP_ROLLBACK:
325 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
327 case CEPH_OSD_OP_STARTSYNC:
329 case CEPH_OSD_OP_NOTIFY:
331 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
332 __le32 timeout = cpu_to_le32(src->watch.timeout);
334 BUG_ON(!req->r_trail);
336 ceph_pagelist_append(req->r_trail,
337 &prot_ver, sizeof(prot_ver));
338 ceph_pagelist_append(req->r_trail,
339 &timeout, sizeof(timeout));
341 case CEPH_OSD_OP_NOTIFY_ACK:
342 case CEPH_OSD_OP_WATCH:
343 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
344 dst->watch.ver = cpu_to_le64(src->watch.ver);
345 dst->watch.flag = src->watch.flag;
348 pr_err("unrecognized osd opcode %d\n", dst->op);
352 dst->payload_len = cpu_to_le32(src->payload_len);
356 * build new request AND message
359 void ceph_osdc_build_request(struct ceph_osd_request *req,
361 struct ceph_osd_req_op *src_ops,
362 struct ceph_snap_context *snapc,
363 struct timespec *mtime,
367 struct ceph_msg *msg = req->r_request;
368 struct ceph_osd_request_head *head;
369 struct ceph_osd_req_op *src_op;
370 struct ceph_osd_op *op;
372 int num_op = get_num_ops(src_ops, NULL);
373 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
374 int flags = req->r_flags;
378 head = msg->front.iov_base;
379 op = (void *)(head + 1);
380 p = (void *)(op + num_op);
382 req->r_snapc = ceph_get_snap_context(snapc);
384 head->client_inc = cpu_to_le32(1); /* always, for now. */
385 head->flags = cpu_to_le32(flags);
386 if (flags & CEPH_OSD_FLAG_WRITE)
387 ceph_encode_timespec(&head->mtime, mtime);
388 head->num_ops = cpu_to_le16(num_op);
392 head->object_len = cpu_to_le32(oid_len);
393 memcpy(p, oid, oid_len);
398 osd_req_encode_op(req, op, src_op);
404 data_len += req->r_trail->length;
407 head->snap_seq = cpu_to_le64(snapc->seq);
408 head->num_snaps = cpu_to_le32(snapc->num_snaps);
409 for (i = 0; i < snapc->num_snaps; i++) {
410 put_unaligned_le64(snapc->snaps[i], p);
415 if (flags & CEPH_OSD_FLAG_WRITE) {
416 req->r_request->hdr.data_off = cpu_to_le16(off);
417 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
418 } else if (data_len) {
419 req->r_request->hdr.data_off = 0;
420 req->r_request->hdr.data_len = cpu_to_le32(data_len);
423 req->r_request->page_alignment = req->r_page_alignment;
425 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
426 msg_size = p - msg->front.iov_base;
427 msg->front.iov_len = msg_size;
428 msg->hdr.front_len = cpu_to_le32(msg_size);
431 EXPORT_SYMBOL(ceph_osdc_build_request);
434 * build new request AND message, calculate layout, and adjust file
437 * if the file was recently truncated, we include information about its
438 * old and new size so that the object can be updated appropriately. (we
439 * avoid synchronously deleting truncated objects because it's slow.)
441 * if @do_sync, include a 'startsync' command so that the osd will flush
444 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
445 struct ceph_file_layout *layout,
446 struct ceph_vino vino,
448 int opcode, int flags,
449 struct ceph_snap_context *snapc,
453 struct timespec *mtime,
454 bool use_mempool, int num_reply,
457 struct ceph_osd_req_op ops[3];
458 struct ceph_osd_request *req;
461 ops[0].extent.truncate_seq = truncate_seq;
462 ops[0].extent.truncate_size = truncate_size;
463 ops[0].payload_len = 0;
466 ops[1].op = CEPH_OSD_OP_STARTSYNC;
467 ops[1].payload_len = 0;
472 req = ceph_osdc_alloc_request(osdc, flags,
475 GFP_NOFS, NULL, NULL);
479 /* calculate max write size */
480 calc_layout(osdc, vino, layout, off, plen, req, ops);
481 req->r_file_layout = *layout; /* keep a copy */
483 /* in case it differs from natural (file) alignment that
484 calc_layout filled in for us */
485 req->r_num_pages = calc_pages_for(page_align, *plen);
486 req->r_page_alignment = page_align;
488 ceph_osdc_build_request(req, off, plen, ops,
491 req->r_oid, req->r_oid_len);
495 EXPORT_SYMBOL(ceph_osdc_new_request);
498 * We keep osd requests in an rbtree, sorted by ->r_tid.
500 static void __insert_request(struct ceph_osd_client *osdc,
501 struct ceph_osd_request *new)
503 struct rb_node **p = &osdc->requests.rb_node;
504 struct rb_node *parent = NULL;
505 struct ceph_osd_request *req = NULL;
509 req = rb_entry(parent, struct ceph_osd_request, r_node);
510 if (new->r_tid < req->r_tid)
512 else if (new->r_tid > req->r_tid)
518 rb_link_node(&new->r_node, parent, p);
519 rb_insert_color(&new->r_node, &osdc->requests);
522 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
525 struct ceph_osd_request *req;
526 struct rb_node *n = osdc->requests.rb_node;
529 req = rb_entry(n, struct ceph_osd_request, r_node);
530 if (tid < req->r_tid)
532 else if (tid > req->r_tid)
540 static struct ceph_osd_request *
541 __lookup_request_ge(struct ceph_osd_client *osdc,
544 struct ceph_osd_request *req;
545 struct rb_node *n = osdc->requests.rb_node;
548 req = rb_entry(n, struct ceph_osd_request, r_node);
549 if (tid < req->r_tid) {
553 } else if (tid > req->r_tid) {
563 * Resubmit requests pending on the given osd.
565 static void __kick_osd_requests(struct ceph_osd_client *osdc,
566 struct ceph_osd *osd)
568 struct ceph_osd_request *req, *nreq;
571 dout("__kick_osd_requests osd%d\n", osd->o_osd);
572 err = __reset_osd(osdc, osd);
576 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
577 list_move(&req->r_req_lru_item, &osdc->req_unsent);
578 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
581 req->r_flags |= CEPH_OSD_FLAG_RETRY;
584 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
587 * reregister request prior to unregistering linger so
588 * that r_osd is preserved.
590 BUG_ON(!list_empty(&req->r_req_lru_item));
591 __register_request(osdc, req);
592 list_add(&req->r_req_lru_item, &osdc->req_unsent);
593 list_add(&req->r_osd_item, &req->r_osd->o_requests);
594 __unregister_linger_request(osdc, req);
595 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
600 static void kick_osd_requests(struct ceph_osd_client *osdc,
601 struct ceph_osd *kickosd)
603 mutex_lock(&osdc->request_mutex);
604 __kick_osd_requests(osdc, kickosd);
605 mutex_unlock(&osdc->request_mutex);
609 * If the osd connection drops, we need to resubmit all requests.
611 static void osd_reset(struct ceph_connection *con)
613 struct ceph_osd *osd = con->private;
614 struct ceph_osd_client *osdc;
618 dout("osd_reset osd%d\n", osd->o_osd);
620 down_read(&osdc->map_sem);
621 kick_osd_requests(osdc, osd);
623 up_read(&osdc->map_sem);
627 * Track open sessions with osds.
629 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
631 struct ceph_osd *osd;
633 osd = kzalloc(sizeof(*osd), GFP_NOFS);
637 atomic_set(&osd->o_ref, 1);
640 INIT_LIST_HEAD(&osd->o_requests);
641 INIT_LIST_HEAD(&osd->o_linger_requests);
642 INIT_LIST_HEAD(&osd->o_osd_lru);
643 osd->o_incarnation = 1;
645 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
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) && osd->o_auth.authorizer) {
668 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
670 if (ac->ops && ac->ops->destroy_authorizer)
671 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
677 * remove an osd from our map
679 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
681 dout("__remove_osd %p\n", osd);
682 BUG_ON(!list_empty(&osd->o_requests));
683 rb_erase(&osd->o_node, &osdc->osds);
684 list_del_init(&osd->o_osd_lru);
685 ceph_con_close(&osd->o_con);
689 static void remove_all_osds(struct ceph_osd_client *osdc)
691 dout("__remove_old_osds %p\n", osdc);
692 mutex_lock(&osdc->request_mutex);
693 while (!RB_EMPTY_ROOT(&osdc->osds)) {
694 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
695 struct ceph_osd, o_node);
696 __remove_osd(osdc, osd);
698 mutex_unlock(&osdc->request_mutex);
701 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
702 struct ceph_osd *osd)
704 dout("__move_osd_to_lru %p\n", osd);
705 BUG_ON(!list_empty(&osd->o_osd_lru));
706 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
707 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
710 static void __remove_osd_from_lru(struct ceph_osd *osd)
712 dout("__remove_osd_from_lru %p\n", osd);
713 if (!list_empty(&osd->o_osd_lru))
714 list_del_init(&osd->o_osd_lru);
717 static void remove_old_osds(struct ceph_osd_client *osdc)
719 struct ceph_osd *osd, *nosd;
721 dout("__remove_old_osds %p\n", osdc);
722 mutex_lock(&osdc->request_mutex);
723 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
724 if (time_before(jiffies, osd->lru_ttl))
726 __remove_osd(osdc, osd);
728 mutex_unlock(&osdc->request_mutex);
734 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
736 struct ceph_osd_request *req;
739 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
740 if (list_empty(&osd->o_requests) &&
741 list_empty(&osd->o_linger_requests)) {
742 __remove_osd(osdc, osd);
743 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
744 &osd->o_con.peer_addr,
745 sizeof(osd->o_con.peer_addr)) == 0 &&
746 !ceph_con_opened(&osd->o_con)) {
747 dout(" osd addr hasn't changed and connection never opened,"
748 " letting msgr retry");
749 /* touch each r_stamp for handle_timeout()'s benfit */
750 list_for_each_entry(req, &osd->o_requests, r_osd_item)
751 req->r_stamp = jiffies;
754 ceph_con_close(&osd->o_con);
755 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
756 &osdc->osdmap->osd_addr[osd->o_osd]);
757 osd->o_incarnation++;
762 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
764 struct rb_node **p = &osdc->osds.rb_node;
765 struct rb_node *parent = NULL;
766 struct ceph_osd *osd = NULL;
768 dout("__insert_osd %p osd%d\n", new, new->o_osd);
771 osd = rb_entry(parent, struct ceph_osd, o_node);
772 if (new->o_osd < osd->o_osd)
774 else if (new->o_osd > osd->o_osd)
780 rb_link_node(&new->o_node, parent, p);
781 rb_insert_color(&new->o_node, &osdc->osds);
784 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
786 struct ceph_osd *osd;
787 struct rb_node *n = osdc->osds.rb_node;
790 osd = rb_entry(n, struct ceph_osd, o_node);
793 else if (o > osd->o_osd)
801 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
803 schedule_delayed_work(&osdc->timeout_work,
804 osdc->client->options->osd_keepalive_timeout * HZ);
807 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
809 cancel_delayed_work(&osdc->timeout_work);
813 * Register request, assign tid. If this is the first request, set up
816 static void __register_request(struct ceph_osd_client *osdc,
817 struct ceph_osd_request *req)
819 req->r_tid = ++osdc->last_tid;
820 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
821 dout("__register_request %p tid %lld\n", req, req->r_tid);
822 __insert_request(osdc, req);
823 ceph_osdc_get_request(req);
824 osdc->num_requests++;
825 if (osdc->num_requests == 1) {
826 dout(" first request, scheduling timeout\n");
827 __schedule_osd_timeout(osdc);
831 static void register_request(struct ceph_osd_client *osdc,
832 struct ceph_osd_request *req)
834 mutex_lock(&osdc->request_mutex);
835 __register_request(osdc, req);
836 mutex_unlock(&osdc->request_mutex);
840 * called under osdc->request_mutex
842 static void __unregister_request(struct ceph_osd_client *osdc,
843 struct ceph_osd_request *req)
845 if (RB_EMPTY_NODE(&req->r_node)) {
846 dout("__unregister_request %p tid %lld not registered\n",
851 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
852 rb_erase(&req->r_node, &osdc->requests);
853 osdc->num_requests--;
856 /* make sure the original request isn't in flight. */
857 ceph_msg_revoke(req->r_request);
859 list_del_init(&req->r_osd_item);
860 if (list_empty(&req->r_osd->o_requests) &&
861 list_empty(&req->r_osd->o_linger_requests)) {
862 dout("moving osd to %p lru\n", req->r_osd);
863 __move_osd_to_lru(osdc, req->r_osd);
865 if (list_empty(&req->r_linger_item))
869 ceph_osdc_put_request(req);
871 list_del_init(&req->r_req_lru_item);
872 if (osdc->num_requests == 0) {
873 dout(" no requests, canceling timeout\n");
874 __cancel_osd_timeout(osdc);
879 * Cancel a previously queued request message
881 static void __cancel_request(struct ceph_osd_request *req)
883 if (req->r_sent && req->r_osd) {
884 ceph_msg_revoke(req->r_request);
889 static void __register_linger_request(struct ceph_osd_client *osdc,
890 struct ceph_osd_request *req)
892 dout("__register_linger_request %p\n", req);
893 list_add_tail(&req->r_linger_item, &osdc->req_linger);
894 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
897 static void __unregister_linger_request(struct ceph_osd_client *osdc,
898 struct ceph_osd_request *req)
900 dout("__unregister_linger_request %p\n", req);
902 list_del_init(&req->r_linger_item);
903 list_del_init(&req->r_linger_osd);
905 if (list_empty(&req->r_osd->o_requests) &&
906 list_empty(&req->r_osd->o_linger_requests)) {
907 dout("moving osd to %p lru\n", req->r_osd);
908 __move_osd_to_lru(osdc, req->r_osd);
910 if (list_empty(&req->r_osd_item))
915 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
916 struct ceph_osd_request *req)
918 mutex_lock(&osdc->request_mutex);
920 __unregister_linger_request(osdc, req);
921 ceph_osdc_put_request(req);
923 mutex_unlock(&osdc->request_mutex);
925 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
927 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
928 struct ceph_osd_request *req)
930 if (!req->r_linger) {
931 dout("set_request_linger %p\n", req);
934 * caller is now responsible for calling
935 * unregister_linger_request
937 ceph_osdc_get_request(req);
940 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
943 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
944 * (as needed), and set the request r_osd appropriately. If there is
945 * no up osd, set r_osd to NULL. Move the request to the appropriate list
946 * (unsent, homeless) or leave on in-flight lru.
948 * Return 0 if unchanged, 1 if changed, or negative on error.
950 * Caller should hold map_sem for read and request_mutex.
952 static int __map_request(struct ceph_osd_client *osdc,
953 struct ceph_osd_request *req, int force_resend)
955 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
957 int acting[CEPH_PG_MAX_SIZE];
961 dout("map_request %p tid %lld\n", req, req->r_tid);
962 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
963 &req->r_file_layout, osdc->osdmap);
965 list_move(&req->r_req_lru_item, &osdc->req_notarget);
968 pgid = reqhead->layout.ol_pgid;
971 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
977 if ((!force_resend &&
978 req->r_osd && req->r_osd->o_osd == o &&
979 req->r_sent >= req->r_osd->o_incarnation &&
980 req->r_num_pg_osds == num &&
981 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
982 (req->r_osd == NULL && o == -1))
983 return 0; /* no change */
985 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
986 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
987 req->r_osd ? req->r_osd->o_osd : -1);
989 /* record full pg acting set */
990 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
991 req->r_num_pg_osds = num;
994 __cancel_request(req);
995 list_del_init(&req->r_osd_item);
999 req->r_osd = __lookup_osd(osdc, o);
1000 if (!req->r_osd && o >= 0) {
1002 req->r_osd = create_osd(osdc, o);
1004 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1008 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1009 __insert_osd(osdc, req->r_osd);
1011 ceph_con_open(&req->r_osd->o_con,
1012 CEPH_ENTITY_TYPE_OSD, o,
1013 &osdc->osdmap->osd_addr[o]);
1017 __remove_osd_from_lru(req->r_osd);
1018 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1019 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1021 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1023 err = 1; /* osd or pg changed */
1030 * caller should hold map_sem (for read) and request_mutex
1032 static void __send_request(struct ceph_osd_client *osdc,
1033 struct ceph_osd_request *req)
1035 struct ceph_osd_request_head *reqhead;
1037 dout("send_request %p tid %llu to osd%d flags %d\n",
1038 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1040 reqhead = req->r_request->front.iov_base;
1041 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1042 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1043 reqhead->reassert_version = req->r_reassert_version;
1045 req->r_stamp = jiffies;
1046 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1048 ceph_msg_get(req->r_request); /* send consumes a ref */
1049 ceph_con_send(&req->r_osd->o_con, req->r_request);
1050 req->r_sent = req->r_osd->o_incarnation;
1054 * Send any requests in the queue (req_unsent).
1056 static void send_queued(struct ceph_osd_client *osdc)
1058 struct ceph_osd_request *req, *tmp;
1060 dout("send_queued\n");
1061 mutex_lock(&osdc->request_mutex);
1062 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1063 __send_request(osdc, req);
1065 mutex_unlock(&osdc->request_mutex);
1069 * Timeout callback, called every N seconds when 1 or more osd
1070 * requests has been active for more than N seconds. When this
1071 * happens, we ping all OSDs with requests who have timed out to
1072 * ensure any communications channel reset is detected. Reset the
1073 * request timeouts another N seconds in the future as we go.
1074 * Reschedule the timeout event another N seconds in future (unless
1075 * there are no open requests).
1077 static void handle_timeout(struct work_struct *work)
1079 struct ceph_osd_client *osdc =
1080 container_of(work, struct ceph_osd_client, timeout_work.work);
1081 struct ceph_osd_request *req, *last_req = NULL;
1082 struct ceph_osd *osd;
1083 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
1084 unsigned long keepalive =
1085 osdc->client->options->osd_keepalive_timeout * HZ;
1086 unsigned long last_stamp = 0;
1087 struct list_head slow_osds;
1089 down_read(&osdc->map_sem);
1091 ceph_monc_request_next_osdmap(&osdc->client->monc);
1093 mutex_lock(&osdc->request_mutex);
1096 * reset osds that appear to be _really_ unresponsive. this
1097 * is a failsafe measure.. we really shouldn't be getting to
1098 * this point if the system is working properly. the monitors
1099 * should mark the osd as failed and we should find out about
1100 * it from an updated osd map.
1102 while (timeout && !list_empty(&osdc->req_lru)) {
1103 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1106 /* hasn't been long enough since we sent it? */
1107 if (time_before(jiffies, req->r_stamp + timeout))
1110 /* hasn't been long enough since it was acked? */
1111 if (req->r_request->ack_stamp == 0 ||
1112 time_before(jiffies, req->r_request->ack_stamp + timeout))
1115 BUG_ON(req == last_req && req->r_stamp == last_stamp);
1117 last_stamp = req->r_stamp;
1121 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1122 req->r_tid, osd->o_osd);
1123 __kick_osd_requests(osdc, osd);
1127 * ping osds that are a bit slow. this ensures that if there
1128 * is a break in the TCP connection we will notice, and reopen
1129 * a connection with that osd (from the fault callback).
1131 INIT_LIST_HEAD(&slow_osds);
1132 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1133 if (time_before(jiffies, req->r_stamp + keepalive))
1138 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1139 req->r_tid, osd->o_osd);
1140 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1142 while (!list_empty(&slow_osds)) {
1143 osd = list_entry(slow_osds.next, struct ceph_osd,
1145 list_del_init(&osd->o_keepalive_item);
1146 ceph_con_keepalive(&osd->o_con);
1149 __schedule_osd_timeout(osdc);
1150 mutex_unlock(&osdc->request_mutex);
1152 up_read(&osdc->map_sem);
1155 static void handle_osds_timeout(struct work_struct *work)
1157 struct ceph_osd_client *osdc =
1158 container_of(work, struct ceph_osd_client,
1159 osds_timeout_work.work);
1160 unsigned long delay =
1161 osdc->client->options->osd_idle_ttl * HZ >> 2;
1163 dout("osds timeout\n");
1164 down_read(&osdc->map_sem);
1165 remove_old_osds(osdc);
1166 up_read(&osdc->map_sem);
1168 schedule_delayed_work(&osdc->osds_timeout_work,
1169 round_jiffies_relative(delay));
1172 static void complete_request(struct ceph_osd_request *req)
1174 if (req->r_safe_callback)
1175 req->r_safe_callback(req, NULL);
1176 complete_all(&req->r_safe_completion); /* fsync waiter */
1180 * handle osd op reply. either call the callback if it is specified,
1181 * or do the completion to wake up the waiting thread.
1183 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1184 struct ceph_connection *con)
1186 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1187 struct ceph_osd_request *req;
1189 int numops, object_len, flags;
1192 tid = le64_to_cpu(msg->hdr.tid);
1193 if (msg->front.iov_len < sizeof(*rhead))
1195 numops = le32_to_cpu(rhead->num_ops);
1196 object_len = le32_to_cpu(rhead->object_len);
1197 result = le32_to_cpu(rhead->result);
1198 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1199 numops * sizeof(struct ceph_osd_op))
1201 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1203 mutex_lock(&osdc->request_mutex);
1204 req = __lookup_request(osdc, tid);
1206 dout("handle_reply tid %llu dne\n", tid);
1207 mutex_unlock(&osdc->request_mutex);
1210 ceph_osdc_get_request(req);
1211 flags = le32_to_cpu(rhead->flags);
1214 * if this connection filled our message, drop our reference now, to
1215 * avoid a (safe but slower) revoke later.
1217 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1218 dout(" dropping con_filling_msg ref %p\n", con);
1219 req->r_con_filling_msg = NULL;
1223 if (!req->r_got_reply) {
1226 req->r_result = le32_to_cpu(rhead->result);
1227 bytes = le32_to_cpu(msg->hdr.data_len);
1228 dout("handle_reply result %d bytes %d\n", req->r_result,
1230 if (req->r_result == 0)
1231 req->r_result = bytes;
1233 /* in case this is a write and we need to replay, */
1234 req->r_reassert_version = rhead->reassert_version;
1236 req->r_got_reply = 1;
1237 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1238 dout("handle_reply tid %llu dup ack\n", tid);
1239 mutex_unlock(&osdc->request_mutex);
1243 dout("handle_reply tid %llu flags %d\n", tid, flags);
1245 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1246 __register_linger_request(osdc, req);
1248 /* either this is a read, or we got the safe response */
1250 (flags & CEPH_OSD_FLAG_ONDISK) ||
1251 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1252 __unregister_request(osdc, req);
1254 mutex_unlock(&osdc->request_mutex);
1256 if (req->r_callback)
1257 req->r_callback(req, msg);
1259 complete_all(&req->r_completion);
1261 if (flags & CEPH_OSD_FLAG_ONDISK)
1262 complete_request(req);
1265 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1266 ceph_osdc_put_request(req);
1270 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1271 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1272 (int)sizeof(*rhead));
1276 static void reset_changed_osds(struct ceph_osd_client *osdc)
1278 struct rb_node *p, *n;
1280 for (p = rb_first(&osdc->osds); p; p = n) {
1281 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1284 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1285 memcmp(&osd->o_con.peer_addr,
1286 ceph_osd_addr(osdc->osdmap,
1288 sizeof(struct ceph_entity_addr)) != 0)
1289 __reset_osd(osdc, osd);
1294 * Requeue requests whose mapping to an OSD has changed. If requests map to
1295 * no osd, request a new map.
1297 * Caller should hold map_sem for read and request_mutex.
1299 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1301 struct ceph_osd_request *req, *nreq;
1306 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1307 mutex_lock(&osdc->request_mutex);
1308 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1309 req = rb_entry(p, struct ceph_osd_request, r_node);
1310 err = __map_request(osdc, req, force_resend);
1312 continue; /* error */
1313 if (req->r_osd == NULL) {
1314 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1315 needmap++; /* request a newer map */
1316 } else if (err > 0) {
1317 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1318 req->r_osd ? req->r_osd->o_osd : -1);
1320 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1324 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1326 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1328 err = __map_request(osdc, req, force_resend);
1330 continue; /* no change and no osd was specified */
1332 continue; /* hrm! */
1333 if (req->r_osd == NULL) {
1334 dout("tid %llu maps to no valid osd\n", req->r_tid);
1335 needmap++; /* request a newer map */
1339 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1340 req->r_osd ? req->r_osd->o_osd : -1);
1341 __unregister_linger_request(osdc, req);
1342 __register_request(osdc, req);
1344 mutex_unlock(&osdc->request_mutex);
1347 dout("%d requests for down osds, need new map\n", needmap);
1348 ceph_monc_request_next_osdmap(&osdc->client->monc);
1354 * Process updated osd map.
1356 * The message contains any number of incremental and full maps, normally
1357 * indicating some sort of topology change in the cluster. Kick requests
1358 * off to different OSDs as needed.
1360 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1362 void *p, *end, *next;
1363 u32 nr_maps, maplen;
1365 struct ceph_osdmap *newmap = NULL, *oldmap;
1367 struct ceph_fsid fsid;
1369 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1370 p = msg->front.iov_base;
1371 end = p + msg->front.iov_len;
1374 ceph_decode_need(&p, end, sizeof(fsid), bad);
1375 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1376 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1379 down_write(&osdc->map_sem);
1381 /* incremental maps */
1382 ceph_decode_32_safe(&p, end, nr_maps, bad);
1383 dout(" %d inc maps\n", nr_maps);
1384 while (nr_maps > 0) {
1385 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1386 epoch = ceph_decode_32(&p);
1387 maplen = ceph_decode_32(&p);
1388 ceph_decode_need(&p, end, maplen, bad);
1390 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1391 dout("applying incremental map %u len %d\n",
1393 newmap = osdmap_apply_incremental(&p, next,
1395 &osdc->client->msgr);
1396 if (IS_ERR(newmap)) {
1397 err = PTR_ERR(newmap);
1401 if (newmap != osdc->osdmap) {
1402 ceph_osdmap_destroy(osdc->osdmap);
1403 osdc->osdmap = newmap;
1405 kick_requests(osdc, 0);
1406 reset_changed_osds(osdc);
1408 dout("ignoring incremental map %u len %d\n",
1418 ceph_decode_32_safe(&p, end, nr_maps, bad);
1419 dout(" %d full maps\n", nr_maps);
1421 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1422 epoch = ceph_decode_32(&p);
1423 maplen = ceph_decode_32(&p);
1424 ceph_decode_need(&p, end, maplen, bad);
1426 dout("skipping non-latest full map %u len %d\n",
1428 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1429 dout("skipping full map %u len %d, "
1430 "older than our %u\n", epoch, maplen,
1431 osdc->osdmap->epoch);
1433 int skipped_map = 0;
1435 dout("taking full map %u len %d\n", epoch, maplen);
1436 newmap = osdmap_decode(&p, p+maplen);
1437 if (IS_ERR(newmap)) {
1438 err = PTR_ERR(newmap);
1442 oldmap = osdc->osdmap;
1443 osdc->osdmap = newmap;
1445 if (oldmap->epoch + 1 < newmap->epoch)
1447 ceph_osdmap_destroy(oldmap);
1449 kick_requests(osdc, skipped_map);
1456 downgrade_write(&osdc->map_sem);
1457 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1460 * subscribe to subsequent osdmap updates if full to ensure
1461 * we find out when we are no longer full and stop returning
1464 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1465 ceph_monc_request_next_osdmap(&osdc->client->monc);
1468 up_read(&osdc->map_sem);
1469 wake_up_all(&osdc->client->auth_wq);
1473 pr_err("osdc handle_map corrupt msg\n");
1475 up_write(&osdc->map_sem);
1480 * watch/notify callback event infrastructure
1482 * These callbacks are used both for watch and notify operations.
1484 static void __release_event(struct kref *kref)
1486 struct ceph_osd_event *event =
1487 container_of(kref, struct ceph_osd_event, kref);
1489 dout("__release_event %p\n", event);
1493 static void get_event(struct ceph_osd_event *event)
1495 kref_get(&event->kref);
1498 void ceph_osdc_put_event(struct ceph_osd_event *event)
1500 kref_put(&event->kref, __release_event);
1502 EXPORT_SYMBOL(ceph_osdc_put_event);
1504 static void __insert_event(struct ceph_osd_client *osdc,
1505 struct ceph_osd_event *new)
1507 struct rb_node **p = &osdc->event_tree.rb_node;
1508 struct rb_node *parent = NULL;
1509 struct ceph_osd_event *event = NULL;
1513 event = rb_entry(parent, struct ceph_osd_event, node);
1514 if (new->cookie < event->cookie)
1516 else if (new->cookie > event->cookie)
1517 p = &(*p)->rb_right;
1522 rb_link_node(&new->node, parent, p);
1523 rb_insert_color(&new->node, &osdc->event_tree);
1526 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1529 struct rb_node **p = &osdc->event_tree.rb_node;
1530 struct rb_node *parent = NULL;
1531 struct ceph_osd_event *event = NULL;
1535 event = rb_entry(parent, struct ceph_osd_event, node);
1536 if (cookie < event->cookie)
1538 else if (cookie > event->cookie)
1539 p = &(*p)->rb_right;
1546 static void __remove_event(struct ceph_osd_event *event)
1548 struct ceph_osd_client *osdc = event->osdc;
1550 if (!RB_EMPTY_NODE(&event->node)) {
1551 dout("__remove_event removed %p\n", event);
1552 rb_erase(&event->node, &osdc->event_tree);
1553 ceph_osdc_put_event(event);
1555 dout("__remove_event didn't remove %p\n", event);
1559 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1560 void (*event_cb)(u64, u64, u8, void *),
1561 int one_shot, void *data,
1562 struct ceph_osd_event **pevent)
1564 struct ceph_osd_event *event;
1566 event = kmalloc(sizeof(*event), GFP_NOIO);
1570 dout("create_event %p\n", event);
1571 event->cb = event_cb;
1572 event->one_shot = one_shot;
1575 INIT_LIST_HEAD(&event->osd_node);
1576 kref_init(&event->kref); /* one ref for us */
1577 kref_get(&event->kref); /* one ref for the caller */
1578 init_completion(&event->completion);
1580 spin_lock(&osdc->event_lock);
1581 event->cookie = ++osdc->event_count;
1582 __insert_event(osdc, event);
1583 spin_unlock(&osdc->event_lock);
1588 EXPORT_SYMBOL(ceph_osdc_create_event);
1590 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1592 struct ceph_osd_client *osdc = event->osdc;
1594 dout("cancel_event %p\n", event);
1595 spin_lock(&osdc->event_lock);
1596 __remove_event(event);
1597 spin_unlock(&osdc->event_lock);
1598 ceph_osdc_put_event(event); /* caller's */
1600 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1603 static void do_event_work(struct work_struct *work)
1605 struct ceph_osd_event_work *event_work =
1606 container_of(work, struct ceph_osd_event_work, work);
1607 struct ceph_osd_event *event = event_work->event;
1608 u64 ver = event_work->ver;
1609 u64 notify_id = event_work->notify_id;
1610 u8 opcode = event_work->opcode;
1612 dout("do_event_work completing %p\n", event);
1613 event->cb(ver, notify_id, opcode, event->data);
1614 complete(&event->completion);
1615 dout("do_event_work completed %p\n", event);
1616 ceph_osdc_put_event(event);
1622 * Process osd watch notifications
1624 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1628 u64 cookie, ver, notify_id;
1630 struct ceph_osd_event *event;
1631 struct ceph_osd_event_work *event_work;
1633 p = msg->front.iov_base;
1634 end = p + msg->front.iov_len;
1636 ceph_decode_8_safe(&p, end, proto_ver, bad);
1637 ceph_decode_8_safe(&p, end, opcode, bad);
1638 ceph_decode_64_safe(&p, end, cookie, bad);
1639 ceph_decode_64_safe(&p, end, ver, bad);
1640 ceph_decode_64_safe(&p, end, notify_id, bad);
1642 spin_lock(&osdc->event_lock);
1643 event = __find_event(osdc, cookie);
1646 if (event->one_shot)
1647 __remove_event(event);
1649 spin_unlock(&osdc->event_lock);
1650 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1651 cookie, ver, event);
1653 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1655 dout("ERROR: could not allocate event_work\n");
1658 INIT_WORK(&event_work->work, do_event_work);
1659 event_work->event = event;
1660 event_work->ver = ver;
1661 event_work->notify_id = notify_id;
1662 event_work->opcode = opcode;
1663 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1664 dout("WARNING: failed to queue notify event work\n");
1672 complete(&event->completion);
1673 ceph_osdc_put_event(event);
1677 pr_err("osdc handle_watch_notify corrupt msg\n");
1681 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1685 dout("wait_event %p\n", event);
1686 err = wait_for_completion_interruptible_timeout(&event->completion,
1688 ceph_osdc_put_event(event);
1691 dout("wait_event %p returns %d\n", event, err);
1694 EXPORT_SYMBOL(ceph_osdc_wait_event);
1697 * Register request, send initial attempt.
1699 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1700 struct ceph_osd_request *req,
1705 req->r_request->pages = req->r_pages;
1706 req->r_request->nr_pages = req->r_num_pages;
1708 req->r_request->bio = req->r_bio;
1710 req->r_request->trail = req->r_trail;
1712 register_request(osdc, req);
1714 down_read(&osdc->map_sem);
1715 mutex_lock(&osdc->request_mutex);
1717 * a racing kick_requests() may have sent the message for us
1718 * while we dropped request_mutex above, so only send now if
1719 * the request still han't been touched yet.
1721 if (req->r_sent == 0) {
1722 rc = __map_request(osdc, req, 0);
1725 dout("osdc_start_request failed map, "
1726 " will retry %lld\n", req->r_tid);
1731 if (req->r_osd == NULL) {
1732 dout("send_request %p no up osds in pg\n", req);
1733 ceph_monc_request_next_osdmap(&osdc->client->monc);
1735 __send_request(osdc, req);
1741 mutex_unlock(&osdc->request_mutex);
1742 up_read(&osdc->map_sem);
1745 EXPORT_SYMBOL(ceph_osdc_start_request);
1748 * wait for a request to complete
1750 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1751 struct ceph_osd_request *req)
1755 rc = wait_for_completion_interruptible(&req->r_completion);
1757 mutex_lock(&osdc->request_mutex);
1758 __cancel_request(req);
1759 __unregister_request(osdc, req);
1760 mutex_unlock(&osdc->request_mutex);
1761 complete_request(req);
1762 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1766 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1767 return req->r_result;
1769 EXPORT_SYMBOL(ceph_osdc_wait_request);
1772 * sync - wait for all in-flight requests to flush. avoid starvation.
1774 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1776 struct ceph_osd_request *req;
1777 u64 last_tid, next_tid = 0;
1779 mutex_lock(&osdc->request_mutex);
1780 last_tid = osdc->last_tid;
1782 req = __lookup_request_ge(osdc, next_tid);
1785 if (req->r_tid > last_tid)
1788 next_tid = req->r_tid + 1;
1789 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1792 ceph_osdc_get_request(req);
1793 mutex_unlock(&osdc->request_mutex);
1794 dout("sync waiting on tid %llu (last is %llu)\n",
1795 req->r_tid, last_tid);
1796 wait_for_completion(&req->r_safe_completion);
1797 mutex_lock(&osdc->request_mutex);
1798 ceph_osdc_put_request(req);
1800 mutex_unlock(&osdc->request_mutex);
1801 dout("sync done (thru tid %llu)\n", last_tid);
1803 EXPORT_SYMBOL(ceph_osdc_sync);
1808 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1813 osdc->client = client;
1814 osdc->osdmap = NULL;
1815 init_rwsem(&osdc->map_sem);
1816 init_completion(&osdc->map_waiters);
1817 osdc->last_requested_map = 0;
1818 mutex_init(&osdc->request_mutex);
1820 osdc->osds = RB_ROOT;
1821 INIT_LIST_HEAD(&osdc->osd_lru);
1822 osdc->requests = RB_ROOT;
1823 INIT_LIST_HEAD(&osdc->req_lru);
1824 INIT_LIST_HEAD(&osdc->req_unsent);
1825 INIT_LIST_HEAD(&osdc->req_notarget);
1826 INIT_LIST_HEAD(&osdc->req_linger);
1827 osdc->num_requests = 0;
1828 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1829 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1830 spin_lock_init(&osdc->event_lock);
1831 osdc->event_tree = RB_ROOT;
1832 osdc->event_count = 0;
1834 schedule_delayed_work(&osdc->osds_timeout_work,
1835 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1838 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1839 sizeof(struct ceph_osd_request));
1840 if (!osdc->req_mempool)
1843 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1844 OSD_OP_FRONT_LEN, 10, true,
1848 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1849 OSD_OPREPLY_FRONT_LEN, 10, true,
1854 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1855 if (IS_ERR(osdc->notify_wq)) {
1856 err = PTR_ERR(osdc->notify_wq);
1857 osdc->notify_wq = NULL;
1863 ceph_msgpool_destroy(&osdc->msgpool_op);
1865 mempool_destroy(osdc->req_mempool);
1869 EXPORT_SYMBOL(ceph_osdc_init);
1871 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1873 flush_workqueue(osdc->notify_wq);
1874 destroy_workqueue(osdc->notify_wq);
1875 cancel_delayed_work_sync(&osdc->timeout_work);
1876 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1878 ceph_osdmap_destroy(osdc->osdmap);
1879 osdc->osdmap = NULL;
1881 remove_all_osds(osdc);
1882 mempool_destroy(osdc->req_mempool);
1883 ceph_msgpool_destroy(&osdc->msgpool_op);
1884 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1886 EXPORT_SYMBOL(ceph_osdc_stop);
1889 * Read some contiguous pages. If we cross a stripe boundary, shorten
1890 * *plen. Return number of bytes read, or error.
1892 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1893 struct ceph_vino vino, struct ceph_file_layout *layout,
1895 u32 truncate_seq, u64 truncate_size,
1896 struct page **pages, int num_pages, int page_align)
1898 struct ceph_osd_request *req;
1901 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1902 vino.snap, off, *plen);
1903 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1904 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1905 NULL, 0, truncate_seq, truncate_size, NULL,
1906 false, 1, page_align);
1910 /* it may be a short read due to an object boundary */
1911 req->r_pages = pages;
1913 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1914 off, *plen, req->r_num_pages, page_align);
1916 rc = ceph_osdc_start_request(osdc, req, false);
1918 rc = ceph_osdc_wait_request(osdc, req);
1920 ceph_osdc_put_request(req);
1921 dout("readpages result %d\n", rc);
1924 EXPORT_SYMBOL(ceph_osdc_readpages);
1927 * do a synchronous write on N pages
1929 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1930 struct ceph_file_layout *layout,
1931 struct ceph_snap_context *snapc,
1933 u32 truncate_seq, u64 truncate_size,
1934 struct timespec *mtime,
1935 struct page **pages, int num_pages,
1936 int flags, int do_sync, bool nofail)
1938 struct ceph_osd_request *req;
1940 int page_align = off & ~PAGE_MASK;
1942 BUG_ON(vino.snap != CEPH_NOSNAP);
1943 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1945 flags | CEPH_OSD_FLAG_ONDISK |
1946 CEPH_OSD_FLAG_WRITE,
1948 truncate_seq, truncate_size, mtime,
1949 nofail, 1, page_align);
1953 /* it may be a short write due to an object boundary */
1954 req->r_pages = pages;
1955 dout("writepages %llu~%llu (%d pages)\n", off, len,
1958 rc = ceph_osdc_start_request(osdc, req, nofail);
1960 rc = ceph_osdc_wait_request(osdc, req);
1962 ceph_osdc_put_request(req);
1965 dout("writepages result %d\n", rc);
1968 EXPORT_SYMBOL(ceph_osdc_writepages);
1971 * handle incoming message
1973 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1975 struct ceph_osd *osd = con->private;
1976 struct ceph_osd_client *osdc;
1977 int type = le16_to_cpu(msg->hdr.type);
1984 case CEPH_MSG_OSD_MAP:
1985 ceph_osdc_handle_map(osdc, msg);
1987 case CEPH_MSG_OSD_OPREPLY:
1988 handle_reply(osdc, msg, con);
1990 case CEPH_MSG_WATCH_NOTIFY:
1991 handle_watch_notify(osdc, msg);
1995 pr_err("received unknown message type %d %s\n", type,
1996 ceph_msg_type_name(type));
2003 * lookup and return message for incoming reply. set up reply message
2006 static struct ceph_msg *get_reply(struct ceph_connection *con,
2007 struct ceph_msg_header *hdr,
2010 struct ceph_osd *osd = con->private;
2011 struct ceph_osd_client *osdc = osd->o_osdc;
2013 struct ceph_osd_request *req;
2014 int front = le32_to_cpu(hdr->front_len);
2015 int data_len = le32_to_cpu(hdr->data_len);
2018 tid = le64_to_cpu(hdr->tid);
2019 mutex_lock(&osdc->request_mutex);
2020 req = __lookup_request(osdc, tid);
2024 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
2029 if (req->r_con_filling_msg) {
2030 dout("%s revoking msg %p from old con %p\n", __func__,
2031 req->r_reply, req->r_con_filling_msg);
2032 ceph_msg_revoke_incoming(req->r_reply);
2033 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2034 req->r_con_filling_msg = NULL;
2037 if (front > req->r_reply->front.iov_len) {
2038 pr_warning("get_reply front %d > preallocated %d\n",
2039 front, (int)req->r_reply->front.iov_len);
2040 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2043 ceph_msg_put(req->r_reply);
2046 m = ceph_msg_get(req->r_reply);
2049 int want = calc_pages_for(req->r_page_alignment, data_len);
2051 if (unlikely(req->r_num_pages < want)) {
2052 pr_warning("tid %lld reply has %d bytes %d pages, we"
2053 " had only %d pages ready\n", tid, data_len,
2054 want, req->r_num_pages);
2060 m->pages = req->r_pages;
2061 m->nr_pages = req->r_num_pages;
2062 m->page_alignment = req->r_page_alignment;
2064 m->bio = req->r_bio;
2068 req->r_con_filling_msg = con->ops->get(con);
2069 dout("get_reply tid %lld %p\n", tid, m);
2072 mutex_unlock(&osdc->request_mutex);
2077 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2078 struct ceph_msg_header *hdr,
2081 struct ceph_osd *osd = con->private;
2082 int type = le16_to_cpu(hdr->type);
2083 int front = le32_to_cpu(hdr->front_len);
2087 case CEPH_MSG_OSD_MAP:
2088 case CEPH_MSG_WATCH_NOTIFY:
2089 return ceph_msg_new(type, front, GFP_NOFS, false);
2090 case CEPH_MSG_OSD_OPREPLY:
2091 return get_reply(con, hdr, skip);
2093 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2101 * Wrappers to refcount containing ceph_osd struct
2103 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2105 struct ceph_osd *osd = con->private;
2111 static void put_osd_con(struct ceph_connection *con)
2113 struct ceph_osd *osd = con->private;
2121 * Note: returned pointer is the address of a structure that's
2122 * managed separately. Caller must *not* attempt to free it.
2124 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2125 int *proto, int force_new)
2127 struct ceph_osd *o = con->private;
2128 struct ceph_osd_client *osdc = o->o_osdc;
2129 struct ceph_auth_client *ac = osdc->client->monc.auth;
2130 struct ceph_auth_handshake *auth = &o->o_auth;
2132 if (force_new && auth->authorizer) {
2133 if (ac->ops && ac->ops->destroy_authorizer)
2134 ac->ops->destroy_authorizer(ac, auth->authorizer);
2135 auth->authorizer = NULL;
2137 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2138 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2141 return ERR_PTR(ret);
2143 *proto = ac->protocol;
2149 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2151 struct ceph_osd *o = con->private;
2152 struct ceph_osd_client *osdc = o->o_osdc;
2153 struct ceph_auth_client *ac = osdc->client->monc.auth;
2156 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2157 * XXX which do we do: succeed or fail?
2159 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2162 static int invalidate_authorizer(struct ceph_connection *con)
2164 struct ceph_osd *o = con->private;
2165 struct ceph_osd_client *osdc = o->o_osdc;
2166 struct ceph_auth_client *ac = osdc->client->monc.auth;
2168 if (ac->ops && ac->ops->invalidate_authorizer)
2169 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2171 return ceph_monc_validate_auth(&osdc->client->monc);
2174 static const struct ceph_connection_operations osd_con_ops = {
2177 .dispatch = dispatch,
2178 .get_authorizer = get_authorizer,
2179 .verify_authorizer_reply = verify_authorizer_reply,
2180 .invalidate_authorizer = invalidate_authorizer,
2181 .alloc_msg = alloc_msg,