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 INIT_LIST_HEAD(&req
->r_unsafe_item
);
225 INIT_LIST_HEAD(&req
->r_linger_item
);
226 INIT_LIST_HEAD(&req
->r_linger_osd
);
227 INIT_LIST_HEAD(&req
->r_req_lru_item
);
228 INIT_LIST_HEAD(&req
->r_osd_item
);
230 req
->r_flags
= flags
;
232 WARN_ON((flags
& (CEPH_OSD_FLAG_READ
|CEPH_OSD_FLAG_WRITE
)) == 0);
234 /* create reply message */
236 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
238 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
239 OSD_OPREPLY_FRONT_LEN
, gfp_flags
, true);
241 ceph_osdc_put_request(req
);
246 /* allocate space for the trailing data */
248 req
->r_trail
= kmalloc(sizeof(struct ceph_pagelist
), gfp_flags
);
250 ceph_osdc_put_request(req
);
253 ceph_pagelist_init(req
->r_trail
);
256 /* create request message; allow space for oid */
257 msg_size
+= MAX_OBJ_NAME_SIZE
;
259 msg_size
+= sizeof(u64
) * snapc
->num_snaps
;
261 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
263 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp_flags
, true);
265 ceph_osdc_put_request(req
);
269 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
271 req
->r_request
= msg
;
272 req
->r_pages
= pages
;
282 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
284 static void osd_req_encode_op(struct ceph_osd_request
*req
,
285 struct ceph_osd_op
*dst
,
286 struct ceph_osd_req_op
*src
)
288 dst
->op
= cpu_to_le16(src
->op
);
291 case CEPH_OSD_OP_READ
:
292 case CEPH_OSD_OP_WRITE
:
294 cpu_to_le64(src
->extent
.offset
);
296 cpu_to_le64(src
->extent
.length
);
297 dst
->extent
.truncate_size
=
298 cpu_to_le64(src
->extent
.truncate_size
);
299 dst
->extent
.truncate_seq
=
300 cpu_to_le32(src
->extent
.truncate_seq
);
303 case CEPH_OSD_OP_GETXATTR
:
304 case CEPH_OSD_OP_SETXATTR
:
305 case CEPH_OSD_OP_CMPXATTR
:
306 BUG_ON(!req
->r_trail
);
308 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
309 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
310 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
311 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
312 ceph_pagelist_append(req
->r_trail
, src
->xattr
.name
,
313 src
->xattr
.name_len
);
314 ceph_pagelist_append(req
->r_trail
, src
->xattr
.val
,
315 src
->xattr
.value_len
);
317 case CEPH_OSD_OP_CALL
:
318 BUG_ON(!req
->r_trail
);
320 dst
->cls
.class_len
= src
->cls
.class_len
;
321 dst
->cls
.method_len
= src
->cls
.method_len
;
322 dst
->cls
.indata_len
= cpu_to_le32(src
->cls
.indata_len
);
324 ceph_pagelist_append(req
->r_trail
, src
->cls
.class_name
,
326 ceph_pagelist_append(req
->r_trail
, src
->cls
.method_name
,
327 src
->cls
.method_len
);
328 ceph_pagelist_append(req
->r_trail
, src
->cls
.indata
,
329 src
->cls
.indata_len
);
331 case CEPH_OSD_OP_ROLLBACK
:
332 dst
->snap
.snapid
= cpu_to_le64(src
->snap
.snapid
);
334 case CEPH_OSD_OP_STARTSYNC
:
336 case CEPH_OSD_OP_NOTIFY
:
338 __le32 prot_ver
= cpu_to_le32(src
->watch
.prot_ver
);
339 __le32 timeout
= cpu_to_le32(src
->watch
.timeout
);
341 BUG_ON(!req
->r_trail
);
343 ceph_pagelist_append(req
->r_trail
,
344 &prot_ver
, sizeof(prot_ver
));
345 ceph_pagelist_append(req
->r_trail
,
346 &timeout
, sizeof(timeout
));
348 case CEPH_OSD_OP_NOTIFY_ACK
:
349 case CEPH_OSD_OP_WATCH
:
350 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
351 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
352 dst
->watch
.flag
= src
->watch
.flag
;
355 pr_err("unrecognized osd opcode %d\n", dst
->op
);
359 dst
->payload_len
= cpu_to_le32(src
->payload_len
);
363 * build new request AND message
366 void ceph_osdc_build_request(struct ceph_osd_request
*req
,
368 struct ceph_osd_req_op
*src_ops
,
369 struct ceph_snap_context
*snapc
,
370 struct timespec
*mtime
,
374 struct ceph_msg
*msg
= req
->r_request
;
375 struct ceph_osd_request_head
*head
;
376 struct ceph_osd_req_op
*src_op
;
377 struct ceph_osd_op
*op
;
379 int num_op
= get_num_ops(src_ops
, NULL
);
380 size_t msg_size
= sizeof(*head
) + num_op
*sizeof(*op
);
381 int flags
= req
->r_flags
;
385 head
= msg
->front
.iov_base
;
386 op
= (void *)(head
+ 1);
387 p
= (void *)(op
+ num_op
);
389 req
->r_snapc
= ceph_get_snap_context(snapc
);
391 head
->client_inc
= cpu_to_le32(1); /* always, for now. */
392 head
->flags
= cpu_to_le32(flags
);
393 if (flags
& CEPH_OSD_FLAG_WRITE
)
394 ceph_encode_timespec(&head
->mtime
, mtime
);
395 head
->num_ops
= cpu_to_le16(num_op
);
399 head
->object_len
= cpu_to_le32(oid_len
);
400 memcpy(p
, oid
, oid_len
);
405 osd_req_encode_op(req
, op
, src_op
);
411 data_len
+= req
->r_trail
->length
;
414 head
->snap_seq
= cpu_to_le64(snapc
->seq
);
415 head
->num_snaps
= cpu_to_le32(snapc
->num_snaps
);
416 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
417 put_unaligned_le64(snapc
->snaps
[i
], p
);
422 if (flags
& CEPH_OSD_FLAG_WRITE
) {
423 req
->r_request
->hdr
.data_off
= cpu_to_le16(off
);
424 req
->r_request
->hdr
.data_len
= cpu_to_le32(*plen
+ data_len
);
425 } else if (data_len
) {
426 req
->r_request
->hdr
.data_off
= 0;
427 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
430 req
->r_request
->page_alignment
= req
->r_page_alignment
;
432 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
433 msg_size
= p
- msg
->front
.iov_base
;
434 msg
->front
.iov_len
= msg_size
;
435 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
438 EXPORT_SYMBOL(ceph_osdc_build_request
);
441 * build new request AND message, calculate layout, and adjust file
444 * if the file was recently truncated, we include information about its
445 * old and new size so that the object can be updated appropriately. (we
446 * avoid synchronously deleting truncated objects because it's slow.)
448 * if @do_sync, include a 'startsync' command so that the osd will flush
451 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
452 struct ceph_file_layout
*layout
,
453 struct ceph_vino vino
,
455 int opcode
, int flags
,
456 struct ceph_snap_context
*snapc
,
460 struct timespec
*mtime
,
461 bool use_mempool
, int num_reply
,
464 struct ceph_osd_req_op ops
[3];
465 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
);
485 return ERR_PTR(-ENOMEM
);
487 /* calculate max write size */
488 r
= calc_layout(osdc
, vino
, layout
, off
, plen
, req
, ops
);
491 req
->r_file_layout
= *layout
; /* keep a copy */
493 /* in case it differs from natural (file) alignment that
494 calc_layout filled in for us */
495 req
->r_num_pages
= calc_pages_for(page_align
, *plen
);
496 req
->r_page_alignment
= page_align
;
498 ceph_osdc_build_request(req
, off
, plen
, ops
,
501 req
->r_oid
, req
->r_oid_len
);
505 EXPORT_SYMBOL(ceph_osdc_new_request
);
508 * We keep osd requests in an rbtree, sorted by ->r_tid.
510 static void __insert_request(struct ceph_osd_client
*osdc
,
511 struct ceph_osd_request
*new)
513 struct rb_node
**p
= &osdc
->requests
.rb_node
;
514 struct rb_node
*parent
= NULL
;
515 struct ceph_osd_request
*req
= NULL
;
519 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
520 if (new->r_tid
< req
->r_tid
)
522 else if (new->r_tid
> req
->r_tid
)
528 rb_link_node(&new->r_node
, parent
, p
);
529 rb_insert_color(&new->r_node
, &osdc
->requests
);
532 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
535 struct ceph_osd_request
*req
;
536 struct rb_node
*n
= osdc
->requests
.rb_node
;
539 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
540 if (tid
< req
->r_tid
)
542 else if (tid
> req
->r_tid
)
550 static struct ceph_osd_request
*
551 __lookup_request_ge(struct ceph_osd_client
*osdc
,
554 struct ceph_osd_request
*req
;
555 struct rb_node
*n
= osdc
->requests
.rb_node
;
558 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
559 if (tid
< req
->r_tid
) {
563 } else if (tid
> req
->r_tid
) {
573 * Resubmit requests pending on the given osd.
575 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
576 struct ceph_osd
*osd
)
578 struct ceph_osd_request
*req
, *nreq
;
581 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
582 err
= __reset_osd(osdc
, osd
);
586 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
587 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
588 dout("requeued %p tid %llu osd%d\n", req
, req
->r_tid
,
591 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
594 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
597 * reregister request prior to unregistering linger so
598 * that r_osd is preserved.
600 BUG_ON(!list_empty(&req
->r_req_lru_item
));
601 __register_request(osdc
, req
);
602 list_add(&req
->r_req_lru_item
, &osdc
->req_unsent
);
603 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
604 __unregister_linger_request(osdc
, req
);
605 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
610 static void kick_osd_requests(struct ceph_osd_client
*osdc
,
611 struct ceph_osd
*kickosd
)
613 mutex_lock(&osdc
->request_mutex
);
614 __kick_osd_requests(osdc
, kickosd
);
615 mutex_unlock(&osdc
->request_mutex
);
619 * If the osd connection drops, we need to resubmit all requests.
621 static void osd_reset(struct ceph_connection
*con
)
623 struct ceph_osd
*osd
= con
->private;
624 struct ceph_osd_client
*osdc
;
628 dout("osd_reset osd%d\n", osd
->o_osd
);
630 down_read(&osdc
->map_sem
);
631 kick_osd_requests(osdc
, osd
);
633 up_read(&osdc
->map_sem
);
637 * Track open sessions with osds.
639 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
641 struct ceph_osd
*osd
;
643 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
647 atomic_set(&osd
->o_ref
, 1);
650 INIT_LIST_HEAD(&osd
->o_requests
);
651 INIT_LIST_HEAD(&osd
->o_linger_requests
);
652 INIT_LIST_HEAD(&osd
->o_osd_lru
);
653 osd
->o_incarnation
= 1;
655 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
657 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
661 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
663 if (atomic_inc_not_zero(&osd
->o_ref
)) {
664 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
665 atomic_read(&osd
->o_ref
));
668 dout("get_osd %p FAIL\n", osd
);
673 static void put_osd(struct ceph_osd
*osd
)
675 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
676 atomic_read(&osd
->o_ref
) - 1);
677 if (atomic_dec_and_test(&osd
->o_ref
) && osd
->o_auth
.authorizer
) {
678 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
680 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
681 ac
->ops
->destroy_authorizer(ac
, osd
->o_auth
.authorizer
);
687 * remove an osd from our map
689 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
691 dout("__remove_osd %p\n", osd
);
692 BUG_ON(!list_empty(&osd
->o_requests
));
693 rb_erase(&osd
->o_node
, &osdc
->osds
);
694 list_del_init(&osd
->o_osd_lru
);
695 ceph_con_close(&osd
->o_con
);
699 static void remove_all_osds(struct ceph_osd_client
*osdc
)
701 dout("%s %p\n", __func__
, osdc
);
702 mutex_lock(&osdc
->request_mutex
);
703 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
704 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
705 struct ceph_osd
, o_node
);
706 __remove_osd(osdc
, osd
);
708 mutex_unlock(&osdc
->request_mutex
);
711 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
712 struct ceph_osd
*osd
)
714 dout("__move_osd_to_lru %p\n", osd
);
715 BUG_ON(!list_empty(&osd
->o_osd_lru
));
716 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
717 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
720 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
722 dout("__remove_osd_from_lru %p\n", osd
);
723 if (!list_empty(&osd
->o_osd_lru
))
724 list_del_init(&osd
->o_osd_lru
);
727 static void remove_old_osds(struct ceph_osd_client
*osdc
)
729 struct ceph_osd
*osd
, *nosd
;
731 dout("__remove_old_osds %p\n", osdc
);
732 mutex_lock(&osdc
->request_mutex
);
733 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
734 if (time_before(jiffies
, osd
->lru_ttl
))
736 __remove_osd(osdc
, osd
);
738 mutex_unlock(&osdc
->request_mutex
);
744 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
746 struct ceph_osd_request
*req
;
749 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
750 if (list_empty(&osd
->o_requests
) &&
751 list_empty(&osd
->o_linger_requests
)) {
752 __remove_osd(osdc
, osd
);
753 } else if (memcmp(&osdc
->osdmap
->osd_addr
[osd
->o_osd
],
754 &osd
->o_con
.peer_addr
,
755 sizeof(osd
->o_con
.peer_addr
)) == 0 &&
756 !ceph_con_opened(&osd
->o_con
)) {
757 dout(" osd addr hasn't changed and connection never opened,"
758 " letting msgr retry");
759 /* touch each r_stamp for handle_timeout()'s benfit */
760 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
761 req
->r_stamp
= jiffies
;
764 ceph_con_close(&osd
->o_con
);
765 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
,
766 &osdc
->osdmap
->osd_addr
[osd
->o_osd
]);
767 osd
->o_incarnation
++;
772 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
774 struct rb_node
**p
= &osdc
->osds
.rb_node
;
775 struct rb_node
*parent
= NULL
;
776 struct ceph_osd
*osd
= NULL
;
778 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
781 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
782 if (new->o_osd
< osd
->o_osd
)
784 else if (new->o_osd
> osd
->o_osd
)
790 rb_link_node(&new->o_node
, parent
, p
);
791 rb_insert_color(&new->o_node
, &osdc
->osds
);
794 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
796 struct ceph_osd
*osd
;
797 struct rb_node
*n
= osdc
->osds
.rb_node
;
800 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
803 else if (o
> osd
->o_osd
)
811 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
813 schedule_delayed_work(&osdc
->timeout_work
,
814 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
817 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
819 cancel_delayed_work(&osdc
->timeout_work
);
823 * Register request, assign tid. If this is the first request, set up
826 static void __register_request(struct ceph_osd_client
*osdc
,
827 struct ceph_osd_request
*req
)
829 req
->r_tid
= ++osdc
->last_tid
;
830 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
831 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
832 __insert_request(osdc
, req
);
833 ceph_osdc_get_request(req
);
834 osdc
->num_requests
++;
835 if (osdc
->num_requests
== 1) {
836 dout(" first request, scheduling timeout\n");
837 __schedule_osd_timeout(osdc
);
841 static void register_request(struct ceph_osd_client
*osdc
,
842 struct ceph_osd_request
*req
)
844 mutex_lock(&osdc
->request_mutex
);
845 __register_request(osdc
, req
);
846 mutex_unlock(&osdc
->request_mutex
);
850 * called under osdc->request_mutex
852 static void __unregister_request(struct ceph_osd_client
*osdc
,
853 struct ceph_osd_request
*req
)
855 if (RB_EMPTY_NODE(&req
->r_node
)) {
856 dout("__unregister_request %p tid %lld not registered\n",
861 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
862 rb_erase(&req
->r_node
, &osdc
->requests
);
863 osdc
->num_requests
--;
866 /* make sure the original request isn't in flight. */
867 ceph_msg_revoke(req
->r_request
);
869 list_del_init(&req
->r_osd_item
);
870 if (list_empty(&req
->r_osd
->o_requests
) &&
871 list_empty(&req
->r_osd
->o_linger_requests
)) {
872 dout("moving osd to %p lru\n", req
->r_osd
);
873 __move_osd_to_lru(osdc
, req
->r_osd
);
875 if (list_empty(&req
->r_linger_item
))
879 ceph_osdc_put_request(req
);
881 list_del_init(&req
->r_req_lru_item
);
882 if (osdc
->num_requests
== 0) {
883 dout(" no requests, canceling timeout\n");
884 __cancel_osd_timeout(osdc
);
889 * Cancel a previously queued request message
891 static void __cancel_request(struct ceph_osd_request
*req
)
893 if (req
->r_sent
&& req
->r_osd
) {
894 ceph_msg_revoke(req
->r_request
);
899 static void __register_linger_request(struct ceph_osd_client
*osdc
,
900 struct ceph_osd_request
*req
)
902 dout("__register_linger_request %p\n", req
);
903 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
905 list_add_tail(&req
->r_linger_osd
,
906 &req
->r_osd
->o_linger_requests
);
909 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
910 struct ceph_osd_request
*req
)
912 dout("__unregister_linger_request %p\n", req
);
914 list_del_init(&req
->r_linger_item
);
915 list_del_init(&req
->r_linger_osd
);
917 if (list_empty(&req
->r_osd
->o_requests
) &&
918 list_empty(&req
->r_osd
->o_linger_requests
)) {
919 dout("moving osd to %p lru\n", req
->r_osd
);
920 __move_osd_to_lru(osdc
, req
->r_osd
);
922 if (list_empty(&req
->r_osd_item
))
927 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
928 struct ceph_osd_request
*req
)
930 mutex_lock(&osdc
->request_mutex
);
932 __unregister_linger_request(osdc
, req
);
933 ceph_osdc_put_request(req
);
935 mutex_unlock(&osdc
->request_mutex
);
937 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
939 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
940 struct ceph_osd_request
*req
)
942 if (!req
->r_linger
) {
943 dout("set_request_linger %p\n", req
);
946 * caller is now responsible for calling
947 * unregister_linger_request
949 ceph_osdc_get_request(req
);
952 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
955 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
956 * (as needed), and set the request r_osd appropriately. If there is
957 * no up osd, set r_osd to NULL. Move the request to the appropriate list
958 * (unsent, homeless) or leave on in-flight lru.
960 * Return 0 if unchanged, 1 if changed, or negative on error.
962 * Caller should hold map_sem for read and request_mutex.
964 static int __map_request(struct ceph_osd_client
*osdc
,
965 struct ceph_osd_request
*req
, int force_resend
)
967 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
969 int acting
[CEPH_PG_MAX_SIZE
];
973 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
974 err
= ceph_calc_object_layout(&reqhead
->layout
, req
->r_oid
,
975 &req
->r_file_layout
, osdc
->osdmap
);
977 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
980 pgid
= reqhead
->layout
.ol_pgid
;
983 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
989 if ((!force_resend
&&
990 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
991 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
992 req
->r_num_pg_osds
== num
&&
993 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
994 (req
->r_osd
== NULL
&& o
== -1))
995 return 0; /* no change */
997 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
998 req
->r_tid
, le32_to_cpu(pgid
.pool
), le16_to_cpu(pgid
.ps
), o
,
999 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1001 /* record full pg acting set */
1002 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
1003 req
->r_num_pg_osds
= num
;
1006 __cancel_request(req
);
1007 list_del_init(&req
->r_osd_item
);
1011 req
->r_osd
= __lookup_osd(osdc
, o
);
1012 if (!req
->r_osd
&& o
>= 0) {
1014 req
->r_osd
= create_osd(osdc
, o
);
1016 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1020 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
1021 __insert_osd(osdc
, req
->r_osd
);
1023 ceph_con_open(&req
->r_osd
->o_con
,
1024 CEPH_ENTITY_TYPE_OSD
, o
,
1025 &osdc
->osdmap
->osd_addr
[o
]);
1029 __remove_osd_from_lru(req
->r_osd
);
1030 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1031 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1033 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1035 err
= 1; /* osd or pg changed */
1042 * caller should hold map_sem (for read) and request_mutex
1044 static void __send_request(struct ceph_osd_client
*osdc
,
1045 struct ceph_osd_request
*req
)
1047 struct ceph_osd_request_head
*reqhead
;
1049 dout("send_request %p tid %llu to osd%d flags %d\n",
1050 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
);
1052 reqhead
= req
->r_request
->front
.iov_base
;
1053 reqhead
->osdmap_epoch
= cpu_to_le32(osdc
->osdmap
->epoch
);
1054 reqhead
->flags
|= cpu_to_le32(req
->r_flags
); /* e.g., RETRY */
1055 reqhead
->reassert_version
= req
->r_reassert_version
;
1057 req
->r_stamp
= jiffies
;
1058 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1060 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1061 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1062 req
->r_sent
= req
->r_osd
->o_incarnation
;
1066 * Send any requests in the queue (req_unsent).
1068 static void send_queued(struct ceph_osd_client
*osdc
)
1070 struct ceph_osd_request
*req
, *tmp
;
1072 dout("send_queued\n");
1073 mutex_lock(&osdc
->request_mutex
);
1074 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
) {
1075 __send_request(osdc
, req
);
1077 mutex_unlock(&osdc
->request_mutex
);
1081 * Timeout callback, called every N seconds when 1 or more osd
1082 * requests has been active for more than N seconds. When this
1083 * happens, we ping all OSDs with requests who have timed out to
1084 * ensure any communications channel reset is detected. Reset the
1085 * request timeouts another N seconds in the future as we go.
1086 * Reschedule the timeout event another N seconds in future (unless
1087 * there are no open requests).
1089 static void handle_timeout(struct work_struct
*work
)
1091 struct ceph_osd_client
*osdc
=
1092 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1093 struct ceph_osd_request
*req
, *last_req
= NULL
;
1094 struct ceph_osd
*osd
;
1095 unsigned long timeout
= osdc
->client
->options
->osd_timeout
* HZ
;
1096 unsigned long keepalive
=
1097 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1098 unsigned long last_stamp
= 0;
1099 struct list_head slow_osds
;
1101 down_read(&osdc
->map_sem
);
1103 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1105 mutex_lock(&osdc
->request_mutex
);
1108 * reset osds that appear to be _really_ unresponsive. this
1109 * is a failsafe measure.. we really shouldn't be getting to
1110 * this point if the system is working properly. the monitors
1111 * should mark the osd as failed and we should find out about
1112 * it from an updated osd map.
1114 while (timeout
&& !list_empty(&osdc
->req_lru
)) {
1115 req
= list_entry(osdc
->req_lru
.next
, struct ceph_osd_request
,
1118 /* hasn't been long enough since we sent it? */
1119 if (time_before(jiffies
, req
->r_stamp
+ timeout
))
1122 /* hasn't been long enough since it was acked? */
1123 if (req
->r_request
->ack_stamp
== 0 ||
1124 time_before(jiffies
, req
->r_request
->ack_stamp
+ timeout
))
1127 BUG_ON(req
== last_req
&& req
->r_stamp
== last_stamp
);
1129 last_stamp
= req
->r_stamp
;
1133 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1134 req
->r_tid
, osd
->o_osd
);
1135 __kick_osd_requests(osdc
, osd
);
1139 * ping osds that are a bit slow. this ensures that if there
1140 * is a break in the TCP connection we will notice, and reopen
1141 * a connection with that osd (from the fault callback).
1143 INIT_LIST_HEAD(&slow_osds
);
1144 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1145 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1150 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1151 req
->r_tid
, osd
->o_osd
);
1152 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1154 while (!list_empty(&slow_osds
)) {
1155 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1157 list_del_init(&osd
->o_keepalive_item
);
1158 ceph_con_keepalive(&osd
->o_con
);
1161 __schedule_osd_timeout(osdc
);
1162 mutex_unlock(&osdc
->request_mutex
);
1164 up_read(&osdc
->map_sem
);
1167 static void handle_osds_timeout(struct work_struct
*work
)
1169 struct ceph_osd_client
*osdc
=
1170 container_of(work
, struct ceph_osd_client
,
1171 osds_timeout_work
.work
);
1172 unsigned long delay
=
1173 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1175 dout("osds timeout\n");
1176 down_read(&osdc
->map_sem
);
1177 remove_old_osds(osdc
);
1178 up_read(&osdc
->map_sem
);
1180 schedule_delayed_work(&osdc
->osds_timeout_work
,
1181 round_jiffies_relative(delay
));
1184 static void complete_request(struct ceph_osd_request
*req
)
1186 if (req
->r_safe_callback
)
1187 req
->r_safe_callback(req
, NULL
);
1188 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1192 * handle osd op reply. either call the callback if it is specified,
1193 * or do the completion to wake up the waiting thread.
1195 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1196 struct ceph_connection
*con
)
1198 struct ceph_osd_reply_head
*rhead
= msg
->front
.iov_base
;
1199 struct ceph_osd_request
*req
;
1201 int numops
, object_len
, flags
;
1204 tid
= le64_to_cpu(msg
->hdr
.tid
);
1205 if (msg
->front
.iov_len
< sizeof(*rhead
))
1207 numops
= le32_to_cpu(rhead
->num_ops
);
1208 object_len
= le32_to_cpu(rhead
->object_len
);
1209 result
= le32_to_cpu(rhead
->result
);
1210 if (msg
->front
.iov_len
!= sizeof(*rhead
) + object_len
+
1211 numops
* sizeof(struct ceph_osd_op
))
1213 dout("handle_reply %p tid %llu result %d\n", msg
, tid
, (int)result
);
1215 mutex_lock(&osdc
->request_mutex
);
1216 req
= __lookup_request(osdc
, tid
);
1218 dout("handle_reply tid %llu dne\n", tid
);
1219 mutex_unlock(&osdc
->request_mutex
);
1222 ceph_osdc_get_request(req
);
1223 flags
= le32_to_cpu(rhead
->flags
);
1226 * if this connection filled our message, drop our reference now, to
1227 * avoid a (safe but slower) revoke later.
1229 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
1230 dout(" dropping con_filling_msg ref %p\n", con
);
1231 req
->r_con_filling_msg
= NULL
;
1235 if (!req
->r_got_reply
) {
1238 req
->r_result
= le32_to_cpu(rhead
->result
);
1239 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1240 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1242 if (req
->r_result
== 0)
1243 req
->r_result
= bytes
;
1245 /* in case this is a write and we need to replay, */
1246 req
->r_reassert_version
= rhead
->reassert_version
;
1248 req
->r_got_reply
= 1;
1249 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1250 dout("handle_reply tid %llu dup ack\n", tid
);
1251 mutex_unlock(&osdc
->request_mutex
);
1255 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1257 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1258 __register_linger_request(osdc
, req
);
1260 /* either this is a read, or we got the safe response */
1262 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1263 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1264 __unregister_request(osdc
, req
);
1266 mutex_unlock(&osdc
->request_mutex
);
1268 if (req
->r_callback
)
1269 req
->r_callback(req
, msg
);
1271 complete_all(&req
->r_completion
);
1273 if (flags
& CEPH_OSD_FLAG_ONDISK
)
1274 complete_request(req
);
1277 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1278 ceph_osdc_put_request(req
);
1282 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1283 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
),
1284 (int)sizeof(*rhead
));
1288 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1290 struct rb_node
*p
, *n
;
1292 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1293 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1296 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1297 memcmp(&osd
->o_con
.peer_addr
,
1298 ceph_osd_addr(osdc
->osdmap
,
1300 sizeof(struct ceph_entity_addr
)) != 0)
1301 __reset_osd(osdc
, osd
);
1306 * Requeue requests whose mapping to an OSD has changed. If requests map to
1307 * no osd, request a new map.
1309 * Caller should hold map_sem for read and request_mutex.
1311 static void kick_requests(struct ceph_osd_client
*osdc
, int force_resend
)
1313 struct ceph_osd_request
*req
, *nreq
;
1318 dout("kick_requests %s\n", force_resend
? " (force resend)" : "");
1319 mutex_lock(&osdc
->request_mutex
);
1320 for (p
= rb_first(&osdc
->requests
); p
; ) {
1321 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1323 err
= __map_request(osdc
, req
, force_resend
);
1325 continue; /* error */
1326 if (req
->r_osd
== NULL
) {
1327 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1328 needmap
++; /* request a newer map */
1329 } else if (err
> 0) {
1330 if (!req
->r_linger
) {
1331 dout("%p tid %llu requeued on osd%d\n", req
,
1333 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1334 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1337 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
1339 * register as a linger so that we will
1340 * re-submit below and get a new tid
1342 dout("%p tid %llu restart on osd%d\n",
1344 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1345 __register_linger_request(osdc
, req
);
1346 __unregister_request(osdc
, req
);
1350 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1352 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1354 err
= __map_request(osdc
, req
, force_resend
);
1356 continue; /* no change and no osd was specified */
1358 continue; /* hrm! */
1359 if (req
->r_osd
== NULL
) {
1360 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1361 needmap
++; /* request a newer map */
1365 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1366 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1367 __unregister_linger_request(osdc
, req
);
1368 __register_request(osdc
, req
);
1370 mutex_unlock(&osdc
->request_mutex
);
1373 dout("%d requests for down osds, need new map\n", needmap
);
1374 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1380 * Process updated osd map.
1382 * The message contains any number of incremental and full maps, normally
1383 * indicating some sort of topology change in the cluster. Kick requests
1384 * off to different OSDs as needed.
1386 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1388 void *p
, *end
, *next
;
1389 u32 nr_maps
, maplen
;
1391 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1393 struct ceph_fsid fsid
;
1395 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1396 p
= msg
->front
.iov_base
;
1397 end
= p
+ msg
->front
.iov_len
;
1400 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1401 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1402 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1405 down_write(&osdc
->map_sem
);
1407 /* incremental maps */
1408 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1409 dout(" %d inc maps\n", nr_maps
);
1410 while (nr_maps
> 0) {
1411 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1412 epoch
= ceph_decode_32(&p
);
1413 maplen
= ceph_decode_32(&p
);
1414 ceph_decode_need(&p
, end
, maplen
, bad
);
1416 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1417 dout("applying incremental map %u len %d\n",
1419 newmap
= osdmap_apply_incremental(&p
, next
,
1421 &osdc
->client
->msgr
);
1422 if (IS_ERR(newmap
)) {
1423 err
= PTR_ERR(newmap
);
1427 if (newmap
!= osdc
->osdmap
) {
1428 ceph_osdmap_destroy(osdc
->osdmap
);
1429 osdc
->osdmap
= newmap
;
1431 kick_requests(osdc
, 0);
1432 reset_changed_osds(osdc
);
1434 dout("ignoring incremental map %u len %d\n",
1444 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1445 dout(" %d full maps\n", nr_maps
);
1447 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1448 epoch
= ceph_decode_32(&p
);
1449 maplen
= ceph_decode_32(&p
);
1450 ceph_decode_need(&p
, end
, maplen
, bad
);
1452 dout("skipping non-latest full map %u len %d\n",
1454 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1455 dout("skipping full map %u len %d, "
1456 "older than our %u\n", epoch
, maplen
,
1457 osdc
->osdmap
->epoch
);
1459 int skipped_map
= 0;
1461 dout("taking full map %u len %d\n", epoch
, maplen
);
1462 newmap
= osdmap_decode(&p
, p
+maplen
);
1463 if (IS_ERR(newmap
)) {
1464 err
= PTR_ERR(newmap
);
1468 oldmap
= osdc
->osdmap
;
1469 osdc
->osdmap
= newmap
;
1471 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
1473 ceph_osdmap_destroy(oldmap
);
1475 kick_requests(osdc
, skipped_map
);
1482 downgrade_write(&osdc
->map_sem
);
1483 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1486 * subscribe to subsequent osdmap updates if full to ensure
1487 * we find out when we are no longer full and stop returning
1490 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
))
1491 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1494 up_read(&osdc
->map_sem
);
1495 wake_up_all(&osdc
->client
->auth_wq
);
1499 pr_err("osdc handle_map corrupt msg\n");
1501 up_write(&osdc
->map_sem
);
1506 * watch/notify callback event infrastructure
1508 * These callbacks are used both for watch and notify operations.
1510 static void __release_event(struct kref
*kref
)
1512 struct ceph_osd_event
*event
=
1513 container_of(kref
, struct ceph_osd_event
, kref
);
1515 dout("__release_event %p\n", event
);
1519 static void get_event(struct ceph_osd_event
*event
)
1521 kref_get(&event
->kref
);
1524 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1526 kref_put(&event
->kref
, __release_event
);
1528 EXPORT_SYMBOL(ceph_osdc_put_event
);
1530 static void __insert_event(struct ceph_osd_client
*osdc
,
1531 struct ceph_osd_event
*new)
1533 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1534 struct rb_node
*parent
= NULL
;
1535 struct ceph_osd_event
*event
= NULL
;
1539 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1540 if (new->cookie
< event
->cookie
)
1542 else if (new->cookie
> event
->cookie
)
1543 p
= &(*p
)->rb_right
;
1548 rb_link_node(&new->node
, parent
, p
);
1549 rb_insert_color(&new->node
, &osdc
->event_tree
);
1552 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1555 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1556 struct rb_node
*parent
= NULL
;
1557 struct ceph_osd_event
*event
= NULL
;
1561 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1562 if (cookie
< event
->cookie
)
1564 else if (cookie
> event
->cookie
)
1565 p
= &(*p
)->rb_right
;
1572 static void __remove_event(struct ceph_osd_event
*event
)
1574 struct ceph_osd_client
*osdc
= event
->osdc
;
1576 if (!RB_EMPTY_NODE(&event
->node
)) {
1577 dout("__remove_event removed %p\n", event
);
1578 rb_erase(&event
->node
, &osdc
->event_tree
);
1579 ceph_osdc_put_event(event
);
1581 dout("__remove_event didn't remove %p\n", event
);
1585 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1586 void (*event_cb
)(u64
, u64
, u8
, void *),
1587 int one_shot
, void *data
,
1588 struct ceph_osd_event
**pevent
)
1590 struct ceph_osd_event
*event
;
1592 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1596 dout("create_event %p\n", event
);
1597 event
->cb
= event_cb
;
1598 event
->one_shot
= one_shot
;
1601 INIT_LIST_HEAD(&event
->osd_node
);
1602 kref_init(&event
->kref
); /* one ref for us */
1603 kref_get(&event
->kref
); /* one ref for the caller */
1604 init_completion(&event
->completion
);
1606 spin_lock(&osdc
->event_lock
);
1607 event
->cookie
= ++osdc
->event_count
;
1608 __insert_event(osdc
, event
);
1609 spin_unlock(&osdc
->event_lock
);
1614 EXPORT_SYMBOL(ceph_osdc_create_event
);
1616 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1618 struct ceph_osd_client
*osdc
= event
->osdc
;
1620 dout("cancel_event %p\n", event
);
1621 spin_lock(&osdc
->event_lock
);
1622 __remove_event(event
);
1623 spin_unlock(&osdc
->event_lock
);
1624 ceph_osdc_put_event(event
); /* caller's */
1626 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1629 static void do_event_work(struct work_struct
*work
)
1631 struct ceph_osd_event_work
*event_work
=
1632 container_of(work
, struct ceph_osd_event_work
, work
);
1633 struct ceph_osd_event
*event
= event_work
->event
;
1634 u64 ver
= event_work
->ver
;
1635 u64 notify_id
= event_work
->notify_id
;
1636 u8 opcode
= event_work
->opcode
;
1638 dout("do_event_work completing %p\n", event
);
1639 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1640 complete(&event
->completion
);
1641 dout("do_event_work completed %p\n", event
);
1642 ceph_osdc_put_event(event
);
1648 * Process osd watch notifications
1650 void handle_watch_notify(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1654 u64 cookie
, ver
, notify_id
;
1656 struct ceph_osd_event
*event
;
1657 struct ceph_osd_event_work
*event_work
;
1659 p
= msg
->front
.iov_base
;
1660 end
= p
+ msg
->front
.iov_len
;
1662 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1663 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1664 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1665 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1666 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1668 spin_lock(&osdc
->event_lock
);
1669 event
= __find_event(osdc
, cookie
);
1672 if (event
->one_shot
)
1673 __remove_event(event
);
1675 spin_unlock(&osdc
->event_lock
);
1676 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1677 cookie
, ver
, event
);
1679 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1681 dout("ERROR: could not allocate event_work\n");
1684 INIT_WORK(&event_work
->work
, do_event_work
);
1685 event_work
->event
= event
;
1686 event_work
->ver
= ver
;
1687 event_work
->notify_id
= notify_id
;
1688 event_work
->opcode
= opcode
;
1689 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1690 dout("WARNING: failed to queue notify event work\n");
1698 complete(&event
->completion
);
1699 ceph_osdc_put_event(event
);
1703 pr_err("osdc handle_watch_notify corrupt msg\n");
1707 int ceph_osdc_wait_event(struct ceph_osd_event
*event
, unsigned long timeout
)
1711 dout("wait_event %p\n", event
);
1712 err
= wait_for_completion_interruptible_timeout(&event
->completion
,
1714 ceph_osdc_put_event(event
);
1717 dout("wait_event %p returns %d\n", event
, err
);
1720 EXPORT_SYMBOL(ceph_osdc_wait_event
);
1723 * Register request, send initial attempt.
1725 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1726 struct ceph_osd_request
*req
,
1731 req
->r_request
->pages
= req
->r_pages
;
1732 req
->r_request
->nr_pages
= req
->r_num_pages
;
1734 req
->r_request
->bio
= req
->r_bio
;
1736 req
->r_request
->trail
= req
->r_trail
;
1738 register_request(osdc
, req
);
1740 down_read(&osdc
->map_sem
);
1741 mutex_lock(&osdc
->request_mutex
);
1743 * a racing kick_requests() may have sent the message for us
1744 * while we dropped request_mutex above, so only send now if
1745 * the request still han't been touched yet.
1747 if (req
->r_sent
== 0) {
1748 rc
= __map_request(osdc
, req
, 0);
1751 dout("osdc_start_request failed map, "
1752 " will retry %lld\n", req
->r_tid
);
1757 if (req
->r_osd
== NULL
) {
1758 dout("send_request %p no up osds in pg\n", req
);
1759 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1761 __send_request(osdc
, req
);
1767 mutex_unlock(&osdc
->request_mutex
);
1768 up_read(&osdc
->map_sem
);
1771 EXPORT_SYMBOL(ceph_osdc_start_request
);
1774 * wait for a request to complete
1776 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1777 struct ceph_osd_request
*req
)
1781 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1783 mutex_lock(&osdc
->request_mutex
);
1784 __cancel_request(req
);
1785 __unregister_request(osdc
, req
);
1786 mutex_unlock(&osdc
->request_mutex
);
1787 complete_request(req
);
1788 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1792 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1793 return req
->r_result
;
1795 EXPORT_SYMBOL(ceph_osdc_wait_request
);
1798 * sync - wait for all in-flight requests to flush. avoid starvation.
1800 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1802 struct ceph_osd_request
*req
;
1803 u64 last_tid
, next_tid
= 0;
1805 mutex_lock(&osdc
->request_mutex
);
1806 last_tid
= osdc
->last_tid
;
1808 req
= __lookup_request_ge(osdc
, next_tid
);
1811 if (req
->r_tid
> last_tid
)
1814 next_tid
= req
->r_tid
+ 1;
1815 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1818 ceph_osdc_get_request(req
);
1819 mutex_unlock(&osdc
->request_mutex
);
1820 dout("sync waiting on tid %llu (last is %llu)\n",
1821 req
->r_tid
, last_tid
);
1822 wait_for_completion(&req
->r_safe_completion
);
1823 mutex_lock(&osdc
->request_mutex
);
1824 ceph_osdc_put_request(req
);
1826 mutex_unlock(&osdc
->request_mutex
);
1827 dout("sync done (thru tid %llu)\n", last_tid
);
1829 EXPORT_SYMBOL(ceph_osdc_sync
);
1834 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
1839 osdc
->client
= client
;
1840 osdc
->osdmap
= NULL
;
1841 init_rwsem(&osdc
->map_sem
);
1842 init_completion(&osdc
->map_waiters
);
1843 osdc
->last_requested_map
= 0;
1844 mutex_init(&osdc
->request_mutex
);
1846 osdc
->osds
= RB_ROOT
;
1847 INIT_LIST_HEAD(&osdc
->osd_lru
);
1848 osdc
->requests
= RB_ROOT
;
1849 INIT_LIST_HEAD(&osdc
->req_lru
);
1850 INIT_LIST_HEAD(&osdc
->req_unsent
);
1851 INIT_LIST_HEAD(&osdc
->req_notarget
);
1852 INIT_LIST_HEAD(&osdc
->req_linger
);
1853 osdc
->num_requests
= 0;
1854 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
1855 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
1856 spin_lock_init(&osdc
->event_lock
);
1857 osdc
->event_tree
= RB_ROOT
;
1858 osdc
->event_count
= 0;
1860 schedule_delayed_work(&osdc
->osds_timeout_work
,
1861 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
1864 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
1865 sizeof(struct ceph_osd_request
));
1866 if (!osdc
->req_mempool
)
1869 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
1870 OSD_OP_FRONT_LEN
, 10, true,
1874 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
1875 OSD_OPREPLY_FRONT_LEN
, 10, true,
1880 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
1881 if (IS_ERR(osdc
->notify_wq
)) {
1882 err
= PTR_ERR(osdc
->notify_wq
);
1883 osdc
->notify_wq
= NULL
;
1889 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1891 mempool_destroy(osdc
->req_mempool
);
1895 EXPORT_SYMBOL(ceph_osdc_init
);
1897 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
1899 flush_workqueue(osdc
->notify_wq
);
1900 destroy_workqueue(osdc
->notify_wq
);
1901 cancel_delayed_work_sync(&osdc
->timeout_work
);
1902 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
1904 ceph_osdmap_destroy(osdc
->osdmap
);
1905 osdc
->osdmap
= NULL
;
1907 remove_all_osds(osdc
);
1908 mempool_destroy(osdc
->req_mempool
);
1909 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1910 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
1912 EXPORT_SYMBOL(ceph_osdc_stop
);
1915 * Read some contiguous pages. If we cross a stripe boundary, shorten
1916 * *plen. Return number of bytes read, or error.
1918 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
1919 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
1921 u32 truncate_seq
, u64 truncate_size
,
1922 struct page
**pages
, int num_pages
, int page_align
)
1924 struct ceph_osd_request
*req
;
1927 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
1928 vino
.snap
, off
, *plen
);
1929 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
1930 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
1931 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
1932 false, 1, page_align
);
1934 return PTR_ERR(req
);
1936 /* it may be a short read due to an object boundary */
1937 req
->r_pages
= pages
;
1939 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1940 off
, *plen
, req
->r_num_pages
, page_align
);
1942 rc
= ceph_osdc_start_request(osdc
, req
, false);
1944 rc
= ceph_osdc_wait_request(osdc
, req
);
1946 ceph_osdc_put_request(req
);
1947 dout("readpages result %d\n", rc
);
1950 EXPORT_SYMBOL(ceph_osdc_readpages
);
1953 * do a synchronous write on N pages
1955 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
1956 struct ceph_file_layout
*layout
,
1957 struct ceph_snap_context
*snapc
,
1959 u32 truncate_seq
, u64 truncate_size
,
1960 struct timespec
*mtime
,
1961 struct page
**pages
, int num_pages
,
1962 int flags
, int do_sync
, bool nofail
)
1964 struct ceph_osd_request
*req
;
1966 int page_align
= off
& ~PAGE_MASK
;
1968 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
1969 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
1971 flags
| CEPH_OSD_FLAG_ONDISK
|
1972 CEPH_OSD_FLAG_WRITE
,
1974 truncate_seq
, truncate_size
, mtime
,
1975 nofail
, 1, page_align
);
1977 return PTR_ERR(req
);
1979 /* it may be a short write due to an object boundary */
1980 req
->r_pages
= pages
;
1981 dout("writepages %llu~%llu (%d pages)\n", off
, len
,
1984 rc
= ceph_osdc_start_request(osdc
, req
, nofail
);
1986 rc
= ceph_osdc_wait_request(osdc
, req
);
1988 ceph_osdc_put_request(req
);
1991 dout("writepages result %d\n", rc
);
1994 EXPORT_SYMBOL(ceph_osdc_writepages
);
1997 * handle incoming message
1999 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2001 struct ceph_osd
*osd
= con
->private;
2002 struct ceph_osd_client
*osdc
;
2003 int type
= le16_to_cpu(msg
->hdr
.type
);
2010 case CEPH_MSG_OSD_MAP
:
2011 ceph_osdc_handle_map(osdc
, msg
);
2013 case CEPH_MSG_OSD_OPREPLY
:
2014 handle_reply(osdc
, msg
, con
);
2016 case CEPH_MSG_WATCH_NOTIFY
:
2017 handle_watch_notify(osdc
, msg
);
2021 pr_err("received unknown message type %d %s\n", type
,
2022 ceph_msg_type_name(type
));
2029 * lookup and return message for incoming reply. set up reply message
2032 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2033 struct ceph_msg_header
*hdr
,
2036 struct ceph_osd
*osd
= con
->private;
2037 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2039 struct ceph_osd_request
*req
;
2040 int front
= le32_to_cpu(hdr
->front_len
);
2041 int data_len
= le32_to_cpu(hdr
->data_len
);
2044 tid
= le64_to_cpu(hdr
->tid
);
2045 mutex_lock(&osdc
->request_mutex
);
2046 req
= __lookup_request(osdc
, tid
);
2050 dout("get_reply unknown tid %llu from osd%d\n", tid
,
2055 if (req
->r_con_filling_msg
) {
2056 dout("%s revoking msg %p from old con %p\n", __func__
,
2057 req
->r_reply
, req
->r_con_filling_msg
);
2058 ceph_msg_revoke_incoming(req
->r_reply
);
2059 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
2060 req
->r_con_filling_msg
= NULL
;
2063 if (front
> req
->r_reply
->front
.iov_len
) {
2064 pr_warning("get_reply front %d > preallocated %d\n",
2065 front
, (int)req
->r_reply
->front
.iov_len
);
2066 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
, false);
2069 ceph_msg_put(req
->r_reply
);
2072 m
= ceph_msg_get(req
->r_reply
);
2075 int want
= calc_pages_for(req
->r_page_alignment
, data_len
);
2077 if (unlikely(req
->r_num_pages
< want
)) {
2078 pr_warning("tid %lld reply has %d bytes %d pages, we"
2079 " had only %d pages ready\n", tid
, data_len
,
2080 want
, req
->r_num_pages
);
2086 m
->pages
= req
->r_pages
;
2087 m
->nr_pages
= req
->r_num_pages
;
2088 m
->page_alignment
= req
->r_page_alignment
;
2090 m
->bio
= req
->r_bio
;
2094 req
->r_con_filling_msg
= con
->ops
->get(con
);
2095 dout("get_reply tid %lld %p\n", tid
, m
);
2098 mutex_unlock(&osdc
->request_mutex
);
2103 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2104 struct ceph_msg_header
*hdr
,
2107 struct ceph_osd
*osd
= con
->private;
2108 int type
= le16_to_cpu(hdr
->type
);
2109 int front
= le32_to_cpu(hdr
->front_len
);
2113 case CEPH_MSG_OSD_MAP
:
2114 case CEPH_MSG_WATCH_NOTIFY
:
2115 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2116 case CEPH_MSG_OSD_OPREPLY
:
2117 return get_reply(con
, hdr
, skip
);
2119 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2127 * Wrappers to refcount containing ceph_osd struct
2129 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2131 struct ceph_osd
*osd
= con
->private;
2137 static void put_osd_con(struct ceph_connection
*con
)
2139 struct ceph_osd
*osd
= con
->private;
2147 * Note: returned pointer is the address of a structure that's
2148 * managed separately. Caller must *not* attempt to free it.
2150 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2151 int *proto
, int force_new
)
2153 struct ceph_osd
*o
= con
->private;
2154 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2155 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2156 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2158 if (force_new
&& auth
->authorizer
) {
2159 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
2160 ac
->ops
->destroy_authorizer(ac
, auth
->authorizer
);
2161 auth
->authorizer
= NULL
;
2163 if (!auth
->authorizer
&& ac
->ops
&& ac
->ops
->create_authorizer
) {
2164 int ret
= ac
->ops
->create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2167 return ERR_PTR(ret
);
2169 *proto
= ac
->protocol
;
2175 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2177 struct ceph_osd
*o
= con
->private;
2178 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2179 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2182 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2183 * XXX which do we do: succeed or fail?
2185 return ac
->ops
->verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
2188 static int invalidate_authorizer(struct ceph_connection
*con
)
2190 struct ceph_osd
*o
= con
->private;
2191 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2192 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2194 if (ac
->ops
&& ac
->ops
->invalidate_authorizer
)
2195 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2197 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2200 static const struct ceph_connection_operations osd_con_ops
= {
2203 .dispatch
= dispatch
,
2204 .get_authorizer
= get_authorizer
,
2205 .verify_authorizer_reply
= verify_authorizer_reply
,
2206 .invalidate_authorizer
= invalidate_authorizer
,
2207 .alloc_msg
= alloc_msg
,