1 #include "ceph_debug.h"
4 #include <linux/highmem.h>
6 #include <linux/pagemap.h>
7 #include <linux/slab.h>
8 #include <linux/uaccess.h>
11 #include "osd_client.h"
12 #include "messenger.h"
16 #define OSD_OP_FRONT_LEN 4096
17 #define OSD_OPREPLY_FRONT_LEN 512
19 static const struct ceph_connection_operations osd_con_ops
;
20 static int __kick_requests(struct ceph_osd_client
*osdc
,
21 struct ceph_osd
*kickosd
);
23 static void kick_requests(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
);
26 * Implement client access to distributed object storage cluster.
28 * All data objects are stored within a cluster/cloud of OSDs, or
29 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
30 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
31 * remote daemons serving up and coordinating consistent and safe
34 * Cluster membership and the mapping of data objects onto storage devices
35 * are described by the osd map.
37 * We keep track of pending OSD requests (read, write), resubmit
38 * requests to different OSDs when the cluster topology/data layout
39 * change, or retry the affected requests when the communications
40 * channel with an OSD is reset.
44 * calculate the mapping of a file extent onto an object, and fill out the
45 * request accordingly. shorten extent as necessary if it crosses an
48 * fill osd op in request message.
50 static void calc_layout(struct ceph_osd_client
*osdc
,
51 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
53 struct ceph_osd_request
*req
)
55 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
56 struct ceph_osd_op
*op
= (void *)(reqhead
+ 1);
58 u64 objoff
, objlen
; /* extent in object */
61 reqhead
->snapid
= cpu_to_le64(vino
.snap
);
64 ceph_calc_file_object_mapping(layout
, off
, plen
, &bno
,
67 dout(" skipping last %llu, final file extent %llu~%llu\n",
68 orig_len
- *plen
, off
, *plen
);
70 sprintf(req
->r_oid
, "%llx.%08llx", vino
.ino
, bno
);
71 req
->r_oid_len
= strlen(req
->r_oid
);
73 op
->extent
.offset
= cpu_to_le64(objoff
);
74 op
->extent
.length
= cpu_to_le64(objlen
);
75 req
->r_num_pages
= calc_pages_for(off
, *plen
);
77 dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
78 req
->r_oid
, req
->r_oid_len
, objoff
, objlen
, req
->r_num_pages
);
84 void ceph_osdc_release_request(struct kref
*kref
)
86 struct ceph_osd_request
*req
= container_of(kref
,
87 struct ceph_osd_request
,
91 ceph_msg_put(req
->r_request
);
93 ceph_msg_put(req
->r_reply
);
94 if (req
->r_con_filling_msg
) {
95 dout("release_request revoking pages %p from con %p\n",
96 req
->r_pages
, req
->r_con_filling_msg
);
97 ceph_con_revoke_message(req
->r_con_filling_msg
,
99 ceph_con_put(req
->r_con_filling_msg
);
101 if (req
->r_own_pages
)
102 ceph_release_page_vector(req
->r_pages
,
104 ceph_put_snap_context(req
->r_snapc
);
106 mempool_free(req
, req
->r_osdc
->req_mempool
);
112 * build new request AND message, calculate layout, and adjust file
115 * if the file was recently truncated, we include information about its
116 * old and new size so that the object can be updated appropriately. (we
117 * avoid synchronously deleting truncated objects because it's slow.)
119 * if @do_sync, include a 'startsync' command so that the osd will flush
122 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
123 struct ceph_file_layout
*layout
,
124 struct ceph_vino vino
,
126 int opcode
, int flags
,
127 struct ceph_snap_context
*snapc
,
131 struct timespec
*mtime
,
132 bool use_mempool
, int num_reply
)
134 struct ceph_osd_request
*req
;
135 struct ceph_msg
*msg
;
136 struct ceph_osd_request_head
*head
;
137 struct ceph_osd_op
*op
;
139 int num_op
= 1 + do_sync
;
140 size_t msg_size
= sizeof(*head
) + num_op
*sizeof(*op
);
144 req
= mempool_alloc(osdc
->req_mempool
, GFP_NOFS
);
145 memset(req
, 0, sizeof(*req
));
147 req
= kzalloc(sizeof(*req
), GFP_NOFS
);
153 req
->r_mempool
= use_mempool
;
154 kref_init(&req
->r_kref
);
155 init_completion(&req
->r_completion
);
156 init_completion(&req
->r_safe_completion
);
157 INIT_LIST_HEAD(&req
->r_unsafe_item
);
158 req
->r_flags
= flags
;
160 WARN_ON((flags
& (CEPH_OSD_FLAG_READ
|CEPH_OSD_FLAG_WRITE
)) == 0);
162 /* create reply message */
164 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
166 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
167 OSD_OPREPLY_FRONT_LEN
, GFP_NOFS
);
169 ceph_osdc_put_request(req
);
174 /* create request message; allow space for oid */
177 msg_size
+= sizeof(u64
) * snapc
->num_snaps
;
179 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
181 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, GFP_NOFS
);
183 ceph_osdc_put_request(req
);
186 msg
->hdr
.type
= cpu_to_le16(CEPH_MSG_OSD_OP
);
187 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
188 head
= msg
->front
.iov_base
;
189 op
= (void *)(head
+ 1);
190 p
= (void *)(op
+ num_op
);
192 req
->r_request
= msg
;
193 req
->r_snapc
= ceph_get_snap_context(snapc
);
195 head
->client_inc
= cpu_to_le32(1); /* always, for now. */
196 head
->flags
= cpu_to_le32(flags
);
197 if (flags
& CEPH_OSD_FLAG_WRITE
)
198 ceph_encode_timespec(&head
->mtime
, mtime
);
199 head
->num_ops
= cpu_to_le16(num_op
);
200 op
->op
= cpu_to_le16(opcode
);
202 /* calculate max write size */
203 calc_layout(osdc
, vino
, layout
, off
, plen
, req
);
204 req
->r_file_layout
= *layout
; /* keep a copy */
206 if (flags
& CEPH_OSD_FLAG_WRITE
) {
207 req
->r_request
->hdr
.data_off
= cpu_to_le16(off
);
208 req
->r_request
->hdr
.data_len
= cpu_to_le32(*plen
);
209 op
->payload_len
= cpu_to_le32(*plen
);
211 op
->extent
.truncate_size
= cpu_to_le64(truncate_size
);
212 op
->extent
.truncate_seq
= cpu_to_le32(truncate_seq
);
215 head
->object_len
= cpu_to_le32(req
->r_oid_len
);
216 memcpy(p
, req
->r_oid
, req
->r_oid_len
);
221 op
->op
= cpu_to_le16(CEPH_OSD_OP_STARTSYNC
);
224 head
->snap_seq
= cpu_to_le64(snapc
->seq
);
225 head
->num_snaps
= cpu_to_le32(snapc
->num_snaps
);
226 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
227 put_unaligned_le64(snapc
->snaps
[i
], p
);
232 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
233 msg_size
= p
- msg
->front
.iov_base
;
234 msg
->front
.iov_len
= msg_size
;
235 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
240 * We keep osd requests in an rbtree, sorted by ->r_tid.
242 static void __insert_request(struct ceph_osd_client
*osdc
,
243 struct ceph_osd_request
*new)
245 struct rb_node
**p
= &osdc
->requests
.rb_node
;
246 struct rb_node
*parent
= NULL
;
247 struct ceph_osd_request
*req
= NULL
;
251 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
252 if (new->r_tid
< req
->r_tid
)
254 else if (new->r_tid
> req
->r_tid
)
260 rb_link_node(&new->r_node
, parent
, p
);
261 rb_insert_color(&new->r_node
, &osdc
->requests
);
264 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
267 struct ceph_osd_request
*req
;
268 struct rb_node
*n
= osdc
->requests
.rb_node
;
271 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
272 if (tid
< req
->r_tid
)
274 else if (tid
> req
->r_tid
)
282 static struct ceph_osd_request
*
283 __lookup_request_ge(struct ceph_osd_client
*osdc
,
286 struct ceph_osd_request
*req
;
287 struct rb_node
*n
= osdc
->requests
.rb_node
;
290 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
291 if (tid
< req
->r_tid
) {
295 } else if (tid
> req
->r_tid
) {
306 * If the osd connection drops, we need to resubmit all requests.
308 static void osd_reset(struct ceph_connection
*con
)
310 struct ceph_osd
*osd
= con
->private;
311 struct ceph_osd_client
*osdc
;
315 dout("osd_reset osd%d\n", osd
->o_osd
);
317 down_read(&osdc
->map_sem
);
318 kick_requests(osdc
, osd
);
319 up_read(&osdc
->map_sem
);
323 * Track open sessions with osds.
325 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
)
327 struct ceph_osd
*osd
;
329 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
333 atomic_set(&osd
->o_ref
, 1);
335 INIT_LIST_HEAD(&osd
->o_requests
);
336 INIT_LIST_HEAD(&osd
->o_osd_lru
);
337 osd
->o_incarnation
= 1;
339 ceph_con_init(osdc
->client
->msgr
, &osd
->o_con
);
340 osd
->o_con
.private = osd
;
341 osd
->o_con
.ops
= &osd_con_ops
;
342 osd
->o_con
.peer_name
.type
= CEPH_ENTITY_TYPE_OSD
;
344 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
348 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
350 if (atomic_inc_not_zero(&osd
->o_ref
)) {
351 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
352 atomic_read(&osd
->o_ref
));
355 dout("get_osd %p FAIL\n", osd
);
360 static void put_osd(struct ceph_osd
*osd
)
362 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
363 atomic_read(&osd
->o_ref
) - 1);
364 if (atomic_dec_and_test(&osd
->o_ref
)) {
365 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
367 if (osd
->o_authorizer
)
368 ac
->ops
->destroy_authorizer(ac
, osd
->o_authorizer
);
374 * remove an osd from our map
376 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
378 dout("__remove_osd %p\n", osd
);
379 BUG_ON(!list_empty(&osd
->o_requests
));
380 rb_erase(&osd
->o_node
, &osdc
->osds
);
381 list_del_init(&osd
->o_osd_lru
);
382 ceph_con_close(&osd
->o_con
);
386 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
387 struct ceph_osd
*osd
)
389 dout("__move_osd_to_lru %p\n", osd
);
390 BUG_ON(!list_empty(&osd
->o_osd_lru
));
391 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
392 osd
->lru_ttl
= jiffies
+ osdc
->client
->mount_args
->osd_idle_ttl
* HZ
;
395 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
397 dout("__remove_osd_from_lru %p\n", osd
);
398 if (!list_empty(&osd
->o_osd_lru
))
399 list_del_init(&osd
->o_osd_lru
);
402 static void remove_old_osds(struct ceph_osd_client
*osdc
, int remove_all
)
404 struct ceph_osd
*osd
, *nosd
;
406 dout("__remove_old_osds %p\n", osdc
);
407 mutex_lock(&osdc
->request_mutex
);
408 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
409 if (!remove_all
&& time_before(jiffies
, osd
->lru_ttl
))
411 __remove_osd(osdc
, osd
);
413 mutex_unlock(&osdc
->request_mutex
);
419 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
421 struct ceph_osd_request
*req
;
424 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
425 if (list_empty(&osd
->o_requests
)) {
426 __remove_osd(osdc
, osd
);
427 } else if (memcmp(&osdc
->osdmap
->osd_addr
[osd
->o_osd
],
428 &osd
->o_con
.peer_addr
,
429 sizeof(osd
->o_con
.peer_addr
)) == 0 &&
430 !ceph_con_opened(&osd
->o_con
)) {
431 dout(" osd addr hasn't changed and connection never opened,"
432 " letting msgr retry");
433 /* touch each r_stamp for handle_timeout()'s benfit */
434 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
435 req
->r_stamp
= jiffies
;
438 ceph_con_close(&osd
->o_con
);
439 ceph_con_open(&osd
->o_con
, &osdc
->osdmap
->osd_addr
[osd
->o_osd
]);
440 osd
->o_incarnation
++;
445 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
447 struct rb_node
**p
= &osdc
->osds
.rb_node
;
448 struct rb_node
*parent
= NULL
;
449 struct ceph_osd
*osd
= NULL
;
453 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
454 if (new->o_osd
< osd
->o_osd
)
456 else if (new->o_osd
> osd
->o_osd
)
462 rb_link_node(&new->o_node
, parent
, p
);
463 rb_insert_color(&new->o_node
, &osdc
->osds
);
466 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
468 struct ceph_osd
*osd
;
469 struct rb_node
*n
= osdc
->osds
.rb_node
;
472 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
475 else if (o
> osd
->o_osd
)
483 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
485 schedule_delayed_work(&osdc
->timeout_work
,
486 osdc
->client
->mount_args
->osd_keepalive_timeout
* HZ
);
489 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
491 cancel_delayed_work(&osdc
->timeout_work
);
495 * Register request, assign tid. If this is the first request, set up
498 static void register_request(struct ceph_osd_client
*osdc
,
499 struct ceph_osd_request
*req
)
501 mutex_lock(&osdc
->request_mutex
);
502 req
->r_tid
= ++osdc
->last_tid
;
503 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
504 INIT_LIST_HEAD(&req
->r_req_lru_item
);
506 dout("register_request %p tid %lld\n", req
, req
->r_tid
);
507 __insert_request(osdc
, req
);
508 ceph_osdc_get_request(req
);
509 osdc
->num_requests
++;
511 if (osdc
->num_requests
== 1) {
512 dout(" first request, scheduling timeout\n");
513 __schedule_osd_timeout(osdc
);
515 mutex_unlock(&osdc
->request_mutex
);
519 * called under osdc->request_mutex
521 static void __unregister_request(struct ceph_osd_client
*osdc
,
522 struct ceph_osd_request
*req
)
524 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
525 rb_erase(&req
->r_node
, &osdc
->requests
);
526 osdc
->num_requests
--;
529 /* make sure the original request isn't in flight. */
530 ceph_con_revoke(&req
->r_osd
->o_con
, req
->r_request
);
532 list_del_init(&req
->r_osd_item
);
533 if (list_empty(&req
->r_osd
->o_requests
))
534 __move_osd_to_lru(osdc
, req
->r_osd
);
538 ceph_osdc_put_request(req
);
540 list_del_init(&req
->r_req_lru_item
);
541 if (osdc
->num_requests
== 0) {
542 dout(" no requests, canceling timeout\n");
543 __cancel_osd_timeout(osdc
);
548 * Cancel a previously queued request message
550 static void __cancel_request(struct ceph_osd_request
*req
)
553 ceph_con_revoke(&req
->r_osd
->o_con
, req
->r_request
);
556 list_del_init(&req
->r_req_lru_item
);
560 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
561 * (as needed), and set the request r_osd appropriately. If there is
562 * no up osd, set r_osd to NULL.
564 * Return 0 if unchanged, 1 if changed, or negative on error.
566 * Caller should hold map_sem for read and request_mutex.
568 static int __map_osds(struct ceph_osd_client
*osdc
,
569 struct ceph_osd_request
*req
)
571 struct ceph_osd_request_head
*reqhead
= req
->r_request
->front
.iov_base
;
573 int acting
[CEPH_PG_MAX_SIZE
];
577 dout("map_osds %p tid %lld\n", req
, req
->r_tid
);
578 err
= ceph_calc_object_layout(&reqhead
->layout
, req
->r_oid
,
579 &req
->r_file_layout
, osdc
->osdmap
);
582 pgid
= reqhead
->layout
.ol_pgid
;
585 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
591 if ((req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
592 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
593 req
->r_num_pg_osds
== num
&&
594 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
595 (req
->r_osd
== NULL
&& o
== -1))
596 return 0; /* no change */
598 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
599 req
->r_tid
, le32_to_cpu(pgid
.pool
), le16_to_cpu(pgid
.ps
), o
,
600 req
->r_osd
? req
->r_osd
->o_osd
: -1);
602 /* record full pg acting set */
603 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
604 req
->r_num_pg_osds
= num
;
607 __cancel_request(req
);
608 list_del_init(&req
->r_osd_item
);
612 req
->r_osd
= __lookup_osd(osdc
, o
);
613 if (!req
->r_osd
&& o
>= 0) {
615 req
->r_osd
= create_osd(osdc
);
619 dout("map_osds osd %p is osd%d\n", req
->r_osd
, o
);
620 req
->r_osd
->o_osd
= o
;
621 req
->r_osd
->o_con
.peer_name
.num
= cpu_to_le64(o
);
622 __insert_osd(osdc
, req
->r_osd
);
624 ceph_con_open(&req
->r_osd
->o_con
, &osdc
->osdmap
->osd_addr
[o
]);
628 __remove_osd_from_lru(req
->r_osd
);
629 list_add(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
631 err
= 1; /* osd or pg changed */
638 * caller should hold map_sem (for read) and request_mutex
640 static int __send_request(struct ceph_osd_client
*osdc
,
641 struct ceph_osd_request
*req
)
643 struct ceph_osd_request_head
*reqhead
;
646 err
= __map_osds(osdc
, req
);
649 if (req
->r_osd
== NULL
) {
650 dout("send_request %p no up osds in pg\n", req
);
651 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
655 dout("send_request %p tid %llu to osd%d flags %d\n",
656 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
);
658 reqhead
= req
->r_request
->front
.iov_base
;
659 reqhead
->osdmap_epoch
= cpu_to_le32(osdc
->osdmap
->epoch
);
660 reqhead
->flags
|= cpu_to_le32(req
->r_flags
); /* e.g., RETRY */
661 reqhead
->reassert_version
= req
->r_reassert_version
;
663 req
->r_stamp
= jiffies
;
664 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
666 ceph_msg_get(req
->r_request
); /* send consumes a ref */
667 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
668 req
->r_sent
= req
->r_osd
->o_incarnation
;
673 * Timeout callback, called every N seconds when 1 or more osd
674 * requests has been active for more than N seconds. When this
675 * happens, we ping all OSDs with requests who have timed out to
676 * ensure any communications channel reset is detected. Reset the
677 * request timeouts another N seconds in the future as we go.
678 * Reschedule the timeout event another N seconds in future (unless
679 * there are no open requests).
681 static void handle_timeout(struct work_struct
*work
)
683 struct ceph_osd_client
*osdc
=
684 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
685 struct ceph_osd_request
*req
, *last_req
= NULL
;
686 struct ceph_osd
*osd
;
687 unsigned long timeout
= osdc
->client
->mount_args
->osd_timeout
* HZ
;
688 unsigned long keepalive
=
689 osdc
->client
->mount_args
->osd_keepalive_timeout
* HZ
;
690 unsigned long last_stamp
= 0;
692 struct list_head slow_osds
;
695 down_read(&osdc
->map_sem
);
697 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
699 mutex_lock(&osdc
->request_mutex
);
700 for (p
= rb_first(&osdc
->requests
); p
; p
= rb_next(p
)) {
701 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
706 dout("osdc resending prev failed %lld\n", req
->r_tid
);
707 err
= __send_request(osdc
, req
);
709 dout("osdc failed again on %lld\n", req
->r_tid
);
711 req
->r_resend
= false;
717 * reset osds that appear to be _really_ unresponsive. this
718 * is a failsafe measure.. we really shouldn't be getting to
719 * this point if the system is working properly. the monitors
720 * should mark the osd as failed and we should find out about
721 * it from an updated osd map.
723 while (timeout
&& !list_empty(&osdc
->req_lru
)) {
724 req
= list_entry(osdc
->req_lru
.next
, struct ceph_osd_request
,
727 if (time_before(jiffies
, req
->r_stamp
+ timeout
))
730 BUG_ON(req
== last_req
&& req
->r_stamp
== last_stamp
);
732 last_stamp
= req
->r_stamp
;
736 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
737 req
->r_tid
, osd
->o_osd
);
738 __kick_requests(osdc
, osd
);
742 * ping osds that are a bit slow. this ensures that if there
743 * is a break in the TCP connection we will notice, and reopen
744 * a connection with that osd (from the fault callback).
746 INIT_LIST_HEAD(&slow_osds
);
747 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
748 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
753 dout(" tid %llu is slow, will send keepalive on osd%d\n",
754 req
->r_tid
, osd
->o_osd
);
755 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
757 while (!list_empty(&slow_osds
)) {
758 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
760 list_del_init(&osd
->o_keepalive_item
);
761 ceph_con_keepalive(&osd
->o_con
);
764 __schedule_osd_timeout(osdc
);
765 mutex_unlock(&osdc
->request_mutex
);
767 up_read(&osdc
->map_sem
);
770 static void handle_osds_timeout(struct work_struct
*work
)
772 struct ceph_osd_client
*osdc
=
773 container_of(work
, struct ceph_osd_client
,
774 osds_timeout_work
.work
);
775 unsigned long delay
=
776 osdc
->client
->mount_args
->osd_idle_ttl
* HZ
>> 2;
778 dout("osds timeout\n");
779 down_read(&osdc
->map_sem
);
780 remove_old_osds(osdc
, 0);
781 up_read(&osdc
->map_sem
);
783 schedule_delayed_work(&osdc
->osds_timeout_work
,
784 round_jiffies_relative(delay
));
788 * handle osd op reply. either call the callback if it is specified,
789 * or do the completion to wake up the waiting thread.
791 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
792 struct ceph_connection
*con
)
794 struct ceph_osd_reply_head
*rhead
= msg
->front
.iov_base
;
795 struct ceph_osd_request
*req
;
797 int numops
, object_len
, flags
;
800 tid
= le64_to_cpu(msg
->hdr
.tid
);
801 if (msg
->front
.iov_len
< sizeof(*rhead
))
803 numops
= le32_to_cpu(rhead
->num_ops
);
804 object_len
= le32_to_cpu(rhead
->object_len
);
805 result
= le32_to_cpu(rhead
->result
);
806 if (msg
->front
.iov_len
!= sizeof(*rhead
) + object_len
+
807 numops
* sizeof(struct ceph_osd_op
))
809 dout("handle_reply %p tid %llu result %d\n", msg
, tid
, (int)result
);
812 mutex_lock(&osdc
->request_mutex
);
813 req
= __lookup_request(osdc
, tid
);
815 dout("handle_reply tid %llu dne\n", tid
);
816 mutex_unlock(&osdc
->request_mutex
);
819 ceph_osdc_get_request(req
);
820 flags
= le32_to_cpu(rhead
->flags
);
823 * if this connection filled our message, drop our reference now, to
824 * avoid a (safe but slower) revoke later.
826 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
827 dout(" dropping con_filling_msg ref %p\n", con
);
828 req
->r_con_filling_msg
= NULL
;
832 if (!req
->r_got_reply
) {
835 req
->r_result
= le32_to_cpu(rhead
->result
);
836 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
837 dout("handle_reply result %d bytes %d\n", req
->r_result
,
839 if (req
->r_result
== 0)
840 req
->r_result
= bytes
;
842 /* in case this is a write and we need to replay, */
843 req
->r_reassert_version
= rhead
->reassert_version
;
845 req
->r_got_reply
= 1;
846 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
847 dout("handle_reply tid %llu dup ack\n", tid
);
848 mutex_unlock(&osdc
->request_mutex
);
852 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
854 /* either this is a read, or we got the safe response */
856 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
857 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
858 __unregister_request(osdc
, req
);
860 mutex_unlock(&osdc
->request_mutex
);
863 req
->r_callback(req
, msg
);
865 complete_all(&req
->r_completion
);
867 if (flags
& CEPH_OSD_FLAG_ONDISK
) {
868 if (req
->r_safe_callback
)
869 req
->r_safe_callback(req
, msg
);
870 complete_all(&req
->r_safe_completion
); /* fsync waiter */
874 ceph_osdc_put_request(req
);
878 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
879 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
),
880 (int)sizeof(*rhead
));
885 static int __kick_requests(struct ceph_osd_client
*osdc
,
886 struct ceph_osd
*kickosd
)
888 struct ceph_osd_request
*req
;
889 struct rb_node
*p
, *n
;
893 dout("kick_requests osd%d\n", kickosd
? kickosd
->o_osd
: -1);
895 err
= __reset_osd(osdc
, kickosd
);
899 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
900 struct ceph_osd
*osd
=
901 rb_entry(p
, struct ceph_osd
, o_node
);
904 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
905 memcmp(&osd
->o_con
.peer_addr
,
906 ceph_osd_addr(osdc
->osdmap
,
908 sizeof(struct ceph_entity_addr
)) != 0)
909 __reset_osd(osdc
, osd
);
913 for (p
= rb_first(&osdc
->requests
); p
; p
= rb_next(p
)) {
914 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
917 dout(" r_resend set on tid %llu\n", req
->r_tid
);
918 __cancel_request(req
);
921 if (req
->r_osd
&& kickosd
== req
->r_osd
) {
922 __cancel_request(req
);
926 err
= __map_osds(osdc
, req
);
928 continue; /* no change */
931 * FIXME: really, we should set the request
932 * error and fail if this isn't a 'nofail'
933 * request, but that's a fair bit more
934 * complicated to do. So retry!
936 dout(" setting r_resend on %llu\n", req
->r_tid
);
937 req
->r_resend
= true;
940 if (req
->r_osd
== NULL
) {
941 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
942 needmap
++; /* request a newer map */
947 dout("kicking %p tid %llu osd%d\n", req
, req
->r_tid
,
948 req
->r_osd
? req
->r_osd
->o_osd
: -1);
949 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
950 err
= __send_request(osdc
, req
);
952 dout(" setting r_resend on %llu\n", req
->r_tid
);
953 req
->r_resend
= true;
961 * Resubmit osd requests whose osd or osd address has changed. Request
962 * a new osd map if osds are down, or we are otherwise unable to determine
963 * how to direct a request.
965 * Close connections to down osds.
967 * If @who is specified, resubmit requests for that specific osd.
969 * Caller should hold map_sem for read and request_mutex.
971 static void kick_requests(struct ceph_osd_client
*osdc
,
972 struct ceph_osd
*kickosd
)
976 mutex_lock(&osdc
->request_mutex
);
977 needmap
= __kick_requests(osdc
, kickosd
);
978 mutex_unlock(&osdc
->request_mutex
);
981 dout("%d requests for down osds, need new map\n", needmap
);
982 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
987 * Process updated osd map.
989 * The message contains any number of incremental and full maps, normally
990 * indicating some sort of topology change in the cluster. Kick requests
991 * off to different OSDs as needed.
993 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
995 void *p
, *end
, *next
;
998 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1000 struct ceph_fsid fsid
;
1002 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1003 p
= msg
->front
.iov_base
;
1004 end
= p
+ msg
->front
.iov_len
;
1007 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1008 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1009 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1012 down_write(&osdc
->map_sem
);
1014 /* incremental maps */
1015 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1016 dout(" %d inc maps\n", nr_maps
);
1017 while (nr_maps
> 0) {
1018 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1019 epoch
= ceph_decode_32(&p
);
1020 maplen
= ceph_decode_32(&p
);
1021 ceph_decode_need(&p
, end
, maplen
, bad
);
1023 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1024 dout("applying incremental map %u len %d\n",
1026 newmap
= osdmap_apply_incremental(&p
, next
,
1028 osdc
->client
->msgr
);
1029 if (IS_ERR(newmap
)) {
1030 err
= PTR_ERR(newmap
);
1034 if (newmap
!= osdc
->osdmap
) {
1035 ceph_osdmap_destroy(osdc
->osdmap
);
1036 osdc
->osdmap
= newmap
;
1039 dout("ignoring incremental map %u len %d\n",
1049 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1050 dout(" %d full maps\n", nr_maps
);
1052 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1053 epoch
= ceph_decode_32(&p
);
1054 maplen
= ceph_decode_32(&p
);
1055 ceph_decode_need(&p
, end
, maplen
, bad
);
1057 dout("skipping non-latest full map %u len %d\n",
1059 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1060 dout("skipping full map %u len %d, "
1061 "older than our %u\n", epoch
, maplen
,
1062 osdc
->osdmap
->epoch
);
1064 dout("taking full map %u len %d\n", epoch
, maplen
);
1065 newmap
= osdmap_decode(&p
, p
+maplen
);
1066 if (IS_ERR(newmap
)) {
1067 err
= PTR_ERR(newmap
);
1071 oldmap
= osdc
->osdmap
;
1072 osdc
->osdmap
= newmap
;
1074 ceph_osdmap_destroy(oldmap
);
1081 downgrade_write(&osdc
->map_sem
);
1082 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1084 kick_requests(osdc
, NULL
);
1085 up_read(&osdc
->map_sem
);
1086 wake_up_all(&osdc
->client
->auth_wq
);
1090 pr_err("osdc handle_map corrupt msg\n");
1092 up_write(&osdc
->map_sem
);
1097 * Register request, send initial attempt.
1099 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1100 struct ceph_osd_request
*req
,
1105 req
->r_request
->pages
= req
->r_pages
;
1106 req
->r_request
->nr_pages
= req
->r_num_pages
;
1108 register_request(osdc
, req
);
1110 down_read(&osdc
->map_sem
);
1111 mutex_lock(&osdc
->request_mutex
);
1113 * a racing kick_requests() may have sent the message for us
1114 * while we dropped request_mutex above, so only send now if
1115 * the request still han't been touched yet.
1117 if (req
->r_sent
== 0) {
1118 rc
= __send_request(osdc
, req
);
1121 dout("osdc_start_request failed send, "
1122 " marking %lld\n", req
->r_tid
);
1123 req
->r_resend
= true;
1126 __unregister_request(osdc
, req
);
1130 mutex_unlock(&osdc
->request_mutex
);
1131 up_read(&osdc
->map_sem
);
1136 * wait for a request to complete
1138 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1139 struct ceph_osd_request
*req
)
1143 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1145 mutex_lock(&osdc
->request_mutex
);
1146 __cancel_request(req
);
1147 __unregister_request(osdc
, req
);
1148 mutex_unlock(&osdc
->request_mutex
);
1149 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1153 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1154 return req
->r_result
;
1158 * sync - wait for all in-flight requests to flush. avoid starvation.
1160 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1162 struct ceph_osd_request
*req
;
1163 u64 last_tid
, next_tid
= 0;
1165 mutex_lock(&osdc
->request_mutex
);
1166 last_tid
= osdc
->last_tid
;
1168 req
= __lookup_request_ge(osdc
, next_tid
);
1171 if (req
->r_tid
> last_tid
)
1174 next_tid
= req
->r_tid
+ 1;
1175 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1178 ceph_osdc_get_request(req
);
1179 mutex_unlock(&osdc
->request_mutex
);
1180 dout("sync waiting on tid %llu (last is %llu)\n",
1181 req
->r_tid
, last_tid
);
1182 wait_for_completion(&req
->r_safe_completion
);
1183 mutex_lock(&osdc
->request_mutex
);
1184 ceph_osdc_put_request(req
);
1186 mutex_unlock(&osdc
->request_mutex
);
1187 dout("sync done (thru tid %llu)\n", last_tid
);
1193 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
1198 osdc
->client
= client
;
1199 osdc
->osdmap
= NULL
;
1200 init_rwsem(&osdc
->map_sem
);
1201 init_completion(&osdc
->map_waiters
);
1202 osdc
->last_requested_map
= 0;
1203 mutex_init(&osdc
->request_mutex
);
1205 osdc
->osds
= RB_ROOT
;
1206 INIT_LIST_HEAD(&osdc
->osd_lru
);
1207 osdc
->requests
= RB_ROOT
;
1208 INIT_LIST_HEAD(&osdc
->req_lru
);
1209 osdc
->num_requests
= 0;
1210 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
1211 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
1213 schedule_delayed_work(&osdc
->osds_timeout_work
,
1214 round_jiffies_relative(osdc
->client
->mount_args
->osd_idle_ttl
* HZ
));
1217 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
1218 sizeof(struct ceph_osd_request
));
1219 if (!osdc
->req_mempool
)
1222 err
= ceph_msgpool_init(&osdc
->msgpool_op
, OSD_OP_FRONT_LEN
, 10, true,
1226 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
,
1227 OSD_OPREPLY_FRONT_LEN
, 10, true,
1234 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1236 mempool_destroy(osdc
->req_mempool
);
1241 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
1243 cancel_delayed_work_sync(&osdc
->timeout_work
);
1244 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
1246 ceph_osdmap_destroy(osdc
->osdmap
);
1247 osdc
->osdmap
= NULL
;
1249 remove_old_osds(osdc
, 1);
1250 mempool_destroy(osdc
->req_mempool
);
1251 ceph_msgpool_destroy(&osdc
->msgpool_op
);
1252 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
1256 * Read some contiguous pages. If we cross a stripe boundary, shorten
1257 * *plen. Return number of bytes read, or error.
1259 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
1260 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
1262 u32 truncate_seq
, u64 truncate_size
,
1263 struct page
**pages
, int num_pages
)
1265 struct ceph_osd_request
*req
;
1268 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
1269 vino
.snap
, off
, *plen
);
1270 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
1271 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
1272 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
1277 /* it may be a short read due to an object boundary */
1278 req
->r_pages
= pages
;
1280 dout("readpages final extent is %llu~%llu (%d pages)\n",
1281 off
, *plen
, req
->r_num_pages
);
1283 rc
= ceph_osdc_start_request(osdc
, req
, false);
1285 rc
= ceph_osdc_wait_request(osdc
, req
);
1287 ceph_osdc_put_request(req
);
1288 dout("readpages result %d\n", rc
);
1293 * do a synchronous write on N pages
1295 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
1296 struct ceph_file_layout
*layout
,
1297 struct ceph_snap_context
*snapc
,
1299 u32 truncate_seq
, u64 truncate_size
,
1300 struct timespec
*mtime
,
1301 struct page
**pages
, int num_pages
,
1302 int flags
, int do_sync
, bool nofail
)
1304 struct ceph_osd_request
*req
;
1307 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
1308 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
1310 flags
| CEPH_OSD_FLAG_ONDISK
|
1311 CEPH_OSD_FLAG_WRITE
,
1313 truncate_seq
, truncate_size
, mtime
,
1318 /* it may be a short write due to an object boundary */
1319 req
->r_pages
= pages
;
1320 dout("writepages %llu~%llu (%d pages)\n", off
, len
,
1323 rc
= ceph_osdc_start_request(osdc
, req
, nofail
);
1325 rc
= ceph_osdc_wait_request(osdc
, req
);
1327 ceph_osdc_put_request(req
);
1330 dout("writepages result %d\n", rc
);
1335 * handle incoming message
1337 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
1339 struct ceph_osd
*osd
= con
->private;
1340 struct ceph_osd_client
*osdc
;
1341 int type
= le16_to_cpu(msg
->hdr
.type
);
1348 case CEPH_MSG_OSD_MAP
:
1349 ceph_osdc_handle_map(osdc
, msg
);
1351 case CEPH_MSG_OSD_OPREPLY
:
1352 handle_reply(osdc
, msg
, con
);
1356 pr_err("received unknown message type %d %s\n", type
,
1357 ceph_msg_type_name(type
));
1364 * lookup and return message for incoming reply. set up reply message
1367 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
1368 struct ceph_msg_header
*hdr
,
1371 struct ceph_osd
*osd
= con
->private;
1372 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
1374 struct ceph_osd_request
*req
;
1375 int front
= le32_to_cpu(hdr
->front_len
);
1376 int data_len
= le32_to_cpu(hdr
->data_len
);
1379 tid
= le64_to_cpu(hdr
->tid
);
1380 mutex_lock(&osdc
->request_mutex
);
1381 req
= __lookup_request(osdc
, tid
);
1385 pr_info("get_reply unknown tid %llu from osd%d\n", tid
,
1390 if (req
->r_con_filling_msg
) {
1391 dout("get_reply revoking msg %p from old con %p\n",
1392 req
->r_reply
, req
->r_con_filling_msg
);
1393 ceph_con_revoke_message(req
->r_con_filling_msg
, req
->r_reply
);
1394 ceph_con_put(req
->r_con_filling_msg
);
1395 req
->r_con_filling_msg
= NULL
;
1398 if (front
> req
->r_reply
->front
.iov_len
) {
1399 pr_warning("get_reply front %d > preallocated %d\n",
1400 front
, (int)req
->r_reply
->front
.iov_len
);
1401 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
);
1404 ceph_msg_put(req
->r_reply
);
1407 m
= ceph_msg_get(req
->r_reply
);
1410 unsigned data_off
= le16_to_cpu(hdr
->data_off
);
1411 int want
= calc_pages_for(data_off
& ~PAGE_MASK
, data_len
);
1413 if (unlikely(req
->r_num_pages
< want
)) {
1414 pr_warning("tid %lld reply %d > expected %d pages\n",
1415 tid
, want
, m
->nr_pages
);
1421 m
->pages
= req
->r_pages
;
1422 m
->nr_pages
= req
->r_num_pages
;
1425 req
->r_con_filling_msg
= ceph_con_get(con
);
1426 dout("get_reply tid %lld %p\n", tid
, m
);
1429 mutex_unlock(&osdc
->request_mutex
);
1434 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
1435 struct ceph_msg_header
*hdr
,
1438 struct ceph_osd
*osd
= con
->private;
1439 int type
= le16_to_cpu(hdr
->type
);
1440 int front
= le32_to_cpu(hdr
->front_len
);
1443 case CEPH_MSG_OSD_MAP
:
1444 return ceph_msg_new(type
, front
, GFP_NOFS
);
1445 case CEPH_MSG_OSD_OPREPLY
:
1446 return get_reply(con
, hdr
, skip
);
1448 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
1456 * Wrappers to refcount containing ceph_osd struct
1458 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
1460 struct ceph_osd
*osd
= con
->private;
1466 static void put_osd_con(struct ceph_connection
*con
)
1468 struct ceph_osd
*osd
= con
->private;
1475 static int get_authorizer(struct ceph_connection
*con
,
1476 void **buf
, int *len
, int *proto
,
1477 void **reply_buf
, int *reply_len
, int force_new
)
1479 struct ceph_osd
*o
= con
->private;
1480 struct ceph_osd_client
*osdc
= o
->o_osdc
;
1481 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
1484 if (force_new
&& o
->o_authorizer
) {
1485 ac
->ops
->destroy_authorizer(ac
, o
->o_authorizer
);
1486 o
->o_authorizer
= NULL
;
1488 if (o
->o_authorizer
== NULL
) {
1489 ret
= ac
->ops
->create_authorizer(
1490 ac
, CEPH_ENTITY_TYPE_OSD
,
1492 &o
->o_authorizer_buf
,
1493 &o
->o_authorizer_buf_len
,
1494 &o
->o_authorizer_reply_buf
,
1495 &o
->o_authorizer_reply_buf_len
);
1500 *proto
= ac
->protocol
;
1501 *buf
= o
->o_authorizer_buf
;
1502 *len
= o
->o_authorizer_buf_len
;
1503 *reply_buf
= o
->o_authorizer_reply_buf
;
1504 *reply_len
= o
->o_authorizer_reply_buf_len
;
1509 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
1511 struct ceph_osd
*o
= con
->private;
1512 struct ceph_osd_client
*osdc
= o
->o_osdc
;
1513 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
1515 return ac
->ops
->verify_authorizer_reply(ac
, o
->o_authorizer
, len
);
1518 static int invalidate_authorizer(struct ceph_connection
*con
)
1520 struct ceph_osd
*o
= con
->private;
1521 struct ceph_osd_client
*osdc
= o
->o_osdc
;
1522 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
1524 if (ac
->ops
->invalidate_authorizer
)
1525 ac
->ops
->invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
1527 return ceph_monc_validate_auth(&osdc
->client
->monc
);
1530 static const struct ceph_connection_operations osd_con_ops
= {
1533 .dispatch
= dispatch
,
1534 .get_authorizer
= get_authorizer
,
1535 .verify_authorizer_reply
= verify_authorizer_reply
,
1536 .invalidate_authorizer
= invalidate_authorizer
,
1537 .alloc_msg
= alloc_msg
,