1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops
;
26 static void send_queued(struct ceph_osd_client
*osdc
);
27 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
);
28 static void __register_request(struct ceph_osd_client
*osdc
,
29 struct ceph_osd_request
*req
);
30 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
31 struct ceph_osd_request
*req
);
32 static void __send_request(struct ceph_osd_client
*osdc
,
33 struct ceph_osd_request
*req
);
35 static int op_needs_trail(int op
)
38 case CEPH_OSD_OP_GETXATTR
:
39 case CEPH_OSD_OP_SETXATTR
:
40 case CEPH_OSD_OP_CMPXATTR
:
41 case CEPH_OSD_OP_CALL
:
42 case CEPH_OSD_OP_NOTIFY
:
49 static int op_has_extent(int op
)
51 return (op
== CEPH_OSD_OP_READ
||
52 op
== CEPH_OSD_OP_WRITE
);
55 int ceph_calc_raw_layout(struct ceph_osd_client
*osdc
,
56 struct ceph_file_layout
*layout
,
58 u64 off
, u64
*plen
, u64
*bno
,
59 struct ceph_osd_request
*req
,
60 struct ceph_osd_req_op
*op
)
62 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
64 u64 objoff
, objlen
; /* extent in object */
67 reqhead
->snapid
= cpu_to_le64(snapid
);
70 r
= ceph_calc_file_object_mapping(layout
, off
, plen
, bno
,
75 dout(" skipping last %llu, final file extent %llu~%llu\n",
76 orig_len
- *plen
, off
, *plen
);
78 if (op_has_extent(op
->op
)) {
79 op
->extent
.offset
= objoff
;
80 op
->extent
.length
= objlen
;
82 req
->r_num_pages
= calc_pages_for(off
, *plen
);
83 req
->r_page_alignment
= off
& ~PAGE_MASK
;
84 if (op
->op
== CEPH_OSD_OP_WRITE
)
85 op
->payload_len
= *plen
;
87 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
88 *bno
, objoff
, objlen
, req
->r_num_pages
);
91 EXPORT_SYMBOL(ceph_calc_raw_layout
);
94 * Implement client access to distributed object storage cluster.
96 * All data objects are stored within a cluster/cloud of OSDs, or
97 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
98 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
99 * remote daemons serving up and coordinating consistent and safe
102 * Cluster membership and the mapping of data objects onto storage devices
103 * are described by the osd map.
105 * We keep track of pending OSD requests (read, write), resubmit
106 * requests to different OSDs when the cluster topology/data layout
107 * change, or retry the affected requests when the communications
108 * channel with an OSD is reset.
112 * calculate the mapping of a file extent onto an object, and fill out the
113 * request accordingly. shorten extent as necessary if it crosses an
116 * fill osd op in request message.
118 static int calc_layout(struct ceph_osd_client
*osdc
,
119 struct ceph_vino vino
,
120 struct ceph_file_layout
*layout
,
122 struct ceph_osd_request
*req
,
123 struct ceph_osd_req_op
*op
)
128 r
= ceph_calc_raw_layout(osdc
, layout
, vino
.snap
, off
,
129 plen
, &bno
, req
, op
);
133 snprintf(req
->r_oid
, sizeof(req
->r_oid
), "%llx.%08llx", vino
.ino
, bno
);
134 req
->r_oid_len
= strlen(req
->r_oid
);
142 void ceph_osdc_release_request(struct kref
*kref
)
144 struct ceph_osd_request
*req
= container_of(kref
,
145 struct ceph_osd_request
,
149 ceph_msg_put(req
->r_request
);
150 if (req
->r_con_filling_msg
) {
151 dout("%s revoking pages %p from con %p\n", __func__
,
152 req
->r_pages
, req
->r_con_filling_msg
);
153 ceph_msg_revoke_incoming(req
->r_reply
);
154 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
157 ceph_msg_put(req
->r_reply
);
158 if (req
->r_own_pages
)
159 ceph_release_page_vector(req
->r_pages
,
165 ceph_put_snap_context(req
->r_snapc
);
167 ceph_pagelist_release(req
->r_trail
);
171 mempool_free(req
, req
->r_osdc
->req_mempool
);
175 EXPORT_SYMBOL(ceph_osdc_release_request
);
177 static int get_num_ops(struct ceph_osd_req_op
*ops
, int *needs_trail
)
184 if (needs_trail
&& op_needs_trail(ops
[i
].op
))
192 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
194 struct ceph_snap_context
*snapc
,
195 struct ceph_osd_req_op
*ops
,
201 struct ceph_osd_request
*req
;
202 struct ceph_msg
*msg
;
204 int num_op
= get_num_ops(ops
, &needs_trail
);
205 size_t msg_size
= sizeof(struct ceph_osd_request_head
);
207 msg_size
+= num_op
*sizeof(struct ceph_osd_op
);
210 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
211 memset(req
, 0, sizeof(*req
));
213 req
= kzalloc(sizeof(*req
), gfp_flags
);
219 req
->r_mempool
= use_mempool
;
221 kref_init(&req
->r_kref
);
222 init_completion(&req
->r_completion
);
223 init_completion(&req
->r_safe_completion
);
224 RB_CLEAR_NODE(&req
->r_node
);
225 INIT_LIST_HEAD(&req
->r_unsafe_item
);
226 INIT_LIST_HEAD(&req
->r_linger_item
);
227 INIT_LIST_HEAD(&req
->r_linger_osd
);
228 INIT_LIST_HEAD(&req
->r_req_lru_item
);
229 INIT_LIST_HEAD(&req
->r_osd_item
);
231 req
->r_flags
= flags
;
233 WARN_ON((flags
& (CEPH_OSD_FLAG_READ
|CEPH_OSD_FLAG_WRITE
)) == 0);
235 /* create reply message */
237 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
239 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
240 OSD_OPREPLY_FRONT_LEN
, gfp_flags
, true);
242 ceph_osdc_put_request(req
);
247 /* allocate space for the trailing data */
249 req
->r_trail
= kmalloc(sizeof(struct ceph_pagelist
), gfp_flags
);
251 ceph_osdc_put_request(req
);
254 ceph_pagelist_init(req
->r_trail
);
257 /* create request message; allow space for oid */
258 msg_size
+= MAX_OBJ_NAME_SIZE
;
260 msg_size
+= sizeof(u64
) * snapc
->num_snaps
;
262 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
264 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp_flags
, true);
266 ceph_osdc_put_request(req
);
270 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
272 req
->r_request
= msg
;
273 req
->r_pages
= pages
;
283 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
285 static void osd_req_encode_op(struct ceph_osd_request
*req
,
286 struct ceph_osd_op
*dst
,
287 struct ceph_osd_req_op
*src
)
289 dst
->op
= cpu_to_le16(src
->op
);
292 case CEPH_OSD_OP_READ
:
293 case CEPH_OSD_OP_WRITE
:
295 cpu_to_le64(src
->extent
.offset
);
297 cpu_to_le64(src
->extent
.length
);
298 dst
->extent
.truncate_size
=
299 cpu_to_le64(src
->extent
.truncate_size
);
300 dst
->extent
.truncate_seq
=
301 cpu_to_le32(src
->extent
.truncate_seq
);
304 case CEPH_OSD_OP_GETXATTR
:
305 case CEPH_OSD_OP_SETXATTR
:
306 case CEPH_OSD_OP_CMPXATTR
:
307 BUG_ON(!req
->r_trail
);
309 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
310 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
311 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
312 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
313 ceph_pagelist_append(req
->r_trail
, src
->xattr
.name
,
314 src
->xattr
.name_len
);
315 ceph_pagelist_append(req
->r_trail
, src
->xattr
.val
,
316 src
->xattr
.value_len
);
318 case CEPH_OSD_OP_CALL
:
319 BUG_ON(!req
->r_trail
);
321 dst
->cls
.class_len
= src
->cls
.class_len
;
322 dst
->cls
.method_len
= src
->cls
.method_len
;
323 dst
->cls
.indata_len
= cpu_to_le32(src
->cls
.indata_len
);
325 ceph_pagelist_append(req
->r_trail
, src
->cls
.class_name
,
327 ceph_pagelist_append(req
->r_trail
, src
->cls
.method_name
,
328 src
->cls
.method_len
);
329 ceph_pagelist_append(req
->r_trail
, src
->cls
.indata
,
330 src
->cls
.indata_len
);
332 case CEPH_OSD_OP_ROLLBACK
:
333 dst
->snap
.snapid
= cpu_to_le64(src
->snap
.snapid
);
335 case CEPH_OSD_OP_STARTSYNC
:
337 case CEPH_OSD_OP_NOTIFY
:
339 __le32 prot_ver
= cpu_to_le32(src
->watch
.prot_ver
);
340 __le32 timeout
= cpu_to_le32(src
->watch
.timeout
);
342 BUG_ON(!req
->r_trail
);
344 ceph_pagelist_append(req
->r_trail
,
345 &prot_ver
, sizeof(prot_ver
));
346 ceph_pagelist_append(req
->r_trail
,
347 &timeout
, sizeof(timeout
));
349 case CEPH_OSD_OP_NOTIFY_ACK
:
350 case CEPH_OSD_OP_WATCH
:
351 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
352 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
353 dst
->watch
.flag
= src
->watch
.flag
;
356 pr_err("unrecognized osd opcode %d\n", dst
->op
);
360 dst
->payload_len
= cpu_to_le32(src
->payload_len
);
364 * build new request AND message
367 void ceph_osdc_build_request(struct ceph_osd_request
*req
,
369 struct ceph_osd_req_op
*src_ops
,
370 struct ceph_snap_context
*snapc
,
371 struct timespec
*mtime
,
375 struct ceph_msg
*msg
= req
->r_request
;
376 struct ceph_osd_request_head
*head
;
377 struct ceph_osd_req_op
*src_op
;
378 struct ceph_osd_op
*op
;
380 int num_op
= get_num_ops(src_ops
, NULL
);
381 size_t msg_size
= sizeof(*head
) + num_op
*sizeof(*op
);
382 int flags
= req
->r_flags
;
386 head
= msg
->front
.iov_base
;
387 op
= (void *)(head
+ 1);
388 p
= (void *)(op
+ num_op
);
390 req
->r_snapc
= ceph_get_snap_context(snapc
);
392 head
->client_inc
= cpu_to_le32(1); /* always, for now. */
393 head
->flags
= cpu_to_le32(flags
);
394 if (flags
& CEPH_OSD_FLAG_WRITE
)
395 ceph_encode_timespec(&head
->mtime
, mtime
);
396 head
->num_ops
= cpu_to_le16(num_op
);
400 head
->object_len
= cpu_to_le32(oid_len
);
401 memcpy(p
, oid
, oid_len
);
406 osd_req_encode_op(req
, op
, src_op
);
412 data_len
+= req
->r_trail
->length
;
415 head
->snap_seq
= cpu_to_le64(snapc
->seq
);
416 head
->num_snaps
= cpu_to_le32(snapc
->num_snaps
);
417 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
418 put_unaligned_le64(snapc
->snaps
[i
], p
);
423 if (flags
& CEPH_OSD_FLAG_WRITE
) {
424 req
->r_request
->hdr
.data_off
= cpu_to_le16(off
);
425 req
->r_request
->hdr
.data_len
= cpu_to_le32(*plen
+ data_len
);
426 } else if (data_len
) {
427 req
->r_request
->hdr
.data_off
= 0;
428 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
431 req
->r_request
->page_alignment
= req
->r_page_alignment
;
433 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
434 msg_size
= p
- msg
->front
.iov_base
;
435 msg
->front
.iov_len
= msg_size
;
436 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
439 EXPORT_SYMBOL(ceph_osdc_build_request
);
442 * build new request AND message, calculate layout, and adjust file
445 * if the file was recently truncated, we include information about its
446 * old and new size so that the object can be updated appropriately. (we
447 * avoid synchronously deleting truncated objects because it's slow.)
449 * if @do_sync, include a 'startsync' command so that the osd will flush
452 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
453 struct ceph_file_layout
*layout
,
454 struct ceph_vino vino
,
456 int opcode
, int flags
,
457 struct ceph_snap_context
*snapc
,
461 struct timespec
*mtime
,
462 bool use_mempool
, int num_reply
,
465 struct ceph_osd_req_op ops
[3];
466 struct ceph_osd_request
*req
;
470 ops
[0].extent
.truncate_seq
= truncate_seq
;
471 ops
[0].extent
.truncate_size
= truncate_size
;
472 ops
[0].payload_len
= 0;
475 ops
[1].op
= CEPH_OSD_OP_STARTSYNC
;
476 ops
[1].payload_len
= 0;
481 req
= ceph_osdc_alloc_request(osdc
, flags
,
484 GFP_NOFS
, NULL
, NULL
);
486 return ERR_PTR(-ENOMEM
);
488 /* calculate max write size */
489 r
= calc_layout(osdc
, vino
, layout
, off
, plen
, req
, ops
);
492 req
->r_file_layout
= *layout
; /* keep a copy */
494 /* in case it differs from natural (file) alignment that
495 calc_layout filled in for us */
496 req
->r_num_pages
= calc_pages_for(page_align
, *plen
);
497 req
->r_page_alignment
= page_align
;
499 ceph_osdc_build_request(req
, off
, plen
, ops
,
502 req
->r_oid
, req
->r_oid_len
);
506 EXPORT_SYMBOL(ceph_osdc_new_request
);
509 * We keep osd requests in an rbtree, sorted by ->r_tid.
511 static void __insert_request(struct ceph_osd_client
*osdc
,
512 struct ceph_osd_request
*new)
514 struct rb_node
**p
= &osdc
->requests
.rb_node
;
515 struct rb_node
*parent
= NULL
;
516 struct ceph_osd_request
*req
= NULL
;
520 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
521 if (new->r_tid
< req
->r_tid
)
523 else if (new->r_tid
> req
->r_tid
)
529 rb_link_node(&new->r_node
, parent
, p
);
530 rb_insert_color(&new->r_node
, &osdc
->requests
);
533 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
536 struct ceph_osd_request
*req
;
537 struct rb_node
*n
= osdc
->requests
.rb_node
;
540 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
541 if (tid
< req
->r_tid
)
543 else if (tid
> req
->r_tid
)
551 static struct ceph_osd_request
*
552 __lookup_request_ge(struct ceph_osd_client
*osdc
,
555 struct ceph_osd_request
*req
;
556 struct rb_node
*n
= osdc
->requests
.rb_node
;
559 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
560 if (tid
< req
->r_tid
) {
564 } else if (tid
> req
->r_tid
) {
574 * Resubmit requests pending on the given osd.
576 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
577 struct ceph_osd
*osd
)
579 struct ceph_osd_request
*req
, *nreq
;
582 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
583 err
= __reset_osd(osdc
, osd
);
587 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
588 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
589 dout("requeued %p tid %llu osd%d\n", req
, req
->r_tid
,
592 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
595 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
598 * reregister request prior to unregistering linger so
599 * that r_osd is preserved.
601 BUG_ON(!list_empty(&req
->r_req_lru_item
));
602 __register_request(osdc
, req
);
603 list_add(&req
->r_req_lru_item
, &osdc
->req_unsent
);
604 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
605 __unregister_linger_request(osdc
, req
);
606 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
612 * If the osd connection drops, we need to resubmit all requests.
614 static void osd_reset(struct ceph_connection
*con
)
616 struct ceph_osd
*osd
= con
->private;
617 struct ceph_osd_client
*osdc
;
621 dout("osd_reset osd%d\n", osd
->o_osd
);
623 down_read(&osdc
->map_sem
);
624 mutex_lock(&osdc
->request_mutex
);
625 __kick_osd_requests(osdc
, osd
);
626 mutex_unlock(&osdc
->request_mutex
);
628 up_read(&osdc
->map_sem
);
632 * Track open sessions with osds.
634 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
636 struct ceph_osd
*osd
;
638 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
642 atomic_set(&osd
->o_ref
, 1);
645 RB_CLEAR_NODE(&osd
->o_node
);
646 INIT_LIST_HEAD(&osd
->o_requests
);
647 INIT_LIST_HEAD(&osd
->o_linger_requests
);
648 INIT_LIST_HEAD(&osd
->o_osd_lru
);
649 osd
->o_incarnation
= 1;
651 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
653 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
657 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
659 if (atomic_inc_not_zero(&osd
->o_ref
)) {
660 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
661 atomic_read(&osd
->o_ref
));
664 dout("get_osd %p FAIL\n", osd
);
669 static void put_osd(struct ceph_osd
*osd
)
671 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
672 atomic_read(&osd
->o_ref
) - 1);
673 if (atomic_dec_and_test(&osd
->o_ref
) && osd
->o_auth
.authorizer
) {
674 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
676 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
677 ac
->ops
->destroy_authorizer(ac
, osd
->o_auth
.authorizer
);
683 * remove an osd from our map
685 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
687 dout("__remove_osd %p\n", osd
);
688 BUG_ON(!list_empty(&osd
->o_requests
));
689 rb_erase(&osd
->o_node
, &osdc
->osds
);
690 list_del_init(&osd
->o_osd_lru
);
691 ceph_con_close(&osd
->o_con
);
695 static void remove_all_osds(struct ceph_osd_client
*osdc
)
697 dout("%s %p\n", __func__
, osdc
);
698 mutex_lock(&osdc
->request_mutex
);
699 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
700 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
701 struct ceph_osd
, o_node
);
702 __remove_osd(osdc
, osd
);
704 mutex_unlock(&osdc
->request_mutex
);
707 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
708 struct ceph_osd
*osd
)
710 dout("__move_osd_to_lru %p\n", osd
);
711 BUG_ON(!list_empty(&osd
->o_osd_lru
));
712 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
713 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
716 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
718 dout("__remove_osd_from_lru %p\n", osd
);
719 if (!list_empty(&osd
->o_osd_lru
))
720 list_del_init(&osd
->o_osd_lru
);
723 static void remove_old_osds(struct ceph_osd_client
*osdc
)
725 struct ceph_osd
*osd
, *nosd
;
727 dout("__remove_old_osds %p\n", osdc
);
728 mutex_lock(&osdc
->request_mutex
);
729 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
730 if (time_before(jiffies
, osd
->lru_ttl
))
732 __remove_osd(osdc
, osd
);
734 mutex_unlock(&osdc
->request_mutex
);
740 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
742 struct ceph_osd_request
*req
;
745 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
746 if (list_empty(&osd
->o_requests
) &&
747 list_empty(&osd
->o_linger_requests
)) {
748 __remove_osd(osdc
, osd
);
750 } else if (memcmp(&osdc
->osdmap
->osd_addr
[osd
->o_osd
],
751 &osd
->o_con
.peer_addr
,
752 sizeof(osd
->o_con
.peer_addr
)) == 0 &&
753 !ceph_con_opened(&osd
->o_con
)) {
754 dout(" osd addr hasn't changed and connection never opened,"
755 " letting msgr retry");
756 /* touch each r_stamp for handle_timeout()'s benfit */
757 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
758 req
->r_stamp
= jiffies
;
761 ceph_con_close(&osd
->o_con
);
762 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
,
763 &osdc
->osdmap
->osd_addr
[osd
->o_osd
]);
764 osd
->o_incarnation
++;
769 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
771 struct rb_node
**p
= &osdc
->osds
.rb_node
;
772 struct rb_node
*parent
= NULL
;
773 struct ceph_osd
*osd
= NULL
;
775 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
778 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
779 if (new->o_osd
< osd
->o_osd
)
781 else if (new->o_osd
> osd
->o_osd
)
787 rb_link_node(&new->o_node
, parent
, p
);
788 rb_insert_color(&new->o_node
, &osdc
->osds
);
791 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
793 struct ceph_osd
*osd
;
794 struct rb_node
*n
= osdc
->osds
.rb_node
;
797 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
800 else if (o
> osd
->o_osd
)
808 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
810 schedule_delayed_work(&osdc
->timeout_work
,
811 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
814 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
816 cancel_delayed_work(&osdc
->timeout_work
);
820 * Register request, assign tid. If this is the first request, set up
823 static void __register_request(struct ceph_osd_client
*osdc
,
824 struct ceph_osd_request
*req
)
826 req
->r_tid
= ++osdc
->last_tid
;
827 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
828 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
829 __insert_request(osdc
, req
);
830 ceph_osdc_get_request(req
);
831 osdc
->num_requests
++;
832 if (osdc
->num_requests
== 1) {
833 dout(" first request, scheduling timeout\n");
834 __schedule_osd_timeout(osdc
);
838 static void register_request(struct ceph_osd_client
*osdc
,
839 struct ceph_osd_request
*req
)
841 mutex_lock(&osdc
->request_mutex
);
842 __register_request(osdc
, req
);
843 mutex_unlock(&osdc
->request_mutex
);
847 * called under osdc->request_mutex
849 static void __unregister_request(struct ceph_osd_client
*osdc
,
850 struct ceph_osd_request
*req
)
852 if (RB_EMPTY_NODE(&req
->r_node
)) {
853 dout("__unregister_request %p tid %lld not registered\n",
858 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
859 rb_erase(&req
->r_node
, &osdc
->requests
);
860 osdc
->num_requests
--;
863 /* make sure the original request isn't in flight. */
864 ceph_msg_revoke(req
->r_request
);
866 list_del_init(&req
->r_osd_item
);
867 if (list_empty(&req
->r_osd
->o_requests
) &&
868 list_empty(&req
->r_osd
->o_linger_requests
)) {
869 dout("moving osd to %p lru\n", req
->r_osd
);
870 __move_osd_to_lru(osdc
, req
->r_osd
);
872 if (list_empty(&req
->r_linger_item
))
876 list_del_init(&req
->r_req_lru_item
);
877 ceph_osdc_put_request(req
);
879 if (osdc
->num_requests
== 0) {
880 dout(" no requests, canceling timeout\n");
881 __cancel_osd_timeout(osdc
);
886 * Cancel a previously queued request message
888 static void __cancel_request(struct ceph_osd_request
*req
)
890 if (req
->r_sent
&& req
->r_osd
) {
891 ceph_msg_revoke(req
->r_request
);
896 static void __register_linger_request(struct ceph_osd_client
*osdc
,
897 struct ceph_osd_request
*req
)
899 dout("__register_linger_request %p\n", req
);
900 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
902 list_add_tail(&req
->r_linger_osd
,
903 &req
->r_osd
->o_linger_requests
);
906 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
907 struct ceph_osd_request
*req
)
909 dout("__unregister_linger_request %p\n", req
);
910 list_del_init(&req
->r_linger_item
);
912 list_del_init(&req
->r_linger_osd
);
914 if (list_empty(&req
->r_osd
->o_requests
) &&
915 list_empty(&req
->r_osd
->o_linger_requests
)) {
916 dout("moving osd to %p lru\n", req
->r_osd
);
917 __move_osd_to_lru(osdc
, req
->r_osd
);
919 if (list_empty(&req
->r_osd_item
))
924 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
925 struct ceph_osd_request
*req
)
927 mutex_lock(&osdc
->request_mutex
);
929 __unregister_linger_request(osdc
, req
);
930 ceph_osdc_put_request(req
);
932 mutex_unlock(&osdc
->request_mutex
);
934 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
936 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
937 struct ceph_osd_request
*req
)
939 if (!req
->r_linger
) {
940 dout("set_request_linger %p\n", req
);
943 * caller is now responsible for calling
944 * unregister_linger_request
946 ceph_osdc_get_request(req
);
949 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
952 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
953 * (as needed), and set the request r_osd appropriately. If there is
954 * no up osd, set r_osd to NULL. Move the request to the appropriate list
955 * (unsent, homeless) or leave on in-flight lru.
957 * Return 0 if unchanged, 1 if changed, or negative on error.
959 * Caller should hold map_sem for read and request_mutex.
961 static int __map_request(struct ceph_osd_client
*osdc
,
962 struct ceph_osd_request
*req
, int force_resend
)
964 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
966 int acting
[CEPH_PG_MAX_SIZE
];
970 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
971 err
= ceph_calc_object_layout(&reqhead
->layout
, req
->r_oid
,
972 &req
->r_file_layout
, osdc
->osdmap
);
974 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
977 pgid
= reqhead
->layout
.ol_pgid
;
980 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
986 if ((!force_resend
&&
987 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
988 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
989 req
->r_num_pg_osds
== num
&&
990 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
991 (req
->r_osd
== NULL
&& o
== -1))
992 return 0; /* no change */
994 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
995 req
->r_tid
, le32_to_cpu(pgid
.pool
), le16_to_cpu(pgid
.ps
), o
,
996 req
->r_osd
? req
->r_osd
->o_osd
: -1);
998 /* record full pg acting set */
999 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
1000 req
->r_num_pg_osds
= num
;
1003 __cancel_request(req
);
1004 list_del_init(&req
->r_osd_item
);
1008 req
->r_osd
= __lookup_osd(osdc
, o
);
1009 if (!req
->r_osd
&& o
>= 0) {
1011 req
->r_osd
= create_osd(osdc
, o
);
1013 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1017 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
1018 __insert_osd(osdc
, req
->r_osd
);
1020 ceph_con_open(&req
->r_osd
->o_con
,
1021 CEPH_ENTITY_TYPE_OSD
, o
,
1022 &osdc
->osdmap
->osd_addr
[o
]);
1026 __remove_osd_from_lru(req
->r_osd
);
1027 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1028 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1030 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1032 err
= 1; /* osd or pg changed */
1039 * caller should hold map_sem (for read) and request_mutex
1041 static void __send_request(struct ceph_osd_client
*osdc
,
1042 struct ceph_osd_request
*req
)
1044 struct ceph_osd_request_head
*reqhead
;
1046 dout("send_request %p tid %llu to osd%d flags %d\n",
1047 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
);
1049 reqhead
= req
->r_request
->front
.iov_base
;
1050 reqhead
->osdmap_epoch
= cpu_to_le32(osdc
->osdmap
->epoch
);
1051 reqhead
->flags
|= cpu_to_le32(req
->r_flags
); /* e.g., RETRY */
1052 reqhead
->reassert_version
= req
->r_reassert_version
;
1054 req
->r_stamp
= jiffies
;
1055 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1057 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1058 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1059 req
->r_sent
= req
->r_osd
->o_incarnation
;
1063 * Send any requests in the queue (req_unsent).
1065 static void send_queued(struct ceph_osd_client
*osdc
)
1067 struct ceph_osd_request
*req
, *tmp
;
1069 dout("send_queued\n");
1070 mutex_lock(&osdc
->request_mutex
);
1071 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
) {
1072 __send_request(osdc
, req
);
1074 mutex_unlock(&osdc
->request_mutex
);
1078 * Timeout callback, called every N seconds when 1 or more osd
1079 * requests has been active for more than N seconds. When this
1080 * happens, we ping all OSDs with requests who have timed out to
1081 * ensure any communications channel reset is detected. Reset the
1082 * request timeouts another N seconds in the future as we go.
1083 * Reschedule the timeout event another N seconds in future (unless
1084 * there are no open requests).
1086 static void handle_timeout(struct work_struct
*work
)
1088 struct ceph_osd_client
*osdc
=
1089 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1090 struct ceph_osd_request
*req
;
1091 struct ceph_osd
*osd
;
1092 unsigned long keepalive
=
1093 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1094 struct list_head slow_osds
;
1096 down_read(&osdc
->map_sem
);
1098 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1100 mutex_lock(&osdc
->request_mutex
);
1103 * ping osds that are a bit slow. this ensures that if there
1104 * is a break in the TCP connection we will notice, and reopen
1105 * a connection with that osd (from the fault callback).
1107 INIT_LIST_HEAD(&slow_osds
);
1108 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1109 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1114 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1115 req
->r_tid
, osd
->o_osd
);
1116 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1118 while (!list_empty(&slow_osds
)) {
1119 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1121 list_del_init(&osd
->o_keepalive_item
);
1122 ceph_con_keepalive(&osd
->o_con
);
1125 __schedule_osd_timeout(osdc
);
1126 mutex_unlock(&osdc
->request_mutex
);
1128 up_read(&osdc
->map_sem
);
1131 static void handle_osds_timeout(struct work_struct
*work
)
1133 struct ceph_osd_client
*osdc
=
1134 container_of(work
, struct ceph_osd_client
,
1135 osds_timeout_work
.work
);
1136 unsigned long delay
=
1137 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1139 dout("osds timeout\n");
1140 down_read(&osdc
->map_sem
);
1141 remove_old_osds(osdc
);
1142 up_read(&osdc
->map_sem
);
1144 schedule_delayed_work(&osdc
->osds_timeout_work
,
1145 round_jiffies_relative(delay
));
1148 static void complete_request(struct ceph_osd_request
*req
)
1150 if (req
->r_safe_callback
)
1151 req
->r_safe_callback(req
, NULL
);
1152 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1156 * handle osd op reply. either call the callback if it is specified,
1157 * or do the completion to wake up the waiting thread.
1159 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1160 struct ceph_connection
*con
)
1162 struct ceph_osd_reply_head
*rhead
= msg
->front
.iov_base
;
1163 struct ceph_osd_request
*req
;
1165 int numops
, object_len
, flags
;
1168 tid
= le64_to_cpu(msg
->hdr
.tid
);
1169 if (msg
->front
.iov_len
< sizeof(*rhead
))
1171 numops
= le32_to_cpu(rhead
->num_ops
);
1172 object_len
= le32_to_cpu(rhead
->object_len
);
1173 result
= le32_to_cpu(rhead
->result
);
1174 if (msg
->front
.iov_len
!= sizeof(*rhead
) + object_len
+
1175 numops
* sizeof(struct ceph_osd_op
))
1177 dout("handle_reply %p tid %llu result %d\n", msg
, tid
, (int)result
);
1179 mutex_lock(&osdc
->request_mutex
);
1180 req
= __lookup_request(osdc
, tid
);
1182 dout("handle_reply tid %llu dne\n", tid
);
1183 mutex_unlock(&osdc
->request_mutex
);
1186 ceph_osdc_get_request(req
);
1187 flags
= le32_to_cpu(rhead
->flags
);
1190 * if this connection filled our message, drop our reference now, to
1191 * avoid a (safe but slower) revoke later.
1193 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
1194 dout(" dropping con_filling_msg ref %p\n", con
);
1195 req
->r_con_filling_msg
= NULL
;
1199 if (!req
->r_got_reply
) {
1202 req
->r_result
= le32_to_cpu(rhead
->result
);
1203 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1204 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1206 if (req
->r_result
== 0)
1207 req
->r_result
= bytes
;
1209 /* in case this is a write and we need to replay, */
1210 req
->r_reassert_version
= rhead
->reassert_version
;
1212 req
->r_got_reply
= 1;
1213 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1214 dout("handle_reply tid %llu dup ack\n", tid
);
1215 mutex_unlock(&osdc
->request_mutex
);
1219 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1221 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1222 __register_linger_request(osdc
, req
);
1224 /* either this is a read, or we got the safe response */
1226 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1227 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1228 __unregister_request(osdc
, req
);
1230 mutex_unlock(&osdc
->request_mutex
);
1232 if (req
->r_callback
)
1233 req
->r_callback(req
, msg
);
1235 complete_all(&req
->r_completion
);
1237 if (flags
& CEPH_OSD_FLAG_ONDISK
)
1238 complete_request(req
);
1241 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1242 ceph_osdc_put_request(req
);
1246 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1247 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
),
1248 (int)sizeof(*rhead
));
1252 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1254 struct rb_node
*p
, *n
;
1256 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1257 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1260 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1261 memcmp(&osd
->o_con
.peer_addr
,
1262 ceph_osd_addr(osdc
->osdmap
,
1264 sizeof(struct ceph_entity_addr
)) != 0)
1265 __reset_osd(osdc
, osd
);
1270 * Requeue requests whose mapping to an OSD has changed. If requests map to
1271 * no osd, request a new map.
1273 * Caller should hold map_sem for read.
1275 static void kick_requests(struct ceph_osd_client
*osdc
, int force_resend
)
1277 struct ceph_osd_request
*req
, *nreq
;
1282 dout("kick_requests %s\n", force_resend
? " (force resend)" : "");
1283 mutex_lock(&osdc
->request_mutex
);
1284 for (p
= rb_first(&osdc
->requests
); p
; ) {
1285 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1289 * For linger requests that have not yet been
1290 * registered, move them to the linger list; they'll
1291 * be sent to the osd in the loop below. Unregister
1292 * the request before re-registering it as a linger
1293 * request to ensure the __map_request() below
1294 * will decide it needs to be sent.
1296 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
1297 dout("%p tid %llu restart on osd%d\n",
1299 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1300 __unregister_request(osdc
, req
);
1301 __register_linger_request(osdc
, req
);
1305 err
= __map_request(osdc
, req
, force_resend
);
1307 continue; /* error */
1308 if (req
->r_osd
== NULL
) {
1309 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1310 needmap
++; /* request a newer map */
1311 } else if (err
> 0) {
1312 if (!req
->r_linger
) {
1313 dout("%p tid %llu requeued on osd%d\n", req
,
1315 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1316 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1321 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1323 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1325 err
= __map_request(osdc
, req
, force_resend
);
1326 dout("__map_request returned %d\n", err
);
1328 continue; /* no change and no osd was specified */
1330 continue; /* hrm! */
1331 if (req
->r_osd
== NULL
) {
1332 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1333 needmap
++; /* request a newer map */
1337 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1338 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1339 __register_request(osdc
, req
);
1340 __unregister_linger_request(osdc
, req
);
1342 mutex_unlock(&osdc
->request_mutex
);
1345 dout("%d requests for down osds, need new map\n", needmap
);
1346 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1348 reset_changed_osds(osdc
);
1353 * Process updated osd map.
1355 * The message contains any number of incremental and full maps, normally
1356 * indicating some sort of topology change in the cluster. Kick requests
1357 * off to different OSDs as needed.
1359 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1361 void *p
, *end
, *next
;
1362 u32 nr_maps
, maplen
;
1364 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1366 struct ceph_fsid fsid
;
1368 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1369 p
= msg
->front
.iov_base
;
1370 end
= p
+ msg
->front
.iov_len
;
1373 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1374 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1375 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1378 down_write(&osdc
->map_sem
);
1380 /* incremental maps */
1381 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1382 dout(" %d inc maps\n", nr_maps
);
1383 while (nr_maps
> 0) {
1384 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1385 epoch
= ceph_decode_32(&p
);
1386 maplen
= ceph_decode_32(&p
);
1387 ceph_decode_need(&p
, end
, maplen
, bad
);
1389 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1390 dout("applying incremental map %u len %d\n",
1392 newmap
= osdmap_apply_incremental(&p
, next
,
1394 &osdc
->client
->msgr
);
1395 if (IS_ERR(newmap
)) {
1396 err
= PTR_ERR(newmap
);
1400 if (newmap
!= osdc
->osdmap
) {
1401 ceph_osdmap_destroy(osdc
->osdmap
);
1402 osdc
->osdmap
= newmap
;
1404 kick_requests(osdc
, 0);
1406 dout("ignoring incremental map %u len %d\n",
1416 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1417 dout(" %d full maps\n", nr_maps
);
1419 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1420 epoch
= ceph_decode_32(&p
);
1421 maplen
= ceph_decode_32(&p
);
1422 ceph_decode_need(&p
, end
, maplen
, bad
);
1424 dout("skipping non-latest full map %u len %d\n",
1426 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1427 dout("skipping full map %u len %d, "
1428 "older than our %u\n", epoch
, maplen
,
1429 osdc
->osdmap
->epoch
);
1431 int skipped_map
= 0;
1433 dout("taking full map %u len %d\n", epoch
, maplen
);
1434 newmap
= osdmap_decode(&p
, p
+maplen
);
1435 if (IS_ERR(newmap
)) {
1436 err
= PTR_ERR(newmap
);
1440 oldmap
= osdc
->osdmap
;
1441 osdc
->osdmap
= newmap
;
1443 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
1445 ceph_osdmap_destroy(oldmap
);
1447 kick_requests(osdc
, skipped_map
);
1454 downgrade_write(&osdc
->map_sem
);
1455 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1458 * subscribe to subsequent osdmap updates if full to ensure
1459 * we find out when we are no longer full and stop returning
1462 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
))
1463 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1466 up_read(&osdc
->map_sem
);
1467 wake_up_all(&osdc
->client
->auth_wq
);
1471 pr_err("osdc handle_map corrupt msg\n");
1473 up_write(&osdc
->map_sem
);
1478 * watch/notify callback event infrastructure
1480 * These callbacks are used both for watch and notify operations.
1482 static void __release_event(struct kref
*kref
)
1484 struct ceph_osd_event
*event
=
1485 container_of(kref
, struct ceph_osd_event
, kref
);
1487 dout("__release_event %p\n", event
);
1491 static void get_event(struct ceph_osd_event
*event
)
1493 kref_get(&event
->kref
);
1496 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1498 kref_put(&event
->kref
, __release_event
);
1500 EXPORT_SYMBOL(ceph_osdc_put_event
);
1502 static void __insert_event(struct ceph_osd_client
*osdc
,
1503 struct ceph_osd_event
*new)
1505 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1506 struct rb_node
*parent
= NULL
;
1507 struct ceph_osd_event
*event
= NULL
;
1511 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1512 if (new->cookie
< event
->cookie
)
1514 else if (new->cookie
> event
->cookie
)
1515 p
= &(*p
)->rb_right
;
1520 rb_link_node(&new->node
, parent
, p
);
1521 rb_insert_color(&new->node
, &osdc
->event_tree
);
1524 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1527 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1528 struct rb_node
*parent
= NULL
;
1529 struct ceph_osd_event
*event
= NULL
;
1533 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1534 if (cookie
< event
->cookie
)
1536 else if (cookie
> event
->cookie
)
1537 p
= &(*p
)->rb_right
;
1544 static void __remove_event(struct ceph_osd_event
*event
)
1546 struct ceph_osd_client
*osdc
= event
->osdc
;
1548 if (!RB_EMPTY_NODE(&event
->node
)) {
1549 dout("__remove_event removed %p\n", event
);
1550 rb_erase(&event
->node
, &osdc
->event_tree
);
1551 ceph_osdc_put_event(event
);
1553 dout("__remove_event didn't remove %p\n", event
);
1557 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1558 void (*event_cb
)(u64
, u64
, u8
, void *),
1559 int one_shot
, void *data
,
1560 struct ceph_osd_event
**pevent
)
1562 struct ceph_osd_event
*event
;
1564 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1568 dout("create_event %p\n", event
);
1569 event
->cb
= event_cb
;
1570 event
->one_shot
= one_shot
;
1573 INIT_LIST_HEAD(&event
->osd_node
);
1574 RB_CLEAR_NODE(&event
->node
);
1575 kref_init(&event
->kref
); /* one ref for us */
1576 kref_get(&event
->kref
); /* one ref for the caller */
1577 init_completion(&event
->completion
);
1579 spin_lock(&osdc
->event_lock
);
1580 event
->cookie
= ++osdc
->event_count
;
1581 __insert_event(osdc
, event
);
1582 spin_unlock(&osdc
->event_lock
);
1587 EXPORT_SYMBOL(ceph_osdc_create_event
);
1589 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1591 struct ceph_osd_client
*osdc
= event
->osdc
;
1593 dout("cancel_event %p\n", event
);
1594 spin_lock(&osdc
->event_lock
);
1595 __remove_event(event
);
1596 spin_unlock(&osdc
->event_lock
);
1597 ceph_osdc_put_event(event
); /* caller's */
1599 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1602 static void do_event_work(struct work_struct
*work
)
1604 struct ceph_osd_event_work
*event_work
=
1605 container_of(work
, struct ceph_osd_event_work
, work
);
1606 struct ceph_osd_event
*event
= event_work
->event
;
1607 u64 ver
= event_work
->ver
;
1608 u64 notify_id
= event_work
->notify_id
;
1609 u8 opcode
= event_work
->opcode
;
1611 dout("do_event_work completing %p\n", event
);
1612 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1613 complete(&event
->completion
);
1614 dout("do_event_work completed %p\n", event
);
1615 ceph_osdc_put_event(event
);
1621 * Process osd watch notifications
1623 void handle_watch_notify(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1627 u64 cookie
, ver
, notify_id
;
1629 struct ceph_osd_event
*event
;
1630 struct ceph_osd_event_work
*event_work
;
1632 p
= msg
->front
.iov_base
;
1633 end
= p
+ msg
->front
.iov_len
;
1635 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1636 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1637 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1638 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1639 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1641 spin_lock(&osdc
->event_lock
);
1642 event
= __find_event(osdc
, cookie
);
1645 if (event
->one_shot
)
1646 __remove_event(event
);
1648 spin_unlock(&osdc
->event_lock
);
1649 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1650 cookie
, ver
, event
);
1652 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1654 dout("ERROR: could not allocate event_work\n");
1657 INIT_WORK(&event_work
->work
, do_event_work
);
1658 event_work
->event
= event
;
1659 event_work
->ver
= ver
;
1660 event_work
->notify_id
= notify_id
;
1661 event_work
->opcode
= opcode
;
1662 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1663 dout("WARNING: failed to queue notify event work\n");
1671 complete(&event
->completion
);
1672 ceph_osdc_put_event(event
);
1676 pr_err("osdc handle_watch_notify corrupt msg\n");
1680 int ceph_osdc_wait_event(struct ceph_osd_event
*event
, unsigned long timeout
)
1684 dout("wait_event %p\n", event
);
1685 err
= wait_for_completion_interruptible_timeout(&event
->completion
,
1687 ceph_osdc_put_event(event
);
1690 dout("wait_event %p returns %d\n", event
, err
);
1693 EXPORT_SYMBOL(ceph_osdc_wait_event
);
1696 * Register request, send initial attempt.
1698 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1699 struct ceph_osd_request
*req
,
1704 req
->r_request
->pages
= req
->r_pages
;
1705 req
->r_request
->nr_pages
= req
->r_num_pages
;
1707 req
->r_request
->bio
= req
->r_bio
;
1709 req
->r_request
->trail
= req
->r_trail
;
1711 register_request(osdc
, req
);
1713 down_read(&osdc
->map_sem
);
1714 mutex_lock(&osdc
->request_mutex
);
1716 * a racing kick_requests() may have sent the message for us
1717 * while we dropped request_mutex above, so only send now if
1718 * the request still han't been touched yet.
1720 if (req
->r_sent
== 0) {
1721 rc
= __map_request(osdc
, req
, 0);
1724 dout("osdc_start_request failed map, "
1725 " will retry %lld\n", req
->r_tid
);
1730 if (req
->r_osd
== NULL
) {
1731 dout("send_request %p no up osds in pg\n", req
);
1732 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1734 __send_request(osdc
, req
);
1740 mutex_unlock(&osdc
->request_mutex
);
1741 up_read(&osdc
->map_sem
);
1744 EXPORT_SYMBOL(ceph_osdc_start_request
);
1747 * wait for a request to complete
1749 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1750 struct ceph_osd_request
*req
)
1754 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1756 mutex_lock(&osdc
->request_mutex
);
1757 __cancel_request(req
);
1758 __unregister_request(osdc
, req
);
1759 mutex_unlock(&osdc
->request_mutex
);
1760 complete_request(req
);
1761 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1765 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1766 return req
->r_result
;
1768 EXPORT_SYMBOL(ceph_osdc_wait_request
);
1771 * sync - wait for all in-flight requests to flush. avoid starvation.
1773 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1775 struct ceph_osd_request
*req
;
1776 u64 last_tid
, next_tid
= 0;
1778 mutex_lock(&osdc
->request_mutex
);
1779 last_tid
= osdc
->last_tid
;
1781 req
= __lookup_request_ge(osdc
, next_tid
);
1784 if (req
->r_tid
> last_tid
)
1787 next_tid
= req
->r_tid
+ 1;
1788 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1791 ceph_osdc_get_request(req
);
1792 mutex_unlock(&osdc
->request_mutex
);
1793 dout("sync waiting on tid %llu (last is %llu)\n",
1794 req
->r_tid
, last_tid
);
1795 wait_for_completion(&req
->r_safe_completion
);
1796 mutex_lock(&osdc
->request_mutex
);
1797 ceph_osdc_put_request(req
);
1799 mutex_unlock(&osdc
->request_mutex
);
1800 dout("sync done (thru tid %llu)\n", last_tid
);
1802 EXPORT_SYMBOL(ceph_osdc_sync
);
1807 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
1812 osdc
->client
= client
;
1813 osdc
->osdmap
= NULL
;
1814 init_rwsem(&osdc
->map_sem
);
1815 init_completion(&osdc
->map_waiters
);
1816 osdc
->last_requested_map
= 0;
1817 mutex_init(&osdc
->request_mutex
);
1819 osdc
->osds
= RB_ROOT
;
1820 INIT_LIST_HEAD(&osdc
->osd_lru
);
1821 osdc
->requests
= RB_ROOT
;
1822 INIT_LIST_HEAD(&osdc
->req_lru
);
1823 INIT_LIST_HEAD(&osdc
->req_unsent
);
1824 INIT_LIST_HEAD(&osdc
->req_notarget
);
1825 INIT_LIST_HEAD(&osdc
->req_linger
);
1826 osdc
->num_requests
= 0;
1827 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
1828 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
1829 spin_lock_init(&osdc
->event_lock
);
1830 osdc
->event_tree
= RB_ROOT
;
1831 osdc
->event_count
= 0;
1833 schedule_delayed_work(&osdc
->osds_timeout_work
,
1834 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
1837 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
1838 sizeof(struct ceph_osd_request
));
1839 if (!osdc
->req_mempool
)
1842 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
1843 OSD_OP_FRONT_LEN
, 10, true,
1847 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
1848 OSD_OPREPLY_FRONT_LEN
, 10, true,
1853 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
1854 if (IS_ERR(osdc
->notify_wq
)) {
1855 err
= PTR_ERR(osdc
->notify_wq
);
1856 osdc
->notify_wq
= NULL
;
1862 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1864 mempool_destroy(osdc
->req_mempool
);
1868 EXPORT_SYMBOL(ceph_osdc_init
);
1870 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
1872 flush_workqueue(osdc
->notify_wq
);
1873 destroy_workqueue(osdc
->notify_wq
);
1874 cancel_delayed_work_sync(&osdc
->timeout_work
);
1875 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
1877 ceph_osdmap_destroy(osdc
->osdmap
);
1878 osdc
->osdmap
= NULL
;
1880 remove_all_osds(osdc
);
1881 mempool_destroy(osdc
->req_mempool
);
1882 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1883 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
1885 EXPORT_SYMBOL(ceph_osdc_stop
);
1888 * Read some contiguous pages. If we cross a stripe boundary, shorten
1889 * *plen. Return number of bytes read, or error.
1891 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
1892 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
1894 u32 truncate_seq
, u64 truncate_size
,
1895 struct page
**pages
, int num_pages
, int page_align
)
1897 struct ceph_osd_request
*req
;
1900 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
1901 vino
.snap
, off
, *plen
);
1902 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
1903 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
1904 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
1905 false, 1, page_align
);
1907 return PTR_ERR(req
);
1909 /* it may be a short read due to an object boundary */
1910 req
->r_pages
= pages
;
1912 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1913 off
, *plen
, req
->r_num_pages
, page_align
);
1915 rc
= ceph_osdc_start_request(osdc
, req
, false);
1917 rc
= ceph_osdc_wait_request(osdc
, req
);
1919 ceph_osdc_put_request(req
);
1920 dout("readpages result %d\n", rc
);
1923 EXPORT_SYMBOL(ceph_osdc_readpages
);
1926 * do a synchronous write on N pages
1928 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
1929 struct ceph_file_layout
*layout
,
1930 struct ceph_snap_context
*snapc
,
1932 u32 truncate_seq
, u64 truncate_size
,
1933 struct timespec
*mtime
,
1934 struct page
**pages
, int num_pages
,
1935 int flags
, int do_sync
, bool nofail
)
1937 struct ceph_osd_request
*req
;
1939 int page_align
= off
& ~PAGE_MASK
;
1941 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
1942 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
1944 flags
| CEPH_OSD_FLAG_ONDISK
|
1945 CEPH_OSD_FLAG_WRITE
,
1947 truncate_seq
, truncate_size
, mtime
,
1948 nofail
, 1, page_align
);
1950 return PTR_ERR(req
);
1952 /* it may be a short write due to an object boundary */
1953 req
->r_pages
= pages
;
1954 dout("writepages %llu~%llu (%d pages)\n", off
, len
,
1957 rc
= ceph_osdc_start_request(osdc
, req
, nofail
);
1959 rc
= ceph_osdc_wait_request(osdc
, req
);
1961 ceph_osdc_put_request(req
);
1964 dout("writepages result %d\n", rc
);
1967 EXPORT_SYMBOL(ceph_osdc_writepages
);
1970 * handle incoming message
1972 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1974 struct ceph_osd
*osd
= con
->private;
1975 struct ceph_osd_client
*osdc
;
1976 int type
= le16_to_cpu(msg
->hdr
.type
);
1983 case CEPH_MSG_OSD_MAP
:
1984 ceph_osdc_handle_map(osdc
, msg
);
1986 case CEPH_MSG_OSD_OPREPLY
:
1987 handle_reply(osdc
, msg
, con
);
1989 case CEPH_MSG_WATCH_NOTIFY
:
1990 handle_watch_notify(osdc
, msg
);
1994 pr_err("received unknown message type %d %s\n", type
,
1995 ceph_msg_type_name(type
));
2002 * lookup and return message for incoming reply. set up reply message
2005 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2006 struct ceph_msg_header
*hdr
,
2009 struct ceph_osd
*osd
= con
->private;
2010 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2012 struct ceph_osd_request
*req
;
2013 int front
= le32_to_cpu(hdr
->front_len
);
2014 int data_len
= le32_to_cpu(hdr
->data_len
);
2017 tid
= le64_to_cpu(hdr
->tid
);
2018 mutex_lock(&osdc
->request_mutex
);
2019 req
= __lookup_request(osdc
, tid
);
2023 dout("get_reply unknown tid %llu from osd%d\n", tid
,
2028 if (req
->r_con_filling_msg
) {
2029 dout("%s revoking msg %p from old con %p\n", __func__
,
2030 req
->r_reply
, req
->r_con_filling_msg
);
2031 ceph_msg_revoke_incoming(req
->r_reply
);
2032 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
2033 req
->r_con_filling_msg
= NULL
;
2036 if (front
> req
->r_reply
->front
.iov_len
) {
2037 pr_warning("get_reply front %d > preallocated %d\n",
2038 front
, (int)req
->r_reply
->front
.iov_len
);
2039 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
, false);
2042 ceph_msg_put(req
->r_reply
);
2045 m
= ceph_msg_get(req
->r_reply
);
2048 int want
= calc_pages_for(req
->r_page_alignment
, data_len
);
2050 if (unlikely(req
->r_num_pages
< want
)) {
2051 pr_warning("tid %lld reply has %d bytes %d pages, we"
2052 " had only %d pages ready\n", tid
, data_len
,
2053 want
, req
->r_num_pages
);
2059 m
->pages
= req
->r_pages
;
2060 m
->nr_pages
= req
->r_num_pages
;
2061 m
->page_alignment
= req
->r_page_alignment
;
2063 m
->bio
= req
->r_bio
;
2067 req
->r_con_filling_msg
= con
->ops
->get(con
);
2068 dout("get_reply tid %lld %p\n", tid
, m
);
2071 mutex_unlock(&osdc
->request_mutex
);
2076 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2077 struct ceph_msg_header
*hdr
,
2080 struct ceph_osd
*osd
= con
->private;
2081 int type
= le16_to_cpu(hdr
->type
);
2082 int front
= le32_to_cpu(hdr
->front_len
);
2086 case CEPH_MSG_OSD_MAP
:
2087 case CEPH_MSG_WATCH_NOTIFY
:
2088 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2089 case CEPH_MSG_OSD_OPREPLY
:
2090 return get_reply(con
, hdr
, skip
);
2092 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2100 * Wrappers to refcount containing ceph_osd struct
2102 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2104 struct ceph_osd
*osd
= con
->private;
2110 static void put_osd_con(struct ceph_connection
*con
)
2112 struct ceph_osd
*osd
= con
->private;
2120 * Note: returned pointer is the address of a structure that's
2121 * managed separately. Caller must *not* attempt to free it.
2123 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2124 int *proto
, int force_new
)
2126 struct ceph_osd
*o
= con
->private;
2127 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2128 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2129 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2131 if (force_new
&& auth
->authorizer
) {
2132 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
2133 ac
->ops
->destroy_authorizer(ac
, auth
->authorizer
);
2134 auth
->authorizer
= NULL
;
2136 if (!auth
->authorizer
&& ac
->ops
&& ac
->ops
->create_authorizer
) {
2137 int ret
= ac
->ops
->create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2140 return ERR_PTR(ret
);
2142 *proto
= ac
->protocol
;
2148 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2150 struct ceph_osd
*o
= con
->private;
2151 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2152 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2155 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2156 * XXX which do we do: succeed or fail?
2158 return ac
->ops
->verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
2161 static int invalidate_authorizer(struct ceph_connection
*con
)
2163 struct ceph_osd
*o
= con
->private;
2164 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2165 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2167 if (ac
->ops
&& ac
->ops
->invalidate_authorizer
)
2168 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2170 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2173 static const struct ceph_connection_operations osd_con_ops
= {
2176 .dispatch
= dispatch
,
2177 .get_authorizer
= get_authorizer
,
2178 .verify_authorizer_reply
= verify_authorizer_reply
,
2179 .invalidate_authorizer
= invalidate_authorizer
,
2180 .alloc_msg
= alloc_msg
,