1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops;
26 static void send_queued(struct ceph_osd_client *osdc);
27 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28 static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30 static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32 static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
35 static int op_needs_trail(int op)
38 case CEPH_OSD_OP_GETXATTR:
39 case CEPH_OSD_OP_SETXATTR:
40 case CEPH_OSD_OP_CMPXATTR:
41 case CEPH_OSD_OP_CALL:
42 case CEPH_OSD_OP_NOTIFY:
49 static int op_has_extent(int op)
51 return (op == CEPH_OSD_OP_READ ||
52 op == CEPH_OSD_OP_WRITE);
55 void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
56 struct ceph_file_layout *layout,
58 u64 off, u64 *plen, u64 *bno,
59 struct ceph_osd_request *req,
60 struct ceph_osd_req_op *op)
62 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
64 u64 objoff, objlen; /* extent in object */
66 reqhead->snapid = cpu_to_le64(snapid);
69 ceph_calc_file_object_mapping(layout, off, plen, bno,
72 dout(" skipping last %llu, final file extent %llu~%llu\n",
73 orig_len - *plen, off, *plen);
75 if (op_has_extent(op->op)) {
76 op->extent.offset = objoff;
77 op->extent.length = objlen;
79 req->r_num_pages = calc_pages_for(off, *plen);
80 req->r_page_alignment = off & ~PAGE_MASK;
81 if (op->op == CEPH_OSD_OP_WRITE)
82 op->payload_len = *plen;
84 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
85 *bno, objoff, objlen, req->r_num_pages);
88 EXPORT_SYMBOL(ceph_calc_raw_layout);
91 * Implement client access to distributed object storage cluster.
93 * All data objects are stored within a cluster/cloud of OSDs, or
94 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
95 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
96 * remote daemons serving up and coordinating consistent and safe
99 * Cluster membership and the mapping of data objects onto storage devices
100 * are described by the osd map.
102 * We keep track of pending OSD requests (read, write), resubmit
103 * requests to different OSDs when the cluster topology/data layout
104 * change, or retry the affected requests when the communications
105 * channel with an OSD is reset.
109 * calculate the mapping of a file extent onto an object, and fill out the
110 * request accordingly. shorten extent as necessary if it crosses an
113 * fill osd op in request message.
115 static void calc_layout(struct ceph_osd_client *osdc,
116 struct ceph_vino vino,
117 struct ceph_file_layout *layout,
119 struct ceph_osd_request *req,
120 struct ceph_osd_req_op *op)
124 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
125 plen, &bno, req, op);
127 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
128 req->r_oid_len = strlen(req->r_oid);
134 void ceph_osdc_release_request(struct kref *kref)
136 struct ceph_osd_request *req = container_of(kref,
137 struct ceph_osd_request,
141 ceph_msg_put(req->r_request);
142 if (req->r_con_filling_msg) {
143 dout("release_request revoking pages %p from con %p\n",
144 req->r_pages, req->r_con_filling_msg);
145 ceph_con_revoke_message(req->r_con_filling_msg,
147 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
150 ceph_msg_put(req->r_reply);
151 if (req->r_own_pages)
152 ceph_release_page_vector(req->r_pages,
158 ceph_put_snap_context(req->r_snapc);
160 ceph_pagelist_release(req->r_trail);
164 mempool_free(req, req->r_osdc->req_mempool);
168 EXPORT_SYMBOL(ceph_osdc_release_request);
170 static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
177 if (needs_trail && op_needs_trail(ops[i].op))
185 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
187 struct ceph_snap_context *snapc,
188 struct ceph_osd_req_op *ops,
194 struct ceph_osd_request *req;
195 struct ceph_msg *msg;
197 int num_op = get_num_ops(ops, &needs_trail);
198 size_t msg_size = sizeof(struct ceph_osd_request_head);
200 msg_size += num_op*sizeof(struct ceph_osd_op);
203 req = mempool_alloc(osdc->req_mempool, gfp_flags);
204 memset(req, 0, sizeof(*req));
206 req = kzalloc(sizeof(*req), gfp_flags);
212 req->r_mempool = use_mempool;
214 kref_init(&req->r_kref);
215 init_completion(&req->r_completion);
216 init_completion(&req->r_safe_completion);
217 INIT_LIST_HEAD(&req->r_unsafe_item);
218 INIT_LIST_HEAD(&req->r_linger_item);
219 INIT_LIST_HEAD(&req->r_linger_osd);
220 INIT_LIST_HEAD(&req->r_req_lru_item);
221 req->r_flags = flags;
223 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
225 /* create reply message */
227 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
229 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
230 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
232 ceph_osdc_put_request(req);
237 /* allocate space for the trailing data */
239 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
241 ceph_osdc_put_request(req);
244 ceph_pagelist_init(req->r_trail);
246 /* create request message; allow space for oid */
247 msg_size += MAX_OBJ_NAME_SIZE;
249 msg_size += sizeof(u64) * snapc->num_snaps;
251 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
253 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
255 ceph_osdc_put_request(req);
259 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
260 memset(msg->front.iov_base, 0, msg->front.iov_len);
262 req->r_request = msg;
263 req->r_pages = pages;
273 EXPORT_SYMBOL(ceph_osdc_alloc_request);
275 static void osd_req_encode_op(struct ceph_osd_request *req,
276 struct ceph_osd_op *dst,
277 struct ceph_osd_req_op *src)
279 dst->op = cpu_to_le16(src->op);
282 case CEPH_OSD_OP_READ:
283 case CEPH_OSD_OP_WRITE:
285 cpu_to_le64(src->extent.offset);
287 cpu_to_le64(src->extent.length);
288 dst->extent.truncate_size =
289 cpu_to_le64(src->extent.truncate_size);
290 dst->extent.truncate_seq =
291 cpu_to_le32(src->extent.truncate_seq);
294 case CEPH_OSD_OP_GETXATTR:
295 case CEPH_OSD_OP_SETXATTR:
296 case CEPH_OSD_OP_CMPXATTR:
297 BUG_ON(!req->r_trail);
299 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
300 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
301 dst->xattr.cmp_op = src->xattr.cmp_op;
302 dst->xattr.cmp_mode = src->xattr.cmp_mode;
303 ceph_pagelist_append(req->r_trail, src->xattr.name,
304 src->xattr.name_len);
305 ceph_pagelist_append(req->r_trail, src->xattr.val,
306 src->xattr.value_len);
308 case CEPH_OSD_OP_CALL:
309 BUG_ON(!req->r_trail);
311 dst->cls.class_len = src->cls.class_len;
312 dst->cls.method_len = src->cls.method_len;
313 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
315 ceph_pagelist_append(req->r_trail, src->cls.class_name,
317 ceph_pagelist_append(req->r_trail, src->cls.method_name,
318 src->cls.method_len);
319 ceph_pagelist_append(req->r_trail, src->cls.indata,
320 src->cls.indata_len);
322 case CEPH_OSD_OP_ROLLBACK:
323 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
325 case CEPH_OSD_OP_STARTSYNC:
327 case CEPH_OSD_OP_NOTIFY:
329 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
330 __le32 timeout = cpu_to_le32(src->watch.timeout);
332 BUG_ON(!req->r_trail);
334 ceph_pagelist_append(req->r_trail,
335 &prot_ver, sizeof(prot_ver));
336 ceph_pagelist_append(req->r_trail,
337 &timeout, sizeof(timeout));
339 case CEPH_OSD_OP_NOTIFY_ACK:
340 case CEPH_OSD_OP_WATCH:
341 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
342 dst->watch.ver = cpu_to_le64(src->watch.ver);
343 dst->watch.flag = src->watch.flag;
346 pr_err("unrecognized osd opcode %d\n", dst->op);
350 dst->payload_len = cpu_to_le32(src->payload_len);
354 * build new request AND message
357 void ceph_osdc_build_request(struct ceph_osd_request *req,
359 struct ceph_osd_req_op *src_ops,
360 struct ceph_snap_context *snapc,
361 struct timespec *mtime,
365 struct ceph_msg *msg = req->r_request;
366 struct ceph_osd_request_head *head;
367 struct ceph_osd_req_op *src_op;
368 struct ceph_osd_op *op;
370 int num_op = get_num_ops(src_ops, NULL);
371 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
372 int flags = req->r_flags;
376 head = msg->front.iov_base;
377 op = (void *)(head + 1);
378 p = (void *)(op + num_op);
380 req->r_snapc = ceph_get_snap_context(snapc);
382 head->client_inc = cpu_to_le32(1); /* always, for now. */
383 head->flags = cpu_to_le32(flags);
384 if (flags & CEPH_OSD_FLAG_WRITE)
385 ceph_encode_timespec(&head->mtime, mtime);
386 head->num_ops = cpu_to_le16(num_op);
390 head->object_len = cpu_to_le32(oid_len);
391 memcpy(p, oid, oid_len);
396 osd_req_encode_op(req, op, src_op);
402 data_len += req->r_trail->length;
405 head->snap_seq = cpu_to_le64(snapc->seq);
406 head->num_snaps = cpu_to_le32(snapc->num_snaps);
407 for (i = 0; i < snapc->num_snaps; i++) {
408 put_unaligned_le64(snapc->snaps[i], p);
413 if (flags & CEPH_OSD_FLAG_WRITE) {
414 req->r_request->hdr.data_off = cpu_to_le16(off);
415 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
416 } else if (data_len) {
417 req->r_request->hdr.data_off = 0;
418 req->r_request->hdr.data_len = cpu_to_le32(data_len);
421 req->r_request->page_alignment = req->r_page_alignment;
423 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
424 msg_size = p - msg->front.iov_base;
425 msg->front.iov_len = msg_size;
426 msg->hdr.front_len = cpu_to_le32(msg_size);
429 EXPORT_SYMBOL(ceph_osdc_build_request);
432 * build new request AND message, calculate layout, and adjust file
435 * if the file was recently truncated, we include information about its
436 * old and new size so that the object can be updated appropriately. (we
437 * avoid synchronously deleting truncated objects because it's slow.)
439 * if @do_sync, include a 'startsync' command so that the osd will flush
442 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
443 struct ceph_file_layout *layout,
444 struct ceph_vino vino,
446 int opcode, int flags,
447 struct ceph_snap_context *snapc,
451 struct timespec *mtime,
452 bool use_mempool, int num_reply,
455 struct ceph_osd_req_op ops[3];
456 struct ceph_osd_request *req;
459 ops[0].extent.truncate_seq = truncate_seq;
460 ops[0].extent.truncate_size = truncate_size;
461 ops[0].payload_len = 0;
464 ops[1].op = CEPH_OSD_OP_STARTSYNC;
465 ops[1].payload_len = 0;
470 req = ceph_osdc_alloc_request(osdc, flags,
473 GFP_NOFS, NULL, NULL);
477 /* calculate max write size */
478 calc_layout(osdc, vino, layout, off, plen, req, ops);
479 req->r_file_layout = *layout; /* keep a copy */
481 /* in case it differs from natural (file) alignment that
482 calc_layout filled in for us */
483 req->r_num_pages = calc_pages_for(page_align, *plen);
484 req->r_page_alignment = page_align;
486 ceph_osdc_build_request(req, off, plen, ops,
489 req->r_oid, req->r_oid_len);
493 EXPORT_SYMBOL(ceph_osdc_new_request);
496 * We keep osd requests in an rbtree, sorted by ->r_tid.
498 static void __insert_request(struct ceph_osd_client *osdc,
499 struct ceph_osd_request *new)
501 struct rb_node **p = &osdc->requests.rb_node;
502 struct rb_node *parent = NULL;
503 struct ceph_osd_request *req = NULL;
507 req = rb_entry(parent, struct ceph_osd_request, r_node);
508 if (new->r_tid < req->r_tid)
510 else if (new->r_tid > req->r_tid)
516 rb_link_node(&new->r_node, parent, p);
517 rb_insert_color(&new->r_node, &osdc->requests);
520 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
523 struct ceph_osd_request *req;
524 struct rb_node *n = osdc->requests.rb_node;
527 req = rb_entry(n, struct ceph_osd_request, r_node);
528 if (tid < req->r_tid)
530 else if (tid > req->r_tid)
538 static struct ceph_osd_request *
539 __lookup_request_ge(struct ceph_osd_client *osdc,
542 struct ceph_osd_request *req;
543 struct rb_node *n = osdc->requests.rb_node;
546 req = rb_entry(n, struct ceph_osd_request, r_node);
547 if (tid < req->r_tid) {
551 } else if (tid > req->r_tid) {
561 * Resubmit requests pending on the given osd.
563 static void __kick_osd_requests(struct ceph_osd_client *osdc,
564 struct ceph_osd *osd)
566 struct ceph_osd_request *req, *nreq;
569 dout("__kick_osd_requests osd%d\n", osd->o_osd);
570 err = __reset_osd(osdc, osd);
574 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
575 list_move(&req->r_req_lru_item, &osdc->req_unsent);
576 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
579 req->r_flags |= CEPH_OSD_FLAG_RETRY;
582 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
585 * reregister request prior to unregistering linger so
586 * that r_osd is preserved.
588 BUG_ON(!list_empty(&req->r_req_lru_item));
589 __register_request(osdc, req);
590 list_add(&req->r_req_lru_item, &osdc->req_unsent);
591 list_add(&req->r_osd_item, &req->r_osd->o_requests);
592 __unregister_linger_request(osdc, req);
593 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
598 static void kick_osd_requests(struct ceph_osd_client *osdc,
599 struct ceph_osd *kickosd)
601 mutex_lock(&osdc->request_mutex);
602 __kick_osd_requests(osdc, kickosd);
603 mutex_unlock(&osdc->request_mutex);
607 * If the osd connection drops, we need to resubmit all requests.
609 static void osd_reset(struct ceph_connection *con)
611 struct ceph_osd *osd = con->private;
612 struct ceph_osd_client *osdc;
616 dout("osd_reset osd%d\n", osd->o_osd);
618 down_read(&osdc->map_sem);
619 kick_osd_requests(osdc, osd);
621 up_read(&osdc->map_sem);
625 * Track open sessions with osds.
627 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
629 struct ceph_osd *osd;
631 osd = kzalloc(sizeof(*osd), GFP_NOFS);
635 atomic_set(&osd->o_ref, 1);
638 INIT_LIST_HEAD(&osd->o_requests);
639 INIT_LIST_HEAD(&osd->o_linger_requests);
640 INIT_LIST_HEAD(&osd->o_osd_lru);
641 osd->o_incarnation = 1;
643 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr,
644 CEPH_ENTITY_TYPE_OSD, onum);
646 INIT_LIST_HEAD(&osd->o_keepalive_item);
650 static struct ceph_osd *get_osd(struct ceph_osd *osd)
652 if (atomic_inc_not_zero(&osd->o_ref)) {
653 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
654 atomic_read(&osd->o_ref));
657 dout("get_osd %p FAIL\n", osd);
662 static void put_osd(struct ceph_osd *osd)
664 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
665 atomic_read(&osd->o_ref) - 1);
666 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
667 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
669 if (ac->ops && ac->ops->destroy_authorizer)
670 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
676 * remove an osd from our map
678 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
680 dout("__remove_osd %p\n", osd);
681 BUG_ON(!list_empty(&osd->o_requests));
682 rb_erase(&osd->o_node, &osdc->osds);
683 list_del_init(&osd->o_osd_lru);
684 ceph_con_close(&osd->o_con);
688 static void remove_all_osds(struct ceph_osd_client *osdc)
690 dout("__remove_old_osds %p\n", osdc);
691 mutex_lock(&osdc->request_mutex);
692 while (!RB_EMPTY_ROOT(&osdc->osds)) {
693 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
694 struct ceph_osd, o_node);
695 __remove_osd(osdc, osd);
697 mutex_unlock(&osdc->request_mutex);
700 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
701 struct ceph_osd *osd)
703 dout("__move_osd_to_lru %p\n", osd);
704 BUG_ON(!list_empty(&osd->o_osd_lru));
705 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
706 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
709 static void __remove_osd_from_lru(struct ceph_osd *osd)
711 dout("__remove_osd_from_lru %p\n", osd);
712 if (!list_empty(&osd->o_osd_lru))
713 list_del_init(&osd->o_osd_lru);
716 static void remove_old_osds(struct ceph_osd_client *osdc)
718 struct ceph_osd *osd, *nosd;
720 dout("__remove_old_osds %p\n", osdc);
721 mutex_lock(&osdc->request_mutex);
722 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
723 if (time_before(jiffies, osd->lru_ttl))
725 __remove_osd(osdc, osd);
727 mutex_unlock(&osdc->request_mutex);
733 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
735 struct ceph_osd_request *req;
738 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
739 if (list_empty(&osd->o_requests) &&
740 list_empty(&osd->o_linger_requests)) {
741 __remove_osd(osdc, osd);
742 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
743 &osd->o_con.peer_addr,
744 sizeof(osd->o_con.peer_addr)) == 0 &&
745 !ceph_con_opened(&osd->o_con)) {
746 dout(" osd addr hasn't changed and connection never opened,"
747 " letting msgr retry");
748 /* touch each r_stamp for handle_timeout()'s benfit */
749 list_for_each_entry(req, &osd->o_requests, r_osd_item)
750 req->r_stamp = jiffies;
753 ceph_con_close(&osd->o_con);
754 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
755 osd->o_incarnation++;
760 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
762 struct rb_node **p = &osdc->osds.rb_node;
763 struct rb_node *parent = NULL;
764 struct ceph_osd *osd = NULL;
766 dout("__insert_osd %p osd%d\n", new, new->o_osd);
769 osd = rb_entry(parent, struct ceph_osd, o_node);
770 if (new->o_osd < osd->o_osd)
772 else if (new->o_osd > osd->o_osd)
778 rb_link_node(&new->o_node, parent, p);
779 rb_insert_color(&new->o_node, &osdc->osds);
782 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
784 struct ceph_osd *osd;
785 struct rb_node *n = osdc->osds.rb_node;
788 osd = rb_entry(n, struct ceph_osd, o_node);
791 else if (o > osd->o_osd)
799 static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
801 schedule_delayed_work(&osdc->timeout_work,
802 osdc->client->options->osd_keepalive_timeout * HZ);
805 static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
807 cancel_delayed_work(&osdc->timeout_work);
811 * Register request, assign tid. If this is the first request, set up
814 static void __register_request(struct ceph_osd_client *osdc,
815 struct ceph_osd_request *req)
817 req->r_tid = ++osdc->last_tid;
818 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
819 dout("__register_request %p tid %lld\n", req, req->r_tid);
820 __insert_request(osdc, req);
821 ceph_osdc_get_request(req);
822 osdc->num_requests++;
823 if (osdc->num_requests == 1) {
824 dout(" first request, scheduling timeout\n");
825 __schedule_osd_timeout(osdc);
829 static void register_request(struct ceph_osd_client *osdc,
830 struct ceph_osd_request *req)
832 mutex_lock(&osdc->request_mutex);
833 __register_request(osdc, req);
834 mutex_unlock(&osdc->request_mutex);
838 * called under osdc->request_mutex
840 static void __unregister_request(struct ceph_osd_client *osdc,
841 struct ceph_osd_request *req)
843 if (RB_EMPTY_NODE(&req->r_node)) {
844 dout("__unregister_request %p tid %lld not registered\n",
849 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
850 rb_erase(&req->r_node, &osdc->requests);
851 osdc->num_requests--;
854 /* make sure the original request isn't in flight. */
855 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
857 list_del_init(&req->r_osd_item);
858 if (list_empty(&req->r_osd->o_requests) &&
859 list_empty(&req->r_osd->o_linger_requests)) {
860 dout("moving osd to %p lru\n", req->r_osd);
861 __move_osd_to_lru(osdc, req->r_osd);
863 if (list_empty(&req->r_linger_item))
867 ceph_osdc_put_request(req);
869 list_del_init(&req->r_req_lru_item);
870 if (osdc->num_requests == 0) {
871 dout(" no requests, canceling timeout\n");
872 __cancel_osd_timeout(osdc);
877 * Cancel a previously queued request message
879 static void __cancel_request(struct ceph_osd_request *req)
881 if (req->r_sent && req->r_osd) {
882 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
887 static void __register_linger_request(struct ceph_osd_client *osdc,
888 struct ceph_osd_request *req)
890 dout("__register_linger_request %p\n", req);
891 list_add_tail(&req->r_linger_item, &osdc->req_linger);
892 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
895 static void __unregister_linger_request(struct ceph_osd_client *osdc,
896 struct ceph_osd_request *req)
898 dout("__unregister_linger_request %p\n", req);
900 list_del_init(&req->r_linger_item);
901 list_del_init(&req->r_linger_osd);
903 if (list_empty(&req->r_osd->o_requests) &&
904 list_empty(&req->r_osd->o_linger_requests)) {
905 dout("moving osd to %p lru\n", req->r_osd);
906 __move_osd_to_lru(osdc, req->r_osd);
908 if (list_empty(&req->r_osd_item))
913 void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
914 struct ceph_osd_request *req)
916 mutex_lock(&osdc->request_mutex);
918 __unregister_linger_request(osdc, req);
919 ceph_osdc_put_request(req);
921 mutex_unlock(&osdc->request_mutex);
923 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
925 void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
926 struct ceph_osd_request *req)
928 if (!req->r_linger) {
929 dout("set_request_linger %p\n", req);
932 * caller is now responsible for calling
933 * unregister_linger_request
935 ceph_osdc_get_request(req);
938 EXPORT_SYMBOL(ceph_osdc_set_request_linger);
941 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
942 * (as needed), and set the request r_osd appropriately. If there is
943 * no up osd, set r_osd to NULL. Move the request to the appropriate list
944 * (unsent, homeless) or leave on in-flight lru.
946 * Return 0 if unchanged, 1 if changed, or negative on error.
948 * Caller should hold map_sem for read and request_mutex.
950 static int __map_request(struct ceph_osd_client *osdc,
951 struct ceph_osd_request *req, int force_resend)
953 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
955 int acting[CEPH_PG_MAX_SIZE];
959 dout("map_request %p tid %lld\n", req, req->r_tid);
960 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
961 &req->r_file_layout, osdc->osdmap);
963 list_move(&req->r_req_lru_item, &osdc->req_notarget);
966 pgid = reqhead->layout.ol_pgid;
969 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
975 if ((!force_resend &&
976 req->r_osd && req->r_osd->o_osd == o &&
977 req->r_sent >= req->r_osd->o_incarnation &&
978 req->r_num_pg_osds == num &&
979 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
980 (req->r_osd == NULL && o == -1))
981 return 0; /* no change */
983 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
984 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
985 req->r_osd ? req->r_osd->o_osd : -1);
987 /* record full pg acting set */
988 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
989 req->r_num_pg_osds = num;
992 __cancel_request(req);
993 list_del_init(&req->r_osd_item);
997 req->r_osd = __lookup_osd(osdc, o);
998 if (!req->r_osd && o >= 0) {
1000 req->r_osd = create_osd(osdc, o);
1002 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1006 dout("map_request osd %p is osd%d\n", req->r_osd, o);
1007 __insert_osd(osdc, req->r_osd);
1009 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
1013 __remove_osd_from_lru(req->r_osd);
1014 list_add(&req->r_osd_item, &req->r_osd->o_requests);
1015 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1017 list_move(&req->r_req_lru_item, &osdc->req_notarget);
1019 err = 1; /* osd or pg changed */
1026 * caller should hold map_sem (for read) and request_mutex
1028 static void __send_request(struct ceph_osd_client *osdc,
1029 struct ceph_osd_request *req)
1031 struct ceph_osd_request_head *reqhead;
1033 dout("send_request %p tid %llu to osd%d flags %d\n",
1034 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1036 reqhead = req->r_request->front.iov_base;
1037 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1038 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1039 reqhead->reassert_version = req->r_reassert_version;
1041 req->r_stamp = jiffies;
1042 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1044 ceph_msg_get(req->r_request); /* send consumes a ref */
1045 ceph_con_send(&req->r_osd->o_con, req->r_request);
1046 req->r_sent = req->r_osd->o_incarnation;
1050 * Send any requests in the queue (req_unsent).
1052 static void send_queued(struct ceph_osd_client *osdc)
1054 struct ceph_osd_request *req, *tmp;
1056 dout("send_queued\n");
1057 mutex_lock(&osdc->request_mutex);
1058 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1059 __send_request(osdc, req);
1061 mutex_unlock(&osdc->request_mutex);
1065 * Timeout callback, called every N seconds when 1 or more osd
1066 * requests has been active for more than N seconds. When this
1067 * happens, we ping all OSDs with requests who have timed out to
1068 * ensure any communications channel reset is detected. Reset the
1069 * request timeouts another N seconds in the future as we go.
1070 * Reschedule the timeout event another N seconds in future (unless
1071 * there are no open requests).
1073 static void handle_timeout(struct work_struct *work)
1075 struct ceph_osd_client *osdc =
1076 container_of(work, struct ceph_osd_client, timeout_work.work);
1077 struct ceph_osd_request *req, *last_req = NULL;
1078 struct ceph_osd *osd;
1079 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
1080 unsigned long keepalive =
1081 osdc->client->options->osd_keepalive_timeout * HZ;
1082 unsigned long last_stamp = 0;
1083 struct list_head slow_osds;
1085 down_read(&osdc->map_sem);
1087 ceph_monc_request_next_osdmap(&osdc->client->monc);
1089 mutex_lock(&osdc->request_mutex);
1092 * reset osds that appear to be _really_ unresponsive. this
1093 * is a failsafe measure.. we really shouldn't be getting to
1094 * this point if the system is working properly. the monitors
1095 * should mark the osd as failed and we should find out about
1096 * it from an updated osd map.
1098 while (timeout && !list_empty(&osdc->req_lru)) {
1099 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1102 /* hasn't been long enough since we sent it? */
1103 if (time_before(jiffies, req->r_stamp + timeout))
1106 /* hasn't been long enough since it was acked? */
1107 if (req->r_request->ack_stamp == 0 ||
1108 time_before(jiffies, req->r_request->ack_stamp + timeout))
1111 BUG_ON(req == last_req && req->r_stamp == last_stamp);
1113 last_stamp = req->r_stamp;
1117 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1118 req->r_tid, osd->o_osd);
1119 __kick_osd_requests(osdc, osd);
1123 * ping osds that are a bit slow. this ensures that if there
1124 * is a break in the TCP connection we will notice, and reopen
1125 * a connection with that osd (from the fault callback).
1127 INIT_LIST_HEAD(&slow_osds);
1128 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1129 if (time_before(jiffies, req->r_stamp + keepalive))
1134 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1135 req->r_tid, osd->o_osd);
1136 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1138 while (!list_empty(&slow_osds)) {
1139 osd = list_entry(slow_osds.next, struct ceph_osd,
1141 list_del_init(&osd->o_keepalive_item);
1142 ceph_con_keepalive(&osd->o_con);
1145 __schedule_osd_timeout(osdc);
1146 mutex_unlock(&osdc->request_mutex);
1148 up_read(&osdc->map_sem);
1151 static void handle_osds_timeout(struct work_struct *work)
1153 struct ceph_osd_client *osdc =
1154 container_of(work, struct ceph_osd_client,
1155 osds_timeout_work.work);
1156 unsigned long delay =
1157 osdc->client->options->osd_idle_ttl * HZ >> 2;
1159 dout("osds timeout\n");
1160 down_read(&osdc->map_sem);
1161 remove_old_osds(osdc);
1162 up_read(&osdc->map_sem);
1164 schedule_delayed_work(&osdc->osds_timeout_work,
1165 round_jiffies_relative(delay));
1168 static void complete_request(struct ceph_osd_request *req)
1170 if (req->r_safe_callback)
1171 req->r_safe_callback(req, NULL);
1172 complete_all(&req->r_safe_completion); /* fsync waiter */
1176 * handle osd op reply. either call the callback if it is specified,
1177 * or do the completion to wake up the waiting thread.
1179 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1180 struct ceph_connection *con)
1182 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1183 struct ceph_osd_request *req;
1185 int numops, object_len, flags;
1188 tid = le64_to_cpu(msg->hdr.tid);
1189 if (msg->front.iov_len < sizeof(*rhead))
1191 numops = le32_to_cpu(rhead->num_ops);
1192 object_len = le32_to_cpu(rhead->object_len);
1193 result = le32_to_cpu(rhead->result);
1194 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1195 numops * sizeof(struct ceph_osd_op))
1197 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1199 mutex_lock(&osdc->request_mutex);
1200 req = __lookup_request(osdc, tid);
1202 dout("handle_reply tid %llu dne\n", tid);
1203 mutex_unlock(&osdc->request_mutex);
1206 ceph_osdc_get_request(req);
1207 flags = le32_to_cpu(rhead->flags);
1210 * if this connection filled our message, drop our reference now, to
1211 * avoid a (safe but slower) revoke later.
1213 if (req->r_con_filling_msg == con && req->r_reply == msg) {
1214 dout(" dropping con_filling_msg ref %p\n", con);
1215 req->r_con_filling_msg = NULL;
1219 if (!req->r_got_reply) {
1222 req->r_result = le32_to_cpu(rhead->result);
1223 bytes = le32_to_cpu(msg->hdr.data_len);
1224 dout("handle_reply result %d bytes %d\n", req->r_result,
1226 if (req->r_result == 0)
1227 req->r_result = bytes;
1229 /* in case this is a write and we need to replay, */
1230 req->r_reassert_version = rhead->reassert_version;
1232 req->r_got_reply = 1;
1233 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1234 dout("handle_reply tid %llu dup ack\n", tid);
1235 mutex_unlock(&osdc->request_mutex);
1239 dout("handle_reply tid %llu flags %d\n", tid, flags);
1241 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1242 __register_linger_request(osdc, req);
1244 /* either this is a read, or we got the safe response */
1246 (flags & CEPH_OSD_FLAG_ONDISK) ||
1247 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1248 __unregister_request(osdc, req);
1250 mutex_unlock(&osdc->request_mutex);
1252 if (req->r_callback)
1253 req->r_callback(req, msg);
1255 complete_all(&req->r_completion);
1257 if (flags & CEPH_OSD_FLAG_ONDISK)
1258 complete_request(req);
1261 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1262 ceph_osdc_put_request(req);
1266 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1267 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1268 (int)sizeof(*rhead));
1272 static void reset_changed_osds(struct ceph_osd_client *osdc)
1274 struct rb_node *p, *n;
1276 for (p = rb_first(&osdc->osds); p; p = n) {
1277 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1280 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1281 memcmp(&osd->o_con.peer_addr,
1282 ceph_osd_addr(osdc->osdmap,
1284 sizeof(struct ceph_entity_addr)) != 0)
1285 __reset_osd(osdc, osd);
1290 * Requeue requests whose mapping to an OSD has changed. If requests map to
1291 * no osd, request a new map.
1293 * Caller should hold map_sem for read and request_mutex.
1295 static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1297 struct ceph_osd_request *req, *nreq;
1302 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1303 mutex_lock(&osdc->request_mutex);
1304 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1305 req = rb_entry(p, struct ceph_osd_request, r_node);
1306 err = __map_request(osdc, req, force_resend);
1308 continue; /* error */
1309 if (req->r_osd == NULL) {
1310 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1311 needmap++; /* request a newer map */
1312 } else if (err > 0) {
1313 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1314 req->r_osd ? req->r_osd->o_osd : -1);
1316 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1320 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1322 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1324 err = __map_request(osdc, req, force_resend);
1326 continue; /* no change and no osd was specified */
1328 continue; /* hrm! */
1329 if (req->r_osd == NULL) {
1330 dout("tid %llu maps to no valid osd\n", req->r_tid);
1331 needmap++; /* request a newer map */
1335 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1336 req->r_osd ? req->r_osd->o_osd : -1);
1337 __unregister_linger_request(osdc, req);
1338 __register_request(osdc, req);
1340 mutex_unlock(&osdc->request_mutex);
1343 dout("%d requests for down osds, need new map\n", needmap);
1344 ceph_monc_request_next_osdmap(&osdc->client->monc);
1350 * Process updated osd map.
1352 * The message contains any number of incremental and full maps, normally
1353 * indicating some sort of topology change in the cluster. Kick requests
1354 * off to different OSDs as needed.
1356 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1358 void *p, *end, *next;
1359 u32 nr_maps, maplen;
1361 struct ceph_osdmap *newmap = NULL, *oldmap;
1363 struct ceph_fsid fsid;
1365 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1366 p = msg->front.iov_base;
1367 end = p + msg->front.iov_len;
1370 ceph_decode_need(&p, end, sizeof(fsid), bad);
1371 ceph_decode_copy(&p, &fsid, sizeof(fsid));
1372 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1375 down_write(&osdc->map_sem);
1377 /* incremental maps */
1378 ceph_decode_32_safe(&p, end, nr_maps, bad);
1379 dout(" %d inc maps\n", nr_maps);
1380 while (nr_maps > 0) {
1381 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1382 epoch = ceph_decode_32(&p);
1383 maplen = ceph_decode_32(&p);
1384 ceph_decode_need(&p, end, maplen, bad);
1386 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1387 dout("applying incremental map %u len %d\n",
1389 newmap = osdmap_apply_incremental(&p, next,
1391 &osdc->client->msgr);
1392 if (IS_ERR(newmap)) {
1393 err = PTR_ERR(newmap);
1397 if (newmap != osdc->osdmap) {
1398 ceph_osdmap_destroy(osdc->osdmap);
1399 osdc->osdmap = newmap;
1401 kick_requests(osdc, 0);
1402 reset_changed_osds(osdc);
1404 dout("ignoring incremental map %u len %d\n",
1414 ceph_decode_32_safe(&p, end, nr_maps, bad);
1415 dout(" %d full maps\n", nr_maps);
1417 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1418 epoch = ceph_decode_32(&p);
1419 maplen = ceph_decode_32(&p);
1420 ceph_decode_need(&p, end, maplen, bad);
1422 dout("skipping non-latest full map %u len %d\n",
1424 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1425 dout("skipping full map %u len %d, "
1426 "older than our %u\n", epoch, maplen,
1427 osdc->osdmap->epoch);
1429 int skipped_map = 0;
1431 dout("taking full map %u len %d\n", epoch, maplen);
1432 newmap = osdmap_decode(&p, p+maplen);
1433 if (IS_ERR(newmap)) {
1434 err = PTR_ERR(newmap);
1438 oldmap = osdc->osdmap;
1439 osdc->osdmap = newmap;
1441 if (oldmap->epoch + 1 < newmap->epoch)
1443 ceph_osdmap_destroy(oldmap);
1445 kick_requests(osdc, skipped_map);
1452 downgrade_write(&osdc->map_sem);
1453 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1456 * subscribe to subsequent osdmap updates if full to ensure
1457 * we find out when we are no longer full and stop returning
1460 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1461 ceph_monc_request_next_osdmap(&osdc->client->monc);
1464 up_read(&osdc->map_sem);
1465 wake_up_all(&osdc->client->auth_wq);
1469 pr_err("osdc handle_map corrupt msg\n");
1471 up_write(&osdc->map_sem);
1476 * watch/notify callback event infrastructure
1478 * These callbacks are used both for watch and notify operations.
1480 static void __release_event(struct kref *kref)
1482 struct ceph_osd_event *event =
1483 container_of(kref, struct ceph_osd_event, kref);
1485 dout("__release_event %p\n", event);
1489 static void get_event(struct ceph_osd_event *event)
1491 kref_get(&event->kref);
1494 void ceph_osdc_put_event(struct ceph_osd_event *event)
1496 kref_put(&event->kref, __release_event);
1498 EXPORT_SYMBOL(ceph_osdc_put_event);
1500 static void __insert_event(struct ceph_osd_client *osdc,
1501 struct ceph_osd_event *new)
1503 struct rb_node **p = &osdc->event_tree.rb_node;
1504 struct rb_node *parent = NULL;
1505 struct ceph_osd_event *event = NULL;
1509 event = rb_entry(parent, struct ceph_osd_event, node);
1510 if (new->cookie < event->cookie)
1512 else if (new->cookie > event->cookie)
1513 p = &(*p)->rb_right;
1518 rb_link_node(&new->node, parent, p);
1519 rb_insert_color(&new->node, &osdc->event_tree);
1522 static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1525 struct rb_node **p = &osdc->event_tree.rb_node;
1526 struct rb_node *parent = NULL;
1527 struct ceph_osd_event *event = NULL;
1531 event = rb_entry(parent, struct ceph_osd_event, node);
1532 if (cookie < event->cookie)
1534 else if (cookie > event->cookie)
1535 p = &(*p)->rb_right;
1542 static void __remove_event(struct ceph_osd_event *event)
1544 struct ceph_osd_client *osdc = event->osdc;
1546 if (!RB_EMPTY_NODE(&event->node)) {
1547 dout("__remove_event removed %p\n", event);
1548 rb_erase(&event->node, &osdc->event_tree);
1549 ceph_osdc_put_event(event);
1551 dout("__remove_event didn't remove %p\n", event);
1555 int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1556 void (*event_cb)(u64, u64, u8, void *),
1557 int one_shot, void *data,
1558 struct ceph_osd_event **pevent)
1560 struct ceph_osd_event *event;
1562 event = kmalloc(sizeof(*event), GFP_NOIO);
1566 dout("create_event %p\n", event);
1567 event->cb = event_cb;
1568 event->one_shot = one_shot;
1571 INIT_LIST_HEAD(&event->osd_node);
1572 kref_init(&event->kref); /* one ref for us */
1573 kref_get(&event->kref); /* one ref for the caller */
1574 init_completion(&event->completion);
1576 spin_lock(&osdc->event_lock);
1577 event->cookie = ++osdc->event_count;
1578 __insert_event(osdc, event);
1579 spin_unlock(&osdc->event_lock);
1584 EXPORT_SYMBOL(ceph_osdc_create_event);
1586 void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1588 struct ceph_osd_client *osdc = event->osdc;
1590 dout("cancel_event %p\n", event);
1591 spin_lock(&osdc->event_lock);
1592 __remove_event(event);
1593 spin_unlock(&osdc->event_lock);
1594 ceph_osdc_put_event(event); /* caller's */
1596 EXPORT_SYMBOL(ceph_osdc_cancel_event);
1599 static void do_event_work(struct work_struct *work)
1601 struct ceph_osd_event_work *event_work =
1602 container_of(work, struct ceph_osd_event_work, work);
1603 struct ceph_osd_event *event = event_work->event;
1604 u64 ver = event_work->ver;
1605 u64 notify_id = event_work->notify_id;
1606 u8 opcode = event_work->opcode;
1608 dout("do_event_work completing %p\n", event);
1609 event->cb(ver, notify_id, opcode, event->data);
1610 complete(&event->completion);
1611 dout("do_event_work completed %p\n", event);
1612 ceph_osdc_put_event(event);
1618 * Process osd watch notifications
1620 void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1624 u64 cookie, ver, notify_id;
1626 struct ceph_osd_event *event;
1627 struct ceph_osd_event_work *event_work;
1629 p = msg->front.iov_base;
1630 end = p + msg->front.iov_len;
1632 ceph_decode_8_safe(&p, end, proto_ver, bad);
1633 ceph_decode_8_safe(&p, end, opcode, bad);
1634 ceph_decode_64_safe(&p, end, cookie, bad);
1635 ceph_decode_64_safe(&p, end, ver, bad);
1636 ceph_decode_64_safe(&p, end, notify_id, bad);
1638 spin_lock(&osdc->event_lock);
1639 event = __find_event(osdc, cookie);
1642 if (event->one_shot)
1643 __remove_event(event);
1645 spin_unlock(&osdc->event_lock);
1646 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1647 cookie, ver, event);
1649 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1651 dout("ERROR: could not allocate event_work\n");
1654 INIT_WORK(&event_work->work, do_event_work);
1655 event_work->event = event;
1656 event_work->ver = ver;
1657 event_work->notify_id = notify_id;
1658 event_work->opcode = opcode;
1659 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1660 dout("WARNING: failed to queue notify event work\n");
1668 complete(&event->completion);
1669 ceph_osdc_put_event(event);
1673 pr_err("osdc handle_watch_notify corrupt msg\n");
1677 int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1681 dout("wait_event %p\n", event);
1682 err = wait_for_completion_interruptible_timeout(&event->completion,
1684 ceph_osdc_put_event(event);
1687 dout("wait_event %p returns %d\n", event, err);
1690 EXPORT_SYMBOL(ceph_osdc_wait_event);
1693 * Register request, send initial attempt.
1695 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1696 struct ceph_osd_request *req,
1701 req->r_request->pages = req->r_pages;
1702 req->r_request->nr_pages = req->r_num_pages;
1704 req->r_request->bio = req->r_bio;
1706 req->r_request->trail = req->r_trail;
1708 register_request(osdc, req);
1710 down_read(&osdc->map_sem);
1711 mutex_lock(&osdc->request_mutex);
1713 * a racing kick_requests() may have sent the message for us
1714 * while we dropped request_mutex above, so only send now if
1715 * the request still han't been touched yet.
1717 if (req->r_sent == 0) {
1718 rc = __map_request(osdc, req, 0);
1721 dout("osdc_start_request failed map, "
1722 " will retry %lld\n", req->r_tid);
1727 if (req->r_osd == NULL) {
1728 dout("send_request %p no up osds in pg\n", req);
1729 ceph_monc_request_next_osdmap(&osdc->client->monc);
1731 __send_request(osdc, req);
1737 mutex_unlock(&osdc->request_mutex);
1738 up_read(&osdc->map_sem);
1741 EXPORT_SYMBOL(ceph_osdc_start_request);
1744 * wait for a request to complete
1746 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1747 struct ceph_osd_request *req)
1751 rc = wait_for_completion_interruptible(&req->r_completion);
1753 mutex_lock(&osdc->request_mutex);
1754 __cancel_request(req);
1755 __unregister_request(osdc, req);
1756 mutex_unlock(&osdc->request_mutex);
1757 complete_request(req);
1758 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1762 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1763 return req->r_result;
1765 EXPORT_SYMBOL(ceph_osdc_wait_request);
1768 * sync - wait for all in-flight requests to flush. avoid starvation.
1770 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1772 struct ceph_osd_request *req;
1773 u64 last_tid, next_tid = 0;
1775 mutex_lock(&osdc->request_mutex);
1776 last_tid = osdc->last_tid;
1778 req = __lookup_request_ge(osdc, next_tid);
1781 if (req->r_tid > last_tid)
1784 next_tid = req->r_tid + 1;
1785 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1788 ceph_osdc_get_request(req);
1789 mutex_unlock(&osdc->request_mutex);
1790 dout("sync waiting on tid %llu (last is %llu)\n",
1791 req->r_tid, last_tid);
1792 wait_for_completion(&req->r_safe_completion);
1793 mutex_lock(&osdc->request_mutex);
1794 ceph_osdc_put_request(req);
1796 mutex_unlock(&osdc->request_mutex);
1797 dout("sync done (thru tid %llu)\n", last_tid);
1799 EXPORT_SYMBOL(ceph_osdc_sync);
1804 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1809 osdc->client = client;
1810 osdc->osdmap = NULL;
1811 init_rwsem(&osdc->map_sem);
1812 init_completion(&osdc->map_waiters);
1813 osdc->last_requested_map = 0;
1814 mutex_init(&osdc->request_mutex);
1816 osdc->osds = RB_ROOT;
1817 INIT_LIST_HEAD(&osdc->osd_lru);
1818 osdc->requests = RB_ROOT;
1819 INIT_LIST_HEAD(&osdc->req_lru);
1820 INIT_LIST_HEAD(&osdc->req_unsent);
1821 INIT_LIST_HEAD(&osdc->req_notarget);
1822 INIT_LIST_HEAD(&osdc->req_linger);
1823 osdc->num_requests = 0;
1824 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1825 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1826 spin_lock_init(&osdc->event_lock);
1827 osdc->event_tree = RB_ROOT;
1828 osdc->event_count = 0;
1830 schedule_delayed_work(&osdc->osds_timeout_work,
1831 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1834 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1835 sizeof(struct ceph_osd_request));
1836 if (!osdc->req_mempool)
1839 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1843 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1844 OSD_OPREPLY_FRONT_LEN, 10, true,
1849 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1850 if (IS_ERR(osdc->notify_wq)) {
1851 err = PTR_ERR(osdc->notify_wq);
1852 osdc->notify_wq = NULL;
1858 ceph_msgpool_destroy(&osdc->msgpool_op);
1860 mempool_destroy(osdc->req_mempool);
1864 EXPORT_SYMBOL(ceph_osdc_init);
1866 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1868 flush_workqueue(osdc->notify_wq);
1869 destroy_workqueue(osdc->notify_wq);
1870 cancel_delayed_work_sync(&osdc->timeout_work);
1871 cancel_delayed_work_sync(&osdc->osds_timeout_work);
1873 ceph_osdmap_destroy(osdc->osdmap);
1874 osdc->osdmap = NULL;
1876 remove_all_osds(osdc);
1877 mempool_destroy(osdc->req_mempool);
1878 ceph_msgpool_destroy(&osdc->msgpool_op);
1879 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1881 EXPORT_SYMBOL(ceph_osdc_stop);
1884 * Read some contiguous pages. If we cross a stripe boundary, shorten
1885 * *plen. Return number of bytes read, or error.
1887 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1888 struct ceph_vino vino, struct ceph_file_layout *layout,
1890 u32 truncate_seq, u64 truncate_size,
1891 struct page **pages, int num_pages, int page_align)
1893 struct ceph_osd_request *req;
1896 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1897 vino.snap, off, *plen);
1898 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1899 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1900 NULL, 0, truncate_seq, truncate_size, NULL,
1901 false, 1, page_align);
1905 /* it may be a short read due to an object boundary */
1906 req->r_pages = pages;
1908 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1909 off, *plen, req->r_num_pages, page_align);
1911 rc = ceph_osdc_start_request(osdc, req, false);
1913 rc = ceph_osdc_wait_request(osdc, req);
1915 ceph_osdc_put_request(req);
1916 dout("readpages result %d\n", rc);
1919 EXPORT_SYMBOL(ceph_osdc_readpages);
1922 * do a synchronous write on N pages
1924 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1925 struct ceph_file_layout *layout,
1926 struct ceph_snap_context *snapc,
1928 u32 truncate_seq, u64 truncate_size,
1929 struct timespec *mtime,
1930 struct page **pages, int num_pages,
1931 int flags, int do_sync, bool nofail)
1933 struct ceph_osd_request *req;
1935 int page_align = off & ~PAGE_MASK;
1937 BUG_ON(vino.snap != CEPH_NOSNAP);
1938 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1940 flags | CEPH_OSD_FLAG_ONDISK |
1941 CEPH_OSD_FLAG_WRITE,
1943 truncate_seq, truncate_size, mtime,
1944 nofail, 1, page_align);
1948 /* it may be a short write due to an object boundary */
1949 req->r_pages = pages;
1950 dout("writepages %llu~%llu (%d pages)\n", off, len,
1953 rc = ceph_osdc_start_request(osdc, req, nofail);
1955 rc = ceph_osdc_wait_request(osdc, req);
1957 ceph_osdc_put_request(req);
1960 dout("writepages result %d\n", rc);
1963 EXPORT_SYMBOL(ceph_osdc_writepages);
1966 * handle incoming message
1968 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1970 struct ceph_osd *osd = con->private;
1971 struct ceph_osd_client *osdc;
1972 int type = le16_to_cpu(msg->hdr.type);
1979 case CEPH_MSG_OSD_MAP:
1980 ceph_osdc_handle_map(osdc, msg);
1982 case CEPH_MSG_OSD_OPREPLY:
1983 handle_reply(osdc, msg, con);
1985 case CEPH_MSG_WATCH_NOTIFY:
1986 handle_watch_notify(osdc, msg);
1990 pr_err("received unknown message type %d %s\n", type,
1991 ceph_msg_type_name(type));
1998 * lookup and return message for incoming reply. set up reply message
2001 static struct ceph_msg *get_reply(struct ceph_connection *con,
2002 struct ceph_msg_header *hdr,
2005 struct ceph_osd *osd = con->private;
2006 struct ceph_osd_client *osdc = osd->o_osdc;
2008 struct ceph_osd_request *req;
2009 int front = le32_to_cpu(hdr->front_len);
2010 int data_len = le32_to_cpu(hdr->data_len);
2013 tid = le64_to_cpu(hdr->tid);
2014 mutex_lock(&osdc->request_mutex);
2015 req = __lookup_request(osdc, tid);
2019 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
2024 if (req->r_con_filling_msg) {
2025 dout("get_reply revoking msg %p from old con %p\n",
2026 req->r_reply, req->r_con_filling_msg);
2027 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
2028 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2029 req->r_con_filling_msg = NULL;
2032 if (front > req->r_reply->front.iov_len) {
2033 pr_warning("get_reply front %d > preallocated %d\n",
2034 front, (int)req->r_reply->front.iov_len);
2035 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2038 ceph_msg_put(req->r_reply);
2041 m = ceph_msg_get(req->r_reply);
2044 int want = calc_pages_for(req->r_page_alignment, data_len);
2046 if (unlikely(req->r_num_pages < want)) {
2047 pr_warning("tid %lld reply has %d bytes %d pages, we"
2048 " had only %d pages ready\n", tid, data_len,
2049 want, req->r_num_pages);
2055 m->pages = req->r_pages;
2056 m->nr_pages = req->r_num_pages;
2057 m->page_alignment = req->r_page_alignment;
2059 m->bio = req->r_bio;
2063 req->r_con_filling_msg = con->ops->get(con);
2064 dout("get_reply tid %lld %p\n", tid, m);
2067 mutex_unlock(&osdc->request_mutex);
2072 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2073 struct ceph_msg_header *hdr,
2076 struct ceph_osd *osd = con->private;
2077 int type = le16_to_cpu(hdr->type);
2078 int front = le32_to_cpu(hdr->front_len);
2081 case CEPH_MSG_OSD_MAP:
2082 case CEPH_MSG_WATCH_NOTIFY:
2083 return ceph_msg_new(type, front, GFP_NOFS, false);
2084 case CEPH_MSG_OSD_OPREPLY:
2085 return get_reply(con, hdr, skip);
2087 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2095 * Wrappers to refcount containing ceph_osd struct
2097 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2099 struct ceph_osd *osd = con->private;
2105 static void put_osd_con(struct ceph_connection *con)
2107 struct ceph_osd *osd = con->private;
2115 * Note: returned pointer is the address of a structure that's
2116 * managed separately. Caller must *not* attempt to free it.
2118 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2119 int *proto, int force_new)
2121 struct ceph_osd *o = con->private;
2122 struct ceph_osd_client *osdc = o->o_osdc;
2123 struct ceph_auth_client *ac = osdc->client->monc.auth;
2124 struct ceph_auth_handshake *auth = &o->o_auth;
2126 if (force_new && auth->authorizer) {
2127 if (ac->ops && ac->ops->destroy_authorizer)
2128 ac->ops->destroy_authorizer(ac, auth->authorizer);
2129 auth->authorizer = NULL;
2131 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2132 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2135 return ERR_PTR(ret);
2137 *proto = ac->protocol;
2143 static int verify_authorizer_reply(struct ceph_connection *con, int len)
2145 struct ceph_osd *o = con->private;
2146 struct ceph_osd_client *osdc = o->o_osdc;
2147 struct ceph_auth_client *ac = osdc->client->monc.auth;
2150 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2151 * XXX which do we do: succeed or fail?
2153 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2156 static int invalidate_authorizer(struct ceph_connection *con)
2158 struct ceph_osd *o = con->private;
2159 struct ceph_osd_client *osdc = o->o_osdc;
2160 struct ceph_auth_client *ac = osdc->client->monc.auth;
2162 if (ac->ops && ac->ops->invalidate_authorizer)
2163 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2165 return ceph_monc_validate_auth(&osdc->client->monc);
2168 static const struct ceph_connection_operations osd_con_ops = {
2171 .dispatch = dispatch,
2172 .get_authorizer = get_authorizer,
2173 .verify_authorizer_reply = verify_authorizer_reply,
2174 .invalidate_authorizer = invalidate_authorizer,
2175 .alloc_msg = alloc_msg,