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
);
36 * Implement client access to distributed object storage cluster.
38 * All data objects are stored within a cluster/cloud of OSDs, or
39 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
40 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
41 * remote daemons serving up and coordinating consistent and safe
44 * Cluster membership and the mapping of data objects onto storage devices
45 * are described by the osd map.
47 * We keep track of pending OSD requests (read, write), resubmit
48 * requests to different OSDs when the cluster topology/data layout
49 * change, or retry the affected requests when the communications
50 * channel with an OSD is reset.
54 * calculate the mapping of a file extent onto an object, and fill out the
55 * request accordingly. shorten extent as necessary if it crosses an
58 * fill osd op in request message.
60 static int calc_layout(struct ceph_file_layout
*layout
, u64 off
, u64
*plen
,
61 u64
*objnum
, u64
*objoff
, u64
*objlen
)
67 r
= ceph_calc_file_object_mapping(layout
, off
, orig_len
, objnum
,
71 if (*objlen
< orig_len
) {
73 dout(" skipping last %llu, final file extent %llu~%llu\n",
74 orig_len
- *plen
, off
, *plen
);
77 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum
, *objoff
, *objlen
);
82 static void ceph_osd_data_init(struct ceph_osd_data
*osd_data
)
84 memset(osd_data
, 0, sizeof (*osd_data
));
85 osd_data
->type
= CEPH_OSD_DATA_TYPE_NONE
;
88 void ceph_osd_data_pages_init(struct ceph_osd_data
*osd_data
,
89 struct page
**pages
, u64 length
, u32 alignment
,
90 bool pages_from_pool
, bool own_pages
)
92 osd_data
->type
= CEPH_OSD_DATA_TYPE_PAGES
;
93 osd_data
->pages
= pages
;
94 osd_data
->length
= length
;
95 osd_data
->alignment
= alignment
;
96 osd_data
->pages_from_pool
= pages_from_pool
;
97 osd_data
->own_pages
= own_pages
;
99 EXPORT_SYMBOL(ceph_osd_data_pages_init
);
101 void ceph_osd_data_pagelist_init(struct ceph_osd_data
*osd_data
,
102 struct ceph_pagelist
*pagelist
)
104 osd_data
->type
= CEPH_OSD_DATA_TYPE_PAGELIST
;
105 osd_data
->pagelist
= pagelist
;
107 EXPORT_SYMBOL(ceph_osd_data_pagelist_init
);
110 void ceph_osd_data_bio_init(struct ceph_osd_data
*osd_data
,
111 struct bio
*bio
, size_t bio_length
)
113 osd_data
->type
= CEPH_OSD_DATA_TYPE_BIO
;
115 osd_data
->bio_length
= bio_length
;
117 EXPORT_SYMBOL(ceph_osd_data_bio_init
);
118 #endif /* CONFIG_BLOCK */
120 static u64
ceph_osd_data_length(struct ceph_osd_data
*osd_data
)
122 switch (osd_data
->type
) {
123 case CEPH_OSD_DATA_TYPE_NONE
:
125 case CEPH_OSD_DATA_TYPE_PAGES
:
126 return osd_data
->length
;
127 case CEPH_OSD_DATA_TYPE_PAGELIST
:
128 return (u64
)osd_data
->pagelist
->length
;
130 case CEPH_OSD_DATA_TYPE_BIO
:
131 return (u64
)osd_data
->bio_length
;
132 #endif /* CONFIG_BLOCK */
134 WARN(true, "unrecognized data type %d\n", (int)osd_data
->type
);
139 static void ceph_osd_data_release(struct ceph_osd_data
*osd_data
)
141 if (osd_data
->type
!= CEPH_OSD_DATA_TYPE_PAGES
)
144 if (osd_data
->own_pages
) {
147 num_pages
= calc_pages_for((u64
)osd_data
->alignment
,
148 (u64
)osd_data
->length
);
149 ceph_release_page_vector(osd_data
->pages
, num_pages
);
156 void ceph_osdc_release_request(struct kref
*kref
)
158 struct ceph_osd_request
*req
;
160 req
= container_of(kref
, struct ceph_osd_request
, r_kref
);
162 ceph_msg_put(req
->r_request
);
164 ceph_msg_revoke_incoming(req
->r_reply
);
165 ceph_msg_put(req
->r_reply
);
168 ceph_osd_data_release(&req
->r_data_in
);
169 ceph_osd_data_release(&req
->r_data_out
);
171 ceph_put_snap_context(req
->r_snapc
);
173 mempool_free(req
, req
->r_osdc
->req_mempool
);
177 EXPORT_SYMBOL(ceph_osdc_release_request
);
179 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
180 struct ceph_snap_context
*snapc
,
181 unsigned int num_ops
,
185 struct ceph_osd_request
*req
;
186 struct ceph_msg
*msg
;
189 BUILD_BUG_ON(CEPH_OSD_MAX_OP
> U16_MAX
);
190 BUG_ON(num_ops
> CEPH_OSD_MAX_OP
);
192 msg_size
= 4 + 4 + 8 + 8 + 4+8;
193 msg_size
+= 2 + 4 + 8 + 4 + 4; /* oloc */
194 msg_size
+= 1 + 8 + 4 + 4; /* pg_t */
195 msg_size
+= 4 + MAX_OBJ_NAME_SIZE
;
196 msg_size
+= 2 + num_ops
*sizeof(struct ceph_osd_op
);
197 msg_size
+= 8; /* snapid */
198 msg_size
+= 8; /* snap_seq */
199 msg_size
+= 8 * (snapc
? snapc
->num_snaps
: 0); /* snaps */
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
;
213 req
->r_num_ops
= num_ops
;
215 kref_init(&req
->r_kref
);
216 init_completion(&req
->r_completion
);
217 init_completion(&req
->r_safe_completion
);
218 RB_CLEAR_NODE(&req
->r_node
);
219 INIT_LIST_HEAD(&req
->r_unsafe_item
);
220 INIT_LIST_HEAD(&req
->r_linger_item
);
221 INIT_LIST_HEAD(&req
->r_linger_osd
);
222 INIT_LIST_HEAD(&req
->r_req_lru_item
);
223 INIT_LIST_HEAD(&req
->r_osd_item
);
225 /* create reply message */
227 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
229 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
230 OSD_OPREPLY_FRONT_LEN
, gfp_flags
, true);
232 ceph_osdc_put_request(req
);
237 ceph_osd_data_init(&req
->r_data_in
);
238 ceph_osd_data_init(&req
->r_data_out
);
240 /* create request message; allow space for oid */
242 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
244 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp_flags
, true);
246 ceph_osdc_put_request(req
);
250 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
252 req
->r_request
= msg
;
256 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
258 static bool osd_req_opcode_valid(u16 opcode
)
261 case CEPH_OSD_OP_READ
:
262 case CEPH_OSD_OP_STAT
:
263 case CEPH_OSD_OP_MAPEXT
:
264 case CEPH_OSD_OP_MASKTRUNC
:
265 case CEPH_OSD_OP_SPARSE_READ
:
266 case CEPH_OSD_OP_NOTIFY
:
267 case CEPH_OSD_OP_NOTIFY_ACK
:
268 case CEPH_OSD_OP_ASSERT_VER
:
269 case CEPH_OSD_OP_WRITE
:
270 case CEPH_OSD_OP_WRITEFULL
:
271 case CEPH_OSD_OP_TRUNCATE
:
272 case CEPH_OSD_OP_ZERO
:
273 case CEPH_OSD_OP_DELETE
:
274 case CEPH_OSD_OP_APPEND
:
275 case CEPH_OSD_OP_STARTSYNC
:
276 case CEPH_OSD_OP_SETTRUNC
:
277 case CEPH_OSD_OP_TRIMTRUNC
:
278 case CEPH_OSD_OP_TMAPUP
:
279 case CEPH_OSD_OP_TMAPPUT
:
280 case CEPH_OSD_OP_TMAPGET
:
281 case CEPH_OSD_OP_CREATE
:
282 case CEPH_OSD_OP_ROLLBACK
:
283 case CEPH_OSD_OP_WATCH
:
284 case CEPH_OSD_OP_OMAPGETKEYS
:
285 case CEPH_OSD_OP_OMAPGETVALS
:
286 case CEPH_OSD_OP_OMAPGETHEADER
:
287 case CEPH_OSD_OP_OMAPGETVALSBYKEYS
:
288 case CEPH_OSD_OP_OMAPSETVALS
:
289 case CEPH_OSD_OP_OMAPSETHEADER
:
290 case CEPH_OSD_OP_OMAPCLEAR
:
291 case CEPH_OSD_OP_OMAPRMKEYS
:
292 case CEPH_OSD_OP_OMAP_CMP
:
293 case CEPH_OSD_OP_CLONERANGE
:
294 case CEPH_OSD_OP_ASSERT_SRC_VERSION
:
295 case CEPH_OSD_OP_SRC_CMPXATTR
:
296 case CEPH_OSD_OP_GETXATTR
:
297 case CEPH_OSD_OP_GETXATTRS
:
298 case CEPH_OSD_OP_CMPXATTR
:
299 case CEPH_OSD_OP_SETXATTR
:
300 case CEPH_OSD_OP_SETXATTRS
:
301 case CEPH_OSD_OP_RESETXATTRS
:
302 case CEPH_OSD_OP_RMXATTR
:
303 case CEPH_OSD_OP_PULL
:
304 case CEPH_OSD_OP_PUSH
:
305 case CEPH_OSD_OP_BALANCEREADS
:
306 case CEPH_OSD_OP_UNBALANCEREADS
:
307 case CEPH_OSD_OP_SCRUB
:
308 case CEPH_OSD_OP_SCRUB_RESERVE
:
309 case CEPH_OSD_OP_SCRUB_UNRESERVE
:
310 case CEPH_OSD_OP_SCRUB_STOP
:
311 case CEPH_OSD_OP_SCRUB_MAP
:
312 case CEPH_OSD_OP_WRLOCK
:
313 case CEPH_OSD_OP_WRUNLOCK
:
314 case CEPH_OSD_OP_RDLOCK
:
315 case CEPH_OSD_OP_RDUNLOCK
:
316 case CEPH_OSD_OP_UPLOCK
:
317 case CEPH_OSD_OP_DNLOCK
:
318 case CEPH_OSD_OP_CALL
:
319 case CEPH_OSD_OP_PGLS
:
320 case CEPH_OSD_OP_PGLS_FILTER
:
328 * This is an osd op init function for opcodes that have no data or
329 * other information associated with them. It also serves as a
330 * common init routine for all the other init functions, below.
332 static struct ceph_osd_req_op
*
333 osd_req_op_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
336 struct ceph_osd_req_op
*op
;
338 BUG_ON(which
>= osd_req
->r_num_ops
);
339 BUG_ON(!osd_req_opcode_valid(opcode
));
341 op
= &osd_req
->r_ops
[which
];
342 memset(op
, 0, sizeof (*op
));
348 void osd_req_op_extent_init(struct ceph_osd_request
*osd_req
,
349 unsigned int which
, u16 opcode
,
350 u64 offset
, u64 length
,
351 u64 truncate_size
, u32 truncate_seq
)
353 struct ceph_osd_req_op
*op
= osd_req_op_init(osd_req
, which
, opcode
);
354 size_t payload_len
= 0;
356 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
);
358 op
->extent
.offset
= offset
;
359 op
->extent
.length
= length
;
360 op
->extent
.truncate_size
= truncate_size
;
361 op
->extent
.truncate_seq
= truncate_seq
;
362 if (opcode
== CEPH_OSD_OP_WRITE
)
363 payload_len
+= length
;
365 op
->payload_len
= payload_len
;
367 EXPORT_SYMBOL(osd_req_op_extent_init
);
369 void osd_req_op_extent_update(struct ceph_osd_request
*osd_req
,
370 unsigned int which
, u64 length
)
372 struct ceph_osd_req_op
*op
;
375 BUG_ON(which
>= osd_req
->r_num_ops
);
376 op
= &osd_req
->r_ops
[which
];
377 previous
= op
->extent
.length
;
379 if (length
== previous
)
380 return; /* Nothing to do */
381 BUG_ON(length
> previous
);
383 op
->extent
.length
= length
;
384 op
->payload_len
-= previous
- length
;
386 EXPORT_SYMBOL(osd_req_op_extent_update
);
388 void osd_req_op_extent_osd_data(struct ceph_osd_request
*osd_req
,
390 struct ceph_osd_data
*osd_data
)
392 BUG_ON(which
>= osd_req
->r_num_ops
);
393 osd_req
->r_ops
[which
].extent
.osd_data
= osd_data
;
395 EXPORT_SYMBOL(osd_req_op_extent_osd_data
);
397 void osd_req_op_cls_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
398 u16 opcode
, const char *class, const char *method
,
399 const void *request_data
, size_t request_data_size
)
401 struct ceph_osd_req_op
*op
= osd_req_op_init(osd_req
, which
, opcode
);
402 size_t payload_len
= 0;
405 BUG_ON(opcode
!= CEPH_OSD_OP_CALL
);
407 op
->cls
.class_name
= class;
408 size
= strlen(class);
409 BUG_ON(size
> (size_t) U8_MAX
);
410 op
->cls
.class_len
= size
;
413 op
->cls
.method_name
= method
;
414 size
= strlen(method
);
415 BUG_ON(size
> (size_t) U8_MAX
);
416 op
->cls
.method_len
= size
;
419 op
->cls
.request_data
= request_data
;
420 BUG_ON(request_data_size
> (size_t) U32_MAX
);
421 op
->cls
.request_data_len
= (u32
) request_data_size
;
422 payload_len
+= request_data_size
;
424 op
->cls
.argc
= 0; /* currently unused */
426 op
->payload_len
= payload_len
;
428 EXPORT_SYMBOL(osd_req_op_cls_init
);
429 void osd_req_op_cls_response_data(struct ceph_osd_request
*osd_req
,
431 struct ceph_osd_data
*response_data
)
433 BUG_ON(which
>= osd_req
->r_num_ops
);
434 osd_req
->r_ops
[which
].cls
.response_data
= response_data
;
436 EXPORT_SYMBOL(osd_req_op_cls_response_data
);
438 void osd_req_op_watch_init(struct ceph_osd_request
*osd_req
,
439 unsigned int which
, u16 opcode
,
440 u64 cookie
, u64 version
, int flag
)
442 struct ceph_osd_req_op
*op
= osd_req_op_init(osd_req
, which
, opcode
);
444 BUG_ON(opcode
!= CEPH_OSD_OP_NOTIFY_ACK
&& opcode
!= CEPH_OSD_OP_WATCH
);
446 op
->watch
.cookie
= cookie
;
447 /* op->watch.ver = version; */ /* XXX 3847 */
448 op
->watch
.ver
= cpu_to_le64(version
);
449 if (opcode
== CEPH_OSD_OP_WATCH
&& flag
)
450 op
->watch
.flag
= (u8
)1;
452 EXPORT_SYMBOL(osd_req_op_watch_init
);
454 static u64
osd_req_encode_op(struct ceph_osd_request
*req
,
455 struct ceph_osd_op
*dst
, unsigned int which
)
457 struct ceph_osd_req_op
*src
;
458 u64 request_data_len
= 0;
459 struct ceph_pagelist
*pagelist
;
461 BUG_ON(which
>= req
->r_num_ops
);
462 src
= &req
->r_ops
[which
];
463 if (WARN_ON(!osd_req_opcode_valid(src
->op
))) {
464 pr_err("unrecognized osd opcode %d\n", src
->op
);
470 case CEPH_OSD_OP_STAT
:
472 case CEPH_OSD_OP_READ
:
473 case CEPH_OSD_OP_WRITE
:
474 if (src
->op
== CEPH_OSD_OP_WRITE
)
475 request_data_len
= src
->extent
.length
;
476 dst
->extent
.offset
= cpu_to_le64(src
->extent
.offset
);
477 dst
->extent
.length
= cpu_to_le64(src
->extent
.length
);
478 dst
->extent
.truncate_size
=
479 cpu_to_le64(src
->extent
.truncate_size
);
480 dst
->extent
.truncate_seq
=
481 cpu_to_le32(src
->extent
.truncate_seq
);
482 if (src
->op
== CEPH_OSD_OP_WRITE
)
483 WARN_ON(src
->extent
.osd_data
!= &req
->r_data_out
);
485 WARN_ON(src
->extent
.osd_data
!= &req
->r_data_in
);
487 case CEPH_OSD_OP_CALL
:
488 pagelist
= kmalloc(sizeof (*pagelist
), GFP_NOFS
);
490 ceph_pagelist_init(pagelist
);
492 dst
->cls
.class_len
= src
->cls
.class_len
;
493 dst
->cls
.method_len
= src
->cls
.method_len
;
494 dst
->cls
.indata_len
= cpu_to_le32(src
->cls
.request_data_len
);
495 ceph_pagelist_append(pagelist
, src
->cls
.class_name
,
497 ceph_pagelist_append(pagelist
, src
->cls
.method_name
,
498 src
->cls
.method_len
);
499 ceph_pagelist_append(pagelist
, src
->cls
.request_data
,
500 src
->cls
.request_data_len
);
501 ceph_osd_data_pagelist_init(&req
->r_data_out
, pagelist
);
503 WARN_ON(src
->cls
.response_data
!= &req
->r_data_in
);
504 request_data_len
= pagelist
->length
;
506 case CEPH_OSD_OP_STARTSYNC
:
508 case CEPH_OSD_OP_NOTIFY_ACK
:
509 case CEPH_OSD_OP_WATCH
:
510 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
511 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
512 dst
->watch
.flag
= src
->watch
.flag
;
515 pr_err("unsupported osd opcode %s\n",
516 ceph_osd_op_name(src
->op
));
521 dst
->op
= cpu_to_le16(src
->op
);
522 dst
->payload_len
= cpu_to_le32(src
->payload_len
);
524 return request_data_len
;
528 * build new request AND message
531 void ceph_osdc_build_request(struct ceph_osd_request
*req
, u64 off
,
532 struct ceph_snap_context
*snapc
, u64 snap_id
,
533 struct timespec
*mtime
)
535 struct ceph_msg
*msg
= req
->r_request
;
538 int flags
= req
->r_flags
;
542 req
->r_snapid
= snap_id
;
543 req
->r_snapc
= ceph_get_snap_context(snapc
);
546 msg
->hdr
.version
= cpu_to_le16(4);
548 p
= msg
->front
.iov_base
;
549 ceph_encode_32(&p
, 1); /* client_inc is always 1 */
550 req
->r_request_osdmap_epoch
= p
;
552 req
->r_request_flags
= p
;
554 if (req
->r_flags
& CEPH_OSD_FLAG_WRITE
)
555 ceph_encode_timespec(p
, mtime
);
556 p
+= sizeof(struct ceph_timespec
);
557 req
->r_request_reassert_version
= p
;
558 p
+= sizeof(struct ceph_eversion
); /* will get filled in */
561 ceph_encode_8(&p
, 4);
562 ceph_encode_8(&p
, 4);
563 ceph_encode_32(&p
, 8 + 4 + 4);
564 req
->r_request_pool
= p
;
566 ceph_encode_32(&p
, -1); /* preferred */
567 ceph_encode_32(&p
, 0); /* key len */
569 ceph_encode_8(&p
, 1);
570 req
->r_request_pgid
= p
;
572 ceph_encode_32(&p
, -1); /* preferred */
575 ceph_encode_32(&p
, req
->r_oid_len
);
576 memcpy(p
, req
->r_oid
, req
->r_oid_len
);
577 dout("oid '%.*s' len %d\n", req
->r_oid_len
, req
->r_oid
, req
->r_oid_len
);
580 /* ops--can imply data */
581 ceph_encode_16(&p
, (u16
)req
->r_num_ops
);
583 for (i
= 0; i
< req
->r_num_ops
; i
++) {
584 data_len
+= osd_req_encode_op(req
, p
, i
);
585 p
+= sizeof(struct ceph_osd_op
);
589 ceph_encode_64(&p
, req
->r_snapid
);
590 ceph_encode_64(&p
, req
->r_snapc
? req
->r_snapc
->seq
: 0);
591 ceph_encode_32(&p
, req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
593 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
594 ceph_encode_64(&p
, req
->r_snapc
->snaps
[i
]);
598 req
->r_request_attempts
= p
;
602 if (flags
& CEPH_OSD_FLAG_WRITE
) {
606 * The header "data_off" is a hint to the receiver
607 * allowing it to align received data into its
608 * buffers such that there's no need to re-copy
609 * it before writing it to disk (direct I/O).
611 data_off
= (u16
) (off
& 0xffff);
612 req
->r_request
->hdr
.data_off
= cpu_to_le16(data_off
);
614 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
616 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
617 msg_size
= p
- msg
->front
.iov_base
;
618 msg
->front
.iov_len
= msg_size
;
619 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
621 dout("build_request msg_size was %d\n", (int)msg_size
);
623 EXPORT_SYMBOL(ceph_osdc_build_request
);
626 * build new request AND message, calculate layout, and adjust file
629 * if the file was recently truncated, we include information about its
630 * old and new size so that the object can be updated appropriately. (we
631 * avoid synchronously deleting truncated objects because it's slow.)
633 * if @do_sync, include a 'startsync' command so that the osd will flush
636 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
637 struct ceph_file_layout
*layout
,
638 struct ceph_vino vino
,
639 u64 off
, u64
*plen
, int num_ops
,
640 int opcode
, int flags
,
641 struct ceph_snap_context
*snapc
,
646 struct ceph_osd_request
*req
;
647 struct ceph_osd_data
*osd_data
;
655 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
);
657 req
= ceph_osdc_alloc_request(osdc
, snapc
, num_ops
, use_mempool
,
660 return ERR_PTR(-ENOMEM
);
661 osd_data
= opcode
== CEPH_OSD_OP_WRITE
? &req
->r_data_out
664 req
->r_flags
= flags
;
666 /* calculate max write size */
667 r
= calc_layout(layout
, off
, plen
, &objnum
, &objoff
, &objlen
);
669 ceph_osdc_put_request(req
);
673 object_size
= le32_to_cpu(layout
->fl_object_size
);
674 object_base
= off
- objoff
;
675 if (truncate_size
<= object_base
) {
678 truncate_size
-= object_base
;
679 if (truncate_size
> object_size
)
680 truncate_size
= object_size
;
683 osd_req_op_extent_init(req
, 0, opcode
, objoff
, objlen
,
684 truncate_size
, truncate_seq
);
685 osd_req_op_extent_osd_data(req
, 0, osd_data
);
688 * A second op in the ops array means the caller wants to
689 * also issue a include a 'startsync' command so that the
690 * osd will flush data quickly.
693 osd_req_op_init(req
, 1, CEPH_OSD_OP_STARTSYNC
);
695 req
->r_file_layout
= *layout
; /* keep a copy */
697 snprintf(req
->r_oid
, sizeof(req
->r_oid
), "%llx.%08llx",
699 req
->r_oid_len
= strlen(req
->r_oid
);
703 EXPORT_SYMBOL(ceph_osdc_new_request
);
706 * We keep osd requests in an rbtree, sorted by ->r_tid.
708 static void __insert_request(struct ceph_osd_client
*osdc
,
709 struct ceph_osd_request
*new)
711 struct rb_node
**p
= &osdc
->requests
.rb_node
;
712 struct rb_node
*parent
= NULL
;
713 struct ceph_osd_request
*req
= NULL
;
717 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
718 if (new->r_tid
< req
->r_tid
)
720 else if (new->r_tid
> req
->r_tid
)
726 rb_link_node(&new->r_node
, parent
, p
);
727 rb_insert_color(&new->r_node
, &osdc
->requests
);
730 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
733 struct ceph_osd_request
*req
;
734 struct rb_node
*n
= osdc
->requests
.rb_node
;
737 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
738 if (tid
< req
->r_tid
)
740 else if (tid
> req
->r_tid
)
748 static struct ceph_osd_request
*
749 __lookup_request_ge(struct ceph_osd_client
*osdc
,
752 struct ceph_osd_request
*req
;
753 struct rb_node
*n
= osdc
->requests
.rb_node
;
756 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
757 if (tid
< req
->r_tid
) {
761 } else if (tid
> req
->r_tid
) {
771 * Resubmit requests pending on the given osd.
773 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
774 struct ceph_osd
*osd
)
776 struct ceph_osd_request
*req
, *nreq
;
780 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
781 err
= __reset_osd(osdc
, osd
);
785 * Build up a list of requests to resend by traversing the
786 * osd's list of requests. Requests for a given object are
787 * sent in tid order, and that is also the order they're
788 * kept on this list. Therefore all requests that are in
789 * flight will be found first, followed by all requests that
790 * have not yet been sent. And to resend requests while
791 * preserving this order we will want to put any sent
792 * requests back on the front of the osd client's unsent
795 * So we build a separate ordered list of already-sent
796 * requests for the affected osd and splice it onto the
797 * front of the osd client's unsent list. Once we've seen a
798 * request that has not yet been sent we're done. Those
799 * requests are already sitting right where they belong.
801 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
804 list_move_tail(&req
->r_req_lru_item
, &resend
);
805 dout("requeueing %p tid %llu osd%d\n", req
, req
->r_tid
,
808 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
810 list_splice(&resend
, &osdc
->req_unsent
);
813 * Linger requests are re-registered before sending, which
814 * sets up a new tid for each. We add them to the unsent
815 * list at the end to keep things in tid order.
817 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
820 * reregister request prior to unregistering linger so
821 * that r_osd is preserved.
823 BUG_ON(!list_empty(&req
->r_req_lru_item
));
824 __register_request(osdc
, req
);
825 list_add_tail(&req
->r_req_lru_item
, &osdc
->req_unsent
);
826 list_add_tail(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
827 __unregister_linger_request(osdc
, req
);
828 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
834 * If the osd connection drops, we need to resubmit all requests.
836 static void osd_reset(struct ceph_connection
*con
)
838 struct ceph_osd
*osd
= con
->private;
839 struct ceph_osd_client
*osdc
;
843 dout("osd_reset osd%d\n", osd
->o_osd
);
845 down_read(&osdc
->map_sem
);
846 mutex_lock(&osdc
->request_mutex
);
847 __kick_osd_requests(osdc
, osd
);
849 mutex_unlock(&osdc
->request_mutex
);
850 up_read(&osdc
->map_sem
);
854 * Track open sessions with osds.
856 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
858 struct ceph_osd
*osd
;
860 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
864 atomic_set(&osd
->o_ref
, 1);
867 RB_CLEAR_NODE(&osd
->o_node
);
868 INIT_LIST_HEAD(&osd
->o_requests
);
869 INIT_LIST_HEAD(&osd
->o_linger_requests
);
870 INIT_LIST_HEAD(&osd
->o_osd_lru
);
871 osd
->o_incarnation
= 1;
873 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
875 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
879 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
881 if (atomic_inc_not_zero(&osd
->o_ref
)) {
882 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
883 atomic_read(&osd
->o_ref
));
886 dout("get_osd %p FAIL\n", osd
);
891 static void put_osd(struct ceph_osd
*osd
)
893 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
894 atomic_read(&osd
->o_ref
) - 1);
895 if (atomic_dec_and_test(&osd
->o_ref
) && osd
->o_auth
.authorizer
) {
896 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
898 ceph_auth_destroy_authorizer(ac
, osd
->o_auth
.authorizer
);
904 * remove an osd from our map
906 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
908 dout("__remove_osd %p\n", osd
);
909 BUG_ON(!list_empty(&osd
->o_requests
));
910 rb_erase(&osd
->o_node
, &osdc
->osds
);
911 list_del_init(&osd
->o_osd_lru
);
912 ceph_con_close(&osd
->o_con
);
916 static void remove_all_osds(struct ceph_osd_client
*osdc
)
918 dout("%s %p\n", __func__
, osdc
);
919 mutex_lock(&osdc
->request_mutex
);
920 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
921 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
922 struct ceph_osd
, o_node
);
923 __remove_osd(osdc
, osd
);
925 mutex_unlock(&osdc
->request_mutex
);
928 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
929 struct ceph_osd
*osd
)
931 dout("__move_osd_to_lru %p\n", osd
);
932 BUG_ON(!list_empty(&osd
->o_osd_lru
));
933 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
934 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
937 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
939 dout("__remove_osd_from_lru %p\n", osd
);
940 if (!list_empty(&osd
->o_osd_lru
))
941 list_del_init(&osd
->o_osd_lru
);
944 static void remove_old_osds(struct ceph_osd_client
*osdc
)
946 struct ceph_osd
*osd
, *nosd
;
948 dout("__remove_old_osds %p\n", osdc
);
949 mutex_lock(&osdc
->request_mutex
);
950 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
951 if (time_before(jiffies
, osd
->lru_ttl
))
953 __remove_osd(osdc
, osd
);
955 mutex_unlock(&osdc
->request_mutex
);
961 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
963 struct ceph_entity_addr
*peer_addr
;
965 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
966 if (list_empty(&osd
->o_requests
) &&
967 list_empty(&osd
->o_linger_requests
)) {
968 __remove_osd(osdc
, osd
);
973 peer_addr
= &osdc
->osdmap
->osd_addr
[osd
->o_osd
];
974 if (!memcmp(peer_addr
, &osd
->o_con
.peer_addr
, sizeof (*peer_addr
)) &&
975 !ceph_con_opened(&osd
->o_con
)) {
976 struct ceph_osd_request
*req
;
978 dout(" osd addr hasn't changed and connection never opened,"
979 " letting msgr retry");
980 /* touch each r_stamp for handle_timeout()'s benfit */
981 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
982 req
->r_stamp
= jiffies
;
987 ceph_con_close(&osd
->o_con
);
988 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
, peer_addr
);
989 osd
->o_incarnation
++;
994 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
996 struct rb_node
**p
= &osdc
->osds
.rb_node
;
997 struct rb_node
*parent
= NULL
;
998 struct ceph_osd
*osd
= NULL
;
1000 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
1003 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
1004 if (new->o_osd
< osd
->o_osd
)
1006 else if (new->o_osd
> osd
->o_osd
)
1007 p
= &(*p
)->rb_right
;
1012 rb_link_node(&new->o_node
, parent
, p
);
1013 rb_insert_color(&new->o_node
, &osdc
->osds
);
1016 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
1018 struct ceph_osd
*osd
;
1019 struct rb_node
*n
= osdc
->osds
.rb_node
;
1022 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
1025 else if (o
> osd
->o_osd
)
1033 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
1035 schedule_delayed_work(&osdc
->timeout_work
,
1036 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
1039 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
1041 cancel_delayed_work(&osdc
->timeout_work
);
1045 * Register request, assign tid. If this is the first request, set up
1046 * the timeout event.
1048 static void __register_request(struct ceph_osd_client
*osdc
,
1049 struct ceph_osd_request
*req
)
1051 req
->r_tid
= ++osdc
->last_tid
;
1052 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1053 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
1054 __insert_request(osdc
, req
);
1055 ceph_osdc_get_request(req
);
1056 osdc
->num_requests
++;
1057 if (osdc
->num_requests
== 1) {
1058 dout(" first request, scheduling timeout\n");
1059 __schedule_osd_timeout(osdc
);
1064 * called under osdc->request_mutex
1066 static void __unregister_request(struct ceph_osd_client
*osdc
,
1067 struct ceph_osd_request
*req
)
1069 if (RB_EMPTY_NODE(&req
->r_node
)) {
1070 dout("__unregister_request %p tid %lld not registered\n",
1075 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
1076 rb_erase(&req
->r_node
, &osdc
->requests
);
1077 osdc
->num_requests
--;
1080 /* make sure the original request isn't in flight. */
1081 ceph_msg_revoke(req
->r_request
);
1083 list_del_init(&req
->r_osd_item
);
1084 if (list_empty(&req
->r_osd
->o_requests
) &&
1085 list_empty(&req
->r_osd
->o_linger_requests
)) {
1086 dout("moving osd to %p lru\n", req
->r_osd
);
1087 __move_osd_to_lru(osdc
, req
->r_osd
);
1089 if (list_empty(&req
->r_linger_item
))
1093 list_del_init(&req
->r_req_lru_item
);
1094 ceph_osdc_put_request(req
);
1096 if (osdc
->num_requests
== 0) {
1097 dout(" no requests, canceling timeout\n");
1098 __cancel_osd_timeout(osdc
);
1103 * Cancel a previously queued request message
1105 static void __cancel_request(struct ceph_osd_request
*req
)
1107 if (req
->r_sent
&& req
->r_osd
) {
1108 ceph_msg_revoke(req
->r_request
);
1113 static void __register_linger_request(struct ceph_osd_client
*osdc
,
1114 struct ceph_osd_request
*req
)
1116 dout("__register_linger_request %p\n", req
);
1117 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
1119 list_add_tail(&req
->r_linger_osd
,
1120 &req
->r_osd
->o_linger_requests
);
1123 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
1124 struct ceph_osd_request
*req
)
1126 dout("__unregister_linger_request %p\n", req
);
1127 list_del_init(&req
->r_linger_item
);
1129 list_del_init(&req
->r_linger_osd
);
1131 if (list_empty(&req
->r_osd
->o_requests
) &&
1132 list_empty(&req
->r_osd
->o_linger_requests
)) {
1133 dout("moving osd to %p lru\n", req
->r_osd
);
1134 __move_osd_to_lru(osdc
, req
->r_osd
);
1136 if (list_empty(&req
->r_osd_item
))
1141 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
1142 struct ceph_osd_request
*req
)
1144 mutex_lock(&osdc
->request_mutex
);
1145 if (req
->r_linger
) {
1146 __unregister_linger_request(osdc
, req
);
1147 ceph_osdc_put_request(req
);
1149 mutex_unlock(&osdc
->request_mutex
);
1151 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
1153 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
1154 struct ceph_osd_request
*req
)
1156 if (!req
->r_linger
) {
1157 dout("set_request_linger %p\n", req
);
1160 * caller is now responsible for calling
1161 * unregister_linger_request
1163 ceph_osdc_get_request(req
);
1166 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
1169 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1170 * (as needed), and set the request r_osd appropriately. If there is
1171 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1172 * (unsent, homeless) or leave on in-flight lru.
1174 * Return 0 if unchanged, 1 if changed, or negative on error.
1176 * Caller should hold map_sem for read and request_mutex.
1178 static int __map_request(struct ceph_osd_client
*osdc
,
1179 struct ceph_osd_request
*req
, int force_resend
)
1181 struct ceph_pg pgid
;
1182 int acting
[CEPH_PG_MAX_SIZE
];
1183 int o
= -1, num
= 0;
1186 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
1187 err
= ceph_calc_ceph_pg(&pgid
, req
->r_oid
, osdc
->osdmap
,
1188 ceph_file_layout_pg_pool(req
->r_file_layout
));
1190 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1195 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
1201 if ((!force_resend
&&
1202 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
1203 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
1204 req
->r_num_pg_osds
== num
&&
1205 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
1206 (req
->r_osd
== NULL
&& o
== -1))
1207 return 0; /* no change */
1209 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1210 req
->r_tid
, pgid
.pool
, pgid
.seed
, o
,
1211 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1213 /* record full pg acting set */
1214 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
1215 req
->r_num_pg_osds
= num
;
1218 __cancel_request(req
);
1219 list_del_init(&req
->r_osd_item
);
1223 req
->r_osd
= __lookup_osd(osdc
, o
);
1224 if (!req
->r_osd
&& o
>= 0) {
1226 req
->r_osd
= create_osd(osdc
, o
);
1228 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1232 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
1233 __insert_osd(osdc
, req
->r_osd
);
1235 ceph_con_open(&req
->r_osd
->o_con
,
1236 CEPH_ENTITY_TYPE_OSD
, o
,
1237 &osdc
->osdmap
->osd_addr
[o
]);
1241 __remove_osd_from_lru(req
->r_osd
);
1242 list_add_tail(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1243 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1245 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1247 err
= 1; /* osd or pg changed */
1254 * caller should hold map_sem (for read) and request_mutex
1256 static void __send_request(struct ceph_osd_client
*osdc
,
1257 struct ceph_osd_request
*req
)
1261 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1262 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
,
1263 (unsigned long long)req
->r_pgid
.pool
, req
->r_pgid
.seed
);
1265 /* fill in message content that changes each time we send it */
1266 put_unaligned_le32(osdc
->osdmap
->epoch
, req
->r_request_osdmap_epoch
);
1267 put_unaligned_le32(req
->r_flags
, req
->r_request_flags
);
1268 put_unaligned_le64(req
->r_pgid
.pool
, req
->r_request_pool
);
1269 p
= req
->r_request_pgid
;
1270 ceph_encode_64(&p
, req
->r_pgid
.pool
);
1271 ceph_encode_32(&p
, req
->r_pgid
.seed
);
1272 put_unaligned_le64(1, req
->r_request_attempts
); /* FIXME */
1273 memcpy(req
->r_request_reassert_version
, &req
->r_reassert_version
,
1274 sizeof(req
->r_reassert_version
));
1276 req
->r_stamp
= jiffies
;
1277 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1279 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1280 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1281 req
->r_sent
= req
->r_osd
->o_incarnation
;
1285 * Send any requests in the queue (req_unsent).
1287 static void __send_queued(struct ceph_osd_client
*osdc
)
1289 struct ceph_osd_request
*req
, *tmp
;
1291 dout("__send_queued\n");
1292 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
)
1293 __send_request(osdc
, req
);
1297 * Timeout callback, called every N seconds when 1 or more osd
1298 * requests has been active for more than N seconds. When this
1299 * happens, we ping all OSDs with requests who have timed out to
1300 * ensure any communications channel reset is detected. Reset the
1301 * request timeouts another N seconds in the future as we go.
1302 * Reschedule the timeout event another N seconds in future (unless
1303 * there are no open requests).
1305 static void handle_timeout(struct work_struct
*work
)
1307 struct ceph_osd_client
*osdc
=
1308 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1309 struct ceph_osd_request
*req
;
1310 struct ceph_osd
*osd
;
1311 unsigned long keepalive
=
1312 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1313 struct list_head slow_osds
;
1315 down_read(&osdc
->map_sem
);
1317 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1319 mutex_lock(&osdc
->request_mutex
);
1322 * ping osds that are a bit slow. this ensures that if there
1323 * is a break in the TCP connection we will notice, and reopen
1324 * a connection with that osd (from the fault callback).
1326 INIT_LIST_HEAD(&slow_osds
);
1327 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1328 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1333 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1334 req
->r_tid
, osd
->o_osd
);
1335 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1337 while (!list_empty(&slow_osds
)) {
1338 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1340 list_del_init(&osd
->o_keepalive_item
);
1341 ceph_con_keepalive(&osd
->o_con
);
1344 __schedule_osd_timeout(osdc
);
1345 __send_queued(osdc
);
1346 mutex_unlock(&osdc
->request_mutex
);
1347 up_read(&osdc
->map_sem
);
1350 static void handle_osds_timeout(struct work_struct
*work
)
1352 struct ceph_osd_client
*osdc
=
1353 container_of(work
, struct ceph_osd_client
,
1354 osds_timeout_work
.work
);
1355 unsigned long delay
=
1356 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1358 dout("osds timeout\n");
1359 down_read(&osdc
->map_sem
);
1360 remove_old_osds(osdc
);
1361 up_read(&osdc
->map_sem
);
1363 schedule_delayed_work(&osdc
->osds_timeout_work
,
1364 round_jiffies_relative(delay
));
1367 static void complete_request(struct ceph_osd_request
*req
)
1369 if (req
->r_safe_callback
)
1370 req
->r_safe_callback(req
, NULL
);
1371 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1375 * handle osd op reply. either call the callback if it is specified,
1376 * or do the completion to wake up the waiting thread.
1378 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1379 struct ceph_connection
*con
)
1382 struct ceph_osd_request
*req
;
1385 unsigned int numops
;
1386 int payload_len
, flags
;
1392 u64 reassert_version
;
1394 int already_completed
;
1398 tid
= le64_to_cpu(msg
->hdr
.tid
);
1399 dout("handle_reply %p tid %llu\n", msg
, tid
);
1401 p
= msg
->front
.iov_base
;
1402 end
= p
+ msg
->front
.iov_len
;
1404 ceph_decode_need(&p
, end
, 4, bad
);
1405 object_len
= ceph_decode_32(&p
);
1406 ceph_decode_need(&p
, end
, object_len
, bad
);
1409 err
= ceph_decode_pgid(&p
, end
, &pg
);
1413 ceph_decode_need(&p
, end
, 8 + 4 + 4 + 8 + 4, bad
);
1414 flags
= ceph_decode_64(&p
);
1415 result
= ceph_decode_32(&p
);
1416 reassert_epoch
= ceph_decode_32(&p
);
1417 reassert_version
= ceph_decode_64(&p
);
1418 osdmap_epoch
= ceph_decode_32(&p
);
1421 mutex_lock(&osdc
->request_mutex
);
1422 req
= __lookup_request(osdc
, tid
);
1424 dout("handle_reply tid %llu dne\n", tid
);
1427 ceph_osdc_get_request(req
);
1429 dout("handle_reply %p tid %llu req %p result %d\n", msg
, tid
,
1432 ceph_decode_need(&p
, end
, 4, bad
);
1433 numops
= ceph_decode_32(&p
);
1434 if (numops
> CEPH_OSD_MAX_OP
)
1436 if (numops
!= req
->r_num_ops
)
1439 ceph_decode_need(&p
, end
, numops
* sizeof(struct ceph_osd_op
), bad
);
1440 for (i
= 0; i
< numops
; i
++) {
1441 struct ceph_osd_op
*op
= p
;
1444 len
= le32_to_cpu(op
->payload_len
);
1445 req
->r_reply_op_len
[i
] = len
;
1446 dout(" op %d has %d bytes\n", i
, len
);
1450 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1451 if (payload_len
!= bytes
) {
1452 pr_warning("sum of op payload lens %d != data_len %d",
1453 payload_len
, bytes
);
1457 ceph_decode_need(&p
, end
, 4 + numops
* 4, bad
);
1458 retry_attempt
= ceph_decode_32(&p
);
1459 for (i
= 0; i
< numops
; i
++)
1460 req
->r_reply_op_result
[i
] = ceph_decode_32(&p
);
1462 if (!req
->r_got_reply
) {
1464 req
->r_result
= result
;
1465 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1467 if (req
->r_result
== 0)
1468 req
->r_result
= bytes
;
1470 /* in case this is a write and we need to replay, */
1471 req
->r_reassert_version
.epoch
= cpu_to_le32(reassert_epoch
);
1472 req
->r_reassert_version
.version
= cpu_to_le64(reassert_version
);
1474 req
->r_got_reply
= 1;
1475 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1476 dout("handle_reply tid %llu dup ack\n", tid
);
1477 mutex_unlock(&osdc
->request_mutex
);
1481 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1483 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1484 __register_linger_request(osdc
, req
);
1486 /* either this is a read, or we got the safe response */
1488 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1489 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1490 __unregister_request(osdc
, req
);
1492 already_completed
= req
->r_completed
;
1493 req
->r_completed
= 1;
1494 mutex_unlock(&osdc
->request_mutex
);
1495 if (already_completed
)
1498 if (req
->r_callback
)
1499 req
->r_callback(req
, msg
);
1501 complete_all(&req
->r_completion
);
1503 if (flags
& CEPH_OSD_FLAG_ONDISK
)
1504 complete_request(req
);
1507 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1508 ceph_osdc_put_request(req
);
1512 ceph_osdc_put_request(req
);
1514 mutex_unlock(&osdc
->request_mutex
);
1516 pr_err("corrupt osd_op_reply got %d %d\n",
1517 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
));
1521 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1523 struct rb_node
*p
, *n
;
1525 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1526 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1529 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1530 memcmp(&osd
->o_con
.peer_addr
,
1531 ceph_osd_addr(osdc
->osdmap
,
1533 sizeof(struct ceph_entity_addr
)) != 0)
1534 __reset_osd(osdc
, osd
);
1539 * Requeue requests whose mapping to an OSD has changed. If requests map to
1540 * no osd, request a new map.
1542 * Caller should hold map_sem for read.
1544 static void kick_requests(struct ceph_osd_client
*osdc
, int force_resend
)
1546 struct ceph_osd_request
*req
, *nreq
;
1551 dout("kick_requests %s\n", force_resend
? " (force resend)" : "");
1552 mutex_lock(&osdc
->request_mutex
);
1553 for (p
= rb_first(&osdc
->requests
); p
; ) {
1554 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1558 * For linger requests that have not yet been
1559 * registered, move them to the linger list; they'll
1560 * be sent to the osd in the loop below. Unregister
1561 * the request before re-registering it as a linger
1562 * request to ensure the __map_request() below
1563 * will decide it needs to be sent.
1565 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
1566 dout("%p tid %llu restart on osd%d\n",
1568 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1569 __unregister_request(osdc
, req
);
1570 __register_linger_request(osdc
, req
);
1574 err
= __map_request(osdc
, req
, force_resend
);
1576 continue; /* error */
1577 if (req
->r_osd
== NULL
) {
1578 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1579 needmap
++; /* request a newer map */
1580 } else if (err
> 0) {
1581 if (!req
->r_linger
) {
1582 dout("%p tid %llu requeued on osd%d\n", req
,
1584 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1585 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1590 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1592 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1594 err
= __map_request(osdc
, req
, force_resend
);
1595 dout("__map_request returned %d\n", err
);
1597 continue; /* no change and no osd was specified */
1599 continue; /* hrm! */
1600 if (req
->r_osd
== NULL
) {
1601 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1602 needmap
++; /* request a newer map */
1606 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1607 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1608 __register_request(osdc
, req
);
1609 __unregister_linger_request(osdc
, req
);
1611 mutex_unlock(&osdc
->request_mutex
);
1614 dout("%d requests for down osds, need new map\n", needmap
);
1615 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1617 reset_changed_osds(osdc
);
1622 * Process updated osd map.
1624 * The message contains any number of incremental and full maps, normally
1625 * indicating some sort of topology change in the cluster. Kick requests
1626 * off to different OSDs as needed.
1628 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1630 void *p
, *end
, *next
;
1631 u32 nr_maps
, maplen
;
1633 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1635 struct ceph_fsid fsid
;
1637 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1638 p
= msg
->front
.iov_base
;
1639 end
= p
+ msg
->front
.iov_len
;
1642 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1643 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1644 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1647 down_write(&osdc
->map_sem
);
1649 /* incremental maps */
1650 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1651 dout(" %d inc maps\n", nr_maps
);
1652 while (nr_maps
> 0) {
1653 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1654 epoch
= ceph_decode_32(&p
);
1655 maplen
= ceph_decode_32(&p
);
1656 ceph_decode_need(&p
, end
, maplen
, bad
);
1658 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1659 dout("applying incremental map %u len %d\n",
1661 newmap
= osdmap_apply_incremental(&p
, next
,
1663 &osdc
->client
->msgr
);
1664 if (IS_ERR(newmap
)) {
1665 err
= PTR_ERR(newmap
);
1669 if (newmap
!= osdc
->osdmap
) {
1670 ceph_osdmap_destroy(osdc
->osdmap
);
1671 osdc
->osdmap
= newmap
;
1673 kick_requests(osdc
, 0);
1675 dout("ignoring incremental map %u len %d\n",
1685 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1686 dout(" %d full maps\n", nr_maps
);
1688 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1689 epoch
= ceph_decode_32(&p
);
1690 maplen
= ceph_decode_32(&p
);
1691 ceph_decode_need(&p
, end
, maplen
, bad
);
1693 dout("skipping non-latest full map %u len %d\n",
1695 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1696 dout("skipping full map %u len %d, "
1697 "older than our %u\n", epoch
, maplen
,
1698 osdc
->osdmap
->epoch
);
1700 int skipped_map
= 0;
1702 dout("taking full map %u len %d\n", epoch
, maplen
);
1703 newmap
= osdmap_decode(&p
, p
+maplen
);
1704 if (IS_ERR(newmap
)) {
1705 err
= PTR_ERR(newmap
);
1709 oldmap
= osdc
->osdmap
;
1710 osdc
->osdmap
= newmap
;
1712 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
1714 ceph_osdmap_destroy(oldmap
);
1716 kick_requests(osdc
, skipped_map
);
1723 downgrade_write(&osdc
->map_sem
);
1724 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1727 * subscribe to subsequent osdmap updates if full to ensure
1728 * we find out when we are no longer full and stop returning
1731 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
))
1732 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1734 mutex_lock(&osdc
->request_mutex
);
1735 __send_queued(osdc
);
1736 mutex_unlock(&osdc
->request_mutex
);
1737 up_read(&osdc
->map_sem
);
1738 wake_up_all(&osdc
->client
->auth_wq
);
1742 pr_err("osdc handle_map corrupt msg\n");
1744 up_write(&osdc
->map_sem
);
1749 * watch/notify callback event infrastructure
1751 * These callbacks are used both for watch and notify operations.
1753 static void __release_event(struct kref
*kref
)
1755 struct ceph_osd_event
*event
=
1756 container_of(kref
, struct ceph_osd_event
, kref
);
1758 dout("__release_event %p\n", event
);
1762 static void get_event(struct ceph_osd_event
*event
)
1764 kref_get(&event
->kref
);
1767 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1769 kref_put(&event
->kref
, __release_event
);
1771 EXPORT_SYMBOL(ceph_osdc_put_event
);
1773 static void __insert_event(struct ceph_osd_client
*osdc
,
1774 struct ceph_osd_event
*new)
1776 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1777 struct rb_node
*parent
= NULL
;
1778 struct ceph_osd_event
*event
= NULL
;
1782 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1783 if (new->cookie
< event
->cookie
)
1785 else if (new->cookie
> event
->cookie
)
1786 p
= &(*p
)->rb_right
;
1791 rb_link_node(&new->node
, parent
, p
);
1792 rb_insert_color(&new->node
, &osdc
->event_tree
);
1795 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1798 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1799 struct rb_node
*parent
= NULL
;
1800 struct ceph_osd_event
*event
= NULL
;
1804 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1805 if (cookie
< event
->cookie
)
1807 else if (cookie
> event
->cookie
)
1808 p
= &(*p
)->rb_right
;
1815 static void __remove_event(struct ceph_osd_event
*event
)
1817 struct ceph_osd_client
*osdc
= event
->osdc
;
1819 if (!RB_EMPTY_NODE(&event
->node
)) {
1820 dout("__remove_event removed %p\n", event
);
1821 rb_erase(&event
->node
, &osdc
->event_tree
);
1822 ceph_osdc_put_event(event
);
1824 dout("__remove_event didn't remove %p\n", event
);
1828 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1829 void (*event_cb
)(u64
, u64
, u8
, void *),
1830 void *data
, struct ceph_osd_event
**pevent
)
1832 struct ceph_osd_event
*event
;
1834 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1838 dout("create_event %p\n", event
);
1839 event
->cb
= event_cb
;
1840 event
->one_shot
= 0;
1843 INIT_LIST_HEAD(&event
->osd_node
);
1844 RB_CLEAR_NODE(&event
->node
);
1845 kref_init(&event
->kref
); /* one ref for us */
1846 kref_get(&event
->kref
); /* one ref for the caller */
1848 spin_lock(&osdc
->event_lock
);
1849 event
->cookie
= ++osdc
->event_count
;
1850 __insert_event(osdc
, event
);
1851 spin_unlock(&osdc
->event_lock
);
1856 EXPORT_SYMBOL(ceph_osdc_create_event
);
1858 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1860 struct ceph_osd_client
*osdc
= event
->osdc
;
1862 dout("cancel_event %p\n", event
);
1863 spin_lock(&osdc
->event_lock
);
1864 __remove_event(event
);
1865 spin_unlock(&osdc
->event_lock
);
1866 ceph_osdc_put_event(event
); /* caller's */
1868 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1871 static void do_event_work(struct work_struct
*work
)
1873 struct ceph_osd_event_work
*event_work
=
1874 container_of(work
, struct ceph_osd_event_work
, work
);
1875 struct ceph_osd_event
*event
= event_work
->event
;
1876 u64 ver
= event_work
->ver
;
1877 u64 notify_id
= event_work
->notify_id
;
1878 u8 opcode
= event_work
->opcode
;
1880 dout("do_event_work completing %p\n", event
);
1881 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1882 dout("do_event_work completed %p\n", event
);
1883 ceph_osdc_put_event(event
);
1889 * Process osd watch notifications
1891 static void handle_watch_notify(struct ceph_osd_client
*osdc
,
1892 struct ceph_msg
*msg
)
1896 u64 cookie
, ver
, notify_id
;
1898 struct ceph_osd_event
*event
;
1899 struct ceph_osd_event_work
*event_work
;
1901 p
= msg
->front
.iov_base
;
1902 end
= p
+ msg
->front
.iov_len
;
1904 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1905 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1906 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1907 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1908 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1910 spin_lock(&osdc
->event_lock
);
1911 event
= __find_event(osdc
, cookie
);
1913 BUG_ON(event
->one_shot
);
1916 spin_unlock(&osdc
->event_lock
);
1917 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1918 cookie
, ver
, event
);
1920 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1922 dout("ERROR: could not allocate event_work\n");
1925 INIT_WORK(&event_work
->work
, do_event_work
);
1926 event_work
->event
= event
;
1927 event_work
->ver
= ver
;
1928 event_work
->notify_id
= notify_id
;
1929 event_work
->opcode
= opcode
;
1930 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1931 dout("WARNING: failed to queue notify event work\n");
1939 ceph_osdc_put_event(event
);
1943 pr_err("osdc handle_watch_notify corrupt msg\n");
1947 static void ceph_osdc_msg_data_set(struct ceph_msg
*msg
,
1948 struct ceph_osd_data
*osd_data
)
1950 u64 length
= ceph_osd_data_length(osd_data
);
1952 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
) {
1953 BUG_ON(length
> (u64
) SIZE_MAX
);
1955 ceph_msg_data_set_pages(msg
, osd_data
->pages
,
1956 length
, osd_data
->alignment
);
1957 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGELIST
) {
1959 ceph_msg_data_set_pagelist(msg
, osd_data
->pagelist
);
1961 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_BIO
) {
1962 ceph_msg_data_set_bio(msg
, osd_data
->bio
, length
);
1965 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_NONE
);
1970 * Register request, send initial attempt.
1972 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1973 struct ceph_osd_request
*req
,
1978 /* Set up response incoming data and request outgoing data fields */
1980 ceph_osdc_msg_data_set(req
->r_reply
, &req
->r_data_in
);
1981 ceph_osdc_msg_data_set(req
->r_request
, &req
->r_data_out
);
1983 down_read(&osdc
->map_sem
);
1984 mutex_lock(&osdc
->request_mutex
);
1985 __register_request(osdc
, req
);
1986 WARN_ON(req
->r_sent
);
1987 rc
= __map_request(osdc
, req
, 0);
1990 dout("osdc_start_request failed map, "
1991 " will retry %lld\n", req
->r_tid
);
1996 if (req
->r_osd
== NULL
) {
1997 dout("send_request %p no up osds in pg\n", req
);
1998 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
2000 __send_queued(osdc
);
2004 mutex_unlock(&osdc
->request_mutex
);
2005 up_read(&osdc
->map_sem
);
2008 EXPORT_SYMBOL(ceph_osdc_start_request
);
2011 * wait for a request to complete
2013 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
2014 struct ceph_osd_request
*req
)
2018 rc
= wait_for_completion_interruptible(&req
->r_completion
);
2020 mutex_lock(&osdc
->request_mutex
);
2021 __cancel_request(req
);
2022 __unregister_request(osdc
, req
);
2023 mutex_unlock(&osdc
->request_mutex
);
2024 complete_request(req
);
2025 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
2029 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
2030 return req
->r_result
;
2032 EXPORT_SYMBOL(ceph_osdc_wait_request
);
2035 * sync - wait for all in-flight requests to flush. avoid starvation.
2037 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
2039 struct ceph_osd_request
*req
;
2040 u64 last_tid
, next_tid
= 0;
2042 mutex_lock(&osdc
->request_mutex
);
2043 last_tid
= osdc
->last_tid
;
2045 req
= __lookup_request_ge(osdc
, next_tid
);
2048 if (req
->r_tid
> last_tid
)
2051 next_tid
= req
->r_tid
+ 1;
2052 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
2055 ceph_osdc_get_request(req
);
2056 mutex_unlock(&osdc
->request_mutex
);
2057 dout("sync waiting on tid %llu (last is %llu)\n",
2058 req
->r_tid
, last_tid
);
2059 wait_for_completion(&req
->r_safe_completion
);
2060 mutex_lock(&osdc
->request_mutex
);
2061 ceph_osdc_put_request(req
);
2063 mutex_unlock(&osdc
->request_mutex
);
2064 dout("sync done (thru tid %llu)\n", last_tid
);
2066 EXPORT_SYMBOL(ceph_osdc_sync
);
2071 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
2076 osdc
->client
= client
;
2077 osdc
->osdmap
= NULL
;
2078 init_rwsem(&osdc
->map_sem
);
2079 init_completion(&osdc
->map_waiters
);
2080 osdc
->last_requested_map
= 0;
2081 mutex_init(&osdc
->request_mutex
);
2083 osdc
->osds
= RB_ROOT
;
2084 INIT_LIST_HEAD(&osdc
->osd_lru
);
2085 osdc
->requests
= RB_ROOT
;
2086 INIT_LIST_HEAD(&osdc
->req_lru
);
2087 INIT_LIST_HEAD(&osdc
->req_unsent
);
2088 INIT_LIST_HEAD(&osdc
->req_notarget
);
2089 INIT_LIST_HEAD(&osdc
->req_linger
);
2090 osdc
->num_requests
= 0;
2091 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
2092 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
2093 spin_lock_init(&osdc
->event_lock
);
2094 osdc
->event_tree
= RB_ROOT
;
2095 osdc
->event_count
= 0;
2097 schedule_delayed_work(&osdc
->osds_timeout_work
,
2098 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
2101 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
2102 sizeof(struct ceph_osd_request
));
2103 if (!osdc
->req_mempool
)
2106 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
2107 OSD_OP_FRONT_LEN
, 10, true,
2111 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
2112 OSD_OPREPLY_FRONT_LEN
, 10, true,
2117 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
2118 if (IS_ERR(osdc
->notify_wq
)) {
2119 err
= PTR_ERR(osdc
->notify_wq
);
2120 osdc
->notify_wq
= NULL
;
2126 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2128 mempool_destroy(osdc
->req_mempool
);
2133 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
2135 flush_workqueue(osdc
->notify_wq
);
2136 destroy_workqueue(osdc
->notify_wq
);
2137 cancel_delayed_work_sync(&osdc
->timeout_work
);
2138 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
2140 ceph_osdmap_destroy(osdc
->osdmap
);
2141 osdc
->osdmap
= NULL
;
2143 remove_all_osds(osdc
);
2144 mempool_destroy(osdc
->req_mempool
);
2145 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2146 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2150 * Read some contiguous pages. If we cross a stripe boundary, shorten
2151 * *plen. Return number of bytes read, or error.
2153 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
2154 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
2156 u32 truncate_seq
, u64 truncate_size
,
2157 struct page
**pages
, int num_pages
, int page_align
)
2159 struct ceph_osd_request
*req
;
2162 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
2163 vino
.snap
, off
, *plen
);
2164 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
, 1,
2165 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
2166 NULL
, truncate_seq
, truncate_size
,
2169 return PTR_ERR(req
);
2171 /* it may be a short read due to an object boundary */
2173 ceph_osd_data_pages_init(&req
->r_data_in
, pages
, *plen
, page_align
,
2176 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2177 off
, *plen
, *plen
, page_align
);
2179 ceph_osdc_build_request(req
, off
, NULL
, vino
.snap
, NULL
);
2181 rc
= ceph_osdc_start_request(osdc
, req
, false);
2183 rc
= ceph_osdc_wait_request(osdc
, req
);
2185 ceph_osdc_put_request(req
);
2186 dout("readpages result %d\n", rc
);
2189 EXPORT_SYMBOL(ceph_osdc_readpages
);
2192 * do a synchronous write on N pages
2194 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
2195 struct ceph_file_layout
*layout
,
2196 struct ceph_snap_context
*snapc
,
2198 u32 truncate_seq
, u64 truncate_size
,
2199 struct timespec
*mtime
,
2200 struct page
**pages
, int num_pages
)
2202 struct ceph_osd_request
*req
;
2204 int page_align
= off
& ~PAGE_MASK
;
2206 BUG_ON(vino
.snap
!= CEPH_NOSNAP
); /* snapshots aren't writeable */
2207 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
, 1,
2209 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
2210 snapc
, truncate_seq
, truncate_size
,
2213 return PTR_ERR(req
);
2215 /* it may be a short write due to an object boundary */
2216 ceph_osd_data_pages_init(&req
->r_data_out
, pages
, len
, page_align
,
2218 dout("writepages %llu~%llu (%llu bytes)\n", off
, len
, len
);
2220 ceph_osdc_build_request(req
, off
, snapc
, CEPH_NOSNAP
, mtime
);
2222 rc
= ceph_osdc_start_request(osdc
, req
, true);
2224 rc
= ceph_osdc_wait_request(osdc
, req
);
2226 ceph_osdc_put_request(req
);
2229 dout("writepages result %d\n", rc
);
2232 EXPORT_SYMBOL(ceph_osdc_writepages
);
2235 * handle incoming message
2237 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2239 struct ceph_osd
*osd
= con
->private;
2240 struct ceph_osd_client
*osdc
;
2241 int type
= le16_to_cpu(msg
->hdr
.type
);
2248 case CEPH_MSG_OSD_MAP
:
2249 ceph_osdc_handle_map(osdc
, msg
);
2251 case CEPH_MSG_OSD_OPREPLY
:
2252 handle_reply(osdc
, msg
, con
);
2254 case CEPH_MSG_WATCH_NOTIFY
:
2255 handle_watch_notify(osdc
, msg
);
2259 pr_err("received unknown message type %d %s\n", type
,
2260 ceph_msg_type_name(type
));
2267 * lookup and return message for incoming reply. set up reply message
2270 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2271 struct ceph_msg_header
*hdr
,
2274 struct ceph_osd
*osd
= con
->private;
2275 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2277 struct ceph_osd_request
*req
;
2278 int front
= le32_to_cpu(hdr
->front_len
);
2279 int data_len
= le32_to_cpu(hdr
->data_len
);
2282 tid
= le64_to_cpu(hdr
->tid
);
2283 mutex_lock(&osdc
->request_mutex
);
2284 req
= __lookup_request(osdc
, tid
);
2288 dout("get_reply unknown tid %llu from osd%d\n", tid
,
2293 if (req
->r_reply
->con
)
2294 dout("%s revoking msg %p from old con %p\n", __func__
,
2295 req
->r_reply
, req
->r_reply
->con
);
2296 ceph_msg_revoke_incoming(req
->r_reply
);
2298 if (front
> req
->r_reply
->front
.iov_len
) {
2299 pr_warning("get_reply front %d > preallocated %d\n",
2300 front
, (int)req
->r_reply
->front
.iov_len
);
2301 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
, false);
2304 ceph_msg_put(req
->r_reply
);
2307 m
= ceph_msg_get(req
->r_reply
);
2310 struct ceph_osd_data
*osd_data
= &req
->r_data_in
;
2312 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
) {
2313 if (osd_data
->pages
&&
2314 unlikely(osd_data
->length
< data_len
)) {
2316 pr_warning("tid %lld reply has %d bytes "
2317 "we had only %llu bytes ready\n",
2318 tid
, data_len
, osd_data
->length
);
2327 dout("get_reply tid %lld %p\n", tid
, m
);
2330 mutex_unlock(&osdc
->request_mutex
);
2335 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2336 struct ceph_msg_header
*hdr
,
2339 struct ceph_osd
*osd
= con
->private;
2340 int type
= le16_to_cpu(hdr
->type
);
2341 int front
= le32_to_cpu(hdr
->front_len
);
2345 case CEPH_MSG_OSD_MAP
:
2346 case CEPH_MSG_WATCH_NOTIFY
:
2347 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2348 case CEPH_MSG_OSD_OPREPLY
:
2349 return get_reply(con
, hdr
, skip
);
2351 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2359 * Wrappers to refcount containing ceph_osd struct
2361 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2363 struct ceph_osd
*osd
= con
->private;
2369 static void put_osd_con(struct ceph_connection
*con
)
2371 struct ceph_osd
*osd
= con
->private;
2379 * Note: returned pointer is the address of a structure that's
2380 * managed separately. Caller must *not* attempt to free it.
2382 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2383 int *proto
, int force_new
)
2385 struct ceph_osd
*o
= con
->private;
2386 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2387 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2388 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2390 if (force_new
&& auth
->authorizer
) {
2391 ceph_auth_destroy_authorizer(ac
, auth
->authorizer
);
2392 auth
->authorizer
= NULL
;
2394 if (!auth
->authorizer
) {
2395 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2398 return ERR_PTR(ret
);
2400 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2403 return ERR_PTR(ret
);
2405 *proto
= ac
->protocol
;
2411 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2413 struct ceph_osd
*o
= con
->private;
2414 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2415 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2417 return ceph_auth_verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
2420 static int invalidate_authorizer(struct ceph_connection
*con
)
2422 struct ceph_osd
*o
= con
->private;
2423 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2424 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2426 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2427 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2430 static const struct ceph_connection_operations osd_con_ops
= {
2433 .dispatch
= dispatch
,
2434 .get_authorizer
= get_authorizer
,
2435 .verify_authorizer_reply
= verify_authorizer_reply
,
2436 .invalidate_authorizer
= invalidate_authorizer
,
2437 .alloc_msg
= alloc_msg
,