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_has_extent(int op)
37 return (op == CEPH_OSD_OP_READ ||
38 op == CEPH_OSD_OP_WRITE);
41 int ceph_calc_raw_layout(struct ceph_file_layout *layout,
42 u64 off, u64 *plen, u64 *bno,
43 struct ceph_osd_request *req,
44 struct ceph_osd_req_op *op)
47 u64 objoff, objlen; /* extent in object */
51 r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
55 if (objlen < orig_len) {
57 dout(" skipping last %llu, final file extent %llu~%llu\n",
58 orig_len - *plen, off, *plen);
61 if (op_has_extent(op->op)) {
62 u32 osize = le32_to_cpu(layout->fl_object_size);
63 op->extent.offset = objoff;
64 op->extent.length = objlen;
65 if (op->extent.truncate_size <= off - objoff) {
66 op->extent.truncate_size = 0;
68 op->extent.truncate_size -= off - objoff;
69 if (op->extent.truncate_size > osize)
70 op->extent.truncate_size = osize;
73 req->r_num_pages = calc_pages_for(off, *plen);
74 req->r_page_alignment = off & ~PAGE_MASK;
75 if (op->op == CEPH_OSD_OP_WRITE)
76 op->payload_len = *plen;
78 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
79 *bno, objoff, objlen, req->r_num_pages);
82 EXPORT_SYMBOL(ceph_calc_raw_layout);
85 * Implement client access to distributed object storage cluster.
87 * All data objects are stored within a cluster/cloud of OSDs, or
88 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
89 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
90 * remote daemons serving up and coordinating consistent and safe
93 * Cluster membership and the mapping of data objects onto storage devices
94 * are described by the osd map.
96 * We keep track of pending OSD requests (read, write), resubmit
97 * requests to different OSDs when the cluster topology/data layout
98 * change, or retry the affected requests when the communications
99 * channel with an OSD is reset.
103 * calculate the mapping of a file extent onto an object, and fill out the
104 * request accordingly. shorten extent as necessary if it crosses an
107 * fill osd op in request message.
109 static int calc_layout(struct ceph_vino vino,
110 struct ceph_file_layout *layout,
112 struct ceph_osd_request *req,
113 struct ceph_osd_req_op *op)
118 r = ceph_calc_raw_layout(layout, off, plen, &bno, req, op);
122 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
123 req->r_oid_len = strlen(req->r_oid);
131 void ceph_osdc_release_request(struct kref *kref)
133 struct ceph_osd_request *req = container_of(kref,
134 struct ceph_osd_request,
138 ceph_msg_put(req->r_request);
139 if (req->r_con_filling_msg) {
140 dout("%s revoking pages %p from con %p\n", __func__,
141 req->r_pages, req->r_con_filling_msg);
142 ceph_msg_revoke_incoming(req->r_reply);
143 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
146 ceph_msg_put(req->r_reply);
147 if (req->r_own_pages)
148 ceph_release_page_vector(req->r_pages,
154 ceph_put_snap_context(req->r_snapc);
155 ceph_pagelist_release(&req->r_trail);
157 mempool_free(req, req->r_osdc->req_mempool);
161 EXPORT_SYMBOL(ceph_osdc_release_request);
163 static int get_num_ops(struct ceph_osd_req_op *ops)
173 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
174 struct ceph_snap_context *snapc,
175 struct ceph_osd_req_op *ops,
181 struct ceph_osd_request *req;
182 struct ceph_msg *msg;
183 int num_op = get_num_ops(ops);
184 size_t msg_size = sizeof(struct ceph_osd_request_head);
186 msg_size += num_op*sizeof(struct ceph_osd_op);
189 req = mempool_alloc(osdc->req_mempool, gfp_flags);
190 memset(req, 0, sizeof(*req));
192 req = kzalloc(sizeof(*req), gfp_flags);
198 req->r_mempool = use_mempool;
200 kref_init(&req->r_kref);
201 init_completion(&req->r_completion);
202 init_completion(&req->r_safe_completion);
203 RB_CLEAR_NODE(&req->r_node);
204 INIT_LIST_HEAD(&req->r_unsafe_item);
205 INIT_LIST_HEAD(&req->r_linger_item);
206 INIT_LIST_HEAD(&req->r_linger_osd);
207 INIT_LIST_HEAD(&req->r_req_lru_item);
208 INIT_LIST_HEAD(&req->r_osd_item);
210 /* create reply message */
212 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
214 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
215 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
217 ceph_osdc_put_request(req);
222 ceph_pagelist_init(&req->r_trail);
224 /* create request message; allow space for oid */
225 msg_size += MAX_OBJ_NAME_SIZE;
227 msg_size += sizeof(u64) * snapc->num_snaps;
229 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
231 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
233 ceph_osdc_put_request(req);
237 memset(msg->front.iov_base, 0, msg->front.iov_len);
239 req->r_request = msg;
240 req->r_pages = pages;
250 EXPORT_SYMBOL(ceph_osdc_alloc_request);
252 static void osd_req_encode_op(struct ceph_osd_request *req,
253 struct ceph_osd_op *dst,
254 struct ceph_osd_req_op *src)
256 dst->op = cpu_to_le16(src->op);
259 case CEPH_OSD_OP_READ:
260 case CEPH_OSD_OP_WRITE:
262 cpu_to_le64(src->extent.offset);
264 cpu_to_le64(src->extent.length);
265 dst->extent.truncate_size =
266 cpu_to_le64(src->extent.truncate_size);
267 dst->extent.truncate_seq =
268 cpu_to_le32(src->extent.truncate_seq);
271 case CEPH_OSD_OP_GETXATTR:
272 case CEPH_OSD_OP_SETXATTR:
273 case CEPH_OSD_OP_CMPXATTR:
274 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
275 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
276 dst->xattr.cmp_op = src->xattr.cmp_op;
277 dst->xattr.cmp_mode = src->xattr.cmp_mode;
278 ceph_pagelist_append(&req->r_trail, src->xattr.name,
279 src->xattr.name_len);
280 ceph_pagelist_append(&req->r_trail, src->xattr.val,
281 src->xattr.value_len);
283 case CEPH_OSD_OP_CALL:
284 dst->cls.class_len = src->cls.class_len;
285 dst->cls.method_len = src->cls.method_len;
286 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
288 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
290 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
291 src->cls.method_len);
292 ceph_pagelist_append(&req->r_trail, src->cls.indata,
293 src->cls.indata_len);
295 case CEPH_OSD_OP_ROLLBACK:
296 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
298 case CEPH_OSD_OP_STARTSYNC:
300 case CEPH_OSD_OP_NOTIFY:
302 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
303 __le32 timeout = cpu_to_le32(src->watch.timeout);
305 ceph_pagelist_append(&req->r_trail,
306 &prot_ver, sizeof(prot_ver));
307 ceph_pagelist_append(&req->r_trail,
308 &timeout, sizeof(timeout));
310 case CEPH_OSD_OP_NOTIFY_ACK:
311 case CEPH_OSD_OP_WATCH:
312 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
313 dst->watch.ver = cpu_to_le64(src->watch.ver);
314 dst->watch.flag = src->watch.flag;
317 pr_err("unrecognized osd opcode %d\n", dst->op);
321 dst->payload_len = cpu_to_le32(src->payload_len);
325 * build new request AND message
328 void ceph_osdc_build_request(struct ceph_osd_request *req,
330 struct ceph_osd_req_op *src_ops,
331 struct ceph_snap_context *snapc, u64 snap_id,
332 struct timespec *mtime)
334 struct ceph_msg *msg = req->r_request;
335 struct ceph_osd_request_head *head;
336 struct ceph_osd_req_op *src_op;
337 struct ceph_osd_op *op;
339 int num_op = get_num_ops(src_ops);
340 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
341 int flags = req->r_flags;
345 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
347 head = msg->front.iov_base;
348 head->snapid = cpu_to_le64(snap_id);
349 op = (void *)(head + 1);
350 p = (void *)(op + num_op);
352 req->r_snapc = ceph_get_snap_context(snapc);
354 head->client_inc = cpu_to_le32(1); /* always, for now. */
355 head->flags = cpu_to_le32(flags);
356 if (flags & CEPH_OSD_FLAG_WRITE)
357 ceph_encode_timespec(&head->mtime, mtime);
358 head->num_ops = cpu_to_le16(num_op);
362 head->object_len = cpu_to_le32(req->r_oid_len);
363 memcpy(p, req->r_oid, req->r_oid_len);
368 osd_req_encode_op(req, op, src_op);
373 data_len += req->r_trail.length;
376 head->snap_seq = cpu_to_le64(snapc->seq);
377 head->num_snaps = cpu_to_le32(snapc->num_snaps);
378 for (i = 0; i < snapc->num_snaps; i++) {
379 put_unaligned_le64(snapc->snaps[i], p);
384 if (flags & CEPH_OSD_FLAG_WRITE) {
385 req->r_request->hdr.data_off = cpu_to_le16(off);
386 req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
387 } else if (data_len) {
388 req->r_request->hdr.data_off = 0;
389 req->r_request->hdr.data_len = cpu_to_le32(data_len);
392 req->r_request->page_alignment = req->r_page_alignment;
394 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
395 msg_size = p - msg->front.iov_base;
396 msg->front.iov_len = msg_size;
397 msg->hdr.front_len = cpu_to_le32(msg_size);
400 EXPORT_SYMBOL(ceph_osdc_build_request);
403 * build new request AND message, calculate layout, and adjust file
406 * if the file was recently truncated, we include information about its
407 * old and new size so that the object can be updated appropriately. (we
408 * avoid synchronously deleting truncated objects because it's slow.)
410 * if @do_sync, include a 'startsync' command so that the osd will flush
413 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
414 struct ceph_file_layout *layout,
415 struct ceph_vino vino,
417 int opcode, int flags,
418 struct ceph_snap_context *snapc,
422 struct timespec *mtime,
423 bool use_mempool, int num_reply,
426 struct ceph_osd_req_op ops[3];
427 struct ceph_osd_request *req;
431 ops[0].extent.truncate_seq = truncate_seq;
432 ops[0].extent.truncate_size = truncate_size;
433 ops[0].payload_len = 0;
436 ops[1].op = CEPH_OSD_OP_STARTSYNC;
437 ops[1].payload_len = 0;
442 req = ceph_osdc_alloc_request(osdc, snapc, ops,
444 GFP_NOFS, NULL, NULL);
446 return ERR_PTR(-ENOMEM);
447 req->r_flags = flags;
449 /* calculate max write size */
450 r = calc_layout(vino, layout, off, plen, req, ops);
453 req->r_file_layout = *layout; /* keep a copy */
455 /* in case it differs from natural (file) alignment that
456 calc_layout filled in for us */
457 req->r_num_pages = calc_pages_for(page_align, *plen);
458 req->r_page_alignment = page_align;
460 ceph_osdc_build_request(req, off, *plen, ops, snapc, vino.snap, mtime);
464 EXPORT_SYMBOL(ceph_osdc_new_request);
467 * We keep osd requests in an rbtree, sorted by ->r_tid.
469 static void __insert_request(struct ceph_osd_client *osdc,
470 struct ceph_osd_request *new)
472 struct rb_node **p = &osdc->requests.rb_node;
473 struct rb_node *parent = NULL;
474 struct ceph_osd_request *req = NULL;
478 req = rb_entry(parent, struct ceph_osd_request, r_node);
479 if (new->r_tid < req->r_tid)
481 else if (new->r_tid > req->r_tid)
487 rb_link_node(&new->r_node, parent, p);
488 rb_insert_color(&new->r_node, &osdc->requests);
491 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
494 struct ceph_osd_request *req;
495 struct rb_node *n = osdc->requests.rb_node;
498 req = rb_entry(n, struct ceph_osd_request, r_node);
499 if (tid < req->r_tid)
501 else if (tid > req->r_tid)
509 static struct ceph_osd_request *
510 __lookup_request_ge(struct ceph_osd_client *osdc,
513 struct ceph_osd_request *req;
514 struct rb_node *n = osdc->requests.rb_node;
517 req = rb_entry(n, struct ceph_osd_request, r_node);
518 if (tid < req->r_tid) {
522 } else if (tid > req->r_tid) {
532 * Resubmit requests pending on the given osd.
534 static void __kick_osd_requests(struct ceph_osd_client *osdc,
535 struct ceph_osd *osd)
537 struct ceph_osd_request *req, *nreq;
540 dout("__kick_osd_requests osd%d\n", osd->o_osd);
541 err = __reset_osd(osdc, osd);
545 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
546 list_move(&req->r_req_lru_item, &osdc->req_unsent);
547 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
550 req->r_flags |= CEPH_OSD_FLAG_RETRY;
553 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
556 * reregister request prior to unregistering linger so
557 * that r_osd is preserved.
559 BUG_ON(!list_empty(&req->r_req_lru_item));
560 __register_request(osdc, req);
561 list_add(&req->r_req_lru_item, &osdc->req_unsent);
562 list_add(&req->r_osd_item, &req->r_osd->o_requests);
563 __unregister_linger_request(osdc, req);
564 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
570 * If the osd connection drops, we need to resubmit all requests.
572 static void osd_reset(struct ceph_connection *con)
574 struct ceph_osd *osd = con->private;
575 struct ceph_osd_client *osdc;
579 dout("osd_reset osd%d\n", osd->o_osd);
581 down_read(&osdc->map_sem);
582 mutex_lock(&osdc->request_mutex);
583 __kick_osd_requests(osdc, osd);
584 mutex_unlock(&osdc->request_mutex);
586 up_read(&osdc->map_sem);
590 * Track open sessions with osds.
592 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
594 struct ceph_osd *osd;
596 osd = kzalloc(sizeof(*osd), GFP_NOFS);
600 atomic_set(&osd->o_ref, 1);
603 RB_CLEAR_NODE(&osd->o_node);
604 INIT_LIST_HEAD(&osd->o_requests);
605 INIT_LIST_HEAD(&osd->o_linger_requests);
606 INIT_LIST_HEAD(&osd->o_osd_lru);
607 osd->o_incarnation = 1;
609 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
611 INIT_LIST_HEAD(&osd->o_keepalive_item);
615 static struct ceph_osd *get_osd(struct ceph_osd *osd)
617 if (atomic_inc_not_zero(&osd->o_ref)) {
618 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
619 atomic_read(&osd->o_ref));
622 dout("get_osd %p FAIL\n", osd);
627 static void put_osd(struct ceph_osd *osd)
629 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
630 atomic_read(&osd->o_ref) - 1);
631 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
632 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
634 if (ac->ops && ac->ops->destroy_authorizer)
635 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
641 * remove an osd from our map
643 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
645 dout("__remove_osd %p\n", osd);
646 BUG_ON(!list_empty(&osd->o_requests));
647 rb_erase(&osd->o_node, &osdc->osds);
648 list_del_init(&osd->o_osd_lru);
649 ceph_con_close(&osd->o_con);
653 static void remove_all_osds(struct ceph_osd_client *osdc)
655 dout("%s %p\n", __func__, osdc);
656 mutex_lock(&osdc->request_mutex);
657 while (!RB_EMPTY_ROOT(&osdc->osds)) {
658 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
659 struct ceph_osd, o_node);
660 __remove_osd(osdc, osd);
662 mutex_unlock(&osdc->request_mutex);
665 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
666 struct ceph_osd *osd)
668 dout("__move_osd_to_lru %p\n", osd);
669 BUG_ON(!list_empty(&osd->o_osd_lru));
670 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
671 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
674 static void __remove_osd_from_lru(struct ceph_osd *osd)
676 dout("__remove_osd_from_lru %p\n", osd);
677 if (!list_empty(&osd->o_osd_lru))
678 list_del_init(&osd->o_osd_lru);
681 static void remove_old_osds(struct ceph_osd_client *osdc)
683 struct ceph_osd *osd, *nosd;
685 dout("__remove_old_osds %p\n", osdc);
686 mutex_lock(&osdc->request_mutex);
687 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
688 if (time_before(jiffies, osd->lru_ttl))
690 __remove_osd(osdc, osd);
692 mutex_unlock(&osdc->request_mutex);
698 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
700 struct ceph_entity_addr *peer_addr;
702 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
703 if (list_empty(&osd->o_requests) &&
704 list_empty(&osd->o_linger_requests)) {
705 __remove_osd(osdc, osd);
710 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
711 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
712 !ceph_con_opened(&osd->o_con)) {
713 struct ceph_osd_request *req;
715 dout(" osd addr hasn't changed and connection never opened,"
716 " letting msgr retry");
717 /* touch each r_stamp for handle_timeout()'s benfit */
718 list_for_each_entry(req, &osd->o_requests, r_osd_item)
719 req->r_stamp = jiffies;
724 ceph_con_close(&osd->o_con);
725 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
726 osd->o_incarnation++;
731 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
733 struct rb_node **p = &osdc->osds.rb_node;
734 struct rb_node *parent = NULL;
735 struct ceph_osd *osd = NULL;
737 dout("__insert_osd %p osd%d\n", new, new->o_osd);
740 osd = rb_entry(parent, struct ceph_osd, o_node);
741 if (new->o_osd < osd->o_osd)
743 else if (new->o_osd > osd->o_osd)
749 rb_link_node(&new->o_node, parent, p);
750 rb_insert_color(&new->o_node, &osdc->osds);
753 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
755 struct ceph_osd *osd;
756 struct rb_node *n = osdc->osds.rb_node;
759 osd = rb_entry(n, struct ceph_osd, o_node);
762 else if (o > osd->o_osd)
770 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
772 schedule_delayed_work(&osdc->timeout_work,
773 osdc->client->options->osd_keepalive_timeout * HZ);
776 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
778 cancel_delayed_work(&osdc->timeout_work);
782 * Register request, assign tid. If this is the first request, set up
785 static void __register_request(struct ceph_osd_client *osdc,
786 struct ceph_osd_request *req)
788 req->r_tid = ++osdc->last_tid;
789 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
790 dout("__register_request %p tid %lld\n", req, req->r_tid);
791 __insert_request(osdc, req);
792 ceph_osdc_get_request(req);
793 osdc->num_requests++;
794 if (osdc->num_requests == 1) {
795 dout(" first request, scheduling timeout\n");
796 __schedule_osd_timeout(osdc);
800 static void register_request(struct ceph_osd_client *osdc,
801 struct ceph_osd_request *req)
803 mutex_lock(&osdc->request_mutex);
804 __register_request(osdc, req);
805 mutex_unlock(&osdc->request_mutex);
809 * called under osdc->request_mutex
811 static void __unregister_request(struct ceph_osd_client *osdc,
812 struct ceph_osd_request *req)
814 if (RB_EMPTY_NODE(&req->r_node)) {
815 dout("__unregister_request %p tid %lld not registered\n",
820 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
821 rb_erase(&req->r_node, &osdc->requests);
822 osdc->num_requests--;
825 /* make sure the original request isn't in flight. */
826 ceph_msg_revoke(req->r_request);
828 list_del_init(&req->r_osd_item);
829 if (list_empty(&req->r_osd->o_requests) &&
830 list_empty(&req->r_osd->o_linger_requests)) {
831 dout("moving osd to %p lru\n", req->r_osd);
832 __move_osd_to_lru(osdc, req->r_osd);
834 if (list_empty(&req->r_linger_item))
838 list_del_init(&req->r_req_lru_item);
839 ceph_osdc_put_request(req);
841 if (osdc->num_requests == 0) {
842 dout(" no requests, canceling timeout\n");
843 __cancel_osd_timeout(osdc);
848 * Cancel a previously queued request message
850 static void __cancel_request(struct ceph_osd_request *req)
852 if (req->r_sent && req->r_osd) {
853 ceph_msg_revoke(req->r_request);
858 static void __register_linger_request(struct ceph_osd_client *osdc,
859 struct ceph_osd_request *req)
861 dout("__register_linger_request %p\n", req);
862 list_add_tail(&req->r_linger_item, &osdc->req_linger);
864 list_add_tail(&req->r_linger_osd,
865 &req->r_osd->o_linger_requests);
868 static void __unregister_linger_request(struct ceph_osd_client *osdc,
869 struct ceph_osd_request *req)
871 dout("__unregister_linger_request %p\n", req);
872 list_del_init(&req->r_linger_item);
874 list_del_init(&req->r_linger_osd);
876 if (list_empty(&req->r_osd->o_requests) &&
877 list_empty(&req->r_osd->o_linger_requests)) {
878 dout("moving osd to %p lru\n", req->r_osd);
879 __move_osd_to_lru(osdc, req->r_osd);
881 if (list_empty(&req->r_osd_item))
886 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
887 struct ceph_osd_request *req)
889 mutex_lock(&osdc->request_mutex);
891 __unregister_linger_request(osdc, req);
892 ceph_osdc_put_request(req);
894 mutex_unlock(&osdc->request_mutex);
896 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
898 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
899 struct ceph_osd_request *req)
901 if (!req->r_linger) {
902 dout("set_request_linger %p\n", req);
905 * caller is now responsible for calling
906 * unregister_linger_request
908 ceph_osdc_get_request(req);
911 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
914 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
915 * (as needed), and set the request r_osd appropriately. If there is
916 * no up osd, set r_osd to NULL. Move the request to the appropriate list
917 * (unsent, homeless) or leave on in-flight lru.
919 * Return 0 if unchanged, 1 if changed, or negative on error.
921 * Caller should hold map_sem for read and request_mutex.
923 static int __map_request(struct ceph_osd_client *osdc,
924 struct ceph_osd_request *req, int force_resend)
926 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
928 int acting[CEPH_PG_MAX_SIZE];
932 dout("map_request %p tid %lld\n", req, req->r_tid);
933 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
934 &req->r_file_layout, osdc->osdmap);
936 list_move(&req->r_req_lru_item, &osdc->req_notarget);
939 pgid = reqhead->layout.ol_pgid;
942 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
948 if ((!force_resend &&
949 req->r_osd && req->r_osd->o_osd == o &&
950 req->r_sent >= req->r_osd->o_incarnation &&
951 req->r_num_pg_osds == num &&
952 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
953 (req->r_osd == NULL && o == -1))
954 return 0; /* no change */
956 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
957 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
958 req->r_osd ? req->r_osd->o_osd : -1);
960 /* record full pg acting set */
961 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
962 req->r_num_pg_osds = num;
965 __cancel_request(req);
966 list_del_init(&req->r_osd_item);
970 req->r_osd = __lookup_osd(osdc, o);
971 if (!req->r_osd && o >= 0) {
973 req->r_osd = create_osd(osdc, o);
975 list_move(&req->r_req_lru_item, &osdc->req_notarget);
979 dout("map_request osd %p is osd%d\n", req->r_osd, o);
980 __insert_osd(osdc, req->r_osd);
982 ceph_con_open(&req->r_osd->o_con,
983 CEPH_ENTITY_TYPE_OSD, o,
984 &osdc->osdmap->osd_addr[o]);
988 __remove_osd_from_lru(req->r_osd);
989 list_add(&req->r_osd_item, &req->r_osd->o_requests);
990 list_move(&req->r_req_lru_item, &osdc->req_unsent);
992 list_move(&req->r_req_lru_item, &osdc->req_notarget);
994 err = 1; /* osd or pg changed */
1001 * caller should hold map_sem (for read) and request_mutex
1003 static void __send_request(struct ceph_osd_client *osdc,
1004 struct ceph_osd_request *req)
1006 struct ceph_osd_request_head *reqhead;
1008 dout("send_request %p tid %llu to osd%d flags %d\n",
1009 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1011 reqhead = req->r_request->front.iov_base;
1012 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1013 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1014 reqhead->reassert_version = req->r_reassert_version;
1016 req->r_stamp = jiffies;
1017 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1019 ceph_msg_get(req->r_request); /* send consumes a ref */
1020 ceph_con_send(&req->r_osd->o_con, req->r_request);
1021 req->r_sent = req->r_osd->o_incarnation;
1025 * Send any requests in the queue (req_unsent).
1027 static void send_queued(struct ceph_osd_client *osdc)
1029 struct ceph_osd_request *req, *tmp;
1031 dout("send_queued\n");
1032 mutex_lock(&osdc->request_mutex);
1033 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1034 __send_request(osdc, req);
1036 mutex_unlock(&osdc->request_mutex);
1040 * Timeout callback, called every N seconds when 1 or more osd
1041 * requests has been active for more than N seconds. When this
1042 * happens, we ping all OSDs with requests who have timed out to
1043 * ensure any communications channel reset is detected. Reset the
1044 * request timeouts another N seconds in the future as we go.
1045 * Reschedule the timeout event another N seconds in future (unless
1046 * there are no open requests).
1048 static void handle_timeout(struct work_struct *work)
1050 struct ceph_osd_client *osdc =
1051 container_of(work, struct ceph_osd_client, timeout_work.work);
1052 struct ceph_osd_request *req;
1053 struct ceph_osd *osd;
1054 unsigned long keepalive =
1055 osdc->client->options->osd_keepalive_timeout * HZ;
1056 struct list_head slow_osds;
1058 down_read(&osdc->map_sem);
1060 ceph_monc_request_next_osdmap(&osdc->client->monc);
1062 mutex_lock(&osdc->request_mutex);
1065 * ping osds that are a bit slow. this ensures that if there
1066 * is a break in the TCP connection we will notice, and reopen
1067 * a connection with that osd (from the fault callback).
1069 INIT_LIST_HEAD(&slow_osds);
1070 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1071 if (time_before(jiffies, req->r_stamp + keepalive))
1076 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1077 req->r_tid, osd->o_osd);
1078 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1080 while (!list_empty(&slow_osds)) {
1081 osd = list_entry(slow_osds.next, struct ceph_osd,
1083 list_del_init(&osd->o_keepalive_item);
1084 ceph_con_keepalive(&osd->o_con);
1087 __schedule_osd_timeout(osdc);
1088 mutex_unlock(&osdc->request_mutex);
1090 up_read(&osdc->map_sem);
1093 static void handle_osds_timeout(struct work_struct *work)
1095 struct ceph_osd_client *osdc =
1096 container_of(work, struct ceph_osd_client,
1097 osds_timeout_work.work);
1098 unsigned long delay =
1099 osdc->client->options->osd_idle_ttl * HZ >> 2;
1101 dout("osds timeout\n");
1102 down_read(&osdc->map_sem);
1103 remove_old_osds(osdc);
1104 up_read(&osdc->map_sem);
1106 schedule_delayed_work(&osdc->osds_timeout_work,
1107 round_jiffies_relative(delay));
1110 static void complete_request(struct ceph_osd_request *req)
1112 if (req->r_safe_callback)
1113 req->r_safe_callback(req, NULL);
1114 complete_all(&req->r_safe_completion); /* fsync waiter */
1118 * handle osd op reply. either call the callback if it is specified,
1119 * or do the completion to wake up the waiting thread.
1121 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1122 struct ceph_connection *con)
1124 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1125 struct ceph_osd_request *req;
1127 int numops, object_len, flags;
1130 tid = le64_to_cpu(msg->hdr.tid);
1131 if (msg->front.iov_len < sizeof(*rhead))
1133 numops = le32_to_cpu(rhead->num_ops);
1134 object_len = le32_to_cpu(rhead->object_len);
1135 result = le32_to_cpu(rhead->result);
1136 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1137 numops * sizeof(struct ceph_osd_op))
1139 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1141 mutex_lock(&osdc->request_mutex);
1142 req = __lookup_request(osdc, tid);
1144 dout("handle_reply tid %llu dne\n", tid);
1145 mutex_unlock(&osdc->request_mutex);
1148 ceph_osdc_get_request(req);
1149 flags = le32_to_cpu(rhead->flags);
1152 * if this connection filled our message, drop our reference now, to
1153 * avoid a (safe but slower) revoke later.
1155 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1156 dout(" dropping con_filling_msg ref %p\n", con);
1157 req->r_con_filling_msg = NULL;
1161 if (!req->r_got_reply) {
1164 req->r_result = le32_to_cpu(rhead->result);
1165 bytes = le32_to_cpu(msg->hdr.data_len);
1166 dout("handle_reply result %d bytes %d\n", req->r_result,
1168 if (req->r_result == 0)
1169 req->r_result = bytes;
1171 /* in case this is a write and we need to replay, */
1172 req->r_reassert_version = rhead->reassert_version;
1174 req->r_got_reply = 1;
1175 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1176 dout("handle_reply tid %llu dup ack\n", tid);
1177 mutex_unlock(&osdc->request_mutex);
1181 dout("handle_reply tid %llu flags %d\n", tid, flags);
1183 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1184 __register_linger_request(osdc, req);
1186 /* either this is a read, or we got the safe response */
1188 (flags & CEPH_OSD_FLAG_ONDISK) ||
1189 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1190 __unregister_request(osdc, req);
1192 mutex_unlock(&osdc->request_mutex);
1194 if (req->r_callback)
1195 req->r_callback(req, msg);
1197 complete_all(&req->r_completion);
1199 if (flags & CEPH_OSD_FLAG_ONDISK)
1200 complete_request(req);
1203 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1204 ceph_osdc_put_request(req);
1208 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1209 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1210 (int)sizeof(*rhead));
1214 static void reset_changed_osds(struct ceph_osd_client *osdc)
1216 struct rb_node *p, *n;
1218 for (p = rb_first(&osdc->osds); p; p = n) {
1219 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1222 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1223 memcmp(&osd->o_con.peer_addr,
1224 ceph_osd_addr(osdc->osdmap,
1226 sizeof(struct ceph_entity_addr)) != 0)
1227 __reset_osd(osdc, osd);
1232 * Requeue requests whose mapping to an OSD has changed. If requests map to
1233 * no osd, request a new map.
1235 * Caller should hold map_sem for read.
1237 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1239 struct ceph_osd_request *req, *nreq;
1244 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1245 mutex_lock(&osdc->request_mutex);
1246 for (p = rb_first(&osdc->requests); p; ) {
1247 req = rb_entry(p, struct ceph_osd_request, r_node);
1251 * For linger requests that have not yet been
1252 * registered, move them to the linger list; they'll
1253 * be sent to the osd in the loop below. Unregister
1254 * the request before re-registering it as a linger
1255 * request to ensure the __map_request() below
1256 * will decide it needs to be sent.
1258 if (req->r_linger && list_empty(&req->r_linger_item)) {
1259 dout("%p tid %llu restart on osd%d\n",
1261 req->r_osd ? req->r_osd->o_osd : -1);
1262 __unregister_request(osdc, req);
1263 __register_linger_request(osdc, req);
1267 err = __map_request(osdc, req, force_resend);
1269 continue; /* error */
1270 if (req->r_osd == NULL) {
1271 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1272 needmap++; /* request a newer map */
1273 } else if (err > 0) {
1274 if (!req->r_linger) {
1275 dout("%p tid %llu requeued on osd%d\n", req,
1277 req->r_osd ? req->r_osd->o_osd : -1);
1278 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1283 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1285 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1287 err = __map_request(osdc, req, force_resend);
1288 dout("__map_request returned %d\n", err);
1290 continue; /* no change and no osd was specified */
1292 continue; /* hrm! */
1293 if (req->r_osd == NULL) {
1294 dout("tid %llu maps to no valid osd\n", req->r_tid);
1295 needmap++; /* request a newer map */
1299 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1300 req->r_osd ? req->r_osd->o_osd : -1);
1301 __register_request(osdc, req);
1302 __unregister_linger_request(osdc, req);
1304 mutex_unlock(&osdc->request_mutex);
1307 dout("%d requests for down osds, need new map\n", needmap);
1308 ceph_monc_request_next_osdmap(&osdc->client->monc);
1310 reset_changed_osds(osdc);
1315 * Process updated osd map.
1317 * The message contains any number of incremental and full maps, normally
1318 * indicating some sort of topology change in the cluster. Kick requests
1319 * off to different OSDs as needed.
1321 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1323 void *p, *end, *next;
1324 u32 nr_maps, maplen;
1326 struct ceph_osdmap *newmap = NULL, *oldmap;
1328 struct ceph_fsid fsid;
1330 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1331 p = msg->front.iov_base;
1332 end = p + msg->front.iov_len;
1335 ceph_decode_need(&p, end, sizeof(fsid), bad);
1336 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1337 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1340 down_write(&osdc->map_sem);
1342 /* incremental maps */
1343 ceph_decode_32_safe(&p, end, nr_maps, bad);
1344 dout(" %d inc maps\n", nr_maps);
1345 while (nr_maps > 0) {
1346 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1347 epoch = ceph_decode_32(&p);
1348 maplen = ceph_decode_32(&p);
1349 ceph_decode_need(&p, end, maplen, bad);
1351 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1352 dout("applying incremental map %u len %d\n",
1354 newmap = osdmap_apply_incremental(&p, next,
1356 &osdc->client->msgr);
1357 if (IS_ERR(newmap)) {
1358 err = PTR_ERR(newmap);
1362 if (newmap != osdc->osdmap) {
1363 ceph_osdmap_destroy(osdc->osdmap);
1364 osdc->osdmap = newmap;
1366 kick_requests(osdc, 0);
1368 dout("ignoring incremental map %u len %d\n",
1378 ceph_decode_32_safe(&p, end, nr_maps, bad);
1379 dout(" %d full maps\n", nr_maps);
1381 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1382 epoch = ceph_decode_32(&p);
1383 maplen = ceph_decode_32(&p);
1384 ceph_decode_need(&p, end, maplen, bad);
1386 dout("skipping non-latest full map %u len %d\n",
1388 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1389 dout("skipping full map %u len %d, "
1390 "older than our %u\n", epoch, maplen,
1391 osdc->osdmap->epoch);
1393 int skipped_map = 0;
1395 dout("taking full map %u len %d\n", epoch, maplen);
1396 newmap = osdmap_decode(&p, p+maplen);
1397 if (IS_ERR(newmap)) {
1398 err = PTR_ERR(newmap);
1402 oldmap = osdc->osdmap;
1403 osdc->osdmap = newmap;
1405 if (oldmap->epoch + 1 < newmap->epoch)
1407 ceph_osdmap_destroy(oldmap);
1409 kick_requests(osdc, skipped_map);
1416 downgrade_write(&osdc->map_sem);
1417 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1420 * subscribe to subsequent osdmap updates if full to ensure
1421 * we find out when we are no longer full and stop returning
1424 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1425 ceph_monc_request_next_osdmap(&osdc->client->monc);
1428 up_read(&osdc->map_sem);
1429 wake_up_all(&osdc->client->auth_wq);
1433 pr_err("osdc handle_map corrupt msg\n");
1435 up_write(&osdc->map_sem);
1440 * watch/notify callback event infrastructure
1442 * These callbacks are used both for watch and notify operations.
1444 static void __release_event(struct kref *kref)
1446 struct ceph_osd_event *event =
1447 container_of(kref, struct ceph_osd_event, kref);
1449 dout("__release_event %p\n", event);
1453 static void get_event(struct ceph_osd_event *event)
1455 kref_get(&event->kref);
1458 void ceph_osdc_put_event(struct ceph_osd_event *event)
1460 kref_put(&event->kref, __release_event);
1462 EXPORT_SYMBOL(ceph_osdc_put_event);
1464 static void __insert_event(struct ceph_osd_client *osdc,
1465 struct ceph_osd_event *new)
1467 struct rb_node **p = &osdc->event_tree.rb_node;
1468 struct rb_node *parent = NULL;
1469 struct ceph_osd_event *event = NULL;
1473 event = rb_entry(parent, struct ceph_osd_event, node);
1474 if (new->cookie < event->cookie)
1476 else if (new->cookie > event->cookie)
1477 p = &(*p)->rb_right;
1482 rb_link_node(&new->node, parent, p);
1483 rb_insert_color(&new->node, &osdc->event_tree);
1486 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1489 struct rb_node **p = &osdc->event_tree.rb_node;
1490 struct rb_node *parent = NULL;
1491 struct ceph_osd_event *event = NULL;
1495 event = rb_entry(parent, struct ceph_osd_event, node);
1496 if (cookie < event->cookie)
1498 else if (cookie > event->cookie)
1499 p = &(*p)->rb_right;
1506 static void __remove_event(struct ceph_osd_event *event)
1508 struct ceph_osd_client *osdc = event->osdc;
1510 if (!RB_EMPTY_NODE(&event->node)) {
1511 dout("__remove_event removed %p\n", event);
1512 rb_erase(&event->node, &osdc->event_tree);
1513 ceph_osdc_put_event(event);
1515 dout("__remove_event didn't remove %p\n", event);
1519 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1520 void (*event_cb)(u64, u64, u8, void *),
1521 int one_shot, void *data,
1522 struct ceph_osd_event **pevent)
1524 struct ceph_osd_event *event;
1526 event = kmalloc(sizeof(*event), GFP_NOIO);
1530 dout("create_event %p\n", event);
1531 event->cb = event_cb;
1532 event->one_shot = one_shot;
1535 INIT_LIST_HEAD(&event->osd_node);
1536 RB_CLEAR_NODE(&event->node);
1537 kref_init(&event->kref); /* one ref for us */
1538 kref_get(&event->kref); /* one ref for the caller */
1539 init_completion(&event->completion);
1541 spin_lock(&osdc->event_lock);
1542 event->cookie = ++osdc->event_count;
1543 __insert_event(osdc, event);
1544 spin_unlock(&osdc->event_lock);
1549 EXPORT_SYMBOL(ceph_osdc_create_event);
1551 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1553 struct ceph_osd_client *osdc = event->osdc;
1555 dout("cancel_event %p\n", event);
1556 spin_lock(&osdc->event_lock);
1557 __remove_event(event);
1558 spin_unlock(&osdc->event_lock);
1559 ceph_osdc_put_event(event); /* caller's */
1561 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1564 static void do_event_work(struct work_struct *work)
1566 struct ceph_osd_event_work *event_work =
1567 container_of(work, struct ceph_osd_event_work, work);
1568 struct ceph_osd_event *event = event_work->event;
1569 u64 ver = event_work->ver;
1570 u64 notify_id = event_work->notify_id;
1571 u8 opcode = event_work->opcode;
1573 dout("do_event_work completing %p\n", event);
1574 event->cb(ver, notify_id, opcode, event->data);
1575 complete(&event->completion);
1576 dout("do_event_work completed %p\n", event);
1577 ceph_osdc_put_event(event);
1583 * Process osd watch notifications
1585 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1589 u64 cookie, ver, notify_id;
1591 struct ceph_osd_event *event;
1592 struct ceph_osd_event_work *event_work;
1594 p = msg->front.iov_base;
1595 end = p + msg->front.iov_len;
1597 ceph_decode_8_safe(&p, end, proto_ver, bad);
1598 ceph_decode_8_safe(&p, end, opcode, bad);
1599 ceph_decode_64_safe(&p, end, cookie, bad);
1600 ceph_decode_64_safe(&p, end, ver, bad);
1601 ceph_decode_64_safe(&p, end, notify_id, bad);
1603 spin_lock(&osdc->event_lock);
1604 event = __find_event(osdc, cookie);
1607 if (event->one_shot)
1608 __remove_event(event);
1610 spin_unlock(&osdc->event_lock);
1611 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1612 cookie, ver, event);
1614 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1616 dout("ERROR: could not allocate event_work\n");
1619 INIT_WORK(&event_work->work, do_event_work);
1620 event_work->event = event;
1621 event_work->ver = ver;
1622 event_work->notify_id = notify_id;
1623 event_work->opcode = opcode;
1624 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1625 dout("WARNING: failed to queue notify event work\n");
1633 complete(&event->completion);
1634 ceph_osdc_put_event(event);
1638 pr_err("osdc handle_watch_notify corrupt msg\n");
1642 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1646 dout("wait_event %p\n", event);
1647 err = wait_for_completion_interruptible_timeout(&event->completion,
1649 ceph_osdc_put_event(event);
1652 dout("wait_event %p returns %d\n", event, err);
1655 EXPORT_SYMBOL(ceph_osdc_wait_event);
1658 * Register request, send initial attempt.
1660 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1661 struct ceph_osd_request *req,
1666 req->r_request->pages = req->r_pages;
1667 req->r_request->nr_pages = req->r_num_pages;
1669 req->r_request->bio = req->r_bio;
1671 req->r_request->trail = &req->r_trail;
1673 register_request(osdc, req);
1675 down_read(&osdc->map_sem);
1676 mutex_lock(&osdc->request_mutex);
1678 * a racing kick_requests() may have sent the message for us
1679 * while we dropped request_mutex above, so only send now if
1680 * the request still han't been touched yet.
1682 if (req->r_sent == 0) {
1683 rc = __map_request(osdc, req, 0);
1686 dout("osdc_start_request failed map, "
1687 " will retry %lld\n", req->r_tid);
1692 if (req->r_osd == NULL) {
1693 dout("send_request %p no up osds in pg\n", req);
1694 ceph_monc_request_next_osdmap(&osdc->client->monc);
1696 __send_request(osdc, req);
1702 mutex_unlock(&osdc->request_mutex);
1703 up_read(&osdc->map_sem);
1706 EXPORT_SYMBOL(ceph_osdc_start_request);
1709 * wait for a request to complete
1711 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1712 struct ceph_osd_request *req)
1716 rc = wait_for_completion_interruptible(&req->r_completion);
1718 mutex_lock(&osdc->request_mutex);
1719 __cancel_request(req);
1720 __unregister_request(osdc, req);
1721 mutex_unlock(&osdc->request_mutex);
1722 complete_request(req);
1723 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1727 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1728 return req->r_result;
1730 EXPORT_SYMBOL(ceph_osdc_wait_request);
1733 * sync - wait for all in-flight requests to flush. avoid starvation.
1735 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1737 struct ceph_osd_request *req;
1738 u64 last_tid, next_tid = 0;
1740 mutex_lock(&osdc->request_mutex);
1741 last_tid = osdc->last_tid;
1743 req = __lookup_request_ge(osdc, next_tid);
1746 if (req->r_tid > last_tid)
1749 next_tid = req->r_tid + 1;
1750 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1753 ceph_osdc_get_request(req);
1754 mutex_unlock(&osdc->request_mutex);
1755 dout("sync waiting on tid %llu (last is %llu)\n",
1756 req->r_tid, last_tid);
1757 wait_for_completion(&req->r_safe_completion);
1758 mutex_lock(&osdc->request_mutex);
1759 ceph_osdc_put_request(req);
1761 mutex_unlock(&osdc->request_mutex);
1762 dout("sync done (thru tid %llu)\n", last_tid);
1764 EXPORT_SYMBOL(ceph_osdc_sync);
1769 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1774 osdc->client = client;
1775 osdc->osdmap = NULL;
1776 init_rwsem(&osdc->map_sem);
1777 init_completion(&osdc->map_waiters);
1778 osdc->last_requested_map = 0;
1779 mutex_init(&osdc->request_mutex);
1781 osdc->osds = RB_ROOT;
1782 INIT_LIST_HEAD(&osdc->osd_lru);
1783 osdc->requests = RB_ROOT;
1784 INIT_LIST_HEAD(&osdc->req_lru);
1785 INIT_LIST_HEAD(&osdc->req_unsent);
1786 INIT_LIST_HEAD(&osdc->req_notarget);
1787 INIT_LIST_HEAD(&osdc->req_linger);
1788 osdc->num_requests = 0;
1789 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1790 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1791 spin_lock_init(&osdc->event_lock);
1792 osdc->event_tree = RB_ROOT;
1793 osdc->event_count = 0;
1795 schedule_delayed_work(&osdc->osds_timeout_work,
1796 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1799 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1800 sizeof(struct ceph_osd_request));
1801 if (!osdc->req_mempool)
1804 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1805 OSD_OP_FRONT_LEN, 10, true,
1809 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1810 OSD_OPREPLY_FRONT_LEN, 10, true,
1815 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1816 if (IS_ERR(osdc->notify_wq)) {
1817 err = PTR_ERR(osdc->notify_wq);
1818 osdc->notify_wq = NULL;
1824 ceph_msgpool_destroy(&osdc->msgpool_op);
1826 mempool_destroy(osdc->req_mempool);
1830 EXPORT_SYMBOL(ceph_osdc_init);
1832 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1834 flush_workqueue(osdc->notify_wq);
1835 destroy_workqueue(osdc->notify_wq);
1836 cancel_delayed_work_sync(&osdc->timeout_work);
1837 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1839 ceph_osdmap_destroy(osdc->osdmap);
1840 osdc->osdmap = NULL;
1842 remove_all_osds(osdc);
1843 mempool_destroy(osdc->req_mempool);
1844 ceph_msgpool_destroy(&osdc->msgpool_op);
1845 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1847 EXPORT_SYMBOL(ceph_osdc_stop);
1850 * Read some contiguous pages. If we cross a stripe boundary, shorten
1851 * *plen. Return number of bytes read, or error.
1853 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1854 struct ceph_vino vino, struct ceph_file_layout *layout,
1856 u32 truncate_seq, u64 truncate_size,
1857 struct page **pages, int num_pages, int page_align)
1859 struct ceph_osd_request *req;
1862 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1863 vino.snap, off, *plen);
1864 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1865 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1866 NULL, 0, truncate_seq, truncate_size, NULL,
1867 false, 1, page_align);
1869 return PTR_ERR(req);
1871 /* it may be a short read due to an object boundary */
1872 req->r_pages = pages;
1874 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1875 off, *plen, req->r_num_pages, page_align);
1877 rc = ceph_osdc_start_request(osdc, req, false);
1879 rc = ceph_osdc_wait_request(osdc, req);
1881 ceph_osdc_put_request(req);
1882 dout("readpages result %d\n", rc);
1885 EXPORT_SYMBOL(ceph_osdc_readpages);
1888 * do a synchronous write on N pages
1890 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1891 struct ceph_file_layout *layout,
1892 struct ceph_snap_context *snapc,
1894 u32 truncate_seq, u64 truncate_size,
1895 struct timespec *mtime,
1896 struct page **pages, int num_pages,
1897 int flags, int do_sync, bool nofail)
1899 struct ceph_osd_request *req;
1901 int page_align = off & ~PAGE_MASK;
1903 BUG_ON(vino.snap != CEPH_NOSNAP);
1904 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1906 flags | CEPH_OSD_FLAG_ONDISK |
1907 CEPH_OSD_FLAG_WRITE,
1909 truncate_seq, truncate_size, mtime,
1910 nofail, 1, page_align);
1912 return PTR_ERR(req);
1914 /* it may be a short write due to an object boundary */
1915 req->r_pages = pages;
1916 dout("writepages %llu~%llu (%d pages)\n", off, len,
1919 rc = ceph_osdc_start_request(osdc, req, nofail);
1921 rc = ceph_osdc_wait_request(osdc, req);
1923 ceph_osdc_put_request(req);
1926 dout("writepages result %d\n", rc);
1929 EXPORT_SYMBOL(ceph_osdc_writepages);
1932 * handle incoming message
1934 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1936 struct ceph_osd *osd = con->private;
1937 struct ceph_osd_client *osdc;
1938 int type = le16_to_cpu(msg->hdr.type);
1945 case CEPH_MSG_OSD_MAP:
1946 ceph_osdc_handle_map(osdc, msg);
1948 case CEPH_MSG_OSD_OPREPLY:
1949 handle_reply(osdc, msg, con);
1951 case CEPH_MSG_WATCH_NOTIFY:
1952 handle_watch_notify(osdc, msg);
1956 pr_err("received unknown message type %d %s\n", type,
1957 ceph_msg_type_name(type));
1964 * lookup and return message for incoming reply. set up reply message
1967 static struct ceph_msg *get_reply(struct ceph_connection *con,
1968 struct ceph_msg_header *hdr,
1971 struct ceph_osd *osd = con->private;
1972 struct ceph_osd_client *osdc = osd->o_osdc;
1974 struct ceph_osd_request *req;
1975 int front = le32_to_cpu(hdr->front_len);
1976 int data_len = le32_to_cpu(hdr->data_len);
1979 tid = le64_to_cpu(hdr->tid);
1980 mutex_lock(&osdc->request_mutex);
1981 req = __lookup_request(osdc, tid);
1985 dout("get_reply unknown tid %llu from osd%d\n", tid,
1990 if (req->r_con_filling_msg) {
1991 dout("%s revoking msg %p from old con %p\n", __func__,
1992 req->r_reply, req->r_con_filling_msg);
1993 ceph_msg_revoke_incoming(req->r_reply);
1994 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
1995 req->r_con_filling_msg = NULL;
1998 if (front > req->r_reply->front.iov_len) {
1999 pr_warning("get_reply front %d > preallocated %d\n",
2000 front, (int)req->r_reply->front.iov_len);
2001 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2004 ceph_msg_put(req->r_reply);
2007 m = ceph_msg_get(req->r_reply);
2010 int want = calc_pages_for(req->r_page_alignment, data_len);
2012 if (unlikely(req->r_num_pages < want)) {
2013 pr_warning("tid %lld reply has %d bytes %d pages, we"
2014 " had only %d pages ready\n", tid, data_len,
2015 want, req->r_num_pages);
2021 m->pages = req->r_pages;
2022 m->nr_pages = req->r_num_pages;
2023 m->page_alignment = req->r_page_alignment;
2025 m->bio = req->r_bio;
2029 req->r_con_filling_msg = con->ops->get(con);
2030 dout("get_reply tid %lld %p\n", tid, m);
2033 mutex_unlock(&osdc->request_mutex);
2038 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2039 struct ceph_msg_header *hdr,
2042 struct ceph_osd *osd = con->private;
2043 int type = le16_to_cpu(hdr->type);
2044 int front = le32_to_cpu(hdr->front_len);
2048 case CEPH_MSG_OSD_MAP:
2049 case CEPH_MSG_WATCH_NOTIFY:
2050 return ceph_msg_new(type, front, GFP_NOFS, false);
2051 case CEPH_MSG_OSD_OPREPLY:
2052 return get_reply(con, hdr, skip);
2054 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2062 * Wrappers to refcount containing ceph_osd struct
2064 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2066 struct ceph_osd *osd = con->private;
2072 static void put_osd_con(struct ceph_connection *con)
2074 struct ceph_osd *osd = con->private;
2082 * Note: returned pointer is the address of a structure that's
2083 * managed separately. Caller must *not* attempt to free it.
2085 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2086 int *proto, int force_new)
2088 struct ceph_osd *o = con->private;
2089 struct ceph_osd_client *osdc = o->o_osdc;
2090 struct ceph_auth_client *ac = osdc->client->monc.auth;
2091 struct ceph_auth_handshake *auth = &o->o_auth;
2093 if (force_new && auth->authorizer) {
2094 if (ac->ops && ac->ops->destroy_authorizer)
2095 ac->ops->destroy_authorizer(ac, auth->authorizer);
2096 auth->authorizer = NULL;
2098 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2099 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2102 return ERR_PTR(ret);
2104 *proto = ac->protocol;
2110 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2112 struct ceph_osd *o = con->private;
2113 struct ceph_osd_client *osdc = o->o_osdc;
2114 struct ceph_auth_client *ac = osdc->client->monc.auth;
2117 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2118 * XXX which do we do: succeed or fail?
2120 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2123 static int invalidate_authorizer(struct ceph_connection *con)
2125 struct ceph_osd *o = con->private;
2126 struct ceph_osd_client *osdc = o->o_osdc;
2127 struct ceph_auth_client *ac = osdc->client->monc.auth;
2129 if (ac->ops && ac->ops->invalidate_authorizer)
2130 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2132 return ceph_monc_validate_auth(&osdc->client->monc);
2135 static const struct ceph_connection_operations osd_con_ops = {
2138 .dispatch = dispatch,
2139 .get_authorizer = get_authorizer,
2140 .verify_authorizer_reply = verify_authorizer_reply,
2141 .invalidate_authorizer = invalidate_authorizer,
2142 .alloc_msg = alloc_msg,