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 u32 osize = le32_to_cpu(layout->fl_object_size);
80 op->extent.offset = objoff;
81 op->extent.length = objlen;
82 if (op->extent.truncate_size <= off - objoff) {
83 op->extent.truncate_size = 0;
85 op->extent.truncate_size -= off - objoff;
86 if (op->extent.truncate_size > osize)
87 op->extent.truncate_size = osize;
90 req->r_num_pages = calc_pages_for(off, *plen);
91 req->r_page_alignment = off & ~PAGE_MASK;
92 if (op->op == CEPH_OSD_OP_WRITE)
93 op->payload_len = *plen;
95 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
96 *bno, objoff, objlen, req->r_num_pages);
99 EXPORT_SYMBOL(ceph_calc_raw_layout);
102 * Implement client access to distributed object storage cluster.
104 * All data objects are stored within a cluster/cloud of OSDs, or
105 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
106 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
107 * remote daemons serving up and coordinating consistent and safe
110 * Cluster membership and the mapping of data objects onto storage devices
111 * are described by the osd map.
113 * We keep track of pending OSD requests (read, write), resubmit
114 * requests to different OSDs when the cluster topology/data layout
115 * change, or retry the affected requests when the communications
116 * channel with an OSD is reset.
120 * calculate the mapping of a file extent onto an object, and fill out the
121 * request accordingly. shorten extent as necessary if it crosses an
124 * fill osd op in request message.
126 static int calc_layout(struct ceph_osd_client *osdc,
127 struct ceph_vino vino,
128 struct ceph_file_layout *layout,
130 struct ceph_osd_request *req,
131 struct ceph_osd_req_op *op)
136 r = ceph_calc_raw_layout(osdc, layout, vino.snap, off,
137 plen, &bno, req, op);
141 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
142 req->r_oid_len = strlen(req->r_oid);
150 void ceph_osdc_release_request(struct kref *kref)
152 struct ceph_osd_request *req = container_of(kref,
153 struct ceph_osd_request,
157 ceph_msg_put(req->r_request);
158 if (req->r_con_filling_msg) {
159 dout("%s revoking pages %p from con %p\n", __func__,
160 req->r_pages, req->r_con_filling_msg);
161 ceph_msg_revoke_incoming(req->r_reply);
162 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
165 ceph_msg_put(req->r_reply);
166 if (req->r_own_pages)
167 ceph_release_page_vector(req->r_pages,
173 ceph_put_snap_context(req->r_snapc);
175 ceph_pagelist_release(req->r_trail);
179 mempool_free(req, req->r_osdc->req_mempool);
183 EXPORT_SYMBOL(ceph_osdc_release_request);
185 static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
192 if (needs_trail && op_needs_trail(ops[i].op))
200 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
202 struct ceph_snap_context *snapc,
203 struct ceph_osd_req_op *ops,
209 struct ceph_osd_request *req;
210 struct ceph_msg *msg;
212 int num_op = get_num_ops(ops, &needs_trail);
213 size_t msg_size = sizeof(struct ceph_osd_request_head);
215 msg_size += num_op*sizeof(struct ceph_osd_op);
218 req = mempool_alloc(osdc->req_mempool, gfp_flags);
219 memset(req, 0, sizeof(*req));
221 req = kzalloc(sizeof(*req), gfp_flags);
227 req->r_mempool = use_mempool;
229 kref_init(&req->r_kref);
230 init_completion(&req->r_completion);
231 init_completion(&req->r_safe_completion);
232 RB_CLEAR_NODE(&req->r_node);
233 INIT_LIST_HEAD(&req->r_unsafe_item);
234 INIT_LIST_HEAD(&req->r_linger_item);
235 INIT_LIST_HEAD(&req->r_linger_osd);
236 INIT_LIST_HEAD(&req->r_req_lru_item);
237 INIT_LIST_HEAD(&req->r_osd_item);
239 req->r_flags = flags;
241 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
243 /* create reply message */
245 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
247 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
248 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
250 ceph_osdc_put_request(req);
255 /* allocate space for the trailing data */
257 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
259 ceph_osdc_put_request(req);
262 ceph_pagelist_init(req->r_trail);
265 /* create request message; allow space for oid */
266 msg_size += MAX_OBJ_NAME_SIZE;
268 msg_size += sizeof(u64) * snapc->num_snaps;
270 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
272 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
274 ceph_osdc_put_request(req);
278 memset(msg->front.iov_base, 0, msg->front.iov_len);
280 req->r_request = msg;
281 req->r_pages = pages;
291 EXPORT_SYMBOL(ceph_osdc_alloc_request);
293 static void osd_req_encode_op(struct ceph_osd_request *req,
294 struct ceph_osd_op *dst,
295 struct ceph_osd_req_op *src)
297 dst->op = cpu_to_le16(src->op);
300 case CEPH_OSD_OP_READ:
301 case CEPH_OSD_OP_WRITE:
303 cpu_to_le64(src->extent.offset);
305 cpu_to_le64(src->extent.length);
306 dst->extent.truncate_size =
307 cpu_to_le64(src->extent.truncate_size);
308 dst->extent.truncate_seq =
309 cpu_to_le32(src->extent.truncate_seq);
312 case CEPH_OSD_OP_GETXATTR:
313 case CEPH_OSD_OP_SETXATTR:
314 case CEPH_OSD_OP_CMPXATTR:
315 BUG_ON(!req->r_trail);
317 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
318 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
319 dst->xattr.cmp_op = src->xattr.cmp_op;
320 dst->xattr.cmp_mode = src->xattr.cmp_mode;
321 ceph_pagelist_append(req->r_trail, src->xattr.name,
322 src->xattr.name_len);
323 ceph_pagelist_append(req->r_trail, src->xattr.val,
324 src->xattr.value_len);
326 case CEPH_OSD_OP_CALL:
327 BUG_ON(!req->r_trail);
329 dst->cls.class_len = src->cls.class_len;
330 dst->cls.method_len = src->cls.method_len;
331 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
333 ceph_pagelist_append(req->r_trail, src->cls.class_name,
335 ceph_pagelist_append(req->r_trail, src->cls.method_name,
336 src->cls.method_len);
337 ceph_pagelist_append(req->r_trail, src->cls.indata,
338 src->cls.indata_len);
340 case CEPH_OSD_OP_ROLLBACK:
341 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
343 case CEPH_OSD_OP_STARTSYNC:
345 case CEPH_OSD_OP_NOTIFY:
347 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
348 __le32 timeout = cpu_to_le32(src->watch.timeout);
350 BUG_ON(!req->r_trail);
352 ceph_pagelist_append(req->r_trail,
353 &prot_ver, sizeof(prot_ver));
354 ceph_pagelist_append(req->r_trail,
355 &timeout, sizeof(timeout));
357 case CEPH_OSD_OP_NOTIFY_ACK:
358 case CEPH_OSD_OP_WATCH:
359 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
360 dst->watch.ver = cpu_to_le64(src->watch.ver);
361 dst->watch.flag = src->watch.flag;
364 pr_err("unrecognized osd opcode %d\n", dst->op);
368 dst->payload_len = cpu_to_le32(src->payload_len);
372 * build new request AND message
375 void ceph_osdc_build_request(struct ceph_osd_request *req,
377 struct ceph_osd_req_op *src_ops,
378 struct ceph_snap_context *snapc,
379 struct timespec *mtime,
383 struct ceph_msg *msg = req->r_request;
384 struct ceph_osd_request_head *head;
385 struct ceph_osd_req_op *src_op;
386 struct ceph_osd_op *op;
388 int num_op = get_num_ops(src_ops, NULL);
389 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
390 int flags = req->r_flags;
394 head = msg->front.iov_base;
395 op = (void *)(head + 1);
396 p = (void *)(op + num_op);
398 req->r_snapc = ceph_get_snap_context(snapc);
400 head->client_inc = cpu_to_le32(1); /* always, for now. */
401 head->flags = cpu_to_le32(flags);
402 if (flags & CEPH_OSD_FLAG_WRITE)
403 ceph_encode_timespec(&head->mtime, mtime);
404 head->num_ops = cpu_to_le16(num_op);
408 head->object_len = cpu_to_le32(oid_len);
409 memcpy(p, oid, oid_len);
414 osd_req_encode_op(req, op, src_op);
420 data_len += req->r_trail->length;
423 head->snap_seq = cpu_to_le64(snapc->seq);
424 head->num_snaps = cpu_to_le32(snapc->num_snaps);
425 for (i = 0; i < snapc->num_snaps; i++) {
426 put_unaligned_le64(snapc->snaps[i], p);
431 if (flags & CEPH_OSD_FLAG_WRITE) {
432 req->r_request->hdr.data_off = cpu_to_le16(off);
433 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
434 } else if (data_len) {
435 req->r_request->hdr.data_off = 0;
436 req->r_request->hdr.data_len = cpu_to_le32(data_len);
439 req->r_request->page_alignment = req->r_page_alignment;
441 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
442 msg_size = p - msg->front.iov_base;
443 msg->front.iov_len = msg_size;
444 msg->hdr.front_len = cpu_to_le32(msg_size);
447 EXPORT_SYMBOL(ceph_osdc_build_request);
450 * build new request AND message, calculate layout, and adjust file
453 * if the file was recently truncated, we include information about its
454 * old and new size so that the object can be updated appropriately. (we
455 * avoid synchronously deleting truncated objects because it's slow.)
457 * if @do_sync, include a 'startsync' command so that the osd will flush
460 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
461 struct ceph_file_layout *layout,
462 struct ceph_vino vino,
464 int opcode, int flags,
465 struct ceph_snap_context *snapc,
469 struct timespec *mtime,
470 bool use_mempool, int num_reply,
473 struct ceph_osd_req_op ops[3];
474 struct ceph_osd_request *req;
478 ops[0].extent.truncate_seq = truncate_seq;
479 ops[0].extent.truncate_size = truncate_size;
480 ops[0].payload_len = 0;
483 ops[1].op = CEPH_OSD_OP_STARTSYNC;
484 ops[1].payload_len = 0;
489 req = ceph_osdc_alloc_request(osdc, flags,
492 GFP_NOFS, NULL, NULL);
494 return ERR_PTR(-ENOMEM);
496 /* calculate max write size */
497 r = calc_layout(osdc, vino, layout, off, plen, req, ops);
500 req->r_file_layout = *layout; /* keep a copy */
502 /* in case it differs from natural (file) alignment that
503 calc_layout filled in for us */
504 req->r_num_pages = calc_pages_for(page_align, *plen);
505 req->r_page_alignment = page_align;
507 ceph_osdc_build_request(req, off, plen, ops,
510 req->r_oid, req->r_oid_len);
514 EXPORT_SYMBOL(ceph_osdc_new_request);
517 * We keep osd requests in an rbtree, sorted by ->r_tid.
519 static void __insert_request(struct ceph_osd_client *osdc,
520 struct ceph_osd_request *new)
522 struct rb_node **p = &osdc->requests.rb_node;
523 struct rb_node *parent = NULL;
524 struct ceph_osd_request *req = NULL;
528 req = rb_entry(parent, struct ceph_osd_request, r_node);
529 if (new->r_tid < req->r_tid)
531 else if (new->r_tid > req->r_tid)
537 rb_link_node(&new->r_node, parent, p);
538 rb_insert_color(&new->r_node, &osdc->requests);
541 static struct ceph_osd_request *__lookup_request(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)
551 else if (tid > req->r_tid)
559 static struct ceph_osd_request *
560 __lookup_request_ge(struct ceph_osd_client *osdc,
563 struct ceph_osd_request *req;
564 struct rb_node *n = osdc->requests.rb_node;
567 req = rb_entry(n, struct ceph_osd_request, r_node);
568 if (tid < req->r_tid) {
572 } else if (tid > req->r_tid) {
582 * Resubmit requests pending on the given osd.
584 static void __kick_osd_requests(struct ceph_osd_client *osdc,
585 struct ceph_osd *osd)
587 struct ceph_osd_request *req, *nreq;
590 dout("__kick_osd_requests osd%d\n", osd->o_osd);
591 err = __reset_osd(osdc, osd);
595 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
596 list_move(&req->r_req_lru_item, &osdc->req_unsent);
597 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
600 req->r_flags |= CEPH_OSD_FLAG_RETRY;
603 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
606 * reregister request prior to unregistering linger so
607 * that r_osd is preserved.
609 BUG_ON(!list_empty(&req->r_req_lru_item));
610 __register_request(osdc, req);
611 list_add(&req->r_req_lru_item, &osdc->req_unsent);
612 list_add(&req->r_osd_item, &req->r_osd->o_requests);
613 __unregister_linger_request(osdc, req);
614 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
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 mutex_lock(&osdc->request_mutex);
633 __kick_osd_requests(osdc, osd);
634 mutex_unlock(&osdc->request_mutex);
636 up_read(&osdc->map_sem);
640 * Track open sessions with osds.
642 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
644 struct ceph_osd *osd;
646 osd = kzalloc(sizeof(*osd), GFP_NOFS);
650 atomic_set(&osd->o_ref, 1);
653 RB_CLEAR_NODE(&osd->o_node);
654 INIT_LIST_HEAD(&osd->o_requests);
655 INIT_LIST_HEAD(&osd->o_linger_requests);
656 INIT_LIST_HEAD(&osd->o_osd_lru);
657 osd->o_incarnation = 1;
659 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
661 INIT_LIST_HEAD(&osd->o_keepalive_item);
665 static struct ceph_osd *get_osd(struct ceph_osd *osd)
667 if (atomic_inc_not_zero(&osd->o_ref)) {
668 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
669 atomic_read(&osd->o_ref));
672 dout("get_osd %p FAIL\n", osd);
677 static void put_osd(struct ceph_osd *osd)
679 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
680 atomic_read(&osd->o_ref) - 1);
681 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
682 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
684 if (ac->ops && ac->ops->destroy_authorizer)
685 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
691 * remove an osd from our map
693 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
695 dout("__remove_osd %p\n", osd);
696 BUG_ON(!list_empty(&osd->o_requests));
697 rb_erase(&osd->o_node, &osdc->osds);
698 list_del_init(&osd->o_osd_lru);
699 ceph_con_close(&osd->o_con);
703 static void remove_all_osds(struct ceph_osd_client *osdc)
705 dout("%s %p\n", __func__, osdc);
706 mutex_lock(&osdc->request_mutex);
707 while (!RB_EMPTY_ROOT(&osdc->osds)) {
708 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
709 struct ceph_osd, o_node);
710 __remove_osd(osdc, osd);
712 mutex_unlock(&osdc->request_mutex);
715 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
716 struct ceph_osd *osd)
718 dout("__move_osd_to_lru %p\n", osd);
719 BUG_ON(!list_empty(&osd->o_osd_lru));
720 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
721 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
724 static void __remove_osd_from_lru(struct ceph_osd *osd)
726 dout("__remove_osd_from_lru %p\n", osd);
727 if (!list_empty(&osd->o_osd_lru))
728 list_del_init(&osd->o_osd_lru);
731 static void remove_old_osds(struct ceph_osd_client *osdc)
733 struct ceph_osd *osd, *nosd;
735 dout("__remove_old_osds %p\n", osdc);
736 mutex_lock(&osdc->request_mutex);
737 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
738 if (time_before(jiffies, osd->lru_ttl))
740 __remove_osd(osdc, osd);
742 mutex_unlock(&osdc->request_mutex);
748 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
750 struct ceph_entity_addr *peer_addr;
752 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
753 if (list_empty(&osd->o_requests) &&
754 list_empty(&osd->o_linger_requests)) {
755 __remove_osd(osdc, osd);
760 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
761 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
762 !ceph_con_opened(&osd->o_con)) {
763 struct ceph_osd_request *req;
765 dout(" osd addr hasn't changed and connection never opened,"
766 " letting msgr retry");
767 /* touch each r_stamp for handle_timeout()'s benfit */
768 list_for_each_entry(req, &osd->o_requests, r_osd_item)
769 req->r_stamp = jiffies;
774 ceph_con_close(&osd->o_con);
775 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
776 osd->o_incarnation++;
781 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
783 struct rb_node **p = &osdc->osds.rb_node;
784 struct rb_node *parent = NULL;
785 struct ceph_osd *osd = NULL;
787 dout("__insert_osd %p osd%d\n", new, new->o_osd);
790 osd = rb_entry(parent, struct ceph_osd, o_node);
791 if (new->o_osd < osd->o_osd)
793 else if (new->o_osd > osd->o_osd)
799 rb_link_node(&new->o_node, parent, p);
800 rb_insert_color(&new->o_node, &osdc->osds);
803 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
805 struct ceph_osd *osd;
806 struct rb_node *n = osdc->osds.rb_node;
809 osd = rb_entry(n, struct ceph_osd, o_node);
812 else if (o > osd->o_osd)
820 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
822 schedule_delayed_work(&osdc->timeout_work,
823 osdc->client->options->osd_keepalive_timeout * HZ);
826 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
828 cancel_delayed_work(&osdc->timeout_work);
832 * Register request, assign tid. If this is the first request, set up
835 static void __register_request(struct ceph_osd_client *osdc,
836 struct ceph_osd_request *req)
838 req->r_tid = ++osdc->last_tid;
839 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
840 dout("__register_request %p tid %lld\n", req, req->r_tid);
841 __insert_request(osdc, req);
842 ceph_osdc_get_request(req);
843 osdc->num_requests++;
844 if (osdc->num_requests == 1) {
845 dout(" first request, scheduling timeout\n");
846 __schedule_osd_timeout(osdc);
850 static void register_request(struct ceph_osd_client *osdc,
851 struct ceph_osd_request *req)
853 mutex_lock(&osdc->request_mutex);
854 __register_request(osdc, req);
855 mutex_unlock(&osdc->request_mutex);
859 * called under osdc->request_mutex
861 static void __unregister_request(struct ceph_osd_client *osdc,
862 struct ceph_osd_request *req)
864 if (RB_EMPTY_NODE(&req->r_node)) {
865 dout("__unregister_request %p tid %lld not registered\n",
870 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
871 rb_erase(&req->r_node, &osdc->requests);
872 osdc->num_requests--;
875 /* make sure the original request isn't in flight. */
876 ceph_msg_revoke(req->r_request);
878 list_del_init(&req->r_osd_item);
879 if (list_empty(&req->r_osd->o_requests) &&
880 list_empty(&req->r_osd->o_linger_requests)) {
881 dout("moving osd to %p lru\n", req->r_osd);
882 __move_osd_to_lru(osdc, req->r_osd);
884 if (list_empty(&req->r_linger_item))
888 list_del_init(&req->r_req_lru_item);
889 ceph_osdc_put_request(req);
891 if (osdc->num_requests == 0) {
892 dout(" no requests, canceling timeout\n");
893 __cancel_osd_timeout(osdc);
898 * Cancel a previously queued request message
900 static void __cancel_request(struct ceph_osd_request *req)
902 if (req->r_sent && req->r_osd) {
903 ceph_msg_revoke(req->r_request);
908 static void __register_linger_request(struct ceph_osd_client *osdc,
909 struct ceph_osd_request *req)
911 dout("__register_linger_request %p\n", req);
912 list_add_tail(&req->r_linger_item, &osdc->req_linger);
914 list_add_tail(&req->r_linger_osd,
915 &req->r_osd->o_linger_requests);
918 static void __unregister_linger_request(struct ceph_osd_client *osdc,
919 struct ceph_osd_request *req)
921 dout("__unregister_linger_request %p\n", req);
922 list_del_init(&req->r_linger_item);
924 list_del_init(&req->r_linger_osd);
926 if (list_empty(&req->r_osd->o_requests) &&
927 list_empty(&req->r_osd->o_linger_requests)) {
928 dout("moving osd to %p lru\n", req->r_osd);
929 __move_osd_to_lru(osdc, req->r_osd);
931 if (list_empty(&req->r_osd_item))
936 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
937 struct ceph_osd_request *req)
939 mutex_lock(&osdc->request_mutex);
941 __unregister_linger_request(osdc, req);
942 ceph_osdc_put_request(req);
944 mutex_unlock(&osdc->request_mutex);
946 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
948 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
949 struct ceph_osd_request *req)
951 if (!req->r_linger) {
952 dout("set_request_linger %p\n", req);
955 * caller is now responsible for calling
956 * unregister_linger_request
958 ceph_osdc_get_request(req);
961 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
964 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
965 * (as needed), and set the request r_osd appropriately. If there is
966 * no up osd, set r_osd to NULL. Move the request to the appropriate list
967 * (unsent, homeless) or leave on in-flight lru.
969 * Return 0 if unchanged, 1 if changed, or negative on error.
971 * Caller should hold map_sem for read and request_mutex.
973 static int __map_request(struct ceph_osd_client *osdc,
974 struct ceph_osd_request *req, int force_resend)
976 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
978 int acting[CEPH_PG_MAX_SIZE];
982 dout("map_request %p tid %lld\n", req, req->r_tid);
983 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
984 &req->r_file_layout, osdc->osdmap);
986 list_move(&req->r_req_lru_item, &osdc->req_notarget);
989 pgid = reqhead->layout.ol_pgid;
992 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
998 if ((!force_resend &&
999 req->r_osd && req->r_osd->o_osd == o &&
1000 req->r_sent >= req->r_osd->o_incarnation &&
1001 req->r_num_pg_osds == num &&
1002 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1003 (req->r_osd == NULL && o == -1))
1004 return 0; /* no change */
1006 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
1007 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
1008 req->r_osd ? req->r_osd->o_osd : -1);
1010 /* record full pg acting set */
1011 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1012 req->r_num_pg_osds = num;
1015 __cancel_request(req);
1016 list_del_init(&req->r_osd_item);
1020 req->r_osd = __lookup_osd(osdc, o);
1021 if (!req->r_osd && o >= 0) {
1023 req->r_osd = create_osd(osdc, o);
1025 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1029 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1030 __insert_osd(osdc, req->r_osd);
1032 ceph_con_open(&req->r_osd->o_con,
1033 CEPH_ENTITY_TYPE_OSD, o,
1034 &osdc->osdmap->osd_addr[o]);
1038 __remove_osd_from_lru(req->r_osd);
1039 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1040 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1042 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1044 err = 1; /* osd or pg changed */
1051 * caller should hold map_sem (for read) and request_mutex
1053 static void __send_request(struct ceph_osd_client *osdc,
1054 struct ceph_osd_request *req)
1056 struct ceph_osd_request_head *reqhead;
1058 dout("send_request %p tid %llu to osd%d flags %d\n",
1059 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1061 reqhead = req->r_request->front.iov_base;
1062 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1063 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1064 reqhead->reassert_version = req->r_reassert_version;
1066 req->r_stamp = jiffies;
1067 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1069 ceph_msg_get(req->r_request); /* send consumes a ref */
1070 ceph_con_send(&req->r_osd->o_con, req->r_request);
1071 req->r_sent = req->r_osd->o_incarnation;
1075 * Send any requests in the queue (req_unsent).
1077 static void send_queued(struct ceph_osd_client *osdc)
1079 struct ceph_osd_request *req, *tmp;
1081 dout("send_queued\n");
1082 mutex_lock(&osdc->request_mutex);
1083 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1084 __send_request(osdc, req);
1086 mutex_unlock(&osdc->request_mutex);
1090 * Timeout callback, called every N seconds when 1 or more osd
1091 * requests has been active for more than N seconds. When this
1092 * happens, we ping all OSDs with requests who have timed out to
1093 * ensure any communications channel reset is detected. Reset the
1094 * request timeouts another N seconds in the future as we go.
1095 * Reschedule the timeout event another N seconds in future (unless
1096 * there are no open requests).
1098 static void handle_timeout(struct work_struct *work)
1100 struct ceph_osd_client *osdc =
1101 container_of(work, struct ceph_osd_client, timeout_work.work);
1102 struct ceph_osd_request *req;
1103 struct ceph_osd *osd;
1104 unsigned long keepalive =
1105 osdc->client->options->osd_keepalive_timeout * HZ;
1106 struct list_head slow_osds;
1108 down_read(&osdc->map_sem);
1110 ceph_monc_request_next_osdmap(&osdc->client->monc);
1112 mutex_lock(&osdc->request_mutex);
1115 * ping osds that are a bit slow. this ensures that if there
1116 * is a break in the TCP connection we will notice, and reopen
1117 * a connection with that osd (from the fault callback).
1119 INIT_LIST_HEAD(&slow_osds);
1120 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1121 if (time_before(jiffies, req->r_stamp + keepalive))
1126 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1127 req->r_tid, osd->o_osd);
1128 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1130 while (!list_empty(&slow_osds)) {
1131 osd = list_entry(slow_osds.next, struct ceph_osd,
1133 list_del_init(&osd->o_keepalive_item);
1134 ceph_con_keepalive(&osd->o_con);
1137 __schedule_osd_timeout(osdc);
1138 mutex_unlock(&osdc->request_mutex);
1140 up_read(&osdc->map_sem);
1143 static void handle_osds_timeout(struct work_struct *work)
1145 struct ceph_osd_client *osdc =
1146 container_of(work, struct ceph_osd_client,
1147 osds_timeout_work.work);
1148 unsigned long delay =
1149 osdc->client->options->osd_idle_ttl * HZ >> 2;
1151 dout("osds timeout\n");
1152 down_read(&osdc->map_sem);
1153 remove_old_osds(osdc);
1154 up_read(&osdc->map_sem);
1156 schedule_delayed_work(&osdc->osds_timeout_work,
1157 round_jiffies_relative(delay));
1160 static void complete_request(struct ceph_osd_request *req)
1162 if (req->r_safe_callback)
1163 req->r_safe_callback(req, NULL);
1164 complete_all(&req->r_safe_completion); /* fsync waiter */
1168 * handle osd op reply. either call the callback if it is specified,
1169 * or do the completion to wake up the waiting thread.
1171 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1172 struct ceph_connection *con)
1174 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1175 struct ceph_osd_request *req;
1177 int numops, object_len, flags;
1180 tid = le64_to_cpu(msg->hdr.tid);
1181 if (msg->front.iov_len < sizeof(*rhead))
1183 numops = le32_to_cpu(rhead->num_ops);
1184 object_len = le32_to_cpu(rhead->object_len);
1185 result = le32_to_cpu(rhead->result);
1186 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1187 numops * sizeof(struct ceph_osd_op))
1189 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1191 mutex_lock(&osdc->request_mutex);
1192 req = __lookup_request(osdc, tid);
1194 dout("handle_reply tid %llu dne\n", tid);
1195 mutex_unlock(&osdc->request_mutex);
1198 ceph_osdc_get_request(req);
1199 flags = le32_to_cpu(rhead->flags);
1202 * if this connection filled our message, drop our reference now, to
1203 * avoid a (safe but slower) revoke later.
1205 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1206 dout(" dropping con_filling_msg ref %p\n", con);
1207 req->r_con_filling_msg = NULL;
1211 if (!req->r_got_reply) {
1214 req->r_result = le32_to_cpu(rhead->result);
1215 bytes = le32_to_cpu(msg->hdr.data_len);
1216 dout("handle_reply result %d bytes %d\n", req->r_result,
1218 if (req->r_result == 0)
1219 req->r_result = bytes;
1221 /* in case this is a write and we need to replay, */
1222 req->r_reassert_version = rhead->reassert_version;
1224 req->r_got_reply = 1;
1225 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1226 dout("handle_reply tid %llu dup ack\n", tid);
1227 mutex_unlock(&osdc->request_mutex);
1231 dout("handle_reply tid %llu flags %d\n", tid, flags);
1233 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1234 __register_linger_request(osdc, req);
1236 /* either this is a read, or we got the safe response */
1238 (flags & CEPH_OSD_FLAG_ONDISK) ||
1239 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1240 __unregister_request(osdc, req);
1242 mutex_unlock(&osdc->request_mutex);
1244 if (req->r_callback)
1245 req->r_callback(req, msg);
1247 complete_all(&req->r_completion);
1249 if (flags & CEPH_OSD_FLAG_ONDISK)
1250 complete_request(req);
1253 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1254 ceph_osdc_put_request(req);
1258 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1259 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1260 (int)sizeof(*rhead));
1264 static void reset_changed_osds(struct ceph_osd_client *osdc)
1266 struct rb_node *p, *n;
1268 for (p = rb_first(&osdc->osds); p; p = n) {
1269 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1272 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1273 memcmp(&osd->o_con.peer_addr,
1274 ceph_osd_addr(osdc->osdmap,
1276 sizeof(struct ceph_entity_addr)) != 0)
1277 __reset_osd(osdc, osd);
1282 * Requeue requests whose mapping to an OSD has changed. If requests map to
1283 * no osd, request a new map.
1285 * Caller should hold map_sem for read.
1287 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1289 struct ceph_osd_request *req, *nreq;
1294 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1295 mutex_lock(&osdc->request_mutex);
1296 for (p = rb_first(&osdc->requests); p; ) {
1297 req = rb_entry(p, struct ceph_osd_request, r_node);
1301 * For linger requests that have not yet been
1302 * registered, move them to the linger list; they'll
1303 * be sent to the osd in the loop below. Unregister
1304 * the request before re-registering it as a linger
1305 * request to ensure the __map_request() below
1306 * will decide it needs to be sent.
1308 if (req->r_linger && list_empty(&req->r_linger_item)) {
1309 dout("%p tid %llu restart on osd%d\n",
1311 req->r_osd ? req->r_osd->o_osd : -1);
1312 __unregister_request(osdc, req);
1313 __register_linger_request(osdc, req);
1317 err = __map_request(osdc, req, force_resend);
1319 continue; /* error */
1320 if (req->r_osd == NULL) {
1321 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1322 needmap++; /* request a newer map */
1323 } else if (err > 0) {
1324 if (!req->r_linger) {
1325 dout("%p tid %llu requeued on osd%d\n", req,
1327 req->r_osd ? req->r_osd->o_osd : -1);
1328 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1333 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1335 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1337 err = __map_request(osdc, req, force_resend);
1338 dout("__map_request returned %d\n", err);
1340 continue; /* no change and no osd was specified */
1342 continue; /* hrm! */
1343 if (req->r_osd == NULL) {
1344 dout("tid %llu maps to no valid osd\n", req->r_tid);
1345 needmap++; /* request a newer map */
1349 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1350 req->r_osd ? req->r_osd->o_osd : -1);
1351 __register_request(osdc, req);
1352 __unregister_linger_request(osdc, req);
1354 mutex_unlock(&osdc->request_mutex);
1357 dout("%d requests for down osds, need new map\n", needmap);
1358 ceph_monc_request_next_osdmap(&osdc->client->monc);
1360 reset_changed_osds(osdc);
1365 * Process updated osd map.
1367 * The message contains any number of incremental and full maps, normally
1368 * indicating some sort of topology change in the cluster. Kick requests
1369 * off to different OSDs as needed.
1371 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1373 void *p, *end, *next;
1374 u32 nr_maps, maplen;
1376 struct ceph_osdmap *newmap = NULL, *oldmap;
1378 struct ceph_fsid fsid;
1380 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1381 p = msg->front.iov_base;
1382 end = p + msg->front.iov_len;
1385 ceph_decode_need(&p, end, sizeof(fsid), bad);
1386 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1387 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1390 down_write(&osdc->map_sem);
1392 /* incremental maps */
1393 ceph_decode_32_safe(&p, end, nr_maps, bad);
1394 dout(" %d inc maps\n", nr_maps);
1395 while (nr_maps > 0) {
1396 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1397 epoch = ceph_decode_32(&p);
1398 maplen = ceph_decode_32(&p);
1399 ceph_decode_need(&p, end, maplen, bad);
1401 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1402 dout("applying incremental map %u len %d\n",
1404 newmap = osdmap_apply_incremental(&p, next,
1406 &osdc->client->msgr);
1407 if (IS_ERR(newmap)) {
1408 err = PTR_ERR(newmap);
1412 if (newmap != osdc->osdmap) {
1413 ceph_osdmap_destroy(osdc->osdmap);
1414 osdc->osdmap = newmap;
1416 kick_requests(osdc, 0);
1418 dout("ignoring incremental map %u len %d\n",
1428 ceph_decode_32_safe(&p, end, nr_maps, bad);
1429 dout(" %d full maps\n", nr_maps);
1431 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1432 epoch = ceph_decode_32(&p);
1433 maplen = ceph_decode_32(&p);
1434 ceph_decode_need(&p, end, maplen, bad);
1436 dout("skipping non-latest full map %u len %d\n",
1438 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1439 dout("skipping full map %u len %d, "
1440 "older than our %u\n", epoch, maplen,
1441 osdc->osdmap->epoch);
1443 int skipped_map = 0;
1445 dout("taking full map %u len %d\n", epoch, maplen);
1446 newmap = osdmap_decode(&p, p+maplen);
1447 if (IS_ERR(newmap)) {
1448 err = PTR_ERR(newmap);
1452 oldmap = osdc->osdmap;
1453 osdc->osdmap = newmap;
1455 if (oldmap->epoch + 1 < newmap->epoch)
1457 ceph_osdmap_destroy(oldmap);
1459 kick_requests(osdc, skipped_map);
1466 downgrade_write(&osdc->map_sem);
1467 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1470 * subscribe to subsequent osdmap updates if full to ensure
1471 * we find out when we are no longer full and stop returning
1474 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1475 ceph_monc_request_next_osdmap(&osdc->client->monc);
1478 up_read(&osdc->map_sem);
1479 wake_up_all(&osdc->client->auth_wq);
1483 pr_err("osdc handle_map corrupt msg\n");
1485 up_write(&osdc->map_sem);
1490 * watch/notify callback event infrastructure
1492 * These callbacks are used both for watch and notify operations.
1494 static void __release_event(struct kref *kref)
1496 struct ceph_osd_event *event =
1497 container_of(kref, struct ceph_osd_event, kref);
1499 dout("__release_event %p\n", event);
1503 static void get_event(struct ceph_osd_event *event)
1505 kref_get(&event->kref);
1508 void ceph_osdc_put_event(struct ceph_osd_event *event)
1510 kref_put(&event->kref, __release_event);
1512 EXPORT_SYMBOL(ceph_osdc_put_event);
1514 static void __insert_event(struct ceph_osd_client *osdc,
1515 struct ceph_osd_event *new)
1517 struct rb_node **p = &osdc->event_tree.rb_node;
1518 struct rb_node *parent = NULL;
1519 struct ceph_osd_event *event = NULL;
1523 event = rb_entry(parent, struct ceph_osd_event, node);
1524 if (new->cookie < event->cookie)
1526 else if (new->cookie > event->cookie)
1527 p = &(*p)->rb_right;
1532 rb_link_node(&new->node, parent, p);
1533 rb_insert_color(&new->node, &osdc->event_tree);
1536 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1539 struct rb_node **p = &osdc->event_tree.rb_node;
1540 struct rb_node *parent = NULL;
1541 struct ceph_osd_event *event = NULL;
1545 event = rb_entry(parent, struct ceph_osd_event, node);
1546 if (cookie < event->cookie)
1548 else if (cookie > event->cookie)
1549 p = &(*p)->rb_right;
1556 static void __remove_event(struct ceph_osd_event *event)
1558 struct ceph_osd_client *osdc = event->osdc;
1560 if (!RB_EMPTY_NODE(&event->node)) {
1561 dout("__remove_event removed %p\n", event);
1562 rb_erase(&event->node, &osdc->event_tree);
1563 ceph_osdc_put_event(event);
1565 dout("__remove_event didn't remove %p\n", event);
1569 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1570 void (*event_cb)(u64, u64, u8, void *),
1571 int one_shot, void *data,
1572 struct ceph_osd_event **pevent)
1574 struct ceph_osd_event *event;
1576 event = kmalloc(sizeof(*event), GFP_NOIO);
1580 dout("create_event %p\n", event);
1581 event->cb = event_cb;
1582 event->one_shot = one_shot;
1585 INIT_LIST_HEAD(&event->osd_node);
1586 RB_CLEAR_NODE(&event->node);
1587 kref_init(&event->kref); /* one ref for us */
1588 kref_get(&event->kref); /* one ref for the caller */
1589 init_completion(&event->completion);
1591 spin_lock(&osdc->event_lock);
1592 event->cookie = ++osdc->event_count;
1593 __insert_event(osdc, event);
1594 spin_unlock(&osdc->event_lock);
1599 EXPORT_SYMBOL(ceph_osdc_create_event);
1601 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1603 struct ceph_osd_client *osdc = event->osdc;
1605 dout("cancel_event %p\n", event);
1606 spin_lock(&osdc->event_lock);
1607 __remove_event(event);
1608 spin_unlock(&osdc->event_lock);
1609 ceph_osdc_put_event(event); /* caller's */
1611 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1614 static void do_event_work(struct work_struct *work)
1616 struct ceph_osd_event_work *event_work =
1617 container_of(work, struct ceph_osd_event_work, work);
1618 struct ceph_osd_event *event = event_work->event;
1619 u64 ver = event_work->ver;
1620 u64 notify_id = event_work->notify_id;
1621 u8 opcode = event_work->opcode;
1623 dout("do_event_work completing %p\n", event);
1624 event->cb(ver, notify_id, opcode, event->data);
1625 complete(&event->completion);
1626 dout("do_event_work completed %p\n", event);
1627 ceph_osdc_put_event(event);
1633 * Process osd watch notifications
1635 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1639 u64 cookie, ver, notify_id;
1641 struct ceph_osd_event *event;
1642 struct ceph_osd_event_work *event_work;
1644 p = msg->front.iov_base;
1645 end = p + msg->front.iov_len;
1647 ceph_decode_8_safe(&p, end, proto_ver, bad);
1648 ceph_decode_8_safe(&p, end, opcode, bad);
1649 ceph_decode_64_safe(&p, end, cookie, bad);
1650 ceph_decode_64_safe(&p, end, ver, bad);
1651 ceph_decode_64_safe(&p, end, notify_id, bad);
1653 spin_lock(&osdc->event_lock);
1654 event = __find_event(osdc, cookie);
1657 if (event->one_shot)
1658 __remove_event(event);
1660 spin_unlock(&osdc->event_lock);
1661 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1662 cookie, ver, event);
1664 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1666 dout("ERROR: could not allocate event_work\n");
1669 INIT_WORK(&event_work->work, do_event_work);
1670 event_work->event = event;
1671 event_work->ver = ver;
1672 event_work->notify_id = notify_id;
1673 event_work->opcode = opcode;
1674 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1675 dout("WARNING: failed to queue notify event work\n");
1683 complete(&event->completion);
1684 ceph_osdc_put_event(event);
1688 pr_err("osdc handle_watch_notify corrupt msg\n");
1692 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1696 dout("wait_event %p\n", event);
1697 err = wait_for_completion_interruptible_timeout(&event->completion,
1699 ceph_osdc_put_event(event);
1702 dout("wait_event %p returns %d\n", event, err);
1705 EXPORT_SYMBOL(ceph_osdc_wait_event);
1708 * Register request, send initial attempt.
1710 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1711 struct ceph_osd_request *req,
1716 req->r_request->pages = req->r_pages;
1717 req->r_request->nr_pages = req->r_num_pages;
1719 req->r_request->bio = req->r_bio;
1721 req->r_request->trail = req->r_trail;
1723 register_request(osdc, req);
1725 down_read(&osdc->map_sem);
1726 mutex_lock(&osdc->request_mutex);
1728 * a racing kick_requests() may have sent the message for us
1729 * while we dropped request_mutex above, so only send now if
1730 * the request still han't been touched yet.
1732 if (req->r_sent == 0) {
1733 rc = __map_request(osdc, req, 0);
1736 dout("osdc_start_request failed map, "
1737 " will retry %lld\n", req->r_tid);
1742 if (req->r_osd == NULL) {
1743 dout("send_request %p no up osds in pg\n", req);
1744 ceph_monc_request_next_osdmap(&osdc->client->monc);
1746 __send_request(osdc, req);
1752 mutex_unlock(&osdc->request_mutex);
1753 up_read(&osdc->map_sem);
1756 EXPORT_SYMBOL(ceph_osdc_start_request);
1759 * wait for a request to complete
1761 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1762 struct ceph_osd_request *req)
1766 rc = wait_for_completion_interruptible(&req->r_completion);
1768 mutex_lock(&osdc->request_mutex);
1769 __cancel_request(req);
1770 __unregister_request(osdc, req);
1771 mutex_unlock(&osdc->request_mutex);
1772 complete_request(req);
1773 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1777 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1778 return req->r_result;
1780 EXPORT_SYMBOL(ceph_osdc_wait_request);
1783 * sync - wait for all in-flight requests to flush. avoid starvation.
1785 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1787 struct ceph_osd_request *req;
1788 u64 last_tid, next_tid = 0;
1790 mutex_lock(&osdc->request_mutex);
1791 last_tid = osdc->last_tid;
1793 req = __lookup_request_ge(osdc, next_tid);
1796 if (req->r_tid > last_tid)
1799 next_tid = req->r_tid + 1;
1800 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1803 ceph_osdc_get_request(req);
1804 mutex_unlock(&osdc->request_mutex);
1805 dout("sync waiting on tid %llu (last is %llu)\n",
1806 req->r_tid, last_tid);
1807 wait_for_completion(&req->r_safe_completion);
1808 mutex_lock(&osdc->request_mutex);
1809 ceph_osdc_put_request(req);
1811 mutex_unlock(&osdc->request_mutex);
1812 dout("sync done (thru tid %llu)\n", last_tid);
1814 EXPORT_SYMBOL(ceph_osdc_sync);
1819 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1824 osdc->client = client;
1825 osdc->osdmap = NULL;
1826 init_rwsem(&osdc->map_sem);
1827 init_completion(&osdc->map_waiters);
1828 osdc->last_requested_map = 0;
1829 mutex_init(&osdc->request_mutex);
1831 osdc->osds = RB_ROOT;
1832 INIT_LIST_HEAD(&osdc->osd_lru);
1833 osdc->requests = RB_ROOT;
1834 INIT_LIST_HEAD(&osdc->req_lru);
1835 INIT_LIST_HEAD(&osdc->req_unsent);
1836 INIT_LIST_HEAD(&osdc->req_notarget);
1837 INIT_LIST_HEAD(&osdc->req_linger);
1838 osdc->num_requests = 0;
1839 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1840 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1841 spin_lock_init(&osdc->event_lock);
1842 osdc->event_tree = RB_ROOT;
1843 osdc->event_count = 0;
1845 schedule_delayed_work(&osdc->osds_timeout_work,
1846 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1849 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1850 sizeof(struct ceph_osd_request));
1851 if (!osdc->req_mempool)
1854 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1855 OSD_OP_FRONT_LEN, 10, true,
1859 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1860 OSD_OPREPLY_FRONT_LEN, 10, true,
1865 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1866 if (IS_ERR(osdc->notify_wq)) {
1867 err = PTR_ERR(osdc->notify_wq);
1868 osdc->notify_wq = NULL;
1874 ceph_msgpool_destroy(&osdc->msgpool_op);
1876 mempool_destroy(osdc->req_mempool);
1880 EXPORT_SYMBOL(ceph_osdc_init);
1882 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1884 flush_workqueue(osdc->notify_wq);
1885 destroy_workqueue(osdc->notify_wq);
1886 cancel_delayed_work_sync(&osdc->timeout_work);
1887 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1889 ceph_osdmap_destroy(osdc->osdmap);
1890 osdc->osdmap = NULL;
1892 remove_all_osds(osdc);
1893 mempool_destroy(osdc->req_mempool);
1894 ceph_msgpool_destroy(&osdc->msgpool_op);
1895 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1897 EXPORT_SYMBOL(ceph_osdc_stop);
1900 * Read some contiguous pages. If we cross a stripe boundary, shorten
1901 * *plen. Return number of bytes read, or error.
1903 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1904 struct ceph_vino vino, struct ceph_file_layout *layout,
1906 u32 truncate_seq, u64 truncate_size,
1907 struct page **pages, int num_pages, int page_align)
1909 struct ceph_osd_request *req;
1912 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1913 vino.snap, off, *plen);
1914 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1915 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1916 NULL, 0, truncate_seq, truncate_size, NULL,
1917 false, 1, page_align);
1919 return PTR_ERR(req);
1921 /* it may be a short read due to an object boundary */
1922 req->r_pages = pages;
1924 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1925 off, *plen, req->r_num_pages, page_align);
1927 rc = ceph_osdc_start_request(osdc, req, false);
1929 rc = ceph_osdc_wait_request(osdc, req);
1931 ceph_osdc_put_request(req);
1932 dout("readpages result %d\n", rc);
1935 EXPORT_SYMBOL(ceph_osdc_readpages);
1938 * do a synchronous write on N pages
1940 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1941 struct ceph_file_layout *layout,
1942 struct ceph_snap_context *snapc,
1944 u32 truncate_seq, u64 truncate_size,
1945 struct timespec *mtime,
1946 struct page **pages, int num_pages,
1947 int flags, int do_sync, bool nofail)
1949 struct ceph_osd_request *req;
1951 int page_align = off & ~PAGE_MASK;
1953 BUG_ON(vino.snap != CEPH_NOSNAP);
1954 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1956 flags | CEPH_OSD_FLAG_ONDISK |
1957 CEPH_OSD_FLAG_WRITE,
1959 truncate_seq, truncate_size, mtime,
1960 nofail, 1, page_align);
1962 return PTR_ERR(req);
1964 /* it may be a short write due to an object boundary */
1965 req->r_pages = pages;
1966 dout("writepages %llu~%llu (%d pages)\n", off, len,
1969 rc = ceph_osdc_start_request(osdc, req, nofail);
1971 rc = ceph_osdc_wait_request(osdc, req);
1973 ceph_osdc_put_request(req);
1976 dout("writepages result %d\n", rc);
1979 EXPORT_SYMBOL(ceph_osdc_writepages);
1982 * handle incoming message
1984 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1986 struct ceph_osd *osd = con->private;
1987 struct ceph_osd_client *osdc;
1988 int type = le16_to_cpu(msg->hdr.type);
1995 case CEPH_MSG_OSD_MAP:
1996 ceph_osdc_handle_map(osdc, msg);
1998 case CEPH_MSG_OSD_OPREPLY:
1999 handle_reply(osdc, msg, con);
2001 case CEPH_MSG_WATCH_NOTIFY:
2002 handle_watch_notify(osdc, msg);
2006 pr_err("received unknown message type %d %s\n", type,
2007 ceph_msg_type_name(type));
2014 * lookup and return message for incoming reply. set up reply message
2017 static struct ceph_msg *get_reply(struct ceph_connection *con,
2018 struct ceph_msg_header *hdr,
2021 struct ceph_osd *osd = con->private;
2022 struct ceph_osd_client *osdc = osd->o_osdc;
2024 struct ceph_osd_request *req;
2025 int front = le32_to_cpu(hdr->front_len);
2026 int data_len = le32_to_cpu(hdr->data_len);
2029 tid = le64_to_cpu(hdr->tid);
2030 mutex_lock(&osdc->request_mutex);
2031 req = __lookup_request(osdc, tid);
2035 dout("get_reply unknown tid %llu from osd%d\n", tid,
2040 if (req->r_con_filling_msg) {
2041 dout("%s revoking msg %p from old con %p\n", __func__,
2042 req->r_reply, req->r_con_filling_msg);
2043 ceph_msg_revoke_incoming(req->r_reply);
2044 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2045 req->r_con_filling_msg = NULL;
2048 if (front > req->r_reply->front.iov_len) {
2049 pr_warning("get_reply front %d > preallocated %d\n",
2050 front, (int)req->r_reply->front.iov_len);
2051 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2054 ceph_msg_put(req->r_reply);
2057 m = ceph_msg_get(req->r_reply);
2060 int want = calc_pages_for(req->r_page_alignment, data_len);
2062 if (unlikely(req->r_num_pages < want)) {
2063 pr_warning("tid %lld reply has %d bytes %d pages, we"
2064 " had only %d pages ready\n", tid, data_len,
2065 want, req->r_num_pages);
2071 m->pages = req->r_pages;
2072 m->nr_pages = req->r_num_pages;
2073 m->page_alignment = req->r_page_alignment;
2075 m->bio = req->r_bio;
2079 req->r_con_filling_msg = con->ops->get(con);
2080 dout("get_reply tid %lld %p\n", tid, m);
2083 mutex_unlock(&osdc->request_mutex);
2088 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2089 struct ceph_msg_header *hdr,
2092 struct ceph_osd *osd = con->private;
2093 int type = le16_to_cpu(hdr->type);
2094 int front = le32_to_cpu(hdr->front_len);
2098 case CEPH_MSG_OSD_MAP:
2099 case CEPH_MSG_WATCH_NOTIFY:
2100 return ceph_msg_new(type, front, GFP_NOFS, false);
2101 case CEPH_MSG_OSD_OPREPLY:
2102 return get_reply(con, hdr, skip);
2104 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2112 * Wrappers to refcount containing ceph_osd struct
2114 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2116 struct ceph_osd *osd = con->private;
2122 static void put_osd_con(struct ceph_connection *con)
2124 struct ceph_osd *osd = con->private;
2132 * Note: returned pointer is the address of a structure that's
2133 * managed separately. Caller must *not* attempt to free it.
2135 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2136 int *proto, int force_new)
2138 struct ceph_osd *o = con->private;
2139 struct ceph_osd_client *osdc = o->o_osdc;
2140 struct ceph_auth_client *ac = osdc->client->monc.auth;
2141 struct ceph_auth_handshake *auth = &o->o_auth;
2143 if (force_new && auth->authorizer) {
2144 if (ac->ops && ac->ops->destroy_authorizer)
2145 ac->ops->destroy_authorizer(ac, auth->authorizer);
2146 auth->authorizer = NULL;
2148 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2149 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2152 return ERR_PTR(ret);
2154 *proto = ac->protocol;
2160 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2162 struct ceph_osd *o = con->private;
2163 struct ceph_osd_client *osdc = o->o_osdc;
2164 struct ceph_auth_client *ac = osdc->client->monc.auth;
2167 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2168 * XXX which do we do: succeed or fail?
2170 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2173 static int invalidate_authorizer(struct ceph_connection *con)
2175 struct ceph_osd *o = con->private;
2176 struct ceph_osd_client *osdc = o->o_osdc;
2177 struct ceph_auth_client *ac = osdc->client->monc.auth;
2179 if (ac->ops && ac->ops->invalidate_authorizer)
2180 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2182 return ceph_monc_validate_auth(&osdc->client->monc);
2185 static const struct ceph_connection_operations osd_con_ops = {
2188 .dispatch = dispatch,
2189 .get_authorizer = get_authorizer,
2190 .verify_authorizer_reply = verify_authorizer_reply,
2191 .invalidate_authorizer = invalidate_authorizer,
2192 .alloc_msg = alloc_msg,