2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
6 #include <linux/highmem.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
12 #include <linux/bio.h>
15 #include <linux/ceph/libceph.h>
16 #include <linux/ceph/osd_client.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/pagelist.h>
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static struct kmem_cache
*ceph_osd_request_cache
;
26 static const struct ceph_connection_operations osd_con_ops
;
28 static void __send_queued(struct ceph_osd_client
*osdc
);
29 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
);
30 static void __register_request(struct ceph_osd_client
*osdc
,
31 struct ceph_osd_request
*req
);
32 static void __unregister_request(struct ceph_osd_client
*osdc
,
33 struct ceph_osd_request
*req
);
34 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
35 struct ceph_osd_request
*req
);
36 static void __enqueue_request(struct ceph_osd_request
*req
);
37 static void __send_request(struct ceph_osd_client
*osdc
,
38 struct ceph_osd_request
*req
);
41 * Implement client access to distributed object storage cluster.
43 * All data objects are stored within a cluster/cloud of OSDs, or
44 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
45 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
46 * remote daemons serving up and coordinating consistent and safe
49 * Cluster membership and the mapping of data objects onto storage devices
50 * are described by the osd map.
52 * We keep track of pending OSD requests (read, write), resubmit
53 * requests to different OSDs when the cluster topology/data layout
54 * change, or retry the affected requests when the communications
55 * channel with an OSD is reset.
59 * calculate the mapping of a file extent onto an object, and fill out the
60 * request accordingly. shorten extent as necessary if it crosses an
63 * fill osd op in request message.
65 static int calc_layout(struct ceph_file_layout
*layout
, u64 off
, u64
*plen
,
66 u64
*objnum
, u64
*objoff
, u64
*objlen
)
72 r
= ceph_calc_file_object_mapping(layout
, off
, orig_len
, objnum
,
76 if (*objlen
< orig_len
) {
78 dout(" skipping last %llu, final file extent %llu~%llu\n",
79 orig_len
- *plen
, off
, *plen
);
82 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum
, *objoff
, *objlen
);
87 static void ceph_osd_data_init(struct ceph_osd_data
*osd_data
)
89 memset(osd_data
, 0, sizeof (*osd_data
));
90 osd_data
->type
= CEPH_OSD_DATA_TYPE_NONE
;
93 static void ceph_osd_data_pages_init(struct ceph_osd_data
*osd_data
,
94 struct page
**pages
, u64 length
, u32 alignment
,
95 bool pages_from_pool
, bool own_pages
)
97 osd_data
->type
= CEPH_OSD_DATA_TYPE_PAGES
;
98 osd_data
->pages
= pages
;
99 osd_data
->length
= length
;
100 osd_data
->alignment
= alignment
;
101 osd_data
->pages_from_pool
= pages_from_pool
;
102 osd_data
->own_pages
= own_pages
;
105 static void ceph_osd_data_pagelist_init(struct ceph_osd_data
*osd_data
,
106 struct ceph_pagelist
*pagelist
)
108 osd_data
->type
= CEPH_OSD_DATA_TYPE_PAGELIST
;
109 osd_data
->pagelist
= pagelist
;
113 static void ceph_osd_data_bio_init(struct ceph_osd_data
*osd_data
,
114 struct bio
*bio
, size_t bio_length
)
116 osd_data
->type
= CEPH_OSD_DATA_TYPE_BIO
;
118 osd_data
->bio_length
= bio_length
;
120 #endif /* CONFIG_BLOCK */
122 #define osd_req_op_data(oreq, whch, typ, fld) \
124 struct ceph_osd_request *__oreq = (oreq); \
125 unsigned int __whch = (whch); \
126 BUG_ON(__whch >= __oreq->r_num_ops); \
127 &__oreq->r_ops[__whch].typ.fld; \
130 static struct ceph_osd_data
*
131 osd_req_op_raw_data_in(struct ceph_osd_request
*osd_req
, unsigned int which
)
133 BUG_ON(which
>= osd_req
->r_num_ops
);
135 return &osd_req
->r_ops
[which
].raw_data_in
;
138 struct ceph_osd_data
*
139 osd_req_op_extent_osd_data(struct ceph_osd_request
*osd_req
,
142 return osd_req_op_data(osd_req
, which
, extent
, osd_data
);
144 EXPORT_SYMBOL(osd_req_op_extent_osd_data
);
146 void osd_req_op_raw_data_in_pages(struct ceph_osd_request
*osd_req
,
147 unsigned int which
, struct page
**pages
,
148 u64 length
, u32 alignment
,
149 bool pages_from_pool
, bool own_pages
)
151 struct ceph_osd_data
*osd_data
;
153 osd_data
= osd_req_op_raw_data_in(osd_req
, which
);
154 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
155 pages_from_pool
, own_pages
);
157 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages
);
159 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request
*osd_req
,
160 unsigned int which
, struct page
**pages
,
161 u64 length
, u32 alignment
,
162 bool pages_from_pool
, bool own_pages
)
164 struct ceph_osd_data
*osd_data
;
166 osd_data
= osd_req_op_data(osd_req
, which
, extent
, osd_data
);
167 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
168 pages_from_pool
, own_pages
);
170 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages
);
172 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request
*osd_req
,
173 unsigned int which
, struct ceph_pagelist
*pagelist
)
175 struct ceph_osd_data
*osd_data
;
177 osd_data
= osd_req_op_data(osd_req
, which
, extent
, osd_data
);
178 ceph_osd_data_pagelist_init(osd_data
, pagelist
);
180 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist
);
183 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request
*osd_req
,
184 unsigned int which
, struct bio
*bio
, size_t bio_length
)
186 struct ceph_osd_data
*osd_data
;
188 osd_data
= osd_req_op_data(osd_req
, which
, extent
, osd_data
);
189 ceph_osd_data_bio_init(osd_data
, bio
, bio_length
);
191 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio
);
192 #endif /* CONFIG_BLOCK */
194 static void osd_req_op_cls_request_info_pagelist(
195 struct ceph_osd_request
*osd_req
,
196 unsigned int which
, struct ceph_pagelist
*pagelist
)
198 struct ceph_osd_data
*osd_data
;
200 osd_data
= osd_req_op_data(osd_req
, which
, cls
, request_info
);
201 ceph_osd_data_pagelist_init(osd_data
, pagelist
);
204 void osd_req_op_cls_request_data_pagelist(
205 struct ceph_osd_request
*osd_req
,
206 unsigned int which
, struct ceph_pagelist
*pagelist
)
208 struct ceph_osd_data
*osd_data
;
210 osd_data
= osd_req_op_data(osd_req
, which
, cls
, request_data
);
211 ceph_osd_data_pagelist_init(osd_data
, pagelist
);
213 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist
);
215 void osd_req_op_cls_request_data_pages(struct ceph_osd_request
*osd_req
,
216 unsigned int which
, struct page
**pages
, u64 length
,
217 u32 alignment
, bool pages_from_pool
, bool own_pages
)
219 struct ceph_osd_data
*osd_data
;
221 osd_data
= osd_req_op_data(osd_req
, which
, cls
, request_data
);
222 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
223 pages_from_pool
, own_pages
);
225 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages
);
227 void osd_req_op_cls_response_data_pages(struct ceph_osd_request
*osd_req
,
228 unsigned int which
, struct page
**pages
, u64 length
,
229 u32 alignment
, bool pages_from_pool
, bool own_pages
)
231 struct ceph_osd_data
*osd_data
;
233 osd_data
= osd_req_op_data(osd_req
, which
, cls
, response_data
);
234 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
235 pages_from_pool
, own_pages
);
237 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages
);
239 static u64
ceph_osd_data_length(struct ceph_osd_data
*osd_data
)
241 switch (osd_data
->type
) {
242 case CEPH_OSD_DATA_TYPE_NONE
:
244 case CEPH_OSD_DATA_TYPE_PAGES
:
245 return osd_data
->length
;
246 case CEPH_OSD_DATA_TYPE_PAGELIST
:
247 return (u64
)osd_data
->pagelist
->length
;
249 case CEPH_OSD_DATA_TYPE_BIO
:
250 return (u64
)osd_data
->bio_length
;
251 #endif /* CONFIG_BLOCK */
253 WARN(true, "unrecognized data type %d\n", (int)osd_data
->type
);
258 static void ceph_osd_data_release(struct ceph_osd_data
*osd_data
)
260 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
&& osd_data
->own_pages
) {
263 num_pages
= calc_pages_for((u64
)osd_data
->alignment
,
264 (u64
)osd_data
->length
);
265 ceph_release_page_vector(osd_data
->pages
, num_pages
);
267 ceph_osd_data_init(osd_data
);
270 static void osd_req_op_data_release(struct ceph_osd_request
*osd_req
,
273 struct ceph_osd_req_op
*op
;
275 BUG_ON(which
>= osd_req
->r_num_ops
);
276 op
= &osd_req
->r_ops
[which
];
279 case CEPH_OSD_OP_READ
:
280 case CEPH_OSD_OP_WRITE
:
281 case CEPH_OSD_OP_WRITEFULL
:
282 ceph_osd_data_release(&op
->extent
.osd_data
);
284 case CEPH_OSD_OP_CALL
:
285 ceph_osd_data_release(&op
->cls
.request_info
);
286 ceph_osd_data_release(&op
->cls
.request_data
);
287 ceph_osd_data_release(&op
->cls
.response_data
);
289 case CEPH_OSD_OP_SETXATTR
:
290 case CEPH_OSD_OP_CMPXATTR
:
291 ceph_osd_data_release(&op
->xattr
.osd_data
);
293 case CEPH_OSD_OP_STAT
:
294 ceph_osd_data_release(&op
->raw_data_in
);
302 * Assumes @t is zero-initialized.
304 static void target_init(struct ceph_osd_request_target
*t
)
306 ceph_oid_init(&t
->base_oid
);
307 ceph_oloc_init(&t
->base_oloc
);
308 ceph_oid_init(&t
->target_oid
);
309 ceph_oloc_init(&t
->target_oloc
);
311 ceph_osds_init(&t
->acting
);
312 ceph_osds_init(&t
->up
);
316 t
->osd
= CEPH_HOMELESS_OSD
;
319 static void target_destroy(struct ceph_osd_request_target
*t
)
321 ceph_oid_destroy(&t
->base_oid
);
322 ceph_oid_destroy(&t
->target_oid
);
328 static void ceph_osdc_release_request(struct kref
*kref
)
330 struct ceph_osd_request
*req
= container_of(kref
,
331 struct ceph_osd_request
, r_kref
);
334 dout("%s %p (r_request %p r_reply %p)\n", __func__
, req
,
335 req
->r_request
, req
->r_reply
);
336 WARN_ON(!RB_EMPTY_NODE(&req
->r_node
));
337 WARN_ON(!list_empty(&req
->r_req_lru_item
));
338 WARN_ON(!list_empty(&req
->r_osd_item
));
339 WARN_ON(!list_empty(&req
->r_linger_item
));
340 WARN_ON(!list_empty(&req
->r_linger_osd_item
));
344 ceph_msg_put(req
->r_request
);
346 ceph_msg_revoke_incoming(req
->r_reply
);
347 ceph_msg_put(req
->r_reply
);
350 for (which
= 0; which
< req
->r_num_ops
; which
++)
351 osd_req_op_data_release(req
, which
);
353 ceph_oid_destroy(&req
->r_base_oid
);
354 ceph_oid_destroy(&req
->r_target_oid
);
355 ceph_put_snap_context(req
->r_snapc
);
358 mempool_free(req
, req
->r_osdc
->req_mempool
);
359 else if (req
->r_num_ops
<= CEPH_OSD_SLAB_OPS
)
360 kmem_cache_free(ceph_osd_request_cache
, req
);
365 void ceph_osdc_get_request(struct ceph_osd_request
*req
)
367 dout("%s %p (was %d)\n", __func__
, req
,
368 atomic_read(&req
->r_kref
.refcount
));
369 kref_get(&req
->r_kref
);
371 EXPORT_SYMBOL(ceph_osdc_get_request
);
373 void ceph_osdc_put_request(struct ceph_osd_request
*req
)
376 dout("%s %p (was %d)\n", __func__
, req
,
377 atomic_read(&req
->r_kref
.refcount
));
378 kref_put(&req
->r_kref
, ceph_osdc_release_request
);
381 EXPORT_SYMBOL(ceph_osdc_put_request
);
383 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
384 struct ceph_snap_context
*snapc
,
385 unsigned int num_ops
,
389 struct ceph_osd_request
*req
;
392 BUG_ON(num_ops
> CEPH_OSD_SLAB_OPS
);
393 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
394 } else if (num_ops
<= CEPH_OSD_SLAB_OPS
) {
395 req
= kmem_cache_alloc(ceph_osd_request_cache
, gfp_flags
);
397 BUG_ON(num_ops
> CEPH_OSD_MAX_OPS
);
398 req
= kmalloc(sizeof(*req
) + num_ops
* sizeof(req
->r_ops
[0]),
404 /* req only, each op is zeroed in _osd_req_op_init() */
405 memset(req
, 0, sizeof(*req
));
408 req
->r_mempool
= use_mempool
;
409 req
->r_num_ops
= num_ops
;
410 req
->r_snapid
= CEPH_NOSNAP
;
411 req
->r_snapc
= ceph_get_snap_context(snapc
);
413 kref_init(&req
->r_kref
);
414 init_completion(&req
->r_completion
);
415 init_completion(&req
->r_safe_completion
);
416 RB_CLEAR_NODE(&req
->r_node
);
417 INIT_LIST_HEAD(&req
->r_unsafe_item
);
418 INIT_LIST_HEAD(&req
->r_linger_item
);
419 INIT_LIST_HEAD(&req
->r_linger_osd_item
);
420 INIT_LIST_HEAD(&req
->r_req_lru_item
);
421 INIT_LIST_HEAD(&req
->r_osd_item
);
423 ceph_oid_init(&req
->r_base_oid
);
424 req
->r_base_oloc
.pool
= -1;
425 ceph_oid_init(&req
->r_target_oid
);
426 req
->r_target_oloc
.pool
= -1;
428 dout("%s req %p\n", __func__
, req
);
431 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
433 int ceph_osdc_alloc_messages(struct ceph_osd_request
*req
, gfp_t gfp
)
435 struct ceph_osd_client
*osdc
= req
->r_osdc
;
436 struct ceph_msg
*msg
;
439 WARN_ON(ceph_oid_empty(&req
->r_base_oid
));
441 /* create request message */
442 msg_size
= 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
443 msg_size
+= 4 + 4 + 4 + 8; /* mtime, reassert_version */
444 msg_size
+= 2 + 4 + 8 + 4 + 4; /* oloc */
445 msg_size
+= 1 + 8 + 4 + 4; /* pgid */
446 msg_size
+= 4 + req
->r_base_oid
.name_len
; /* oid */
447 msg_size
+= 2 + req
->r_num_ops
* sizeof(struct ceph_osd_op
);
448 msg_size
+= 8; /* snapid */
449 msg_size
+= 8; /* snap_seq */
450 msg_size
+= 4 + 8 * (req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
451 msg_size
+= 4; /* retry_attempt */
454 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
456 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp
, true);
460 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
461 req
->r_request
= msg
;
463 /* create reply message */
464 msg_size
= OSD_OPREPLY_FRONT_LEN
;
465 msg_size
+= req
->r_base_oid
.name_len
;
466 msg_size
+= req
->r_num_ops
* sizeof(struct ceph_osd_op
);
469 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
471 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, msg_size
, gfp
, true);
479 EXPORT_SYMBOL(ceph_osdc_alloc_messages
);
481 static bool osd_req_opcode_valid(u16 opcode
)
484 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
485 __CEPH_FORALL_OSD_OPS(GENERATE_CASE
)
493 * This is an osd op init function for opcodes that have no data or
494 * other information associated with them. It also serves as a
495 * common init routine for all the other init functions, below.
497 static struct ceph_osd_req_op
*
498 _osd_req_op_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
499 u16 opcode
, u32 flags
)
501 struct ceph_osd_req_op
*op
;
503 BUG_ON(which
>= osd_req
->r_num_ops
);
504 BUG_ON(!osd_req_opcode_valid(opcode
));
506 op
= &osd_req
->r_ops
[which
];
507 memset(op
, 0, sizeof (*op
));
514 void osd_req_op_init(struct ceph_osd_request
*osd_req
,
515 unsigned int which
, u16 opcode
, u32 flags
)
517 (void)_osd_req_op_init(osd_req
, which
, opcode
, flags
);
519 EXPORT_SYMBOL(osd_req_op_init
);
521 void osd_req_op_extent_init(struct ceph_osd_request
*osd_req
,
522 unsigned int which
, u16 opcode
,
523 u64 offset
, u64 length
,
524 u64 truncate_size
, u32 truncate_seq
)
526 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
528 size_t payload_len
= 0;
530 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
&&
531 opcode
!= CEPH_OSD_OP_WRITEFULL
&& opcode
!= CEPH_OSD_OP_ZERO
&&
532 opcode
!= CEPH_OSD_OP_TRUNCATE
);
534 op
->extent
.offset
= offset
;
535 op
->extent
.length
= length
;
536 op
->extent
.truncate_size
= truncate_size
;
537 op
->extent
.truncate_seq
= truncate_seq
;
538 if (opcode
== CEPH_OSD_OP_WRITE
|| opcode
== CEPH_OSD_OP_WRITEFULL
)
539 payload_len
+= length
;
541 op
->indata_len
= payload_len
;
543 EXPORT_SYMBOL(osd_req_op_extent_init
);
545 void osd_req_op_extent_update(struct ceph_osd_request
*osd_req
,
546 unsigned int which
, u64 length
)
548 struct ceph_osd_req_op
*op
;
551 BUG_ON(which
>= osd_req
->r_num_ops
);
552 op
= &osd_req
->r_ops
[which
];
553 previous
= op
->extent
.length
;
555 if (length
== previous
)
556 return; /* Nothing to do */
557 BUG_ON(length
> previous
);
559 op
->extent
.length
= length
;
560 op
->indata_len
-= previous
- length
;
562 EXPORT_SYMBOL(osd_req_op_extent_update
);
564 void osd_req_op_extent_dup_last(struct ceph_osd_request
*osd_req
,
565 unsigned int which
, u64 offset_inc
)
567 struct ceph_osd_req_op
*op
, *prev_op
;
569 BUG_ON(which
+ 1 >= osd_req
->r_num_ops
);
571 prev_op
= &osd_req
->r_ops
[which
];
572 op
= _osd_req_op_init(osd_req
, which
+ 1, prev_op
->op
, prev_op
->flags
);
573 /* dup previous one */
574 op
->indata_len
= prev_op
->indata_len
;
575 op
->outdata_len
= prev_op
->outdata_len
;
576 op
->extent
= prev_op
->extent
;
578 op
->extent
.offset
+= offset_inc
;
579 op
->extent
.length
-= offset_inc
;
581 if (op
->op
== CEPH_OSD_OP_WRITE
|| op
->op
== CEPH_OSD_OP_WRITEFULL
)
582 op
->indata_len
-= offset_inc
;
584 EXPORT_SYMBOL(osd_req_op_extent_dup_last
);
586 void osd_req_op_cls_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
587 u16 opcode
, const char *class, const char *method
)
589 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
591 struct ceph_pagelist
*pagelist
;
592 size_t payload_len
= 0;
595 BUG_ON(opcode
!= CEPH_OSD_OP_CALL
);
597 pagelist
= kmalloc(sizeof (*pagelist
), GFP_NOFS
);
599 ceph_pagelist_init(pagelist
);
601 op
->cls
.class_name
= class;
602 size
= strlen(class);
603 BUG_ON(size
> (size_t) U8_MAX
);
604 op
->cls
.class_len
= size
;
605 ceph_pagelist_append(pagelist
, class, size
);
608 op
->cls
.method_name
= method
;
609 size
= strlen(method
);
610 BUG_ON(size
> (size_t) U8_MAX
);
611 op
->cls
.method_len
= size
;
612 ceph_pagelist_append(pagelist
, method
, size
);
615 osd_req_op_cls_request_info_pagelist(osd_req
, which
, pagelist
);
617 op
->cls
.argc
= 0; /* currently unused */
619 op
->indata_len
= payload_len
;
621 EXPORT_SYMBOL(osd_req_op_cls_init
);
623 int osd_req_op_xattr_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
624 u16 opcode
, const char *name
, const void *value
,
625 size_t size
, u8 cmp_op
, u8 cmp_mode
)
627 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
629 struct ceph_pagelist
*pagelist
;
632 BUG_ON(opcode
!= CEPH_OSD_OP_SETXATTR
&& opcode
!= CEPH_OSD_OP_CMPXATTR
);
634 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
638 ceph_pagelist_init(pagelist
);
640 payload_len
= strlen(name
);
641 op
->xattr
.name_len
= payload_len
;
642 ceph_pagelist_append(pagelist
, name
, payload_len
);
644 op
->xattr
.value_len
= size
;
645 ceph_pagelist_append(pagelist
, value
, size
);
648 op
->xattr
.cmp_op
= cmp_op
;
649 op
->xattr
.cmp_mode
= cmp_mode
;
651 ceph_osd_data_pagelist_init(&op
->xattr
.osd_data
, pagelist
);
652 op
->indata_len
= payload_len
;
655 EXPORT_SYMBOL(osd_req_op_xattr_init
);
657 void osd_req_op_watch_init(struct ceph_osd_request
*osd_req
,
658 unsigned int which
, u16 opcode
,
659 u64 cookie
, u64 version
, int flag
)
661 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
664 BUG_ON(opcode
!= CEPH_OSD_OP_NOTIFY_ACK
&& opcode
!= CEPH_OSD_OP_WATCH
);
666 op
->watch
.cookie
= cookie
;
667 op
->watch
.ver
= version
;
668 if (opcode
== CEPH_OSD_OP_WATCH
&& flag
)
669 op
->watch
.flag
= (u8
)1;
671 EXPORT_SYMBOL(osd_req_op_watch_init
);
673 void osd_req_op_alloc_hint_init(struct ceph_osd_request
*osd_req
,
675 u64 expected_object_size
,
676 u64 expected_write_size
)
678 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
679 CEPH_OSD_OP_SETALLOCHINT
,
682 op
->alloc_hint
.expected_object_size
= expected_object_size
;
683 op
->alloc_hint
.expected_write_size
= expected_write_size
;
686 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
687 * not worth a feature bit. Set FAILOK per-op flag to make
688 * sure older osds don't trip over an unsupported opcode.
690 op
->flags
|= CEPH_OSD_OP_FLAG_FAILOK
;
692 EXPORT_SYMBOL(osd_req_op_alloc_hint_init
);
694 static void ceph_osdc_msg_data_add(struct ceph_msg
*msg
,
695 struct ceph_osd_data
*osd_data
)
697 u64 length
= ceph_osd_data_length(osd_data
);
699 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
) {
700 BUG_ON(length
> (u64
) SIZE_MAX
);
702 ceph_msg_data_add_pages(msg
, osd_data
->pages
,
703 length
, osd_data
->alignment
);
704 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGELIST
) {
706 ceph_msg_data_add_pagelist(msg
, osd_data
->pagelist
);
708 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_BIO
) {
709 ceph_msg_data_add_bio(msg
, osd_data
->bio
, length
);
712 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_NONE
);
716 static u64
osd_req_encode_op(struct ceph_osd_request
*req
,
717 struct ceph_osd_op
*dst
, unsigned int which
)
719 struct ceph_osd_req_op
*src
;
720 struct ceph_osd_data
*osd_data
;
721 u64 request_data_len
= 0;
724 BUG_ON(which
>= req
->r_num_ops
);
725 src
= &req
->r_ops
[which
];
726 if (WARN_ON(!osd_req_opcode_valid(src
->op
))) {
727 pr_err("unrecognized osd opcode %d\n", src
->op
);
733 case CEPH_OSD_OP_STAT
:
734 osd_data
= &src
->raw_data_in
;
735 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
737 case CEPH_OSD_OP_READ
:
738 case CEPH_OSD_OP_WRITE
:
739 case CEPH_OSD_OP_WRITEFULL
:
740 case CEPH_OSD_OP_ZERO
:
741 case CEPH_OSD_OP_TRUNCATE
:
742 if (src
->op
== CEPH_OSD_OP_WRITE
||
743 src
->op
== CEPH_OSD_OP_WRITEFULL
)
744 request_data_len
= src
->extent
.length
;
745 dst
->extent
.offset
= cpu_to_le64(src
->extent
.offset
);
746 dst
->extent
.length
= cpu_to_le64(src
->extent
.length
);
747 dst
->extent
.truncate_size
=
748 cpu_to_le64(src
->extent
.truncate_size
);
749 dst
->extent
.truncate_seq
=
750 cpu_to_le32(src
->extent
.truncate_seq
);
751 osd_data
= &src
->extent
.osd_data
;
752 if (src
->op
== CEPH_OSD_OP_WRITE
||
753 src
->op
== CEPH_OSD_OP_WRITEFULL
)
754 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
756 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
758 case CEPH_OSD_OP_CALL
:
759 dst
->cls
.class_len
= src
->cls
.class_len
;
760 dst
->cls
.method_len
= src
->cls
.method_len
;
761 osd_data
= &src
->cls
.request_info
;
762 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
763 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_PAGELIST
);
764 request_data_len
= osd_data
->pagelist
->length
;
766 osd_data
= &src
->cls
.request_data
;
767 data_length
= ceph_osd_data_length(osd_data
);
769 BUG_ON(osd_data
->type
== CEPH_OSD_DATA_TYPE_NONE
);
770 dst
->cls
.indata_len
= cpu_to_le32(data_length
);
771 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
772 src
->indata_len
+= data_length
;
773 request_data_len
+= data_length
;
775 osd_data
= &src
->cls
.response_data
;
776 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
778 case CEPH_OSD_OP_STARTSYNC
:
780 case CEPH_OSD_OP_NOTIFY_ACK
:
781 case CEPH_OSD_OP_WATCH
:
782 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
783 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
784 dst
->watch
.flag
= src
->watch
.flag
;
786 case CEPH_OSD_OP_SETALLOCHINT
:
787 dst
->alloc_hint
.expected_object_size
=
788 cpu_to_le64(src
->alloc_hint
.expected_object_size
);
789 dst
->alloc_hint
.expected_write_size
=
790 cpu_to_le64(src
->alloc_hint
.expected_write_size
);
792 case CEPH_OSD_OP_SETXATTR
:
793 case CEPH_OSD_OP_CMPXATTR
:
794 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
795 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
796 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
797 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
798 osd_data
= &src
->xattr
.osd_data
;
799 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
800 request_data_len
= osd_data
->pagelist
->length
;
802 case CEPH_OSD_OP_CREATE
:
803 case CEPH_OSD_OP_DELETE
:
806 pr_err("unsupported osd opcode %s\n",
807 ceph_osd_op_name(src
->op
));
813 dst
->op
= cpu_to_le16(src
->op
);
814 dst
->flags
= cpu_to_le32(src
->flags
);
815 dst
->payload_len
= cpu_to_le32(src
->indata_len
);
817 return request_data_len
;
821 * build new request AND message, calculate layout, and adjust file
824 * if the file was recently truncated, we include information about its
825 * old and new size so that the object can be updated appropriately. (we
826 * avoid synchronously deleting truncated objects because it's slow.)
828 * if @do_sync, include a 'startsync' command so that the osd will flush
831 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
832 struct ceph_file_layout
*layout
,
833 struct ceph_vino vino
,
835 unsigned int which
, int num_ops
,
836 int opcode
, int flags
,
837 struct ceph_snap_context
*snapc
,
842 struct ceph_osd_request
*req
;
848 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
&&
849 opcode
!= CEPH_OSD_OP_ZERO
&& opcode
!= CEPH_OSD_OP_TRUNCATE
&&
850 opcode
!= CEPH_OSD_OP_CREATE
&& opcode
!= CEPH_OSD_OP_DELETE
);
852 req
= ceph_osdc_alloc_request(osdc
, snapc
, num_ops
, use_mempool
,
859 req
->r_flags
= flags
;
861 /* calculate max write size */
862 r
= calc_layout(layout
, off
, plen
, &objnum
, &objoff
, &objlen
);
866 if (opcode
== CEPH_OSD_OP_CREATE
|| opcode
== CEPH_OSD_OP_DELETE
) {
867 osd_req_op_init(req
, which
, opcode
, 0);
869 u32 object_size
= le32_to_cpu(layout
->fl_object_size
);
870 u32 object_base
= off
- objoff
;
871 if (!(truncate_seq
== 1 && truncate_size
== -1ULL)) {
872 if (truncate_size
<= object_base
) {
875 truncate_size
-= object_base
;
876 if (truncate_size
> object_size
)
877 truncate_size
= object_size
;
880 osd_req_op_extent_init(req
, which
, opcode
, objoff
, objlen
,
881 truncate_size
, truncate_seq
);
884 req
->r_base_oloc
.pool
= ceph_file_layout_pg_pool(*layout
);
885 ceph_oid_printf(&req
->r_base_oid
, "%llx.%08llx", vino
.ino
, objnum
);
887 r
= ceph_osdc_alloc_messages(req
, GFP_NOFS
);
894 ceph_osdc_put_request(req
);
897 EXPORT_SYMBOL(ceph_osdc_new_request
);
900 * We keep osd requests in an rbtree, sorted by ->r_tid.
902 DEFINE_RB_FUNCS(request
, struct ceph_osd_request
, r_tid
, r_node
)
904 static struct ceph_osd_request
*
905 __lookup_request_ge(struct ceph_osd_client
*osdc
,
908 struct ceph_osd_request
*req
;
909 struct rb_node
*n
= osdc
->requests
.rb_node
;
912 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
913 if (tid
< req
->r_tid
) {
917 } else if (tid
> req
->r_tid
) {
926 static void __kick_linger_request(struct ceph_osd_request
*req
)
928 struct ceph_osd_client
*osdc
= req
->r_osdc
;
929 struct ceph_osd
*osd
= req
->r_osd
;
932 * Linger requests need to be resent with a new tid to avoid
933 * the dup op detection logic on the OSDs. Achieve this with
934 * a re-register dance instead of open-coding.
936 ceph_osdc_get_request(req
);
937 if (!list_empty(&req
->r_linger_item
))
938 __unregister_linger_request(osdc
, req
);
940 __unregister_request(osdc
, req
);
941 __register_request(osdc
, req
);
942 ceph_osdc_put_request(req
);
945 * Unless request has been registered as both normal and
946 * lingering, __unregister{,_linger}_request clears r_osd.
947 * However, here we need to preserve r_osd to make sure we
948 * requeue on the same OSD.
950 WARN_ON(req
->r_osd
|| !osd
);
953 dout("%s requeueing %p tid %llu\n", __func__
, req
, req
->r_tid
);
954 __enqueue_request(req
);
958 * Resubmit requests pending on the given osd.
960 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
961 struct ceph_osd
*osd
)
963 struct ceph_osd_request
*req
, *nreq
;
965 LIST_HEAD(resend_linger
);
968 dout("%s osd%d\n", __func__
, osd
->o_osd
);
969 err
= __reset_osd(osdc
, osd
);
974 * Build up a list of requests to resend by traversing the
975 * osd's list of requests. Requests for a given object are
976 * sent in tid order, and that is also the order they're
977 * kept on this list. Therefore all requests that are in
978 * flight will be found first, followed by all requests that
979 * have not yet been sent. And to resend requests while
980 * preserving this order we will want to put any sent
981 * requests back on the front of the osd client's unsent
984 * So we build a separate ordered list of already-sent
985 * requests for the affected osd and splice it onto the
986 * front of the osd client's unsent list. Once we've seen a
987 * request that has not yet been sent we're done. Those
988 * requests are already sitting right where they belong.
990 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
994 if (!req
->r_linger
) {
995 dout("%s requeueing %p tid %llu\n", __func__
, req
,
997 list_move_tail(&req
->r_req_lru_item
, &resend
);
998 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1000 list_move_tail(&req
->r_req_lru_item
, &resend_linger
);
1003 list_splice(&resend
, &osdc
->req_unsent
);
1006 * Both registered and not yet registered linger requests are
1007 * enqueued with a new tid on the same OSD. We add/move them
1008 * to req_unsent/o_requests at the end to keep things in tid
1011 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
1012 r_linger_osd_item
) {
1013 WARN_ON(!list_empty(&req
->r_req_lru_item
));
1014 __kick_linger_request(req
);
1017 list_for_each_entry_safe(req
, nreq
, &resend_linger
, r_req_lru_item
)
1018 __kick_linger_request(req
);
1022 * If the osd connection drops, we need to resubmit all requests.
1024 static void osd_reset(struct ceph_connection
*con
)
1026 struct ceph_osd
*osd
= con
->private;
1027 struct ceph_osd_client
*osdc
;
1031 dout("osd_reset osd%d\n", osd
->o_osd
);
1033 down_read(&osdc
->map_sem
);
1034 mutex_lock(&osdc
->request_mutex
);
1035 __kick_osd_requests(osdc
, osd
);
1036 __send_queued(osdc
);
1037 mutex_unlock(&osdc
->request_mutex
);
1038 up_read(&osdc
->map_sem
);
1042 * Track open sessions with osds.
1044 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
1046 struct ceph_osd
*osd
;
1048 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
1052 atomic_set(&osd
->o_ref
, 1);
1055 RB_CLEAR_NODE(&osd
->o_node
);
1056 INIT_LIST_HEAD(&osd
->o_requests
);
1057 INIT_LIST_HEAD(&osd
->o_linger_requests
);
1058 INIT_LIST_HEAD(&osd
->o_osd_lru
);
1059 osd
->o_incarnation
= 1;
1061 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
1063 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
1067 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
1069 if (atomic_inc_not_zero(&osd
->o_ref
)) {
1070 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
1071 atomic_read(&osd
->o_ref
));
1074 dout("get_osd %p FAIL\n", osd
);
1079 static void put_osd(struct ceph_osd
*osd
)
1081 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
1082 atomic_read(&osd
->o_ref
) - 1);
1083 if (atomic_dec_and_test(&osd
->o_ref
)) {
1084 if (osd
->o_auth
.authorizer
)
1085 ceph_auth_destroy_authorizer(osd
->o_auth
.authorizer
);
1090 DEFINE_RB_FUNCS(osd
, struct ceph_osd
, o_osd
, o_node
)
1093 * remove an osd from our map
1095 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1097 dout("%s %p osd%d\n", __func__
, osd
, osd
->o_osd
);
1098 WARN_ON(!list_empty(&osd
->o_requests
));
1099 WARN_ON(!list_empty(&osd
->o_linger_requests
));
1101 list_del_init(&osd
->o_osd_lru
);
1102 erase_osd(&osdc
->osds
, osd
);
1105 static void remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1107 dout("%s %p osd%d\n", __func__
, osd
, osd
->o_osd
);
1109 if (!RB_EMPTY_NODE(&osd
->o_node
)) {
1110 ceph_con_close(&osd
->o_con
);
1111 __remove_osd(osdc
, osd
);
1116 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
1117 struct ceph_osd
*osd
)
1119 dout("%s %p\n", __func__
, osd
);
1120 BUG_ON(!list_empty(&osd
->o_osd_lru
));
1122 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
1123 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
;
1126 static void maybe_move_osd_to_lru(struct ceph_osd_client
*osdc
,
1127 struct ceph_osd
*osd
)
1129 dout("%s %p\n", __func__
, osd
);
1131 if (list_empty(&osd
->o_requests
) &&
1132 list_empty(&osd
->o_linger_requests
))
1133 __move_osd_to_lru(osdc
, osd
);
1136 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
1138 dout("__remove_osd_from_lru %p\n", osd
);
1139 if (!list_empty(&osd
->o_osd_lru
))
1140 list_del_init(&osd
->o_osd_lru
);
1146 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1148 struct ceph_entity_addr
*peer_addr
;
1150 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
1151 if (list_empty(&osd
->o_requests
) &&
1152 list_empty(&osd
->o_linger_requests
)) {
1153 remove_osd(osdc
, osd
);
1157 peer_addr
= &osdc
->osdmap
->osd_addr
[osd
->o_osd
];
1158 if (!memcmp(peer_addr
, &osd
->o_con
.peer_addr
, sizeof (*peer_addr
)) &&
1159 !ceph_con_opened(&osd
->o_con
)) {
1160 struct ceph_osd_request
*req
;
1162 dout("osd addr hasn't changed and connection never opened, "
1163 "letting msgr retry\n");
1164 /* touch each r_stamp for handle_timeout()'s benfit */
1165 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
1166 req
->r_stamp
= jiffies
;
1171 ceph_con_close(&osd
->o_con
);
1172 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
, peer_addr
);
1173 osd
->o_incarnation
++;
1178 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
1180 schedule_delayed_work(&osdc
->timeout_work
,
1181 osdc
->client
->options
->osd_keepalive_timeout
);
1184 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
1186 cancel_delayed_work(&osdc
->timeout_work
);
1190 * Register request, assign tid. If this is the first request, set up
1191 * the timeout event.
1193 static void __register_request(struct ceph_osd_client
*osdc
,
1194 struct ceph_osd_request
*req
)
1196 req
->r_tid
= ++osdc
->last_tid
;
1197 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1198 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
1199 insert_request(&osdc
->requests
, req
);
1200 ceph_osdc_get_request(req
);
1201 osdc
->num_requests
++;
1202 if (osdc
->num_requests
== 1) {
1203 dout(" first request, scheduling timeout\n");
1204 __schedule_osd_timeout(osdc
);
1209 * called under osdc->request_mutex
1211 static void __unregister_request(struct ceph_osd_client
*osdc
,
1212 struct ceph_osd_request
*req
)
1214 if (RB_EMPTY_NODE(&req
->r_node
)) {
1215 dout("__unregister_request %p tid %lld not registered\n",
1220 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
1221 erase_request(&osdc
->requests
, req
);
1222 osdc
->num_requests
--;
1225 /* make sure the original request isn't in flight. */
1226 ceph_msg_revoke(req
->r_request
);
1228 list_del_init(&req
->r_osd_item
);
1229 maybe_move_osd_to_lru(osdc
, req
->r_osd
);
1230 if (list_empty(&req
->r_linger_osd_item
))
1234 list_del_init(&req
->r_req_lru_item
);
1235 ceph_osdc_put_request(req
);
1237 if (osdc
->num_requests
== 0) {
1238 dout(" no requests, canceling timeout\n");
1239 __cancel_osd_timeout(osdc
);
1244 * Cancel a previously queued request message
1246 static void __cancel_request(struct ceph_osd_request
*req
)
1248 if (req
->r_sent
&& req
->r_osd
) {
1249 ceph_msg_revoke(req
->r_request
);
1254 static void __register_linger_request(struct ceph_osd_client
*osdc
,
1255 struct ceph_osd_request
*req
)
1257 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
1258 WARN_ON(!req
->r_linger
);
1260 ceph_osdc_get_request(req
);
1261 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
1263 list_add_tail(&req
->r_linger_osd_item
,
1264 &req
->r_osd
->o_linger_requests
);
1267 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
1268 struct ceph_osd_request
*req
)
1270 WARN_ON(!req
->r_linger
);
1272 if (list_empty(&req
->r_linger_item
)) {
1273 dout("%s %p tid %llu not registered\n", __func__
, req
,
1278 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
1279 list_del_init(&req
->r_linger_item
);
1282 list_del_init(&req
->r_linger_osd_item
);
1283 maybe_move_osd_to_lru(osdc
, req
->r_osd
);
1284 if (list_empty(&req
->r_osd_item
))
1287 ceph_osdc_put_request(req
);
1290 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
1291 struct ceph_osd_request
*req
)
1293 if (!req
->r_linger
) {
1294 dout("set_request_linger %p\n", req
);
1298 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
1300 static bool __pool_full(struct ceph_pg_pool_info
*pi
)
1302 return pi
->flags
& CEPH_POOL_FLAG_FULL
;
1306 * Returns whether a request should be blocked from being sent
1307 * based on the current osdmap and osd_client settings.
1309 * Caller should hold map_sem for read.
1311 static bool __req_should_be_paused(struct ceph_osd_client
*osdc
,
1312 struct ceph_osd_request
*req
)
1314 bool pauserd
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSERD
);
1315 bool pausewr
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSEWR
) ||
1316 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
);
1317 return (req
->r_flags
& CEPH_OSD_FLAG_READ
&& pauserd
) ||
1318 (req
->r_flags
& CEPH_OSD_FLAG_WRITE
&& pausewr
);
1321 static bool target_should_be_paused(struct ceph_osd_client
*osdc
,
1322 const struct ceph_osd_request_target
*t
,
1323 struct ceph_pg_pool_info
*pi
)
1325 bool pauserd
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSERD
);
1326 bool pausewr
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSEWR
) ||
1327 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
) ||
1330 WARN_ON(pi
->id
!= t
->base_oloc
.pool
);
1331 return (t
->flags
& CEPH_OSD_FLAG_READ
&& pauserd
) ||
1332 (t
->flags
& CEPH_OSD_FLAG_WRITE
&& pausewr
);
1336 * Calculate mapping of a request to a PG. Takes tiering into account.
1338 static int __calc_request_pg(struct ceph_osdmap
*osdmap
,
1339 struct ceph_osd_request
*req
,
1340 struct ceph_pg
*pg_out
)
1342 bool need_check_tiering
;
1344 need_check_tiering
= false;
1345 if (req
->r_target_oloc
.pool
== -1) {
1346 req
->r_target_oloc
= req
->r_base_oloc
; /* struct */
1347 need_check_tiering
= true;
1349 if (ceph_oid_empty(&req
->r_target_oid
)) {
1350 ceph_oid_copy(&req
->r_target_oid
, &req
->r_base_oid
);
1351 need_check_tiering
= true;
1354 if (need_check_tiering
&&
1355 (req
->r_flags
& CEPH_OSD_FLAG_IGNORE_OVERLAY
) == 0) {
1356 struct ceph_pg_pool_info
*pi
;
1358 pi
= ceph_pg_pool_by_id(osdmap
, req
->r_target_oloc
.pool
);
1360 if ((req
->r_flags
& CEPH_OSD_FLAG_READ
) &&
1362 req
->r_target_oloc
.pool
= pi
->read_tier
;
1363 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) &&
1364 pi
->write_tier
>= 0)
1365 req
->r_target_oloc
.pool
= pi
->write_tier
;
1367 /* !pi is caught in ceph_oloc_oid_to_pg() */
1370 return ceph_object_locator_to_pg(osdmap
, &req
->r_target_oid
,
1371 &req
->r_target_oloc
, pg_out
);
1374 enum calc_target_result
{
1375 CALC_TARGET_NO_ACTION
= 0,
1376 CALC_TARGET_NEED_RESEND
,
1377 CALC_TARGET_POOL_DNE
,
1380 static enum calc_target_result
calc_target(struct ceph_osd_client
*osdc
,
1381 struct ceph_osd_request_target
*t
,
1382 u32
*last_force_resend
,
1385 struct ceph_pg_pool_info
*pi
;
1386 struct ceph_pg pgid
, last_pgid
;
1387 struct ceph_osds up
, acting
;
1388 bool force_resend
= false;
1389 bool need_check_tiering
= false;
1390 bool need_resend
= false;
1391 bool sort_bitwise
= ceph_osdmap_flag(osdc
->osdmap
,
1392 CEPH_OSDMAP_SORTBITWISE
);
1393 enum calc_target_result ct_res
;
1396 pi
= ceph_pg_pool_by_id(osdc
->osdmap
, t
->base_oloc
.pool
);
1398 t
->osd
= CEPH_HOMELESS_OSD
;
1399 ct_res
= CALC_TARGET_POOL_DNE
;
1403 if (osdc
->osdmap
->epoch
== pi
->last_force_request_resend
) {
1404 if (last_force_resend
&&
1405 *last_force_resend
< pi
->last_force_request_resend
) {
1406 *last_force_resend
= pi
->last_force_request_resend
;
1407 force_resend
= true;
1408 } else if (!last_force_resend
) {
1409 force_resend
= true;
1412 if (ceph_oid_empty(&t
->target_oid
) || force_resend
) {
1413 ceph_oid_copy(&t
->target_oid
, &t
->base_oid
);
1414 need_check_tiering
= true;
1416 if (ceph_oloc_empty(&t
->target_oloc
) || force_resend
) {
1417 ceph_oloc_copy(&t
->target_oloc
, &t
->base_oloc
);
1418 need_check_tiering
= true;
1421 if (need_check_tiering
&&
1422 (t
->flags
& CEPH_OSD_FLAG_IGNORE_OVERLAY
) == 0) {
1423 if (t
->flags
& CEPH_OSD_FLAG_READ
&& pi
->read_tier
>= 0)
1424 t
->target_oloc
.pool
= pi
->read_tier
;
1425 if (t
->flags
& CEPH_OSD_FLAG_WRITE
&& pi
->write_tier
>= 0)
1426 t
->target_oloc
.pool
= pi
->write_tier
;
1429 ret
= ceph_object_locator_to_pg(osdc
->osdmap
, &t
->target_oid
,
1430 &t
->target_oloc
, &pgid
);
1432 WARN_ON(ret
!= -ENOENT
);
1433 t
->osd
= CEPH_HOMELESS_OSD
;
1434 ct_res
= CALC_TARGET_POOL_DNE
;
1437 last_pgid
.pool
= pgid
.pool
;
1438 last_pgid
.seed
= ceph_stable_mod(pgid
.seed
, t
->pg_num
, t
->pg_num_mask
);
1440 ceph_pg_to_up_acting_osds(osdc
->osdmap
, &pgid
, &up
, &acting
);
1442 ceph_is_new_interval(&t
->acting
,
1455 force_resend
= true;
1457 if (t
->paused
&& !target_should_be_paused(osdc
, t
, pi
)) {
1462 if (ceph_pg_compare(&t
->pgid
, &pgid
) ||
1463 ceph_osds_changed(&t
->acting
, &acting
, any_change
) ||
1465 t
->pgid
= pgid
; /* struct */
1466 ceph_osds_copy(&t
->acting
, &acting
);
1467 ceph_osds_copy(&t
->up
, &up
);
1469 t
->min_size
= pi
->min_size
;
1470 t
->pg_num
= pi
->pg_num
;
1471 t
->pg_num_mask
= pi
->pg_num_mask
;
1472 t
->sort_bitwise
= sort_bitwise
;
1474 t
->osd
= acting
.primary
;
1478 ct_res
= need_resend
? CALC_TARGET_NEED_RESEND
: CALC_TARGET_NO_ACTION
;
1480 dout("%s t %p -> ct_res %d osd %d\n", __func__
, t
, ct_res
, t
->osd
);
1484 static void __enqueue_request(struct ceph_osd_request
*req
)
1486 struct ceph_osd_client
*osdc
= req
->r_osdc
;
1488 dout("%s %p tid %llu to osd%d\n", __func__
, req
, req
->r_tid
,
1489 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1492 __remove_osd_from_lru(req
->r_osd
);
1493 list_add_tail(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1494 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1496 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1501 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1502 * (as needed), and set the request r_osd appropriately. If there is
1503 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1504 * (unsent, homeless) or leave on in-flight lru.
1506 * Return 0 if unchanged, 1 if changed, or negative on error.
1508 * Caller should hold map_sem for read and request_mutex.
1510 static int __map_request(struct ceph_osd_client
*osdc
,
1511 struct ceph_osd_request
*req
, int force_resend
)
1513 struct ceph_pg pgid
;
1514 struct ceph_osds up
, acting
;
1518 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
1520 err
= __calc_request_pg(osdc
->osdmap
, req
, &pgid
);
1522 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1527 ceph_pg_to_up_acting_osds(osdc
->osdmap
, &pgid
, &up
, &acting
);
1529 was_paused
= req
->r_paused
;
1530 req
->r_paused
= __req_should_be_paused(osdc
, req
);
1531 if (was_paused
&& !req
->r_paused
)
1534 if ((!force_resend
&&
1535 req
->r_osd
&& req
->r_osd
->o_osd
== acting
.primary
&&
1536 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
1537 req
->r_num_pg_osds
== acting
.size
&&
1538 memcmp(req
->r_pg_osds
, acting
.osds
,
1539 acting
.size
* sizeof(acting
.osds
[0])) == 0) ||
1540 (req
->r_osd
== NULL
&& acting
.primary
== -1) ||
1542 return 0; /* no change */
1544 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1545 req
->r_tid
, pgid
.pool
, pgid
.seed
, acting
.primary
,
1546 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1548 /* record full pg acting set */
1549 memcpy(req
->r_pg_osds
, acting
.osds
,
1550 acting
.size
* sizeof(acting
.osds
[0]));
1551 req
->r_num_pg_osds
= acting
.size
;
1554 __cancel_request(req
);
1555 list_del_init(&req
->r_osd_item
);
1556 list_del_init(&req
->r_linger_osd_item
);
1560 req
->r_osd
= lookup_osd(&osdc
->osds
, acting
.primary
);
1561 if (!req
->r_osd
&& acting
.primary
>= 0) {
1563 req
->r_osd
= create_osd(osdc
, acting
.primary
);
1565 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1569 dout("map_request osd %p is osd%d\n", req
->r_osd
,
1571 insert_osd(&osdc
->osds
, req
->r_osd
);
1573 ceph_con_open(&req
->r_osd
->o_con
,
1574 CEPH_ENTITY_TYPE_OSD
, acting
.primary
,
1575 &osdc
->osdmap
->osd_addr
[acting
.primary
]);
1578 __enqueue_request(req
);
1579 err
= 1; /* osd or pg changed */
1586 * caller should hold map_sem (for read) and request_mutex
1588 static void __send_request(struct ceph_osd_client
*osdc
,
1589 struct ceph_osd_request
*req
)
1593 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1594 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
,
1595 (unsigned long long)req
->r_pgid
.pool
, req
->r_pgid
.seed
);
1597 /* fill in message content that changes each time we send it */
1598 put_unaligned_le32(osdc
->osdmap
->epoch
, req
->r_request_osdmap_epoch
);
1599 put_unaligned_le32(req
->r_flags
, req
->r_request_flags
);
1600 put_unaligned_le64(req
->r_target_oloc
.pool
, req
->r_request_pool
);
1601 p
= req
->r_request_pgid
;
1602 ceph_encode_64(&p
, req
->r_pgid
.pool
);
1603 ceph_encode_32(&p
, req
->r_pgid
.seed
);
1604 put_unaligned_le64(1, req
->r_request_attempts
); /* FIXME */
1605 memcpy(req
->r_request_reassert_version
, &req
->r_reassert_version
,
1606 sizeof(req
->r_reassert_version
));
1608 req
->r_stamp
= jiffies
;
1609 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1611 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1613 req
->r_sent
= req
->r_osd
->o_incarnation
;
1615 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1619 * Send any requests in the queue (req_unsent).
1621 static void __send_queued(struct ceph_osd_client
*osdc
)
1623 struct ceph_osd_request
*req
, *tmp
;
1625 dout("__send_queued\n");
1626 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
)
1627 __send_request(osdc
, req
);
1631 * Caller should hold map_sem for read and request_mutex.
1633 static int __ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1634 struct ceph_osd_request
*req
,
1639 __register_request(osdc
, req
);
1641 req
->r_got_reply
= 0;
1642 rc
= __map_request(osdc
, req
, 0);
1645 dout("osdc_start_request failed map, "
1646 " will retry %lld\n", req
->r_tid
);
1649 __unregister_request(osdc
, req
);
1654 if (req
->r_osd
== NULL
) {
1655 dout("send_request %p no up osds in pg\n", req
);
1656 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1658 __send_queued(osdc
);
1665 * Timeout callback, called every N seconds when 1 or more osd
1666 * requests has been active for more than N seconds. When this
1667 * happens, we ping all OSDs with requests who have timed out to
1668 * ensure any communications channel reset is detected. Reset the
1669 * request timeouts another N seconds in the future as we go.
1670 * Reschedule the timeout event another N seconds in future (unless
1671 * there are no open requests).
1673 static void handle_timeout(struct work_struct
*work
)
1675 struct ceph_osd_client
*osdc
=
1676 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1677 struct ceph_options
*opts
= osdc
->client
->options
;
1678 struct ceph_osd_request
*req
;
1679 struct ceph_osd
*osd
;
1680 struct list_head slow_osds
;
1682 down_read(&osdc
->map_sem
);
1684 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1686 mutex_lock(&osdc
->request_mutex
);
1689 * ping osds that are a bit slow. this ensures that if there
1690 * is a break in the TCP connection we will notice, and reopen
1691 * a connection with that osd (from the fault callback).
1693 INIT_LIST_HEAD(&slow_osds
);
1694 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1695 if (time_before(jiffies
,
1696 req
->r_stamp
+ opts
->osd_keepalive_timeout
))
1701 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1702 req
->r_tid
, osd
->o_osd
);
1703 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1705 while (!list_empty(&slow_osds
)) {
1706 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1708 list_del_init(&osd
->o_keepalive_item
);
1709 ceph_con_keepalive(&osd
->o_con
);
1712 __schedule_osd_timeout(osdc
);
1713 __send_queued(osdc
);
1714 mutex_unlock(&osdc
->request_mutex
);
1715 up_read(&osdc
->map_sem
);
1718 static void handle_osds_timeout(struct work_struct
*work
)
1720 struct ceph_osd_client
*osdc
=
1721 container_of(work
, struct ceph_osd_client
,
1722 osds_timeout_work
.work
);
1723 unsigned long delay
= osdc
->client
->options
->osd_idle_ttl
/ 4;
1724 struct ceph_osd
*osd
, *nosd
;
1726 dout("%s osdc %p\n", __func__
, osdc
);
1727 down_read(&osdc
->map_sem
);
1728 mutex_lock(&osdc
->request_mutex
);
1730 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
1731 if (time_before(jiffies
, osd
->lru_ttl
))
1734 remove_osd(osdc
, osd
);
1737 mutex_unlock(&osdc
->request_mutex
);
1738 up_read(&osdc
->map_sem
);
1739 schedule_delayed_work(&osdc
->osds_timeout_work
,
1740 round_jiffies_relative(delay
));
1743 static int ceph_oloc_decode(void **p
, void *end
,
1744 struct ceph_object_locator
*oloc
)
1746 u8 struct_v
, struct_cv
;
1751 ceph_decode_need(p
, end
, 1 + 1 + 4, e_inval
);
1752 struct_v
= ceph_decode_8(p
);
1753 struct_cv
= ceph_decode_8(p
);
1755 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1756 struct_v
, struct_cv
);
1759 if (struct_cv
> 6) {
1760 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1761 struct_v
, struct_cv
);
1764 len
= ceph_decode_32(p
);
1765 ceph_decode_need(p
, end
, len
, e_inval
);
1766 struct_end
= *p
+ len
;
1768 oloc
->pool
= ceph_decode_64(p
);
1769 *p
+= 4; /* skip preferred */
1771 len
= ceph_decode_32(p
);
1773 pr_warn("ceph_object_locator::key is set\n");
1777 if (struct_v
>= 5) {
1778 len
= ceph_decode_32(p
);
1780 pr_warn("ceph_object_locator::nspace is set\n");
1785 if (struct_v
>= 6) {
1786 s64 hash
= ceph_decode_64(p
);
1788 pr_warn("ceph_object_locator::hash is set\n");
1803 static int ceph_redirect_decode(void **p
, void *end
,
1804 struct ceph_request_redirect
*redir
)
1806 u8 struct_v
, struct_cv
;
1811 ceph_decode_need(p
, end
, 1 + 1 + 4, e_inval
);
1812 struct_v
= ceph_decode_8(p
);
1813 struct_cv
= ceph_decode_8(p
);
1814 if (struct_cv
> 1) {
1815 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1816 struct_v
, struct_cv
);
1819 len
= ceph_decode_32(p
);
1820 ceph_decode_need(p
, end
, len
, e_inval
);
1821 struct_end
= *p
+ len
;
1823 ret
= ceph_oloc_decode(p
, end
, &redir
->oloc
);
1827 len
= ceph_decode_32(p
);
1829 pr_warn("ceph_request_redirect::object_name is set\n");
1833 len
= ceph_decode_32(p
);
1834 *p
+= len
; /* skip osd_instructions */
1846 static void complete_request(struct ceph_osd_request
*req
)
1848 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1852 * handle osd op reply. either call the callback if it is specified,
1853 * or do the completion to wake up the waiting thread.
1855 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1858 struct ceph_osd_request
*req
;
1859 struct ceph_request_redirect redir
;
1862 unsigned int numops
;
1863 int payload_len
, flags
;
1869 u64 reassert_version
;
1871 int already_completed
;
1876 tid
= le64_to_cpu(msg
->hdr
.tid
);
1877 dout("handle_reply %p tid %llu\n", msg
, tid
);
1879 p
= msg
->front
.iov_base
;
1880 end
= p
+ msg
->front
.iov_len
;
1882 ceph_decode_need(&p
, end
, 4, bad
);
1883 object_len
= ceph_decode_32(&p
);
1884 ceph_decode_need(&p
, end
, object_len
, bad
);
1887 err
= ceph_decode_pgid(&p
, end
, &pg
);
1891 ceph_decode_need(&p
, end
, 8 + 4 + 4 + 8 + 4, bad
);
1892 flags
= ceph_decode_64(&p
);
1893 result
= ceph_decode_32(&p
);
1894 reassert_epoch
= ceph_decode_32(&p
);
1895 reassert_version
= ceph_decode_64(&p
);
1896 osdmap_epoch
= ceph_decode_32(&p
);
1899 down_read(&osdc
->map_sem
);
1900 mutex_lock(&osdc
->request_mutex
);
1901 req
= lookup_request(&osdc
->requests
, tid
);
1903 dout("handle_reply tid %llu dne\n", tid
);
1906 ceph_osdc_get_request(req
);
1908 dout("handle_reply %p tid %llu req %p result %d\n", msg
, tid
,
1911 ceph_decode_need(&p
, end
, 4, bad_put
);
1912 numops
= ceph_decode_32(&p
);
1913 if (numops
> CEPH_OSD_MAX_OPS
)
1915 if (numops
!= req
->r_num_ops
)
1918 ceph_decode_need(&p
, end
, numops
* sizeof(struct ceph_osd_op
), bad_put
);
1919 for (i
= 0; i
< numops
; i
++) {
1920 struct ceph_osd_op
*op
= p
;
1923 len
= le32_to_cpu(op
->payload_len
);
1924 req
->r_ops
[i
].outdata_len
= len
;
1925 dout(" op %d has %d bytes\n", i
, len
);
1929 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1930 if (payload_len
!= bytes
) {
1931 pr_warn("sum of op payload lens %d != data_len %d\n",
1932 payload_len
, bytes
);
1936 ceph_decode_need(&p
, end
, 4 + numops
* 4, bad_put
);
1937 retry_attempt
= ceph_decode_32(&p
);
1938 for (i
= 0; i
< numops
; i
++)
1939 req
->r_ops
[i
].rval
= ceph_decode_32(&p
);
1941 if (le16_to_cpu(msg
->hdr
.version
) >= 6) {
1942 p
+= 8 + 4; /* skip replay_version */
1943 p
+= 8; /* skip user_version */
1945 if (le16_to_cpu(msg
->hdr
.version
) >= 7)
1946 ceph_decode_8_safe(&p
, end
, decode_redir
, bad_put
);
1954 err
= ceph_redirect_decode(&p
, end
, &redir
);
1958 redir
.oloc
.pool
= -1;
1961 if (!ceph_oloc_empty(&redir
.oloc
)) {
1962 dout("redirect pool %lld\n", redir
.oloc
.pool
);
1964 __unregister_request(osdc
, req
);
1966 ceph_oloc_copy(&req
->r_target_oloc
, &redir
.oloc
);
1969 * Start redirect requests with nofail=true. If
1970 * mapping fails, request will end up on the notarget
1971 * list, waiting for the new osdmap (which can take
1972 * a while), even though the original request mapped
1973 * successfully. In the future we might want to follow
1974 * original request's nofail setting here.
1976 err
= __ceph_osdc_start_request(osdc
, req
, true);
1982 already_completed
= req
->r_got_reply
;
1983 if (!req
->r_got_reply
) {
1984 req
->r_result
= result
;
1985 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1987 if (req
->r_result
== 0)
1988 req
->r_result
= bytes
;
1990 /* in case this is a write and we need to replay, */
1991 req
->r_reassert_version
.epoch
= cpu_to_le32(reassert_epoch
);
1992 req
->r_reassert_version
.version
= cpu_to_le64(reassert_version
);
1994 req
->r_got_reply
= 1;
1995 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1996 dout("handle_reply tid %llu dup ack\n", tid
);
2000 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
2002 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
2003 __register_linger_request(osdc
, req
);
2005 /* either this is a read, or we got the safe response */
2007 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
2008 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
2009 __unregister_request(osdc
, req
);
2011 mutex_unlock(&osdc
->request_mutex
);
2012 up_read(&osdc
->map_sem
);
2014 if (!already_completed
) {
2015 if (req
->r_unsafe_callback
&&
2016 result
>= 0 && !(flags
& CEPH_OSD_FLAG_ONDISK
))
2017 req
->r_unsafe_callback(req
, true);
2018 if (req
->r_callback
)
2019 req
->r_callback(req
, msg
);
2021 complete_all(&req
->r_completion
);
2024 if (flags
& CEPH_OSD_FLAG_ONDISK
) {
2025 if (req
->r_unsafe_callback
&& already_completed
)
2026 req
->r_unsafe_callback(req
, false);
2027 complete_request(req
);
2031 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
2032 ceph_osdc_put_request(req
);
2035 mutex_unlock(&osdc
->request_mutex
);
2036 up_read(&osdc
->map_sem
);
2040 req
->r_result
= -EIO
;
2041 __unregister_request(osdc
, req
);
2042 if (req
->r_callback
)
2043 req
->r_callback(req
, msg
);
2045 complete_all(&req
->r_completion
);
2046 complete_request(req
);
2047 ceph_osdc_put_request(req
);
2049 mutex_unlock(&osdc
->request_mutex
);
2050 up_read(&osdc
->map_sem
);
2052 pr_err("corrupt osd_op_reply got %d %d\n",
2053 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
));
2057 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
2059 struct rb_node
*p
, *n
;
2061 dout("%s %p\n", __func__
, osdc
);
2062 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
2063 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
2066 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
2067 memcmp(&osd
->o_con
.peer_addr
,
2068 ceph_osd_addr(osdc
->osdmap
,
2070 sizeof(struct ceph_entity_addr
)) != 0)
2071 __reset_osd(osdc
, osd
);
2076 * Requeue requests whose mapping to an OSD has changed. If requests map to
2077 * no osd, request a new map.
2079 * Caller should hold map_sem for read.
2081 static void kick_requests(struct ceph_osd_client
*osdc
, bool force_resend
,
2082 bool force_resend_writes
)
2084 struct ceph_osd_request
*req
, *nreq
;
2088 bool force_resend_req
;
2090 dout("kick_requests %s %s\n", force_resend
? " (force resend)" : "",
2091 force_resend_writes
? " (force resend writes)" : "");
2092 mutex_lock(&osdc
->request_mutex
);
2093 for (p
= rb_first(&osdc
->requests
); p
; ) {
2094 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
2098 * For linger requests that have not yet been
2099 * registered, move them to the linger list; they'll
2100 * be sent to the osd in the loop below. Unregister
2101 * the request before re-registering it as a linger
2102 * request to ensure the __map_request() below
2103 * will decide it needs to be sent.
2105 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
2106 dout("%p tid %llu restart on osd%d\n",
2108 req
->r_osd
? req
->r_osd
->o_osd
: -1);
2109 ceph_osdc_get_request(req
);
2110 __unregister_request(osdc
, req
);
2111 __register_linger_request(osdc
, req
);
2112 ceph_osdc_put_request(req
);
2116 force_resend_req
= force_resend
||
2117 (force_resend_writes
&&
2118 req
->r_flags
& CEPH_OSD_FLAG_WRITE
);
2119 err
= __map_request(osdc
, req
, force_resend_req
);
2121 continue; /* error */
2122 if (req
->r_osd
== NULL
) {
2123 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
2124 needmap
++; /* request a newer map */
2125 } else if (err
> 0) {
2126 if (!req
->r_linger
) {
2127 dout("%p tid %llu requeued on osd%d\n", req
,
2129 req
->r_osd
? req
->r_osd
->o_osd
: -1);
2130 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
2135 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
2137 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
2139 err
= __map_request(osdc
, req
,
2140 force_resend
|| force_resend_writes
);
2141 dout("__map_request returned %d\n", err
);
2143 continue; /* hrm! */
2144 if (req
->r_osd
== NULL
|| err
> 0) {
2145 if (req
->r_osd
== NULL
) {
2146 dout("lingering %p tid %llu maps to no osd\n",
2149 * A homeless lingering request makes
2150 * no sense, as it's job is to keep
2151 * a particular OSD connection open.
2152 * Request a newer map and kick the
2153 * request, knowing that it won't be
2154 * resent until we actually get a map
2155 * that can tell us where to send it.
2160 dout("kicking lingering %p tid %llu osd%d\n", req
,
2161 req
->r_tid
, req
->r_osd
? req
->r_osd
->o_osd
: -1);
2162 __register_request(osdc
, req
);
2163 __unregister_linger_request(osdc
, req
);
2166 reset_changed_osds(osdc
);
2167 mutex_unlock(&osdc
->request_mutex
);
2170 dout("%d requests for down osds, need new map\n", needmap
);
2171 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
2177 * Process updated osd map.
2179 * The message contains any number of incremental and full maps, normally
2180 * indicating some sort of topology change in the cluster. Kick requests
2181 * off to different OSDs as needed.
2183 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
2185 void *p
, *end
, *next
;
2186 u32 nr_maps
, maplen
;
2188 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
2190 struct ceph_fsid fsid
;
2193 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
2194 p
= msg
->front
.iov_base
;
2195 end
= p
+ msg
->front
.iov_len
;
2198 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
2199 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
2200 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
2203 down_write(&osdc
->map_sem
);
2205 was_full
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
);
2207 /* incremental maps */
2208 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
2209 dout(" %d inc maps\n", nr_maps
);
2210 while (nr_maps
> 0) {
2211 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2212 epoch
= ceph_decode_32(&p
);
2213 maplen
= ceph_decode_32(&p
);
2214 ceph_decode_need(&p
, end
, maplen
, bad
);
2216 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
2217 dout("applying incremental map %u len %d\n",
2219 newmap
= osdmap_apply_incremental(&p
, next
,
2221 if (IS_ERR(newmap
)) {
2222 err
= PTR_ERR(newmap
);
2226 if (newmap
!= osdc
->osdmap
) {
2227 ceph_osdmap_destroy(osdc
->osdmap
);
2228 osdc
->osdmap
= newmap
;
2230 was_full
= was_full
||
2231 ceph_osdmap_flag(osdc
->osdmap
,
2233 kick_requests(osdc
, 0, was_full
);
2235 dout("ignoring incremental map %u len %d\n",
2245 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
2246 dout(" %d full maps\n", nr_maps
);
2248 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2249 epoch
= ceph_decode_32(&p
);
2250 maplen
= ceph_decode_32(&p
);
2251 ceph_decode_need(&p
, end
, maplen
, bad
);
2253 dout("skipping non-latest full map %u len %d\n",
2255 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
2256 dout("skipping full map %u len %d, "
2257 "older than our %u\n", epoch
, maplen
,
2258 osdc
->osdmap
->epoch
);
2260 int skipped_map
= 0;
2262 dout("taking full map %u len %d\n", epoch
, maplen
);
2263 newmap
= ceph_osdmap_decode(&p
, p
+maplen
);
2264 if (IS_ERR(newmap
)) {
2265 err
= PTR_ERR(newmap
);
2269 oldmap
= osdc
->osdmap
;
2270 osdc
->osdmap
= newmap
;
2272 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
2274 ceph_osdmap_destroy(oldmap
);
2276 was_full
= was_full
||
2277 ceph_osdmap_flag(osdc
->osdmap
,
2279 kick_requests(osdc
, skipped_map
, was_full
);
2288 downgrade_write(&osdc
->map_sem
);
2289 ceph_monc_got_map(&osdc
->client
->monc
, CEPH_SUB_OSDMAP
,
2290 osdc
->osdmap
->epoch
);
2293 * subscribe to subsequent osdmap updates if full to ensure
2294 * we find out when we are no longer full and stop returning
2297 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
) ||
2298 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSERD
) ||
2299 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSEWR
))
2300 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
2302 mutex_lock(&osdc
->request_mutex
);
2303 __send_queued(osdc
);
2304 mutex_unlock(&osdc
->request_mutex
);
2305 up_read(&osdc
->map_sem
);
2306 wake_up_all(&osdc
->client
->auth_wq
);
2310 pr_err("osdc handle_map corrupt msg\n");
2312 up_write(&osdc
->map_sem
);
2316 * watch/notify callback event infrastructure
2318 * These callbacks are used both for watch and notify operations.
2320 static void __release_event(struct kref
*kref
)
2322 struct ceph_osd_event
*event
=
2323 container_of(kref
, struct ceph_osd_event
, kref
);
2325 dout("__release_event %p\n", event
);
2329 static void get_event(struct ceph_osd_event
*event
)
2331 kref_get(&event
->kref
);
2334 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
2336 kref_put(&event
->kref
, __release_event
);
2338 EXPORT_SYMBOL(ceph_osdc_put_event
);
2340 static void __insert_event(struct ceph_osd_client
*osdc
,
2341 struct ceph_osd_event
*new)
2343 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
2344 struct rb_node
*parent
= NULL
;
2345 struct ceph_osd_event
*event
= NULL
;
2349 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
2350 if (new->cookie
< event
->cookie
)
2352 else if (new->cookie
> event
->cookie
)
2353 p
= &(*p
)->rb_right
;
2358 rb_link_node(&new->node
, parent
, p
);
2359 rb_insert_color(&new->node
, &osdc
->event_tree
);
2362 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
2365 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
2366 struct rb_node
*parent
= NULL
;
2367 struct ceph_osd_event
*event
= NULL
;
2371 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
2372 if (cookie
< event
->cookie
)
2374 else if (cookie
> event
->cookie
)
2375 p
= &(*p
)->rb_right
;
2382 static void __remove_event(struct ceph_osd_event
*event
)
2384 struct ceph_osd_client
*osdc
= event
->osdc
;
2386 if (!RB_EMPTY_NODE(&event
->node
)) {
2387 dout("__remove_event removed %p\n", event
);
2388 rb_erase(&event
->node
, &osdc
->event_tree
);
2389 ceph_osdc_put_event(event
);
2391 dout("__remove_event didn't remove %p\n", event
);
2395 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
2396 void (*event_cb
)(u64
, u64
, u8
, void *),
2397 void *data
, struct ceph_osd_event
**pevent
)
2399 struct ceph_osd_event
*event
;
2401 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
2405 dout("create_event %p\n", event
);
2406 event
->cb
= event_cb
;
2407 event
->one_shot
= 0;
2410 INIT_LIST_HEAD(&event
->osd_node
);
2411 RB_CLEAR_NODE(&event
->node
);
2412 kref_init(&event
->kref
); /* one ref for us */
2413 kref_get(&event
->kref
); /* one ref for the caller */
2415 spin_lock(&osdc
->event_lock
);
2416 event
->cookie
= ++osdc
->event_count
;
2417 __insert_event(osdc
, event
);
2418 spin_unlock(&osdc
->event_lock
);
2423 EXPORT_SYMBOL(ceph_osdc_create_event
);
2425 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
2427 struct ceph_osd_client
*osdc
= event
->osdc
;
2429 dout("cancel_event %p\n", event
);
2430 spin_lock(&osdc
->event_lock
);
2431 __remove_event(event
);
2432 spin_unlock(&osdc
->event_lock
);
2433 ceph_osdc_put_event(event
); /* caller's */
2435 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
2438 static void do_event_work(struct work_struct
*work
)
2440 struct ceph_osd_event_work
*event_work
=
2441 container_of(work
, struct ceph_osd_event_work
, work
);
2442 struct ceph_osd_event
*event
= event_work
->event
;
2443 u64 ver
= event_work
->ver
;
2444 u64 notify_id
= event_work
->notify_id
;
2445 u8 opcode
= event_work
->opcode
;
2447 dout("do_event_work completing %p\n", event
);
2448 event
->cb(ver
, notify_id
, opcode
, event
->data
);
2449 dout("do_event_work completed %p\n", event
);
2450 ceph_osdc_put_event(event
);
2456 * Process osd watch notifications
2458 static void handle_watch_notify(struct ceph_osd_client
*osdc
,
2459 struct ceph_msg
*msg
)
2463 u64 cookie
, ver
, notify_id
;
2465 struct ceph_osd_event
*event
;
2466 struct ceph_osd_event_work
*event_work
;
2468 p
= msg
->front
.iov_base
;
2469 end
= p
+ msg
->front
.iov_len
;
2471 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
2472 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
2473 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
2474 ceph_decode_64_safe(&p
, end
, ver
, bad
);
2475 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
2477 spin_lock(&osdc
->event_lock
);
2478 event
= __find_event(osdc
, cookie
);
2480 BUG_ON(event
->one_shot
);
2483 spin_unlock(&osdc
->event_lock
);
2484 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2485 cookie
, ver
, event
);
2487 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
2489 pr_err("couldn't allocate event_work\n");
2490 ceph_osdc_put_event(event
);
2493 INIT_WORK(&event_work
->work
, do_event_work
);
2494 event_work
->event
= event
;
2495 event_work
->ver
= ver
;
2496 event_work
->notify_id
= notify_id
;
2497 event_work
->opcode
= opcode
;
2499 queue_work(osdc
->notify_wq
, &event_work
->work
);
2505 pr_err("osdc handle_watch_notify corrupt msg\n");
2509 * build new request AND message
2512 void ceph_osdc_build_request(struct ceph_osd_request
*req
, u64 off
,
2513 struct ceph_snap_context
*snapc
, u64 snap_id
,
2514 struct timespec
*mtime
)
2516 struct ceph_msg
*msg
= req
->r_request
;
2519 int flags
= req
->r_flags
;
2523 req
->r_snapid
= snap_id
;
2524 WARN_ON(snapc
!= req
->r_snapc
);
2526 /* encode request */
2527 msg
->hdr
.version
= cpu_to_le16(4);
2529 p
= msg
->front
.iov_base
;
2530 ceph_encode_32(&p
, 1); /* client_inc is always 1 */
2531 req
->r_request_osdmap_epoch
= p
;
2533 req
->r_request_flags
= p
;
2535 if (req
->r_flags
& CEPH_OSD_FLAG_WRITE
)
2536 ceph_encode_timespec(p
, mtime
);
2537 p
+= sizeof(struct ceph_timespec
);
2538 req
->r_request_reassert_version
= p
;
2539 p
+= sizeof(struct ceph_eversion
); /* will get filled in */
2542 ceph_encode_8(&p
, 4);
2543 ceph_encode_8(&p
, 4);
2544 ceph_encode_32(&p
, 8 + 4 + 4);
2545 req
->r_request_pool
= p
;
2547 ceph_encode_32(&p
, -1); /* preferred */
2548 ceph_encode_32(&p
, 0); /* key len */
2550 ceph_encode_8(&p
, 1);
2551 req
->r_request_pgid
= p
;
2553 ceph_encode_32(&p
, -1); /* preferred */
2556 ceph_encode_32(&p
, req
->r_base_oid
.name_len
);
2557 memcpy(p
, req
->r_base_oid
.name
, req
->r_base_oid
.name_len
);
2558 dout("oid %*pE len %d\n", req
->r_base_oid
.name_len
,
2559 req
->r_base_oid
.name
, req
->r_base_oid
.name_len
);
2560 p
+= req
->r_base_oid
.name_len
;
2562 /* ops--can imply data */
2563 ceph_encode_16(&p
, (u16
)req
->r_num_ops
);
2565 for (i
= 0; i
< req
->r_num_ops
; i
++) {
2566 data_len
+= osd_req_encode_op(req
, p
, i
);
2567 p
+= sizeof(struct ceph_osd_op
);
2571 ceph_encode_64(&p
, req
->r_snapid
);
2572 ceph_encode_64(&p
, req
->r_snapc
? req
->r_snapc
->seq
: 0);
2573 ceph_encode_32(&p
, req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
2575 for (i
= 0; i
< req
->r_snapc
->num_snaps
; i
++) {
2576 ceph_encode_64(&p
, req
->r_snapc
->snaps
[i
]);
2580 req
->r_request_attempts
= p
;
2584 if (flags
& CEPH_OSD_FLAG_WRITE
) {
2588 * The header "data_off" is a hint to the receiver
2589 * allowing it to align received data into its
2590 * buffers such that there's no need to re-copy
2591 * it before writing it to disk (direct I/O).
2593 data_off
= (u16
) (off
& 0xffff);
2594 req
->r_request
->hdr
.data_off
= cpu_to_le16(data_off
);
2596 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
2598 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
2599 msg_size
= p
- msg
->front
.iov_base
;
2600 msg
->front
.iov_len
= msg_size
;
2601 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
2603 dout("build_request msg_size was %d\n", (int)msg_size
);
2605 EXPORT_SYMBOL(ceph_osdc_build_request
);
2608 * Register request, send initial attempt.
2610 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
2611 struct ceph_osd_request
*req
,
2616 down_read(&osdc
->map_sem
);
2617 mutex_lock(&osdc
->request_mutex
);
2619 rc
= __ceph_osdc_start_request(osdc
, req
, nofail
);
2621 mutex_unlock(&osdc
->request_mutex
);
2622 up_read(&osdc
->map_sem
);
2626 EXPORT_SYMBOL(ceph_osdc_start_request
);
2629 * Unregister a registered request. The request is not completed (i.e.
2630 * no callbacks or wakeups) - higher layers are supposed to know what
2631 * they are canceling.
2633 void ceph_osdc_cancel_request(struct ceph_osd_request
*req
)
2635 struct ceph_osd_client
*osdc
= req
->r_osdc
;
2637 mutex_lock(&osdc
->request_mutex
);
2639 __unregister_linger_request(osdc
, req
);
2640 __unregister_request(osdc
, req
);
2641 mutex_unlock(&osdc
->request_mutex
);
2643 dout("%s %p tid %llu canceled\n", __func__
, req
, req
->r_tid
);
2645 EXPORT_SYMBOL(ceph_osdc_cancel_request
);
2648 * wait for a request to complete
2650 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
2651 struct ceph_osd_request
*req
)
2655 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
2657 rc
= wait_for_completion_interruptible(&req
->r_completion
);
2659 dout("%s %p tid %llu interrupted\n", __func__
, req
, req
->r_tid
);
2660 ceph_osdc_cancel_request(req
);
2661 complete_request(req
);
2665 dout("%s %p tid %llu result %d\n", __func__
, req
, req
->r_tid
,
2667 return req
->r_result
;
2669 EXPORT_SYMBOL(ceph_osdc_wait_request
);
2672 * sync - wait for all in-flight requests to flush. avoid starvation.
2674 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
2676 struct ceph_osd_request
*req
;
2677 u64 last_tid
, next_tid
= 0;
2679 mutex_lock(&osdc
->request_mutex
);
2680 last_tid
= osdc
->last_tid
;
2682 req
= __lookup_request_ge(osdc
, next_tid
);
2685 if (req
->r_tid
> last_tid
)
2688 next_tid
= req
->r_tid
+ 1;
2689 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
2692 ceph_osdc_get_request(req
);
2693 mutex_unlock(&osdc
->request_mutex
);
2694 dout("sync waiting on tid %llu (last is %llu)\n",
2695 req
->r_tid
, last_tid
);
2696 wait_for_completion(&req
->r_safe_completion
);
2697 mutex_lock(&osdc
->request_mutex
);
2698 ceph_osdc_put_request(req
);
2700 mutex_unlock(&osdc
->request_mutex
);
2701 dout("sync done (thru tid %llu)\n", last_tid
);
2703 EXPORT_SYMBOL(ceph_osdc_sync
);
2706 * Call all pending notify callbacks - for use after a watch is
2707 * unregistered, to make sure no more callbacks for it will be invoked
2709 void ceph_osdc_flush_notifies(struct ceph_osd_client
*osdc
)
2711 flush_workqueue(osdc
->notify_wq
);
2713 EXPORT_SYMBOL(ceph_osdc_flush_notifies
);
2719 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
2724 osdc
->client
= client
;
2725 osdc
->osdmap
= NULL
;
2726 init_rwsem(&osdc
->map_sem
);
2727 mutex_init(&osdc
->request_mutex
);
2729 osdc
->osds
= RB_ROOT
;
2730 INIT_LIST_HEAD(&osdc
->osd_lru
);
2731 osdc
->requests
= RB_ROOT
;
2732 INIT_LIST_HEAD(&osdc
->req_lru
);
2733 INIT_LIST_HEAD(&osdc
->req_unsent
);
2734 INIT_LIST_HEAD(&osdc
->req_notarget
);
2735 INIT_LIST_HEAD(&osdc
->req_linger
);
2736 osdc
->num_requests
= 0;
2737 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
2738 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
2739 spin_lock_init(&osdc
->event_lock
);
2740 osdc
->event_tree
= RB_ROOT
;
2741 osdc
->event_count
= 0;
2743 schedule_delayed_work(&osdc
->osds_timeout_work
,
2744 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
));
2747 osdc
->req_mempool
= mempool_create_slab_pool(10,
2748 ceph_osd_request_cache
);
2749 if (!osdc
->req_mempool
)
2752 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
2753 PAGE_SIZE
, 10, true, "osd_op");
2756 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
2757 PAGE_SIZE
, 10, true, "osd_op_reply");
2762 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
2763 if (!osdc
->notify_wq
)
2764 goto out_msgpool_reply
;
2769 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2771 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2773 mempool_destroy(osdc
->req_mempool
);
2778 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
2780 flush_workqueue(osdc
->notify_wq
);
2781 destroy_workqueue(osdc
->notify_wq
);
2782 cancel_delayed_work_sync(&osdc
->timeout_work
);
2783 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
2785 mutex_lock(&osdc
->request_mutex
);
2786 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
2787 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
2788 struct ceph_osd
, o_node
);
2789 remove_osd(osdc
, osd
);
2791 mutex_unlock(&osdc
->request_mutex
);
2794 ceph_osdmap_destroy(osdc
->osdmap
);
2795 osdc
->osdmap
= NULL
;
2797 mempool_destroy(osdc
->req_mempool
);
2798 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2799 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2803 * Read some contiguous pages. If we cross a stripe boundary, shorten
2804 * *plen. Return number of bytes read, or error.
2806 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
2807 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
2809 u32 truncate_seq
, u64 truncate_size
,
2810 struct page
**pages
, int num_pages
, int page_align
)
2812 struct ceph_osd_request
*req
;
2815 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
2816 vino
.snap
, off
, *plen
);
2817 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
, 0, 1,
2818 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
2819 NULL
, truncate_seq
, truncate_size
,
2822 return PTR_ERR(req
);
2824 /* it may be a short read due to an object boundary */
2826 osd_req_op_extent_osd_data_pages(req
, 0,
2827 pages
, *plen
, page_align
, false, false);
2829 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2830 off
, *plen
, *plen
, page_align
);
2832 ceph_osdc_build_request(req
, off
, NULL
, vino
.snap
, NULL
);
2834 rc
= ceph_osdc_start_request(osdc
, req
, false);
2836 rc
= ceph_osdc_wait_request(osdc
, req
);
2838 ceph_osdc_put_request(req
);
2839 dout("readpages result %d\n", rc
);
2842 EXPORT_SYMBOL(ceph_osdc_readpages
);
2845 * do a synchronous write on N pages
2847 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
2848 struct ceph_file_layout
*layout
,
2849 struct ceph_snap_context
*snapc
,
2851 u32 truncate_seq
, u64 truncate_size
,
2852 struct timespec
*mtime
,
2853 struct page
**pages
, int num_pages
)
2855 struct ceph_osd_request
*req
;
2857 int page_align
= off
& ~PAGE_MASK
;
2859 BUG_ON(vino
.snap
!= CEPH_NOSNAP
); /* snapshots aren't writeable */
2860 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
, 0, 1,
2862 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
2863 snapc
, truncate_seq
, truncate_size
,
2866 return PTR_ERR(req
);
2868 /* it may be a short write due to an object boundary */
2869 osd_req_op_extent_osd_data_pages(req
, 0, pages
, len
, page_align
,
2871 dout("writepages %llu~%llu (%llu bytes)\n", off
, len
, len
);
2873 ceph_osdc_build_request(req
, off
, snapc
, CEPH_NOSNAP
, mtime
);
2875 rc
= ceph_osdc_start_request(osdc
, req
, true);
2877 rc
= ceph_osdc_wait_request(osdc
, req
);
2879 ceph_osdc_put_request(req
);
2882 dout("writepages result %d\n", rc
);
2885 EXPORT_SYMBOL(ceph_osdc_writepages
);
2887 int ceph_osdc_setup(void)
2889 size_t size
= sizeof(struct ceph_osd_request
) +
2890 CEPH_OSD_SLAB_OPS
* sizeof(struct ceph_osd_req_op
);
2892 BUG_ON(ceph_osd_request_cache
);
2893 ceph_osd_request_cache
= kmem_cache_create("ceph_osd_request", size
,
2896 return ceph_osd_request_cache
? 0 : -ENOMEM
;
2898 EXPORT_SYMBOL(ceph_osdc_setup
);
2900 void ceph_osdc_cleanup(void)
2902 BUG_ON(!ceph_osd_request_cache
);
2903 kmem_cache_destroy(ceph_osd_request_cache
);
2904 ceph_osd_request_cache
= NULL
;
2906 EXPORT_SYMBOL(ceph_osdc_cleanup
);
2909 * handle incoming message
2911 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2913 struct ceph_osd
*osd
= con
->private;
2914 struct ceph_osd_client
*osdc
;
2915 int type
= le16_to_cpu(msg
->hdr
.type
);
2922 case CEPH_MSG_OSD_MAP
:
2923 ceph_osdc_handle_map(osdc
, msg
);
2925 case CEPH_MSG_OSD_OPREPLY
:
2926 handle_reply(osdc
, msg
);
2928 case CEPH_MSG_WATCH_NOTIFY
:
2929 handle_watch_notify(osdc
, msg
);
2933 pr_err("received unknown message type %d %s\n", type
,
2934 ceph_msg_type_name(type
));
2941 * Lookup and return message for incoming reply. Don't try to do
2942 * anything about a larger than preallocated data portion of the
2943 * message at the moment - for now, just skip the message.
2945 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2946 struct ceph_msg_header
*hdr
,
2949 struct ceph_osd
*osd
= con
->private;
2950 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2952 struct ceph_osd_request
*req
;
2953 int front_len
= le32_to_cpu(hdr
->front_len
);
2954 int data_len
= le32_to_cpu(hdr
->data_len
);
2957 tid
= le64_to_cpu(hdr
->tid
);
2958 mutex_lock(&osdc
->request_mutex
);
2959 req
= lookup_request(&osdc
->requests
, tid
);
2961 dout("%s osd%d tid %llu unknown, skipping\n", __func__
,
2968 ceph_msg_revoke_incoming(req
->r_reply
);
2970 if (front_len
> req
->r_reply
->front_alloc_len
) {
2971 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2972 __func__
, osd
->o_osd
, req
->r_tid
, front_len
,
2973 req
->r_reply
->front_alloc_len
);
2974 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front_len
, GFP_NOFS
,
2978 ceph_msg_put(req
->r_reply
);
2982 if (data_len
> req
->r_reply
->data_length
) {
2983 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2984 __func__
, osd
->o_osd
, req
->r_tid
, data_len
,
2985 req
->r_reply
->data_length
);
2991 m
= ceph_msg_get(req
->r_reply
);
2992 dout("get_reply tid %lld %p\n", tid
, m
);
2995 mutex_unlock(&osdc
->request_mutex
);
2999 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
3000 struct ceph_msg_header
*hdr
,
3003 struct ceph_osd
*osd
= con
->private;
3004 int type
= le16_to_cpu(hdr
->type
);
3005 int front
= le32_to_cpu(hdr
->front_len
);
3009 case CEPH_MSG_OSD_MAP
:
3010 case CEPH_MSG_WATCH_NOTIFY
:
3011 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
3012 case CEPH_MSG_OSD_OPREPLY
:
3013 return get_reply(con
, hdr
, skip
);
3015 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
3023 * Wrappers to refcount containing ceph_osd struct
3025 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
3027 struct ceph_osd
*osd
= con
->private;
3033 static void put_osd_con(struct ceph_connection
*con
)
3035 struct ceph_osd
*osd
= con
->private;
3043 * Note: returned pointer is the address of a structure that's
3044 * managed separately. Caller must *not* attempt to free it.
3046 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
3047 int *proto
, int force_new
)
3049 struct ceph_osd
*o
= con
->private;
3050 struct ceph_osd_client
*osdc
= o
->o_osdc
;
3051 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
3052 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
3054 if (force_new
&& auth
->authorizer
) {
3055 ceph_auth_destroy_authorizer(auth
->authorizer
);
3056 auth
->authorizer
= NULL
;
3058 if (!auth
->authorizer
) {
3059 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
3062 return ERR_PTR(ret
);
3064 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
3067 return ERR_PTR(ret
);
3069 *proto
= ac
->protocol
;
3075 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3077 struct ceph_osd
*o
= con
->private;
3078 struct ceph_osd_client
*osdc
= o
->o_osdc
;
3079 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
3081 return ceph_auth_verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
3084 static int invalidate_authorizer(struct ceph_connection
*con
)
3086 struct ceph_osd
*o
= con
->private;
3087 struct ceph_osd_client
*osdc
= o
->o_osdc
;
3088 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
3090 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
3091 return ceph_monc_validate_auth(&osdc
->client
->monc
);
3094 static int osd_sign_message(struct ceph_msg
*msg
)
3096 struct ceph_osd
*o
= msg
->con
->private;
3097 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
3099 return ceph_auth_sign_message(auth
, msg
);
3102 static int osd_check_message_signature(struct ceph_msg
*msg
)
3104 struct ceph_osd
*o
= msg
->con
->private;
3105 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
3107 return ceph_auth_check_message_signature(auth
, msg
);
3110 static const struct ceph_connection_operations osd_con_ops
= {
3113 .dispatch
= dispatch
,
3114 .get_authorizer
= get_authorizer
,
3115 .verify_authorizer_reply
= verify_authorizer_reply
,
3116 .invalidate_authorizer
= invalidate_authorizer
,
3117 .alloc_msg
= alloc_msg
,
3118 .sign_message
= osd_sign_message
,
3119 .check_message_signature
= osd_check_message_signature
,