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 target_destroy(&req
->r_t
);
354 ceph_put_snap_context(req
->r_snapc
);
357 mempool_free(req
, req
->r_osdc
->req_mempool
);
358 else if (req
->r_num_ops
<= CEPH_OSD_SLAB_OPS
)
359 kmem_cache_free(ceph_osd_request_cache
, req
);
364 void ceph_osdc_get_request(struct ceph_osd_request
*req
)
366 dout("%s %p (was %d)\n", __func__
, req
,
367 atomic_read(&req
->r_kref
.refcount
));
368 kref_get(&req
->r_kref
);
370 EXPORT_SYMBOL(ceph_osdc_get_request
);
372 void ceph_osdc_put_request(struct ceph_osd_request
*req
)
375 dout("%s %p (was %d)\n", __func__
, req
,
376 atomic_read(&req
->r_kref
.refcount
));
377 kref_put(&req
->r_kref
, ceph_osdc_release_request
);
380 EXPORT_SYMBOL(ceph_osdc_put_request
);
382 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
383 struct ceph_snap_context
*snapc
,
384 unsigned int num_ops
,
388 struct ceph_osd_request
*req
;
391 BUG_ON(num_ops
> CEPH_OSD_SLAB_OPS
);
392 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
393 } else if (num_ops
<= CEPH_OSD_SLAB_OPS
) {
394 req
= kmem_cache_alloc(ceph_osd_request_cache
, gfp_flags
);
396 BUG_ON(num_ops
> CEPH_OSD_MAX_OPS
);
397 req
= kmalloc(sizeof(*req
) + num_ops
* sizeof(req
->r_ops
[0]),
403 /* req only, each op is zeroed in _osd_req_op_init() */
404 memset(req
, 0, sizeof(*req
));
407 req
->r_mempool
= use_mempool
;
408 req
->r_num_ops
= num_ops
;
409 req
->r_snapid
= CEPH_NOSNAP
;
410 req
->r_snapc
= ceph_get_snap_context(snapc
);
412 kref_init(&req
->r_kref
);
413 init_completion(&req
->r_completion
);
414 init_completion(&req
->r_safe_completion
);
415 RB_CLEAR_NODE(&req
->r_node
);
416 INIT_LIST_HEAD(&req
->r_unsafe_item
);
417 INIT_LIST_HEAD(&req
->r_linger_item
);
418 INIT_LIST_HEAD(&req
->r_linger_osd_item
);
419 INIT_LIST_HEAD(&req
->r_req_lru_item
);
420 INIT_LIST_HEAD(&req
->r_osd_item
);
422 target_init(&req
->r_t
);
424 dout("%s req %p\n", __func__
, req
);
427 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
429 int ceph_osdc_alloc_messages(struct ceph_osd_request
*req
, gfp_t gfp
)
431 struct ceph_osd_client
*osdc
= req
->r_osdc
;
432 struct ceph_msg
*msg
;
435 WARN_ON(ceph_oid_empty(&req
->r_base_oid
));
437 /* create request message */
438 msg_size
= 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
439 msg_size
+= 4 + 4 + 4 + 8; /* mtime, reassert_version */
440 msg_size
+= 2 + 4 + 8 + 4 + 4; /* oloc */
441 msg_size
+= 1 + 8 + 4 + 4; /* pgid */
442 msg_size
+= 4 + req
->r_base_oid
.name_len
; /* oid */
443 msg_size
+= 2 + req
->r_num_ops
* sizeof(struct ceph_osd_op
);
444 msg_size
+= 8; /* snapid */
445 msg_size
+= 8; /* snap_seq */
446 msg_size
+= 4 + 8 * (req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
447 msg_size
+= 4; /* retry_attempt */
450 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
452 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp
, true);
456 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
457 req
->r_request
= msg
;
459 /* create reply message */
460 msg_size
= OSD_OPREPLY_FRONT_LEN
;
461 msg_size
+= req
->r_base_oid
.name_len
;
462 msg_size
+= req
->r_num_ops
* sizeof(struct ceph_osd_op
);
465 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
467 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, msg_size
, gfp
, true);
475 EXPORT_SYMBOL(ceph_osdc_alloc_messages
);
477 static bool osd_req_opcode_valid(u16 opcode
)
480 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
481 __CEPH_FORALL_OSD_OPS(GENERATE_CASE
)
489 * This is an osd op init function for opcodes that have no data or
490 * other information associated with them. It also serves as a
491 * common init routine for all the other init functions, below.
493 static struct ceph_osd_req_op
*
494 _osd_req_op_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
495 u16 opcode
, u32 flags
)
497 struct ceph_osd_req_op
*op
;
499 BUG_ON(which
>= osd_req
->r_num_ops
);
500 BUG_ON(!osd_req_opcode_valid(opcode
));
502 op
= &osd_req
->r_ops
[which
];
503 memset(op
, 0, sizeof (*op
));
510 void osd_req_op_init(struct ceph_osd_request
*osd_req
,
511 unsigned int which
, u16 opcode
, u32 flags
)
513 (void)_osd_req_op_init(osd_req
, which
, opcode
, flags
);
515 EXPORT_SYMBOL(osd_req_op_init
);
517 void osd_req_op_extent_init(struct ceph_osd_request
*osd_req
,
518 unsigned int which
, u16 opcode
,
519 u64 offset
, u64 length
,
520 u64 truncate_size
, u32 truncate_seq
)
522 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
524 size_t payload_len
= 0;
526 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
&&
527 opcode
!= CEPH_OSD_OP_WRITEFULL
&& opcode
!= CEPH_OSD_OP_ZERO
&&
528 opcode
!= CEPH_OSD_OP_TRUNCATE
);
530 op
->extent
.offset
= offset
;
531 op
->extent
.length
= length
;
532 op
->extent
.truncate_size
= truncate_size
;
533 op
->extent
.truncate_seq
= truncate_seq
;
534 if (opcode
== CEPH_OSD_OP_WRITE
|| opcode
== CEPH_OSD_OP_WRITEFULL
)
535 payload_len
+= length
;
537 op
->indata_len
= payload_len
;
539 EXPORT_SYMBOL(osd_req_op_extent_init
);
541 void osd_req_op_extent_update(struct ceph_osd_request
*osd_req
,
542 unsigned int which
, u64 length
)
544 struct ceph_osd_req_op
*op
;
547 BUG_ON(which
>= osd_req
->r_num_ops
);
548 op
= &osd_req
->r_ops
[which
];
549 previous
= op
->extent
.length
;
551 if (length
== previous
)
552 return; /* Nothing to do */
553 BUG_ON(length
> previous
);
555 op
->extent
.length
= length
;
556 op
->indata_len
-= previous
- length
;
558 EXPORT_SYMBOL(osd_req_op_extent_update
);
560 void osd_req_op_extent_dup_last(struct ceph_osd_request
*osd_req
,
561 unsigned int which
, u64 offset_inc
)
563 struct ceph_osd_req_op
*op
, *prev_op
;
565 BUG_ON(which
+ 1 >= osd_req
->r_num_ops
);
567 prev_op
= &osd_req
->r_ops
[which
];
568 op
= _osd_req_op_init(osd_req
, which
+ 1, prev_op
->op
, prev_op
->flags
);
569 /* dup previous one */
570 op
->indata_len
= prev_op
->indata_len
;
571 op
->outdata_len
= prev_op
->outdata_len
;
572 op
->extent
= prev_op
->extent
;
574 op
->extent
.offset
+= offset_inc
;
575 op
->extent
.length
-= offset_inc
;
577 if (op
->op
== CEPH_OSD_OP_WRITE
|| op
->op
== CEPH_OSD_OP_WRITEFULL
)
578 op
->indata_len
-= offset_inc
;
580 EXPORT_SYMBOL(osd_req_op_extent_dup_last
);
582 void osd_req_op_cls_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
583 u16 opcode
, const char *class, const char *method
)
585 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
587 struct ceph_pagelist
*pagelist
;
588 size_t payload_len
= 0;
591 BUG_ON(opcode
!= CEPH_OSD_OP_CALL
);
593 pagelist
= kmalloc(sizeof (*pagelist
), GFP_NOFS
);
595 ceph_pagelist_init(pagelist
);
597 op
->cls
.class_name
= class;
598 size
= strlen(class);
599 BUG_ON(size
> (size_t) U8_MAX
);
600 op
->cls
.class_len
= size
;
601 ceph_pagelist_append(pagelist
, class, size
);
604 op
->cls
.method_name
= method
;
605 size
= strlen(method
);
606 BUG_ON(size
> (size_t) U8_MAX
);
607 op
->cls
.method_len
= size
;
608 ceph_pagelist_append(pagelist
, method
, size
);
611 osd_req_op_cls_request_info_pagelist(osd_req
, which
, pagelist
);
613 op
->cls
.argc
= 0; /* currently unused */
615 op
->indata_len
= payload_len
;
617 EXPORT_SYMBOL(osd_req_op_cls_init
);
619 int osd_req_op_xattr_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
620 u16 opcode
, const char *name
, const void *value
,
621 size_t size
, u8 cmp_op
, u8 cmp_mode
)
623 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
625 struct ceph_pagelist
*pagelist
;
628 BUG_ON(opcode
!= CEPH_OSD_OP_SETXATTR
&& opcode
!= CEPH_OSD_OP_CMPXATTR
);
630 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
634 ceph_pagelist_init(pagelist
);
636 payload_len
= strlen(name
);
637 op
->xattr
.name_len
= payload_len
;
638 ceph_pagelist_append(pagelist
, name
, payload_len
);
640 op
->xattr
.value_len
= size
;
641 ceph_pagelist_append(pagelist
, value
, size
);
644 op
->xattr
.cmp_op
= cmp_op
;
645 op
->xattr
.cmp_mode
= cmp_mode
;
647 ceph_osd_data_pagelist_init(&op
->xattr
.osd_data
, pagelist
);
648 op
->indata_len
= payload_len
;
651 EXPORT_SYMBOL(osd_req_op_xattr_init
);
653 void osd_req_op_watch_init(struct ceph_osd_request
*osd_req
,
654 unsigned int which
, u16 opcode
,
655 u64 cookie
, u64 version
, int flag
)
657 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
660 BUG_ON(opcode
!= CEPH_OSD_OP_NOTIFY_ACK
&& opcode
!= CEPH_OSD_OP_WATCH
);
662 op
->watch
.cookie
= cookie
;
663 op
->watch
.ver
= version
;
664 if (opcode
== CEPH_OSD_OP_WATCH
&& flag
)
665 op
->watch
.flag
= (u8
)1;
667 EXPORT_SYMBOL(osd_req_op_watch_init
);
669 void osd_req_op_alloc_hint_init(struct ceph_osd_request
*osd_req
,
671 u64 expected_object_size
,
672 u64 expected_write_size
)
674 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
675 CEPH_OSD_OP_SETALLOCHINT
,
678 op
->alloc_hint
.expected_object_size
= expected_object_size
;
679 op
->alloc_hint
.expected_write_size
= expected_write_size
;
682 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
683 * not worth a feature bit. Set FAILOK per-op flag to make
684 * sure older osds don't trip over an unsupported opcode.
686 op
->flags
|= CEPH_OSD_OP_FLAG_FAILOK
;
688 EXPORT_SYMBOL(osd_req_op_alloc_hint_init
);
690 static void ceph_osdc_msg_data_add(struct ceph_msg
*msg
,
691 struct ceph_osd_data
*osd_data
)
693 u64 length
= ceph_osd_data_length(osd_data
);
695 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
) {
696 BUG_ON(length
> (u64
) SIZE_MAX
);
698 ceph_msg_data_add_pages(msg
, osd_data
->pages
,
699 length
, osd_data
->alignment
);
700 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGELIST
) {
702 ceph_msg_data_add_pagelist(msg
, osd_data
->pagelist
);
704 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_BIO
) {
705 ceph_msg_data_add_bio(msg
, osd_data
->bio
, length
);
708 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_NONE
);
712 static u64
osd_req_encode_op(struct ceph_osd_request
*req
,
713 struct ceph_osd_op
*dst
, unsigned int which
)
715 struct ceph_osd_req_op
*src
;
716 struct ceph_osd_data
*osd_data
;
717 u64 request_data_len
= 0;
720 BUG_ON(which
>= req
->r_num_ops
);
721 src
= &req
->r_ops
[which
];
722 if (WARN_ON(!osd_req_opcode_valid(src
->op
))) {
723 pr_err("unrecognized osd opcode %d\n", src
->op
);
729 case CEPH_OSD_OP_STAT
:
730 osd_data
= &src
->raw_data_in
;
731 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
733 case CEPH_OSD_OP_READ
:
734 case CEPH_OSD_OP_WRITE
:
735 case CEPH_OSD_OP_WRITEFULL
:
736 case CEPH_OSD_OP_ZERO
:
737 case CEPH_OSD_OP_TRUNCATE
:
738 if (src
->op
== CEPH_OSD_OP_WRITE
||
739 src
->op
== CEPH_OSD_OP_WRITEFULL
)
740 request_data_len
= src
->extent
.length
;
741 dst
->extent
.offset
= cpu_to_le64(src
->extent
.offset
);
742 dst
->extent
.length
= cpu_to_le64(src
->extent
.length
);
743 dst
->extent
.truncate_size
=
744 cpu_to_le64(src
->extent
.truncate_size
);
745 dst
->extent
.truncate_seq
=
746 cpu_to_le32(src
->extent
.truncate_seq
);
747 osd_data
= &src
->extent
.osd_data
;
748 if (src
->op
== CEPH_OSD_OP_WRITE
||
749 src
->op
== CEPH_OSD_OP_WRITEFULL
)
750 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
752 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
754 case CEPH_OSD_OP_CALL
:
755 dst
->cls
.class_len
= src
->cls
.class_len
;
756 dst
->cls
.method_len
= src
->cls
.method_len
;
757 osd_data
= &src
->cls
.request_info
;
758 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
759 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_PAGELIST
);
760 request_data_len
= osd_data
->pagelist
->length
;
762 osd_data
= &src
->cls
.request_data
;
763 data_length
= ceph_osd_data_length(osd_data
);
765 BUG_ON(osd_data
->type
== CEPH_OSD_DATA_TYPE_NONE
);
766 dst
->cls
.indata_len
= cpu_to_le32(data_length
);
767 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
768 src
->indata_len
+= data_length
;
769 request_data_len
+= data_length
;
771 osd_data
= &src
->cls
.response_data
;
772 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
774 case CEPH_OSD_OP_STARTSYNC
:
776 case CEPH_OSD_OP_NOTIFY_ACK
:
777 case CEPH_OSD_OP_WATCH
:
778 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
779 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
780 dst
->watch
.flag
= src
->watch
.flag
;
782 case CEPH_OSD_OP_SETALLOCHINT
:
783 dst
->alloc_hint
.expected_object_size
=
784 cpu_to_le64(src
->alloc_hint
.expected_object_size
);
785 dst
->alloc_hint
.expected_write_size
=
786 cpu_to_le64(src
->alloc_hint
.expected_write_size
);
788 case CEPH_OSD_OP_SETXATTR
:
789 case CEPH_OSD_OP_CMPXATTR
:
790 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
791 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
792 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
793 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
794 osd_data
= &src
->xattr
.osd_data
;
795 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
796 request_data_len
= osd_data
->pagelist
->length
;
798 case CEPH_OSD_OP_CREATE
:
799 case CEPH_OSD_OP_DELETE
:
802 pr_err("unsupported osd opcode %s\n",
803 ceph_osd_op_name(src
->op
));
809 dst
->op
= cpu_to_le16(src
->op
);
810 dst
->flags
= cpu_to_le32(src
->flags
);
811 dst
->payload_len
= cpu_to_le32(src
->indata_len
);
813 return request_data_len
;
817 * build new request AND message, calculate layout, and adjust file
820 * if the file was recently truncated, we include information about its
821 * old and new size so that the object can be updated appropriately. (we
822 * avoid synchronously deleting truncated objects because it's slow.)
824 * if @do_sync, include a 'startsync' command so that the osd will flush
827 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
828 struct ceph_file_layout
*layout
,
829 struct ceph_vino vino
,
831 unsigned int which
, int num_ops
,
832 int opcode
, int flags
,
833 struct ceph_snap_context
*snapc
,
838 struct ceph_osd_request
*req
;
844 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
&&
845 opcode
!= CEPH_OSD_OP_ZERO
&& opcode
!= CEPH_OSD_OP_TRUNCATE
&&
846 opcode
!= CEPH_OSD_OP_CREATE
&& opcode
!= CEPH_OSD_OP_DELETE
);
848 req
= ceph_osdc_alloc_request(osdc
, snapc
, num_ops
, use_mempool
,
855 req
->r_flags
= flags
;
857 /* calculate max write size */
858 r
= calc_layout(layout
, off
, plen
, &objnum
, &objoff
, &objlen
);
862 if (opcode
== CEPH_OSD_OP_CREATE
|| opcode
== CEPH_OSD_OP_DELETE
) {
863 osd_req_op_init(req
, which
, opcode
, 0);
865 u32 object_size
= le32_to_cpu(layout
->fl_object_size
);
866 u32 object_base
= off
- objoff
;
867 if (!(truncate_seq
== 1 && truncate_size
== -1ULL)) {
868 if (truncate_size
<= object_base
) {
871 truncate_size
-= object_base
;
872 if (truncate_size
> object_size
)
873 truncate_size
= object_size
;
876 osd_req_op_extent_init(req
, which
, opcode
, objoff
, objlen
,
877 truncate_size
, truncate_seq
);
880 req
->r_base_oloc
.pool
= ceph_file_layout_pg_pool(*layout
);
881 ceph_oid_printf(&req
->r_base_oid
, "%llx.%08llx", vino
.ino
, objnum
);
883 r
= ceph_osdc_alloc_messages(req
, GFP_NOFS
);
890 ceph_osdc_put_request(req
);
893 EXPORT_SYMBOL(ceph_osdc_new_request
);
896 * We keep osd requests in an rbtree, sorted by ->r_tid.
898 DEFINE_RB_FUNCS(request
, struct ceph_osd_request
, r_tid
, r_node
)
900 static struct ceph_osd_request
*
901 __lookup_request_ge(struct ceph_osd_client
*osdc
,
904 struct ceph_osd_request
*req
;
905 struct rb_node
*n
= osdc
->requests
.rb_node
;
908 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
909 if (tid
< req
->r_tid
) {
913 } else if (tid
> req
->r_tid
) {
922 static void __kick_linger_request(struct ceph_osd_request
*req
)
924 struct ceph_osd_client
*osdc
= req
->r_osdc
;
925 struct ceph_osd
*osd
= req
->r_osd
;
928 * Linger requests need to be resent with a new tid to avoid
929 * the dup op detection logic on the OSDs. Achieve this with
930 * a re-register dance instead of open-coding.
932 ceph_osdc_get_request(req
);
933 if (!list_empty(&req
->r_linger_item
))
934 __unregister_linger_request(osdc
, req
);
936 __unregister_request(osdc
, req
);
937 __register_request(osdc
, req
);
938 ceph_osdc_put_request(req
);
941 * Unless request has been registered as both normal and
942 * lingering, __unregister{,_linger}_request clears r_osd.
943 * However, here we need to preserve r_osd to make sure we
944 * requeue on the same OSD.
946 WARN_ON(req
->r_osd
|| !osd
);
949 dout("%s requeueing %p tid %llu\n", __func__
, req
, req
->r_tid
);
950 __enqueue_request(req
);
954 * Resubmit requests pending on the given osd.
956 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
957 struct ceph_osd
*osd
)
959 struct ceph_osd_request
*req
, *nreq
;
961 LIST_HEAD(resend_linger
);
964 dout("%s osd%d\n", __func__
, osd
->o_osd
);
965 err
= __reset_osd(osdc
, osd
);
970 * Build up a list of requests to resend by traversing the
971 * osd's list of requests. Requests for a given object are
972 * sent in tid order, and that is also the order they're
973 * kept on this list. Therefore all requests that are in
974 * flight will be found first, followed by all requests that
975 * have not yet been sent. And to resend requests while
976 * preserving this order we will want to put any sent
977 * requests back on the front of the osd client's unsent
980 * So we build a separate ordered list of already-sent
981 * requests for the affected osd and splice it onto the
982 * front of the osd client's unsent list. Once we've seen a
983 * request that has not yet been sent we're done. Those
984 * requests are already sitting right where they belong.
986 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
990 if (!req
->r_linger
) {
991 dout("%s requeueing %p tid %llu\n", __func__
, req
,
993 list_move_tail(&req
->r_req_lru_item
, &resend
);
994 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
996 list_move_tail(&req
->r_req_lru_item
, &resend_linger
);
999 list_splice(&resend
, &osdc
->req_unsent
);
1002 * Both registered and not yet registered linger requests are
1003 * enqueued with a new tid on the same OSD. We add/move them
1004 * to req_unsent/o_requests at the end to keep things in tid
1007 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
1008 r_linger_osd_item
) {
1009 WARN_ON(!list_empty(&req
->r_req_lru_item
));
1010 __kick_linger_request(req
);
1013 list_for_each_entry_safe(req
, nreq
, &resend_linger
, r_req_lru_item
)
1014 __kick_linger_request(req
);
1018 * If the osd connection drops, we need to resubmit all requests.
1020 static void osd_reset(struct ceph_connection
*con
)
1022 struct ceph_osd
*osd
= con
->private;
1023 struct ceph_osd_client
*osdc
;
1027 dout("osd_reset osd%d\n", osd
->o_osd
);
1029 down_read(&osdc
->map_sem
);
1030 mutex_lock(&osdc
->request_mutex
);
1031 __kick_osd_requests(osdc
, osd
);
1032 __send_queued(osdc
);
1033 mutex_unlock(&osdc
->request_mutex
);
1034 up_read(&osdc
->map_sem
);
1038 * Track open sessions with osds.
1040 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
1042 struct ceph_osd
*osd
;
1044 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
1048 atomic_set(&osd
->o_ref
, 1);
1051 RB_CLEAR_NODE(&osd
->o_node
);
1052 INIT_LIST_HEAD(&osd
->o_requests
);
1053 INIT_LIST_HEAD(&osd
->o_linger_requests
);
1054 INIT_LIST_HEAD(&osd
->o_osd_lru
);
1055 osd
->o_incarnation
= 1;
1057 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
1059 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
1063 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
1065 if (atomic_inc_not_zero(&osd
->o_ref
)) {
1066 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
1067 atomic_read(&osd
->o_ref
));
1070 dout("get_osd %p FAIL\n", osd
);
1075 static void put_osd(struct ceph_osd
*osd
)
1077 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
1078 atomic_read(&osd
->o_ref
) - 1);
1079 if (atomic_dec_and_test(&osd
->o_ref
)) {
1080 if (osd
->o_auth
.authorizer
)
1081 ceph_auth_destroy_authorizer(osd
->o_auth
.authorizer
);
1086 DEFINE_RB_FUNCS(osd
, struct ceph_osd
, o_osd
, o_node
)
1089 * remove an osd from our map
1091 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1093 dout("%s %p osd%d\n", __func__
, osd
, osd
->o_osd
);
1094 WARN_ON(!list_empty(&osd
->o_requests
));
1095 WARN_ON(!list_empty(&osd
->o_linger_requests
));
1097 list_del_init(&osd
->o_osd_lru
);
1098 erase_osd(&osdc
->osds
, osd
);
1101 static void remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1103 dout("%s %p osd%d\n", __func__
, osd
, osd
->o_osd
);
1105 if (!RB_EMPTY_NODE(&osd
->o_node
)) {
1106 ceph_con_close(&osd
->o_con
);
1107 __remove_osd(osdc
, osd
);
1112 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
1113 struct ceph_osd
*osd
)
1115 dout("%s %p\n", __func__
, osd
);
1116 BUG_ON(!list_empty(&osd
->o_osd_lru
));
1118 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
1119 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
;
1122 static void maybe_move_osd_to_lru(struct ceph_osd_client
*osdc
,
1123 struct ceph_osd
*osd
)
1125 dout("%s %p\n", __func__
, osd
);
1127 if (list_empty(&osd
->o_requests
) &&
1128 list_empty(&osd
->o_linger_requests
))
1129 __move_osd_to_lru(osdc
, osd
);
1132 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
1134 dout("__remove_osd_from_lru %p\n", osd
);
1135 if (!list_empty(&osd
->o_osd_lru
))
1136 list_del_init(&osd
->o_osd_lru
);
1142 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1144 struct ceph_entity_addr
*peer_addr
;
1146 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
1147 if (list_empty(&osd
->o_requests
) &&
1148 list_empty(&osd
->o_linger_requests
)) {
1149 remove_osd(osdc
, osd
);
1153 peer_addr
= &osdc
->osdmap
->osd_addr
[osd
->o_osd
];
1154 if (!memcmp(peer_addr
, &osd
->o_con
.peer_addr
, sizeof (*peer_addr
)) &&
1155 !ceph_con_opened(&osd
->o_con
)) {
1156 struct ceph_osd_request
*req
;
1158 dout("osd addr hasn't changed and connection never opened, "
1159 "letting msgr retry\n");
1160 /* touch each r_stamp for handle_timeout()'s benfit */
1161 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
1162 req
->r_stamp
= jiffies
;
1167 ceph_con_close(&osd
->o_con
);
1168 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
, peer_addr
);
1169 osd
->o_incarnation
++;
1174 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
1176 schedule_delayed_work(&osdc
->timeout_work
,
1177 osdc
->client
->options
->osd_keepalive_timeout
);
1180 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
1182 cancel_delayed_work(&osdc
->timeout_work
);
1186 * Register request, assign tid. If this is the first request, set up
1187 * the timeout event.
1189 static void __register_request(struct ceph_osd_client
*osdc
,
1190 struct ceph_osd_request
*req
)
1192 req
->r_tid
= ++osdc
->last_tid
;
1193 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1194 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
1195 insert_request(&osdc
->requests
, req
);
1196 ceph_osdc_get_request(req
);
1197 osdc
->num_requests
++;
1198 if (osdc
->num_requests
== 1) {
1199 dout(" first request, scheduling timeout\n");
1200 __schedule_osd_timeout(osdc
);
1205 * called under osdc->request_mutex
1207 static void __unregister_request(struct ceph_osd_client
*osdc
,
1208 struct ceph_osd_request
*req
)
1210 if (RB_EMPTY_NODE(&req
->r_node
)) {
1211 dout("__unregister_request %p tid %lld not registered\n",
1216 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
1217 erase_request(&osdc
->requests
, req
);
1218 osdc
->num_requests
--;
1221 /* make sure the original request isn't in flight. */
1222 ceph_msg_revoke(req
->r_request
);
1224 list_del_init(&req
->r_osd_item
);
1225 maybe_move_osd_to_lru(osdc
, req
->r_osd
);
1226 if (list_empty(&req
->r_linger_osd_item
))
1230 list_del_init(&req
->r_req_lru_item
);
1231 ceph_osdc_put_request(req
);
1233 if (osdc
->num_requests
== 0) {
1234 dout(" no requests, canceling timeout\n");
1235 __cancel_osd_timeout(osdc
);
1240 * Cancel a previously queued request message
1242 static void __cancel_request(struct ceph_osd_request
*req
)
1244 if (req
->r_sent
&& req
->r_osd
) {
1245 ceph_msg_revoke(req
->r_request
);
1250 static void __register_linger_request(struct ceph_osd_client
*osdc
,
1251 struct ceph_osd_request
*req
)
1253 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
1254 WARN_ON(!req
->r_linger
);
1256 ceph_osdc_get_request(req
);
1257 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
1259 list_add_tail(&req
->r_linger_osd_item
,
1260 &req
->r_osd
->o_linger_requests
);
1263 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
1264 struct ceph_osd_request
*req
)
1266 WARN_ON(!req
->r_linger
);
1268 if (list_empty(&req
->r_linger_item
)) {
1269 dout("%s %p tid %llu not registered\n", __func__
, req
,
1274 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
1275 list_del_init(&req
->r_linger_item
);
1278 list_del_init(&req
->r_linger_osd_item
);
1279 maybe_move_osd_to_lru(osdc
, req
->r_osd
);
1280 if (list_empty(&req
->r_osd_item
))
1283 ceph_osdc_put_request(req
);
1286 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
1287 struct ceph_osd_request
*req
)
1289 if (!req
->r_linger
) {
1290 dout("set_request_linger %p\n", req
);
1294 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
1296 static bool __pool_full(struct ceph_pg_pool_info
*pi
)
1298 return pi
->flags
& CEPH_POOL_FLAG_FULL
;
1302 * Returns whether a request should be blocked from being sent
1303 * based on the current osdmap and osd_client settings.
1305 * Caller should hold map_sem for read.
1307 static bool target_should_be_paused(struct ceph_osd_client
*osdc
,
1308 const struct ceph_osd_request_target
*t
,
1309 struct ceph_pg_pool_info
*pi
)
1311 bool pauserd
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSERD
);
1312 bool pausewr
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSEWR
) ||
1313 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
) ||
1316 WARN_ON(pi
->id
!= t
->base_oloc
.pool
);
1317 return (t
->flags
& CEPH_OSD_FLAG_READ
&& pauserd
) ||
1318 (t
->flags
& CEPH_OSD_FLAG_WRITE
&& pausewr
);
1321 enum calc_target_result
{
1322 CALC_TARGET_NO_ACTION
= 0,
1323 CALC_TARGET_NEED_RESEND
,
1324 CALC_TARGET_POOL_DNE
,
1327 static enum calc_target_result
calc_target(struct ceph_osd_client
*osdc
,
1328 struct ceph_osd_request_target
*t
,
1329 u32
*last_force_resend
,
1332 struct ceph_pg_pool_info
*pi
;
1333 struct ceph_pg pgid
, last_pgid
;
1334 struct ceph_osds up
, acting
;
1335 bool force_resend
= false;
1336 bool need_check_tiering
= false;
1337 bool need_resend
= false;
1338 bool sort_bitwise
= ceph_osdmap_flag(osdc
->osdmap
,
1339 CEPH_OSDMAP_SORTBITWISE
);
1340 enum calc_target_result ct_res
;
1343 pi
= ceph_pg_pool_by_id(osdc
->osdmap
, t
->base_oloc
.pool
);
1345 t
->osd
= CEPH_HOMELESS_OSD
;
1346 ct_res
= CALC_TARGET_POOL_DNE
;
1350 if (osdc
->osdmap
->epoch
== pi
->last_force_request_resend
) {
1351 if (last_force_resend
&&
1352 *last_force_resend
< pi
->last_force_request_resend
) {
1353 *last_force_resend
= pi
->last_force_request_resend
;
1354 force_resend
= true;
1355 } else if (!last_force_resend
) {
1356 force_resend
= true;
1359 if (ceph_oid_empty(&t
->target_oid
) || force_resend
) {
1360 ceph_oid_copy(&t
->target_oid
, &t
->base_oid
);
1361 need_check_tiering
= true;
1363 if (ceph_oloc_empty(&t
->target_oloc
) || force_resend
) {
1364 ceph_oloc_copy(&t
->target_oloc
, &t
->base_oloc
);
1365 need_check_tiering
= true;
1368 if (need_check_tiering
&&
1369 (t
->flags
& CEPH_OSD_FLAG_IGNORE_OVERLAY
) == 0) {
1370 if (t
->flags
& CEPH_OSD_FLAG_READ
&& pi
->read_tier
>= 0)
1371 t
->target_oloc
.pool
= pi
->read_tier
;
1372 if (t
->flags
& CEPH_OSD_FLAG_WRITE
&& pi
->write_tier
>= 0)
1373 t
->target_oloc
.pool
= pi
->write_tier
;
1376 ret
= ceph_object_locator_to_pg(osdc
->osdmap
, &t
->target_oid
,
1377 &t
->target_oloc
, &pgid
);
1379 WARN_ON(ret
!= -ENOENT
);
1380 t
->osd
= CEPH_HOMELESS_OSD
;
1381 ct_res
= CALC_TARGET_POOL_DNE
;
1384 last_pgid
.pool
= pgid
.pool
;
1385 last_pgid
.seed
= ceph_stable_mod(pgid
.seed
, t
->pg_num
, t
->pg_num_mask
);
1387 ceph_pg_to_up_acting_osds(osdc
->osdmap
, &pgid
, &up
, &acting
);
1389 ceph_is_new_interval(&t
->acting
,
1402 force_resend
= true;
1404 if (t
->paused
&& !target_should_be_paused(osdc
, t
, pi
)) {
1409 if (ceph_pg_compare(&t
->pgid
, &pgid
) ||
1410 ceph_osds_changed(&t
->acting
, &acting
, any_change
) ||
1412 t
->pgid
= pgid
; /* struct */
1413 ceph_osds_copy(&t
->acting
, &acting
);
1414 ceph_osds_copy(&t
->up
, &up
);
1416 t
->min_size
= pi
->min_size
;
1417 t
->pg_num
= pi
->pg_num
;
1418 t
->pg_num_mask
= pi
->pg_num_mask
;
1419 t
->sort_bitwise
= sort_bitwise
;
1421 t
->osd
= acting
.primary
;
1425 ct_res
= need_resend
? CALC_TARGET_NEED_RESEND
: CALC_TARGET_NO_ACTION
;
1427 dout("%s t %p -> ct_res %d osd %d\n", __func__
, t
, ct_res
, t
->osd
);
1431 static void __enqueue_request(struct ceph_osd_request
*req
)
1433 struct ceph_osd_client
*osdc
= req
->r_osdc
;
1435 dout("%s %p tid %llu to osd%d\n", __func__
, req
, req
->r_tid
,
1436 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1439 __remove_osd_from_lru(req
->r_osd
);
1440 list_add_tail(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1441 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1443 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1448 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1449 * (as needed), and set the request r_osd appropriately. If there is
1450 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1451 * (unsent, homeless) or leave on in-flight lru.
1453 * Return 0 if unchanged, 1 if changed, or negative on error.
1455 * Caller should hold map_sem for read and request_mutex.
1457 static int __map_request(struct ceph_osd_client
*osdc
,
1458 struct ceph_osd_request
*req
, int force_resend
)
1460 enum calc_target_result ct_res
;
1463 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
1465 ct_res
= calc_target(osdc
, &req
->r_t
, NULL
, force_resend
);
1467 case CALC_TARGET_POOL_DNE
:
1468 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1470 case CALC_TARGET_NO_ACTION
:
1471 return 0; /* no change */
1473 BUG_ON(ct_res
!= CALC_TARGET_NEED_RESEND
);
1476 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1477 req
->r_tid
, req
->r_t
.pgid
.pool
, req
->r_t
.pgid
.seed
, req
->r_t
.osd
,
1478 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1481 __cancel_request(req
);
1482 list_del_init(&req
->r_osd_item
);
1483 list_del_init(&req
->r_linger_osd_item
);
1487 req
->r_osd
= lookup_osd(&osdc
->osds
, req
->r_t
.osd
);
1488 if (!req
->r_osd
&& req
->r_t
.osd
>= 0) {
1490 req
->r_osd
= create_osd(osdc
, req
->r_t
.osd
);
1492 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1496 dout("map_request osd %p is osd%d\n", req
->r_osd
,
1498 insert_osd(&osdc
->osds
, req
->r_osd
);
1500 ceph_con_open(&req
->r_osd
->o_con
,
1501 CEPH_ENTITY_TYPE_OSD
, req
->r_osd
->o_osd
,
1502 &osdc
->osdmap
->osd_addr
[req
->r_osd
->o_osd
]);
1505 __enqueue_request(req
);
1506 err
= 1; /* osd or pg changed */
1513 * caller should hold map_sem (for read) and request_mutex
1515 static void __send_request(struct ceph_osd_client
*osdc
,
1516 struct ceph_osd_request
*req
)
1520 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1521 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
,
1522 req
->r_t
.pgid
.pool
, req
->r_t
.pgid
.seed
);
1524 /* fill in message content that changes each time we send it */
1525 put_unaligned_le32(osdc
->osdmap
->epoch
, req
->r_request_osdmap_epoch
);
1526 put_unaligned_le32(req
->r_flags
, req
->r_request_flags
);
1527 put_unaligned_le64(req
->r_t
.target_oloc
.pool
, req
->r_request_pool
);
1528 p
= req
->r_request_pgid
;
1529 ceph_encode_64(&p
, req
->r_t
.pgid
.pool
);
1530 ceph_encode_32(&p
, req
->r_t
.pgid
.seed
);
1531 put_unaligned_le64(1, req
->r_request_attempts
); /* FIXME */
1532 memcpy(req
->r_request_reassert_version
, &req
->r_reassert_version
,
1533 sizeof(req
->r_reassert_version
));
1535 req
->r_stamp
= jiffies
;
1536 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1538 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1540 req
->r_sent
= req
->r_osd
->o_incarnation
;
1542 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1546 * Send any requests in the queue (req_unsent).
1548 static void __send_queued(struct ceph_osd_client
*osdc
)
1550 struct ceph_osd_request
*req
, *tmp
;
1552 dout("__send_queued\n");
1553 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
)
1554 __send_request(osdc
, req
);
1558 * Caller should hold map_sem for read and request_mutex.
1560 static int __ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1561 struct ceph_osd_request
*req
,
1566 __register_request(osdc
, req
);
1568 req
->r_got_reply
= 0;
1569 rc
= __map_request(osdc
, req
, 0);
1572 dout("osdc_start_request failed map, "
1573 " will retry %lld\n", req
->r_tid
);
1576 __unregister_request(osdc
, req
);
1581 if (req
->r_osd
== NULL
) {
1582 dout("send_request %p no up osds in pg\n", req
);
1583 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1585 __send_queued(osdc
);
1592 * Timeout callback, called every N seconds when 1 or more osd
1593 * requests has been active for more than N seconds. When this
1594 * happens, we ping all OSDs with requests who have timed out to
1595 * ensure any communications channel reset is detected. Reset the
1596 * request timeouts another N seconds in the future as we go.
1597 * Reschedule the timeout event another N seconds in future (unless
1598 * there are no open requests).
1600 static void handle_timeout(struct work_struct
*work
)
1602 struct ceph_osd_client
*osdc
=
1603 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1604 struct ceph_options
*opts
= osdc
->client
->options
;
1605 struct ceph_osd_request
*req
;
1606 struct ceph_osd
*osd
;
1607 struct list_head slow_osds
;
1609 down_read(&osdc
->map_sem
);
1611 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1613 mutex_lock(&osdc
->request_mutex
);
1616 * ping osds that are a bit slow. this ensures that if there
1617 * is a break in the TCP connection we will notice, and reopen
1618 * a connection with that osd (from the fault callback).
1620 INIT_LIST_HEAD(&slow_osds
);
1621 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1622 if (time_before(jiffies
,
1623 req
->r_stamp
+ opts
->osd_keepalive_timeout
))
1628 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1629 req
->r_tid
, osd
->o_osd
);
1630 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1632 while (!list_empty(&slow_osds
)) {
1633 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1635 list_del_init(&osd
->o_keepalive_item
);
1636 ceph_con_keepalive(&osd
->o_con
);
1639 __schedule_osd_timeout(osdc
);
1640 __send_queued(osdc
);
1641 mutex_unlock(&osdc
->request_mutex
);
1642 up_read(&osdc
->map_sem
);
1645 static void handle_osds_timeout(struct work_struct
*work
)
1647 struct ceph_osd_client
*osdc
=
1648 container_of(work
, struct ceph_osd_client
,
1649 osds_timeout_work
.work
);
1650 unsigned long delay
= osdc
->client
->options
->osd_idle_ttl
/ 4;
1651 struct ceph_osd
*osd
, *nosd
;
1653 dout("%s osdc %p\n", __func__
, osdc
);
1654 down_read(&osdc
->map_sem
);
1655 mutex_lock(&osdc
->request_mutex
);
1657 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
1658 if (time_before(jiffies
, osd
->lru_ttl
))
1661 remove_osd(osdc
, osd
);
1664 mutex_unlock(&osdc
->request_mutex
);
1665 up_read(&osdc
->map_sem
);
1666 schedule_delayed_work(&osdc
->osds_timeout_work
,
1667 round_jiffies_relative(delay
));
1670 static int ceph_oloc_decode(void **p
, void *end
,
1671 struct ceph_object_locator
*oloc
)
1673 u8 struct_v
, struct_cv
;
1678 ceph_decode_need(p
, end
, 1 + 1 + 4, e_inval
);
1679 struct_v
= ceph_decode_8(p
);
1680 struct_cv
= ceph_decode_8(p
);
1682 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1683 struct_v
, struct_cv
);
1686 if (struct_cv
> 6) {
1687 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1688 struct_v
, struct_cv
);
1691 len
= ceph_decode_32(p
);
1692 ceph_decode_need(p
, end
, len
, e_inval
);
1693 struct_end
= *p
+ len
;
1695 oloc
->pool
= ceph_decode_64(p
);
1696 *p
+= 4; /* skip preferred */
1698 len
= ceph_decode_32(p
);
1700 pr_warn("ceph_object_locator::key is set\n");
1704 if (struct_v
>= 5) {
1705 len
= ceph_decode_32(p
);
1707 pr_warn("ceph_object_locator::nspace is set\n");
1712 if (struct_v
>= 6) {
1713 s64 hash
= ceph_decode_64(p
);
1715 pr_warn("ceph_object_locator::hash is set\n");
1730 static int ceph_redirect_decode(void **p
, void *end
,
1731 struct ceph_request_redirect
*redir
)
1733 u8 struct_v
, struct_cv
;
1738 ceph_decode_need(p
, end
, 1 + 1 + 4, e_inval
);
1739 struct_v
= ceph_decode_8(p
);
1740 struct_cv
= ceph_decode_8(p
);
1741 if (struct_cv
> 1) {
1742 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1743 struct_v
, struct_cv
);
1746 len
= ceph_decode_32(p
);
1747 ceph_decode_need(p
, end
, len
, e_inval
);
1748 struct_end
= *p
+ len
;
1750 ret
= ceph_oloc_decode(p
, end
, &redir
->oloc
);
1754 len
= ceph_decode_32(p
);
1756 pr_warn("ceph_request_redirect::object_name is set\n");
1760 len
= ceph_decode_32(p
);
1761 *p
+= len
; /* skip osd_instructions */
1773 static void complete_request(struct ceph_osd_request
*req
)
1775 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1779 * handle osd op reply. either call the callback if it is specified,
1780 * or do the completion to wake up the waiting thread.
1782 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1785 struct ceph_osd_request
*req
;
1786 struct ceph_request_redirect redir
;
1789 unsigned int numops
;
1790 int payload_len
, flags
;
1796 u64 reassert_version
;
1798 int already_completed
;
1803 tid
= le64_to_cpu(msg
->hdr
.tid
);
1804 dout("handle_reply %p tid %llu\n", msg
, tid
);
1806 p
= msg
->front
.iov_base
;
1807 end
= p
+ msg
->front
.iov_len
;
1809 ceph_decode_need(&p
, end
, 4, bad
);
1810 object_len
= ceph_decode_32(&p
);
1811 ceph_decode_need(&p
, end
, object_len
, bad
);
1814 err
= ceph_decode_pgid(&p
, end
, &pg
);
1818 ceph_decode_need(&p
, end
, 8 + 4 + 4 + 8 + 4, bad
);
1819 flags
= ceph_decode_64(&p
);
1820 result
= ceph_decode_32(&p
);
1821 reassert_epoch
= ceph_decode_32(&p
);
1822 reassert_version
= ceph_decode_64(&p
);
1823 osdmap_epoch
= ceph_decode_32(&p
);
1826 down_read(&osdc
->map_sem
);
1827 mutex_lock(&osdc
->request_mutex
);
1828 req
= lookup_request(&osdc
->requests
, tid
);
1830 dout("handle_reply tid %llu dne\n", tid
);
1833 ceph_osdc_get_request(req
);
1835 dout("handle_reply %p tid %llu req %p result %d\n", msg
, tid
,
1838 ceph_decode_need(&p
, end
, 4, bad_put
);
1839 numops
= ceph_decode_32(&p
);
1840 if (numops
> CEPH_OSD_MAX_OPS
)
1842 if (numops
!= req
->r_num_ops
)
1845 ceph_decode_need(&p
, end
, numops
* sizeof(struct ceph_osd_op
), bad_put
);
1846 for (i
= 0; i
< numops
; i
++) {
1847 struct ceph_osd_op
*op
= p
;
1850 len
= le32_to_cpu(op
->payload_len
);
1851 req
->r_ops
[i
].outdata_len
= len
;
1852 dout(" op %d has %d bytes\n", i
, len
);
1856 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1857 if (payload_len
!= bytes
) {
1858 pr_warn("sum of op payload lens %d != data_len %d\n",
1859 payload_len
, bytes
);
1863 ceph_decode_need(&p
, end
, 4 + numops
* 4, bad_put
);
1864 retry_attempt
= ceph_decode_32(&p
);
1865 for (i
= 0; i
< numops
; i
++)
1866 req
->r_ops
[i
].rval
= ceph_decode_32(&p
);
1868 if (le16_to_cpu(msg
->hdr
.version
) >= 6) {
1869 p
+= 8 + 4; /* skip replay_version */
1870 p
+= 8; /* skip user_version */
1872 if (le16_to_cpu(msg
->hdr
.version
) >= 7)
1873 ceph_decode_8_safe(&p
, end
, decode_redir
, bad_put
);
1881 err
= ceph_redirect_decode(&p
, end
, &redir
);
1885 redir
.oloc
.pool
= -1;
1888 if (!ceph_oloc_empty(&redir
.oloc
)) {
1889 dout("redirect pool %lld\n", redir
.oloc
.pool
);
1891 __unregister_request(osdc
, req
);
1893 ceph_oloc_copy(&req
->r_t
.target_oloc
, &redir
.oloc
);
1896 * Start redirect requests with nofail=true. If
1897 * mapping fails, request will end up on the notarget
1898 * list, waiting for the new osdmap (which can take
1899 * a while), even though the original request mapped
1900 * successfully. In the future we might want to follow
1901 * original request's nofail setting here.
1903 err
= __ceph_osdc_start_request(osdc
, req
, true);
1909 already_completed
= req
->r_got_reply
;
1910 if (!req
->r_got_reply
) {
1911 req
->r_result
= result
;
1912 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1914 if (req
->r_result
== 0)
1915 req
->r_result
= bytes
;
1917 /* in case this is a write and we need to replay, */
1918 req
->r_reassert_version
.epoch
= cpu_to_le32(reassert_epoch
);
1919 req
->r_reassert_version
.version
= cpu_to_le64(reassert_version
);
1921 req
->r_got_reply
= 1;
1922 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1923 dout("handle_reply tid %llu dup ack\n", tid
);
1927 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1929 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1930 __register_linger_request(osdc
, req
);
1932 /* either this is a read, or we got the safe response */
1934 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1935 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1936 __unregister_request(osdc
, req
);
1938 mutex_unlock(&osdc
->request_mutex
);
1939 up_read(&osdc
->map_sem
);
1941 if (!already_completed
) {
1942 if (req
->r_unsafe_callback
&&
1943 result
>= 0 && !(flags
& CEPH_OSD_FLAG_ONDISK
))
1944 req
->r_unsafe_callback(req
, true);
1945 if (req
->r_callback
)
1946 req
->r_callback(req
, msg
);
1948 complete_all(&req
->r_completion
);
1951 if (flags
& CEPH_OSD_FLAG_ONDISK
) {
1952 if (req
->r_unsafe_callback
&& already_completed
)
1953 req
->r_unsafe_callback(req
, false);
1954 complete_request(req
);
1958 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1959 ceph_osdc_put_request(req
);
1962 mutex_unlock(&osdc
->request_mutex
);
1963 up_read(&osdc
->map_sem
);
1967 req
->r_result
= -EIO
;
1968 __unregister_request(osdc
, req
);
1969 if (req
->r_callback
)
1970 req
->r_callback(req
, msg
);
1972 complete_all(&req
->r_completion
);
1973 complete_request(req
);
1974 ceph_osdc_put_request(req
);
1976 mutex_unlock(&osdc
->request_mutex
);
1977 up_read(&osdc
->map_sem
);
1979 pr_err("corrupt osd_op_reply got %d %d\n",
1980 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
));
1984 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1986 struct rb_node
*p
, *n
;
1988 dout("%s %p\n", __func__
, osdc
);
1989 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1990 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1993 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1994 memcmp(&osd
->o_con
.peer_addr
,
1995 ceph_osd_addr(osdc
->osdmap
,
1997 sizeof(struct ceph_entity_addr
)) != 0)
1998 __reset_osd(osdc
, osd
);
2003 * Requeue requests whose mapping to an OSD has changed. If requests map to
2004 * no osd, request a new map.
2006 * Caller should hold map_sem for read.
2008 static void kick_requests(struct ceph_osd_client
*osdc
, bool force_resend
,
2009 bool force_resend_writes
)
2011 struct ceph_osd_request
*req
, *nreq
;
2015 bool force_resend_req
;
2017 dout("kick_requests %s %s\n", force_resend
? " (force resend)" : "",
2018 force_resend_writes
? " (force resend writes)" : "");
2019 mutex_lock(&osdc
->request_mutex
);
2020 for (p
= rb_first(&osdc
->requests
); p
; ) {
2021 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
2025 * For linger requests that have not yet been
2026 * registered, move them to the linger list; they'll
2027 * be sent to the osd in the loop below. Unregister
2028 * the request before re-registering it as a linger
2029 * request to ensure the __map_request() below
2030 * will decide it needs to be sent.
2032 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
2033 dout("%p tid %llu restart on osd%d\n",
2035 req
->r_osd
? req
->r_osd
->o_osd
: -1);
2036 ceph_osdc_get_request(req
);
2037 __unregister_request(osdc
, req
);
2038 __register_linger_request(osdc
, req
);
2039 ceph_osdc_put_request(req
);
2043 force_resend_req
= force_resend
||
2044 (force_resend_writes
&&
2045 req
->r_flags
& CEPH_OSD_FLAG_WRITE
);
2046 err
= __map_request(osdc
, req
, force_resend_req
);
2048 continue; /* error */
2049 if (req
->r_osd
== NULL
) {
2050 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
2051 needmap
++; /* request a newer map */
2052 } else if (err
> 0) {
2053 if (!req
->r_linger
) {
2054 dout("%p tid %llu requeued on osd%d\n", req
,
2056 req
->r_osd
? req
->r_osd
->o_osd
: -1);
2057 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
2062 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
2064 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
2066 err
= __map_request(osdc
, req
,
2067 force_resend
|| force_resend_writes
);
2068 dout("__map_request returned %d\n", err
);
2070 continue; /* hrm! */
2071 if (req
->r_osd
== NULL
|| err
> 0) {
2072 if (req
->r_osd
== NULL
) {
2073 dout("lingering %p tid %llu maps to no osd\n",
2076 * A homeless lingering request makes
2077 * no sense, as it's job is to keep
2078 * a particular OSD connection open.
2079 * Request a newer map and kick the
2080 * request, knowing that it won't be
2081 * resent until we actually get a map
2082 * that can tell us where to send it.
2087 dout("kicking lingering %p tid %llu osd%d\n", req
,
2088 req
->r_tid
, req
->r_osd
? req
->r_osd
->o_osd
: -1);
2089 __register_request(osdc
, req
);
2090 __unregister_linger_request(osdc
, req
);
2093 reset_changed_osds(osdc
);
2094 mutex_unlock(&osdc
->request_mutex
);
2097 dout("%d requests for down osds, need new map\n", needmap
);
2098 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
2104 * Process updated osd map.
2106 * The message contains any number of incremental and full maps, normally
2107 * indicating some sort of topology change in the cluster. Kick requests
2108 * off to different OSDs as needed.
2110 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
2112 void *p
, *end
, *next
;
2113 u32 nr_maps
, maplen
;
2115 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
2117 struct ceph_fsid fsid
;
2120 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
2121 p
= msg
->front
.iov_base
;
2122 end
= p
+ msg
->front
.iov_len
;
2125 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
2126 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
2127 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
2130 down_write(&osdc
->map_sem
);
2132 was_full
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
);
2134 /* incremental maps */
2135 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
2136 dout(" %d inc maps\n", nr_maps
);
2137 while (nr_maps
> 0) {
2138 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2139 epoch
= ceph_decode_32(&p
);
2140 maplen
= ceph_decode_32(&p
);
2141 ceph_decode_need(&p
, end
, maplen
, bad
);
2143 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
2144 dout("applying incremental map %u len %d\n",
2146 newmap
= osdmap_apply_incremental(&p
, next
,
2148 if (IS_ERR(newmap
)) {
2149 err
= PTR_ERR(newmap
);
2153 if (newmap
!= osdc
->osdmap
) {
2154 ceph_osdmap_destroy(osdc
->osdmap
);
2155 osdc
->osdmap
= newmap
;
2157 was_full
= was_full
||
2158 ceph_osdmap_flag(osdc
->osdmap
,
2160 kick_requests(osdc
, 0, was_full
);
2162 dout("ignoring incremental map %u len %d\n",
2172 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
2173 dout(" %d full maps\n", nr_maps
);
2175 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2176 epoch
= ceph_decode_32(&p
);
2177 maplen
= ceph_decode_32(&p
);
2178 ceph_decode_need(&p
, end
, maplen
, bad
);
2180 dout("skipping non-latest full map %u len %d\n",
2182 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
2183 dout("skipping full map %u len %d, "
2184 "older than our %u\n", epoch
, maplen
,
2185 osdc
->osdmap
->epoch
);
2187 int skipped_map
= 0;
2189 dout("taking full map %u len %d\n", epoch
, maplen
);
2190 newmap
= ceph_osdmap_decode(&p
, p
+maplen
);
2191 if (IS_ERR(newmap
)) {
2192 err
= PTR_ERR(newmap
);
2196 oldmap
= osdc
->osdmap
;
2197 osdc
->osdmap
= newmap
;
2199 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
2201 ceph_osdmap_destroy(oldmap
);
2203 was_full
= was_full
||
2204 ceph_osdmap_flag(osdc
->osdmap
,
2206 kick_requests(osdc
, skipped_map
, was_full
);
2215 downgrade_write(&osdc
->map_sem
);
2216 ceph_monc_got_map(&osdc
->client
->monc
, CEPH_SUB_OSDMAP
,
2217 osdc
->osdmap
->epoch
);
2220 * subscribe to subsequent osdmap updates if full to ensure
2221 * we find out when we are no longer full and stop returning
2224 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
) ||
2225 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSERD
) ||
2226 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSEWR
))
2227 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
2229 mutex_lock(&osdc
->request_mutex
);
2230 __send_queued(osdc
);
2231 mutex_unlock(&osdc
->request_mutex
);
2232 up_read(&osdc
->map_sem
);
2233 wake_up_all(&osdc
->client
->auth_wq
);
2237 pr_err("osdc handle_map corrupt msg\n");
2239 up_write(&osdc
->map_sem
);
2243 * watch/notify callback event infrastructure
2245 * These callbacks are used both for watch and notify operations.
2247 static void __release_event(struct kref
*kref
)
2249 struct ceph_osd_event
*event
=
2250 container_of(kref
, struct ceph_osd_event
, kref
);
2252 dout("__release_event %p\n", event
);
2256 static void get_event(struct ceph_osd_event
*event
)
2258 kref_get(&event
->kref
);
2261 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
2263 kref_put(&event
->kref
, __release_event
);
2265 EXPORT_SYMBOL(ceph_osdc_put_event
);
2267 static void __insert_event(struct ceph_osd_client
*osdc
,
2268 struct ceph_osd_event
*new)
2270 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
2271 struct rb_node
*parent
= NULL
;
2272 struct ceph_osd_event
*event
= NULL
;
2276 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
2277 if (new->cookie
< event
->cookie
)
2279 else if (new->cookie
> event
->cookie
)
2280 p
= &(*p
)->rb_right
;
2285 rb_link_node(&new->node
, parent
, p
);
2286 rb_insert_color(&new->node
, &osdc
->event_tree
);
2289 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
2292 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
2293 struct rb_node
*parent
= NULL
;
2294 struct ceph_osd_event
*event
= NULL
;
2298 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
2299 if (cookie
< event
->cookie
)
2301 else if (cookie
> event
->cookie
)
2302 p
= &(*p
)->rb_right
;
2309 static void __remove_event(struct ceph_osd_event
*event
)
2311 struct ceph_osd_client
*osdc
= event
->osdc
;
2313 if (!RB_EMPTY_NODE(&event
->node
)) {
2314 dout("__remove_event removed %p\n", event
);
2315 rb_erase(&event
->node
, &osdc
->event_tree
);
2316 ceph_osdc_put_event(event
);
2318 dout("__remove_event didn't remove %p\n", event
);
2322 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
2323 void (*event_cb
)(u64
, u64
, u8
, void *),
2324 void *data
, struct ceph_osd_event
**pevent
)
2326 struct ceph_osd_event
*event
;
2328 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
2332 dout("create_event %p\n", event
);
2333 event
->cb
= event_cb
;
2334 event
->one_shot
= 0;
2337 INIT_LIST_HEAD(&event
->osd_node
);
2338 RB_CLEAR_NODE(&event
->node
);
2339 kref_init(&event
->kref
); /* one ref for us */
2340 kref_get(&event
->kref
); /* one ref for the caller */
2342 spin_lock(&osdc
->event_lock
);
2343 event
->cookie
= ++osdc
->event_count
;
2344 __insert_event(osdc
, event
);
2345 spin_unlock(&osdc
->event_lock
);
2350 EXPORT_SYMBOL(ceph_osdc_create_event
);
2352 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
2354 struct ceph_osd_client
*osdc
= event
->osdc
;
2356 dout("cancel_event %p\n", event
);
2357 spin_lock(&osdc
->event_lock
);
2358 __remove_event(event
);
2359 spin_unlock(&osdc
->event_lock
);
2360 ceph_osdc_put_event(event
); /* caller's */
2362 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
2365 static void do_event_work(struct work_struct
*work
)
2367 struct ceph_osd_event_work
*event_work
=
2368 container_of(work
, struct ceph_osd_event_work
, work
);
2369 struct ceph_osd_event
*event
= event_work
->event
;
2370 u64 ver
= event_work
->ver
;
2371 u64 notify_id
= event_work
->notify_id
;
2372 u8 opcode
= event_work
->opcode
;
2374 dout("do_event_work completing %p\n", event
);
2375 event
->cb(ver
, notify_id
, opcode
, event
->data
);
2376 dout("do_event_work completed %p\n", event
);
2377 ceph_osdc_put_event(event
);
2383 * Process osd watch notifications
2385 static void handle_watch_notify(struct ceph_osd_client
*osdc
,
2386 struct ceph_msg
*msg
)
2390 u64 cookie
, ver
, notify_id
;
2392 struct ceph_osd_event
*event
;
2393 struct ceph_osd_event_work
*event_work
;
2395 p
= msg
->front
.iov_base
;
2396 end
= p
+ msg
->front
.iov_len
;
2398 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
2399 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
2400 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
2401 ceph_decode_64_safe(&p
, end
, ver
, bad
);
2402 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
2404 spin_lock(&osdc
->event_lock
);
2405 event
= __find_event(osdc
, cookie
);
2407 BUG_ON(event
->one_shot
);
2410 spin_unlock(&osdc
->event_lock
);
2411 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2412 cookie
, ver
, event
);
2414 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
2416 pr_err("couldn't allocate event_work\n");
2417 ceph_osdc_put_event(event
);
2420 INIT_WORK(&event_work
->work
, do_event_work
);
2421 event_work
->event
= event
;
2422 event_work
->ver
= ver
;
2423 event_work
->notify_id
= notify_id
;
2424 event_work
->opcode
= opcode
;
2426 queue_work(osdc
->notify_wq
, &event_work
->work
);
2432 pr_err("osdc handle_watch_notify corrupt msg\n");
2436 * build new request AND message
2439 void ceph_osdc_build_request(struct ceph_osd_request
*req
, u64 off
,
2440 struct ceph_snap_context
*snapc
, u64 snap_id
,
2441 struct timespec
*mtime
)
2443 struct ceph_msg
*msg
= req
->r_request
;
2446 int flags
= req
->r_flags
;
2450 req
->r_snapid
= snap_id
;
2451 WARN_ON(snapc
!= req
->r_snapc
);
2453 /* encode request */
2454 msg
->hdr
.version
= cpu_to_le16(4);
2456 p
= msg
->front
.iov_base
;
2457 ceph_encode_32(&p
, 1); /* client_inc is always 1 */
2458 req
->r_request_osdmap_epoch
= p
;
2460 req
->r_request_flags
= p
;
2462 if (req
->r_flags
& CEPH_OSD_FLAG_WRITE
)
2463 ceph_encode_timespec(p
, mtime
);
2464 p
+= sizeof(struct ceph_timespec
);
2465 req
->r_request_reassert_version
= p
;
2466 p
+= sizeof(struct ceph_eversion
); /* will get filled in */
2469 ceph_encode_8(&p
, 4);
2470 ceph_encode_8(&p
, 4);
2471 ceph_encode_32(&p
, 8 + 4 + 4);
2472 req
->r_request_pool
= p
;
2474 ceph_encode_32(&p
, -1); /* preferred */
2475 ceph_encode_32(&p
, 0); /* key len */
2477 ceph_encode_8(&p
, 1);
2478 req
->r_request_pgid
= p
;
2480 ceph_encode_32(&p
, -1); /* preferred */
2483 ceph_encode_32(&p
, req
->r_base_oid
.name_len
);
2484 memcpy(p
, req
->r_base_oid
.name
, req
->r_base_oid
.name_len
);
2485 dout("oid %*pE len %d\n", req
->r_base_oid
.name_len
,
2486 req
->r_base_oid
.name
, req
->r_base_oid
.name_len
);
2487 p
+= req
->r_base_oid
.name_len
;
2489 /* ops--can imply data */
2490 ceph_encode_16(&p
, (u16
)req
->r_num_ops
);
2492 for (i
= 0; i
< req
->r_num_ops
; i
++) {
2493 data_len
+= osd_req_encode_op(req
, p
, i
);
2494 p
+= sizeof(struct ceph_osd_op
);
2498 ceph_encode_64(&p
, req
->r_snapid
);
2499 ceph_encode_64(&p
, req
->r_snapc
? req
->r_snapc
->seq
: 0);
2500 ceph_encode_32(&p
, req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
2502 for (i
= 0; i
< req
->r_snapc
->num_snaps
; i
++) {
2503 ceph_encode_64(&p
, req
->r_snapc
->snaps
[i
]);
2507 req
->r_request_attempts
= p
;
2511 if (flags
& CEPH_OSD_FLAG_WRITE
) {
2515 * The header "data_off" is a hint to the receiver
2516 * allowing it to align received data into its
2517 * buffers such that there's no need to re-copy
2518 * it before writing it to disk (direct I/O).
2520 data_off
= (u16
) (off
& 0xffff);
2521 req
->r_request
->hdr
.data_off
= cpu_to_le16(data_off
);
2523 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
2525 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
2526 msg_size
= p
- msg
->front
.iov_base
;
2527 msg
->front
.iov_len
= msg_size
;
2528 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
2530 dout("build_request msg_size was %d\n", (int)msg_size
);
2532 EXPORT_SYMBOL(ceph_osdc_build_request
);
2535 * Register request, send initial attempt.
2537 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
2538 struct ceph_osd_request
*req
,
2543 down_read(&osdc
->map_sem
);
2544 mutex_lock(&osdc
->request_mutex
);
2546 rc
= __ceph_osdc_start_request(osdc
, req
, nofail
);
2548 mutex_unlock(&osdc
->request_mutex
);
2549 up_read(&osdc
->map_sem
);
2553 EXPORT_SYMBOL(ceph_osdc_start_request
);
2556 * Unregister a registered request. The request is not completed (i.e.
2557 * no callbacks or wakeups) - higher layers are supposed to know what
2558 * they are canceling.
2560 void ceph_osdc_cancel_request(struct ceph_osd_request
*req
)
2562 struct ceph_osd_client
*osdc
= req
->r_osdc
;
2564 mutex_lock(&osdc
->request_mutex
);
2566 __unregister_linger_request(osdc
, req
);
2567 __unregister_request(osdc
, req
);
2568 mutex_unlock(&osdc
->request_mutex
);
2570 dout("%s %p tid %llu canceled\n", __func__
, req
, req
->r_tid
);
2572 EXPORT_SYMBOL(ceph_osdc_cancel_request
);
2575 * wait for a request to complete
2577 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
2578 struct ceph_osd_request
*req
)
2582 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
2584 rc
= wait_for_completion_interruptible(&req
->r_completion
);
2586 dout("%s %p tid %llu interrupted\n", __func__
, req
, req
->r_tid
);
2587 ceph_osdc_cancel_request(req
);
2588 complete_request(req
);
2592 dout("%s %p tid %llu result %d\n", __func__
, req
, req
->r_tid
,
2594 return req
->r_result
;
2596 EXPORT_SYMBOL(ceph_osdc_wait_request
);
2599 * sync - wait for all in-flight requests to flush. avoid starvation.
2601 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
2603 struct ceph_osd_request
*req
;
2604 u64 last_tid
, next_tid
= 0;
2606 mutex_lock(&osdc
->request_mutex
);
2607 last_tid
= osdc
->last_tid
;
2609 req
= __lookup_request_ge(osdc
, next_tid
);
2612 if (req
->r_tid
> last_tid
)
2615 next_tid
= req
->r_tid
+ 1;
2616 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
2619 ceph_osdc_get_request(req
);
2620 mutex_unlock(&osdc
->request_mutex
);
2621 dout("sync waiting on tid %llu (last is %llu)\n",
2622 req
->r_tid
, last_tid
);
2623 wait_for_completion(&req
->r_safe_completion
);
2624 mutex_lock(&osdc
->request_mutex
);
2625 ceph_osdc_put_request(req
);
2627 mutex_unlock(&osdc
->request_mutex
);
2628 dout("sync done (thru tid %llu)\n", last_tid
);
2630 EXPORT_SYMBOL(ceph_osdc_sync
);
2633 * Call all pending notify callbacks - for use after a watch is
2634 * unregistered, to make sure no more callbacks for it will be invoked
2636 void ceph_osdc_flush_notifies(struct ceph_osd_client
*osdc
)
2638 flush_workqueue(osdc
->notify_wq
);
2640 EXPORT_SYMBOL(ceph_osdc_flush_notifies
);
2646 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
2651 osdc
->client
= client
;
2652 osdc
->osdmap
= NULL
;
2653 init_rwsem(&osdc
->map_sem
);
2654 mutex_init(&osdc
->request_mutex
);
2656 osdc
->osds
= RB_ROOT
;
2657 INIT_LIST_HEAD(&osdc
->osd_lru
);
2658 osdc
->requests
= RB_ROOT
;
2659 INIT_LIST_HEAD(&osdc
->req_lru
);
2660 INIT_LIST_HEAD(&osdc
->req_unsent
);
2661 INIT_LIST_HEAD(&osdc
->req_notarget
);
2662 INIT_LIST_HEAD(&osdc
->req_linger
);
2663 osdc
->num_requests
= 0;
2664 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
2665 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
2666 spin_lock_init(&osdc
->event_lock
);
2667 osdc
->event_tree
= RB_ROOT
;
2668 osdc
->event_count
= 0;
2670 schedule_delayed_work(&osdc
->osds_timeout_work
,
2671 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
));
2674 osdc
->req_mempool
= mempool_create_slab_pool(10,
2675 ceph_osd_request_cache
);
2676 if (!osdc
->req_mempool
)
2679 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
2680 PAGE_SIZE
, 10, true, "osd_op");
2683 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
2684 PAGE_SIZE
, 10, true, "osd_op_reply");
2689 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
2690 if (!osdc
->notify_wq
)
2691 goto out_msgpool_reply
;
2696 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2698 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2700 mempool_destroy(osdc
->req_mempool
);
2705 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
2707 flush_workqueue(osdc
->notify_wq
);
2708 destroy_workqueue(osdc
->notify_wq
);
2709 cancel_delayed_work_sync(&osdc
->timeout_work
);
2710 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
2712 mutex_lock(&osdc
->request_mutex
);
2713 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
2714 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
2715 struct ceph_osd
, o_node
);
2716 remove_osd(osdc
, osd
);
2718 mutex_unlock(&osdc
->request_mutex
);
2721 ceph_osdmap_destroy(osdc
->osdmap
);
2722 osdc
->osdmap
= NULL
;
2724 mempool_destroy(osdc
->req_mempool
);
2725 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2726 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2730 * Read some contiguous pages. If we cross a stripe boundary, shorten
2731 * *plen. Return number of bytes read, or error.
2733 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
2734 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
2736 u32 truncate_seq
, u64 truncate_size
,
2737 struct page
**pages
, int num_pages
, int page_align
)
2739 struct ceph_osd_request
*req
;
2742 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
2743 vino
.snap
, off
, *plen
);
2744 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
, 0, 1,
2745 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
2746 NULL
, truncate_seq
, truncate_size
,
2749 return PTR_ERR(req
);
2751 /* it may be a short read due to an object boundary */
2753 osd_req_op_extent_osd_data_pages(req
, 0,
2754 pages
, *plen
, page_align
, false, false);
2756 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2757 off
, *plen
, *plen
, page_align
);
2759 ceph_osdc_build_request(req
, off
, NULL
, vino
.snap
, NULL
);
2761 rc
= ceph_osdc_start_request(osdc
, req
, false);
2763 rc
= ceph_osdc_wait_request(osdc
, req
);
2765 ceph_osdc_put_request(req
);
2766 dout("readpages result %d\n", rc
);
2769 EXPORT_SYMBOL(ceph_osdc_readpages
);
2772 * do a synchronous write on N pages
2774 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
2775 struct ceph_file_layout
*layout
,
2776 struct ceph_snap_context
*snapc
,
2778 u32 truncate_seq
, u64 truncate_size
,
2779 struct timespec
*mtime
,
2780 struct page
**pages
, int num_pages
)
2782 struct ceph_osd_request
*req
;
2784 int page_align
= off
& ~PAGE_MASK
;
2786 BUG_ON(vino
.snap
!= CEPH_NOSNAP
); /* snapshots aren't writeable */
2787 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
, 0, 1,
2789 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
2790 snapc
, truncate_seq
, truncate_size
,
2793 return PTR_ERR(req
);
2795 /* it may be a short write due to an object boundary */
2796 osd_req_op_extent_osd_data_pages(req
, 0, pages
, len
, page_align
,
2798 dout("writepages %llu~%llu (%llu bytes)\n", off
, len
, len
);
2800 ceph_osdc_build_request(req
, off
, snapc
, CEPH_NOSNAP
, mtime
);
2802 rc
= ceph_osdc_start_request(osdc
, req
, true);
2804 rc
= ceph_osdc_wait_request(osdc
, req
);
2806 ceph_osdc_put_request(req
);
2809 dout("writepages result %d\n", rc
);
2812 EXPORT_SYMBOL(ceph_osdc_writepages
);
2814 int ceph_osdc_setup(void)
2816 size_t size
= sizeof(struct ceph_osd_request
) +
2817 CEPH_OSD_SLAB_OPS
* sizeof(struct ceph_osd_req_op
);
2819 BUG_ON(ceph_osd_request_cache
);
2820 ceph_osd_request_cache
= kmem_cache_create("ceph_osd_request", size
,
2823 return ceph_osd_request_cache
? 0 : -ENOMEM
;
2825 EXPORT_SYMBOL(ceph_osdc_setup
);
2827 void ceph_osdc_cleanup(void)
2829 BUG_ON(!ceph_osd_request_cache
);
2830 kmem_cache_destroy(ceph_osd_request_cache
);
2831 ceph_osd_request_cache
= NULL
;
2833 EXPORT_SYMBOL(ceph_osdc_cleanup
);
2836 * handle incoming message
2838 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2840 struct ceph_osd
*osd
= con
->private;
2841 struct ceph_osd_client
*osdc
;
2842 int type
= le16_to_cpu(msg
->hdr
.type
);
2849 case CEPH_MSG_OSD_MAP
:
2850 ceph_osdc_handle_map(osdc
, msg
);
2852 case CEPH_MSG_OSD_OPREPLY
:
2853 handle_reply(osdc
, msg
);
2855 case CEPH_MSG_WATCH_NOTIFY
:
2856 handle_watch_notify(osdc
, msg
);
2860 pr_err("received unknown message type %d %s\n", type
,
2861 ceph_msg_type_name(type
));
2868 * Lookup and return message for incoming reply. Don't try to do
2869 * anything about a larger than preallocated data portion of the
2870 * message at the moment - for now, just skip the message.
2872 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2873 struct ceph_msg_header
*hdr
,
2876 struct ceph_osd
*osd
= con
->private;
2877 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2879 struct ceph_osd_request
*req
;
2880 int front_len
= le32_to_cpu(hdr
->front_len
);
2881 int data_len
= le32_to_cpu(hdr
->data_len
);
2884 tid
= le64_to_cpu(hdr
->tid
);
2885 mutex_lock(&osdc
->request_mutex
);
2886 req
= lookup_request(&osdc
->requests
, tid
);
2888 dout("%s osd%d tid %llu unknown, skipping\n", __func__
,
2895 ceph_msg_revoke_incoming(req
->r_reply
);
2897 if (front_len
> req
->r_reply
->front_alloc_len
) {
2898 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2899 __func__
, osd
->o_osd
, req
->r_tid
, front_len
,
2900 req
->r_reply
->front_alloc_len
);
2901 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front_len
, GFP_NOFS
,
2905 ceph_msg_put(req
->r_reply
);
2909 if (data_len
> req
->r_reply
->data_length
) {
2910 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2911 __func__
, osd
->o_osd
, req
->r_tid
, data_len
,
2912 req
->r_reply
->data_length
);
2918 m
= ceph_msg_get(req
->r_reply
);
2919 dout("get_reply tid %lld %p\n", tid
, m
);
2922 mutex_unlock(&osdc
->request_mutex
);
2926 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2927 struct ceph_msg_header
*hdr
,
2930 struct ceph_osd
*osd
= con
->private;
2931 int type
= le16_to_cpu(hdr
->type
);
2932 int front
= le32_to_cpu(hdr
->front_len
);
2936 case CEPH_MSG_OSD_MAP
:
2937 case CEPH_MSG_WATCH_NOTIFY
:
2938 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2939 case CEPH_MSG_OSD_OPREPLY
:
2940 return get_reply(con
, hdr
, skip
);
2942 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2950 * Wrappers to refcount containing ceph_osd struct
2952 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2954 struct ceph_osd
*osd
= con
->private;
2960 static void put_osd_con(struct ceph_connection
*con
)
2962 struct ceph_osd
*osd
= con
->private;
2970 * Note: returned pointer is the address of a structure that's
2971 * managed separately. Caller must *not* attempt to free it.
2973 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2974 int *proto
, int force_new
)
2976 struct ceph_osd
*o
= con
->private;
2977 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2978 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2979 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2981 if (force_new
&& auth
->authorizer
) {
2982 ceph_auth_destroy_authorizer(auth
->authorizer
);
2983 auth
->authorizer
= NULL
;
2985 if (!auth
->authorizer
) {
2986 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2989 return ERR_PTR(ret
);
2991 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2994 return ERR_PTR(ret
);
2996 *proto
= ac
->protocol
;
3002 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3004 struct ceph_osd
*o
= con
->private;
3005 struct ceph_osd_client
*osdc
= o
->o_osdc
;
3006 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
3008 return ceph_auth_verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
3011 static int invalidate_authorizer(struct ceph_connection
*con
)
3013 struct ceph_osd
*o
= con
->private;
3014 struct ceph_osd_client
*osdc
= o
->o_osdc
;
3015 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
3017 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
3018 return ceph_monc_validate_auth(&osdc
->client
->monc
);
3021 static int osd_sign_message(struct ceph_msg
*msg
)
3023 struct ceph_osd
*o
= msg
->con
->private;
3024 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
3026 return ceph_auth_sign_message(auth
, msg
);
3029 static int osd_check_message_signature(struct ceph_msg
*msg
)
3031 struct ceph_osd
*o
= msg
->con
->private;
3032 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
3034 return ceph_auth_check_message_signature(auth
, msg
);
3037 static const struct ceph_connection_operations osd_con_ops
= {
3040 .dispatch
= dispatch
,
3041 .get_authorizer
= get_authorizer
,
3042 .verify_authorizer_reply
= verify_authorizer_reply
,
3043 .invalidate_authorizer
= invalidate_authorizer
,
3044 .alloc_msg
= alloc_msg
,
3045 .sign_message
= osd_sign_message
,
3046 .check_message_signature
= osd_check_message_signature
,