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 u32 osize
= le32_to_cpu(layout
->fl_object_size
);
80 op
->extent
.offset
= objoff
;
81 op
->extent
.length
= objlen
;
82 if (op
->extent
.truncate_size
<= off
- objoff
) {
83 op
->extent
.truncate_size
= 0;
85 op
->extent
.truncate_size
-= off
- objoff
;
86 if (op
->extent
.truncate_size
> osize
)
87 op
->extent
.truncate_size
= osize
;
90 req
->r_num_pages
= calc_pages_for(off
, *plen
);
91 req
->r_page_alignment
= off
& ~PAGE_MASK
;
92 if (op
->op
== CEPH_OSD_OP_WRITE
)
93 op
->payload_len
= *plen
;
95 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
96 *bno
, objoff
, objlen
, req
->r_num_pages
);
99 EXPORT_SYMBOL(ceph_calc_raw_layout
);
102 * Implement client access to distributed object storage cluster.
104 * All data objects are stored within a cluster/cloud of OSDs, or
105 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
106 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
107 * remote daemons serving up and coordinating consistent and safe
110 * Cluster membership and the mapping of data objects onto storage devices
111 * are described by the osd map.
113 * We keep track of pending OSD requests (read, write), resubmit
114 * requests to different OSDs when the cluster topology/data layout
115 * change, or retry the affected requests when the communications
116 * channel with an OSD is reset.
120 * calculate the mapping of a file extent onto an object, and fill out the
121 * request accordingly. shorten extent as necessary if it crosses an
124 * fill osd op in request message.
126 static int calc_layout(struct ceph_osd_client
*osdc
,
127 struct ceph_vino vino
,
128 struct ceph_file_layout
*layout
,
130 struct ceph_osd_request
*req
,
131 struct ceph_osd_req_op
*op
)
136 r
= ceph_calc_raw_layout(osdc
, layout
, vino
.snap
, off
,
137 plen
, &bno
, req
, op
);
141 snprintf(req
->r_oid
, sizeof(req
->r_oid
), "%llx.%08llx", vino
.ino
, bno
);
142 req
->r_oid_len
= strlen(req
->r_oid
);
150 void ceph_osdc_release_request(struct kref
*kref
)
152 struct ceph_osd_request
*req
= container_of(kref
,
153 struct ceph_osd_request
,
157 ceph_msg_put(req
->r_request
);
158 if (req
->r_con_filling_msg
) {
159 dout("%s revoking pages %p from con %p\n", __func__
,
160 req
->r_pages
, req
->r_con_filling_msg
);
161 ceph_msg_revoke_incoming(req
->r_reply
);
162 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
165 ceph_msg_put(req
->r_reply
);
166 if (req
->r_own_pages
)
167 ceph_release_page_vector(req
->r_pages
,
173 ceph_put_snap_context(req
->r_snapc
);
175 ceph_pagelist_release(req
->r_trail
);
179 mempool_free(req
, req
->r_osdc
->req_mempool
);
183 EXPORT_SYMBOL(ceph_osdc_release_request
);
185 static int get_num_ops(struct ceph_osd_req_op
*ops
, int *needs_trail
)
192 if (needs_trail
&& op_needs_trail(ops
[i
].op
))
200 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
202 struct ceph_snap_context
*snapc
,
203 struct ceph_osd_req_op
*ops
,
209 struct ceph_osd_request
*req
;
210 struct ceph_msg
*msg
;
212 int num_op
= get_num_ops(ops
, &needs_trail
);
213 size_t msg_size
= sizeof(struct ceph_osd_request_head
);
215 msg_size
+= num_op
*sizeof(struct ceph_osd_op
);
218 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
219 memset(req
, 0, sizeof(*req
));
221 req
= kzalloc(sizeof(*req
), gfp_flags
);
227 req
->r_mempool
= use_mempool
;
229 kref_init(&req
->r_kref
);
230 init_completion(&req
->r_completion
);
231 init_completion(&req
->r_safe_completion
);
232 RB_CLEAR_NODE(&req
->r_node
);
233 INIT_LIST_HEAD(&req
->r_unsafe_item
);
234 INIT_LIST_HEAD(&req
->r_linger_item
);
235 INIT_LIST_HEAD(&req
->r_linger_osd
);
236 INIT_LIST_HEAD(&req
->r_req_lru_item
);
237 INIT_LIST_HEAD(&req
->r_osd_item
);
239 req
->r_flags
= flags
;
241 WARN_ON((flags
& (CEPH_OSD_FLAG_READ
|CEPH_OSD_FLAG_WRITE
)) == 0);
243 /* create reply message */
245 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
247 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
248 OSD_OPREPLY_FRONT_LEN
, gfp_flags
, true);
250 ceph_osdc_put_request(req
);
255 /* allocate space for the trailing data */
257 req
->r_trail
= kmalloc(sizeof(struct ceph_pagelist
), gfp_flags
);
259 ceph_osdc_put_request(req
);
262 ceph_pagelist_init(req
->r_trail
);
265 /* create request message; allow space for oid */
266 msg_size
+= MAX_OBJ_NAME_SIZE
;
268 msg_size
+= sizeof(u64
) * snapc
->num_snaps
;
270 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
272 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp_flags
, true);
274 ceph_osdc_put_request(req
);
278 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
280 req
->r_request
= msg
;
281 req
->r_pages
= pages
;
291 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
293 static void osd_req_encode_op(struct ceph_osd_request
*req
,
294 struct ceph_osd_op
*dst
,
295 struct ceph_osd_req_op
*src
)
297 dst
->op
= cpu_to_le16(src
->op
);
300 case CEPH_OSD_OP_READ
:
301 case CEPH_OSD_OP_WRITE
:
303 cpu_to_le64(src
->extent
.offset
);
305 cpu_to_le64(src
->extent
.length
);
306 dst
->extent
.truncate_size
=
307 cpu_to_le64(src
->extent
.truncate_size
);
308 dst
->extent
.truncate_seq
=
309 cpu_to_le32(src
->extent
.truncate_seq
);
312 case CEPH_OSD_OP_GETXATTR
:
313 case CEPH_OSD_OP_SETXATTR
:
314 case CEPH_OSD_OP_CMPXATTR
:
315 BUG_ON(!req
->r_trail
);
317 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
318 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
319 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
320 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
321 ceph_pagelist_append(req
->r_trail
, src
->xattr
.name
,
322 src
->xattr
.name_len
);
323 ceph_pagelist_append(req
->r_trail
, src
->xattr
.val
,
324 src
->xattr
.value_len
);
326 case CEPH_OSD_OP_CALL
:
327 BUG_ON(!req
->r_trail
);
329 dst
->cls
.class_len
= src
->cls
.class_len
;
330 dst
->cls
.method_len
= src
->cls
.method_len
;
331 dst
->cls
.indata_len
= cpu_to_le32(src
->cls
.indata_len
);
333 ceph_pagelist_append(req
->r_trail
, src
->cls
.class_name
,
335 ceph_pagelist_append(req
->r_trail
, src
->cls
.method_name
,
336 src
->cls
.method_len
);
337 ceph_pagelist_append(req
->r_trail
, src
->cls
.indata
,
338 src
->cls
.indata_len
);
340 case CEPH_OSD_OP_ROLLBACK
:
341 dst
->snap
.snapid
= cpu_to_le64(src
->snap
.snapid
);
343 case CEPH_OSD_OP_STARTSYNC
:
345 case CEPH_OSD_OP_NOTIFY
:
347 __le32 prot_ver
= cpu_to_le32(src
->watch
.prot_ver
);
348 __le32 timeout
= cpu_to_le32(src
->watch
.timeout
);
350 BUG_ON(!req
->r_trail
);
352 ceph_pagelist_append(req
->r_trail
,
353 &prot_ver
, sizeof(prot_ver
));
354 ceph_pagelist_append(req
->r_trail
,
355 &timeout
, sizeof(timeout
));
357 case CEPH_OSD_OP_NOTIFY_ACK
:
358 case CEPH_OSD_OP_WATCH
:
359 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
360 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
361 dst
->watch
.flag
= src
->watch
.flag
;
364 pr_err("unrecognized osd opcode %d\n", dst
->op
);
368 dst
->payload_len
= cpu_to_le32(src
->payload_len
);
372 * build new request AND message
375 void ceph_osdc_build_request(struct ceph_osd_request
*req
,
377 struct ceph_osd_req_op
*src_ops
,
378 struct ceph_snap_context
*snapc
,
379 struct timespec
*mtime
)
381 struct ceph_msg
*msg
= req
->r_request
;
382 struct ceph_osd_request_head
*head
;
383 struct ceph_osd_req_op
*src_op
;
384 struct ceph_osd_op
*op
;
386 int num_op
= get_num_ops(src_ops
, NULL
);
387 size_t msg_size
= sizeof(*head
) + num_op
*sizeof(*op
);
388 int flags
= req
->r_flags
;
392 head
= msg
->front
.iov_base
;
393 op
= (void *)(head
+ 1);
394 p
= (void *)(op
+ num_op
);
396 req
->r_snapc
= ceph_get_snap_context(snapc
);
398 head
->client_inc
= cpu_to_le32(1); /* always, for now. */
399 head
->flags
= cpu_to_le32(flags
);
400 if (flags
& CEPH_OSD_FLAG_WRITE
)
401 ceph_encode_timespec(&head
->mtime
, mtime
);
402 head
->num_ops
= cpu_to_le16(num_op
);
406 head
->object_len
= cpu_to_le32(req
->r_oid_len
);
407 memcpy(p
, req
->r_oid
, req
->r_oid_len
);
412 osd_req_encode_op(req
, op
, src_op
);
418 data_len
+= req
->r_trail
->length
;
421 head
->snap_seq
= cpu_to_le64(snapc
->seq
);
422 head
->num_snaps
= cpu_to_le32(snapc
->num_snaps
);
423 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
424 put_unaligned_le64(snapc
->snaps
[i
], p
);
429 if (flags
& CEPH_OSD_FLAG_WRITE
) {
430 req
->r_request
->hdr
.data_off
= cpu_to_le16(off
);
431 req
->r_request
->hdr
.data_len
= cpu_to_le32(*plen
+ data_len
);
432 } else if (data_len
) {
433 req
->r_request
->hdr
.data_off
= 0;
434 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
437 req
->r_request
->page_alignment
= req
->r_page_alignment
;
439 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
440 msg_size
= p
- msg
->front
.iov_base
;
441 msg
->front
.iov_len
= msg_size
;
442 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
445 EXPORT_SYMBOL(ceph_osdc_build_request
);
448 * build new request AND message, calculate layout, and adjust file
451 * if the file was recently truncated, we include information about its
452 * old and new size so that the object can be updated appropriately. (we
453 * avoid synchronously deleting truncated objects because it's slow.)
455 * if @do_sync, include a 'startsync' command so that the osd will flush
458 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
459 struct ceph_file_layout
*layout
,
460 struct ceph_vino vino
,
462 int opcode
, int flags
,
463 struct ceph_snap_context
*snapc
,
467 struct timespec
*mtime
,
468 bool use_mempool
, int num_reply
,
471 struct ceph_osd_req_op ops
[3];
472 struct ceph_osd_request
*req
;
476 ops
[0].extent
.truncate_seq
= truncate_seq
;
477 ops
[0].extent
.truncate_size
= truncate_size
;
478 ops
[0].payload_len
= 0;
481 ops
[1].op
= CEPH_OSD_OP_STARTSYNC
;
482 ops
[1].payload_len
= 0;
487 req
= ceph_osdc_alloc_request(osdc
, flags
,
490 GFP_NOFS
, NULL
, NULL
);
492 return ERR_PTR(-ENOMEM
);
494 /* calculate max write size */
495 r
= calc_layout(osdc
, vino
, layout
, off
, plen
, req
, ops
);
498 req
->r_file_layout
= *layout
; /* keep a copy */
500 /* in case it differs from natural (file) alignment that
501 calc_layout filled in for us */
502 req
->r_num_pages
= calc_pages_for(page_align
, *plen
);
503 req
->r_page_alignment
= page_align
;
505 ceph_osdc_build_request(req
, off
, plen
, ops
,
511 EXPORT_SYMBOL(ceph_osdc_new_request
);
514 * We keep osd requests in an rbtree, sorted by ->r_tid.
516 static void __insert_request(struct ceph_osd_client
*osdc
,
517 struct ceph_osd_request
*new)
519 struct rb_node
**p
= &osdc
->requests
.rb_node
;
520 struct rb_node
*parent
= NULL
;
521 struct ceph_osd_request
*req
= NULL
;
525 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
526 if (new->r_tid
< req
->r_tid
)
528 else if (new->r_tid
> req
->r_tid
)
534 rb_link_node(&new->r_node
, parent
, p
);
535 rb_insert_color(&new->r_node
, &osdc
->requests
);
538 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
541 struct ceph_osd_request
*req
;
542 struct rb_node
*n
= osdc
->requests
.rb_node
;
545 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
546 if (tid
< req
->r_tid
)
548 else if (tid
> req
->r_tid
)
556 static struct ceph_osd_request
*
557 __lookup_request_ge(struct ceph_osd_client
*osdc
,
560 struct ceph_osd_request
*req
;
561 struct rb_node
*n
= osdc
->requests
.rb_node
;
564 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
565 if (tid
< req
->r_tid
) {
569 } else if (tid
> req
->r_tid
) {
579 * Resubmit requests pending on the given osd.
581 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
582 struct ceph_osd
*osd
)
584 struct ceph_osd_request
*req
, *nreq
;
587 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
588 err
= __reset_osd(osdc
, osd
);
592 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
593 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
594 dout("requeued %p tid %llu osd%d\n", req
, req
->r_tid
,
597 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
600 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
603 * reregister request prior to unregistering linger so
604 * that r_osd is preserved.
606 BUG_ON(!list_empty(&req
->r_req_lru_item
));
607 __register_request(osdc
, req
);
608 list_add(&req
->r_req_lru_item
, &osdc
->req_unsent
);
609 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
610 __unregister_linger_request(osdc
, req
);
611 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
617 * If the osd connection drops, we need to resubmit all requests.
619 static void osd_reset(struct ceph_connection
*con
)
621 struct ceph_osd
*osd
= con
->private;
622 struct ceph_osd_client
*osdc
;
626 dout("osd_reset osd%d\n", osd
->o_osd
);
628 down_read(&osdc
->map_sem
);
629 mutex_lock(&osdc
->request_mutex
);
630 __kick_osd_requests(osdc
, osd
);
631 mutex_unlock(&osdc
->request_mutex
);
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 RB_CLEAR_NODE(&osd
->o_node
);
651 INIT_LIST_HEAD(&osd
->o_requests
);
652 INIT_LIST_HEAD(&osd
->o_linger_requests
);
653 INIT_LIST_HEAD(&osd
->o_osd_lru
);
654 osd
->o_incarnation
= 1;
656 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
658 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
662 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
664 if (atomic_inc_not_zero(&osd
->o_ref
)) {
665 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
666 atomic_read(&osd
->o_ref
));
669 dout("get_osd %p FAIL\n", osd
);
674 static void put_osd(struct ceph_osd
*osd
)
676 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
677 atomic_read(&osd
->o_ref
) - 1);
678 if (atomic_dec_and_test(&osd
->o_ref
) && osd
->o_auth
.authorizer
) {
679 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
681 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
682 ac
->ops
->destroy_authorizer(ac
, osd
->o_auth
.authorizer
);
688 * remove an osd from our map
690 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
692 dout("__remove_osd %p\n", osd
);
693 BUG_ON(!list_empty(&osd
->o_requests
));
694 rb_erase(&osd
->o_node
, &osdc
->osds
);
695 list_del_init(&osd
->o_osd_lru
);
696 ceph_con_close(&osd
->o_con
);
700 static void remove_all_osds(struct ceph_osd_client
*osdc
)
702 dout("%s %p\n", __func__
, osdc
);
703 mutex_lock(&osdc
->request_mutex
);
704 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
705 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
706 struct ceph_osd
, o_node
);
707 __remove_osd(osdc
, osd
);
709 mutex_unlock(&osdc
->request_mutex
);
712 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
713 struct ceph_osd
*osd
)
715 dout("__move_osd_to_lru %p\n", osd
);
716 BUG_ON(!list_empty(&osd
->o_osd_lru
));
717 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
718 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
721 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
723 dout("__remove_osd_from_lru %p\n", osd
);
724 if (!list_empty(&osd
->o_osd_lru
))
725 list_del_init(&osd
->o_osd_lru
);
728 static void remove_old_osds(struct ceph_osd_client
*osdc
)
730 struct ceph_osd
*osd
, *nosd
;
732 dout("__remove_old_osds %p\n", osdc
);
733 mutex_lock(&osdc
->request_mutex
);
734 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
735 if (time_before(jiffies
, osd
->lru_ttl
))
737 __remove_osd(osdc
, osd
);
739 mutex_unlock(&osdc
->request_mutex
);
745 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
747 struct ceph_entity_addr
*peer_addr
;
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
);
757 peer_addr
= &osdc
->osdmap
->osd_addr
[osd
->o_osd
];
758 if (!memcmp(peer_addr
, &osd
->o_con
.peer_addr
, sizeof (*peer_addr
)) &&
759 !ceph_con_opened(&osd
->o_con
)) {
760 struct ceph_osd_request
*req
;
762 dout(" osd addr hasn't changed and connection never opened,"
763 " letting msgr retry");
764 /* touch each r_stamp for handle_timeout()'s benfit */
765 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
766 req
->r_stamp
= jiffies
;
771 ceph_con_close(&osd
->o_con
);
772 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
, peer_addr
);
773 osd
->o_incarnation
++;
778 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
780 struct rb_node
**p
= &osdc
->osds
.rb_node
;
781 struct rb_node
*parent
= NULL
;
782 struct ceph_osd
*osd
= NULL
;
784 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
787 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
788 if (new->o_osd
< osd
->o_osd
)
790 else if (new->o_osd
> osd
->o_osd
)
796 rb_link_node(&new->o_node
, parent
, p
);
797 rb_insert_color(&new->o_node
, &osdc
->osds
);
800 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
802 struct ceph_osd
*osd
;
803 struct rb_node
*n
= osdc
->osds
.rb_node
;
806 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
809 else if (o
> osd
->o_osd
)
817 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
819 schedule_delayed_work(&osdc
->timeout_work
,
820 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
823 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
825 cancel_delayed_work(&osdc
->timeout_work
);
829 * Register request, assign tid. If this is the first request, set up
832 static void __register_request(struct ceph_osd_client
*osdc
,
833 struct ceph_osd_request
*req
)
835 req
->r_tid
= ++osdc
->last_tid
;
836 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
837 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
838 __insert_request(osdc
, req
);
839 ceph_osdc_get_request(req
);
840 osdc
->num_requests
++;
841 if (osdc
->num_requests
== 1) {
842 dout(" first request, scheduling timeout\n");
843 __schedule_osd_timeout(osdc
);
847 static void register_request(struct ceph_osd_client
*osdc
,
848 struct ceph_osd_request
*req
)
850 mutex_lock(&osdc
->request_mutex
);
851 __register_request(osdc
, req
);
852 mutex_unlock(&osdc
->request_mutex
);
856 * called under osdc->request_mutex
858 static void __unregister_request(struct ceph_osd_client
*osdc
,
859 struct ceph_osd_request
*req
)
861 if (RB_EMPTY_NODE(&req
->r_node
)) {
862 dout("__unregister_request %p tid %lld not registered\n",
867 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
868 rb_erase(&req
->r_node
, &osdc
->requests
);
869 osdc
->num_requests
--;
872 /* make sure the original request isn't in flight. */
873 ceph_msg_revoke(req
->r_request
);
875 list_del_init(&req
->r_osd_item
);
876 if (list_empty(&req
->r_osd
->o_requests
) &&
877 list_empty(&req
->r_osd
->o_linger_requests
)) {
878 dout("moving osd to %p lru\n", req
->r_osd
);
879 __move_osd_to_lru(osdc
, req
->r_osd
);
881 if (list_empty(&req
->r_linger_item
))
885 list_del_init(&req
->r_req_lru_item
);
886 ceph_osdc_put_request(req
);
888 if (osdc
->num_requests
== 0) {
889 dout(" no requests, canceling timeout\n");
890 __cancel_osd_timeout(osdc
);
895 * Cancel a previously queued request message
897 static void __cancel_request(struct ceph_osd_request
*req
)
899 if (req
->r_sent
&& req
->r_osd
) {
900 ceph_msg_revoke(req
->r_request
);
905 static void __register_linger_request(struct ceph_osd_client
*osdc
,
906 struct ceph_osd_request
*req
)
908 dout("__register_linger_request %p\n", req
);
909 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
911 list_add_tail(&req
->r_linger_osd
,
912 &req
->r_osd
->o_linger_requests
);
915 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
916 struct ceph_osd_request
*req
)
918 dout("__unregister_linger_request %p\n", req
);
919 list_del_init(&req
->r_linger_item
);
921 list_del_init(&req
->r_linger_osd
);
923 if (list_empty(&req
->r_osd
->o_requests
) &&
924 list_empty(&req
->r_osd
->o_linger_requests
)) {
925 dout("moving osd to %p lru\n", req
->r_osd
);
926 __move_osd_to_lru(osdc
, req
->r_osd
);
928 if (list_empty(&req
->r_osd_item
))
933 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
934 struct ceph_osd_request
*req
)
936 mutex_lock(&osdc
->request_mutex
);
938 __unregister_linger_request(osdc
, req
);
939 ceph_osdc_put_request(req
);
941 mutex_unlock(&osdc
->request_mutex
);
943 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
945 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
946 struct ceph_osd_request
*req
)
948 if (!req
->r_linger
) {
949 dout("set_request_linger %p\n", req
);
952 * caller is now responsible for calling
953 * unregister_linger_request
955 ceph_osdc_get_request(req
);
958 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
961 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
962 * (as needed), and set the request r_osd appropriately. If there is
963 * no up osd, set r_osd to NULL. Move the request to the appropriate list
964 * (unsent, homeless) or leave on in-flight lru.
966 * Return 0 if unchanged, 1 if changed, or negative on error.
968 * Caller should hold map_sem for read and request_mutex.
970 static int __map_request(struct ceph_osd_client
*osdc
,
971 struct ceph_osd_request
*req
, int force_resend
)
973 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
975 int acting
[CEPH_PG_MAX_SIZE
];
979 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
980 err
= ceph_calc_object_layout(&reqhead
->layout
, req
->r_oid
,
981 &req
->r_file_layout
, osdc
->osdmap
);
983 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
986 pgid
= reqhead
->layout
.ol_pgid
;
989 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
995 if ((!force_resend
&&
996 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
997 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
998 req
->r_num_pg_osds
== num
&&
999 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
1000 (req
->r_osd
== NULL
&& o
== -1))
1001 return 0; /* no change */
1003 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
1004 req
->r_tid
, le32_to_cpu(pgid
.pool
), le16_to_cpu(pgid
.ps
), o
,
1005 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1007 /* record full pg acting set */
1008 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
1009 req
->r_num_pg_osds
= num
;
1012 __cancel_request(req
);
1013 list_del_init(&req
->r_osd_item
);
1017 req
->r_osd
= __lookup_osd(osdc
, o
);
1018 if (!req
->r_osd
&& o
>= 0) {
1020 req
->r_osd
= create_osd(osdc
, o
);
1022 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1026 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
1027 __insert_osd(osdc
, req
->r_osd
);
1029 ceph_con_open(&req
->r_osd
->o_con
,
1030 CEPH_ENTITY_TYPE_OSD
, o
,
1031 &osdc
->osdmap
->osd_addr
[o
]);
1035 __remove_osd_from_lru(req
->r_osd
);
1036 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1037 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1039 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1041 err
= 1; /* osd or pg changed */
1048 * caller should hold map_sem (for read) and request_mutex
1050 static void __send_request(struct ceph_osd_client
*osdc
,
1051 struct ceph_osd_request
*req
)
1053 struct ceph_osd_request_head
*reqhead
;
1055 dout("send_request %p tid %llu to osd%d flags %d\n",
1056 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
);
1058 reqhead
= req
->r_request
->front
.iov_base
;
1059 reqhead
->osdmap_epoch
= cpu_to_le32(osdc
->osdmap
->epoch
);
1060 reqhead
->flags
|= cpu_to_le32(req
->r_flags
); /* e.g., RETRY */
1061 reqhead
->reassert_version
= req
->r_reassert_version
;
1063 req
->r_stamp
= jiffies
;
1064 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1066 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1067 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1068 req
->r_sent
= req
->r_osd
->o_incarnation
;
1072 * Send any requests in the queue (req_unsent).
1074 static void send_queued(struct ceph_osd_client
*osdc
)
1076 struct ceph_osd_request
*req
, *tmp
;
1078 dout("send_queued\n");
1079 mutex_lock(&osdc
->request_mutex
);
1080 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
) {
1081 __send_request(osdc
, req
);
1083 mutex_unlock(&osdc
->request_mutex
);
1087 * Timeout callback, called every N seconds when 1 or more osd
1088 * requests has been active for more than N seconds. When this
1089 * happens, we ping all OSDs with requests who have timed out to
1090 * ensure any communications channel reset is detected. Reset the
1091 * request timeouts another N seconds in the future as we go.
1092 * Reschedule the timeout event another N seconds in future (unless
1093 * there are no open requests).
1095 static void handle_timeout(struct work_struct
*work
)
1097 struct ceph_osd_client
*osdc
=
1098 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1099 struct ceph_osd_request
*req
;
1100 struct ceph_osd
*osd
;
1101 unsigned long keepalive
=
1102 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1103 struct list_head slow_osds
;
1105 down_read(&osdc
->map_sem
);
1107 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1109 mutex_lock(&osdc
->request_mutex
);
1112 * ping osds that are a bit slow. this ensures that if there
1113 * is a break in the TCP connection we will notice, and reopen
1114 * a connection with that osd (from the fault callback).
1116 INIT_LIST_HEAD(&slow_osds
);
1117 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1118 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1123 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1124 req
->r_tid
, osd
->o_osd
);
1125 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1127 while (!list_empty(&slow_osds
)) {
1128 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1130 list_del_init(&osd
->o_keepalive_item
);
1131 ceph_con_keepalive(&osd
->o_con
);
1134 __schedule_osd_timeout(osdc
);
1135 mutex_unlock(&osdc
->request_mutex
);
1137 up_read(&osdc
->map_sem
);
1140 static void handle_osds_timeout(struct work_struct
*work
)
1142 struct ceph_osd_client
*osdc
=
1143 container_of(work
, struct ceph_osd_client
,
1144 osds_timeout_work
.work
);
1145 unsigned long delay
=
1146 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1148 dout("osds timeout\n");
1149 down_read(&osdc
->map_sem
);
1150 remove_old_osds(osdc
);
1151 up_read(&osdc
->map_sem
);
1153 schedule_delayed_work(&osdc
->osds_timeout_work
,
1154 round_jiffies_relative(delay
));
1157 static void complete_request(struct ceph_osd_request
*req
)
1159 if (req
->r_safe_callback
)
1160 req
->r_safe_callback(req
, NULL
);
1161 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1165 * handle osd op reply. either call the callback if it is specified,
1166 * or do the completion to wake up the waiting thread.
1168 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1169 struct ceph_connection
*con
)
1171 struct ceph_osd_reply_head
*rhead
= msg
->front
.iov_base
;
1172 struct ceph_osd_request
*req
;
1174 int numops
, object_len
, flags
;
1177 tid
= le64_to_cpu(msg
->hdr
.tid
);
1178 if (msg
->front
.iov_len
< sizeof(*rhead
))
1180 numops
= le32_to_cpu(rhead
->num_ops
);
1181 object_len
= le32_to_cpu(rhead
->object_len
);
1182 result
= le32_to_cpu(rhead
->result
);
1183 if (msg
->front
.iov_len
!= sizeof(*rhead
) + object_len
+
1184 numops
* sizeof(struct ceph_osd_op
))
1186 dout("handle_reply %p tid %llu result %d\n", msg
, tid
, (int)result
);
1188 mutex_lock(&osdc
->request_mutex
);
1189 req
= __lookup_request(osdc
, tid
);
1191 dout("handle_reply tid %llu dne\n", tid
);
1192 mutex_unlock(&osdc
->request_mutex
);
1195 ceph_osdc_get_request(req
);
1196 flags
= le32_to_cpu(rhead
->flags
);
1199 * if this connection filled our message, drop our reference now, to
1200 * avoid a (safe but slower) revoke later.
1202 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
1203 dout(" dropping con_filling_msg ref %p\n", con
);
1204 req
->r_con_filling_msg
= NULL
;
1208 if (!req
->r_got_reply
) {
1211 req
->r_result
= le32_to_cpu(rhead
->result
);
1212 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1213 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1215 if (req
->r_result
== 0)
1216 req
->r_result
= bytes
;
1218 /* in case this is a write and we need to replay, */
1219 req
->r_reassert_version
= rhead
->reassert_version
;
1221 req
->r_got_reply
= 1;
1222 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1223 dout("handle_reply tid %llu dup ack\n", tid
);
1224 mutex_unlock(&osdc
->request_mutex
);
1228 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1230 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1231 __register_linger_request(osdc
, req
);
1233 /* either this is a read, or we got the safe response */
1235 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1236 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1237 __unregister_request(osdc
, req
);
1239 mutex_unlock(&osdc
->request_mutex
);
1241 if (req
->r_callback
)
1242 req
->r_callback(req
, msg
);
1244 complete_all(&req
->r_completion
);
1246 if (flags
& CEPH_OSD_FLAG_ONDISK
)
1247 complete_request(req
);
1250 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1251 ceph_osdc_put_request(req
);
1255 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1256 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
),
1257 (int)sizeof(*rhead
));
1261 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1263 struct rb_node
*p
, *n
;
1265 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1266 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1269 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1270 memcmp(&osd
->o_con
.peer_addr
,
1271 ceph_osd_addr(osdc
->osdmap
,
1273 sizeof(struct ceph_entity_addr
)) != 0)
1274 __reset_osd(osdc
, osd
);
1279 * Requeue requests whose mapping to an OSD has changed. If requests map to
1280 * no osd, request a new map.
1282 * Caller should hold map_sem for read.
1284 static void kick_requests(struct ceph_osd_client
*osdc
, int force_resend
)
1286 struct ceph_osd_request
*req
, *nreq
;
1291 dout("kick_requests %s\n", force_resend
? " (force resend)" : "");
1292 mutex_lock(&osdc
->request_mutex
);
1293 for (p
= rb_first(&osdc
->requests
); p
; ) {
1294 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1298 * For linger requests that have not yet been
1299 * registered, move them to the linger list; they'll
1300 * be sent to the osd in the loop below. Unregister
1301 * the request before re-registering it as a linger
1302 * request to ensure the __map_request() below
1303 * will decide it needs to be sent.
1305 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
1306 dout("%p tid %llu restart on osd%d\n",
1308 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1309 __unregister_request(osdc
, req
);
1310 __register_linger_request(osdc
, req
);
1314 err
= __map_request(osdc
, req
, force_resend
);
1316 continue; /* error */
1317 if (req
->r_osd
== NULL
) {
1318 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1319 needmap
++; /* request a newer map */
1320 } else if (err
> 0) {
1321 if (!req
->r_linger
) {
1322 dout("%p tid %llu requeued on osd%d\n", req
,
1324 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1325 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1330 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1332 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1334 err
= __map_request(osdc
, req
, force_resend
);
1335 dout("__map_request returned %d\n", err
);
1337 continue; /* no change and no osd was specified */
1339 continue; /* hrm! */
1340 if (req
->r_osd
== NULL
) {
1341 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1342 needmap
++; /* request a newer map */
1346 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1347 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1348 __register_request(osdc
, req
);
1349 __unregister_linger_request(osdc
, req
);
1351 mutex_unlock(&osdc
->request_mutex
);
1354 dout("%d requests for down osds, need new map\n", needmap
);
1355 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1357 reset_changed_osds(osdc
);
1362 * Process updated osd map.
1364 * The message contains any number of incremental and full maps, normally
1365 * indicating some sort of topology change in the cluster. Kick requests
1366 * off to different OSDs as needed.
1368 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1370 void *p
, *end
, *next
;
1371 u32 nr_maps
, maplen
;
1373 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1375 struct ceph_fsid fsid
;
1377 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1378 p
= msg
->front
.iov_base
;
1379 end
= p
+ msg
->front
.iov_len
;
1382 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1383 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1384 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1387 down_write(&osdc
->map_sem
);
1389 /* incremental maps */
1390 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1391 dout(" %d inc maps\n", nr_maps
);
1392 while (nr_maps
> 0) {
1393 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1394 epoch
= ceph_decode_32(&p
);
1395 maplen
= ceph_decode_32(&p
);
1396 ceph_decode_need(&p
, end
, maplen
, bad
);
1398 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1399 dout("applying incremental map %u len %d\n",
1401 newmap
= osdmap_apply_incremental(&p
, next
,
1403 &osdc
->client
->msgr
);
1404 if (IS_ERR(newmap
)) {
1405 err
= PTR_ERR(newmap
);
1409 if (newmap
!= osdc
->osdmap
) {
1410 ceph_osdmap_destroy(osdc
->osdmap
);
1411 osdc
->osdmap
= newmap
;
1413 kick_requests(osdc
, 0);
1415 dout("ignoring incremental map %u len %d\n",
1425 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1426 dout(" %d full maps\n", nr_maps
);
1428 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1429 epoch
= ceph_decode_32(&p
);
1430 maplen
= ceph_decode_32(&p
);
1431 ceph_decode_need(&p
, end
, maplen
, bad
);
1433 dout("skipping non-latest full map %u len %d\n",
1435 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1436 dout("skipping full map %u len %d, "
1437 "older than our %u\n", epoch
, maplen
,
1438 osdc
->osdmap
->epoch
);
1440 int skipped_map
= 0;
1442 dout("taking full map %u len %d\n", epoch
, maplen
);
1443 newmap
= osdmap_decode(&p
, p
+maplen
);
1444 if (IS_ERR(newmap
)) {
1445 err
= PTR_ERR(newmap
);
1449 oldmap
= osdc
->osdmap
;
1450 osdc
->osdmap
= newmap
;
1452 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
1454 ceph_osdmap_destroy(oldmap
);
1456 kick_requests(osdc
, skipped_map
);
1463 downgrade_write(&osdc
->map_sem
);
1464 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1467 * subscribe to subsequent osdmap updates if full to ensure
1468 * we find out when we are no longer full and stop returning
1471 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
))
1472 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1475 up_read(&osdc
->map_sem
);
1476 wake_up_all(&osdc
->client
->auth_wq
);
1480 pr_err("osdc handle_map corrupt msg\n");
1482 up_write(&osdc
->map_sem
);
1487 * watch/notify callback event infrastructure
1489 * These callbacks are used both for watch and notify operations.
1491 static void __release_event(struct kref
*kref
)
1493 struct ceph_osd_event
*event
=
1494 container_of(kref
, struct ceph_osd_event
, kref
);
1496 dout("__release_event %p\n", event
);
1500 static void get_event(struct ceph_osd_event
*event
)
1502 kref_get(&event
->kref
);
1505 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1507 kref_put(&event
->kref
, __release_event
);
1509 EXPORT_SYMBOL(ceph_osdc_put_event
);
1511 static void __insert_event(struct ceph_osd_client
*osdc
,
1512 struct ceph_osd_event
*new)
1514 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1515 struct rb_node
*parent
= NULL
;
1516 struct ceph_osd_event
*event
= NULL
;
1520 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1521 if (new->cookie
< event
->cookie
)
1523 else if (new->cookie
> event
->cookie
)
1524 p
= &(*p
)->rb_right
;
1529 rb_link_node(&new->node
, parent
, p
);
1530 rb_insert_color(&new->node
, &osdc
->event_tree
);
1533 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1536 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1537 struct rb_node
*parent
= NULL
;
1538 struct ceph_osd_event
*event
= NULL
;
1542 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1543 if (cookie
< event
->cookie
)
1545 else if (cookie
> event
->cookie
)
1546 p
= &(*p
)->rb_right
;
1553 static void __remove_event(struct ceph_osd_event
*event
)
1555 struct ceph_osd_client
*osdc
= event
->osdc
;
1557 if (!RB_EMPTY_NODE(&event
->node
)) {
1558 dout("__remove_event removed %p\n", event
);
1559 rb_erase(&event
->node
, &osdc
->event_tree
);
1560 ceph_osdc_put_event(event
);
1562 dout("__remove_event didn't remove %p\n", event
);
1566 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1567 void (*event_cb
)(u64
, u64
, u8
, void *),
1568 int one_shot
, void *data
,
1569 struct ceph_osd_event
**pevent
)
1571 struct ceph_osd_event
*event
;
1573 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1577 dout("create_event %p\n", event
);
1578 event
->cb
= event_cb
;
1579 event
->one_shot
= one_shot
;
1582 INIT_LIST_HEAD(&event
->osd_node
);
1583 RB_CLEAR_NODE(&event
->node
);
1584 kref_init(&event
->kref
); /* one ref for us */
1585 kref_get(&event
->kref
); /* one ref for the caller */
1586 init_completion(&event
->completion
);
1588 spin_lock(&osdc
->event_lock
);
1589 event
->cookie
= ++osdc
->event_count
;
1590 __insert_event(osdc
, event
);
1591 spin_unlock(&osdc
->event_lock
);
1596 EXPORT_SYMBOL(ceph_osdc_create_event
);
1598 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1600 struct ceph_osd_client
*osdc
= event
->osdc
;
1602 dout("cancel_event %p\n", event
);
1603 spin_lock(&osdc
->event_lock
);
1604 __remove_event(event
);
1605 spin_unlock(&osdc
->event_lock
);
1606 ceph_osdc_put_event(event
); /* caller's */
1608 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1611 static void do_event_work(struct work_struct
*work
)
1613 struct ceph_osd_event_work
*event_work
=
1614 container_of(work
, struct ceph_osd_event_work
, work
);
1615 struct ceph_osd_event
*event
= event_work
->event
;
1616 u64 ver
= event_work
->ver
;
1617 u64 notify_id
= event_work
->notify_id
;
1618 u8 opcode
= event_work
->opcode
;
1620 dout("do_event_work completing %p\n", event
);
1621 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1622 complete(&event
->completion
);
1623 dout("do_event_work completed %p\n", event
);
1624 ceph_osdc_put_event(event
);
1630 * Process osd watch notifications
1632 void handle_watch_notify(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1636 u64 cookie
, ver
, notify_id
;
1638 struct ceph_osd_event
*event
;
1639 struct ceph_osd_event_work
*event_work
;
1641 p
= msg
->front
.iov_base
;
1642 end
= p
+ msg
->front
.iov_len
;
1644 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1645 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1646 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1647 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1648 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1650 spin_lock(&osdc
->event_lock
);
1651 event
= __find_event(osdc
, cookie
);
1654 if (event
->one_shot
)
1655 __remove_event(event
);
1657 spin_unlock(&osdc
->event_lock
);
1658 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1659 cookie
, ver
, event
);
1661 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1663 dout("ERROR: could not allocate event_work\n");
1666 INIT_WORK(&event_work
->work
, do_event_work
);
1667 event_work
->event
= event
;
1668 event_work
->ver
= ver
;
1669 event_work
->notify_id
= notify_id
;
1670 event_work
->opcode
= opcode
;
1671 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1672 dout("WARNING: failed to queue notify event work\n");
1680 complete(&event
->completion
);
1681 ceph_osdc_put_event(event
);
1685 pr_err("osdc handle_watch_notify corrupt msg\n");
1689 int ceph_osdc_wait_event(struct ceph_osd_event
*event
, unsigned long timeout
)
1693 dout("wait_event %p\n", event
);
1694 err
= wait_for_completion_interruptible_timeout(&event
->completion
,
1696 ceph_osdc_put_event(event
);
1699 dout("wait_event %p returns %d\n", event
, err
);
1702 EXPORT_SYMBOL(ceph_osdc_wait_event
);
1705 * Register request, send initial attempt.
1707 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1708 struct ceph_osd_request
*req
,
1713 req
->r_request
->pages
= req
->r_pages
;
1714 req
->r_request
->nr_pages
= req
->r_num_pages
;
1716 req
->r_request
->bio
= req
->r_bio
;
1718 req
->r_request
->trail
= req
->r_trail
;
1720 register_request(osdc
, req
);
1722 down_read(&osdc
->map_sem
);
1723 mutex_lock(&osdc
->request_mutex
);
1725 * a racing kick_requests() may have sent the message for us
1726 * while we dropped request_mutex above, so only send now if
1727 * the request still han't been touched yet.
1729 if (req
->r_sent
== 0) {
1730 rc
= __map_request(osdc
, req
, 0);
1733 dout("osdc_start_request failed map, "
1734 " will retry %lld\n", req
->r_tid
);
1739 if (req
->r_osd
== NULL
) {
1740 dout("send_request %p no up osds in pg\n", req
);
1741 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1743 __send_request(osdc
, req
);
1749 mutex_unlock(&osdc
->request_mutex
);
1750 up_read(&osdc
->map_sem
);
1753 EXPORT_SYMBOL(ceph_osdc_start_request
);
1756 * wait for a request to complete
1758 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1759 struct ceph_osd_request
*req
)
1763 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1765 mutex_lock(&osdc
->request_mutex
);
1766 __cancel_request(req
);
1767 __unregister_request(osdc
, req
);
1768 mutex_unlock(&osdc
->request_mutex
);
1769 complete_request(req
);
1770 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1774 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1775 return req
->r_result
;
1777 EXPORT_SYMBOL(ceph_osdc_wait_request
);
1780 * sync - wait for all in-flight requests to flush. avoid starvation.
1782 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1784 struct ceph_osd_request
*req
;
1785 u64 last_tid
, next_tid
= 0;
1787 mutex_lock(&osdc
->request_mutex
);
1788 last_tid
= osdc
->last_tid
;
1790 req
= __lookup_request_ge(osdc
, next_tid
);
1793 if (req
->r_tid
> last_tid
)
1796 next_tid
= req
->r_tid
+ 1;
1797 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1800 ceph_osdc_get_request(req
);
1801 mutex_unlock(&osdc
->request_mutex
);
1802 dout("sync waiting on tid %llu (last is %llu)\n",
1803 req
->r_tid
, last_tid
);
1804 wait_for_completion(&req
->r_safe_completion
);
1805 mutex_lock(&osdc
->request_mutex
);
1806 ceph_osdc_put_request(req
);
1808 mutex_unlock(&osdc
->request_mutex
);
1809 dout("sync done (thru tid %llu)\n", last_tid
);
1811 EXPORT_SYMBOL(ceph_osdc_sync
);
1816 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
1821 osdc
->client
= client
;
1822 osdc
->osdmap
= NULL
;
1823 init_rwsem(&osdc
->map_sem
);
1824 init_completion(&osdc
->map_waiters
);
1825 osdc
->last_requested_map
= 0;
1826 mutex_init(&osdc
->request_mutex
);
1828 osdc
->osds
= RB_ROOT
;
1829 INIT_LIST_HEAD(&osdc
->osd_lru
);
1830 osdc
->requests
= RB_ROOT
;
1831 INIT_LIST_HEAD(&osdc
->req_lru
);
1832 INIT_LIST_HEAD(&osdc
->req_unsent
);
1833 INIT_LIST_HEAD(&osdc
->req_notarget
);
1834 INIT_LIST_HEAD(&osdc
->req_linger
);
1835 osdc
->num_requests
= 0;
1836 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
1837 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
1838 spin_lock_init(&osdc
->event_lock
);
1839 osdc
->event_tree
= RB_ROOT
;
1840 osdc
->event_count
= 0;
1842 schedule_delayed_work(&osdc
->osds_timeout_work
,
1843 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
1846 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
1847 sizeof(struct ceph_osd_request
));
1848 if (!osdc
->req_mempool
)
1851 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
1852 OSD_OP_FRONT_LEN
, 10, true,
1856 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
1857 OSD_OPREPLY_FRONT_LEN
, 10, true,
1862 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
1863 if (IS_ERR(osdc
->notify_wq
)) {
1864 err
= PTR_ERR(osdc
->notify_wq
);
1865 osdc
->notify_wq
= NULL
;
1871 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1873 mempool_destroy(osdc
->req_mempool
);
1877 EXPORT_SYMBOL(ceph_osdc_init
);
1879 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
1881 flush_workqueue(osdc
->notify_wq
);
1882 destroy_workqueue(osdc
->notify_wq
);
1883 cancel_delayed_work_sync(&osdc
->timeout_work
);
1884 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
1886 ceph_osdmap_destroy(osdc
->osdmap
);
1887 osdc
->osdmap
= NULL
;
1889 remove_all_osds(osdc
);
1890 mempool_destroy(osdc
->req_mempool
);
1891 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1892 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
1894 EXPORT_SYMBOL(ceph_osdc_stop
);
1897 * Read some contiguous pages. If we cross a stripe boundary, shorten
1898 * *plen. Return number of bytes read, or error.
1900 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
1901 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
1903 u32 truncate_seq
, u64 truncate_size
,
1904 struct page
**pages
, int num_pages
, int page_align
)
1906 struct ceph_osd_request
*req
;
1909 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
1910 vino
.snap
, off
, *plen
);
1911 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
1912 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
1913 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
1914 false, 1, page_align
);
1916 return PTR_ERR(req
);
1918 /* it may be a short read due to an object boundary */
1919 req
->r_pages
= pages
;
1921 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1922 off
, *plen
, req
->r_num_pages
, page_align
);
1924 rc
= ceph_osdc_start_request(osdc
, req
, false);
1926 rc
= ceph_osdc_wait_request(osdc
, req
);
1928 ceph_osdc_put_request(req
);
1929 dout("readpages result %d\n", rc
);
1932 EXPORT_SYMBOL(ceph_osdc_readpages
);
1935 * do a synchronous write on N pages
1937 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
1938 struct ceph_file_layout
*layout
,
1939 struct ceph_snap_context
*snapc
,
1941 u32 truncate_seq
, u64 truncate_size
,
1942 struct timespec
*mtime
,
1943 struct page
**pages
, int num_pages
,
1944 int flags
, int do_sync
, bool nofail
)
1946 struct ceph_osd_request
*req
;
1948 int page_align
= off
& ~PAGE_MASK
;
1950 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
1951 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
1953 flags
| CEPH_OSD_FLAG_ONDISK
|
1954 CEPH_OSD_FLAG_WRITE
,
1956 truncate_seq
, truncate_size
, mtime
,
1957 nofail
, 1, page_align
);
1959 return PTR_ERR(req
);
1961 /* it may be a short write due to an object boundary */
1962 req
->r_pages
= pages
;
1963 dout("writepages %llu~%llu (%d pages)\n", off
, len
,
1966 rc
= ceph_osdc_start_request(osdc
, req
, nofail
);
1968 rc
= ceph_osdc_wait_request(osdc
, req
);
1970 ceph_osdc_put_request(req
);
1973 dout("writepages result %d\n", rc
);
1976 EXPORT_SYMBOL(ceph_osdc_writepages
);
1979 * handle incoming message
1981 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1983 struct ceph_osd
*osd
= con
->private;
1984 struct ceph_osd_client
*osdc
;
1985 int type
= le16_to_cpu(msg
->hdr
.type
);
1992 case CEPH_MSG_OSD_MAP
:
1993 ceph_osdc_handle_map(osdc
, msg
);
1995 case CEPH_MSG_OSD_OPREPLY
:
1996 handle_reply(osdc
, msg
, con
);
1998 case CEPH_MSG_WATCH_NOTIFY
:
1999 handle_watch_notify(osdc
, msg
);
2003 pr_err("received unknown message type %d %s\n", type
,
2004 ceph_msg_type_name(type
));
2011 * lookup and return message for incoming reply. set up reply message
2014 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2015 struct ceph_msg_header
*hdr
,
2018 struct ceph_osd
*osd
= con
->private;
2019 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2021 struct ceph_osd_request
*req
;
2022 int front
= le32_to_cpu(hdr
->front_len
);
2023 int data_len
= le32_to_cpu(hdr
->data_len
);
2026 tid
= le64_to_cpu(hdr
->tid
);
2027 mutex_lock(&osdc
->request_mutex
);
2028 req
= __lookup_request(osdc
, tid
);
2032 dout("get_reply unknown tid %llu from osd%d\n", tid
,
2037 if (req
->r_con_filling_msg
) {
2038 dout("%s revoking msg %p from old con %p\n", __func__
,
2039 req
->r_reply
, req
->r_con_filling_msg
);
2040 ceph_msg_revoke_incoming(req
->r_reply
);
2041 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
2042 req
->r_con_filling_msg
= NULL
;
2045 if (front
> req
->r_reply
->front
.iov_len
) {
2046 pr_warning("get_reply front %d > preallocated %d\n",
2047 front
, (int)req
->r_reply
->front
.iov_len
);
2048 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
, false);
2051 ceph_msg_put(req
->r_reply
);
2054 m
= ceph_msg_get(req
->r_reply
);
2057 int want
= calc_pages_for(req
->r_page_alignment
, data_len
);
2059 if (unlikely(req
->r_num_pages
< want
)) {
2060 pr_warning("tid %lld reply has %d bytes %d pages, we"
2061 " had only %d pages ready\n", tid
, data_len
,
2062 want
, req
->r_num_pages
);
2068 m
->pages
= req
->r_pages
;
2069 m
->nr_pages
= req
->r_num_pages
;
2070 m
->page_alignment
= req
->r_page_alignment
;
2072 m
->bio
= req
->r_bio
;
2076 req
->r_con_filling_msg
= con
->ops
->get(con
);
2077 dout("get_reply tid %lld %p\n", tid
, m
);
2080 mutex_unlock(&osdc
->request_mutex
);
2085 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2086 struct ceph_msg_header
*hdr
,
2089 struct ceph_osd
*osd
= con
->private;
2090 int type
= le16_to_cpu(hdr
->type
);
2091 int front
= le32_to_cpu(hdr
->front_len
);
2095 case CEPH_MSG_OSD_MAP
:
2096 case CEPH_MSG_WATCH_NOTIFY
:
2097 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2098 case CEPH_MSG_OSD_OPREPLY
:
2099 return get_reply(con
, hdr
, skip
);
2101 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2109 * Wrappers to refcount containing ceph_osd struct
2111 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2113 struct ceph_osd
*osd
= con
->private;
2119 static void put_osd_con(struct ceph_connection
*con
)
2121 struct ceph_osd
*osd
= con
->private;
2129 * Note: returned pointer is the address of a structure that's
2130 * managed separately. Caller must *not* attempt to free it.
2132 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2133 int *proto
, int force_new
)
2135 struct ceph_osd
*o
= con
->private;
2136 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2137 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2138 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2140 if (force_new
&& auth
->authorizer
) {
2141 if (ac
->ops
&& ac
->ops
->destroy_authorizer
)
2142 ac
->ops
->destroy_authorizer(ac
, auth
->authorizer
);
2143 auth
->authorizer
= NULL
;
2145 if (!auth
->authorizer
&& ac
->ops
&& ac
->ops
->create_authorizer
) {
2146 int ret
= ac
->ops
->create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2149 return ERR_PTR(ret
);
2151 *proto
= ac
->protocol
;
2157 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2159 struct ceph_osd
*o
= con
->private;
2160 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2161 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2164 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2165 * XXX which do we do: succeed or fail?
2167 return ac
->ops
->verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
2170 static int invalidate_authorizer(struct ceph_connection
*con
)
2172 struct ceph_osd
*o
= con
->private;
2173 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2174 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2176 if (ac
->ops
&& ac
->ops
->invalidate_authorizer
)
2177 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2179 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2182 static const struct ceph_connection_operations osd_con_ops
= {
2185 .dispatch
= dispatch
,
2186 .get_authorizer
= get_authorizer
,
2187 .verify_authorizer_reply
= verify_authorizer_reply
,
2188 .invalidate_authorizer
= invalidate_authorizer
,
2189 .alloc_msg
= alloc_msg
,