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 int __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 void 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 */
66 reqhead
->snapid
= cpu_to_le64(snapid
);
69 ceph_calc_file_object_mapping(layout
, off
, plen
, bno
,
72 dout(" skipping last %llu, final file extent %llu~%llu\n",
73 orig_len
- *plen
, off
, *plen
);
75 if (op_has_extent(op
->op
)) {
76 op
->extent
.offset
= objoff
;
77 op
->extent
.length
= objlen
;
79 req
->r_num_pages
= calc_pages_for(off
, *plen
);
80 req
->r_page_alignment
= off
& ~PAGE_MASK
;
81 if (op
->op
== CEPH_OSD_OP_WRITE
)
82 op
->payload_len
= *plen
;
84 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
85 *bno
, objoff
, objlen
, req
->r_num_pages
);
88 EXPORT_SYMBOL(ceph_calc_raw_layout
);
91 * Implement client access to distributed object storage cluster.
93 * All data objects are stored within a cluster/cloud of OSDs, or
94 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
95 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
96 * remote daemons serving up and coordinating consistent and safe
99 * Cluster membership and the mapping of data objects onto storage devices
100 * are described by the osd map.
102 * We keep track of pending OSD requests (read, write), resubmit
103 * requests to different OSDs when the cluster topology/data layout
104 * change, or retry the affected requests when the communications
105 * channel with an OSD is reset.
109 * calculate the mapping of a file extent onto an object, and fill out the
110 * request accordingly. shorten extent as necessary if it crosses an
113 * fill osd op in request message.
115 static void calc_layout(struct ceph_osd_client
*osdc
,
116 struct ceph_vino vino
,
117 struct ceph_file_layout
*layout
,
119 struct ceph_osd_request
*req
,
120 struct ceph_osd_req_op
*op
)
124 ceph_calc_raw_layout(osdc
, layout
, vino
.snap
, off
,
125 plen
, &bno
, req
, op
);
127 sprintf(req
->r_oid
, "%llx.%08llx", vino
.ino
, bno
);
128 req
->r_oid_len
= strlen(req
->r_oid
);
134 void ceph_osdc_release_request(struct kref
*kref
)
136 struct ceph_osd_request
*req
= container_of(kref
,
137 struct ceph_osd_request
,
141 ceph_msg_put(req
->r_request
);
143 ceph_msg_put(req
->r_reply
);
144 if (req
->r_con_filling_msg
) {
145 dout("release_request revoking pages %p from con %p\n",
146 req
->r_pages
, req
->r_con_filling_msg
);
147 ceph_con_revoke_message(req
->r_con_filling_msg
,
149 ceph_con_put(req
->r_con_filling_msg
);
151 if (req
->r_own_pages
)
152 ceph_release_page_vector(req
->r_pages
,
158 ceph_put_snap_context(req
->r_snapc
);
160 ceph_pagelist_release(req
->r_trail
);
164 mempool_free(req
, req
->r_osdc
->req_mempool
);
168 EXPORT_SYMBOL(ceph_osdc_release_request
);
170 static int get_num_ops(struct ceph_osd_req_op
*ops
, int *needs_trail
)
177 if (needs_trail
&& op_needs_trail(ops
[i
].op
))
185 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
187 struct ceph_snap_context
*snapc
,
188 struct ceph_osd_req_op
*ops
,
194 struct ceph_osd_request
*req
;
195 struct ceph_msg
*msg
;
197 int num_op
= get_num_ops(ops
, &needs_trail
);
198 size_t msg_size
= sizeof(struct ceph_osd_request_head
);
200 msg_size
+= num_op
*sizeof(struct ceph_osd_op
);
203 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
204 memset(req
, 0, sizeof(*req
));
206 req
= kzalloc(sizeof(*req
), gfp_flags
);
212 req
->r_mempool
= use_mempool
;
214 kref_init(&req
->r_kref
);
215 init_completion(&req
->r_completion
);
216 init_completion(&req
->r_safe_completion
);
217 INIT_LIST_HEAD(&req
->r_unsafe_item
);
218 INIT_LIST_HEAD(&req
->r_linger_item
);
219 INIT_LIST_HEAD(&req
->r_linger_osd
);
220 req
->r_flags
= flags
;
222 WARN_ON((flags
& (CEPH_OSD_FLAG_READ
|CEPH_OSD_FLAG_WRITE
)) == 0);
224 /* create reply message */
226 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
228 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
229 OSD_OPREPLY_FRONT_LEN
, gfp_flags
);
231 ceph_osdc_put_request(req
);
236 /* allocate space for the trailing data */
238 req
->r_trail
= kmalloc(sizeof(struct ceph_pagelist
), gfp_flags
);
240 ceph_osdc_put_request(req
);
243 ceph_pagelist_init(req
->r_trail
);
245 /* create request message; allow space for oid */
248 msg_size
+= sizeof(u64
) * snapc
->num_snaps
;
250 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
252 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp_flags
);
254 ceph_osdc_put_request(req
);
258 msg
->hdr
.type
= cpu_to_le16(CEPH_MSG_OSD_OP
);
259 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
261 req
->r_request
= msg
;
262 req
->r_pages
= pages
;
272 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
274 static void osd_req_encode_op(struct ceph_osd_request
*req
,
275 struct ceph_osd_op
*dst
,
276 struct ceph_osd_req_op
*src
)
278 dst
->op
= cpu_to_le16(src
->op
);
281 case CEPH_OSD_OP_READ
:
282 case CEPH_OSD_OP_WRITE
:
284 cpu_to_le64(src
->extent
.offset
);
286 cpu_to_le64(src
->extent
.length
);
287 dst
->extent
.truncate_size
=
288 cpu_to_le64(src
->extent
.truncate_size
);
289 dst
->extent
.truncate_seq
=
290 cpu_to_le32(src
->extent
.truncate_seq
);
293 case CEPH_OSD_OP_GETXATTR
:
294 case CEPH_OSD_OP_SETXATTR
:
295 case CEPH_OSD_OP_CMPXATTR
:
296 BUG_ON(!req
->r_trail
);
298 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
299 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
300 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
301 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
302 ceph_pagelist_append(req
->r_trail
, src
->xattr
.name
,
303 src
->xattr
.name_len
);
304 ceph_pagelist_append(req
->r_trail
, src
->xattr
.val
,
305 src
->xattr
.value_len
);
307 case CEPH_OSD_OP_CALL
:
308 BUG_ON(!req
->r_trail
);
310 dst
->cls
.class_len
= src
->cls
.class_len
;
311 dst
->cls
.method_len
= src
->cls
.method_len
;
312 dst
->cls
.indata_len
= cpu_to_le32(src
->cls
.indata_len
);
314 ceph_pagelist_append(req
->r_trail
, src
->cls
.class_name
,
316 ceph_pagelist_append(req
->r_trail
, src
->cls
.method_name
,
317 src
->cls
.method_len
);
318 ceph_pagelist_append(req
->r_trail
, src
->cls
.indata
,
319 src
->cls
.indata_len
);
321 case CEPH_OSD_OP_ROLLBACK
:
322 dst
->snap
.snapid
= cpu_to_le64(src
->snap
.snapid
);
324 case CEPH_OSD_OP_STARTSYNC
:
326 case CEPH_OSD_OP_NOTIFY
:
328 __le32 prot_ver
= cpu_to_le32(src
->watch
.prot_ver
);
329 __le32 timeout
= cpu_to_le32(src
->watch
.timeout
);
331 BUG_ON(!req
->r_trail
);
333 ceph_pagelist_append(req
->r_trail
,
334 &prot_ver
, sizeof(prot_ver
));
335 ceph_pagelist_append(req
->r_trail
,
336 &timeout
, sizeof(timeout
));
338 case CEPH_OSD_OP_NOTIFY_ACK
:
339 case CEPH_OSD_OP_WATCH
:
340 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
341 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
342 dst
->watch
.flag
= src
->watch
.flag
;
345 pr_err("unrecognized osd opcode %d\n", dst
->op
);
349 dst
->payload_len
= cpu_to_le32(src
->payload_len
);
353 * build new request AND message
356 void ceph_osdc_build_request(struct ceph_osd_request
*req
,
358 struct ceph_osd_req_op
*src_ops
,
359 struct ceph_snap_context
*snapc
,
360 struct timespec
*mtime
,
364 struct ceph_msg
*msg
= req
->r_request
;
365 struct ceph_osd_request_head
*head
;
366 struct ceph_osd_req_op
*src_op
;
367 struct ceph_osd_op
*op
;
369 int num_op
= get_num_ops(src_ops
, NULL
);
370 size_t msg_size
= sizeof(*head
) + num_op
*sizeof(*op
);
371 int flags
= req
->r_flags
;
375 head
= msg
->front
.iov_base
;
376 op
= (void *)(head
+ 1);
377 p
= (void *)(op
+ num_op
);
379 req
->r_snapc
= ceph_get_snap_context(snapc
);
381 head
->client_inc
= cpu_to_le32(1); /* always, for now. */
382 head
->flags
= cpu_to_le32(flags
);
383 if (flags
& CEPH_OSD_FLAG_WRITE
)
384 ceph_encode_timespec(&head
->mtime
, mtime
);
385 head
->num_ops
= cpu_to_le16(num_op
);
389 head
->object_len
= cpu_to_le32(oid_len
);
390 memcpy(p
, oid
, oid_len
);
395 osd_req_encode_op(req
, op
, src_op
);
401 data_len
+= req
->r_trail
->length
;
404 head
->snap_seq
= cpu_to_le64(snapc
->seq
);
405 head
->num_snaps
= cpu_to_le32(snapc
->num_snaps
);
406 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
407 put_unaligned_le64(snapc
->snaps
[i
], p
);
412 if (flags
& CEPH_OSD_FLAG_WRITE
) {
413 req
->r_request
->hdr
.data_off
= cpu_to_le16(off
);
414 req
->r_request
->hdr
.data_len
= cpu_to_le32(*plen
+ data_len
);
415 } else if (data_len
) {
416 req
->r_request
->hdr
.data_off
= 0;
417 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
420 req
->r_request
->page_alignment
= req
->r_page_alignment
;
422 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
423 msg_size
= p
- msg
->front
.iov_base
;
424 msg
->front
.iov_len
= msg_size
;
425 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
428 EXPORT_SYMBOL(ceph_osdc_build_request
);
431 * build new request AND message, calculate layout, and adjust file
434 * if the file was recently truncated, we include information about its
435 * old and new size so that the object can be updated appropriately. (we
436 * avoid synchronously deleting truncated objects because it's slow.)
438 * if @do_sync, include a 'startsync' command so that the osd will flush
441 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
442 struct ceph_file_layout
*layout
,
443 struct ceph_vino vino
,
445 int opcode
, int flags
,
446 struct ceph_snap_context
*snapc
,
450 struct timespec
*mtime
,
451 bool use_mempool
, int num_reply
,
454 struct ceph_osd_req_op ops
[3];
455 struct ceph_osd_request
*req
;
458 ops
[0].extent
.truncate_seq
= truncate_seq
;
459 ops
[0].extent
.truncate_size
= truncate_size
;
460 ops
[0].payload_len
= 0;
463 ops
[1].op
= CEPH_OSD_OP_STARTSYNC
;
464 ops
[1].payload_len
= 0;
469 req
= ceph_osdc_alloc_request(osdc
, flags
,
472 GFP_NOFS
, NULL
, NULL
);
476 /* calculate max write size */
477 calc_layout(osdc
, vino
, layout
, off
, plen
, req
, ops
);
478 req
->r_file_layout
= *layout
; /* keep a copy */
480 /* in case it differs from natural alignment that calc_layout
482 req
->r_page_alignment
= page_align
;
484 ceph_osdc_build_request(req
, off
, plen
, ops
,
487 req
->r_oid
, req
->r_oid_len
);
491 EXPORT_SYMBOL(ceph_osdc_new_request
);
494 * We keep osd requests in an rbtree, sorted by ->r_tid.
496 static void __insert_request(struct ceph_osd_client
*osdc
,
497 struct ceph_osd_request
*new)
499 struct rb_node
**p
= &osdc
->requests
.rb_node
;
500 struct rb_node
*parent
= NULL
;
501 struct ceph_osd_request
*req
= NULL
;
505 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
506 if (new->r_tid
< req
->r_tid
)
508 else if (new->r_tid
> req
->r_tid
)
514 rb_link_node(&new->r_node
, parent
, p
);
515 rb_insert_color(&new->r_node
, &osdc
->requests
);
518 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
521 struct ceph_osd_request
*req
;
522 struct rb_node
*n
= osdc
->requests
.rb_node
;
525 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
526 if (tid
< req
->r_tid
)
528 else if (tid
> req
->r_tid
)
536 static struct ceph_osd_request
*
537 __lookup_request_ge(struct ceph_osd_client
*osdc
,
540 struct ceph_osd_request
*req
;
541 struct rb_node
*n
= osdc
->requests
.rb_node
;
544 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
545 if (tid
< req
->r_tid
) {
549 } else if (tid
> req
->r_tid
) {
559 * Resubmit requests pending on the given osd.
561 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
562 struct ceph_osd
*osd
)
564 struct ceph_osd_request
*req
, *nreq
;
567 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
568 err
= __reset_osd(osdc
, osd
);
572 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
573 list_move(&req
->r_req_lru_item
, &osdc
->req_unsent
);
574 dout("requeued %p tid %llu osd%d\n", req
, req
->r_tid
,
577 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
580 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
583 * reregister request prior to unregistering linger so
584 * that r_osd is preserved.
586 BUG_ON(!list_empty(&req
->r_req_lru_item
));
587 __register_request(osdc
, req
);
588 list_add(&req
->r_req_lru_item
, &osdc
->req_unsent
);
589 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
590 __unregister_linger_request(osdc
, req
);
591 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
596 static void kick_osd_requests(struct ceph_osd_client
*osdc
,
597 struct ceph_osd
*kickosd
)
599 mutex_lock(&osdc
->request_mutex
);
600 __kick_osd_requests(osdc
, kickosd
);
601 mutex_unlock(&osdc
->request_mutex
);
605 * If the osd connection drops, we need to resubmit all requests.
607 static void osd_reset(struct ceph_connection
*con
)
609 struct ceph_osd
*osd
= con
->private;
610 struct ceph_osd_client
*osdc
;
614 dout("osd_reset osd%d\n", osd
->o_osd
);
616 down_read(&osdc
->map_sem
);
617 kick_osd_requests(osdc
, osd
);
619 up_read(&osdc
->map_sem
);
623 * Track open sessions with osds.
625 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
)
627 struct ceph_osd
*osd
;
629 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
633 atomic_set(&osd
->o_ref
, 1);
635 INIT_LIST_HEAD(&osd
->o_requests
);
636 INIT_LIST_HEAD(&osd
->o_linger_requests
);
637 INIT_LIST_HEAD(&osd
->o_osd_lru
);
638 osd
->o_incarnation
= 1;
640 ceph_con_init(osdc
->client
->msgr
, &osd
->o_con
);
641 osd
->o_con
.private = osd
;
642 osd
->o_con
.ops
= &osd_con_ops
;
643 osd
->o_con
.peer_name
.type
= CEPH_ENTITY_TYPE_OSD
;
645 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
649 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
651 if (atomic_inc_not_zero(&osd
->o_ref
)) {
652 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
653 atomic_read(&osd
->o_ref
));
656 dout("get_osd %p FAIL\n", osd
);
661 static void put_osd(struct ceph_osd
*osd
)
663 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
664 atomic_read(&osd
->o_ref
) - 1);
665 if (atomic_dec_and_test(&osd
->o_ref
)) {
666 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
668 if (osd
->o_authorizer
)
669 ac
->ops
->destroy_authorizer(ac
, osd
->o_authorizer
);
675 * remove an osd from our map
677 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
679 dout("__remove_osd %p\n", osd
);
680 BUG_ON(!list_empty(&osd
->o_requests
));
681 rb_erase(&osd
->o_node
, &osdc
->osds
);
682 list_del_init(&osd
->o_osd_lru
);
683 ceph_con_close(&osd
->o_con
);
687 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
688 struct ceph_osd
*osd
)
690 dout("__move_osd_to_lru %p\n", osd
);
691 BUG_ON(!list_empty(&osd
->o_osd_lru
));
692 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
693 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
696 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
698 dout("__remove_osd_from_lru %p\n", osd
);
699 if (!list_empty(&osd
->o_osd_lru
))
700 list_del_init(&osd
->o_osd_lru
);
703 static void remove_old_osds(struct ceph_osd_client
*osdc
, int remove_all
)
705 struct ceph_osd
*osd
, *nosd
;
707 dout("__remove_old_osds %p\n", osdc
);
708 mutex_lock(&osdc
->request_mutex
);
709 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
710 if (!remove_all
&& time_before(jiffies
, osd
->lru_ttl
))
712 __remove_osd(osdc
, osd
);
714 mutex_unlock(&osdc
->request_mutex
);
720 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
722 struct ceph_osd_request
*req
;
725 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
726 if (list_empty(&osd
->o_requests
) &&
727 list_empty(&osd
->o_linger_requests
)) {
728 __remove_osd(osdc
, osd
);
729 } else if (memcmp(&osdc
->osdmap
->osd_addr
[osd
->o_osd
],
730 &osd
->o_con
.peer_addr
,
731 sizeof(osd
->o_con
.peer_addr
)) == 0 &&
732 !ceph_con_opened(&osd
->o_con
)) {
733 dout(" osd addr hasn't changed and connection never opened,"
734 " letting msgr retry");
735 /* touch each r_stamp for handle_timeout()'s benfit */
736 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
737 req
->r_stamp
= jiffies
;
740 ceph_con_close(&osd
->o_con
);
741 ceph_con_open(&osd
->o_con
, &osdc
->osdmap
->osd_addr
[osd
->o_osd
]);
742 osd
->o_incarnation
++;
747 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
749 struct rb_node
**p
= &osdc
->osds
.rb_node
;
750 struct rb_node
*parent
= NULL
;
751 struct ceph_osd
*osd
= NULL
;
755 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
756 if (new->o_osd
< osd
->o_osd
)
758 else if (new->o_osd
> osd
->o_osd
)
764 rb_link_node(&new->o_node
, parent
, p
);
765 rb_insert_color(&new->o_node
, &osdc
->osds
);
768 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
770 struct ceph_osd
*osd
;
771 struct rb_node
*n
= osdc
->osds
.rb_node
;
774 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
777 else if (o
> osd
->o_osd
)
785 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
787 schedule_delayed_work(&osdc
->timeout_work
,
788 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
791 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
793 cancel_delayed_work(&osdc
->timeout_work
);
797 * Register request, assign tid. If this is the first request, set up
800 static void __register_request(struct ceph_osd_client
*osdc
,
801 struct ceph_osd_request
*req
)
803 req
->r_tid
= ++osdc
->last_tid
;
804 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
805 INIT_LIST_HEAD(&req
->r_req_lru_item
);
807 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
808 __insert_request(osdc
, req
);
809 ceph_osdc_get_request(req
);
810 osdc
->num_requests
++;
812 if (osdc
->num_requests
== 1) {
813 dout(" first request, scheduling timeout\n");
814 __schedule_osd_timeout(osdc
);
818 static void register_request(struct ceph_osd_client
*osdc
,
819 struct ceph_osd_request
*req
)
821 mutex_lock(&osdc
->request_mutex
);
822 __register_request(osdc
, req
);
823 mutex_unlock(&osdc
->request_mutex
);
827 * called under osdc->request_mutex
829 static void __unregister_request(struct ceph_osd_client
*osdc
,
830 struct ceph_osd_request
*req
)
832 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
833 rb_erase(&req
->r_node
, &osdc
->requests
);
834 osdc
->num_requests
--;
837 /* make sure the original request isn't in flight. */
838 ceph_con_revoke(&req
->r_osd
->o_con
, req
->r_request
);
840 list_del_init(&req
->r_osd_item
);
841 if (list_empty(&req
->r_osd
->o_requests
) &&
842 list_empty(&req
->r_osd
->o_linger_requests
)) {
843 dout("moving osd to %p lru\n", req
->r_osd
);
844 __move_osd_to_lru(osdc
, req
->r_osd
);
846 if (list_empty(&req
->r_linger_item
))
850 ceph_osdc_put_request(req
);
852 list_del_init(&req
->r_req_lru_item
);
853 if (osdc
->num_requests
== 0) {
854 dout(" no requests, canceling timeout\n");
855 __cancel_osd_timeout(osdc
);
860 * Cancel a previously queued request message
862 static void __cancel_request(struct ceph_osd_request
*req
)
864 if (req
->r_sent
&& req
->r_osd
) {
865 ceph_con_revoke(&req
->r_osd
->o_con
, req
->r_request
);
870 static void __register_linger_request(struct ceph_osd_client
*osdc
,
871 struct ceph_osd_request
*req
)
873 dout("__register_linger_request %p\n", req
);
874 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
875 list_add_tail(&req
->r_linger_osd
, &req
->r_osd
->o_linger_requests
);
878 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
879 struct ceph_osd_request
*req
)
881 dout("__unregister_linger_request %p\n", req
);
883 list_del_init(&req
->r_linger_item
);
884 list_del_init(&req
->r_linger_osd
);
886 if (list_empty(&req
->r_osd
->o_requests
) &&
887 list_empty(&req
->r_osd
->o_linger_requests
)) {
888 dout("moving osd to %p lru\n", req
->r_osd
);
889 __move_osd_to_lru(osdc
, req
->r_osd
);
891 if (list_empty(&req
->r_osd_item
))
896 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
897 struct ceph_osd_request
*req
)
899 mutex_lock(&osdc
->request_mutex
);
901 __unregister_linger_request(osdc
, req
);
902 ceph_osdc_put_request(req
);
904 mutex_unlock(&osdc
->request_mutex
);
906 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
908 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
909 struct ceph_osd_request
*req
)
911 if (!req
->r_linger
) {
912 dout("set_request_linger %p\n", req
);
915 * caller is now responsible for calling
916 * unregister_linger_request
918 ceph_osdc_get_request(req
);
921 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
924 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
925 * (as needed), and set the request r_osd appropriately. If there is
926 * no up osd, set r_osd to NULL. Move the request to the appropriate list
927 * (unsent, homeless) or leave on in-flight lru.
929 * Return 0 if unchanged, 1 if changed, or negative on error.
931 * Caller should hold map_sem for read and request_mutex.
933 static int __map_request(struct ceph_osd_client
*osdc
,
934 struct ceph_osd_request
*req
)
936 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
938 int acting
[CEPH_PG_MAX_SIZE
];
942 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
943 err
= ceph_calc_object_layout(&reqhead
->layout
, req
->r_oid
,
944 &req
->r_file_layout
, osdc
->osdmap
);
946 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
949 pgid
= reqhead
->layout
.ol_pgid
;
952 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
958 if ((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
);
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 req
->r_osd
->o_osd
= o
;
990 req
->r_osd
->o_con
.peer_name
.num
= cpu_to_le64(o
);
991 __insert_osd(osdc
, req
->r_osd
);
993 ceph_con_open(&req
->r_osd
->o_con
, &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 int __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
;
1035 * Send any requests in the queue (req_unsent).
1037 static void send_queued(struct ceph_osd_client
*osdc
)
1039 struct ceph_osd_request
*req
, *tmp
;
1041 dout("send_queued\n");
1042 mutex_lock(&osdc
->request_mutex
);
1043 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
) {
1044 __send_request(osdc
, req
);
1046 mutex_unlock(&osdc
->request_mutex
);
1050 * Timeout callback, called every N seconds when 1 or more osd
1051 * requests has been active for more than N seconds. When this
1052 * happens, we ping all OSDs with requests who have timed out to
1053 * ensure any communications channel reset is detected. Reset the
1054 * request timeouts another N seconds in the future as we go.
1055 * Reschedule the timeout event another N seconds in future (unless
1056 * there are no open requests).
1058 static void handle_timeout(struct work_struct
*work
)
1060 struct ceph_osd_client
*osdc
=
1061 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1062 struct ceph_osd_request
*req
, *last_req
= NULL
;
1063 struct ceph_osd
*osd
;
1064 unsigned long timeout
= osdc
->client
->options
->osd_timeout
* HZ
;
1065 unsigned long keepalive
=
1066 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1067 unsigned long last_stamp
= 0;
1068 struct list_head slow_osds
;
1070 down_read(&osdc
->map_sem
);
1072 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1074 mutex_lock(&osdc
->request_mutex
);
1077 * reset osds that appear to be _really_ unresponsive. this
1078 * is a failsafe measure.. we really shouldn't be getting to
1079 * this point if the system is working properly. the monitors
1080 * should mark the osd as failed and we should find out about
1081 * it from an updated osd map.
1083 while (timeout
&& !list_empty(&osdc
->req_lru
)) {
1084 req
= list_entry(osdc
->req_lru
.next
, struct ceph_osd_request
,
1087 if (time_before(jiffies
, req
->r_stamp
+ timeout
))
1090 BUG_ON(req
== last_req
&& req
->r_stamp
== last_stamp
);
1092 last_stamp
= req
->r_stamp
;
1096 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1097 req
->r_tid
, osd
->o_osd
);
1098 __kick_osd_requests(osdc
, osd
);
1102 * ping osds that are a bit slow. this ensures that if there
1103 * is a break in the TCP connection we will notice, and reopen
1104 * a connection with that osd (from the fault callback).
1106 INIT_LIST_HEAD(&slow_osds
);
1107 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1108 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1113 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1114 req
->r_tid
, osd
->o_osd
);
1115 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1117 while (!list_empty(&slow_osds
)) {
1118 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1120 list_del_init(&osd
->o_keepalive_item
);
1121 ceph_con_keepalive(&osd
->o_con
);
1124 __schedule_osd_timeout(osdc
);
1125 mutex_unlock(&osdc
->request_mutex
);
1127 up_read(&osdc
->map_sem
);
1130 static void handle_osds_timeout(struct work_struct
*work
)
1132 struct ceph_osd_client
*osdc
=
1133 container_of(work
, struct ceph_osd_client
,
1134 osds_timeout_work
.work
);
1135 unsigned long delay
=
1136 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1138 dout("osds timeout\n");
1139 down_read(&osdc
->map_sem
);
1140 remove_old_osds(osdc
, 0);
1141 up_read(&osdc
->map_sem
);
1143 schedule_delayed_work(&osdc
->osds_timeout_work
,
1144 round_jiffies_relative(delay
));
1148 * handle osd op reply. either call the callback if it is specified,
1149 * or do the completion to wake up the waiting thread.
1151 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1152 struct ceph_connection
*con
)
1154 struct ceph_osd_reply_head
*rhead
= msg
->front
.iov_base
;
1155 struct ceph_osd_request
*req
;
1157 int numops
, object_len
, flags
;
1160 tid
= le64_to_cpu(msg
->hdr
.tid
);
1161 if (msg
->front
.iov_len
< sizeof(*rhead
))
1163 numops
= le32_to_cpu(rhead
->num_ops
);
1164 object_len
= le32_to_cpu(rhead
->object_len
);
1165 result
= le32_to_cpu(rhead
->result
);
1166 if (msg
->front
.iov_len
!= sizeof(*rhead
) + object_len
+
1167 numops
* sizeof(struct ceph_osd_op
))
1169 dout("handle_reply %p tid %llu result %d\n", msg
, tid
, (int)result
);
1171 mutex_lock(&osdc
->request_mutex
);
1172 req
= __lookup_request(osdc
, tid
);
1174 dout("handle_reply tid %llu dne\n", tid
);
1175 mutex_unlock(&osdc
->request_mutex
);
1178 ceph_osdc_get_request(req
);
1179 flags
= le32_to_cpu(rhead
->flags
);
1182 * if this connection filled our message, drop our reference now, to
1183 * avoid a (safe but slower) revoke later.
1185 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
1186 dout(" dropping con_filling_msg ref %p\n", con
);
1187 req
->r_con_filling_msg
= NULL
;
1191 if (!req
->r_got_reply
) {
1194 req
->r_result
= le32_to_cpu(rhead
->result
);
1195 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1196 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1198 if (req
->r_result
== 0)
1199 req
->r_result
= bytes
;
1201 /* in case this is a write and we need to replay, */
1202 req
->r_reassert_version
= rhead
->reassert_version
;
1204 req
->r_got_reply
= 1;
1205 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1206 dout("handle_reply tid %llu dup ack\n", tid
);
1207 mutex_unlock(&osdc
->request_mutex
);
1211 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1213 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1214 __register_linger_request(osdc
, req
);
1216 /* either this is a read, or we got the safe response */
1218 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1219 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1220 __unregister_request(osdc
, req
);
1222 mutex_unlock(&osdc
->request_mutex
);
1224 if (req
->r_callback
)
1225 req
->r_callback(req
, msg
);
1227 complete_all(&req
->r_completion
);
1229 if (flags
& CEPH_OSD_FLAG_ONDISK
) {
1230 if (req
->r_safe_callback
)
1231 req
->r_safe_callback(req
, msg
);
1232 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1236 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1237 ceph_osdc_put_request(req
);
1241 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1242 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
),
1243 (int)sizeof(*rhead
));
1247 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1249 struct rb_node
*p
, *n
;
1251 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1252 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1255 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1256 memcmp(&osd
->o_con
.peer_addr
,
1257 ceph_osd_addr(osdc
->osdmap
,
1259 sizeof(struct ceph_entity_addr
)) != 0)
1260 __reset_osd(osdc
, osd
);
1265 * Requeue requests whose mapping to an OSD has changed. If requests map to
1266 * no osd, request a new map.
1268 * Caller should hold map_sem for read and request_mutex.
1270 static void kick_requests(struct ceph_osd_client
*osdc
)
1272 struct ceph_osd_request
*req
, *nreq
;
1277 dout("kick_requests\n");
1278 mutex_lock(&osdc
->request_mutex
);
1279 for (p
= rb_first(&osdc
->requests
); p
; p
= rb_next(p
)) {
1280 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1281 err
= __map_request(osdc
, req
);
1283 continue; /* error */
1284 if (req
->r_osd
== NULL
) {
1285 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1286 needmap
++; /* request a newer map */
1287 } else if (err
> 0) {
1288 dout("%p tid %llu requeued on osd%d\n", req
, req
->r_tid
,
1289 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1291 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1295 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1297 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1299 err
= __map_request(osdc
, req
);
1301 continue; /* no change and no osd was specified */
1303 continue; /* hrm! */
1304 if (req
->r_osd
== NULL
) {
1305 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1306 needmap
++; /* request a newer map */
1310 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1311 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1312 __unregister_linger_request(osdc
, req
);
1313 __register_request(osdc
, req
);
1315 mutex_unlock(&osdc
->request_mutex
);
1318 dout("%d requests for down osds, need new map\n", needmap
);
1319 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1325 * Process updated osd map.
1327 * The message contains any number of incremental and full maps, normally
1328 * indicating some sort of topology change in the cluster. Kick requests
1329 * off to different OSDs as needed.
1331 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1333 void *p
, *end
, *next
;
1334 u32 nr_maps
, maplen
;
1336 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1338 struct ceph_fsid fsid
;
1340 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1341 p
= msg
->front
.iov_base
;
1342 end
= p
+ msg
->front
.iov_len
;
1345 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1346 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1347 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1350 down_write(&osdc
->map_sem
);
1352 /* incremental maps */
1353 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1354 dout(" %d inc maps\n", nr_maps
);
1355 while (nr_maps
> 0) {
1356 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1357 epoch
= ceph_decode_32(&p
);
1358 maplen
= ceph_decode_32(&p
);
1359 ceph_decode_need(&p
, end
, maplen
, bad
);
1361 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1362 dout("applying incremental map %u len %d\n",
1364 newmap
= osdmap_apply_incremental(&p
, next
,
1366 osdc
->client
->msgr
);
1367 if (IS_ERR(newmap
)) {
1368 err
= PTR_ERR(newmap
);
1372 if (newmap
!= osdc
->osdmap
) {
1373 ceph_osdmap_destroy(osdc
->osdmap
);
1374 osdc
->osdmap
= newmap
;
1376 kick_requests(osdc
);
1377 reset_changed_osds(osdc
);
1379 dout("ignoring incremental map %u len %d\n",
1389 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1390 dout(" %d full maps\n", nr_maps
);
1392 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1393 epoch
= ceph_decode_32(&p
);
1394 maplen
= ceph_decode_32(&p
);
1395 ceph_decode_need(&p
, end
, maplen
, bad
);
1397 dout("skipping non-latest full map %u len %d\n",
1399 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1400 dout("skipping full map %u len %d, "
1401 "older than our %u\n", epoch
, maplen
,
1402 osdc
->osdmap
->epoch
);
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 ceph_osdmap_destroy(oldmap
);
1415 kick_requests(osdc
);
1422 downgrade_write(&osdc
->map_sem
);
1423 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1425 up_read(&osdc
->map_sem
);
1426 wake_up_all(&osdc
->client
->auth_wq
);
1430 pr_err("osdc handle_map corrupt msg\n");
1432 up_write(&osdc
->map_sem
);
1437 * watch/notify callback event infrastructure
1439 * These callbacks are used both for watch and notify operations.
1441 static void __release_event(struct kref
*kref
)
1443 struct ceph_osd_event
*event
=
1444 container_of(kref
, struct ceph_osd_event
, kref
);
1446 dout("__release_event %p\n", event
);
1450 static void get_event(struct ceph_osd_event
*event
)
1452 kref_get(&event
->kref
);
1455 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1457 kref_put(&event
->kref
, __release_event
);
1459 EXPORT_SYMBOL(ceph_osdc_put_event
);
1461 static void __insert_event(struct ceph_osd_client
*osdc
,
1462 struct ceph_osd_event
*new)
1464 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1465 struct rb_node
*parent
= NULL
;
1466 struct ceph_osd_event
*event
= NULL
;
1470 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1471 if (new->cookie
< event
->cookie
)
1473 else if (new->cookie
> event
->cookie
)
1474 p
= &(*p
)->rb_right
;
1479 rb_link_node(&new->node
, parent
, p
);
1480 rb_insert_color(&new->node
, &osdc
->event_tree
);
1483 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1486 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1487 struct rb_node
*parent
= NULL
;
1488 struct ceph_osd_event
*event
= NULL
;
1492 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1493 if (cookie
< event
->cookie
)
1495 else if (cookie
> event
->cookie
)
1496 p
= &(*p
)->rb_right
;
1503 static void __remove_event(struct ceph_osd_event
*event
)
1505 struct ceph_osd_client
*osdc
= event
->osdc
;
1507 if (!RB_EMPTY_NODE(&event
->node
)) {
1508 dout("__remove_event removed %p\n", event
);
1509 rb_erase(&event
->node
, &osdc
->event_tree
);
1510 ceph_osdc_put_event(event
);
1512 dout("__remove_event didn't remove %p\n", event
);
1516 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1517 void (*event_cb
)(u64
, u64
, u8
, void *),
1518 int one_shot
, void *data
,
1519 struct ceph_osd_event
**pevent
)
1521 struct ceph_osd_event
*event
;
1523 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1527 dout("create_event %p\n", event
);
1528 event
->cb
= event_cb
;
1529 event
->one_shot
= one_shot
;
1532 INIT_LIST_HEAD(&event
->osd_node
);
1533 kref_init(&event
->kref
); /* one ref for us */
1534 kref_get(&event
->kref
); /* one ref for the caller */
1535 init_completion(&event
->completion
);
1537 spin_lock(&osdc
->event_lock
);
1538 event
->cookie
= ++osdc
->event_count
;
1539 __insert_event(osdc
, event
);
1540 spin_unlock(&osdc
->event_lock
);
1545 EXPORT_SYMBOL(ceph_osdc_create_event
);
1547 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1549 struct ceph_osd_client
*osdc
= event
->osdc
;
1551 dout("cancel_event %p\n", event
);
1552 spin_lock(&osdc
->event_lock
);
1553 __remove_event(event
);
1554 spin_unlock(&osdc
->event_lock
);
1555 ceph_osdc_put_event(event
); /* caller's */
1557 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1560 static void do_event_work(struct work_struct
*work
)
1562 struct ceph_osd_event_work
*event_work
=
1563 container_of(work
, struct ceph_osd_event_work
, work
);
1564 struct ceph_osd_event
*event
= event_work
->event
;
1565 u64 ver
= event_work
->ver
;
1566 u64 notify_id
= event_work
->notify_id
;
1567 u8 opcode
= event_work
->opcode
;
1569 dout("do_event_work completing %p\n", event
);
1570 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1571 complete(&event
->completion
);
1572 dout("do_event_work completed %p\n", event
);
1573 ceph_osdc_put_event(event
);
1579 * Process osd watch notifications
1581 void handle_watch_notify(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1585 u64 cookie
, ver
, notify_id
;
1587 struct ceph_osd_event
*event
;
1588 struct ceph_osd_event_work
*event_work
;
1590 p
= msg
->front
.iov_base
;
1591 end
= p
+ msg
->front
.iov_len
;
1593 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1594 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1595 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1596 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1597 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1599 spin_lock(&osdc
->event_lock
);
1600 event
= __find_event(osdc
, cookie
);
1603 if (event
->one_shot
)
1604 __remove_event(event
);
1606 spin_unlock(&osdc
->event_lock
);
1607 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1608 cookie
, ver
, event
);
1610 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1612 dout("ERROR: could not allocate event_work\n");
1615 INIT_WORK(&event_work
->work
, do_event_work
);
1616 event_work
->event
= event
;
1617 event_work
->ver
= ver
;
1618 event_work
->notify_id
= notify_id
;
1619 event_work
->opcode
= opcode
;
1620 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1621 dout("WARNING: failed to queue notify event work\n");
1629 complete(&event
->completion
);
1630 ceph_osdc_put_event(event
);
1634 pr_err("osdc handle_watch_notify corrupt msg\n");
1638 int ceph_osdc_wait_event(struct ceph_osd_event
*event
, unsigned long timeout
)
1642 dout("wait_event %p\n", event
);
1643 err
= wait_for_completion_interruptible_timeout(&event
->completion
,
1645 ceph_osdc_put_event(event
);
1648 dout("wait_event %p returns %d\n", event
, err
);
1651 EXPORT_SYMBOL(ceph_osdc_wait_event
);
1654 * Register request, send initial attempt.
1656 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1657 struct ceph_osd_request
*req
,
1662 req
->r_request
->pages
= req
->r_pages
;
1663 req
->r_request
->nr_pages
= req
->r_num_pages
;
1665 req
->r_request
->bio
= req
->r_bio
;
1667 req
->r_request
->trail
= req
->r_trail
;
1669 register_request(osdc
, req
);
1671 down_read(&osdc
->map_sem
);
1672 mutex_lock(&osdc
->request_mutex
);
1674 * a racing kick_requests() may have sent the message for us
1675 * while we dropped request_mutex above, so only send now if
1676 * the request still han't been touched yet.
1678 if (req
->r_sent
== 0) {
1679 rc
= __map_request(osdc
, req
);
1682 if (req
->r_osd
== NULL
) {
1683 dout("send_request %p no up osds in pg\n", req
);
1684 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1686 rc
= __send_request(osdc
, req
);
1689 dout("osdc_start_request failed send, "
1690 " will retry %lld\n", req
->r_tid
);
1693 __unregister_request(osdc
, req
);
1700 mutex_unlock(&osdc
->request_mutex
);
1701 up_read(&osdc
->map_sem
);
1704 EXPORT_SYMBOL(ceph_osdc_start_request
);
1707 * wait for a request to complete
1709 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1710 struct ceph_osd_request
*req
)
1714 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1716 mutex_lock(&osdc
->request_mutex
);
1717 __cancel_request(req
);
1718 __unregister_request(osdc
, req
);
1719 mutex_unlock(&osdc
->request_mutex
);
1720 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1724 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1725 return req
->r_result
;
1727 EXPORT_SYMBOL(ceph_osdc_wait_request
);
1730 * sync - wait for all in-flight requests to flush. avoid starvation.
1732 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1734 struct ceph_osd_request
*req
;
1735 u64 last_tid
, next_tid
= 0;
1737 mutex_lock(&osdc
->request_mutex
);
1738 last_tid
= osdc
->last_tid
;
1740 req
= __lookup_request_ge(osdc
, next_tid
);
1743 if (req
->r_tid
> last_tid
)
1746 next_tid
= req
->r_tid
+ 1;
1747 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1750 ceph_osdc_get_request(req
);
1751 mutex_unlock(&osdc
->request_mutex
);
1752 dout("sync waiting on tid %llu (last is %llu)\n",
1753 req
->r_tid
, last_tid
);
1754 wait_for_completion(&req
->r_safe_completion
);
1755 mutex_lock(&osdc
->request_mutex
);
1756 ceph_osdc_put_request(req
);
1758 mutex_unlock(&osdc
->request_mutex
);
1759 dout("sync done (thru tid %llu)\n", last_tid
);
1761 EXPORT_SYMBOL(ceph_osdc_sync
);
1766 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
1771 osdc
->client
= client
;
1772 osdc
->osdmap
= NULL
;
1773 init_rwsem(&osdc
->map_sem
);
1774 init_completion(&osdc
->map_waiters
);
1775 osdc
->last_requested_map
= 0;
1776 mutex_init(&osdc
->request_mutex
);
1778 osdc
->osds
= RB_ROOT
;
1779 INIT_LIST_HEAD(&osdc
->osd_lru
);
1780 osdc
->requests
= RB_ROOT
;
1781 INIT_LIST_HEAD(&osdc
->req_lru
);
1782 INIT_LIST_HEAD(&osdc
->req_unsent
);
1783 INIT_LIST_HEAD(&osdc
->req_notarget
);
1784 INIT_LIST_HEAD(&osdc
->req_linger
);
1785 osdc
->num_requests
= 0;
1786 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
1787 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
1788 spin_lock_init(&osdc
->event_lock
);
1789 osdc
->event_tree
= RB_ROOT
;
1790 osdc
->event_count
= 0;
1792 schedule_delayed_work(&osdc
->osds_timeout_work
,
1793 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
1796 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
1797 sizeof(struct ceph_osd_request
));
1798 if (!osdc
->req_mempool
)
1801 err
= ceph_msgpool_init(&osdc
->msgpool_op
, OSD_OP_FRONT_LEN
, 10, true,
1805 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
,
1806 OSD_OPREPLY_FRONT_LEN
, 10, true,
1811 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
1812 if (IS_ERR(osdc
->notify_wq
)) {
1813 err
= PTR_ERR(osdc
->notify_wq
);
1814 osdc
->notify_wq
= NULL
;
1820 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1822 mempool_destroy(osdc
->req_mempool
);
1826 EXPORT_SYMBOL(ceph_osdc_init
);
1828 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
1830 flush_workqueue(osdc
->notify_wq
);
1831 destroy_workqueue(osdc
->notify_wq
);
1832 cancel_delayed_work_sync(&osdc
->timeout_work
);
1833 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
1835 ceph_osdmap_destroy(osdc
->osdmap
);
1836 osdc
->osdmap
= NULL
;
1838 remove_old_osds(osdc
, 1);
1839 WARN_ON(!RB_EMPTY_ROOT(&osdc
->osds
));
1840 mempool_destroy(osdc
->req_mempool
);
1841 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1842 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
1844 EXPORT_SYMBOL(ceph_osdc_stop
);
1847 * Read some contiguous pages. If we cross a stripe boundary, shorten
1848 * *plen. Return number of bytes read, or error.
1850 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
1851 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
1853 u32 truncate_seq
, u64 truncate_size
,
1854 struct page
**pages
, int num_pages
, int page_align
)
1856 struct ceph_osd_request
*req
;
1859 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
1860 vino
.snap
, off
, *plen
);
1861 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
1862 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
1863 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
1864 false, 1, page_align
);
1868 /* it may be a short read due to an object boundary */
1869 req
->r_pages
= pages
;
1871 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1872 off
, *plen
, req
->r_num_pages
, page_align
);
1874 rc
= ceph_osdc_start_request(osdc
, req
, false);
1876 rc
= ceph_osdc_wait_request(osdc
, req
);
1878 ceph_osdc_put_request(req
);
1879 dout("readpages result %d\n", rc
);
1882 EXPORT_SYMBOL(ceph_osdc_readpages
);
1885 * do a synchronous write on N pages
1887 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
1888 struct ceph_file_layout
*layout
,
1889 struct ceph_snap_context
*snapc
,
1891 u32 truncate_seq
, u64 truncate_size
,
1892 struct timespec
*mtime
,
1893 struct page
**pages
, int num_pages
,
1894 int flags
, int do_sync
, bool nofail
)
1896 struct ceph_osd_request
*req
;
1898 int page_align
= off
& ~PAGE_MASK
;
1900 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
1901 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
1903 flags
| CEPH_OSD_FLAG_ONDISK
|
1904 CEPH_OSD_FLAG_WRITE
,
1906 truncate_seq
, truncate_size
, mtime
,
1907 nofail
, 1, page_align
);
1911 /* it may be a short write due to an object boundary */
1912 req
->r_pages
= pages
;
1913 dout("writepages %llu~%llu (%d pages)\n", off
, len
,
1916 rc
= ceph_osdc_start_request(osdc
, req
, nofail
);
1918 rc
= ceph_osdc_wait_request(osdc
, req
);
1920 ceph_osdc_put_request(req
);
1923 dout("writepages result %d\n", rc
);
1926 EXPORT_SYMBOL(ceph_osdc_writepages
);
1929 * handle incoming message
1931 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1933 struct ceph_osd
*osd
= con
->private;
1934 struct ceph_osd_client
*osdc
;
1935 int type
= le16_to_cpu(msg
->hdr
.type
);
1942 case CEPH_MSG_OSD_MAP
:
1943 ceph_osdc_handle_map(osdc
, msg
);
1945 case CEPH_MSG_OSD_OPREPLY
:
1946 handle_reply(osdc
, msg
, con
);
1948 case CEPH_MSG_WATCH_NOTIFY
:
1949 handle_watch_notify(osdc
, msg
);
1953 pr_err("received unknown message type %d %s\n", type
,
1954 ceph_msg_type_name(type
));
1961 * lookup and return message for incoming reply. set up reply message
1964 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
1965 struct ceph_msg_header
*hdr
,
1968 struct ceph_osd
*osd
= con
->private;
1969 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
1971 struct ceph_osd_request
*req
;
1972 int front
= le32_to_cpu(hdr
->front_len
);
1973 int data_len
= le32_to_cpu(hdr
->data_len
);
1976 tid
= le64_to_cpu(hdr
->tid
);
1977 mutex_lock(&osdc
->request_mutex
);
1978 req
= __lookup_request(osdc
, tid
);
1982 pr_info("get_reply unknown tid %llu from osd%d\n", tid
,
1987 if (req
->r_con_filling_msg
) {
1988 dout("get_reply revoking msg %p from old con %p\n",
1989 req
->r_reply
, req
->r_con_filling_msg
);
1990 ceph_con_revoke_message(req
->r_con_filling_msg
, req
->r_reply
);
1991 ceph_con_put(req
->r_con_filling_msg
);
1992 req
->r_con_filling_msg
= NULL
;
1995 if (front
> req
->r_reply
->front
.iov_len
) {
1996 pr_warning("get_reply front %d > preallocated %d\n",
1997 front
, (int)req
->r_reply
->front
.iov_len
);
1998 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
);
2001 ceph_msg_put(req
->r_reply
);
2004 m
= ceph_msg_get(req
->r_reply
);
2007 int want
= calc_pages_for(req
->r_page_alignment
, data_len
);
2009 if (unlikely(req
->r_num_pages
< want
)) {
2010 pr_warning("tid %lld reply %d > expected %d pages\n",
2011 tid
, want
, m
->nr_pages
);
2017 m
->pages
= req
->r_pages
;
2018 m
->nr_pages
= req
->r_num_pages
;
2019 m
->page_alignment
= req
->r_page_alignment
;
2021 m
->bio
= req
->r_bio
;
2025 req
->r_con_filling_msg
= ceph_con_get(con
);
2026 dout("get_reply tid %lld %p\n", tid
, m
);
2029 mutex_unlock(&osdc
->request_mutex
);
2034 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2035 struct ceph_msg_header
*hdr
,
2038 struct ceph_osd
*osd
= con
->private;
2039 int type
= le16_to_cpu(hdr
->type
);
2040 int front
= le32_to_cpu(hdr
->front_len
);
2043 case CEPH_MSG_OSD_MAP
:
2044 case CEPH_MSG_WATCH_NOTIFY
:
2045 return ceph_msg_new(type
, front
, GFP_NOFS
);
2046 case CEPH_MSG_OSD_OPREPLY
:
2047 return get_reply(con
, hdr
, skip
);
2049 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2057 * Wrappers to refcount containing ceph_osd struct
2059 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2061 struct ceph_osd
*osd
= con
->private;
2067 static void put_osd_con(struct ceph_connection
*con
)
2069 struct ceph_osd
*osd
= con
->private;
2076 static int get_authorizer(struct ceph_connection
*con
,
2077 void **buf
, int *len
, int *proto
,
2078 void **reply_buf
, int *reply_len
, int force_new
)
2080 struct ceph_osd
*o
= con
->private;
2081 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2082 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2085 if (force_new
&& o
->o_authorizer
) {
2086 ac
->ops
->destroy_authorizer(ac
, o
->o_authorizer
);
2087 o
->o_authorizer
= NULL
;
2089 if (o
->o_authorizer
== NULL
) {
2090 ret
= ac
->ops
->create_authorizer(
2091 ac
, CEPH_ENTITY_TYPE_OSD
,
2093 &o
->o_authorizer_buf
,
2094 &o
->o_authorizer_buf_len
,
2095 &o
->o_authorizer_reply_buf
,
2096 &o
->o_authorizer_reply_buf_len
);
2101 *proto
= ac
->protocol
;
2102 *buf
= o
->o_authorizer_buf
;
2103 *len
= o
->o_authorizer_buf_len
;
2104 *reply_buf
= o
->o_authorizer_reply_buf
;
2105 *reply_len
= o
->o_authorizer_reply_buf_len
;
2110 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2112 struct ceph_osd
*o
= con
->private;
2113 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2114 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2116 return ac
->ops
->verify_authorizer_reply(ac
, o
->o_authorizer
, len
);
2119 static int invalidate_authorizer(struct ceph_connection
*con
)
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
;
2125 if (ac
->ops
->invalidate_authorizer
)
2126 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2128 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2131 static const struct ceph_connection_operations osd_con_ops
= {
2134 .dispatch
= dispatch
,
2135 .get_authorizer
= get_authorizer
,
2136 .verify_authorizer_reply
= verify_authorizer_reply
,
2137 .invalidate_authorizer
= invalidate_authorizer
,
2138 .alloc_msg
= alloc_msg
,