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_init_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
;
469 ops
[0].extent
.truncate_seq
= truncate_seq
;
470 ops
[0].extent
.truncate_size
= truncate_size
;
471 ops
[0].payload_len
= 0;
474 ops
[1].op
= CEPH_OSD_OP_STARTSYNC
;
475 ops
[1].payload_len
= 0;
480 req
= ceph_osdc_alloc_request(osdc
, flags
,
483 GFP_NOFS
, NULL
, NULL
);
487 /* calculate max write size */
488 calc_layout(osdc
, vino
, layout
, off
, plen
, req
, ops
);
489 req
->r_file_layout
= *layout
; /* keep a copy */
491 /* in case it differs from natural (file) alignment that
492 calc_layout filled in for us */
493 req
->r_num_pages
= calc_pages_for(page_align
, *plen
);
494 req
->r_page_alignment
= page_align
;
496 ceph_osdc_build_request(req
, off
, plen
, ops
,
499 req
->r_oid
, req
->r_oid_len
);
503 EXPORT_SYMBOL(ceph_osdc_new_request
);
506 * We keep osd requests in an rbtree, sorted by ->r_tid.
508 static void __insert_request(struct ceph_osd_client
*osdc
,
509 struct ceph_osd_request
*new)
511 struct rb_node
**p
= &osdc
->requests
.rb_node
;
512 struct rb_node
*parent
= NULL
;
513 struct ceph_osd_request
*req
= NULL
;
517 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
518 if (new->r_tid
< req
->r_tid
)
520 else if (new->r_tid
> req
->r_tid
)
526 rb_link_node(&new->r_node
, parent
, p
);
527 rb_insert_color(&new->r_node
, &osdc
->requests
);
530 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
533 struct ceph_osd_request
*req
;
534 struct rb_node
*n
= osdc
->requests
.rb_node
;
537 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
538 if (tid
< req
->r_tid
)
540 else if (tid
> req
->r_tid
)
548 static struct ceph_osd_request
*
549 __lookup_request_ge(struct ceph_osd_client
*osdc
,
552 struct ceph_osd_request
*req
;
553 struct rb_node
*n
= osdc
->requests
.rb_node
;
556 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
557 if (tid
< req
->r_tid
) {
561 } else if (tid
> req
->r_tid
) {
571 * Resubmit requests pending on the given osd.
573 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
574 struct ceph_osd
*osd
)
576 struct ceph_osd_request
*req
, *nreq
;
579 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
580 err
= __reset_osd(osdc
, osd
);
584 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
585 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
586 dout("requeued %p tid %llu osd%d\n", req
, req
->r_tid
,
589 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
592 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
595 * reregister request prior to unregistering linger so
596 * that r_osd is preserved.
598 BUG_ON(!list_empty(&req
->r_req_lru_item
));
599 __register_request(osdc
, req
);
600 list_add(&req
->r_req_lru_item
, &osdc
->req_unsent
);
601 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
602 __unregister_linger_request(osdc
, req
);
603 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
608 static void kick_osd_requests(struct ceph_osd_client
*osdc
,
609 struct ceph_osd
*kickosd
)
611 mutex_lock(&osdc
->request_mutex
);
612 __kick_osd_requests(osdc
, kickosd
);
613 mutex_unlock(&osdc
->request_mutex
);
617 * If the osd connection drops, we need to resubmit all requests.
619 static void osd_reset(struct ceph_connection
*con
)
621 struct ceph_osd
*osd
= con
->private;
622 struct ceph_osd_client
*osdc
;
626 dout("osd_reset osd%d\n", osd
->o_osd
);
628 down_read(&osdc
->map_sem
);
629 kick_osd_requests(osdc
, osd
);
631 up_read(&osdc
->map_sem
);
635 * Track open sessions with osds.
637 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
639 struct ceph_osd
*osd
;
641 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
645 atomic_set(&osd
->o_ref
, 1);
648 INIT_LIST_HEAD(&osd
->o_requests
);
649 INIT_LIST_HEAD(&osd
->o_linger_requests
);
650 INIT_LIST_HEAD(&osd
->o_osd_lru
);
651 osd
->o_incarnation
= 1;
653 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
655 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
659 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
661 if (atomic_inc_not_zero(&osd
->o_ref
)) {
662 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
663 atomic_read(&osd
->o_ref
));
666 dout("get_osd %p FAIL\n", osd
);
671 static void put_osd(struct ceph_osd
*osd
)
673 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
674 atomic_read(&osd
->o_ref
) - 1);
675 if (atomic_dec_and_test(&osd
->o_ref
) && osd
->o_auth
.authorizer
) {
676 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
678 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
679 ac
->ops
->destroy_authorizer(ac
, osd
->o_auth
.authorizer
);
685 * remove an osd from our map
687 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
689 dout("__remove_osd %p\n", osd
);
690 BUG_ON(!list_empty(&osd
->o_requests
));
691 rb_erase(&osd
->o_node
, &osdc
->osds
);
692 list_del_init(&osd
->o_osd_lru
);
693 ceph_con_close(&osd
->o_con
);
697 static void remove_all_osds(struct ceph_osd_client
*osdc
)
699 dout("%s %p\n", __func__
, osdc
);
700 mutex_lock(&osdc
->request_mutex
);
701 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
702 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
703 struct ceph_osd
, o_node
);
704 __remove_osd(osdc
, osd
);
706 mutex_unlock(&osdc
->request_mutex
);
709 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
710 struct ceph_osd
*osd
)
712 dout("__move_osd_to_lru %p\n", osd
);
713 BUG_ON(!list_empty(&osd
->o_osd_lru
));
714 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
715 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
718 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
720 dout("__remove_osd_from_lru %p\n", osd
);
721 if (!list_empty(&osd
->o_osd_lru
))
722 list_del_init(&osd
->o_osd_lru
);
725 static void remove_old_osds(struct ceph_osd_client
*osdc
)
727 struct ceph_osd
*osd
, *nosd
;
729 dout("__remove_old_osds %p\n", osdc
);
730 mutex_lock(&osdc
->request_mutex
);
731 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
732 if (time_before(jiffies
, osd
->lru_ttl
))
734 __remove_osd(osdc
, osd
);
736 mutex_unlock(&osdc
->request_mutex
);
742 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
744 struct ceph_osd_request
*req
;
747 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
748 if (list_empty(&osd
->o_requests
) &&
749 list_empty(&osd
->o_linger_requests
)) {
750 __remove_osd(osdc
, osd
);
751 } else if (memcmp(&osdc
->osdmap
->osd_addr
[osd
->o_osd
],
752 &osd
->o_con
.peer_addr
,
753 sizeof(osd
->o_con
.peer_addr
)) == 0 &&
754 !ceph_con_opened(&osd
->o_con
)) {
755 dout(" osd addr hasn't changed and connection never opened,"
756 " letting msgr retry");
757 /* touch each r_stamp for handle_timeout()'s benfit */
758 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
759 req
->r_stamp
= jiffies
;
762 ceph_con_close(&osd
->o_con
);
763 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
,
764 &osdc
->osdmap
->osd_addr
[osd
->o_osd
]);
765 osd
->o_incarnation
++;
770 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
772 struct rb_node
**p
= &osdc
->osds
.rb_node
;
773 struct rb_node
*parent
= NULL
;
774 struct ceph_osd
*osd
= NULL
;
776 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
779 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
780 if (new->o_osd
< osd
->o_osd
)
782 else if (new->o_osd
> osd
->o_osd
)
788 rb_link_node(&new->o_node
, parent
, p
);
789 rb_insert_color(&new->o_node
, &osdc
->osds
);
792 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
794 struct ceph_osd
*osd
;
795 struct rb_node
*n
= osdc
->osds
.rb_node
;
798 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
801 else if (o
> osd
->o_osd
)
809 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
811 schedule_delayed_work(&osdc
->timeout_work
,
812 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
815 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
817 cancel_delayed_work(&osdc
->timeout_work
);
821 * Register request, assign tid. If this is the first request, set up
824 static void __register_request(struct ceph_osd_client
*osdc
,
825 struct ceph_osd_request
*req
)
827 req
->r_tid
= ++osdc
->last_tid
;
828 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
829 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
830 __insert_request(osdc
, req
);
831 ceph_osdc_get_request(req
);
832 osdc
->num_requests
++;
833 if (osdc
->num_requests
== 1) {
834 dout(" first request, scheduling timeout\n");
835 __schedule_osd_timeout(osdc
);
839 static void register_request(struct ceph_osd_client
*osdc
,
840 struct ceph_osd_request
*req
)
842 mutex_lock(&osdc
->request_mutex
);
843 __register_request(osdc
, req
);
844 mutex_unlock(&osdc
->request_mutex
);
848 * called under osdc->request_mutex
850 static void __unregister_request(struct ceph_osd_client
*osdc
,
851 struct ceph_osd_request
*req
)
853 if (RB_EMPTY_NODE(&req
->r_node
)) {
854 dout("__unregister_request %p tid %lld not registered\n",
859 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
860 rb_erase(&req
->r_node
, &osdc
->requests
);
861 osdc
->num_requests
--;
864 /* make sure the original request isn't in flight. */
865 ceph_msg_revoke(req
->r_request
);
867 list_del_init(&req
->r_osd_item
);
868 if (list_empty(&req
->r_osd
->o_requests
) &&
869 list_empty(&req
->r_osd
->o_linger_requests
)) {
870 dout("moving osd to %p lru\n", req
->r_osd
);
871 __move_osd_to_lru(osdc
, req
->r_osd
);
873 if (list_empty(&req
->r_linger_item
))
877 ceph_osdc_put_request(req
);
879 list_del_init(&req
->r_req_lru_item
);
880 if (osdc
->num_requests
== 0) {
881 dout(" no requests, canceling timeout\n");
882 __cancel_osd_timeout(osdc
);
887 * Cancel a previously queued request message
889 static void __cancel_request(struct ceph_osd_request
*req
)
891 if (req
->r_sent
&& req
->r_osd
) {
892 ceph_msg_revoke(req
->r_request
);
897 static void __register_linger_request(struct ceph_osd_client
*osdc
,
898 struct ceph_osd_request
*req
)
900 dout("__register_linger_request %p\n", req
);
901 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
903 list_add_tail(&req
->r_linger_osd
,
904 &req
->r_osd
->o_linger_requests
);
907 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
908 struct ceph_osd_request
*req
)
910 dout("__unregister_linger_request %p\n", req
);
912 list_del_init(&req
->r_linger_item
);
913 list_del_init(&req
->r_linger_osd
);
915 if (list_empty(&req
->r_osd
->o_requests
) &&
916 list_empty(&req
->r_osd
->o_linger_requests
)) {
917 dout("moving osd to %p lru\n", req
->r_osd
);
918 __move_osd_to_lru(osdc
, req
->r_osd
);
920 if (list_empty(&req
->r_osd_item
))
925 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
926 struct ceph_osd_request
*req
)
928 mutex_lock(&osdc
->request_mutex
);
930 __unregister_linger_request(osdc
, req
);
931 ceph_osdc_put_request(req
);
933 mutex_unlock(&osdc
->request_mutex
);
935 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
937 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
938 struct ceph_osd_request
*req
)
940 if (!req
->r_linger
) {
941 dout("set_request_linger %p\n", req
);
944 * caller is now responsible for calling
945 * unregister_linger_request
947 ceph_osdc_get_request(req
);
950 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
953 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
954 * (as needed), and set the request r_osd appropriately. If there is
955 * no up osd, set r_osd to NULL. Move the request to the appropriate list
956 * (unsent, homeless) or leave on in-flight lru.
958 * Return 0 if unchanged, 1 if changed, or negative on error.
960 * Caller should hold map_sem for read and request_mutex.
962 static int __map_request(struct ceph_osd_client
*osdc
,
963 struct ceph_osd_request
*req
, int force_resend
)
965 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
967 int acting
[CEPH_PG_MAX_SIZE
];
971 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
972 err
= ceph_calc_object_layout(&reqhead
->layout
, req
->r_oid
,
973 &req
->r_file_layout
, osdc
->osdmap
);
975 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
978 pgid
= reqhead
->layout
.ol_pgid
;
981 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
987 if ((!force_resend
&&
988 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
989 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
990 req
->r_num_pg_osds
== num
&&
991 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
992 (req
->r_osd
== NULL
&& o
== -1))
993 return 0; /* no change */
995 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
996 req
->r_tid
, le32_to_cpu(pgid
.pool
), le16_to_cpu(pgid
.ps
), o
,
997 req
->r_osd
? req
->r_osd
->o_osd
: -1);
999 /* record full pg acting set */
1000 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
1001 req
->r_num_pg_osds
= num
;
1004 __cancel_request(req
);
1005 list_del_init(&req
->r_osd_item
);
1009 req
->r_osd
= __lookup_osd(osdc
, o
);
1010 if (!req
->r_osd
&& o
>= 0) {
1012 req
->r_osd
= create_osd(osdc
, o
);
1014 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1018 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
1019 __insert_osd(osdc
, req
->r_osd
);
1021 ceph_con_open(&req
->r_osd
->o_con
,
1022 CEPH_ENTITY_TYPE_OSD
, o
,
1023 &osdc
->osdmap
->osd_addr
[o
]);
1027 __remove_osd_from_lru(req
->r_osd
);
1028 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1029 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1031 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1033 err
= 1; /* osd or pg changed */
1040 * caller should hold map_sem (for read) and request_mutex
1042 static void __send_request(struct ceph_osd_client
*osdc
,
1043 struct ceph_osd_request
*req
)
1045 struct ceph_osd_request_head
*reqhead
;
1047 dout("send_request %p tid %llu to osd%d flags %d\n",
1048 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
);
1050 reqhead
= req
->r_request
->front
.iov_base
;
1051 reqhead
->osdmap_epoch
= cpu_to_le32(osdc
->osdmap
->epoch
);
1052 reqhead
->flags
|= cpu_to_le32(req
->r_flags
); /* e.g., RETRY */
1053 reqhead
->reassert_version
= req
->r_reassert_version
;
1055 req
->r_stamp
= jiffies
;
1056 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1058 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1059 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1060 req
->r_sent
= req
->r_osd
->o_incarnation
;
1064 * Send any requests in the queue (req_unsent).
1066 static void send_queued(struct ceph_osd_client
*osdc
)
1068 struct ceph_osd_request
*req
, *tmp
;
1070 dout("send_queued\n");
1071 mutex_lock(&osdc
->request_mutex
);
1072 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
) {
1073 __send_request(osdc
, req
);
1075 mutex_unlock(&osdc
->request_mutex
);
1079 * Timeout callback, called every N seconds when 1 or more osd
1080 * requests has been active for more than N seconds. When this
1081 * happens, we ping all OSDs with requests who have timed out to
1082 * ensure any communications channel reset is detected. Reset the
1083 * request timeouts another N seconds in the future as we go.
1084 * Reschedule the timeout event another N seconds in future (unless
1085 * there are no open requests).
1087 static void handle_timeout(struct work_struct
*work
)
1089 struct ceph_osd_client
*osdc
=
1090 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1091 struct ceph_osd_request
*req
, *last_req
= NULL
;
1092 struct ceph_osd
*osd
;
1093 unsigned long timeout
= osdc
->client
->options
->osd_timeout
* HZ
;
1094 unsigned long keepalive
=
1095 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1096 unsigned long last_stamp
= 0;
1097 struct list_head slow_osds
;
1099 down_read(&osdc
->map_sem
);
1101 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1103 mutex_lock(&osdc
->request_mutex
);
1106 * reset osds that appear to be _really_ unresponsive. this
1107 * is a failsafe measure.. we really shouldn't be getting to
1108 * this point if the system is working properly. the monitors
1109 * should mark the osd as failed and we should find out about
1110 * it from an updated osd map.
1112 while (timeout
&& !list_empty(&osdc
->req_lru
)) {
1113 req
= list_entry(osdc
->req_lru
.next
, struct ceph_osd_request
,
1116 /* hasn't been long enough since we sent it? */
1117 if (time_before(jiffies
, req
->r_stamp
+ timeout
))
1120 /* hasn't been long enough since it was acked? */
1121 if (req
->r_request
->ack_stamp
== 0 ||
1122 time_before(jiffies
, req
->r_request
->ack_stamp
+ timeout
))
1125 BUG_ON(req
== last_req
&& req
->r_stamp
== last_stamp
);
1127 last_stamp
= req
->r_stamp
;
1131 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1132 req
->r_tid
, osd
->o_osd
);
1133 __kick_osd_requests(osdc
, osd
);
1137 * ping osds that are a bit slow. this ensures that if there
1138 * is a break in the TCP connection we will notice, and reopen
1139 * a connection with that osd (from the fault callback).
1141 INIT_LIST_HEAD(&slow_osds
);
1142 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1143 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1148 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1149 req
->r_tid
, osd
->o_osd
);
1150 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1152 while (!list_empty(&slow_osds
)) {
1153 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1155 list_del_init(&osd
->o_keepalive_item
);
1156 ceph_con_keepalive(&osd
->o_con
);
1159 __schedule_osd_timeout(osdc
);
1160 mutex_unlock(&osdc
->request_mutex
);
1162 up_read(&osdc
->map_sem
);
1165 static void handle_osds_timeout(struct work_struct
*work
)
1167 struct ceph_osd_client
*osdc
=
1168 container_of(work
, struct ceph_osd_client
,
1169 osds_timeout_work
.work
);
1170 unsigned long delay
=
1171 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1173 dout("osds timeout\n");
1174 down_read(&osdc
->map_sem
);
1175 remove_old_osds(osdc
);
1176 up_read(&osdc
->map_sem
);
1178 schedule_delayed_work(&osdc
->osds_timeout_work
,
1179 round_jiffies_relative(delay
));
1182 static void complete_request(struct ceph_osd_request
*req
)
1184 if (req
->r_safe_callback
)
1185 req
->r_safe_callback(req
, NULL
);
1186 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1190 * handle osd op reply. either call the callback if it is specified,
1191 * or do the completion to wake up the waiting thread.
1193 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1194 struct ceph_connection
*con
)
1196 struct ceph_osd_reply_head
*rhead
= msg
->front
.iov_base
;
1197 struct ceph_osd_request
*req
;
1199 int numops
, object_len
, flags
;
1202 tid
= le64_to_cpu(msg
->hdr
.tid
);
1203 if (msg
->front
.iov_len
< sizeof(*rhead
))
1205 numops
= le32_to_cpu(rhead
->num_ops
);
1206 object_len
= le32_to_cpu(rhead
->object_len
);
1207 result
= le32_to_cpu(rhead
->result
);
1208 if (msg
->front
.iov_len
!= sizeof(*rhead
) + object_len
+
1209 numops
* sizeof(struct ceph_osd_op
))
1211 dout("handle_reply %p tid %llu result %d\n", msg
, tid
, (int)result
);
1213 mutex_lock(&osdc
->request_mutex
);
1214 req
= __lookup_request(osdc
, tid
);
1216 dout("handle_reply tid %llu dne\n", tid
);
1217 mutex_unlock(&osdc
->request_mutex
);
1220 ceph_osdc_get_request(req
);
1221 flags
= le32_to_cpu(rhead
->flags
);
1224 * if this connection filled our message, drop our reference now, to
1225 * avoid a (safe but slower) revoke later.
1227 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
1228 dout(" dropping con_filling_msg ref %p\n", con
);
1229 req
->r_con_filling_msg
= NULL
;
1233 if (!req
->r_got_reply
) {
1236 req
->r_result
= le32_to_cpu(rhead
->result
);
1237 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1238 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1240 if (req
->r_result
== 0)
1241 req
->r_result
= bytes
;
1243 /* in case this is a write and we need to replay, */
1244 req
->r_reassert_version
= rhead
->reassert_version
;
1246 req
->r_got_reply
= 1;
1247 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1248 dout("handle_reply tid %llu dup ack\n", tid
);
1249 mutex_unlock(&osdc
->request_mutex
);
1253 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1255 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1256 __register_linger_request(osdc
, req
);
1258 /* either this is a read, or we got the safe response */
1260 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1261 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1262 __unregister_request(osdc
, req
);
1264 mutex_unlock(&osdc
->request_mutex
);
1266 if (req
->r_callback
)
1267 req
->r_callback(req
, msg
);
1269 complete_all(&req
->r_completion
);
1271 if (flags
& CEPH_OSD_FLAG_ONDISK
)
1272 complete_request(req
);
1275 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1276 ceph_osdc_put_request(req
);
1280 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1281 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
),
1282 (int)sizeof(*rhead
));
1286 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1288 struct rb_node
*p
, *n
;
1290 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1291 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1294 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1295 memcmp(&osd
->o_con
.peer_addr
,
1296 ceph_osd_addr(osdc
->osdmap
,
1298 sizeof(struct ceph_entity_addr
)) != 0)
1299 __reset_osd(osdc
, osd
);
1304 * Requeue requests whose mapping to an OSD has changed. If requests map to
1305 * no osd, request a new map.
1307 * Caller should hold map_sem for read and request_mutex.
1309 static void kick_requests(struct ceph_osd_client
*osdc
, int force_resend
)
1311 struct ceph_osd_request
*req
, *nreq
;
1316 dout("kick_requests %s\n", force_resend
? " (force resend)" : "");
1317 mutex_lock(&osdc
->request_mutex
);
1318 for (p
= rb_first(&osdc
->requests
); p
; ) {
1319 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1321 err
= __map_request(osdc
, req
, force_resend
);
1323 continue; /* error */
1324 if (req
->r_osd
== NULL
) {
1325 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1326 needmap
++; /* request a newer map */
1327 } else if (err
> 0) {
1328 if (!req
->r_linger
) {
1329 dout("%p tid %llu requeued on osd%d\n", req
,
1331 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1332 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1335 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
1337 * register as a linger so that we will
1338 * re-submit below and get a new tid
1340 dout("%p tid %llu restart on osd%d\n",
1342 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1343 __register_linger_request(osdc
, req
);
1344 __unregister_request(osdc
, req
);
1348 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1350 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1352 err
= __map_request(osdc
, req
, force_resend
);
1354 continue; /* no change and no osd was specified */
1356 continue; /* hrm! */
1357 if (req
->r_osd
== NULL
) {
1358 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1359 needmap
++; /* request a newer map */
1363 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1364 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1365 __unregister_linger_request(osdc
, req
);
1366 __register_request(osdc
, req
);
1368 mutex_unlock(&osdc
->request_mutex
);
1371 dout("%d requests for down osds, need new map\n", needmap
);
1372 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1378 * Process updated osd map.
1380 * The message contains any number of incremental and full maps, normally
1381 * indicating some sort of topology change in the cluster. Kick requests
1382 * off to different OSDs as needed.
1384 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1386 void *p
, *end
, *next
;
1387 u32 nr_maps
, maplen
;
1389 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1391 struct ceph_fsid fsid
;
1393 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1394 p
= msg
->front
.iov_base
;
1395 end
= p
+ msg
->front
.iov_len
;
1398 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1399 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1400 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1403 down_write(&osdc
->map_sem
);
1405 /* incremental maps */
1406 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1407 dout(" %d inc maps\n", nr_maps
);
1408 while (nr_maps
> 0) {
1409 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1410 epoch
= ceph_decode_32(&p
);
1411 maplen
= ceph_decode_32(&p
);
1412 ceph_decode_need(&p
, end
, maplen
, bad
);
1414 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1415 dout("applying incremental map %u len %d\n",
1417 newmap
= osdmap_apply_incremental(&p
, next
,
1419 &osdc
->client
->msgr
);
1420 if (IS_ERR(newmap
)) {
1421 err
= PTR_ERR(newmap
);
1425 if (newmap
!= osdc
->osdmap
) {
1426 ceph_osdmap_destroy(osdc
->osdmap
);
1427 osdc
->osdmap
= newmap
;
1429 kick_requests(osdc
, 0);
1430 reset_changed_osds(osdc
);
1432 dout("ignoring incremental map %u len %d\n",
1442 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1443 dout(" %d full maps\n", nr_maps
);
1445 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1446 epoch
= ceph_decode_32(&p
);
1447 maplen
= ceph_decode_32(&p
);
1448 ceph_decode_need(&p
, end
, maplen
, bad
);
1450 dout("skipping non-latest full map %u len %d\n",
1452 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1453 dout("skipping full map %u len %d, "
1454 "older than our %u\n", epoch
, maplen
,
1455 osdc
->osdmap
->epoch
);
1457 int skipped_map
= 0;
1459 dout("taking full map %u len %d\n", epoch
, maplen
);
1460 newmap
= osdmap_decode(&p
, p
+maplen
);
1461 if (IS_ERR(newmap
)) {
1462 err
= PTR_ERR(newmap
);
1466 oldmap
= osdc
->osdmap
;
1467 osdc
->osdmap
= newmap
;
1469 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
1471 ceph_osdmap_destroy(oldmap
);
1473 kick_requests(osdc
, skipped_map
);
1480 downgrade_write(&osdc
->map_sem
);
1481 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1484 * subscribe to subsequent osdmap updates if full to ensure
1485 * we find out when we are no longer full and stop returning
1488 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
))
1489 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1492 up_read(&osdc
->map_sem
);
1493 wake_up_all(&osdc
->client
->auth_wq
);
1497 pr_err("osdc handle_map corrupt msg\n");
1499 up_write(&osdc
->map_sem
);
1504 * watch/notify callback event infrastructure
1506 * These callbacks are used both for watch and notify operations.
1508 static void __release_event(struct kref
*kref
)
1510 struct ceph_osd_event
*event
=
1511 container_of(kref
, struct ceph_osd_event
, kref
);
1513 dout("__release_event %p\n", event
);
1517 static void get_event(struct ceph_osd_event
*event
)
1519 kref_get(&event
->kref
);
1522 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1524 kref_put(&event
->kref
, __release_event
);
1526 EXPORT_SYMBOL(ceph_osdc_put_event
);
1528 static void __insert_event(struct ceph_osd_client
*osdc
,
1529 struct ceph_osd_event
*new)
1531 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1532 struct rb_node
*parent
= NULL
;
1533 struct ceph_osd_event
*event
= NULL
;
1537 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1538 if (new->cookie
< event
->cookie
)
1540 else if (new->cookie
> event
->cookie
)
1541 p
= &(*p
)->rb_right
;
1546 rb_link_node(&new->node
, parent
, p
);
1547 rb_insert_color(&new->node
, &osdc
->event_tree
);
1550 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1553 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1554 struct rb_node
*parent
= NULL
;
1555 struct ceph_osd_event
*event
= NULL
;
1559 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1560 if (cookie
< event
->cookie
)
1562 else if (cookie
> event
->cookie
)
1563 p
= &(*p
)->rb_right
;
1570 static void __remove_event(struct ceph_osd_event
*event
)
1572 struct ceph_osd_client
*osdc
= event
->osdc
;
1574 if (!RB_EMPTY_NODE(&event
->node
)) {
1575 dout("__remove_event removed %p\n", event
);
1576 rb_erase(&event
->node
, &osdc
->event_tree
);
1577 ceph_osdc_put_event(event
);
1579 dout("__remove_event didn't remove %p\n", event
);
1583 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1584 void (*event_cb
)(u64
, u64
, u8
, void *),
1585 int one_shot
, void *data
,
1586 struct ceph_osd_event
**pevent
)
1588 struct ceph_osd_event
*event
;
1590 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1594 dout("create_event %p\n", event
);
1595 event
->cb
= event_cb
;
1596 event
->one_shot
= one_shot
;
1599 INIT_LIST_HEAD(&event
->osd_node
);
1600 kref_init(&event
->kref
); /* one ref for us */
1601 kref_get(&event
->kref
); /* one ref for the caller */
1602 init_completion(&event
->completion
);
1604 spin_lock(&osdc
->event_lock
);
1605 event
->cookie
= ++osdc
->event_count
;
1606 __insert_event(osdc
, event
);
1607 spin_unlock(&osdc
->event_lock
);
1612 EXPORT_SYMBOL(ceph_osdc_create_event
);
1614 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1616 struct ceph_osd_client
*osdc
= event
->osdc
;
1618 dout("cancel_event %p\n", event
);
1619 spin_lock(&osdc
->event_lock
);
1620 __remove_event(event
);
1621 spin_unlock(&osdc
->event_lock
);
1622 ceph_osdc_put_event(event
); /* caller's */
1624 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1627 static void do_event_work(struct work_struct
*work
)
1629 struct ceph_osd_event_work
*event_work
=
1630 container_of(work
, struct ceph_osd_event_work
, work
);
1631 struct ceph_osd_event
*event
= event_work
->event
;
1632 u64 ver
= event_work
->ver
;
1633 u64 notify_id
= event_work
->notify_id
;
1634 u8 opcode
= event_work
->opcode
;
1636 dout("do_event_work completing %p\n", event
);
1637 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1638 complete(&event
->completion
);
1639 dout("do_event_work completed %p\n", event
);
1640 ceph_osdc_put_event(event
);
1646 * Process osd watch notifications
1648 void handle_watch_notify(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1652 u64 cookie
, ver
, notify_id
;
1654 struct ceph_osd_event
*event
;
1655 struct ceph_osd_event_work
*event_work
;
1657 p
= msg
->front
.iov_base
;
1658 end
= p
+ msg
->front
.iov_len
;
1660 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1661 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1662 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1663 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1664 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1666 spin_lock(&osdc
->event_lock
);
1667 event
= __find_event(osdc
, cookie
);
1670 if (event
->one_shot
)
1671 __remove_event(event
);
1673 spin_unlock(&osdc
->event_lock
);
1674 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1675 cookie
, ver
, event
);
1677 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1679 dout("ERROR: could not allocate event_work\n");
1682 INIT_WORK(&event_work
->work
, do_event_work
);
1683 event_work
->event
= event
;
1684 event_work
->ver
= ver
;
1685 event_work
->notify_id
= notify_id
;
1686 event_work
->opcode
= opcode
;
1687 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1688 dout("WARNING: failed to queue notify event work\n");
1696 complete(&event
->completion
);
1697 ceph_osdc_put_event(event
);
1701 pr_err("osdc handle_watch_notify corrupt msg\n");
1705 int ceph_osdc_wait_event(struct ceph_osd_event
*event
, unsigned long timeout
)
1709 dout("wait_event %p\n", event
);
1710 err
= wait_for_completion_interruptible_timeout(&event
->completion
,
1712 ceph_osdc_put_event(event
);
1715 dout("wait_event %p returns %d\n", event
, err
);
1718 EXPORT_SYMBOL(ceph_osdc_wait_event
);
1721 * Register request, send initial attempt.
1723 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1724 struct ceph_osd_request
*req
,
1729 req
->r_request
->pages
= req
->r_pages
;
1730 req
->r_request
->nr_pages
= req
->r_num_pages
;
1732 req
->r_request
->bio
= req
->r_bio
;
1734 req
->r_request
->trail
= req
->r_trail
;
1736 register_request(osdc
, req
);
1738 down_read(&osdc
->map_sem
);
1739 mutex_lock(&osdc
->request_mutex
);
1741 * a racing kick_requests() may have sent the message for us
1742 * while we dropped request_mutex above, so only send now if
1743 * the request still han't been touched yet.
1745 if (req
->r_sent
== 0) {
1746 rc
= __map_request(osdc
, req
, 0);
1749 dout("osdc_start_request failed map, "
1750 " will retry %lld\n", req
->r_tid
);
1755 if (req
->r_osd
== NULL
) {
1756 dout("send_request %p no up osds in pg\n", req
);
1757 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1759 __send_request(osdc
, req
);
1765 mutex_unlock(&osdc
->request_mutex
);
1766 up_read(&osdc
->map_sem
);
1769 EXPORT_SYMBOL(ceph_osdc_start_request
);
1772 * wait for a request to complete
1774 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1775 struct ceph_osd_request
*req
)
1779 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1781 mutex_lock(&osdc
->request_mutex
);
1782 __cancel_request(req
);
1783 __unregister_request(osdc
, req
);
1784 mutex_unlock(&osdc
->request_mutex
);
1785 complete_request(req
);
1786 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1790 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1791 return req
->r_result
;
1793 EXPORT_SYMBOL(ceph_osdc_wait_request
);
1796 * sync - wait for all in-flight requests to flush. avoid starvation.
1798 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1800 struct ceph_osd_request
*req
;
1801 u64 last_tid
, next_tid
= 0;
1803 mutex_lock(&osdc
->request_mutex
);
1804 last_tid
= osdc
->last_tid
;
1806 req
= __lookup_request_ge(osdc
, next_tid
);
1809 if (req
->r_tid
> last_tid
)
1812 next_tid
= req
->r_tid
+ 1;
1813 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1816 ceph_osdc_get_request(req
);
1817 mutex_unlock(&osdc
->request_mutex
);
1818 dout("sync waiting on tid %llu (last is %llu)\n",
1819 req
->r_tid
, last_tid
);
1820 wait_for_completion(&req
->r_safe_completion
);
1821 mutex_lock(&osdc
->request_mutex
);
1822 ceph_osdc_put_request(req
);
1824 mutex_unlock(&osdc
->request_mutex
);
1825 dout("sync done (thru tid %llu)\n", last_tid
);
1827 EXPORT_SYMBOL(ceph_osdc_sync
);
1832 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
1837 osdc
->client
= client
;
1838 osdc
->osdmap
= NULL
;
1839 init_rwsem(&osdc
->map_sem
);
1840 init_completion(&osdc
->map_waiters
);
1841 osdc
->last_requested_map
= 0;
1842 mutex_init(&osdc
->request_mutex
);
1844 osdc
->osds
= RB_ROOT
;
1845 INIT_LIST_HEAD(&osdc
->osd_lru
);
1846 osdc
->requests
= RB_ROOT
;
1847 INIT_LIST_HEAD(&osdc
->req_lru
);
1848 INIT_LIST_HEAD(&osdc
->req_unsent
);
1849 INIT_LIST_HEAD(&osdc
->req_notarget
);
1850 INIT_LIST_HEAD(&osdc
->req_linger
);
1851 osdc
->num_requests
= 0;
1852 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
1853 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
1854 spin_lock_init(&osdc
->event_lock
);
1855 osdc
->event_tree
= RB_ROOT
;
1856 osdc
->event_count
= 0;
1858 schedule_delayed_work(&osdc
->osds_timeout_work
,
1859 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
1862 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
1863 sizeof(struct ceph_osd_request
));
1864 if (!osdc
->req_mempool
)
1867 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
1868 OSD_OP_FRONT_LEN
, 10, true,
1872 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
1873 OSD_OPREPLY_FRONT_LEN
, 10, true,
1878 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
1879 if (IS_ERR(osdc
->notify_wq
)) {
1880 err
= PTR_ERR(osdc
->notify_wq
);
1881 osdc
->notify_wq
= NULL
;
1887 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1889 mempool_destroy(osdc
->req_mempool
);
1893 EXPORT_SYMBOL(ceph_osdc_init
);
1895 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
1897 flush_workqueue(osdc
->notify_wq
);
1898 destroy_workqueue(osdc
->notify_wq
);
1899 cancel_delayed_work_sync(&osdc
->timeout_work
);
1900 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
1902 ceph_osdmap_destroy(osdc
->osdmap
);
1903 osdc
->osdmap
= NULL
;
1905 remove_all_osds(osdc
);
1906 mempool_destroy(osdc
->req_mempool
);
1907 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1908 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
1910 EXPORT_SYMBOL(ceph_osdc_stop
);
1913 * Read some contiguous pages. If we cross a stripe boundary, shorten
1914 * *plen. Return number of bytes read, or error.
1916 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
1917 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
1919 u32 truncate_seq
, u64 truncate_size
,
1920 struct page
**pages
, int num_pages
, int page_align
)
1922 struct ceph_osd_request
*req
;
1925 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
1926 vino
.snap
, off
, *plen
);
1927 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
1928 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
1929 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
1930 false, 1, page_align
);
1934 /* it may be a short read due to an object boundary */
1935 req
->r_pages
= pages
;
1937 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1938 off
, *plen
, req
->r_num_pages
, page_align
);
1940 rc
= ceph_osdc_start_request(osdc
, req
, false);
1942 rc
= ceph_osdc_wait_request(osdc
, req
);
1944 ceph_osdc_put_request(req
);
1945 dout("readpages result %d\n", rc
);
1948 EXPORT_SYMBOL(ceph_osdc_readpages
);
1951 * do a synchronous write on N pages
1953 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
1954 struct ceph_file_layout
*layout
,
1955 struct ceph_snap_context
*snapc
,
1957 u32 truncate_seq
, u64 truncate_size
,
1958 struct timespec
*mtime
,
1959 struct page
**pages
, int num_pages
,
1960 int flags
, int do_sync
, bool nofail
)
1962 struct ceph_osd_request
*req
;
1964 int page_align
= off
& ~PAGE_MASK
;
1966 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
1967 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
1969 flags
| CEPH_OSD_FLAG_ONDISK
|
1970 CEPH_OSD_FLAG_WRITE
,
1972 truncate_seq
, truncate_size
, mtime
,
1973 nofail
, 1, page_align
);
1977 /* it may be a short write due to an object boundary */
1978 req
->r_pages
= pages
;
1979 dout("writepages %llu~%llu (%d pages)\n", off
, len
,
1982 rc
= ceph_osdc_start_request(osdc
, req
, nofail
);
1984 rc
= ceph_osdc_wait_request(osdc
, req
);
1986 ceph_osdc_put_request(req
);
1989 dout("writepages result %d\n", rc
);
1992 EXPORT_SYMBOL(ceph_osdc_writepages
);
1995 * handle incoming message
1997 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1999 struct ceph_osd
*osd
= con
->private;
2000 struct ceph_osd_client
*osdc
;
2001 int type
= le16_to_cpu(msg
->hdr
.type
);
2008 case CEPH_MSG_OSD_MAP
:
2009 ceph_osdc_handle_map(osdc
, msg
);
2011 case CEPH_MSG_OSD_OPREPLY
:
2012 handle_reply(osdc
, msg
, con
);
2014 case CEPH_MSG_WATCH_NOTIFY
:
2015 handle_watch_notify(osdc
, msg
);
2019 pr_err("received unknown message type %d %s\n", type
,
2020 ceph_msg_type_name(type
));
2027 * lookup and return message for incoming reply. set up reply message
2030 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2031 struct ceph_msg_header
*hdr
,
2034 struct ceph_osd
*osd
= con
->private;
2035 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2037 struct ceph_osd_request
*req
;
2038 int front
= le32_to_cpu(hdr
->front_len
);
2039 int data_len
= le32_to_cpu(hdr
->data_len
);
2042 tid
= le64_to_cpu(hdr
->tid
);
2043 mutex_lock(&osdc
->request_mutex
);
2044 req
= __lookup_request(osdc
, tid
);
2048 dout("get_reply unknown tid %llu from osd%d\n", tid
,
2053 if (req
->r_con_filling_msg
) {
2054 dout("%s revoking msg %p from old con %p\n", __func__
,
2055 req
->r_reply
, req
->r_con_filling_msg
);
2056 ceph_msg_revoke_incoming(req
->r_reply
);
2057 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
2058 req
->r_con_filling_msg
= NULL
;
2061 if (front
> req
->r_reply
->front
.iov_len
) {
2062 pr_warning("get_reply front %d > preallocated %d\n",
2063 front
, (int)req
->r_reply
->front
.iov_len
);
2064 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
, false);
2067 ceph_msg_put(req
->r_reply
);
2070 m
= ceph_msg_get(req
->r_reply
);
2073 int want
= calc_pages_for(req
->r_page_alignment
, data_len
);
2075 if (unlikely(req
->r_num_pages
< want
)) {
2076 pr_warning("tid %lld reply has %d bytes %d pages, we"
2077 " had only %d pages ready\n", tid
, data_len
,
2078 want
, req
->r_num_pages
);
2084 m
->pages
= req
->r_pages
;
2085 m
->nr_pages
= req
->r_num_pages
;
2086 m
->page_alignment
= req
->r_page_alignment
;
2088 m
->bio
= req
->r_bio
;
2092 req
->r_con_filling_msg
= con
->ops
->get(con
);
2093 dout("get_reply tid %lld %p\n", tid
, m
);
2096 mutex_unlock(&osdc
->request_mutex
);
2101 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2102 struct ceph_msg_header
*hdr
,
2105 struct ceph_osd
*osd
= con
->private;
2106 int type
= le16_to_cpu(hdr
->type
);
2107 int front
= le32_to_cpu(hdr
->front_len
);
2111 case CEPH_MSG_OSD_MAP
:
2112 case CEPH_MSG_WATCH_NOTIFY
:
2113 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2114 case CEPH_MSG_OSD_OPREPLY
:
2115 return get_reply(con
, hdr
, skip
);
2117 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2125 * Wrappers to refcount containing ceph_osd struct
2127 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2129 struct ceph_osd
*osd
= con
->private;
2135 static void put_osd_con(struct ceph_connection
*con
)
2137 struct ceph_osd
*osd
= con
->private;
2145 * Note: returned pointer is the address of a structure that's
2146 * managed separately. Caller must *not* attempt to free it.
2148 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2149 int *proto
, int force_new
)
2151 struct ceph_osd
*o
= con
->private;
2152 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2153 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2154 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2156 if (force_new
&& auth
->authorizer
) {
2157 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
2158 ac
->ops
->destroy_authorizer(ac
, auth
->authorizer
);
2159 auth
->authorizer
= NULL
;
2161 if (!auth
->authorizer
&& ac
->ops
&& ac
->ops
->create_authorizer
) {
2162 int ret
= ac
->ops
->create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2165 return ERR_PTR(ret
);
2167 *proto
= ac
->protocol
;
2173 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2175 struct ceph_osd
*o
= con
->private;
2176 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2177 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2180 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2181 * XXX which do we do: succeed or fail?
2183 return ac
->ops
->verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
2186 static int invalidate_authorizer(struct ceph_connection
*con
)
2188 struct ceph_osd
*o
= con
->private;
2189 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2190 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2192 if (ac
->ops
&& ac
->ops
->invalidate_authorizer
)
2193 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2195 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2198 static const struct ceph_connection_operations osd_con_ops
= {
2201 .dispatch
= dispatch
,
2202 .get_authorizer
= get_authorizer
,
2203 .verify_authorizer_reply
= verify_authorizer_reply
,
2204 .invalidate_authorizer
= invalidate_authorizer
,
2205 .alloc_msg
= alloc_msg
,