1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops
;
26 static void send_queued(struct ceph_osd_client
*osdc
);
27 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
);
28 static void __register_request(struct ceph_osd_client
*osdc
,
29 struct ceph_osd_request
*req
);
30 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
31 struct ceph_osd_request
*req
);
32 static void __send_request(struct ceph_osd_client
*osdc
,
33 struct ceph_osd_request
*req
);
35 static int op_has_extent(int op
)
37 return (op
== CEPH_OSD_OP_READ
||
38 op
== CEPH_OSD_OP_WRITE
);
41 int ceph_calc_raw_layout(struct ceph_osd_client
*osdc
,
42 struct ceph_file_layout
*layout
,
44 u64 off
, u64
*plen
, u64
*bno
,
45 struct ceph_osd_request
*req
,
46 struct ceph_osd_req_op
*op
)
48 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
50 u64 objoff
, objlen
; /* extent in object */
53 reqhead
->snapid
= cpu_to_le64(snapid
);
56 r
= ceph_calc_file_object_mapping(layout
, off
, plen
, bno
,
61 dout(" skipping last %llu, final file extent %llu~%llu\n",
62 orig_len
- *plen
, off
, *plen
);
64 if (op_has_extent(op
->op
)) {
65 u32 osize
= le32_to_cpu(layout
->fl_object_size
);
66 op
->extent
.offset
= objoff
;
67 op
->extent
.length
= objlen
;
68 if (op
->extent
.truncate_size
<= off
- objoff
) {
69 op
->extent
.truncate_size
= 0;
71 op
->extent
.truncate_size
-= off
- objoff
;
72 if (op
->extent
.truncate_size
> osize
)
73 op
->extent
.truncate_size
= osize
;
76 req
->r_num_pages
= calc_pages_for(off
, *plen
);
77 req
->r_page_alignment
= off
& ~PAGE_MASK
;
78 if (op
->op
== CEPH_OSD_OP_WRITE
)
79 op
->payload_len
= *plen
;
81 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
82 *bno
, objoff
, objlen
, req
->r_num_pages
);
85 EXPORT_SYMBOL(ceph_calc_raw_layout
);
88 * Implement client access to distributed object storage cluster.
90 * All data objects are stored within a cluster/cloud of OSDs, or
91 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
92 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
93 * remote daemons serving up and coordinating consistent and safe
96 * Cluster membership and the mapping of data objects onto storage devices
97 * are described by the osd map.
99 * We keep track of pending OSD requests (read, write), resubmit
100 * requests to different OSDs when the cluster topology/data layout
101 * change, or retry the affected requests when the communications
102 * channel with an OSD is reset.
106 * calculate the mapping of a file extent onto an object, and fill out the
107 * request accordingly. shorten extent as necessary if it crosses an
110 * fill osd op in request message.
112 static int calc_layout(struct ceph_osd_client
*osdc
,
113 struct ceph_vino vino
,
114 struct ceph_file_layout
*layout
,
116 struct ceph_osd_request
*req
,
117 struct ceph_osd_req_op
*op
)
122 r
= ceph_calc_raw_layout(osdc
, layout
, vino
.snap
, off
,
123 plen
, &bno
, req
, op
);
127 snprintf(req
->r_oid
, sizeof(req
->r_oid
), "%llx.%08llx", vino
.ino
, bno
);
128 req
->r_oid_len
= strlen(req
->r_oid
);
136 void ceph_osdc_release_request(struct kref
*kref
)
138 struct ceph_osd_request
*req
= container_of(kref
,
139 struct ceph_osd_request
,
143 ceph_msg_put(req
->r_request
);
144 if (req
->r_con_filling_msg
) {
145 dout("%s revoking pages %p from con %p\n", __func__
,
146 req
->r_pages
, req
->r_con_filling_msg
);
147 ceph_msg_revoke_incoming(req
->r_reply
);
148 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
151 ceph_msg_put(req
->r_reply
);
152 if (req
->r_own_pages
)
153 ceph_release_page_vector(req
->r_pages
,
159 ceph_put_snap_context(req
->r_snapc
);
160 ceph_pagelist_release(&req
->r_trail
);
162 mempool_free(req
, req
->r_osdc
->req_mempool
);
166 EXPORT_SYMBOL(ceph_osdc_release_request
);
168 static int get_num_ops(struct ceph_osd_req_op
*ops
)
178 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
180 struct ceph_snap_context
*snapc
,
181 struct ceph_osd_req_op
*ops
,
187 struct ceph_osd_request
*req
;
188 struct ceph_msg
*msg
;
189 int num_op
= get_num_ops(ops
);
190 size_t msg_size
= sizeof(struct ceph_osd_request_head
);
192 msg_size
+= num_op
*sizeof(struct ceph_osd_op
);
195 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
196 memset(req
, 0, sizeof(*req
));
198 req
= kzalloc(sizeof(*req
), gfp_flags
);
204 req
->r_mempool
= use_mempool
;
206 kref_init(&req
->r_kref
);
207 init_completion(&req
->r_completion
);
208 init_completion(&req
->r_safe_completion
);
209 RB_CLEAR_NODE(&req
->r_node
);
210 INIT_LIST_HEAD(&req
->r_unsafe_item
);
211 INIT_LIST_HEAD(&req
->r_linger_item
);
212 INIT_LIST_HEAD(&req
->r_linger_osd
);
213 INIT_LIST_HEAD(&req
->r_req_lru_item
);
214 INIT_LIST_HEAD(&req
->r_osd_item
);
216 req
->r_flags
= flags
;
218 WARN_ON((flags
& (CEPH_OSD_FLAG_READ
|CEPH_OSD_FLAG_WRITE
)) == 0);
220 /* create reply message */
222 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
224 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
225 OSD_OPREPLY_FRONT_LEN
, gfp_flags
, true);
227 ceph_osdc_put_request(req
);
232 ceph_pagelist_init(&req
->r_trail
);
234 /* create request message; allow space for oid */
235 msg_size
+= MAX_OBJ_NAME_SIZE
;
237 msg_size
+= sizeof(u64
) * snapc
->num_snaps
;
239 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
241 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp_flags
, true);
243 ceph_osdc_put_request(req
);
247 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
249 req
->r_request
= msg
;
250 req
->r_pages
= pages
;
260 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
262 static void osd_req_encode_op(struct ceph_osd_request
*req
,
263 struct ceph_osd_op
*dst
,
264 struct ceph_osd_req_op
*src
)
266 dst
->op
= cpu_to_le16(src
->op
);
269 case CEPH_OSD_OP_READ
:
270 case CEPH_OSD_OP_WRITE
:
272 cpu_to_le64(src
->extent
.offset
);
274 cpu_to_le64(src
->extent
.length
);
275 dst
->extent
.truncate_size
=
276 cpu_to_le64(src
->extent
.truncate_size
);
277 dst
->extent
.truncate_seq
=
278 cpu_to_le32(src
->extent
.truncate_seq
);
281 case CEPH_OSD_OP_GETXATTR
:
282 case CEPH_OSD_OP_SETXATTR
:
283 case CEPH_OSD_OP_CMPXATTR
:
284 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
285 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
286 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
287 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
288 ceph_pagelist_append(&req
->r_trail
, src
->xattr
.name
,
289 src
->xattr
.name_len
);
290 ceph_pagelist_append(&req
->r_trail
, src
->xattr
.val
,
291 src
->xattr
.value_len
);
293 case CEPH_OSD_OP_CALL
:
294 dst
->cls
.class_len
= src
->cls
.class_len
;
295 dst
->cls
.method_len
= src
->cls
.method_len
;
296 dst
->cls
.indata_len
= cpu_to_le32(src
->cls
.indata_len
);
298 ceph_pagelist_append(&req
->r_trail
, src
->cls
.class_name
,
300 ceph_pagelist_append(&req
->r_trail
, src
->cls
.method_name
,
301 src
->cls
.method_len
);
302 ceph_pagelist_append(&req
->r_trail
, src
->cls
.indata
,
303 src
->cls
.indata_len
);
305 case CEPH_OSD_OP_ROLLBACK
:
306 dst
->snap
.snapid
= cpu_to_le64(src
->snap
.snapid
);
308 case CEPH_OSD_OP_STARTSYNC
:
310 case CEPH_OSD_OP_NOTIFY
:
312 __le32 prot_ver
= cpu_to_le32(src
->watch
.prot_ver
);
313 __le32 timeout
= cpu_to_le32(src
->watch
.timeout
);
315 ceph_pagelist_append(&req
->r_trail
,
316 &prot_ver
, sizeof(prot_ver
));
317 ceph_pagelist_append(&req
->r_trail
,
318 &timeout
, sizeof(timeout
));
320 case CEPH_OSD_OP_NOTIFY_ACK
:
321 case CEPH_OSD_OP_WATCH
:
322 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
323 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
324 dst
->watch
.flag
= src
->watch
.flag
;
327 pr_err("unrecognized osd opcode %d\n", dst
->op
);
331 dst
->payload_len
= cpu_to_le32(src
->payload_len
);
335 * build new request AND message
338 void ceph_osdc_build_request(struct ceph_osd_request
*req
,
340 struct ceph_osd_req_op
*src_ops
,
341 struct ceph_snap_context
*snapc
,
342 struct timespec
*mtime
)
344 struct ceph_msg
*msg
= req
->r_request
;
345 struct ceph_osd_request_head
*head
;
346 struct ceph_osd_req_op
*src_op
;
347 struct ceph_osd_op
*op
;
349 int num_op
= get_num_ops(src_ops
);
350 size_t msg_size
= sizeof(*head
) + num_op
*sizeof(*op
);
351 int flags
= req
->r_flags
;
355 head
= msg
->front
.iov_base
;
356 op
= (void *)(head
+ 1);
357 p
= (void *)(op
+ num_op
);
359 req
->r_snapc
= ceph_get_snap_context(snapc
);
361 head
->client_inc
= cpu_to_le32(1); /* always, for now. */
362 head
->flags
= cpu_to_le32(flags
);
363 if (flags
& CEPH_OSD_FLAG_WRITE
)
364 ceph_encode_timespec(&head
->mtime
, mtime
);
365 head
->num_ops
= cpu_to_le16(num_op
);
369 head
->object_len
= cpu_to_le32(req
->r_oid_len
);
370 memcpy(p
, req
->r_oid
, req
->r_oid_len
);
375 osd_req_encode_op(req
, op
, src_op
);
380 data_len
+= req
->r_trail
.length
;
383 head
->snap_seq
= cpu_to_le64(snapc
->seq
);
384 head
->num_snaps
= cpu_to_le32(snapc
->num_snaps
);
385 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
386 put_unaligned_le64(snapc
->snaps
[i
], p
);
391 if (flags
& CEPH_OSD_FLAG_WRITE
) {
392 req
->r_request
->hdr
.data_off
= cpu_to_le16(off
);
393 req
->r_request
->hdr
.data_len
= cpu_to_le32(*plen
+ data_len
);
394 } else if (data_len
) {
395 req
->r_request
->hdr
.data_off
= 0;
396 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
399 req
->r_request
->page_alignment
= req
->r_page_alignment
;
401 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
402 msg_size
= p
- msg
->front
.iov_base
;
403 msg
->front
.iov_len
= msg_size
;
404 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
407 EXPORT_SYMBOL(ceph_osdc_build_request
);
410 * build new request AND message, calculate layout, and adjust file
413 * if the file was recently truncated, we include information about its
414 * old and new size so that the object can be updated appropriately. (we
415 * avoid synchronously deleting truncated objects because it's slow.)
417 * if @do_sync, include a 'startsync' command so that the osd will flush
420 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
421 struct ceph_file_layout
*layout
,
422 struct ceph_vino vino
,
424 int opcode
, int flags
,
425 struct ceph_snap_context
*snapc
,
429 struct timespec
*mtime
,
430 bool use_mempool
, int num_reply
,
433 struct ceph_osd_req_op ops
[3];
434 struct ceph_osd_request
*req
;
438 ops
[0].extent
.truncate_seq
= truncate_seq
;
439 ops
[0].extent
.truncate_size
= truncate_size
;
440 ops
[0].payload_len
= 0;
443 ops
[1].op
= CEPH_OSD_OP_STARTSYNC
;
444 ops
[1].payload_len
= 0;
449 req
= ceph_osdc_alloc_request(osdc
, flags
,
452 GFP_NOFS
, NULL
, NULL
);
454 return ERR_PTR(-ENOMEM
);
456 /* calculate max write size */
457 r
= calc_layout(osdc
, vino
, layout
, off
, plen
, req
, ops
);
460 req
->r_file_layout
= *layout
; /* keep a copy */
462 /* in case it differs from natural (file) alignment that
463 calc_layout filled in for us */
464 req
->r_num_pages
= calc_pages_for(page_align
, *plen
);
465 req
->r_page_alignment
= page_align
;
467 ceph_osdc_build_request(req
, off
, plen
, ops
,
473 EXPORT_SYMBOL(ceph_osdc_new_request
);
476 * We keep osd requests in an rbtree, sorted by ->r_tid.
478 static void __insert_request(struct ceph_osd_client
*osdc
,
479 struct ceph_osd_request
*new)
481 struct rb_node
**p
= &osdc
->requests
.rb_node
;
482 struct rb_node
*parent
= NULL
;
483 struct ceph_osd_request
*req
= NULL
;
487 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
488 if (new->r_tid
< req
->r_tid
)
490 else if (new->r_tid
> req
->r_tid
)
496 rb_link_node(&new->r_node
, parent
, p
);
497 rb_insert_color(&new->r_node
, &osdc
->requests
);
500 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
503 struct ceph_osd_request
*req
;
504 struct rb_node
*n
= osdc
->requests
.rb_node
;
507 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
508 if (tid
< req
->r_tid
)
510 else if (tid
> req
->r_tid
)
518 static struct ceph_osd_request
*
519 __lookup_request_ge(struct ceph_osd_client
*osdc
,
522 struct ceph_osd_request
*req
;
523 struct rb_node
*n
= osdc
->requests
.rb_node
;
526 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
527 if (tid
< req
->r_tid
) {
531 } else if (tid
> req
->r_tid
) {
541 * Resubmit requests pending on the given osd.
543 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
544 struct ceph_osd
*osd
)
546 struct ceph_osd_request
*req
, *nreq
;
549 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
550 err
= __reset_osd(osdc
, osd
);
554 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
555 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
556 dout("requeued %p tid %llu osd%d\n", req
, req
->r_tid
,
559 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
562 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
565 * reregister request prior to unregistering linger so
566 * that r_osd is preserved.
568 BUG_ON(!list_empty(&req
->r_req_lru_item
));
569 __register_request(osdc
, req
);
570 list_add(&req
->r_req_lru_item
, &osdc
->req_unsent
);
571 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
572 __unregister_linger_request(osdc
, req
);
573 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
579 * If the osd connection drops, we need to resubmit all requests.
581 static void osd_reset(struct ceph_connection
*con
)
583 struct ceph_osd
*osd
= con
->private;
584 struct ceph_osd_client
*osdc
;
588 dout("osd_reset osd%d\n", osd
->o_osd
);
590 down_read(&osdc
->map_sem
);
591 mutex_lock(&osdc
->request_mutex
);
592 __kick_osd_requests(osdc
, osd
);
593 mutex_unlock(&osdc
->request_mutex
);
595 up_read(&osdc
->map_sem
);
599 * Track open sessions with osds.
601 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
603 struct ceph_osd
*osd
;
605 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
609 atomic_set(&osd
->o_ref
, 1);
612 RB_CLEAR_NODE(&osd
->o_node
);
613 INIT_LIST_HEAD(&osd
->o_requests
);
614 INIT_LIST_HEAD(&osd
->o_linger_requests
);
615 INIT_LIST_HEAD(&osd
->o_osd_lru
);
616 osd
->o_incarnation
= 1;
618 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
620 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
624 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
626 if (atomic_inc_not_zero(&osd
->o_ref
)) {
627 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
628 atomic_read(&osd
->o_ref
));
631 dout("get_osd %p FAIL\n", osd
);
636 static void put_osd(struct ceph_osd
*osd
)
638 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
639 atomic_read(&osd
->o_ref
) - 1);
640 if (atomic_dec_and_test(&osd
->o_ref
) && osd
->o_auth
.authorizer
) {
641 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
643 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
644 ac
->ops
->destroy_authorizer(ac
, osd
->o_auth
.authorizer
);
650 * remove an osd from our map
652 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
654 dout("__remove_osd %p\n", osd
);
655 BUG_ON(!list_empty(&osd
->o_requests
));
656 rb_erase(&osd
->o_node
, &osdc
->osds
);
657 list_del_init(&osd
->o_osd_lru
);
658 ceph_con_close(&osd
->o_con
);
662 static void remove_all_osds(struct ceph_osd_client
*osdc
)
664 dout("%s %p\n", __func__
, osdc
);
665 mutex_lock(&osdc
->request_mutex
);
666 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
667 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
668 struct ceph_osd
, o_node
);
669 __remove_osd(osdc
, osd
);
671 mutex_unlock(&osdc
->request_mutex
);
674 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
675 struct ceph_osd
*osd
)
677 dout("__move_osd_to_lru %p\n", osd
);
678 BUG_ON(!list_empty(&osd
->o_osd_lru
));
679 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
680 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
683 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
685 dout("__remove_osd_from_lru %p\n", osd
);
686 if (!list_empty(&osd
->o_osd_lru
))
687 list_del_init(&osd
->o_osd_lru
);
690 static void remove_old_osds(struct ceph_osd_client
*osdc
)
692 struct ceph_osd
*osd
, *nosd
;
694 dout("__remove_old_osds %p\n", osdc
);
695 mutex_lock(&osdc
->request_mutex
);
696 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
697 if (time_before(jiffies
, osd
->lru_ttl
))
699 __remove_osd(osdc
, osd
);
701 mutex_unlock(&osdc
->request_mutex
);
707 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
709 struct ceph_entity_addr
*peer_addr
;
711 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
712 if (list_empty(&osd
->o_requests
) &&
713 list_empty(&osd
->o_linger_requests
)) {
714 __remove_osd(osdc
, osd
);
719 peer_addr
= &osdc
->osdmap
->osd_addr
[osd
->o_osd
];
720 if (!memcmp(peer_addr
, &osd
->o_con
.peer_addr
, sizeof (*peer_addr
)) &&
721 !ceph_con_opened(&osd
->o_con
)) {
722 struct ceph_osd_request
*req
;
724 dout(" osd addr hasn't changed and connection never opened,"
725 " letting msgr retry");
726 /* touch each r_stamp for handle_timeout()'s benfit */
727 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
728 req
->r_stamp
= jiffies
;
733 ceph_con_close(&osd
->o_con
);
734 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
, peer_addr
);
735 osd
->o_incarnation
++;
740 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
742 struct rb_node
**p
= &osdc
->osds
.rb_node
;
743 struct rb_node
*parent
= NULL
;
744 struct ceph_osd
*osd
= NULL
;
746 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
749 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
750 if (new->o_osd
< osd
->o_osd
)
752 else if (new->o_osd
> osd
->o_osd
)
758 rb_link_node(&new->o_node
, parent
, p
);
759 rb_insert_color(&new->o_node
, &osdc
->osds
);
762 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
764 struct ceph_osd
*osd
;
765 struct rb_node
*n
= osdc
->osds
.rb_node
;
768 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
771 else if (o
> osd
->o_osd
)
779 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
781 schedule_delayed_work(&osdc
->timeout_work
,
782 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
785 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
787 cancel_delayed_work(&osdc
->timeout_work
);
791 * Register request, assign tid. If this is the first request, set up
794 static void __register_request(struct ceph_osd_client
*osdc
,
795 struct ceph_osd_request
*req
)
797 req
->r_tid
= ++osdc
->last_tid
;
798 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
799 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
800 __insert_request(osdc
, req
);
801 ceph_osdc_get_request(req
);
802 osdc
->num_requests
++;
803 if (osdc
->num_requests
== 1) {
804 dout(" first request, scheduling timeout\n");
805 __schedule_osd_timeout(osdc
);
809 static void register_request(struct ceph_osd_client
*osdc
,
810 struct ceph_osd_request
*req
)
812 mutex_lock(&osdc
->request_mutex
);
813 __register_request(osdc
, req
);
814 mutex_unlock(&osdc
->request_mutex
);
818 * called under osdc->request_mutex
820 static void __unregister_request(struct ceph_osd_client
*osdc
,
821 struct ceph_osd_request
*req
)
823 if (RB_EMPTY_NODE(&req
->r_node
)) {
824 dout("__unregister_request %p tid %lld not registered\n",
829 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
830 rb_erase(&req
->r_node
, &osdc
->requests
);
831 osdc
->num_requests
--;
834 /* make sure the original request isn't in flight. */
835 ceph_msg_revoke(req
->r_request
);
837 list_del_init(&req
->r_osd_item
);
838 if (list_empty(&req
->r_osd
->o_requests
) &&
839 list_empty(&req
->r_osd
->o_linger_requests
)) {
840 dout("moving osd to %p lru\n", req
->r_osd
);
841 __move_osd_to_lru(osdc
, req
->r_osd
);
843 if (list_empty(&req
->r_linger_item
))
847 list_del_init(&req
->r_req_lru_item
);
848 ceph_osdc_put_request(req
);
850 if (osdc
->num_requests
== 0) {
851 dout(" no requests, canceling timeout\n");
852 __cancel_osd_timeout(osdc
);
857 * Cancel a previously queued request message
859 static void __cancel_request(struct ceph_osd_request
*req
)
861 if (req
->r_sent
&& req
->r_osd
) {
862 ceph_msg_revoke(req
->r_request
);
867 static void __register_linger_request(struct ceph_osd_client
*osdc
,
868 struct ceph_osd_request
*req
)
870 dout("__register_linger_request %p\n", req
);
871 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
873 list_add_tail(&req
->r_linger_osd
,
874 &req
->r_osd
->o_linger_requests
);
877 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
878 struct ceph_osd_request
*req
)
880 dout("__unregister_linger_request %p\n", req
);
881 list_del_init(&req
->r_linger_item
);
883 list_del_init(&req
->r_linger_osd
);
885 if (list_empty(&req
->r_osd
->o_requests
) &&
886 list_empty(&req
->r_osd
->o_linger_requests
)) {
887 dout("moving osd to %p lru\n", req
->r_osd
);
888 __move_osd_to_lru(osdc
, req
->r_osd
);
890 if (list_empty(&req
->r_osd_item
))
895 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
896 struct ceph_osd_request
*req
)
898 mutex_lock(&osdc
->request_mutex
);
900 __unregister_linger_request(osdc
, req
);
901 ceph_osdc_put_request(req
);
903 mutex_unlock(&osdc
->request_mutex
);
905 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
907 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
908 struct ceph_osd_request
*req
)
910 if (!req
->r_linger
) {
911 dout("set_request_linger %p\n", req
);
914 * caller is now responsible for calling
915 * unregister_linger_request
917 ceph_osdc_get_request(req
);
920 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
923 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
924 * (as needed), and set the request r_osd appropriately. If there is
925 * no up osd, set r_osd to NULL. Move the request to the appropriate list
926 * (unsent, homeless) or leave on in-flight lru.
928 * Return 0 if unchanged, 1 if changed, or negative on error.
930 * Caller should hold map_sem for read and request_mutex.
932 static int __map_request(struct ceph_osd_client
*osdc
,
933 struct ceph_osd_request
*req
, int force_resend
)
935 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
937 int acting
[CEPH_PG_MAX_SIZE
];
941 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
942 err
= ceph_calc_object_layout(&reqhead
->layout
, req
->r_oid
,
943 &req
->r_file_layout
, osdc
->osdmap
);
945 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
948 pgid
= reqhead
->layout
.ol_pgid
;
951 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
957 if ((!force_resend
&&
958 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
959 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
960 req
->r_num_pg_osds
== num
&&
961 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
962 (req
->r_osd
== NULL
&& o
== -1))
963 return 0; /* no change */
965 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
966 req
->r_tid
, le32_to_cpu(pgid
.pool
), le16_to_cpu(pgid
.ps
), o
,
967 req
->r_osd
? req
->r_osd
->o_osd
: -1);
969 /* record full pg acting set */
970 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
971 req
->r_num_pg_osds
= num
;
974 __cancel_request(req
);
975 list_del_init(&req
->r_osd_item
);
979 req
->r_osd
= __lookup_osd(osdc
, o
);
980 if (!req
->r_osd
&& o
>= 0) {
982 req
->r_osd
= create_osd(osdc
, o
);
984 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
988 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
989 __insert_osd(osdc
, req
->r_osd
);
991 ceph_con_open(&req
->r_osd
->o_con
,
992 CEPH_ENTITY_TYPE_OSD
, o
,
993 &osdc
->osdmap
->osd_addr
[o
]);
997 __remove_osd_from_lru(req
->r_osd
);
998 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
999 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1001 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1003 err
= 1; /* osd or pg changed */
1010 * caller should hold map_sem (for read) and request_mutex
1012 static void __send_request(struct ceph_osd_client
*osdc
,
1013 struct ceph_osd_request
*req
)
1015 struct ceph_osd_request_head
*reqhead
;
1017 dout("send_request %p tid %llu to osd%d flags %d\n",
1018 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
);
1020 reqhead
= req
->r_request
->front
.iov_base
;
1021 reqhead
->osdmap_epoch
= cpu_to_le32(osdc
->osdmap
->epoch
);
1022 reqhead
->flags
|= cpu_to_le32(req
->r_flags
); /* e.g., RETRY */
1023 reqhead
->reassert_version
= req
->r_reassert_version
;
1025 req
->r_stamp
= jiffies
;
1026 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1028 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1029 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1030 req
->r_sent
= req
->r_osd
->o_incarnation
;
1034 * Send any requests in the queue (req_unsent).
1036 static void send_queued(struct ceph_osd_client
*osdc
)
1038 struct ceph_osd_request
*req
, *tmp
;
1040 dout("send_queued\n");
1041 mutex_lock(&osdc
->request_mutex
);
1042 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
) {
1043 __send_request(osdc
, req
);
1045 mutex_unlock(&osdc
->request_mutex
);
1049 * Timeout callback, called every N seconds when 1 or more osd
1050 * requests has been active for more than N seconds. When this
1051 * happens, we ping all OSDs with requests who have timed out to
1052 * ensure any communications channel reset is detected. Reset the
1053 * request timeouts another N seconds in the future as we go.
1054 * Reschedule the timeout event another N seconds in future (unless
1055 * there are no open requests).
1057 static void handle_timeout(struct work_struct
*work
)
1059 struct ceph_osd_client
*osdc
=
1060 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1061 struct ceph_osd_request
*req
;
1062 struct ceph_osd
*osd
;
1063 unsigned long keepalive
=
1064 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1065 struct list_head slow_osds
;
1067 down_read(&osdc
->map_sem
);
1069 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1071 mutex_lock(&osdc
->request_mutex
);
1074 * ping osds that are a bit slow. this ensures that if there
1075 * is a break in the TCP connection we will notice, and reopen
1076 * a connection with that osd (from the fault callback).
1078 INIT_LIST_HEAD(&slow_osds
);
1079 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1080 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1085 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1086 req
->r_tid
, osd
->o_osd
);
1087 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1089 while (!list_empty(&slow_osds
)) {
1090 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1092 list_del_init(&osd
->o_keepalive_item
);
1093 ceph_con_keepalive(&osd
->o_con
);
1096 __schedule_osd_timeout(osdc
);
1097 mutex_unlock(&osdc
->request_mutex
);
1099 up_read(&osdc
->map_sem
);
1102 static void handle_osds_timeout(struct work_struct
*work
)
1104 struct ceph_osd_client
*osdc
=
1105 container_of(work
, struct ceph_osd_client
,
1106 osds_timeout_work
.work
);
1107 unsigned long delay
=
1108 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1110 dout("osds timeout\n");
1111 down_read(&osdc
->map_sem
);
1112 remove_old_osds(osdc
);
1113 up_read(&osdc
->map_sem
);
1115 schedule_delayed_work(&osdc
->osds_timeout_work
,
1116 round_jiffies_relative(delay
));
1119 static void complete_request(struct ceph_osd_request
*req
)
1121 if (req
->r_safe_callback
)
1122 req
->r_safe_callback(req
, NULL
);
1123 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1127 * handle osd op reply. either call the callback if it is specified,
1128 * or do the completion to wake up the waiting thread.
1130 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1131 struct ceph_connection
*con
)
1133 struct ceph_osd_reply_head
*rhead
= msg
->front
.iov_base
;
1134 struct ceph_osd_request
*req
;
1136 int numops
, object_len
, flags
;
1139 tid
= le64_to_cpu(msg
->hdr
.tid
);
1140 if (msg
->front
.iov_len
< sizeof(*rhead
))
1142 numops
= le32_to_cpu(rhead
->num_ops
);
1143 object_len
= le32_to_cpu(rhead
->object_len
);
1144 result
= le32_to_cpu(rhead
->result
);
1145 if (msg
->front
.iov_len
!= sizeof(*rhead
) + object_len
+
1146 numops
* sizeof(struct ceph_osd_op
))
1148 dout("handle_reply %p tid %llu result %d\n", msg
, tid
, (int)result
);
1150 mutex_lock(&osdc
->request_mutex
);
1151 req
= __lookup_request(osdc
, tid
);
1153 dout("handle_reply tid %llu dne\n", tid
);
1154 mutex_unlock(&osdc
->request_mutex
);
1157 ceph_osdc_get_request(req
);
1158 flags
= le32_to_cpu(rhead
->flags
);
1161 * if this connection filled our message, drop our reference now, to
1162 * avoid a (safe but slower) revoke later.
1164 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
1165 dout(" dropping con_filling_msg ref %p\n", con
);
1166 req
->r_con_filling_msg
= NULL
;
1170 if (!req
->r_got_reply
) {
1173 req
->r_result
= le32_to_cpu(rhead
->result
);
1174 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1175 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1177 if (req
->r_result
== 0)
1178 req
->r_result
= bytes
;
1180 /* in case this is a write and we need to replay, */
1181 req
->r_reassert_version
= rhead
->reassert_version
;
1183 req
->r_got_reply
= 1;
1184 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1185 dout("handle_reply tid %llu dup ack\n", tid
);
1186 mutex_unlock(&osdc
->request_mutex
);
1190 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1192 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1193 __register_linger_request(osdc
, req
);
1195 /* either this is a read, or we got the safe response */
1197 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1198 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1199 __unregister_request(osdc
, req
);
1201 mutex_unlock(&osdc
->request_mutex
);
1203 if (req
->r_callback
)
1204 req
->r_callback(req
, msg
);
1206 complete_all(&req
->r_completion
);
1208 if (flags
& CEPH_OSD_FLAG_ONDISK
)
1209 complete_request(req
);
1212 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1213 ceph_osdc_put_request(req
);
1217 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1218 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
),
1219 (int)sizeof(*rhead
));
1223 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1225 struct rb_node
*p
, *n
;
1227 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1228 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1231 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1232 memcmp(&osd
->o_con
.peer_addr
,
1233 ceph_osd_addr(osdc
->osdmap
,
1235 sizeof(struct ceph_entity_addr
)) != 0)
1236 __reset_osd(osdc
, osd
);
1241 * Requeue requests whose mapping to an OSD has changed. If requests map to
1242 * no osd, request a new map.
1244 * Caller should hold map_sem for read.
1246 static void kick_requests(struct ceph_osd_client
*osdc
, int force_resend
)
1248 struct ceph_osd_request
*req
, *nreq
;
1253 dout("kick_requests %s\n", force_resend
? " (force resend)" : "");
1254 mutex_lock(&osdc
->request_mutex
);
1255 for (p
= rb_first(&osdc
->requests
); p
; ) {
1256 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1260 * For linger requests that have not yet been
1261 * registered, move them to the linger list; they'll
1262 * be sent to the osd in the loop below. Unregister
1263 * the request before re-registering it as a linger
1264 * request to ensure the __map_request() below
1265 * will decide it needs to be sent.
1267 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
1268 dout("%p tid %llu restart on osd%d\n",
1270 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1271 __unregister_request(osdc
, req
);
1272 __register_linger_request(osdc
, req
);
1276 err
= __map_request(osdc
, req
, force_resend
);
1278 continue; /* error */
1279 if (req
->r_osd
== NULL
) {
1280 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1281 needmap
++; /* request a newer map */
1282 } else if (err
> 0) {
1283 if (!req
->r_linger
) {
1284 dout("%p tid %llu requeued on osd%d\n", req
,
1286 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1287 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1292 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1294 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1296 err
= __map_request(osdc
, req
, force_resend
);
1297 dout("__map_request returned %d\n", err
);
1299 continue; /* no change and no osd was specified */
1301 continue; /* hrm! */
1302 if (req
->r_osd
== NULL
) {
1303 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1304 needmap
++; /* request a newer map */
1308 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1309 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1310 __register_request(osdc
, req
);
1311 __unregister_linger_request(osdc
, req
);
1313 mutex_unlock(&osdc
->request_mutex
);
1316 dout("%d requests for down osds, need new map\n", needmap
);
1317 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1319 reset_changed_osds(osdc
);
1324 * Process updated osd map.
1326 * The message contains any number of incremental and full maps, normally
1327 * indicating some sort of topology change in the cluster. Kick requests
1328 * off to different OSDs as needed.
1330 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1332 void *p
, *end
, *next
;
1333 u32 nr_maps
, maplen
;
1335 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1337 struct ceph_fsid fsid
;
1339 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1340 p
= msg
->front
.iov_base
;
1341 end
= p
+ msg
->front
.iov_len
;
1344 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1345 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1346 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1349 down_write(&osdc
->map_sem
);
1351 /* incremental maps */
1352 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1353 dout(" %d inc maps\n", nr_maps
);
1354 while (nr_maps
> 0) {
1355 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1356 epoch
= ceph_decode_32(&p
);
1357 maplen
= ceph_decode_32(&p
);
1358 ceph_decode_need(&p
, end
, maplen
, bad
);
1360 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1361 dout("applying incremental map %u len %d\n",
1363 newmap
= osdmap_apply_incremental(&p
, next
,
1365 &osdc
->client
->msgr
);
1366 if (IS_ERR(newmap
)) {
1367 err
= PTR_ERR(newmap
);
1371 if (newmap
!= osdc
->osdmap
) {
1372 ceph_osdmap_destroy(osdc
->osdmap
);
1373 osdc
->osdmap
= newmap
;
1375 kick_requests(osdc
, 0);
1377 dout("ignoring incremental map %u len %d\n",
1387 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1388 dout(" %d full maps\n", nr_maps
);
1390 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1391 epoch
= ceph_decode_32(&p
);
1392 maplen
= ceph_decode_32(&p
);
1393 ceph_decode_need(&p
, end
, maplen
, bad
);
1395 dout("skipping non-latest full map %u len %d\n",
1397 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1398 dout("skipping full map %u len %d, "
1399 "older than our %u\n", epoch
, maplen
,
1400 osdc
->osdmap
->epoch
);
1402 int skipped_map
= 0;
1404 dout("taking full map %u len %d\n", epoch
, maplen
);
1405 newmap
= osdmap_decode(&p
, p
+maplen
);
1406 if (IS_ERR(newmap
)) {
1407 err
= PTR_ERR(newmap
);
1411 oldmap
= osdc
->osdmap
;
1412 osdc
->osdmap
= newmap
;
1414 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
1416 ceph_osdmap_destroy(oldmap
);
1418 kick_requests(osdc
, skipped_map
);
1425 downgrade_write(&osdc
->map_sem
);
1426 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1429 * subscribe to subsequent osdmap updates if full to ensure
1430 * we find out when we are no longer full and stop returning
1433 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
))
1434 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1437 up_read(&osdc
->map_sem
);
1438 wake_up_all(&osdc
->client
->auth_wq
);
1442 pr_err("osdc handle_map corrupt msg\n");
1444 up_write(&osdc
->map_sem
);
1449 * watch/notify callback event infrastructure
1451 * These callbacks are used both for watch and notify operations.
1453 static void __release_event(struct kref
*kref
)
1455 struct ceph_osd_event
*event
=
1456 container_of(kref
, struct ceph_osd_event
, kref
);
1458 dout("__release_event %p\n", event
);
1462 static void get_event(struct ceph_osd_event
*event
)
1464 kref_get(&event
->kref
);
1467 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1469 kref_put(&event
->kref
, __release_event
);
1471 EXPORT_SYMBOL(ceph_osdc_put_event
);
1473 static void __insert_event(struct ceph_osd_client
*osdc
,
1474 struct ceph_osd_event
*new)
1476 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1477 struct rb_node
*parent
= NULL
;
1478 struct ceph_osd_event
*event
= NULL
;
1482 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1483 if (new->cookie
< event
->cookie
)
1485 else if (new->cookie
> event
->cookie
)
1486 p
= &(*p
)->rb_right
;
1491 rb_link_node(&new->node
, parent
, p
);
1492 rb_insert_color(&new->node
, &osdc
->event_tree
);
1495 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1498 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1499 struct rb_node
*parent
= NULL
;
1500 struct ceph_osd_event
*event
= NULL
;
1504 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1505 if (cookie
< event
->cookie
)
1507 else if (cookie
> event
->cookie
)
1508 p
= &(*p
)->rb_right
;
1515 static void __remove_event(struct ceph_osd_event
*event
)
1517 struct ceph_osd_client
*osdc
= event
->osdc
;
1519 if (!RB_EMPTY_NODE(&event
->node
)) {
1520 dout("__remove_event removed %p\n", event
);
1521 rb_erase(&event
->node
, &osdc
->event_tree
);
1522 ceph_osdc_put_event(event
);
1524 dout("__remove_event didn't remove %p\n", event
);
1528 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1529 void (*event_cb
)(u64
, u64
, u8
, void *),
1530 int one_shot
, void *data
,
1531 struct ceph_osd_event
**pevent
)
1533 struct ceph_osd_event
*event
;
1535 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1539 dout("create_event %p\n", event
);
1540 event
->cb
= event_cb
;
1541 event
->one_shot
= one_shot
;
1544 INIT_LIST_HEAD(&event
->osd_node
);
1545 RB_CLEAR_NODE(&event
->node
);
1546 kref_init(&event
->kref
); /* one ref for us */
1547 kref_get(&event
->kref
); /* one ref for the caller */
1548 init_completion(&event
->completion
);
1550 spin_lock(&osdc
->event_lock
);
1551 event
->cookie
= ++osdc
->event_count
;
1552 __insert_event(osdc
, event
);
1553 spin_unlock(&osdc
->event_lock
);
1558 EXPORT_SYMBOL(ceph_osdc_create_event
);
1560 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1562 struct ceph_osd_client
*osdc
= event
->osdc
;
1564 dout("cancel_event %p\n", event
);
1565 spin_lock(&osdc
->event_lock
);
1566 __remove_event(event
);
1567 spin_unlock(&osdc
->event_lock
);
1568 ceph_osdc_put_event(event
); /* caller's */
1570 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1573 static void do_event_work(struct work_struct
*work
)
1575 struct ceph_osd_event_work
*event_work
=
1576 container_of(work
, struct ceph_osd_event_work
, work
);
1577 struct ceph_osd_event
*event
= event_work
->event
;
1578 u64 ver
= event_work
->ver
;
1579 u64 notify_id
= event_work
->notify_id
;
1580 u8 opcode
= event_work
->opcode
;
1582 dout("do_event_work completing %p\n", event
);
1583 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1584 complete(&event
->completion
);
1585 dout("do_event_work completed %p\n", event
);
1586 ceph_osdc_put_event(event
);
1592 * Process osd watch notifications
1594 void handle_watch_notify(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1598 u64 cookie
, ver
, notify_id
;
1600 struct ceph_osd_event
*event
;
1601 struct ceph_osd_event_work
*event_work
;
1603 p
= msg
->front
.iov_base
;
1604 end
= p
+ msg
->front
.iov_len
;
1606 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1607 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1608 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1609 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1610 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1612 spin_lock(&osdc
->event_lock
);
1613 event
= __find_event(osdc
, cookie
);
1616 if (event
->one_shot
)
1617 __remove_event(event
);
1619 spin_unlock(&osdc
->event_lock
);
1620 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1621 cookie
, ver
, event
);
1623 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1625 dout("ERROR: could not allocate event_work\n");
1628 INIT_WORK(&event_work
->work
, do_event_work
);
1629 event_work
->event
= event
;
1630 event_work
->ver
= ver
;
1631 event_work
->notify_id
= notify_id
;
1632 event_work
->opcode
= opcode
;
1633 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1634 dout("WARNING: failed to queue notify event work\n");
1642 complete(&event
->completion
);
1643 ceph_osdc_put_event(event
);
1647 pr_err("osdc handle_watch_notify corrupt msg\n");
1651 int ceph_osdc_wait_event(struct ceph_osd_event
*event
, unsigned long timeout
)
1655 dout("wait_event %p\n", event
);
1656 err
= wait_for_completion_interruptible_timeout(&event
->completion
,
1658 ceph_osdc_put_event(event
);
1661 dout("wait_event %p returns %d\n", event
, err
);
1664 EXPORT_SYMBOL(ceph_osdc_wait_event
);
1667 * Register request, send initial attempt.
1669 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1670 struct ceph_osd_request
*req
,
1675 req
->r_request
->pages
= req
->r_pages
;
1676 req
->r_request
->nr_pages
= req
->r_num_pages
;
1678 req
->r_request
->bio
= req
->r_bio
;
1680 req
->r_request
->trail
= &req
->r_trail
;
1682 register_request(osdc
, req
);
1684 down_read(&osdc
->map_sem
);
1685 mutex_lock(&osdc
->request_mutex
);
1687 * a racing kick_requests() may have sent the message for us
1688 * while we dropped request_mutex above, so only send now if
1689 * the request still han't been touched yet.
1691 if (req
->r_sent
== 0) {
1692 rc
= __map_request(osdc
, req
, 0);
1695 dout("osdc_start_request failed map, "
1696 " will retry %lld\n", req
->r_tid
);
1701 if (req
->r_osd
== NULL
) {
1702 dout("send_request %p no up osds in pg\n", req
);
1703 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1705 __send_request(osdc
, req
);
1711 mutex_unlock(&osdc
->request_mutex
);
1712 up_read(&osdc
->map_sem
);
1715 EXPORT_SYMBOL(ceph_osdc_start_request
);
1718 * wait for a request to complete
1720 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1721 struct ceph_osd_request
*req
)
1725 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1727 mutex_lock(&osdc
->request_mutex
);
1728 __cancel_request(req
);
1729 __unregister_request(osdc
, req
);
1730 mutex_unlock(&osdc
->request_mutex
);
1731 complete_request(req
);
1732 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1736 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1737 return req
->r_result
;
1739 EXPORT_SYMBOL(ceph_osdc_wait_request
);
1742 * sync - wait for all in-flight requests to flush. avoid starvation.
1744 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1746 struct ceph_osd_request
*req
;
1747 u64 last_tid
, next_tid
= 0;
1749 mutex_lock(&osdc
->request_mutex
);
1750 last_tid
= osdc
->last_tid
;
1752 req
= __lookup_request_ge(osdc
, next_tid
);
1755 if (req
->r_tid
> last_tid
)
1758 next_tid
= req
->r_tid
+ 1;
1759 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1762 ceph_osdc_get_request(req
);
1763 mutex_unlock(&osdc
->request_mutex
);
1764 dout("sync waiting on tid %llu (last is %llu)\n",
1765 req
->r_tid
, last_tid
);
1766 wait_for_completion(&req
->r_safe_completion
);
1767 mutex_lock(&osdc
->request_mutex
);
1768 ceph_osdc_put_request(req
);
1770 mutex_unlock(&osdc
->request_mutex
);
1771 dout("sync done (thru tid %llu)\n", last_tid
);
1773 EXPORT_SYMBOL(ceph_osdc_sync
);
1778 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
1783 osdc
->client
= client
;
1784 osdc
->osdmap
= NULL
;
1785 init_rwsem(&osdc
->map_sem
);
1786 init_completion(&osdc
->map_waiters
);
1787 osdc
->last_requested_map
= 0;
1788 mutex_init(&osdc
->request_mutex
);
1790 osdc
->osds
= RB_ROOT
;
1791 INIT_LIST_HEAD(&osdc
->osd_lru
);
1792 osdc
->requests
= RB_ROOT
;
1793 INIT_LIST_HEAD(&osdc
->req_lru
);
1794 INIT_LIST_HEAD(&osdc
->req_unsent
);
1795 INIT_LIST_HEAD(&osdc
->req_notarget
);
1796 INIT_LIST_HEAD(&osdc
->req_linger
);
1797 osdc
->num_requests
= 0;
1798 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
1799 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
1800 spin_lock_init(&osdc
->event_lock
);
1801 osdc
->event_tree
= RB_ROOT
;
1802 osdc
->event_count
= 0;
1804 schedule_delayed_work(&osdc
->osds_timeout_work
,
1805 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
1808 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
1809 sizeof(struct ceph_osd_request
));
1810 if (!osdc
->req_mempool
)
1813 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
1814 OSD_OP_FRONT_LEN
, 10, true,
1818 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
1819 OSD_OPREPLY_FRONT_LEN
, 10, true,
1824 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
1825 if (IS_ERR(osdc
->notify_wq
)) {
1826 err
= PTR_ERR(osdc
->notify_wq
);
1827 osdc
->notify_wq
= NULL
;
1833 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1835 mempool_destroy(osdc
->req_mempool
);
1839 EXPORT_SYMBOL(ceph_osdc_init
);
1841 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
1843 flush_workqueue(osdc
->notify_wq
);
1844 destroy_workqueue(osdc
->notify_wq
);
1845 cancel_delayed_work_sync(&osdc
->timeout_work
);
1846 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
1848 ceph_osdmap_destroy(osdc
->osdmap
);
1849 osdc
->osdmap
= NULL
;
1851 remove_all_osds(osdc
);
1852 mempool_destroy(osdc
->req_mempool
);
1853 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1854 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
1856 EXPORT_SYMBOL(ceph_osdc_stop
);
1859 * Read some contiguous pages. If we cross a stripe boundary, shorten
1860 * *plen. Return number of bytes read, or error.
1862 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
1863 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
1865 u32 truncate_seq
, u64 truncate_size
,
1866 struct page
**pages
, int num_pages
, int page_align
)
1868 struct ceph_osd_request
*req
;
1871 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
1872 vino
.snap
, off
, *plen
);
1873 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
1874 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
1875 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
1876 false, 1, page_align
);
1878 return PTR_ERR(req
);
1880 /* it may be a short read due to an object boundary */
1881 req
->r_pages
= pages
;
1883 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1884 off
, *plen
, req
->r_num_pages
, page_align
);
1886 rc
= ceph_osdc_start_request(osdc
, req
, false);
1888 rc
= ceph_osdc_wait_request(osdc
, req
);
1890 ceph_osdc_put_request(req
);
1891 dout("readpages result %d\n", rc
);
1894 EXPORT_SYMBOL(ceph_osdc_readpages
);
1897 * do a synchronous write on N pages
1899 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
1900 struct ceph_file_layout
*layout
,
1901 struct ceph_snap_context
*snapc
,
1903 u32 truncate_seq
, u64 truncate_size
,
1904 struct timespec
*mtime
,
1905 struct page
**pages
, int num_pages
,
1906 int flags
, int do_sync
, bool nofail
)
1908 struct ceph_osd_request
*req
;
1910 int page_align
= off
& ~PAGE_MASK
;
1912 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
1913 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
1915 flags
| CEPH_OSD_FLAG_ONDISK
|
1916 CEPH_OSD_FLAG_WRITE
,
1918 truncate_seq
, truncate_size
, mtime
,
1919 nofail
, 1, page_align
);
1921 return PTR_ERR(req
);
1923 /* it may be a short write due to an object boundary */
1924 req
->r_pages
= pages
;
1925 dout("writepages %llu~%llu (%d pages)\n", off
, len
,
1928 rc
= ceph_osdc_start_request(osdc
, req
, nofail
);
1930 rc
= ceph_osdc_wait_request(osdc
, req
);
1932 ceph_osdc_put_request(req
);
1935 dout("writepages result %d\n", rc
);
1938 EXPORT_SYMBOL(ceph_osdc_writepages
);
1941 * handle incoming message
1943 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1945 struct ceph_osd
*osd
= con
->private;
1946 struct ceph_osd_client
*osdc
;
1947 int type
= le16_to_cpu(msg
->hdr
.type
);
1954 case CEPH_MSG_OSD_MAP
:
1955 ceph_osdc_handle_map(osdc
, msg
);
1957 case CEPH_MSG_OSD_OPREPLY
:
1958 handle_reply(osdc
, msg
, con
);
1960 case CEPH_MSG_WATCH_NOTIFY
:
1961 handle_watch_notify(osdc
, msg
);
1965 pr_err("received unknown message type %d %s\n", type
,
1966 ceph_msg_type_name(type
));
1973 * lookup and return message for incoming reply. set up reply message
1976 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
1977 struct ceph_msg_header
*hdr
,
1980 struct ceph_osd
*osd
= con
->private;
1981 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
1983 struct ceph_osd_request
*req
;
1984 int front
= le32_to_cpu(hdr
->front_len
);
1985 int data_len
= le32_to_cpu(hdr
->data_len
);
1988 tid
= le64_to_cpu(hdr
->tid
);
1989 mutex_lock(&osdc
->request_mutex
);
1990 req
= __lookup_request(osdc
, tid
);
1994 dout("get_reply unknown tid %llu from osd%d\n", tid
,
1999 if (req
->r_con_filling_msg
) {
2000 dout("%s revoking msg %p from old con %p\n", __func__
,
2001 req
->r_reply
, req
->r_con_filling_msg
);
2002 ceph_msg_revoke_incoming(req
->r_reply
);
2003 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
2004 req
->r_con_filling_msg
= NULL
;
2007 if (front
> req
->r_reply
->front
.iov_len
) {
2008 pr_warning("get_reply front %d > preallocated %d\n",
2009 front
, (int)req
->r_reply
->front
.iov_len
);
2010 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
, false);
2013 ceph_msg_put(req
->r_reply
);
2016 m
= ceph_msg_get(req
->r_reply
);
2019 int want
= calc_pages_for(req
->r_page_alignment
, data_len
);
2021 if (unlikely(req
->r_num_pages
< want
)) {
2022 pr_warning("tid %lld reply has %d bytes %d pages, we"
2023 " had only %d pages ready\n", tid
, data_len
,
2024 want
, req
->r_num_pages
);
2030 m
->pages
= req
->r_pages
;
2031 m
->nr_pages
= req
->r_num_pages
;
2032 m
->page_alignment
= req
->r_page_alignment
;
2034 m
->bio
= req
->r_bio
;
2038 req
->r_con_filling_msg
= con
->ops
->get(con
);
2039 dout("get_reply tid %lld %p\n", tid
, m
);
2042 mutex_unlock(&osdc
->request_mutex
);
2047 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2048 struct ceph_msg_header
*hdr
,
2051 struct ceph_osd
*osd
= con
->private;
2052 int type
= le16_to_cpu(hdr
->type
);
2053 int front
= le32_to_cpu(hdr
->front_len
);
2057 case CEPH_MSG_OSD_MAP
:
2058 case CEPH_MSG_WATCH_NOTIFY
:
2059 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2060 case CEPH_MSG_OSD_OPREPLY
:
2061 return get_reply(con
, hdr
, skip
);
2063 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2071 * Wrappers to refcount containing ceph_osd struct
2073 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2075 struct ceph_osd
*osd
= con
->private;
2081 static void put_osd_con(struct ceph_connection
*con
)
2083 struct ceph_osd
*osd
= con
->private;
2091 * Note: returned pointer is the address of a structure that's
2092 * managed separately. Caller must *not* attempt to free it.
2094 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2095 int *proto
, int force_new
)
2097 struct ceph_osd
*o
= con
->private;
2098 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2099 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2100 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2102 if (force_new
&& auth
->authorizer
) {
2103 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
2104 ac
->ops
->destroy_authorizer(ac
, auth
->authorizer
);
2105 auth
->authorizer
= NULL
;
2107 if (!auth
->authorizer
&& ac
->ops
&& ac
->ops
->create_authorizer
) {
2108 int ret
= ac
->ops
->create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2111 return ERR_PTR(ret
);
2113 *proto
= ac
->protocol
;
2119 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2121 struct ceph_osd
*o
= con
->private;
2122 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2123 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2126 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2127 * XXX which do we do: succeed or fail?
2129 return ac
->ops
->verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
2132 static int invalidate_authorizer(struct ceph_connection
*con
)
2134 struct ceph_osd
*o
= con
->private;
2135 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2136 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2138 if (ac
->ops
&& ac
->ops
->invalidate_authorizer
)
2139 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2141 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2144 static const struct ceph_connection_operations osd_con_ops
= {
2147 .dispatch
= dispatch
,
2148 .get_authorizer
= get_authorizer
,
2149 .verify_authorizer_reply
= verify_authorizer_reply
,
2150 .invalidate_authorizer
= invalidate_authorizer
,
2151 .alloc_msg
= alloc_msg
,