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,
150 ceph_put_snap_context(req->r_snapc);
151 ceph_pagelist_release(&req->r_trail);
153 mempool_free(req, req->r_osdc->req_mempool);
157 EXPORT_SYMBOL(ceph_osdc_release_request);
159 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
160 struct ceph_snap_context *snapc,
165 struct ceph_osd_request *req;
166 struct ceph_msg *msg;
167 size_t msg_size = sizeof(struct ceph_osd_request_head);
169 msg_size += num_op*sizeof(struct ceph_osd_op);
172 req = mempool_alloc(osdc->req_mempool, gfp_flags);
173 memset(req, 0, sizeof(*req));
175 req = kzalloc(sizeof(*req), gfp_flags);
181 req->r_mempool = use_mempool;
183 kref_init(&req->r_kref);
184 init_completion(&req->r_completion);
185 init_completion(&req->r_safe_completion);
186 RB_CLEAR_NODE(&req->r_node);
187 INIT_LIST_HEAD(&req->r_unsafe_item);
188 INIT_LIST_HEAD(&req->r_linger_item);
189 INIT_LIST_HEAD(&req->r_linger_osd);
190 INIT_LIST_HEAD(&req->r_req_lru_item);
191 INIT_LIST_HEAD(&req->r_osd_item);
193 /* create reply message */
195 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
197 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
198 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
200 ceph_osdc_put_request(req);
205 ceph_pagelist_init(&req->r_trail);
207 /* create request message; allow space for oid */
208 msg_size += MAX_OBJ_NAME_SIZE;
210 msg_size += sizeof(u64) * snapc->num_snaps;
212 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
214 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
216 ceph_osdc_put_request(req);
220 memset(msg->front.iov_base, 0, msg->front.iov_len);
222 req->r_request = msg;
226 EXPORT_SYMBOL(ceph_osdc_alloc_request);
228 static void osd_req_encode_op(struct ceph_osd_request *req,
229 struct ceph_osd_op *dst,
230 struct ceph_osd_req_op *src)
232 dst->op = cpu_to_le16(src->op);
235 case CEPH_OSD_OP_READ:
236 case CEPH_OSD_OP_WRITE:
238 cpu_to_le64(src->extent.offset);
240 cpu_to_le64(src->extent.length);
241 dst->extent.truncate_size =
242 cpu_to_le64(src->extent.truncate_size);
243 dst->extent.truncate_seq =
244 cpu_to_le32(src->extent.truncate_seq);
247 case CEPH_OSD_OP_GETXATTR:
248 case CEPH_OSD_OP_SETXATTR:
249 case CEPH_OSD_OP_CMPXATTR:
250 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
251 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
252 dst->xattr.cmp_op = src->xattr.cmp_op;
253 dst->xattr.cmp_mode = src->xattr.cmp_mode;
254 ceph_pagelist_append(&req->r_trail, src->xattr.name,
255 src->xattr.name_len);
256 ceph_pagelist_append(&req->r_trail, src->xattr.val,
257 src->xattr.value_len);
259 case CEPH_OSD_OP_CALL:
260 dst->cls.class_len = src->cls.class_len;
261 dst->cls.method_len = src->cls.method_len;
262 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
264 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
266 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
267 src->cls.method_len);
268 ceph_pagelist_append(&req->r_trail, src->cls.indata,
269 src->cls.indata_len);
271 case CEPH_OSD_OP_ROLLBACK:
272 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
274 case CEPH_OSD_OP_STARTSYNC:
276 case CEPH_OSD_OP_NOTIFY:
278 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
279 __le32 timeout = cpu_to_le32(src->watch.timeout);
281 ceph_pagelist_append(&req->r_trail,
282 &prot_ver, sizeof(prot_ver));
283 ceph_pagelist_append(&req->r_trail,
284 &timeout, sizeof(timeout));
286 case CEPH_OSD_OP_NOTIFY_ACK:
287 case CEPH_OSD_OP_WATCH:
288 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
289 dst->watch.ver = cpu_to_le64(src->watch.ver);
290 dst->watch.flag = src->watch.flag;
293 pr_err("unrecognized osd opcode %d\n", dst->op);
297 dst->payload_len = cpu_to_le32(src->payload_len);
301 * build new request AND message
304 void ceph_osdc_build_request(struct ceph_osd_request *req,
305 u64 off, u64 len, unsigned int num_op,
306 struct ceph_osd_req_op *src_ops,
307 struct ceph_snap_context *snapc, u64 snap_id,
308 struct timespec *mtime)
310 struct ceph_msg *msg = req->r_request;
311 struct ceph_osd_request_head *head;
312 struct ceph_osd_req_op *src_op;
313 struct ceph_osd_op *op;
315 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
316 int flags = req->r_flags;
320 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
322 head = msg->front.iov_base;
323 head->snapid = cpu_to_le64(snap_id);
324 op = (void *)(head + 1);
325 p = (void *)(op + num_op);
327 req->r_snapc = ceph_get_snap_context(snapc);
329 head->client_inc = cpu_to_le32(1); /* always, for now. */
330 head->flags = cpu_to_le32(flags);
331 if (flags & CEPH_OSD_FLAG_WRITE)
332 ceph_encode_timespec(&head->mtime, mtime);
333 BUG_ON(num_op > (unsigned int) ((u16) -1));
334 head->num_ops = cpu_to_le16(num_op);
337 head->object_len = cpu_to_le32(req->r_oid_len);
338 memcpy(p, req->r_oid, req->r_oid_len);
343 osd_req_encode_op(req, op++, src_op++);
345 data_len += req->r_trail.length;
348 head->snap_seq = cpu_to_le64(snapc->seq);
349 head->num_snaps = cpu_to_le32(snapc->num_snaps);
350 for (i = 0; i < snapc->num_snaps; i++) {
351 put_unaligned_le64(snapc->snaps[i], p);
356 if (flags & CEPH_OSD_FLAG_WRITE) {
357 req->r_request->hdr.data_off = cpu_to_le16(off);
358 req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
359 } else if (data_len) {
360 req->r_request->hdr.data_off = 0;
361 req->r_request->hdr.data_len = cpu_to_le32(data_len);
364 req->r_request->page_alignment = req->r_page_alignment;
366 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
367 msg_size = p - msg->front.iov_base;
368 msg->front.iov_len = msg_size;
369 msg->hdr.front_len = cpu_to_le32(msg_size);
372 EXPORT_SYMBOL(ceph_osdc_build_request);
375 * build new request AND message, calculate layout, and adjust file
378 * if the file was recently truncated, we include information about its
379 * old and new size so that the object can be updated appropriately. (we
380 * avoid synchronously deleting truncated objects because it's slow.)
382 * if @do_sync, include a 'startsync' command so that the osd will flush
385 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
386 struct ceph_file_layout *layout,
387 struct ceph_vino vino,
389 int opcode, int flags,
390 struct ceph_snap_context *snapc,
394 struct timespec *mtime,
395 bool use_mempool, int num_reply,
398 struct ceph_osd_req_op ops[2];
399 struct ceph_osd_request *req;
400 unsigned int num_op = 1;
403 memset(&ops, 0, sizeof ops);
406 ops[0].extent.truncate_seq = truncate_seq;
407 ops[0].extent.truncate_size = truncate_size;
410 ops[1].op = CEPH_OSD_OP_STARTSYNC;
414 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
417 return ERR_PTR(-ENOMEM);
418 req->r_flags = flags;
420 /* calculate max write size */
421 r = calc_layout(vino, layout, off, plen, req, ops);
424 req->r_file_layout = *layout; /* keep a copy */
426 /* in case it differs from natural (file) alignment that
427 calc_layout filled in for us */
428 req->r_num_pages = calc_pages_for(page_align, *plen);
429 req->r_page_alignment = page_align;
431 ceph_osdc_build_request(req, off, *plen, num_op, ops,
432 snapc, vino.snap, mtime);
436 EXPORT_SYMBOL(ceph_osdc_new_request);
439 * We keep osd requests in an rbtree, sorted by ->r_tid.
441 static void __insert_request(struct ceph_osd_client *osdc,
442 struct ceph_osd_request *new)
444 struct rb_node **p = &osdc->requests.rb_node;
445 struct rb_node *parent = NULL;
446 struct ceph_osd_request *req = NULL;
450 req = rb_entry(parent, struct ceph_osd_request, r_node);
451 if (new->r_tid < req->r_tid)
453 else if (new->r_tid > req->r_tid)
459 rb_link_node(&new->r_node, parent, p);
460 rb_insert_color(&new->r_node, &osdc->requests);
463 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
466 struct ceph_osd_request *req;
467 struct rb_node *n = osdc->requests.rb_node;
470 req = rb_entry(n, struct ceph_osd_request, r_node);
471 if (tid < req->r_tid)
473 else if (tid > req->r_tid)
481 static struct ceph_osd_request *
482 __lookup_request_ge(struct ceph_osd_client *osdc,
485 struct ceph_osd_request *req;
486 struct rb_node *n = osdc->requests.rb_node;
489 req = rb_entry(n, struct ceph_osd_request, r_node);
490 if (tid < req->r_tid) {
494 } else if (tid > req->r_tid) {
504 * Resubmit requests pending on the given osd.
506 static void __kick_osd_requests(struct ceph_osd_client *osdc,
507 struct ceph_osd *osd)
509 struct ceph_osd_request *req, *nreq;
512 dout("__kick_osd_requests osd%d\n", osd->o_osd);
513 err = __reset_osd(osdc, osd);
517 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
518 list_move(&req->r_req_lru_item, &osdc->req_unsent);
519 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
522 req->r_flags |= CEPH_OSD_FLAG_RETRY;
525 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
528 * reregister request prior to unregistering linger so
529 * that r_osd is preserved.
531 BUG_ON(!list_empty(&req->r_req_lru_item));
532 __register_request(osdc, req);
533 list_add(&req->r_req_lru_item, &osdc->req_unsent);
534 list_add(&req->r_osd_item, &req->r_osd->o_requests);
535 __unregister_linger_request(osdc, req);
536 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
542 * If the osd connection drops, we need to resubmit all requests.
544 static void osd_reset(struct ceph_connection *con)
546 struct ceph_osd *osd = con->private;
547 struct ceph_osd_client *osdc;
551 dout("osd_reset osd%d\n", osd->o_osd);
553 down_read(&osdc->map_sem);
554 mutex_lock(&osdc->request_mutex);
555 __kick_osd_requests(osdc, osd);
556 mutex_unlock(&osdc->request_mutex);
558 up_read(&osdc->map_sem);
562 * Track open sessions with osds.
564 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
566 struct ceph_osd *osd;
568 osd = kzalloc(sizeof(*osd), GFP_NOFS);
572 atomic_set(&osd->o_ref, 1);
575 RB_CLEAR_NODE(&osd->o_node);
576 INIT_LIST_HEAD(&osd->o_requests);
577 INIT_LIST_HEAD(&osd->o_linger_requests);
578 INIT_LIST_HEAD(&osd->o_osd_lru);
579 osd->o_incarnation = 1;
581 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
583 INIT_LIST_HEAD(&osd->o_keepalive_item);
587 static struct ceph_osd *get_osd(struct ceph_osd *osd)
589 if (atomic_inc_not_zero(&osd->o_ref)) {
590 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
591 atomic_read(&osd->o_ref));
594 dout("get_osd %p FAIL\n", osd);
599 static void put_osd(struct ceph_osd *osd)
601 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
602 atomic_read(&osd->o_ref) - 1);
603 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
604 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
606 if (ac->ops && ac->ops->destroy_authorizer)
607 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
613 * remove an osd from our map
615 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
617 dout("__remove_osd %p\n", osd);
618 BUG_ON(!list_empty(&osd->o_requests));
619 rb_erase(&osd->o_node, &osdc->osds);
620 list_del_init(&osd->o_osd_lru);
621 ceph_con_close(&osd->o_con);
625 static void remove_all_osds(struct ceph_osd_client *osdc)
627 dout("%s %p\n", __func__, osdc);
628 mutex_lock(&osdc->request_mutex);
629 while (!RB_EMPTY_ROOT(&osdc->osds)) {
630 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
631 struct ceph_osd, o_node);
632 __remove_osd(osdc, osd);
634 mutex_unlock(&osdc->request_mutex);
637 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
638 struct ceph_osd *osd)
640 dout("__move_osd_to_lru %p\n", osd);
641 BUG_ON(!list_empty(&osd->o_osd_lru));
642 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
643 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
646 static void __remove_osd_from_lru(struct ceph_osd *osd)
648 dout("__remove_osd_from_lru %p\n", osd);
649 if (!list_empty(&osd->o_osd_lru))
650 list_del_init(&osd->o_osd_lru);
653 static void remove_old_osds(struct ceph_osd_client *osdc)
655 struct ceph_osd *osd, *nosd;
657 dout("__remove_old_osds %p\n", osdc);
658 mutex_lock(&osdc->request_mutex);
659 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
660 if (time_before(jiffies, osd->lru_ttl))
662 __remove_osd(osdc, osd);
664 mutex_unlock(&osdc->request_mutex);
670 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
672 struct ceph_entity_addr *peer_addr;
674 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
675 if (list_empty(&osd->o_requests) &&
676 list_empty(&osd->o_linger_requests)) {
677 __remove_osd(osdc, osd);
682 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
683 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
684 !ceph_con_opened(&osd->o_con)) {
685 struct ceph_osd_request *req;
687 dout(" osd addr hasn't changed and connection never opened,"
688 " letting msgr retry");
689 /* touch each r_stamp for handle_timeout()'s benfit */
690 list_for_each_entry(req, &osd->o_requests, r_osd_item)
691 req->r_stamp = jiffies;
696 ceph_con_close(&osd->o_con);
697 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
698 osd->o_incarnation++;
703 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
705 struct rb_node **p = &osdc->osds.rb_node;
706 struct rb_node *parent = NULL;
707 struct ceph_osd *osd = NULL;
709 dout("__insert_osd %p osd%d\n", new, new->o_osd);
712 osd = rb_entry(parent, struct ceph_osd, o_node);
713 if (new->o_osd < osd->o_osd)
715 else if (new->o_osd > osd->o_osd)
721 rb_link_node(&new->o_node, parent, p);
722 rb_insert_color(&new->o_node, &osdc->osds);
725 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
727 struct ceph_osd *osd;
728 struct rb_node *n = osdc->osds.rb_node;
731 osd = rb_entry(n, struct ceph_osd, o_node);
734 else if (o > osd->o_osd)
742 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
744 schedule_delayed_work(&osdc->timeout_work,
745 osdc->client->options->osd_keepalive_timeout * HZ);
748 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
750 cancel_delayed_work(&osdc->timeout_work);
754 * Register request, assign tid. If this is the first request, set up
757 static void __register_request(struct ceph_osd_client *osdc,
758 struct ceph_osd_request *req)
760 req->r_tid = ++osdc->last_tid;
761 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
762 dout("__register_request %p tid %lld\n", req, req->r_tid);
763 __insert_request(osdc, req);
764 ceph_osdc_get_request(req);
765 osdc->num_requests++;
766 if (osdc->num_requests == 1) {
767 dout(" first request, scheduling timeout\n");
768 __schedule_osd_timeout(osdc);
772 static void register_request(struct ceph_osd_client *osdc,
773 struct ceph_osd_request *req)
775 mutex_lock(&osdc->request_mutex);
776 __register_request(osdc, req);
777 mutex_unlock(&osdc->request_mutex);
781 * called under osdc->request_mutex
783 static void __unregister_request(struct ceph_osd_client *osdc,
784 struct ceph_osd_request *req)
786 if (RB_EMPTY_NODE(&req->r_node)) {
787 dout("__unregister_request %p tid %lld not registered\n",
792 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
793 rb_erase(&req->r_node, &osdc->requests);
794 osdc->num_requests--;
797 /* make sure the original request isn't in flight. */
798 ceph_msg_revoke(req->r_request);
800 list_del_init(&req->r_osd_item);
801 if (list_empty(&req->r_osd->o_requests) &&
802 list_empty(&req->r_osd->o_linger_requests)) {
803 dout("moving osd to %p lru\n", req->r_osd);
804 __move_osd_to_lru(osdc, req->r_osd);
806 if (list_empty(&req->r_linger_item))
810 list_del_init(&req->r_req_lru_item);
811 ceph_osdc_put_request(req);
813 if (osdc->num_requests == 0) {
814 dout(" no requests, canceling timeout\n");
815 __cancel_osd_timeout(osdc);
820 * Cancel a previously queued request message
822 static void __cancel_request(struct ceph_osd_request *req)
824 if (req->r_sent && req->r_osd) {
825 ceph_msg_revoke(req->r_request);
830 static void __register_linger_request(struct ceph_osd_client *osdc,
831 struct ceph_osd_request *req)
833 dout("__register_linger_request %p\n", req);
834 list_add_tail(&req->r_linger_item, &osdc->req_linger);
836 list_add_tail(&req->r_linger_osd,
837 &req->r_osd->o_linger_requests);
840 static void __unregister_linger_request(struct ceph_osd_client *osdc,
841 struct ceph_osd_request *req)
843 dout("__unregister_linger_request %p\n", req);
844 list_del_init(&req->r_linger_item);
846 list_del_init(&req->r_linger_osd);
848 if (list_empty(&req->r_osd->o_requests) &&
849 list_empty(&req->r_osd->o_linger_requests)) {
850 dout("moving osd to %p lru\n", req->r_osd);
851 __move_osd_to_lru(osdc, req->r_osd);
853 if (list_empty(&req->r_osd_item))
858 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
859 struct ceph_osd_request *req)
861 mutex_lock(&osdc->request_mutex);
863 __unregister_linger_request(osdc, req);
864 ceph_osdc_put_request(req);
866 mutex_unlock(&osdc->request_mutex);
868 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
870 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
871 struct ceph_osd_request *req)
873 if (!req->r_linger) {
874 dout("set_request_linger %p\n", req);
877 * caller is now responsible for calling
878 * unregister_linger_request
880 ceph_osdc_get_request(req);
883 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
886 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
887 * (as needed), and set the request r_osd appropriately. If there is
888 * no up osd, set r_osd to NULL. Move the request to the appropriate list
889 * (unsent, homeless) or leave on in-flight lru.
891 * Return 0 if unchanged, 1 if changed, or negative on error.
893 * Caller should hold map_sem for read and request_mutex.
895 static int __map_request(struct ceph_osd_client *osdc,
896 struct ceph_osd_request *req, int force_resend)
898 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
900 int acting[CEPH_PG_MAX_SIZE];
904 dout("map_request %p tid %lld\n", req, req->r_tid);
905 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
906 &req->r_file_layout, osdc->osdmap);
908 list_move(&req->r_req_lru_item, &osdc->req_notarget);
911 pgid = reqhead->layout.ol_pgid;
914 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
920 if ((!force_resend &&
921 req->r_osd && req->r_osd->o_osd == o &&
922 req->r_sent >= req->r_osd->o_incarnation &&
923 req->r_num_pg_osds == num &&
924 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
925 (req->r_osd == NULL && o == -1))
926 return 0; /* no change */
928 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
929 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
930 req->r_osd ? req->r_osd->o_osd : -1);
932 /* record full pg acting set */
933 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
934 req->r_num_pg_osds = num;
937 __cancel_request(req);
938 list_del_init(&req->r_osd_item);
942 req->r_osd = __lookup_osd(osdc, o);
943 if (!req->r_osd && o >= 0) {
945 req->r_osd = create_osd(osdc, o);
947 list_move(&req->r_req_lru_item, &osdc->req_notarget);
951 dout("map_request osd %p is osd%d\n", req->r_osd, o);
952 __insert_osd(osdc, req->r_osd);
954 ceph_con_open(&req->r_osd->o_con,
955 CEPH_ENTITY_TYPE_OSD, o,
956 &osdc->osdmap->osd_addr[o]);
960 __remove_osd_from_lru(req->r_osd);
961 list_add(&req->r_osd_item, &req->r_osd->o_requests);
962 list_move(&req->r_req_lru_item, &osdc->req_unsent);
964 list_move(&req->r_req_lru_item, &osdc->req_notarget);
966 err = 1; /* osd or pg changed */
973 * caller should hold map_sem (for read) and request_mutex
975 static void __send_request(struct ceph_osd_client *osdc,
976 struct ceph_osd_request *req)
978 struct ceph_osd_request_head *reqhead;
980 dout("send_request %p tid %llu to osd%d flags %d\n",
981 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
983 reqhead = req->r_request->front.iov_base;
984 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
985 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
986 reqhead->reassert_version = req->r_reassert_version;
988 req->r_stamp = jiffies;
989 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
991 ceph_msg_get(req->r_request); /* send consumes a ref */
992 ceph_con_send(&req->r_osd->o_con, req->r_request);
993 req->r_sent = req->r_osd->o_incarnation;
997 * Send any requests in the queue (req_unsent).
999 static void send_queued(struct ceph_osd_client *osdc)
1001 struct ceph_osd_request *req, *tmp;
1003 dout("send_queued\n");
1004 mutex_lock(&osdc->request_mutex);
1005 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1006 __send_request(osdc, req);
1008 mutex_unlock(&osdc->request_mutex);
1012 * Timeout callback, called every N seconds when 1 or more osd
1013 * requests has been active for more than N seconds. When this
1014 * happens, we ping all OSDs with requests who have timed out to
1015 * ensure any communications channel reset is detected. Reset the
1016 * request timeouts another N seconds in the future as we go.
1017 * Reschedule the timeout event another N seconds in future (unless
1018 * there are no open requests).
1020 static void handle_timeout(struct work_struct *work)
1022 struct ceph_osd_client *osdc =
1023 container_of(work, struct ceph_osd_client, timeout_work.work);
1024 struct ceph_osd_request *req;
1025 struct ceph_osd *osd;
1026 unsigned long keepalive =
1027 osdc->client->options->osd_keepalive_timeout * HZ;
1028 struct list_head slow_osds;
1030 down_read(&osdc->map_sem);
1032 ceph_monc_request_next_osdmap(&osdc->client->monc);
1034 mutex_lock(&osdc->request_mutex);
1037 * ping osds that are a bit slow. this ensures that if there
1038 * is a break in the TCP connection we will notice, and reopen
1039 * a connection with that osd (from the fault callback).
1041 INIT_LIST_HEAD(&slow_osds);
1042 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1043 if (time_before(jiffies, req->r_stamp + keepalive))
1048 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1049 req->r_tid, osd->o_osd);
1050 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1052 while (!list_empty(&slow_osds)) {
1053 osd = list_entry(slow_osds.next, struct ceph_osd,
1055 list_del_init(&osd->o_keepalive_item);
1056 ceph_con_keepalive(&osd->o_con);
1059 __schedule_osd_timeout(osdc);
1060 mutex_unlock(&osdc->request_mutex);
1062 up_read(&osdc->map_sem);
1065 static void handle_osds_timeout(struct work_struct *work)
1067 struct ceph_osd_client *osdc =
1068 container_of(work, struct ceph_osd_client,
1069 osds_timeout_work.work);
1070 unsigned long delay =
1071 osdc->client->options->osd_idle_ttl * HZ >> 2;
1073 dout("osds timeout\n");
1074 down_read(&osdc->map_sem);
1075 remove_old_osds(osdc);
1076 up_read(&osdc->map_sem);
1078 schedule_delayed_work(&osdc->osds_timeout_work,
1079 round_jiffies_relative(delay));
1082 static void complete_request(struct ceph_osd_request *req)
1084 if (req->r_safe_callback)
1085 req->r_safe_callback(req, NULL);
1086 complete_all(&req->r_safe_completion); /* fsync waiter */
1090 * handle osd op reply. either call the callback if it is specified,
1091 * or do the completion to wake up the waiting thread.
1093 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1094 struct ceph_connection *con)
1096 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1097 struct ceph_osd_request *req;
1099 int numops, object_len, flags;
1102 tid = le64_to_cpu(msg->hdr.tid);
1103 if (msg->front.iov_len < sizeof(*rhead))
1105 numops = le32_to_cpu(rhead->num_ops);
1106 object_len = le32_to_cpu(rhead->object_len);
1107 result = le32_to_cpu(rhead->result);
1108 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1109 numops * sizeof(struct ceph_osd_op))
1111 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1113 mutex_lock(&osdc->request_mutex);
1114 req = __lookup_request(osdc, tid);
1116 dout("handle_reply tid %llu dne\n", tid);
1117 mutex_unlock(&osdc->request_mutex);
1120 ceph_osdc_get_request(req);
1121 flags = le32_to_cpu(rhead->flags);
1124 * if this connection filled our message, drop our reference now, to
1125 * avoid a (safe but slower) revoke later.
1127 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1128 dout(" dropping con_filling_msg ref %p\n", con);
1129 req->r_con_filling_msg = NULL;
1133 if (!req->r_got_reply) {
1136 req->r_result = le32_to_cpu(rhead->result);
1137 bytes = le32_to_cpu(msg->hdr.data_len);
1138 dout("handle_reply result %d bytes %d\n", req->r_result,
1140 if (req->r_result == 0)
1141 req->r_result = bytes;
1143 /* in case this is a write and we need to replay, */
1144 req->r_reassert_version = rhead->reassert_version;
1146 req->r_got_reply = 1;
1147 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1148 dout("handle_reply tid %llu dup ack\n", tid);
1149 mutex_unlock(&osdc->request_mutex);
1153 dout("handle_reply tid %llu flags %d\n", tid, flags);
1155 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1156 __register_linger_request(osdc, req);
1158 /* either this is a read, or we got the safe response */
1160 (flags & CEPH_OSD_FLAG_ONDISK) ||
1161 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1162 __unregister_request(osdc, req);
1164 mutex_unlock(&osdc->request_mutex);
1166 if (req->r_callback)
1167 req->r_callback(req, msg);
1169 complete_all(&req->r_completion);
1171 if (flags & CEPH_OSD_FLAG_ONDISK)
1172 complete_request(req);
1175 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1176 ceph_osdc_put_request(req);
1180 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1181 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1182 (int)sizeof(*rhead));
1186 static void reset_changed_osds(struct ceph_osd_client *osdc)
1188 struct rb_node *p, *n;
1190 for (p = rb_first(&osdc->osds); p; p = n) {
1191 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1194 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1195 memcmp(&osd->o_con.peer_addr,
1196 ceph_osd_addr(osdc->osdmap,
1198 sizeof(struct ceph_entity_addr)) != 0)
1199 __reset_osd(osdc, osd);
1204 * Requeue requests whose mapping to an OSD has changed. If requests map to
1205 * no osd, request a new map.
1207 * Caller should hold map_sem for read.
1209 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1211 struct ceph_osd_request *req, *nreq;
1216 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1217 mutex_lock(&osdc->request_mutex);
1218 for (p = rb_first(&osdc->requests); p; ) {
1219 req = rb_entry(p, struct ceph_osd_request, r_node);
1223 * For linger requests that have not yet been
1224 * registered, move them to the linger list; they'll
1225 * be sent to the osd in the loop below. Unregister
1226 * the request before re-registering it as a linger
1227 * request to ensure the __map_request() below
1228 * will decide it needs to be sent.
1230 if (req->r_linger && list_empty(&req->r_linger_item)) {
1231 dout("%p tid %llu restart on osd%d\n",
1233 req->r_osd ? req->r_osd->o_osd : -1);
1234 __unregister_request(osdc, req);
1235 __register_linger_request(osdc, req);
1239 err = __map_request(osdc, req, force_resend);
1241 continue; /* error */
1242 if (req->r_osd == NULL) {
1243 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1244 needmap++; /* request a newer map */
1245 } else if (err > 0) {
1246 if (!req->r_linger) {
1247 dout("%p tid %llu requeued on osd%d\n", req,
1249 req->r_osd ? req->r_osd->o_osd : -1);
1250 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1255 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1257 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1259 err = __map_request(osdc, req, force_resend);
1260 dout("__map_request returned %d\n", err);
1262 continue; /* no change and no osd was specified */
1264 continue; /* hrm! */
1265 if (req->r_osd == NULL) {
1266 dout("tid %llu maps to no valid osd\n", req->r_tid);
1267 needmap++; /* request a newer map */
1271 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1272 req->r_osd ? req->r_osd->o_osd : -1);
1273 __register_request(osdc, req);
1274 __unregister_linger_request(osdc, req);
1276 mutex_unlock(&osdc->request_mutex);
1279 dout("%d requests for down osds, need new map\n", needmap);
1280 ceph_monc_request_next_osdmap(&osdc->client->monc);
1282 reset_changed_osds(osdc);
1287 * Process updated osd map.
1289 * The message contains any number of incremental and full maps, normally
1290 * indicating some sort of topology change in the cluster. Kick requests
1291 * off to different OSDs as needed.
1293 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1295 void *p, *end, *next;
1296 u32 nr_maps, maplen;
1298 struct ceph_osdmap *newmap = NULL, *oldmap;
1300 struct ceph_fsid fsid;
1302 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1303 p = msg->front.iov_base;
1304 end = p + msg->front.iov_len;
1307 ceph_decode_need(&p, end, sizeof(fsid), bad);
1308 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1309 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1312 down_write(&osdc->map_sem);
1314 /* incremental maps */
1315 ceph_decode_32_safe(&p, end, nr_maps, bad);
1316 dout(" %d inc maps\n", nr_maps);
1317 while (nr_maps > 0) {
1318 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1319 epoch = ceph_decode_32(&p);
1320 maplen = ceph_decode_32(&p);
1321 ceph_decode_need(&p, end, maplen, bad);
1323 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1324 dout("applying incremental map %u len %d\n",
1326 newmap = osdmap_apply_incremental(&p, next,
1328 &osdc->client->msgr);
1329 if (IS_ERR(newmap)) {
1330 err = PTR_ERR(newmap);
1334 if (newmap != osdc->osdmap) {
1335 ceph_osdmap_destroy(osdc->osdmap);
1336 osdc->osdmap = newmap;
1338 kick_requests(osdc, 0);
1340 dout("ignoring incremental map %u len %d\n",
1350 ceph_decode_32_safe(&p, end, nr_maps, bad);
1351 dout(" %d full maps\n", nr_maps);
1353 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1354 epoch = ceph_decode_32(&p);
1355 maplen = ceph_decode_32(&p);
1356 ceph_decode_need(&p, end, maplen, bad);
1358 dout("skipping non-latest full map %u len %d\n",
1360 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1361 dout("skipping full map %u len %d, "
1362 "older than our %u\n", epoch, maplen,
1363 osdc->osdmap->epoch);
1365 int skipped_map = 0;
1367 dout("taking full map %u len %d\n", epoch, maplen);
1368 newmap = osdmap_decode(&p, p+maplen);
1369 if (IS_ERR(newmap)) {
1370 err = PTR_ERR(newmap);
1374 oldmap = osdc->osdmap;
1375 osdc->osdmap = newmap;
1377 if (oldmap->epoch + 1 < newmap->epoch)
1379 ceph_osdmap_destroy(oldmap);
1381 kick_requests(osdc, skipped_map);
1388 downgrade_write(&osdc->map_sem);
1389 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1392 * subscribe to subsequent osdmap updates if full to ensure
1393 * we find out when we are no longer full and stop returning
1396 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1397 ceph_monc_request_next_osdmap(&osdc->client->monc);
1400 up_read(&osdc->map_sem);
1401 wake_up_all(&osdc->client->auth_wq);
1405 pr_err("osdc handle_map corrupt msg\n");
1407 up_write(&osdc->map_sem);
1412 * watch/notify callback event infrastructure
1414 * These callbacks are used both for watch and notify operations.
1416 static void __release_event(struct kref *kref)
1418 struct ceph_osd_event *event =
1419 container_of(kref, struct ceph_osd_event, kref);
1421 dout("__release_event %p\n", event);
1425 static void get_event(struct ceph_osd_event *event)
1427 kref_get(&event->kref);
1430 void ceph_osdc_put_event(struct ceph_osd_event *event)
1432 kref_put(&event->kref, __release_event);
1434 EXPORT_SYMBOL(ceph_osdc_put_event);
1436 static void __insert_event(struct ceph_osd_client *osdc,
1437 struct ceph_osd_event *new)
1439 struct rb_node **p = &osdc->event_tree.rb_node;
1440 struct rb_node *parent = NULL;
1441 struct ceph_osd_event *event = NULL;
1445 event = rb_entry(parent, struct ceph_osd_event, node);
1446 if (new->cookie < event->cookie)
1448 else if (new->cookie > event->cookie)
1449 p = &(*p)->rb_right;
1454 rb_link_node(&new->node, parent, p);
1455 rb_insert_color(&new->node, &osdc->event_tree);
1458 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1461 struct rb_node **p = &osdc->event_tree.rb_node;
1462 struct rb_node *parent = NULL;
1463 struct ceph_osd_event *event = NULL;
1467 event = rb_entry(parent, struct ceph_osd_event, node);
1468 if (cookie < event->cookie)
1470 else if (cookie > event->cookie)
1471 p = &(*p)->rb_right;
1478 static void __remove_event(struct ceph_osd_event *event)
1480 struct ceph_osd_client *osdc = event->osdc;
1482 if (!RB_EMPTY_NODE(&event->node)) {
1483 dout("__remove_event removed %p\n", event);
1484 rb_erase(&event->node, &osdc->event_tree);
1485 ceph_osdc_put_event(event);
1487 dout("__remove_event didn't remove %p\n", event);
1491 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1492 void (*event_cb)(u64, u64, u8, void *),
1493 int one_shot, void *data,
1494 struct ceph_osd_event **pevent)
1496 struct ceph_osd_event *event;
1498 event = kmalloc(sizeof(*event), GFP_NOIO);
1502 dout("create_event %p\n", event);
1503 event->cb = event_cb;
1504 event->one_shot = one_shot;
1507 INIT_LIST_HEAD(&event->osd_node);
1508 RB_CLEAR_NODE(&event->node);
1509 kref_init(&event->kref); /* one ref for us */
1510 kref_get(&event->kref); /* one ref for the caller */
1511 init_completion(&event->completion);
1513 spin_lock(&osdc->event_lock);
1514 event->cookie = ++osdc->event_count;
1515 __insert_event(osdc, event);
1516 spin_unlock(&osdc->event_lock);
1521 EXPORT_SYMBOL(ceph_osdc_create_event);
1523 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1525 struct ceph_osd_client *osdc = event->osdc;
1527 dout("cancel_event %p\n", event);
1528 spin_lock(&osdc->event_lock);
1529 __remove_event(event);
1530 spin_unlock(&osdc->event_lock);
1531 ceph_osdc_put_event(event); /* caller's */
1533 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1536 static void do_event_work(struct work_struct *work)
1538 struct ceph_osd_event_work *event_work =
1539 container_of(work, struct ceph_osd_event_work, work);
1540 struct ceph_osd_event *event = event_work->event;
1541 u64 ver = event_work->ver;
1542 u64 notify_id = event_work->notify_id;
1543 u8 opcode = event_work->opcode;
1545 dout("do_event_work completing %p\n", event);
1546 event->cb(ver, notify_id, opcode, event->data);
1547 complete(&event->completion);
1548 dout("do_event_work completed %p\n", event);
1549 ceph_osdc_put_event(event);
1555 * Process osd watch notifications
1557 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1561 u64 cookie, ver, notify_id;
1563 struct ceph_osd_event *event;
1564 struct ceph_osd_event_work *event_work;
1566 p = msg->front.iov_base;
1567 end = p + msg->front.iov_len;
1569 ceph_decode_8_safe(&p, end, proto_ver, bad);
1570 ceph_decode_8_safe(&p, end, opcode, bad);
1571 ceph_decode_64_safe(&p, end, cookie, bad);
1572 ceph_decode_64_safe(&p, end, ver, bad);
1573 ceph_decode_64_safe(&p, end, notify_id, bad);
1575 spin_lock(&osdc->event_lock);
1576 event = __find_event(osdc, cookie);
1579 if (event->one_shot)
1580 __remove_event(event);
1582 spin_unlock(&osdc->event_lock);
1583 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1584 cookie, ver, event);
1586 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1588 dout("ERROR: could not allocate event_work\n");
1591 INIT_WORK(&event_work->work, do_event_work);
1592 event_work->event = event;
1593 event_work->ver = ver;
1594 event_work->notify_id = notify_id;
1595 event_work->opcode = opcode;
1596 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1597 dout("WARNING: failed to queue notify event work\n");
1605 complete(&event->completion);
1606 ceph_osdc_put_event(event);
1610 pr_err("osdc handle_watch_notify corrupt msg\n");
1614 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1618 dout("wait_event %p\n", event);
1619 err = wait_for_completion_interruptible_timeout(&event->completion,
1621 ceph_osdc_put_event(event);
1624 dout("wait_event %p returns %d\n", event, err);
1627 EXPORT_SYMBOL(ceph_osdc_wait_event);
1630 * Register request, send initial attempt.
1632 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1633 struct ceph_osd_request *req,
1638 req->r_request->pages = req->r_pages;
1639 req->r_request->nr_pages = req->r_num_pages;
1641 req->r_request->bio = req->r_bio;
1643 req->r_request->trail = &req->r_trail;
1645 register_request(osdc, req);
1647 down_read(&osdc->map_sem);
1648 mutex_lock(&osdc->request_mutex);
1650 * a racing kick_requests() may have sent the message for us
1651 * while we dropped request_mutex above, so only send now if
1652 * the request still han't been touched yet.
1654 if (req->r_sent == 0) {
1655 rc = __map_request(osdc, req, 0);
1658 dout("osdc_start_request failed map, "
1659 " will retry %lld\n", req->r_tid);
1664 if (req->r_osd == NULL) {
1665 dout("send_request %p no up osds in pg\n", req);
1666 ceph_monc_request_next_osdmap(&osdc->client->monc);
1668 __send_request(osdc, req);
1674 mutex_unlock(&osdc->request_mutex);
1675 up_read(&osdc->map_sem);
1678 EXPORT_SYMBOL(ceph_osdc_start_request);
1681 * wait for a request to complete
1683 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1684 struct ceph_osd_request *req)
1688 rc = wait_for_completion_interruptible(&req->r_completion);
1690 mutex_lock(&osdc->request_mutex);
1691 __cancel_request(req);
1692 __unregister_request(osdc, req);
1693 mutex_unlock(&osdc->request_mutex);
1694 complete_request(req);
1695 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1699 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1700 return req->r_result;
1702 EXPORT_SYMBOL(ceph_osdc_wait_request);
1705 * sync - wait for all in-flight requests to flush. avoid starvation.
1707 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1709 struct ceph_osd_request *req;
1710 u64 last_tid, next_tid = 0;
1712 mutex_lock(&osdc->request_mutex);
1713 last_tid = osdc->last_tid;
1715 req = __lookup_request_ge(osdc, next_tid);
1718 if (req->r_tid > last_tid)
1721 next_tid = req->r_tid + 1;
1722 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1725 ceph_osdc_get_request(req);
1726 mutex_unlock(&osdc->request_mutex);
1727 dout("sync waiting on tid %llu (last is %llu)\n",
1728 req->r_tid, last_tid);
1729 wait_for_completion(&req->r_safe_completion);
1730 mutex_lock(&osdc->request_mutex);
1731 ceph_osdc_put_request(req);
1733 mutex_unlock(&osdc->request_mutex);
1734 dout("sync done (thru tid %llu)\n", last_tid);
1736 EXPORT_SYMBOL(ceph_osdc_sync);
1741 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1746 osdc->client = client;
1747 osdc->osdmap = NULL;
1748 init_rwsem(&osdc->map_sem);
1749 init_completion(&osdc->map_waiters);
1750 osdc->last_requested_map = 0;
1751 mutex_init(&osdc->request_mutex);
1753 osdc->osds = RB_ROOT;
1754 INIT_LIST_HEAD(&osdc->osd_lru);
1755 osdc->requests = RB_ROOT;
1756 INIT_LIST_HEAD(&osdc->req_lru);
1757 INIT_LIST_HEAD(&osdc->req_unsent);
1758 INIT_LIST_HEAD(&osdc->req_notarget);
1759 INIT_LIST_HEAD(&osdc->req_linger);
1760 osdc->num_requests = 0;
1761 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1762 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1763 spin_lock_init(&osdc->event_lock);
1764 osdc->event_tree = RB_ROOT;
1765 osdc->event_count = 0;
1767 schedule_delayed_work(&osdc->osds_timeout_work,
1768 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1771 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1772 sizeof(struct ceph_osd_request));
1773 if (!osdc->req_mempool)
1776 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1777 OSD_OP_FRONT_LEN, 10, true,
1781 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1782 OSD_OPREPLY_FRONT_LEN, 10, true,
1787 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1788 if (IS_ERR(osdc->notify_wq)) {
1789 err = PTR_ERR(osdc->notify_wq);
1790 osdc->notify_wq = NULL;
1796 ceph_msgpool_destroy(&osdc->msgpool_op);
1798 mempool_destroy(osdc->req_mempool);
1802 EXPORT_SYMBOL(ceph_osdc_init);
1804 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1806 flush_workqueue(osdc->notify_wq);
1807 destroy_workqueue(osdc->notify_wq);
1808 cancel_delayed_work_sync(&osdc->timeout_work);
1809 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1811 ceph_osdmap_destroy(osdc->osdmap);
1812 osdc->osdmap = NULL;
1814 remove_all_osds(osdc);
1815 mempool_destroy(osdc->req_mempool);
1816 ceph_msgpool_destroy(&osdc->msgpool_op);
1817 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1819 EXPORT_SYMBOL(ceph_osdc_stop);
1822 * Read some contiguous pages. If we cross a stripe boundary, shorten
1823 * *plen. Return number of bytes read, or error.
1825 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1826 struct ceph_vino vino, struct ceph_file_layout *layout,
1828 u32 truncate_seq, u64 truncate_size,
1829 struct page **pages, int num_pages, int page_align)
1831 struct ceph_osd_request *req;
1834 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1835 vino.snap, off, *plen);
1836 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1837 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1838 NULL, 0, truncate_seq, truncate_size, NULL,
1839 false, 1, page_align);
1841 return PTR_ERR(req);
1843 /* it may be a short read due to an object boundary */
1844 req->r_pages = pages;
1846 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1847 off, *plen, req->r_num_pages, page_align);
1849 rc = ceph_osdc_start_request(osdc, req, false);
1851 rc = ceph_osdc_wait_request(osdc, req);
1853 ceph_osdc_put_request(req);
1854 dout("readpages result %d\n", rc);
1857 EXPORT_SYMBOL(ceph_osdc_readpages);
1860 * do a synchronous write on N pages
1862 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1863 struct ceph_file_layout *layout,
1864 struct ceph_snap_context *snapc,
1866 u32 truncate_seq, u64 truncate_size,
1867 struct timespec *mtime,
1868 struct page **pages, int num_pages,
1869 int flags, int do_sync, bool nofail)
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 flags | CEPH_OSD_FLAG_ONDISK |
1879 CEPH_OSD_FLAG_WRITE,
1881 truncate_seq, truncate_size, mtime,
1882 nofail, 1, page_align);
1884 return PTR_ERR(req);
1886 /* it may be a short write due to an object boundary */
1887 req->r_pages = pages;
1888 dout("writepages %llu~%llu (%d pages)\n", off, len,
1891 rc = ceph_osdc_start_request(osdc, req, nofail);
1893 rc = ceph_osdc_wait_request(osdc, req);
1895 ceph_osdc_put_request(req);
1898 dout("writepages result %d\n", rc);
1901 EXPORT_SYMBOL(ceph_osdc_writepages);
1904 * handle incoming message
1906 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1908 struct ceph_osd *osd = con->private;
1909 struct ceph_osd_client *osdc;
1910 int type = le16_to_cpu(msg->hdr.type);
1917 case CEPH_MSG_OSD_MAP:
1918 ceph_osdc_handle_map(osdc, msg);
1920 case CEPH_MSG_OSD_OPREPLY:
1921 handle_reply(osdc, msg, con);
1923 case CEPH_MSG_WATCH_NOTIFY:
1924 handle_watch_notify(osdc, msg);
1928 pr_err("received unknown message type %d %s\n", type,
1929 ceph_msg_type_name(type));
1936 * lookup and return message for incoming reply. set up reply message
1939 static struct ceph_msg *get_reply(struct ceph_connection *con,
1940 struct ceph_msg_header *hdr,
1943 struct ceph_osd *osd = con->private;
1944 struct ceph_osd_client *osdc = osd->o_osdc;
1946 struct ceph_osd_request *req;
1947 int front = le32_to_cpu(hdr->front_len);
1948 int data_len = le32_to_cpu(hdr->data_len);
1951 tid = le64_to_cpu(hdr->tid);
1952 mutex_lock(&osdc->request_mutex);
1953 req = __lookup_request(osdc, tid);
1957 dout("get_reply unknown tid %llu from osd%d\n", tid,
1962 if (req->r_con_filling_msg) {
1963 dout("%s revoking msg %p from old con %p\n", __func__,
1964 req->r_reply, req->r_con_filling_msg);
1965 ceph_msg_revoke_incoming(req->r_reply);
1966 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
1967 req->r_con_filling_msg = NULL;
1970 if (front > req->r_reply->front.iov_len) {
1971 pr_warning("get_reply front %d > preallocated %d\n",
1972 front, (int)req->r_reply->front.iov_len);
1973 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
1976 ceph_msg_put(req->r_reply);
1979 m = ceph_msg_get(req->r_reply);
1982 int want = calc_pages_for(req->r_page_alignment, data_len);
1984 if (unlikely(req->r_num_pages < want)) {
1985 pr_warning("tid %lld reply has %d bytes %d pages, we"
1986 " had only %d pages ready\n", tid, data_len,
1987 want, req->r_num_pages);
1993 m->pages = req->r_pages;
1994 m->nr_pages = req->r_num_pages;
1995 m->page_alignment = req->r_page_alignment;
1997 m->bio = req->r_bio;
2001 req->r_con_filling_msg = con->ops->get(con);
2002 dout("get_reply tid %lld %p\n", tid, m);
2005 mutex_unlock(&osdc->request_mutex);
2010 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2011 struct ceph_msg_header *hdr,
2014 struct ceph_osd *osd = con->private;
2015 int type = le16_to_cpu(hdr->type);
2016 int front = le32_to_cpu(hdr->front_len);
2020 case CEPH_MSG_OSD_MAP:
2021 case CEPH_MSG_WATCH_NOTIFY:
2022 return ceph_msg_new(type, front, GFP_NOFS, false);
2023 case CEPH_MSG_OSD_OPREPLY:
2024 return get_reply(con, hdr, skip);
2026 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2034 * Wrappers to refcount containing ceph_osd struct
2036 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2038 struct ceph_osd *osd = con->private;
2044 static void put_osd_con(struct ceph_connection *con)
2046 struct ceph_osd *osd = con->private;
2054 * Note: returned pointer is the address of a structure that's
2055 * managed separately. Caller must *not* attempt to free it.
2057 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2058 int *proto, int force_new)
2060 struct ceph_osd *o = con->private;
2061 struct ceph_osd_client *osdc = o->o_osdc;
2062 struct ceph_auth_client *ac = osdc->client->monc.auth;
2063 struct ceph_auth_handshake *auth = &o->o_auth;
2065 if (force_new && auth->authorizer) {
2066 if (ac->ops && ac->ops->destroy_authorizer)
2067 ac->ops->destroy_authorizer(ac, auth->authorizer);
2068 auth->authorizer = NULL;
2070 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2071 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2074 return ERR_PTR(ret);
2076 *proto = ac->protocol;
2082 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2084 struct ceph_osd *o = con->private;
2085 struct ceph_osd_client *osdc = o->o_osdc;
2086 struct ceph_auth_client *ac = osdc->client->monc.auth;
2089 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2090 * XXX which do we do: succeed or fail?
2092 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2095 static int invalidate_authorizer(struct ceph_connection *con)
2097 struct ceph_osd *o = con->private;
2098 struct ceph_osd_client *osdc = o->o_osdc;
2099 struct ceph_auth_client *ac = osdc->client->monc.auth;
2101 if (ac->ops && ac->ops->invalidate_authorizer)
2102 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2104 return ceph_monc_validate_auth(&osdc->client->monc);
2107 static const struct ceph_connection_operations osd_con_ops = {
2110 .dispatch = dispatch,
2111 .get_authorizer = get_authorizer,
2112 .verify_authorizer_reply = verify_authorizer_reply,
2113 .invalidate_authorizer = invalidate_authorizer,
2114 .alloc_msg = alloc_msg,