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 msg %p from con %p\n", __func__,
141 req->r_reply, 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);
144 req->r_con_filling_msg = NULL;
147 ceph_msg_put(req->r_reply);
148 if (req->r_own_pages)
149 ceph_release_page_vector(req->r_pages,
151 ceph_put_snap_context(req->r_snapc);
152 ceph_pagelist_release(&req->r_trail);
154 mempool_free(req, req->r_osdc->req_mempool);
158 EXPORT_SYMBOL(ceph_osdc_release_request);
160 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
161 struct ceph_snap_context *snapc,
166 struct ceph_osd_request *req;
167 struct ceph_msg *msg;
168 size_t msg_size = sizeof(struct ceph_osd_request_head);
170 msg_size += num_op*sizeof(struct ceph_osd_op);
173 req = mempool_alloc(osdc->req_mempool, gfp_flags);
174 memset(req, 0, sizeof(*req));
176 req = kzalloc(sizeof(*req), gfp_flags);
182 req->r_mempool = use_mempool;
184 kref_init(&req->r_kref);
185 init_completion(&req->r_completion);
186 init_completion(&req->r_safe_completion);
187 RB_CLEAR_NODE(&req->r_node);
188 INIT_LIST_HEAD(&req->r_unsafe_item);
189 INIT_LIST_HEAD(&req->r_linger_item);
190 INIT_LIST_HEAD(&req->r_linger_osd);
191 INIT_LIST_HEAD(&req->r_req_lru_item);
192 INIT_LIST_HEAD(&req->r_osd_item);
194 /* create reply message */
196 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
198 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
199 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
201 ceph_osdc_put_request(req);
206 ceph_pagelist_init(&req->r_trail);
208 /* create request message; allow space for oid */
209 msg_size += MAX_OBJ_NAME_SIZE;
211 msg_size += sizeof(u64) * snapc->num_snaps;
213 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
215 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
217 ceph_osdc_put_request(req);
221 memset(msg->front.iov_base, 0, msg->front.iov_len);
223 req->r_request = msg;
227 EXPORT_SYMBOL(ceph_osdc_alloc_request);
229 static void osd_req_encode_op(struct ceph_osd_request *req,
230 struct ceph_osd_op *dst,
231 struct ceph_osd_req_op *src)
233 dst->op = cpu_to_le16(src->op);
236 case CEPH_OSD_OP_READ:
237 case CEPH_OSD_OP_WRITE:
239 cpu_to_le64(src->extent.offset);
241 cpu_to_le64(src->extent.length);
242 dst->extent.truncate_size =
243 cpu_to_le64(src->extent.truncate_size);
244 dst->extent.truncate_seq =
245 cpu_to_le32(src->extent.truncate_seq);
248 case CEPH_OSD_OP_GETXATTR:
249 case CEPH_OSD_OP_SETXATTR:
250 case CEPH_OSD_OP_CMPXATTR:
251 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
252 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
253 dst->xattr.cmp_op = src->xattr.cmp_op;
254 dst->xattr.cmp_mode = src->xattr.cmp_mode;
255 ceph_pagelist_append(&req->r_trail, src->xattr.name,
256 src->xattr.name_len);
257 ceph_pagelist_append(&req->r_trail, src->xattr.val,
258 src->xattr.value_len);
260 case CEPH_OSD_OP_CALL:
261 dst->cls.class_len = src->cls.class_len;
262 dst->cls.method_len = src->cls.method_len;
263 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
265 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
267 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
268 src->cls.method_len);
269 ceph_pagelist_append(&req->r_trail, src->cls.indata,
270 src->cls.indata_len);
272 case CEPH_OSD_OP_ROLLBACK:
273 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
275 case CEPH_OSD_OP_STARTSYNC:
277 case CEPH_OSD_OP_NOTIFY:
279 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
280 __le32 timeout = cpu_to_le32(src->watch.timeout);
282 ceph_pagelist_append(&req->r_trail,
283 &prot_ver, sizeof(prot_ver));
284 ceph_pagelist_append(&req->r_trail,
285 &timeout, sizeof(timeout));
287 case CEPH_OSD_OP_NOTIFY_ACK:
288 case CEPH_OSD_OP_WATCH:
289 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
290 dst->watch.ver = cpu_to_le64(src->watch.ver);
291 dst->watch.flag = src->watch.flag;
294 pr_err("unrecognized osd opcode %d\n", dst->op);
298 dst->payload_len = cpu_to_le32(src->payload_len);
302 * build new request AND message
305 void ceph_osdc_build_request(struct ceph_osd_request *req,
306 u64 off, u64 len, unsigned int num_op,
307 struct ceph_osd_req_op *src_ops,
308 struct ceph_snap_context *snapc, u64 snap_id,
309 struct timespec *mtime)
311 struct ceph_msg *msg = req->r_request;
312 struct ceph_osd_request_head *head;
313 struct ceph_osd_req_op *src_op;
314 struct ceph_osd_op *op;
316 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
317 int flags = req->r_flags;
321 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
323 head = msg->front.iov_base;
324 head->snapid = cpu_to_le64(snap_id);
325 op = (void *)(head + 1);
326 p = (void *)(op + num_op);
328 req->r_snapc = ceph_get_snap_context(snapc);
330 head->client_inc = cpu_to_le32(1); /* always, for now. */
331 head->flags = cpu_to_le32(flags);
332 if (flags & CEPH_OSD_FLAG_WRITE)
333 ceph_encode_timespec(&head->mtime, mtime);
334 BUG_ON(num_op > (unsigned int) ((u16) -1));
335 head->num_ops = cpu_to_le16(num_op);
338 head->object_len = cpu_to_le32(req->r_oid_len);
339 memcpy(p, req->r_oid, req->r_oid_len);
344 osd_req_encode_op(req, op++, src_op++);
346 data_len += req->r_trail.length;
349 head->snap_seq = cpu_to_le64(snapc->seq);
350 head->num_snaps = cpu_to_le32(snapc->num_snaps);
351 for (i = 0; i < snapc->num_snaps; i++) {
352 put_unaligned_le64(snapc->snaps[i], p);
357 if (flags & CEPH_OSD_FLAG_WRITE) {
358 req->r_request->hdr.data_off = cpu_to_le16(off);
359 req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
360 } else if (data_len) {
361 req->r_request->hdr.data_off = 0;
362 req->r_request->hdr.data_len = cpu_to_le32(data_len);
365 req->r_request->page_alignment = req->r_page_alignment;
367 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
368 msg_size = p - msg->front.iov_base;
369 msg->front.iov_len = msg_size;
370 msg->hdr.front_len = cpu_to_le32(msg_size);
373 EXPORT_SYMBOL(ceph_osdc_build_request);
376 * build new request AND message, calculate layout, and adjust file
379 * if the file was recently truncated, we include information about its
380 * old and new size so that the object can be updated appropriately. (we
381 * avoid synchronously deleting truncated objects because it's slow.)
383 * if @do_sync, include a 'startsync' command so that the osd will flush
386 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
387 struct ceph_file_layout *layout,
388 struct ceph_vino vino,
390 int opcode, int flags,
391 struct ceph_snap_context *snapc,
395 struct timespec *mtime,
399 struct ceph_osd_req_op ops[2];
400 struct ceph_osd_request *req;
401 unsigned int num_op = 1;
404 memset(&ops, 0, sizeof ops);
407 ops[0].extent.truncate_seq = truncate_seq;
408 ops[0].extent.truncate_size = truncate_size;
411 ops[1].op = CEPH_OSD_OP_STARTSYNC;
415 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
418 return ERR_PTR(-ENOMEM);
419 req->r_flags = flags;
421 /* calculate max write size */
422 r = calc_layout(vino, layout, off, plen, req, ops);
425 req->r_file_layout = *layout; /* keep a copy */
427 /* in case it differs from natural (file) alignment that
428 calc_layout filled in for us */
429 req->r_num_pages = calc_pages_for(page_align, *plen);
430 req->r_page_alignment = page_align;
432 ceph_osdc_build_request(req, off, *plen, num_op, ops,
433 snapc, vino.snap, mtime);
437 EXPORT_SYMBOL(ceph_osdc_new_request);
440 * We keep osd requests in an rbtree, sorted by ->r_tid.
442 static void __insert_request(struct ceph_osd_client *osdc,
443 struct ceph_osd_request *new)
445 struct rb_node **p = &osdc->requests.rb_node;
446 struct rb_node *parent = NULL;
447 struct ceph_osd_request *req = NULL;
451 req = rb_entry(parent, struct ceph_osd_request, r_node);
452 if (new->r_tid < req->r_tid)
454 else if (new->r_tid > req->r_tid)
460 rb_link_node(&new->r_node, parent, p);
461 rb_insert_color(&new->r_node, &osdc->requests);
464 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
467 struct ceph_osd_request *req;
468 struct rb_node *n = osdc->requests.rb_node;
471 req = rb_entry(n, struct ceph_osd_request, r_node);
472 if (tid < req->r_tid)
474 else if (tid > req->r_tid)
482 static struct ceph_osd_request *
483 __lookup_request_ge(struct ceph_osd_client *osdc,
486 struct ceph_osd_request *req;
487 struct rb_node *n = osdc->requests.rb_node;
490 req = rb_entry(n, struct ceph_osd_request, r_node);
491 if (tid < req->r_tid) {
495 } else if (tid > req->r_tid) {
505 * Resubmit requests pending on the given osd.
507 static void __kick_osd_requests(struct ceph_osd_client *osdc,
508 struct ceph_osd *osd)
510 struct ceph_osd_request *req, *nreq;
513 dout("__kick_osd_requests osd%d\n", osd->o_osd);
514 err = __reset_osd(osdc, osd);
518 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
519 list_move(&req->r_req_lru_item, &osdc->req_unsent);
520 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
523 req->r_flags |= CEPH_OSD_FLAG_RETRY;
526 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
529 * reregister request prior to unregistering linger so
530 * that r_osd is preserved.
532 BUG_ON(!list_empty(&req->r_req_lru_item));
533 __register_request(osdc, req);
534 list_add(&req->r_req_lru_item, &osdc->req_unsent);
535 list_add(&req->r_osd_item, &req->r_osd->o_requests);
536 __unregister_linger_request(osdc, req);
537 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
543 * If the osd connection drops, we need to resubmit all requests.
545 static void osd_reset(struct ceph_connection *con)
547 struct ceph_osd *osd = con->private;
548 struct ceph_osd_client *osdc;
552 dout("osd_reset osd%d\n", osd->o_osd);
554 down_read(&osdc->map_sem);
555 mutex_lock(&osdc->request_mutex);
556 __kick_osd_requests(osdc, osd);
557 mutex_unlock(&osdc->request_mutex);
559 up_read(&osdc->map_sem);
563 * Track open sessions with osds.
565 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
567 struct ceph_osd *osd;
569 osd = kzalloc(sizeof(*osd), GFP_NOFS);
573 atomic_set(&osd->o_ref, 1);
576 RB_CLEAR_NODE(&osd->o_node);
577 INIT_LIST_HEAD(&osd->o_requests);
578 INIT_LIST_HEAD(&osd->o_linger_requests);
579 INIT_LIST_HEAD(&osd->o_osd_lru);
580 osd->o_incarnation = 1;
582 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
584 INIT_LIST_HEAD(&osd->o_keepalive_item);
588 static struct ceph_osd *get_osd(struct ceph_osd *osd)
590 if (atomic_inc_not_zero(&osd->o_ref)) {
591 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
592 atomic_read(&osd->o_ref));
595 dout("get_osd %p FAIL\n", osd);
600 static void put_osd(struct ceph_osd *osd)
602 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
603 atomic_read(&osd->o_ref) - 1);
604 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
605 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
607 if (ac->ops && ac->ops->destroy_authorizer)
608 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
614 * remove an osd from our map
616 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
618 dout("__remove_osd %p\n", osd);
619 BUG_ON(!list_empty(&osd->o_requests));
620 rb_erase(&osd->o_node, &osdc->osds);
621 list_del_init(&osd->o_osd_lru);
622 ceph_con_close(&osd->o_con);
626 static void remove_all_osds(struct ceph_osd_client *osdc)
628 dout("%s %p\n", __func__, osdc);
629 mutex_lock(&osdc->request_mutex);
630 while (!RB_EMPTY_ROOT(&osdc->osds)) {
631 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
632 struct ceph_osd, o_node);
633 __remove_osd(osdc, osd);
635 mutex_unlock(&osdc->request_mutex);
638 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
639 struct ceph_osd *osd)
641 dout("__move_osd_to_lru %p\n", osd);
642 BUG_ON(!list_empty(&osd->o_osd_lru));
643 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
644 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
647 static void __remove_osd_from_lru(struct ceph_osd *osd)
649 dout("__remove_osd_from_lru %p\n", osd);
650 if (!list_empty(&osd->o_osd_lru))
651 list_del_init(&osd->o_osd_lru);
654 static void remove_old_osds(struct ceph_osd_client *osdc)
656 struct ceph_osd *osd, *nosd;
658 dout("__remove_old_osds %p\n", osdc);
659 mutex_lock(&osdc->request_mutex);
660 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
661 if (time_before(jiffies, osd->lru_ttl))
663 __remove_osd(osdc, osd);
665 mutex_unlock(&osdc->request_mutex);
671 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
673 struct ceph_entity_addr *peer_addr;
675 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
676 if (list_empty(&osd->o_requests) &&
677 list_empty(&osd->o_linger_requests)) {
678 __remove_osd(osdc, osd);
683 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
684 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
685 !ceph_con_opened(&osd->o_con)) {
686 struct ceph_osd_request *req;
688 dout(" osd addr hasn't changed and connection never opened,"
689 " letting msgr retry");
690 /* touch each r_stamp for handle_timeout()'s benfit */
691 list_for_each_entry(req, &osd->o_requests, r_osd_item)
692 req->r_stamp = jiffies;
697 ceph_con_close(&osd->o_con);
698 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
699 osd->o_incarnation++;
704 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
706 struct rb_node **p = &osdc->osds.rb_node;
707 struct rb_node *parent = NULL;
708 struct ceph_osd *osd = NULL;
710 dout("__insert_osd %p osd%d\n", new, new->o_osd);
713 osd = rb_entry(parent, struct ceph_osd, o_node);
714 if (new->o_osd < osd->o_osd)
716 else if (new->o_osd > osd->o_osd)
722 rb_link_node(&new->o_node, parent, p);
723 rb_insert_color(&new->o_node, &osdc->osds);
726 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
728 struct ceph_osd *osd;
729 struct rb_node *n = osdc->osds.rb_node;
732 osd = rb_entry(n, struct ceph_osd, o_node);
735 else if (o > osd->o_osd)
743 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
745 schedule_delayed_work(&osdc->timeout_work,
746 osdc->client->options->osd_keepalive_timeout * HZ);
749 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
751 cancel_delayed_work(&osdc->timeout_work);
755 * Register request, assign tid. If this is the first request, set up
758 static void __register_request(struct ceph_osd_client *osdc,
759 struct ceph_osd_request *req)
761 req->r_tid = ++osdc->last_tid;
762 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
763 dout("__register_request %p tid %lld\n", req, req->r_tid);
764 __insert_request(osdc, req);
765 ceph_osdc_get_request(req);
766 osdc->num_requests++;
767 if (osdc->num_requests == 1) {
768 dout(" first request, scheduling timeout\n");
769 __schedule_osd_timeout(osdc);
773 static void register_request(struct ceph_osd_client *osdc,
774 struct ceph_osd_request *req)
776 mutex_lock(&osdc->request_mutex);
777 __register_request(osdc, req);
778 mutex_unlock(&osdc->request_mutex);
782 * called under osdc->request_mutex
784 static void __unregister_request(struct ceph_osd_client *osdc,
785 struct ceph_osd_request *req)
787 if (RB_EMPTY_NODE(&req->r_node)) {
788 dout("__unregister_request %p tid %lld not registered\n",
793 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
794 rb_erase(&req->r_node, &osdc->requests);
795 osdc->num_requests--;
798 /* make sure the original request isn't in flight. */
799 ceph_msg_revoke(req->r_request);
801 list_del_init(&req->r_osd_item);
802 if (list_empty(&req->r_osd->o_requests) &&
803 list_empty(&req->r_osd->o_linger_requests)) {
804 dout("moving osd to %p lru\n", req->r_osd);
805 __move_osd_to_lru(osdc, req->r_osd);
807 if (list_empty(&req->r_linger_item))
811 list_del_init(&req->r_req_lru_item);
812 ceph_osdc_put_request(req);
814 if (osdc->num_requests == 0) {
815 dout(" no requests, canceling timeout\n");
816 __cancel_osd_timeout(osdc);
821 * Cancel a previously queued request message
823 static void __cancel_request(struct ceph_osd_request *req)
825 if (req->r_sent && req->r_osd) {
826 ceph_msg_revoke(req->r_request);
831 static void __register_linger_request(struct ceph_osd_client *osdc,
832 struct ceph_osd_request *req)
834 dout("__register_linger_request %p\n", req);
835 list_add_tail(&req->r_linger_item, &osdc->req_linger);
837 list_add_tail(&req->r_linger_osd,
838 &req->r_osd->o_linger_requests);
841 static void __unregister_linger_request(struct ceph_osd_client *osdc,
842 struct ceph_osd_request *req)
844 dout("__unregister_linger_request %p\n", req);
845 list_del_init(&req->r_linger_item);
847 list_del_init(&req->r_linger_osd);
849 if (list_empty(&req->r_osd->o_requests) &&
850 list_empty(&req->r_osd->o_linger_requests)) {
851 dout("moving osd to %p lru\n", req->r_osd);
852 __move_osd_to_lru(osdc, req->r_osd);
854 if (list_empty(&req->r_osd_item))
859 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
860 struct ceph_osd_request *req)
862 mutex_lock(&osdc->request_mutex);
864 __unregister_linger_request(osdc, req);
865 ceph_osdc_put_request(req);
867 mutex_unlock(&osdc->request_mutex);
869 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
871 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
872 struct ceph_osd_request *req)
874 if (!req->r_linger) {
875 dout("set_request_linger %p\n", req);
878 * caller is now responsible for calling
879 * unregister_linger_request
881 ceph_osdc_get_request(req);
884 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
887 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
888 * (as needed), and set the request r_osd appropriately. If there is
889 * no up osd, set r_osd to NULL. Move the request to the appropriate list
890 * (unsent, homeless) or leave on in-flight lru.
892 * Return 0 if unchanged, 1 if changed, or negative on error.
894 * Caller should hold map_sem for read and request_mutex.
896 static int __map_request(struct ceph_osd_client *osdc,
897 struct ceph_osd_request *req, int force_resend)
899 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
901 int acting[CEPH_PG_MAX_SIZE];
905 dout("map_request %p tid %lld\n", req, req->r_tid);
906 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
907 &req->r_file_layout, osdc->osdmap);
909 list_move(&req->r_req_lru_item, &osdc->req_notarget);
912 pgid = reqhead->layout.ol_pgid;
915 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
921 if ((!force_resend &&
922 req->r_osd && req->r_osd->o_osd == o &&
923 req->r_sent >= req->r_osd->o_incarnation &&
924 req->r_num_pg_osds == num &&
925 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
926 (req->r_osd == NULL && o == -1))
927 return 0; /* no change */
929 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
930 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
931 req->r_osd ? req->r_osd->o_osd : -1);
933 /* record full pg acting set */
934 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
935 req->r_num_pg_osds = num;
938 __cancel_request(req);
939 list_del_init(&req->r_osd_item);
943 req->r_osd = __lookup_osd(osdc, o);
944 if (!req->r_osd && o >= 0) {
946 req->r_osd = create_osd(osdc, o);
948 list_move(&req->r_req_lru_item, &osdc->req_notarget);
952 dout("map_request osd %p is osd%d\n", req->r_osd, o);
953 __insert_osd(osdc, req->r_osd);
955 ceph_con_open(&req->r_osd->o_con,
956 CEPH_ENTITY_TYPE_OSD, o,
957 &osdc->osdmap->osd_addr[o]);
961 __remove_osd_from_lru(req->r_osd);
962 list_add(&req->r_osd_item, &req->r_osd->o_requests);
963 list_move(&req->r_req_lru_item, &osdc->req_unsent);
965 list_move(&req->r_req_lru_item, &osdc->req_notarget);
967 err = 1; /* osd or pg changed */
974 * caller should hold map_sem (for read) and request_mutex
976 static void __send_request(struct ceph_osd_client *osdc,
977 struct ceph_osd_request *req)
979 struct ceph_osd_request_head *reqhead;
981 dout("send_request %p tid %llu to osd%d flags %d\n",
982 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
984 reqhead = req->r_request->front.iov_base;
985 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
986 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
987 reqhead->reassert_version = req->r_reassert_version;
989 req->r_stamp = jiffies;
990 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
992 ceph_msg_get(req->r_request); /* send consumes a ref */
993 ceph_con_send(&req->r_osd->o_con, req->r_request);
994 req->r_sent = req->r_osd->o_incarnation;
998 * Send any requests in the queue (req_unsent).
1000 static void send_queued(struct ceph_osd_client *osdc)
1002 struct ceph_osd_request *req, *tmp;
1004 dout("send_queued\n");
1005 mutex_lock(&osdc->request_mutex);
1006 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1007 __send_request(osdc, req);
1009 mutex_unlock(&osdc->request_mutex);
1013 * Timeout callback, called every N seconds when 1 or more osd
1014 * requests has been active for more than N seconds. When this
1015 * happens, we ping all OSDs with requests who have timed out to
1016 * ensure any communications channel reset is detected. Reset the
1017 * request timeouts another N seconds in the future as we go.
1018 * Reschedule the timeout event another N seconds in future (unless
1019 * there are no open requests).
1021 static void handle_timeout(struct work_struct *work)
1023 struct ceph_osd_client *osdc =
1024 container_of(work, struct ceph_osd_client, timeout_work.work);
1025 struct ceph_osd_request *req;
1026 struct ceph_osd *osd;
1027 unsigned long keepalive =
1028 osdc->client->options->osd_keepalive_timeout * HZ;
1029 struct list_head slow_osds;
1031 down_read(&osdc->map_sem);
1033 ceph_monc_request_next_osdmap(&osdc->client->monc);
1035 mutex_lock(&osdc->request_mutex);
1038 * ping osds that are a bit slow. this ensures that if there
1039 * is a break in the TCP connection we will notice, and reopen
1040 * a connection with that osd (from the fault callback).
1042 INIT_LIST_HEAD(&slow_osds);
1043 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1044 if (time_before(jiffies, req->r_stamp + keepalive))
1049 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1050 req->r_tid, osd->o_osd);
1051 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1053 while (!list_empty(&slow_osds)) {
1054 osd = list_entry(slow_osds.next, struct ceph_osd,
1056 list_del_init(&osd->o_keepalive_item);
1057 ceph_con_keepalive(&osd->o_con);
1060 __schedule_osd_timeout(osdc);
1061 mutex_unlock(&osdc->request_mutex);
1063 up_read(&osdc->map_sem);
1066 static void handle_osds_timeout(struct work_struct *work)
1068 struct ceph_osd_client *osdc =
1069 container_of(work, struct ceph_osd_client,
1070 osds_timeout_work.work);
1071 unsigned long delay =
1072 osdc->client->options->osd_idle_ttl * HZ >> 2;
1074 dout("osds timeout\n");
1075 down_read(&osdc->map_sem);
1076 remove_old_osds(osdc);
1077 up_read(&osdc->map_sem);
1079 schedule_delayed_work(&osdc->osds_timeout_work,
1080 round_jiffies_relative(delay));
1083 static void complete_request(struct ceph_osd_request *req)
1085 if (req->r_safe_callback)
1086 req->r_safe_callback(req, NULL);
1087 complete_all(&req->r_safe_completion); /* fsync waiter */
1091 * handle osd op reply. either call the callback if it is specified,
1092 * or do the completion to wake up the waiting thread.
1094 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1095 struct ceph_connection *con)
1097 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1098 struct ceph_osd_request *req;
1100 int numops, object_len, flags;
1103 tid = le64_to_cpu(msg->hdr.tid);
1104 if (msg->front.iov_len < sizeof(*rhead))
1106 numops = le32_to_cpu(rhead->num_ops);
1107 object_len = le32_to_cpu(rhead->object_len);
1108 result = le32_to_cpu(rhead->result);
1109 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1110 numops * sizeof(struct ceph_osd_op))
1112 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1114 mutex_lock(&osdc->request_mutex);
1115 req = __lookup_request(osdc, tid);
1117 dout("handle_reply tid %llu dne\n", tid);
1118 mutex_unlock(&osdc->request_mutex);
1121 ceph_osdc_get_request(req);
1122 flags = le32_to_cpu(rhead->flags);
1125 * if this connection filled our message, drop our reference now, to
1126 * avoid a (safe but slower) revoke later.
1128 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1129 dout(" dropping con_filling_msg ref %p\n", con);
1130 req->r_con_filling_msg = NULL;
1134 if (!req->r_got_reply) {
1137 req->r_result = le32_to_cpu(rhead->result);
1138 bytes = le32_to_cpu(msg->hdr.data_len);
1139 dout("handle_reply result %d bytes %d\n", req->r_result,
1141 if (req->r_result == 0)
1142 req->r_result = bytes;
1144 /* in case this is a write and we need to replay, */
1145 req->r_reassert_version = rhead->reassert_version;
1147 req->r_got_reply = 1;
1148 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1149 dout("handle_reply tid %llu dup ack\n", tid);
1150 mutex_unlock(&osdc->request_mutex);
1154 dout("handle_reply tid %llu flags %d\n", tid, flags);
1156 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1157 __register_linger_request(osdc, req);
1159 /* either this is a read, or we got the safe response */
1161 (flags & CEPH_OSD_FLAG_ONDISK) ||
1162 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1163 __unregister_request(osdc, req);
1165 mutex_unlock(&osdc->request_mutex);
1167 if (req->r_callback)
1168 req->r_callback(req, msg);
1170 complete_all(&req->r_completion);
1172 if (flags & CEPH_OSD_FLAG_ONDISK)
1173 complete_request(req);
1176 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1177 ceph_osdc_put_request(req);
1181 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1182 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1183 (int)sizeof(*rhead));
1187 static void reset_changed_osds(struct ceph_osd_client *osdc)
1189 struct rb_node *p, *n;
1191 for (p = rb_first(&osdc->osds); p; p = n) {
1192 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1195 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1196 memcmp(&osd->o_con.peer_addr,
1197 ceph_osd_addr(osdc->osdmap,
1199 sizeof(struct ceph_entity_addr)) != 0)
1200 __reset_osd(osdc, osd);
1205 * Requeue requests whose mapping to an OSD has changed. If requests map to
1206 * no osd, request a new map.
1208 * Caller should hold map_sem for read.
1210 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1212 struct ceph_osd_request *req, *nreq;
1217 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1218 mutex_lock(&osdc->request_mutex);
1219 for (p = rb_first(&osdc->requests); p; ) {
1220 req = rb_entry(p, struct ceph_osd_request, r_node);
1224 * For linger requests that have not yet been
1225 * registered, move them to the linger list; they'll
1226 * be sent to the osd in the loop below. Unregister
1227 * the request before re-registering it as a linger
1228 * request to ensure the __map_request() below
1229 * will decide it needs to be sent.
1231 if (req->r_linger && list_empty(&req->r_linger_item)) {
1232 dout("%p tid %llu restart on osd%d\n",
1234 req->r_osd ? req->r_osd->o_osd : -1);
1235 __unregister_request(osdc, req);
1236 __register_linger_request(osdc, req);
1240 err = __map_request(osdc, req, force_resend);
1242 continue; /* error */
1243 if (req->r_osd == NULL) {
1244 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1245 needmap++; /* request a newer map */
1246 } else if (err > 0) {
1247 if (!req->r_linger) {
1248 dout("%p tid %llu requeued on osd%d\n", req,
1250 req->r_osd ? req->r_osd->o_osd : -1);
1251 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1256 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1258 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1260 err = __map_request(osdc, req, force_resend);
1261 dout("__map_request returned %d\n", err);
1263 continue; /* no change and no osd was specified */
1265 continue; /* hrm! */
1266 if (req->r_osd == NULL) {
1267 dout("tid %llu maps to no valid osd\n", req->r_tid);
1268 needmap++; /* request a newer map */
1272 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1273 req->r_osd ? req->r_osd->o_osd : -1);
1274 __register_request(osdc, req);
1275 __unregister_linger_request(osdc, req);
1277 mutex_unlock(&osdc->request_mutex);
1280 dout("%d requests for down osds, need new map\n", needmap);
1281 ceph_monc_request_next_osdmap(&osdc->client->monc);
1283 reset_changed_osds(osdc);
1288 * Process updated osd map.
1290 * The message contains any number of incremental and full maps, normally
1291 * indicating some sort of topology change in the cluster. Kick requests
1292 * off to different OSDs as needed.
1294 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1296 void *p, *end, *next;
1297 u32 nr_maps, maplen;
1299 struct ceph_osdmap *newmap = NULL, *oldmap;
1301 struct ceph_fsid fsid;
1303 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1304 p = msg->front.iov_base;
1305 end = p + msg->front.iov_len;
1308 ceph_decode_need(&p, end, sizeof(fsid), bad);
1309 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1310 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1313 down_write(&osdc->map_sem);
1315 /* incremental maps */
1316 ceph_decode_32_safe(&p, end, nr_maps, bad);
1317 dout(" %d inc maps\n", nr_maps);
1318 while (nr_maps > 0) {
1319 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1320 epoch = ceph_decode_32(&p);
1321 maplen = ceph_decode_32(&p);
1322 ceph_decode_need(&p, end, maplen, bad);
1324 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1325 dout("applying incremental map %u len %d\n",
1327 newmap = osdmap_apply_incremental(&p, next,
1329 &osdc->client->msgr);
1330 if (IS_ERR(newmap)) {
1331 err = PTR_ERR(newmap);
1335 if (newmap != osdc->osdmap) {
1336 ceph_osdmap_destroy(osdc->osdmap);
1337 osdc->osdmap = newmap;
1339 kick_requests(osdc, 0);
1341 dout("ignoring incremental map %u len %d\n",
1351 ceph_decode_32_safe(&p, end, nr_maps, bad);
1352 dout(" %d full maps\n", nr_maps);
1354 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1355 epoch = ceph_decode_32(&p);
1356 maplen = ceph_decode_32(&p);
1357 ceph_decode_need(&p, end, maplen, bad);
1359 dout("skipping non-latest full map %u len %d\n",
1361 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1362 dout("skipping full map %u len %d, "
1363 "older than our %u\n", epoch, maplen,
1364 osdc->osdmap->epoch);
1366 int skipped_map = 0;
1368 dout("taking full map %u len %d\n", epoch, maplen);
1369 newmap = osdmap_decode(&p, p+maplen);
1370 if (IS_ERR(newmap)) {
1371 err = PTR_ERR(newmap);
1375 oldmap = osdc->osdmap;
1376 osdc->osdmap = newmap;
1378 if (oldmap->epoch + 1 < newmap->epoch)
1380 ceph_osdmap_destroy(oldmap);
1382 kick_requests(osdc, skipped_map);
1389 downgrade_write(&osdc->map_sem);
1390 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1393 * subscribe to subsequent osdmap updates if full to ensure
1394 * we find out when we are no longer full and stop returning
1397 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1398 ceph_monc_request_next_osdmap(&osdc->client->monc);
1401 up_read(&osdc->map_sem);
1402 wake_up_all(&osdc->client->auth_wq);
1406 pr_err("osdc handle_map corrupt msg\n");
1408 up_write(&osdc->map_sem);
1413 * watch/notify callback event infrastructure
1415 * These callbacks are used both for watch and notify operations.
1417 static void __release_event(struct kref *kref)
1419 struct ceph_osd_event *event =
1420 container_of(kref, struct ceph_osd_event, kref);
1422 dout("__release_event %p\n", event);
1426 static void get_event(struct ceph_osd_event *event)
1428 kref_get(&event->kref);
1431 void ceph_osdc_put_event(struct ceph_osd_event *event)
1433 kref_put(&event->kref, __release_event);
1435 EXPORT_SYMBOL(ceph_osdc_put_event);
1437 static void __insert_event(struct ceph_osd_client *osdc,
1438 struct ceph_osd_event *new)
1440 struct rb_node **p = &osdc->event_tree.rb_node;
1441 struct rb_node *parent = NULL;
1442 struct ceph_osd_event *event = NULL;
1446 event = rb_entry(parent, struct ceph_osd_event, node);
1447 if (new->cookie < event->cookie)
1449 else if (new->cookie > event->cookie)
1450 p = &(*p)->rb_right;
1455 rb_link_node(&new->node, parent, p);
1456 rb_insert_color(&new->node, &osdc->event_tree);
1459 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1462 struct rb_node **p = &osdc->event_tree.rb_node;
1463 struct rb_node *parent = NULL;
1464 struct ceph_osd_event *event = NULL;
1468 event = rb_entry(parent, struct ceph_osd_event, node);
1469 if (cookie < event->cookie)
1471 else if (cookie > event->cookie)
1472 p = &(*p)->rb_right;
1479 static void __remove_event(struct ceph_osd_event *event)
1481 struct ceph_osd_client *osdc = event->osdc;
1483 if (!RB_EMPTY_NODE(&event->node)) {
1484 dout("__remove_event removed %p\n", event);
1485 rb_erase(&event->node, &osdc->event_tree);
1486 ceph_osdc_put_event(event);
1488 dout("__remove_event didn't remove %p\n", event);
1492 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1493 void (*event_cb)(u64, u64, u8, void *),
1494 int one_shot, void *data,
1495 struct ceph_osd_event **pevent)
1497 struct ceph_osd_event *event;
1499 event = kmalloc(sizeof(*event), GFP_NOIO);
1503 dout("create_event %p\n", event);
1504 event->cb = event_cb;
1505 event->one_shot = one_shot;
1508 INIT_LIST_HEAD(&event->osd_node);
1509 RB_CLEAR_NODE(&event->node);
1510 kref_init(&event->kref); /* one ref for us */
1511 kref_get(&event->kref); /* one ref for the caller */
1512 init_completion(&event->completion);
1514 spin_lock(&osdc->event_lock);
1515 event->cookie = ++osdc->event_count;
1516 __insert_event(osdc, event);
1517 spin_unlock(&osdc->event_lock);
1522 EXPORT_SYMBOL(ceph_osdc_create_event);
1524 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1526 struct ceph_osd_client *osdc = event->osdc;
1528 dout("cancel_event %p\n", event);
1529 spin_lock(&osdc->event_lock);
1530 __remove_event(event);
1531 spin_unlock(&osdc->event_lock);
1532 ceph_osdc_put_event(event); /* caller's */
1534 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1537 static void do_event_work(struct work_struct *work)
1539 struct ceph_osd_event_work *event_work =
1540 container_of(work, struct ceph_osd_event_work, work);
1541 struct ceph_osd_event *event = event_work->event;
1542 u64 ver = event_work->ver;
1543 u64 notify_id = event_work->notify_id;
1544 u8 opcode = event_work->opcode;
1546 dout("do_event_work completing %p\n", event);
1547 event->cb(ver, notify_id, opcode, event->data);
1548 complete(&event->completion);
1549 dout("do_event_work completed %p\n", event);
1550 ceph_osdc_put_event(event);
1556 * Process osd watch notifications
1558 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1562 u64 cookie, ver, notify_id;
1564 struct ceph_osd_event *event;
1565 struct ceph_osd_event_work *event_work;
1567 p = msg->front.iov_base;
1568 end = p + msg->front.iov_len;
1570 ceph_decode_8_safe(&p, end, proto_ver, bad);
1571 ceph_decode_8_safe(&p, end, opcode, bad);
1572 ceph_decode_64_safe(&p, end, cookie, bad);
1573 ceph_decode_64_safe(&p, end, ver, bad);
1574 ceph_decode_64_safe(&p, end, notify_id, bad);
1576 spin_lock(&osdc->event_lock);
1577 event = __find_event(osdc, cookie);
1580 if (event->one_shot)
1581 __remove_event(event);
1583 spin_unlock(&osdc->event_lock);
1584 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1585 cookie, ver, event);
1587 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1589 dout("ERROR: could not allocate event_work\n");
1592 INIT_WORK(&event_work->work, do_event_work);
1593 event_work->event = event;
1594 event_work->ver = ver;
1595 event_work->notify_id = notify_id;
1596 event_work->opcode = opcode;
1597 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1598 dout("WARNING: failed to queue notify event work\n");
1606 complete(&event->completion);
1607 ceph_osdc_put_event(event);
1611 pr_err("osdc handle_watch_notify corrupt msg\n");
1615 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1619 dout("wait_event %p\n", event);
1620 err = wait_for_completion_interruptible_timeout(&event->completion,
1622 ceph_osdc_put_event(event);
1625 dout("wait_event %p returns %d\n", event, err);
1628 EXPORT_SYMBOL(ceph_osdc_wait_event);
1631 * Register request, send initial attempt.
1633 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1634 struct ceph_osd_request *req,
1639 req->r_request->pages = req->r_pages;
1640 req->r_request->nr_pages = req->r_num_pages;
1642 req->r_request->bio = req->r_bio;
1644 req->r_request->trail = &req->r_trail;
1646 register_request(osdc, req);
1648 down_read(&osdc->map_sem);
1649 mutex_lock(&osdc->request_mutex);
1651 * a racing kick_requests() may have sent the message for us
1652 * while we dropped request_mutex above, so only send now if
1653 * the request still han't been touched yet.
1655 if (req->r_sent == 0) {
1656 rc = __map_request(osdc, req, 0);
1659 dout("osdc_start_request failed map, "
1660 " will retry %lld\n", req->r_tid);
1665 if (req->r_osd == NULL) {
1666 dout("send_request %p no up osds in pg\n", req);
1667 ceph_monc_request_next_osdmap(&osdc->client->monc);
1669 __send_request(osdc, req);
1675 mutex_unlock(&osdc->request_mutex);
1676 up_read(&osdc->map_sem);
1679 EXPORT_SYMBOL(ceph_osdc_start_request);
1682 * wait for a request to complete
1684 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1685 struct ceph_osd_request *req)
1689 rc = wait_for_completion_interruptible(&req->r_completion);
1691 mutex_lock(&osdc->request_mutex);
1692 __cancel_request(req);
1693 __unregister_request(osdc, req);
1694 mutex_unlock(&osdc->request_mutex);
1695 complete_request(req);
1696 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1700 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1701 return req->r_result;
1703 EXPORT_SYMBOL(ceph_osdc_wait_request);
1706 * sync - wait for all in-flight requests to flush. avoid starvation.
1708 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1710 struct ceph_osd_request *req;
1711 u64 last_tid, next_tid = 0;
1713 mutex_lock(&osdc->request_mutex);
1714 last_tid = osdc->last_tid;
1716 req = __lookup_request_ge(osdc, next_tid);
1719 if (req->r_tid > last_tid)
1722 next_tid = req->r_tid + 1;
1723 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1726 ceph_osdc_get_request(req);
1727 mutex_unlock(&osdc->request_mutex);
1728 dout("sync waiting on tid %llu (last is %llu)\n",
1729 req->r_tid, last_tid);
1730 wait_for_completion(&req->r_safe_completion);
1731 mutex_lock(&osdc->request_mutex);
1732 ceph_osdc_put_request(req);
1734 mutex_unlock(&osdc->request_mutex);
1735 dout("sync done (thru tid %llu)\n", last_tid);
1737 EXPORT_SYMBOL(ceph_osdc_sync);
1742 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1747 osdc->client = client;
1748 osdc->osdmap = NULL;
1749 init_rwsem(&osdc->map_sem);
1750 init_completion(&osdc->map_waiters);
1751 osdc->last_requested_map = 0;
1752 mutex_init(&osdc->request_mutex);
1754 osdc->osds = RB_ROOT;
1755 INIT_LIST_HEAD(&osdc->osd_lru);
1756 osdc->requests = RB_ROOT;
1757 INIT_LIST_HEAD(&osdc->req_lru);
1758 INIT_LIST_HEAD(&osdc->req_unsent);
1759 INIT_LIST_HEAD(&osdc->req_notarget);
1760 INIT_LIST_HEAD(&osdc->req_linger);
1761 osdc->num_requests = 0;
1762 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1763 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1764 spin_lock_init(&osdc->event_lock);
1765 osdc->event_tree = RB_ROOT;
1766 osdc->event_count = 0;
1768 schedule_delayed_work(&osdc->osds_timeout_work,
1769 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1772 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1773 sizeof(struct ceph_osd_request));
1774 if (!osdc->req_mempool)
1777 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1778 OSD_OP_FRONT_LEN, 10, true,
1782 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1783 OSD_OPREPLY_FRONT_LEN, 10, true,
1788 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1789 if (IS_ERR(osdc->notify_wq)) {
1790 err = PTR_ERR(osdc->notify_wq);
1791 osdc->notify_wq = NULL;
1797 ceph_msgpool_destroy(&osdc->msgpool_op);
1799 mempool_destroy(osdc->req_mempool);
1803 EXPORT_SYMBOL(ceph_osdc_init);
1805 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1807 flush_workqueue(osdc->notify_wq);
1808 destroy_workqueue(osdc->notify_wq);
1809 cancel_delayed_work_sync(&osdc->timeout_work);
1810 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1812 ceph_osdmap_destroy(osdc->osdmap);
1813 osdc->osdmap = NULL;
1815 remove_all_osds(osdc);
1816 mempool_destroy(osdc->req_mempool);
1817 ceph_msgpool_destroy(&osdc->msgpool_op);
1818 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1820 EXPORT_SYMBOL(ceph_osdc_stop);
1823 * Read some contiguous pages. If we cross a stripe boundary, shorten
1824 * *plen. Return number of bytes read, or error.
1826 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1827 struct ceph_vino vino, struct ceph_file_layout *layout,
1829 u32 truncate_seq, u64 truncate_size,
1830 struct page **pages, int num_pages, int page_align)
1832 struct ceph_osd_request *req;
1835 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1836 vino.snap, off, *plen);
1837 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1838 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1839 NULL, 0, truncate_seq, truncate_size, NULL,
1842 return PTR_ERR(req);
1844 /* it may be a short read due to an object boundary */
1845 req->r_pages = pages;
1847 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1848 off, *plen, req->r_num_pages, page_align);
1850 rc = ceph_osdc_start_request(osdc, req, false);
1852 rc = ceph_osdc_wait_request(osdc, req);
1854 ceph_osdc_put_request(req);
1855 dout("readpages result %d\n", rc);
1858 EXPORT_SYMBOL(ceph_osdc_readpages);
1861 * do a synchronous write on N pages
1863 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1864 struct ceph_file_layout *layout,
1865 struct ceph_snap_context *snapc,
1867 u32 truncate_seq, u64 truncate_size,
1868 struct timespec *mtime,
1869 struct page **pages, int num_pages)
1871 struct ceph_osd_request *req;
1873 int page_align = off & ~PAGE_MASK;
1875 BUG_ON(vino.snap != CEPH_NOSNAP);
1876 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1878 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1880 truncate_seq, truncate_size, mtime,
1883 return PTR_ERR(req);
1885 /* it may be a short write due to an object boundary */
1886 req->r_pages = pages;
1887 dout("writepages %llu~%llu (%d pages)\n", off, len,
1890 rc = ceph_osdc_start_request(osdc, req, true);
1892 rc = ceph_osdc_wait_request(osdc, req);
1894 ceph_osdc_put_request(req);
1897 dout("writepages result %d\n", rc);
1900 EXPORT_SYMBOL(ceph_osdc_writepages);
1903 * handle incoming message
1905 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1907 struct ceph_osd *osd = con->private;
1908 struct ceph_osd_client *osdc;
1909 int type = le16_to_cpu(msg->hdr.type);
1916 case CEPH_MSG_OSD_MAP:
1917 ceph_osdc_handle_map(osdc, msg);
1919 case CEPH_MSG_OSD_OPREPLY:
1920 handle_reply(osdc, msg, con);
1922 case CEPH_MSG_WATCH_NOTIFY:
1923 handle_watch_notify(osdc, msg);
1927 pr_err("received unknown message type %d %s\n", type,
1928 ceph_msg_type_name(type));
1935 * lookup and return message for incoming reply. set up reply message
1938 static struct ceph_msg *get_reply(struct ceph_connection *con,
1939 struct ceph_msg_header *hdr,
1942 struct ceph_osd *osd = con->private;
1943 struct ceph_osd_client *osdc = osd->o_osdc;
1945 struct ceph_osd_request *req;
1946 int front = le32_to_cpu(hdr->front_len);
1947 int data_len = le32_to_cpu(hdr->data_len);
1950 tid = le64_to_cpu(hdr->tid);
1951 mutex_lock(&osdc->request_mutex);
1952 req = __lookup_request(osdc, tid);
1956 dout("get_reply unknown tid %llu from osd%d\n", tid,
1961 if (req->r_con_filling_msg) {
1962 dout("%s revoking msg %p from old con %p\n", __func__,
1963 req->r_reply, req->r_con_filling_msg);
1964 ceph_msg_revoke_incoming(req->r_reply);
1965 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
1966 req->r_con_filling_msg = NULL;
1969 if (front > req->r_reply->front.iov_len) {
1970 pr_warning("get_reply front %d > preallocated %d\n",
1971 front, (int)req->r_reply->front.iov_len);
1972 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
1975 ceph_msg_put(req->r_reply);
1978 m = ceph_msg_get(req->r_reply);
1981 int want = calc_pages_for(req->r_page_alignment, data_len);
1983 if (req->r_pages && unlikely(req->r_num_pages < want)) {
1984 pr_warning("tid %lld reply has %d bytes %d pages, we"
1985 " had only %d pages ready\n", tid, data_len,
1986 want, req->r_num_pages);
1992 m->pages = req->r_pages;
1993 m->nr_pages = req->r_num_pages;
1994 m->page_alignment = req->r_page_alignment;
1996 m->bio = req->r_bio;
2000 req->r_con_filling_msg = con->ops->get(con);
2001 dout("get_reply tid %lld %p\n", tid, m);
2004 mutex_unlock(&osdc->request_mutex);
2009 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2010 struct ceph_msg_header *hdr,
2013 struct ceph_osd *osd = con->private;
2014 int type = le16_to_cpu(hdr->type);
2015 int front = le32_to_cpu(hdr->front_len);
2019 case CEPH_MSG_OSD_MAP:
2020 case CEPH_MSG_WATCH_NOTIFY:
2021 return ceph_msg_new(type, front, GFP_NOFS, false);
2022 case CEPH_MSG_OSD_OPREPLY:
2023 return get_reply(con, hdr, skip);
2025 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2033 * Wrappers to refcount containing ceph_osd struct
2035 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2037 struct ceph_osd *osd = con->private;
2043 static void put_osd_con(struct ceph_connection *con)
2045 struct ceph_osd *osd = con->private;
2053 * Note: returned pointer is the address of a structure that's
2054 * managed separately. Caller must *not* attempt to free it.
2056 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2057 int *proto, int force_new)
2059 struct ceph_osd *o = con->private;
2060 struct ceph_osd_client *osdc = o->o_osdc;
2061 struct ceph_auth_client *ac = osdc->client->monc.auth;
2062 struct ceph_auth_handshake *auth = &o->o_auth;
2064 if (force_new && auth->authorizer) {
2065 if (ac->ops && ac->ops->destroy_authorizer)
2066 ac->ops->destroy_authorizer(ac, auth->authorizer);
2067 auth->authorizer = NULL;
2069 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2070 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2073 return ERR_PTR(ret);
2075 *proto = ac->protocol;
2081 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2083 struct ceph_osd *o = con->private;
2084 struct ceph_osd_client *osdc = o->o_osdc;
2085 struct ceph_auth_client *ac = osdc->client->monc.auth;
2088 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2089 * XXX which do we do: succeed or fail?
2091 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2094 static int invalidate_authorizer(struct ceph_connection *con)
2096 struct ceph_osd *o = con->private;
2097 struct ceph_osd_client *osdc = o->o_osdc;
2098 struct ceph_auth_client *ac = osdc->client->monc.auth;
2100 if (ac->ops && ac->ops->invalidate_authorizer)
2101 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2103 return ceph_monc_validate_auth(&osdc->client->monc);
2106 static const struct ceph_connection_operations osd_con_ops = {
2109 .dispatch = dispatch,
2110 .get_authorizer = get_authorizer,
2111 .verify_authorizer_reply = verify_authorizer_reply,
2112 .invalidate_authorizer = invalidate_authorizer,
2113 .alloc_msg = alloc_msg,