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 struct ceph_osd_data
*
147 osd_req_op_cls_response_data(struct ceph_osd_request
*osd_req
,
150 return osd_req_op_data(osd_req
, which
, cls
, response_data
);
152 EXPORT_SYMBOL(osd_req_op_cls_response_data
); /* ??? */
154 void osd_req_op_raw_data_in_pages(struct ceph_osd_request
*osd_req
,
155 unsigned int which
, struct page
**pages
,
156 u64 length
, u32 alignment
,
157 bool pages_from_pool
, bool own_pages
)
159 struct ceph_osd_data
*osd_data
;
161 osd_data
= osd_req_op_raw_data_in(osd_req
, which
);
162 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
163 pages_from_pool
, own_pages
);
165 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages
);
167 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request
*osd_req
,
168 unsigned int which
, struct page
**pages
,
169 u64 length
, u32 alignment
,
170 bool pages_from_pool
, bool own_pages
)
172 struct ceph_osd_data
*osd_data
;
174 osd_data
= osd_req_op_data(osd_req
, which
, extent
, osd_data
);
175 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
176 pages_from_pool
, own_pages
);
178 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages
);
180 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request
*osd_req
,
181 unsigned int which
, struct ceph_pagelist
*pagelist
)
183 struct ceph_osd_data
*osd_data
;
185 osd_data
= osd_req_op_data(osd_req
, which
, extent
, osd_data
);
186 ceph_osd_data_pagelist_init(osd_data
, pagelist
);
188 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist
);
191 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request
*osd_req
,
192 unsigned int which
, struct bio
*bio
, size_t bio_length
)
194 struct ceph_osd_data
*osd_data
;
196 osd_data
= osd_req_op_data(osd_req
, which
, extent
, osd_data
);
197 ceph_osd_data_bio_init(osd_data
, bio
, bio_length
);
199 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio
);
200 #endif /* CONFIG_BLOCK */
202 static void osd_req_op_cls_request_info_pagelist(
203 struct ceph_osd_request
*osd_req
,
204 unsigned int which
, struct ceph_pagelist
*pagelist
)
206 struct ceph_osd_data
*osd_data
;
208 osd_data
= osd_req_op_data(osd_req
, which
, cls
, request_info
);
209 ceph_osd_data_pagelist_init(osd_data
, pagelist
);
212 void osd_req_op_cls_request_data_pagelist(
213 struct ceph_osd_request
*osd_req
,
214 unsigned int which
, struct ceph_pagelist
*pagelist
)
216 struct ceph_osd_data
*osd_data
;
218 osd_data
= osd_req_op_data(osd_req
, which
, cls
, request_data
);
219 ceph_osd_data_pagelist_init(osd_data
, pagelist
);
221 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist
);
223 void osd_req_op_cls_request_data_pages(struct ceph_osd_request
*osd_req
,
224 unsigned int which
, struct page
**pages
, u64 length
,
225 u32 alignment
, bool pages_from_pool
, bool own_pages
)
227 struct ceph_osd_data
*osd_data
;
229 osd_data
= osd_req_op_data(osd_req
, which
, cls
, request_data
);
230 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
231 pages_from_pool
, own_pages
);
233 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages
);
235 void osd_req_op_cls_response_data_pages(struct ceph_osd_request
*osd_req
,
236 unsigned int which
, struct page
**pages
, u64 length
,
237 u32 alignment
, bool pages_from_pool
, bool own_pages
)
239 struct ceph_osd_data
*osd_data
;
241 osd_data
= osd_req_op_data(osd_req
, which
, cls
, response_data
);
242 ceph_osd_data_pages_init(osd_data
, pages
, length
, alignment
,
243 pages_from_pool
, own_pages
);
245 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages
);
247 static u64
ceph_osd_data_length(struct ceph_osd_data
*osd_data
)
249 switch (osd_data
->type
) {
250 case CEPH_OSD_DATA_TYPE_NONE
:
252 case CEPH_OSD_DATA_TYPE_PAGES
:
253 return osd_data
->length
;
254 case CEPH_OSD_DATA_TYPE_PAGELIST
:
255 return (u64
)osd_data
->pagelist
->length
;
257 case CEPH_OSD_DATA_TYPE_BIO
:
258 return (u64
)osd_data
->bio_length
;
259 #endif /* CONFIG_BLOCK */
261 WARN(true, "unrecognized data type %d\n", (int)osd_data
->type
);
266 static void ceph_osd_data_release(struct ceph_osd_data
*osd_data
)
268 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
&& osd_data
->own_pages
) {
271 num_pages
= calc_pages_for((u64
)osd_data
->alignment
,
272 (u64
)osd_data
->length
);
273 ceph_release_page_vector(osd_data
->pages
, num_pages
);
275 ceph_osd_data_init(osd_data
);
278 static void osd_req_op_data_release(struct ceph_osd_request
*osd_req
,
281 struct ceph_osd_req_op
*op
;
283 BUG_ON(which
>= osd_req
->r_num_ops
);
284 op
= &osd_req
->r_ops
[which
];
287 case CEPH_OSD_OP_READ
:
288 case CEPH_OSD_OP_WRITE
:
289 case CEPH_OSD_OP_WRITEFULL
:
290 ceph_osd_data_release(&op
->extent
.osd_data
);
292 case CEPH_OSD_OP_CALL
:
293 ceph_osd_data_release(&op
->cls
.request_info
);
294 ceph_osd_data_release(&op
->cls
.request_data
);
295 ceph_osd_data_release(&op
->cls
.response_data
);
297 case CEPH_OSD_OP_SETXATTR
:
298 case CEPH_OSD_OP_CMPXATTR
:
299 ceph_osd_data_release(&op
->xattr
.osd_data
);
301 case CEPH_OSD_OP_STAT
:
302 ceph_osd_data_release(&op
->raw_data_in
);
312 static void ceph_osdc_release_request(struct kref
*kref
)
314 struct ceph_osd_request
*req
= container_of(kref
,
315 struct ceph_osd_request
, r_kref
);
318 dout("%s %p (r_request %p r_reply %p)\n", __func__
, req
,
319 req
->r_request
, req
->r_reply
);
320 WARN_ON(!RB_EMPTY_NODE(&req
->r_node
));
321 WARN_ON(!list_empty(&req
->r_req_lru_item
));
322 WARN_ON(!list_empty(&req
->r_osd_item
));
323 WARN_ON(!list_empty(&req
->r_linger_item
));
324 WARN_ON(!list_empty(&req
->r_linger_osd_item
));
328 ceph_msg_put(req
->r_request
);
330 ceph_msg_revoke_incoming(req
->r_reply
);
331 ceph_msg_put(req
->r_reply
);
334 for (which
= 0; which
< req
->r_num_ops
; which
++)
335 osd_req_op_data_release(req
, which
);
337 ceph_put_snap_context(req
->r_snapc
);
339 mempool_free(req
, req
->r_osdc
->req_mempool
);
340 else if (req
->r_num_ops
<= CEPH_OSD_SLAB_OPS
)
341 kmem_cache_free(ceph_osd_request_cache
, req
);
346 void ceph_osdc_get_request(struct ceph_osd_request
*req
)
348 dout("%s %p (was %d)\n", __func__
, req
,
349 atomic_read(&req
->r_kref
.refcount
));
350 kref_get(&req
->r_kref
);
352 EXPORT_SYMBOL(ceph_osdc_get_request
);
354 void ceph_osdc_put_request(struct ceph_osd_request
*req
)
357 dout("%s %p (was %d)\n", __func__
, req
,
358 atomic_read(&req
->r_kref
.refcount
));
359 kref_put(&req
->r_kref
, ceph_osdc_release_request
);
362 EXPORT_SYMBOL(ceph_osdc_put_request
);
364 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
365 struct ceph_snap_context
*snapc
,
366 unsigned int num_ops
,
370 struct ceph_osd_request
*req
;
373 BUG_ON(num_ops
> CEPH_OSD_SLAB_OPS
);
374 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
375 } else if (num_ops
<= CEPH_OSD_SLAB_OPS
) {
376 req
= kmem_cache_alloc(ceph_osd_request_cache
, gfp_flags
);
378 BUG_ON(num_ops
> CEPH_OSD_MAX_OPS
);
379 req
= kmalloc(sizeof(*req
) + num_ops
* sizeof(req
->r_ops
[0]),
385 /* req only, each op is zeroed in _osd_req_op_init() */
386 memset(req
, 0, sizeof(*req
));
389 req
->r_mempool
= use_mempool
;
390 req
->r_num_ops
= num_ops
;
391 req
->r_snapid
= CEPH_NOSNAP
;
392 req
->r_snapc
= ceph_get_snap_context(snapc
);
394 kref_init(&req
->r_kref
);
395 init_completion(&req
->r_completion
);
396 init_completion(&req
->r_safe_completion
);
397 RB_CLEAR_NODE(&req
->r_node
);
398 INIT_LIST_HEAD(&req
->r_unsafe_item
);
399 INIT_LIST_HEAD(&req
->r_linger_item
);
400 INIT_LIST_HEAD(&req
->r_linger_osd_item
);
401 INIT_LIST_HEAD(&req
->r_req_lru_item
);
402 INIT_LIST_HEAD(&req
->r_osd_item
);
404 req
->r_base_oloc
.pool
= -1;
405 req
->r_target_oloc
.pool
= -1;
407 dout("%s req %p\n", __func__
, req
);
410 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
412 int ceph_osdc_alloc_messages(struct ceph_osd_request
*req
, gfp_t gfp
)
414 struct ceph_osd_client
*osdc
= req
->r_osdc
;
415 struct ceph_msg
*msg
;
418 /* create request message */
419 msg_size
= 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
420 msg_size
+= 4 + 4 + 4 + 8; /* mtime, reassert_version */
421 msg_size
+= 2 + 4 + 8 + 4 + 4; /* oloc */
422 msg_size
+= 1 + 8 + 4 + 4; /* pgid */
423 msg_size
+= 4 + req
->r_base_oid
.name_len
; /* oid */
424 msg_size
+= 2 + req
->r_num_ops
* sizeof(struct ceph_osd_op
);
425 msg_size
+= 8; /* snapid */
426 msg_size
+= 8; /* snap_seq */
427 msg_size
+= 4 + 8 * (req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
428 msg_size
+= 4; /* retry_attempt */
431 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
433 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp
, true);
437 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
438 req
->r_request
= msg
;
440 /* create reply message */
441 msg_size
= OSD_OPREPLY_FRONT_LEN
;
442 msg_size
+= req
->r_base_oid
.name_len
;
443 msg_size
+= req
->r_num_ops
* sizeof(struct ceph_osd_op
);
446 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
448 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, msg_size
, gfp
, true);
456 EXPORT_SYMBOL(ceph_osdc_alloc_messages
);
458 static bool osd_req_opcode_valid(u16 opcode
)
461 #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
462 __CEPH_FORALL_OSD_OPS(GENERATE_CASE
)
470 * This is an osd op init function for opcodes that have no data or
471 * other information associated with them. It also serves as a
472 * common init routine for all the other init functions, below.
474 static struct ceph_osd_req_op
*
475 _osd_req_op_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
476 u16 opcode
, u32 flags
)
478 struct ceph_osd_req_op
*op
;
480 BUG_ON(which
>= osd_req
->r_num_ops
);
481 BUG_ON(!osd_req_opcode_valid(opcode
));
483 op
= &osd_req
->r_ops
[which
];
484 memset(op
, 0, sizeof (*op
));
491 void osd_req_op_init(struct ceph_osd_request
*osd_req
,
492 unsigned int which
, u16 opcode
, u32 flags
)
494 (void)_osd_req_op_init(osd_req
, which
, opcode
, flags
);
496 EXPORT_SYMBOL(osd_req_op_init
);
498 void osd_req_op_extent_init(struct ceph_osd_request
*osd_req
,
499 unsigned int which
, u16 opcode
,
500 u64 offset
, u64 length
,
501 u64 truncate_size
, u32 truncate_seq
)
503 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
505 size_t payload_len
= 0;
507 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
&&
508 opcode
!= CEPH_OSD_OP_WRITEFULL
&& opcode
!= CEPH_OSD_OP_ZERO
&&
509 opcode
!= CEPH_OSD_OP_TRUNCATE
);
511 op
->extent
.offset
= offset
;
512 op
->extent
.length
= length
;
513 op
->extent
.truncate_size
= truncate_size
;
514 op
->extent
.truncate_seq
= truncate_seq
;
515 if (opcode
== CEPH_OSD_OP_WRITE
|| opcode
== CEPH_OSD_OP_WRITEFULL
)
516 payload_len
+= length
;
518 op
->indata_len
= payload_len
;
520 EXPORT_SYMBOL(osd_req_op_extent_init
);
522 void osd_req_op_extent_update(struct ceph_osd_request
*osd_req
,
523 unsigned int which
, u64 length
)
525 struct ceph_osd_req_op
*op
;
528 BUG_ON(which
>= osd_req
->r_num_ops
);
529 op
= &osd_req
->r_ops
[which
];
530 previous
= op
->extent
.length
;
532 if (length
== previous
)
533 return; /* Nothing to do */
534 BUG_ON(length
> previous
);
536 op
->extent
.length
= length
;
537 op
->indata_len
-= previous
- length
;
539 EXPORT_SYMBOL(osd_req_op_extent_update
);
541 void osd_req_op_extent_dup_last(struct ceph_osd_request
*osd_req
,
542 unsigned int which
, u64 offset_inc
)
544 struct ceph_osd_req_op
*op
, *prev_op
;
546 BUG_ON(which
+ 1 >= osd_req
->r_num_ops
);
548 prev_op
= &osd_req
->r_ops
[which
];
549 op
= _osd_req_op_init(osd_req
, which
+ 1, prev_op
->op
, prev_op
->flags
);
550 /* dup previous one */
551 op
->indata_len
= prev_op
->indata_len
;
552 op
->outdata_len
= prev_op
->outdata_len
;
553 op
->extent
= prev_op
->extent
;
555 op
->extent
.offset
+= offset_inc
;
556 op
->extent
.length
-= offset_inc
;
558 if (op
->op
== CEPH_OSD_OP_WRITE
|| op
->op
== CEPH_OSD_OP_WRITEFULL
)
559 op
->indata_len
-= offset_inc
;
561 EXPORT_SYMBOL(osd_req_op_extent_dup_last
);
563 void osd_req_op_cls_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
564 u16 opcode
, const char *class, const char *method
)
566 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
568 struct ceph_pagelist
*pagelist
;
569 size_t payload_len
= 0;
572 BUG_ON(opcode
!= CEPH_OSD_OP_CALL
);
574 pagelist
= kmalloc(sizeof (*pagelist
), GFP_NOFS
);
576 ceph_pagelist_init(pagelist
);
578 op
->cls
.class_name
= class;
579 size
= strlen(class);
580 BUG_ON(size
> (size_t) U8_MAX
);
581 op
->cls
.class_len
= size
;
582 ceph_pagelist_append(pagelist
, class, size
);
585 op
->cls
.method_name
= method
;
586 size
= strlen(method
);
587 BUG_ON(size
> (size_t) U8_MAX
);
588 op
->cls
.method_len
= size
;
589 ceph_pagelist_append(pagelist
, method
, size
);
592 osd_req_op_cls_request_info_pagelist(osd_req
, which
, pagelist
);
594 op
->cls
.argc
= 0; /* currently unused */
596 op
->indata_len
= payload_len
;
598 EXPORT_SYMBOL(osd_req_op_cls_init
);
600 int osd_req_op_xattr_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
601 u16 opcode
, const char *name
, const void *value
,
602 size_t size
, u8 cmp_op
, u8 cmp_mode
)
604 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
606 struct ceph_pagelist
*pagelist
;
609 BUG_ON(opcode
!= CEPH_OSD_OP_SETXATTR
&& opcode
!= CEPH_OSD_OP_CMPXATTR
);
611 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
615 ceph_pagelist_init(pagelist
);
617 payload_len
= strlen(name
);
618 op
->xattr
.name_len
= payload_len
;
619 ceph_pagelist_append(pagelist
, name
, payload_len
);
621 op
->xattr
.value_len
= size
;
622 ceph_pagelist_append(pagelist
, value
, size
);
625 op
->xattr
.cmp_op
= cmp_op
;
626 op
->xattr
.cmp_mode
= cmp_mode
;
628 ceph_osd_data_pagelist_init(&op
->xattr
.osd_data
, pagelist
);
629 op
->indata_len
= payload_len
;
632 EXPORT_SYMBOL(osd_req_op_xattr_init
);
634 void osd_req_op_watch_init(struct ceph_osd_request
*osd_req
,
635 unsigned int which
, u16 opcode
,
636 u64 cookie
, u64 version
, int flag
)
638 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
641 BUG_ON(opcode
!= CEPH_OSD_OP_NOTIFY_ACK
&& opcode
!= CEPH_OSD_OP_WATCH
);
643 op
->watch
.cookie
= cookie
;
644 op
->watch
.ver
= version
;
645 if (opcode
== CEPH_OSD_OP_WATCH
&& flag
)
646 op
->watch
.flag
= (u8
)1;
648 EXPORT_SYMBOL(osd_req_op_watch_init
);
650 void osd_req_op_alloc_hint_init(struct ceph_osd_request
*osd_req
,
652 u64 expected_object_size
,
653 u64 expected_write_size
)
655 struct ceph_osd_req_op
*op
= _osd_req_op_init(osd_req
, which
,
656 CEPH_OSD_OP_SETALLOCHINT
,
659 op
->alloc_hint
.expected_object_size
= expected_object_size
;
660 op
->alloc_hint
.expected_write_size
= expected_write_size
;
663 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
664 * not worth a feature bit. Set FAILOK per-op flag to make
665 * sure older osds don't trip over an unsupported opcode.
667 op
->flags
|= CEPH_OSD_OP_FLAG_FAILOK
;
669 EXPORT_SYMBOL(osd_req_op_alloc_hint_init
);
671 static void ceph_osdc_msg_data_add(struct ceph_msg
*msg
,
672 struct ceph_osd_data
*osd_data
)
674 u64 length
= ceph_osd_data_length(osd_data
);
676 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
) {
677 BUG_ON(length
> (u64
) SIZE_MAX
);
679 ceph_msg_data_add_pages(msg
, osd_data
->pages
,
680 length
, osd_data
->alignment
);
681 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGELIST
) {
683 ceph_msg_data_add_pagelist(msg
, osd_data
->pagelist
);
685 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_BIO
) {
686 ceph_msg_data_add_bio(msg
, osd_data
->bio
, length
);
689 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_NONE
);
693 static u64
osd_req_encode_op(struct ceph_osd_request
*req
,
694 struct ceph_osd_op
*dst
, unsigned int which
)
696 struct ceph_osd_req_op
*src
;
697 struct ceph_osd_data
*osd_data
;
698 u64 request_data_len
= 0;
701 BUG_ON(which
>= req
->r_num_ops
);
702 src
= &req
->r_ops
[which
];
703 if (WARN_ON(!osd_req_opcode_valid(src
->op
))) {
704 pr_err("unrecognized osd opcode %d\n", src
->op
);
710 case CEPH_OSD_OP_STAT
:
711 osd_data
= &src
->raw_data_in
;
712 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
714 case CEPH_OSD_OP_READ
:
715 case CEPH_OSD_OP_WRITE
:
716 case CEPH_OSD_OP_WRITEFULL
:
717 case CEPH_OSD_OP_ZERO
:
718 case CEPH_OSD_OP_TRUNCATE
:
719 if (src
->op
== CEPH_OSD_OP_WRITE
||
720 src
->op
== CEPH_OSD_OP_WRITEFULL
)
721 request_data_len
= src
->extent
.length
;
722 dst
->extent
.offset
= cpu_to_le64(src
->extent
.offset
);
723 dst
->extent
.length
= cpu_to_le64(src
->extent
.length
);
724 dst
->extent
.truncate_size
=
725 cpu_to_le64(src
->extent
.truncate_size
);
726 dst
->extent
.truncate_seq
=
727 cpu_to_le32(src
->extent
.truncate_seq
);
728 osd_data
= &src
->extent
.osd_data
;
729 if (src
->op
== CEPH_OSD_OP_WRITE
||
730 src
->op
== CEPH_OSD_OP_WRITEFULL
)
731 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
733 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
735 case CEPH_OSD_OP_CALL
:
736 dst
->cls
.class_len
= src
->cls
.class_len
;
737 dst
->cls
.method_len
= src
->cls
.method_len
;
738 osd_data
= &src
->cls
.request_info
;
739 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
740 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_PAGELIST
);
741 request_data_len
= osd_data
->pagelist
->length
;
743 osd_data
= &src
->cls
.request_data
;
744 data_length
= ceph_osd_data_length(osd_data
);
746 BUG_ON(osd_data
->type
== CEPH_OSD_DATA_TYPE_NONE
);
747 dst
->cls
.indata_len
= cpu_to_le32(data_length
);
748 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
749 src
->indata_len
+= data_length
;
750 request_data_len
+= data_length
;
752 osd_data
= &src
->cls
.response_data
;
753 ceph_osdc_msg_data_add(req
->r_reply
, osd_data
);
755 case CEPH_OSD_OP_STARTSYNC
:
757 case CEPH_OSD_OP_NOTIFY_ACK
:
758 case CEPH_OSD_OP_WATCH
:
759 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
760 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
761 dst
->watch
.flag
= src
->watch
.flag
;
763 case CEPH_OSD_OP_SETALLOCHINT
:
764 dst
->alloc_hint
.expected_object_size
=
765 cpu_to_le64(src
->alloc_hint
.expected_object_size
);
766 dst
->alloc_hint
.expected_write_size
=
767 cpu_to_le64(src
->alloc_hint
.expected_write_size
);
769 case CEPH_OSD_OP_SETXATTR
:
770 case CEPH_OSD_OP_CMPXATTR
:
771 dst
->xattr
.name_len
= cpu_to_le32(src
->xattr
.name_len
);
772 dst
->xattr
.value_len
= cpu_to_le32(src
->xattr
.value_len
);
773 dst
->xattr
.cmp_op
= src
->xattr
.cmp_op
;
774 dst
->xattr
.cmp_mode
= src
->xattr
.cmp_mode
;
775 osd_data
= &src
->xattr
.osd_data
;
776 ceph_osdc_msg_data_add(req
->r_request
, osd_data
);
777 request_data_len
= osd_data
->pagelist
->length
;
779 case CEPH_OSD_OP_CREATE
:
780 case CEPH_OSD_OP_DELETE
:
783 pr_err("unsupported osd opcode %s\n",
784 ceph_osd_op_name(src
->op
));
790 dst
->op
= cpu_to_le16(src
->op
);
791 dst
->flags
= cpu_to_le32(src
->flags
);
792 dst
->payload_len
= cpu_to_le32(src
->indata_len
);
794 return request_data_len
;
798 * build new request AND message, calculate layout, and adjust file
801 * if the file was recently truncated, we include information about its
802 * old and new size so that the object can be updated appropriately. (we
803 * avoid synchronously deleting truncated objects because it's slow.)
805 * if @do_sync, include a 'startsync' command so that the osd will flush
808 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
809 struct ceph_file_layout
*layout
,
810 struct ceph_vino vino
,
812 unsigned int which
, int num_ops
,
813 int opcode
, int flags
,
814 struct ceph_snap_context
*snapc
,
819 struct ceph_osd_request
*req
;
825 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
&&
826 opcode
!= CEPH_OSD_OP_ZERO
&& opcode
!= CEPH_OSD_OP_TRUNCATE
&&
827 opcode
!= CEPH_OSD_OP_CREATE
&& opcode
!= CEPH_OSD_OP_DELETE
);
829 req
= ceph_osdc_alloc_request(osdc
, snapc
, num_ops
, use_mempool
,
836 req
->r_flags
= flags
;
838 /* calculate max write size */
839 r
= calc_layout(layout
, off
, plen
, &objnum
, &objoff
, &objlen
);
843 if (opcode
== CEPH_OSD_OP_CREATE
|| opcode
== CEPH_OSD_OP_DELETE
) {
844 osd_req_op_init(req
, which
, opcode
, 0);
846 u32 object_size
= le32_to_cpu(layout
->fl_object_size
);
847 u32 object_base
= off
- objoff
;
848 if (!(truncate_seq
== 1 && truncate_size
== -1ULL)) {
849 if (truncate_size
<= object_base
) {
852 truncate_size
-= object_base
;
853 if (truncate_size
> object_size
)
854 truncate_size
= object_size
;
857 osd_req_op_extent_init(req
, which
, opcode
, objoff
, objlen
,
858 truncate_size
, truncate_seq
);
861 req
->r_base_oloc
.pool
= ceph_file_layout_pg_pool(*layout
);
863 snprintf(req
->r_base_oid
.name
, sizeof(req
->r_base_oid
.name
),
864 "%llx.%08llx", vino
.ino
, objnum
);
865 req
->r_base_oid
.name_len
= strlen(req
->r_base_oid
.name
);
867 r
= ceph_osdc_alloc_messages(req
, GFP_NOFS
);
874 ceph_osdc_put_request(req
);
877 EXPORT_SYMBOL(ceph_osdc_new_request
);
880 * We keep osd requests in an rbtree, sorted by ->r_tid.
882 static void __insert_request(struct ceph_osd_client
*osdc
,
883 struct ceph_osd_request
*new)
885 struct rb_node
**p
= &osdc
->requests
.rb_node
;
886 struct rb_node
*parent
= NULL
;
887 struct ceph_osd_request
*req
= NULL
;
891 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
892 if (new->r_tid
< req
->r_tid
)
894 else if (new->r_tid
> req
->r_tid
)
900 rb_link_node(&new->r_node
, parent
, p
);
901 rb_insert_color(&new->r_node
, &osdc
->requests
);
904 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
907 struct ceph_osd_request
*req
;
908 struct rb_node
*n
= osdc
->requests
.rb_node
;
911 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
912 if (tid
< req
->r_tid
)
914 else if (tid
> req
->r_tid
)
922 static struct ceph_osd_request
*
923 __lookup_request_ge(struct ceph_osd_client
*osdc
,
926 struct ceph_osd_request
*req
;
927 struct rb_node
*n
= osdc
->requests
.rb_node
;
930 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
931 if (tid
< req
->r_tid
) {
935 } else if (tid
> req
->r_tid
) {
944 static void __kick_linger_request(struct ceph_osd_request
*req
)
946 struct ceph_osd_client
*osdc
= req
->r_osdc
;
947 struct ceph_osd
*osd
= req
->r_osd
;
950 * Linger requests need to be resent with a new tid to avoid
951 * the dup op detection logic on the OSDs. Achieve this with
952 * a re-register dance instead of open-coding.
954 ceph_osdc_get_request(req
);
955 if (!list_empty(&req
->r_linger_item
))
956 __unregister_linger_request(osdc
, req
);
958 __unregister_request(osdc
, req
);
959 __register_request(osdc
, req
);
960 ceph_osdc_put_request(req
);
963 * Unless request has been registered as both normal and
964 * lingering, __unregister{,_linger}_request clears r_osd.
965 * However, here we need to preserve r_osd to make sure we
966 * requeue on the same OSD.
968 WARN_ON(req
->r_osd
|| !osd
);
971 dout("%s requeueing %p tid %llu\n", __func__
, req
, req
->r_tid
);
972 __enqueue_request(req
);
976 * Resubmit requests pending on the given osd.
978 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
979 struct ceph_osd
*osd
)
981 struct ceph_osd_request
*req
, *nreq
;
983 LIST_HEAD(resend_linger
);
986 dout("%s osd%d\n", __func__
, osd
->o_osd
);
987 err
= __reset_osd(osdc
, osd
);
992 * Build up a list of requests to resend by traversing the
993 * osd's list of requests. Requests for a given object are
994 * sent in tid order, and that is also the order they're
995 * kept on this list. Therefore all requests that are in
996 * flight will be found first, followed by all requests that
997 * have not yet been sent. And to resend requests while
998 * preserving this order we will want to put any sent
999 * requests back on the front of the osd client's unsent
1002 * So we build a separate ordered list of already-sent
1003 * requests for the affected osd and splice it onto the
1004 * front of the osd client's unsent list. Once we've seen a
1005 * request that has not yet been sent we're done. Those
1006 * requests are already sitting right where they belong.
1008 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
1012 if (!req
->r_linger
) {
1013 dout("%s requeueing %p tid %llu\n", __func__
, req
,
1015 list_move_tail(&req
->r_req_lru_item
, &resend
);
1016 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1018 list_move_tail(&req
->r_req_lru_item
, &resend_linger
);
1021 list_splice(&resend
, &osdc
->req_unsent
);
1024 * Both registered and not yet registered linger requests are
1025 * enqueued with a new tid on the same OSD. We add/move them
1026 * to req_unsent/o_requests at the end to keep things in tid
1029 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
1030 r_linger_osd_item
) {
1031 WARN_ON(!list_empty(&req
->r_req_lru_item
));
1032 __kick_linger_request(req
);
1035 list_for_each_entry_safe(req
, nreq
, &resend_linger
, r_req_lru_item
)
1036 __kick_linger_request(req
);
1040 * If the osd connection drops, we need to resubmit all requests.
1042 static void osd_reset(struct ceph_connection
*con
)
1044 struct ceph_osd
*osd
= con
->private;
1045 struct ceph_osd_client
*osdc
;
1049 dout("osd_reset osd%d\n", osd
->o_osd
);
1051 down_read(&osdc
->map_sem
);
1052 mutex_lock(&osdc
->request_mutex
);
1053 __kick_osd_requests(osdc
, osd
);
1054 __send_queued(osdc
);
1055 mutex_unlock(&osdc
->request_mutex
);
1056 up_read(&osdc
->map_sem
);
1060 * Track open sessions with osds.
1062 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
1064 struct ceph_osd
*osd
;
1066 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
1070 atomic_set(&osd
->o_ref
, 1);
1073 RB_CLEAR_NODE(&osd
->o_node
);
1074 INIT_LIST_HEAD(&osd
->o_requests
);
1075 INIT_LIST_HEAD(&osd
->o_linger_requests
);
1076 INIT_LIST_HEAD(&osd
->o_osd_lru
);
1077 osd
->o_incarnation
= 1;
1079 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
1081 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
1085 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
1087 if (atomic_inc_not_zero(&osd
->o_ref
)) {
1088 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
1089 atomic_read(&osd
->o_ref
));
1092 dout("get_osd %p FAIL\n", osd
);
1097 static void put_osd(struct ceph_osd
*osd
)
1099 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
1100 atomic_read(&osd
->o_ref
) - 1);
1101 if (atomic_dec_and_test(&osd
->o_ref
)) {
1102 if (osd
->o_auth
.authorizer
)
1103 ceph_auth_destroy_authorizer(osd
->o_auth
.authorizer
);
1109 * remove an osd from our map
1111 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1113 dout("%s %p osd%d\n", __func__
, osd
, osd
->o_osd
);
1114 WARN_ON(!list_empty(&osd
->o_requests
));
1115 WARN_ON(!list_empty(&osd
->o_linger_requests
));
1117 list_del_init(&osd
->o_osd_lru
);
1118 rb_erase(&osd
->o_node
, &osdc
->osds
);
1119 RB_CLEAR_NODE(&osd
->o_node
);
1122 static void remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1124 dout("%s %p osd%d\n", __func__
, osd
, osd
->o_osd
);
1126 if (!RB_EMPTY_NODE(&osd
->o_node
)) {
1127 ceph_con_close(&osd
->o_con
);
1128 __remove_osd(osdc
, osd
);
1133 static void remove_all_osds(struct ceph_osd_client
*osdc
)
1135 dout("%s %p\n", __func__
, osdc
);
1136 mutex_lock(&osdc
->request_mutex
);
1137 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
1138 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
1139 struct ceph_osd
, o_node
);
1140 remove_osd(osdc
, osd
);
1142 mutex_unlock(&osdc
->request_mutex
);
1145 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
1146 struct ceph_osd
*osd
)
1148 dout("%s %p\n", __func__
, osd
);
1149 BUG_ON(!list_empty(&osd
->o_osd_lru
));
1151 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
1152 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
;
1155 static void maybe_move_osd_to_lru(struct ceph_osd_client
*osdc
,
1156 struct ceph_osd
*osd
)
1158 dout("%s %p\n", __func__
, osd
);
1160 if (list_empty(&osd
->o_requests
) &&
1161 list_empty(&osd
->o_linger_requests
))
1162 __move_osd_to_lru(osdc
, osd
);
1165 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
1167 dout("__remove_osd_from_lru %p\n", osd
);
1168 if (!list_empty(&osd
->o_osd_lru
))
1169 list_del_init(&osd
->o_osd_lru
);
1172 static void remove_old_osds(struct ceph_osd_client
*osdc
)
1174 struct ceph_osd
*osd
, *nosd
;
1176 dout("__remove_old_osds %p\n", osdc
);
1177 mutex_lock(&osdc
->request_mutex
);
1178 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
1179 if (time_before(jiffies
, osd
->lru_ttl
))
1181 remove_osd(osdc
, osd
);
1183 mutex_unlock(&osdc
->request_mutex
);
1189 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
1191 struct ceph_entity_addr
*peer_addr
;
1193 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
1194 if (list_empty(&osd
->o_requests
) &&
1195 list_empty(&osd
->o_linger_requests
)) {
1196 remove_osd(osdc
, osd
);
1200 peer_addr
= &osdc
->osdmap
->osd_addr
[osd
->o_osd
];
1201 if (!memcmp(peer_addr
, &osd
->o_con
.peer_addr
, sizeof (*peer_addr
)) &&
1202 !ceph_con_opened(&osd
->o_con
)) {
1203 struct ceph_osd_request
*req
;
1205 dout("osd addr hasn't changed and connection never opened, "
1206 "letting msgr retry\n");
1207 /* touch each r_stamp for handle_timeout()'s benfit */
1208 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
1209 req
->r_stamp
= jiffies
;
1214 ceph_con_close(&osd
->o_con
);
1215 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
, peer_addr
);
1216 osd
->o_incarnation
++;
1221 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
1223 struct rb_node
**p
= &osdc
->osds
.rb_node
;
1224 struct rb_node
*parent
= NULL
;
1225 struct ceph_osd
*osd
= NULL
;
1227 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
1230 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
1231 if (new->o_osd
< osd
->o_osd
)
1233 else if (new->o_osd
> osd
->o_osd
)
1234 p
= &(*p
)->rb_right
;
1239 rb_link_node(&new->o_node
, parent
, p
);
1240 rb_insert_color(&new->o_node
, &osdc
->osds
);
1243 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
1245 struct ceph_osd
*osd
;
1246 struct rb_node
*n
= osdc
->osds
.rb_node
;
1249 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
1252 else if (o
> osd
->o_osd
)
1260 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
1262 schedule_delayed_work(&osdc
->timeout_work
,
1263 osdc
->client
->options
->osd_keepalive_timeout
);
1266 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
1268 cancel_delayed_work(&osdc
->timeout_work
);
1272 * Register request, assign tid. If this is the first request, set up
1273 * the timeout event.
1275 static void __register_request(struct ceph_osd_client
*osdc
,
1276 struct ceph_osd_request
*req
)
1278 req
->r_tid
= ++osdc
->last_tid
;
1279 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
1280 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
1281 __insert_request(osdc
, req
);
1282 ceph_osdc_get_request(req
);
1283 osdc
->num_requests
++;
1284 if (osdc
->num_requests
== 1) {
1285 dout(" first request, scheduling timeout\n");
1286 __schedule_osd_timeout(osdc
);
1291 * called under osdc->request_mutex
1293 static void __unregister_request(struct ceph_osd_client
*osdc
,
1294 struct ceph_osd_request
*req
)
1296 if (RB_EMPTY_NODE(&req
->r_node
)) {
1297 dout("__unregister_request %p tid %lld not registered\n",
1302 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
1303 rb_erase(&req
->r_node
, &osdc
->requests
);
1304 RB_CLEAR_NODE(&req
->r_node
);
1305 osdc
->num_requests
--;
1308 /* make sure the original request isn't in flight. */
1309 ceph_msg_revoke(req
->r_request
);
1311 list_del_init(&req
->r_osd_item
);
1312 maybe_move_osd_to_lru(osdc
, req
->r_osd
);
1313 if (list_empty(&req
->r_linger_osd_item
))
1317 list_del_init(&req
->r_req_lru_item
);
1318 ceph_osdc_put_request(req
);
1320 if (osdc
->num_requests
== 0) {
1321 dout(" no requests, canceling timeout\n");
1322 __cancel_osd_timeout(osdc
);
1327 * Cancel a previously queued request message
1329 static void __cancel_request(struct ceph_osd_request
*req
)
1331 if (req
->r_sent
&& req
->r_osd
) {
1332 ceph_msg_revoke(req
->r_request
);
1337 static void __register_linger_request(struct ceph_osd_client
*osdc
,
1338 struct ceph_osd_request
*req
)
1340 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
1341 WARN_ON(!req
->r_linger
);
1343 ceph_osdc_get_request(req
);
1344 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
1346 list_add_tail(&req
->r_linger_osd_item
,
1347 &req
->r_osd
->o_linger_requests
);
1350 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
1351 struct ceph_osd_request
*req
)
1353 WARN_ON(!req
->r_linger
);
1355 if (list_empty(&req
->r_linger_item
)) {
1356 dout("%s %p tid %llu not registered\n", __func__
, req
,
1361 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
1362 list_del_init(&req
->r_linger_item
);
1365 list_del_init(&req
->r_linger_osd_item
);
1366 maybe_move_osd_to_lru(osdc
, req
->r_osd
);
1367 if (list_empty(&req
->r_osd_item
))
1370 ceph_osdc_put_request(req
);
1373 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
1374 struct ceph_osd_request
*req
)
1376 if (!req
->r_linger
) {
1377 dout("set_request_linger %p\n", req
);
1381 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
1384 * Returns whether a request should be blocked from being sent
1385 * based on the current osdmap and osd_client settings.
1387 * Caller should hold map_sem for read.
1389 static bool __req_should_be_paused(struct ceph_osd_client
*osdc
,
1390 struct ceph_osd_request
*req
)
1392 bool pauserd
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSERD
);
1393 bool pausewr
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSEWR
) ||
1394 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
);
1395 return (req
->r_flags
& CEPH_OSD_FLAG_READ
&& pauserd
) ||
1396 (req
->r_flags
& CEPH_OSD_FLAG_WRITE
&& pausewr
);
1400 * Calculate mapping of a request to a PG. Takes tiering into account.
1402 static int __calc_request_pg(struct ceph_osdmap
*osdmap
,
1403 struct ceph_osd_request
*req
,
1404 struct ceph_pg
*pg_out
)
1406 bool need_check_tiering
;
1408 need_check_tiering
= false;
1409 if (req
->r_target_oloc
.pool
== -1) {
1410 req
->r_target_oloc
= req
->r_base_oloc
; /* struct */
1411 need_check_tiering
= true;
1413 if (req
->r_target_oid
.name_len
== 0) {
1414 ceph_oid_copy(&req
->r_target_oid
, &req
->r_base_oid
);
1415 need_check_tiering
= true;
1418 if (need_check_tiering
&&
1419 (req
->r_flags
& CEPH_OSD_FLAG_IGNORE_OVERLAY
) == 0) {
1420 struct ceph_pg_pool_info
*pi
;
1422 pi
= ceph_pg_pool_by_id(osdmap
, req
->r_target_oloc
.pool
);
1424 if ((req
->r_flags
& CEPH_OSD_FLAG_READ
) &&
1426 req
->r_target_oloc
.pool
= pi
->read_tier
;
1427 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) &&
1428 pi
->write_tier
>= 0)
1429 req
->r_target_oloc
.pool
= pi
->write_tier
;
1431 /* !pi is caught in ceph_oloc_oid_to_pg() */
1434 return ceph_oloc_oid_to_pg(osdmap
, &req
->r_target_oloc
,
1435 &req
->r_target_oid
, pg_out
);
1438 static void __enqueue_request(struct ceph_osd_request
*req
)
1440 struct ceph_osd_client
*osdc
= req
->r_osdc
;
1442 dout("%s %p tid %llu to osd%d\n", __func__
, req
, req
->r_tid
,
1443 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1446 __remove_osd_from_lru(req
->r_osd
);
1447 list_add_tail(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1448 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1450 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1455 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1456 * (as needed), and set the request r_osd appropriately. If there is
1457 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1458 * (unsent, homeless) or leave on in-flight lru.
1460 * Return 0 if unchanged, 1 if changed, or negative on error.
1462 * Caller should hold map_sem for read and request_mutex.
1464 static int __map_request(struct ceph_osd_client
*osdc
,
1465 struct ceph_osd_request
*req
, int force_resend
)
1467 struct ceph_pg pgid
;
1468 int acting
[CEPH_PG_MAX_SIZE
];
1473 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
1475 err
= __calc_request_pg(osdc
->osdmap
, req
, &pgid
);
1477 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1482 num
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
, &o
);
1486 was_paused
= req
->r_paused
;
1487 req
->r_paused
= __req_should_be_paused(osdc
, req
);
1488 if (was_paused
&& !req
->r_paused
)
1491 if ((!force_resend
&&
1492 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
1493 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
1494 req
->r_num_pg_osds
== num
&&
1495 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
1496 (req
->r_osd
== NULL
&& o
== -1) ||
1498 return 0; /* no change */
1500 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1501 req
->r_tid
, pgid
.pool
, pgid
.seed
, o
,
1502 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1504 /* record full pg acting set */
1505 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
1506 req
->r_num_pg_osds
= num
;
1509 __cancel_request(req
);
1510 list_del_init(&req
->r_osd_item
);
1511 list_del_init(&req
->r_linger_osd_item
);
1515 req
->r_osd
= __lookup_osd(osdc
, o
);
1516 if (!req
->r_osd
&& o
>= 0) {
1518 req
->r_osd
= create_osd(osdc
, o
);
1520 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1524 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
1525 __insert_osd(osdc
, req
->r_osd
);
1527 ceph_con_open(&req
->r_osd
->o_con
,
1528 CEPH_ENTITY_TYPE_OSD
, o
,
1529 &osdc
->osdmap
->osd_addr
[o
]);
1532 __enqueue_request(req
);
1533 err
= 1; /* osd or pg changed */
1540 * caller should hold map_sem (for read) and request_mutex
1542 static void __send_request(struct ceph_osd_client
*osdc
,
1543 struct ceph_osd_request
*req
)
1547 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1548 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
,
1549 (unsigned long long)req
->r_pgid
.pool
, req
->r_pgid
.seed
);
1551 /* fill in message content that changes each time we send it */
1552 put_unaligned_le32(osdc
->osdmap
->epoch
, req
->r_request_osdmap_epoch
);
1553 put_unaligned_le32(req
->r_flags
, req
->r_request_flags
);
1554 put_unaligned_le64(req
->r_target_oloc
.pool
, req
->r_request_pool
);
1555 p
= req
->r_request_pgid
;
1556 ceph_encode_64(&p
, req
->r_pgid
.pool
);
1557 ceph_encode_32(&p
, req
->r_pgid
.seed
);
1558 put_unaligned_le64(1, req
->r_request_attempts
); /* FIXME */
1559 memcpy(req
->r_request_reassert_version
, &req
->r_reassert_version
,
1560 sizeof(req
->r_reassert_version
));
1562 req
->r_stamp
= jiffies
;
1563 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1565 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1567 req
->r_sent
= req
->r_osd
->o_incarnation
;
1569 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1573 * Send any requests in the queue (req_unsent).
1575 static void __send_queued(struct ceph_osd_client
*osdc
)
1577 struct ceph_osd_request
*req
, *tmp
;
1579 dout("__send_queued\n");
1580 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
)
1581 __send_request(osdc
, req
);
1585 * Caller should hold map_sem for read and request_mutex.
1587 static int __ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1588 struct ceph_osd_request
*req
,
1593 __register_request(osdc
, req
);
1595 req
->r_got_reply
= 0;
1596 rc
= __map_request(osdc
, req
, 0);
1599 dout("osdc_start_request failed map, "
1600 " will retry %lld\n", req
->r_tid
);
1603 __unregister_request(osdc
, req
);
1608 if (req
->r_osd
== NULL
) {
1609 dout("send_request %p no up osds in pg\n", req
);
1610 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1612 __send_queued(osdc
);
1619 * Timeout callback, called every N seconds when 1 or more osd
1620 * requests has been active for more than N seconds. When this
1621 * happens, we ping all OSDs with requests who have timed out to
1622 * ensure any communications channel reset is detected. Reset the
1623 * request timeouts another N seconds in the future as we go.
1624 * Reschedule the timeout event another N seconds in future (unless
1625 * there are no open requests).
1627 static void handle_timeout(struct work_struct
*work
)
1629 struct ceph_osd_client
*osdc
=
1630 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1631 struct ceph_options
*opts
= osdc
->client
->options
;
1632 struct ceph_osd_request
*req
;
1633 struct ceph_osd
*osd
;
1634 struct list_head slow_osds
;
1636 down_read(&osdc
->map_sem
);
1638 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1640 mutex_lock(&osdc
->request_mutex
);
1643 * ping osds that are a bit slow. this ensures that if there
1644 * is a break in the TCP connection we will notice, and reopen
1645 * a connection with that osd (from the fault callback).
1647 INIT_LIST_HEAD(&slow_osds
);
1648 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1649 if (time_before(jiffies
,
1650 req
->r_stamp
+ opts
->osd_keepalive_timeout
))
1655 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1656 req
->r_tid
, osd
->o_osd
);
1657 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1659 while (!list_empty(&slow_osds
)) {
1660 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1662 list_del_init(&osd
->o_keepalive_item
);
1663 ceph_con_keepalive(&osd
->o_con
);
1666 __schedule_osd_timeout(osdc
);
1667 __send_queued(osdc
);
1668 mutex_unlock(&osdc
->request_mutex
);
1669 up_read(&osdc
->map_sem
);
1672 static void handle_osds_timeout(struct work_struct
*work
)
1674 struct ceph_osd_client
*osdc
=
1675 container_of(work
, struct ceph_osd_client
,
1676 osds_timeout_work
.work
);
1677 unsigned long delay
= osdc
->client
->options
->osd_idle_ttl
/ 4;
1679 dout("osds timeout\n");
1680 down_read(&osdc
->map_sem
);
1681 remove_old_osds(osdc
);
1682 up_read(&osdc
->map_sem
);
1684 schedule_delayed_work(&osdc
->osds_timeout_work
,
1685 round_jiffies_relative(delay
));
1688 static int ceph_oloc_decode(void **p
, void *end
,
1689 struct ceph_object_locator
*oloc
)
1691 u8 struct_v
, struct_cv
;
1696 ceph_decode_need(p
, end
, 1 + 1 + 4, e_inval
);
1697 struct_v
= ceph_decode_8(p
);
1698 struct_cv
= ceph_decode_8(p
);
1700 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1701 struct_v
, struct_cv
);
1704 if (struct_cv
> 6) {
1705 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1706 struct_v
, struct_cv
);
1709 len
= ceph_decode_32(p
);
1710 ceph_decode_need(p
, end
, len
, e_inval
);
1711 struct_end
= *p
+ len
;
1713 oloc
->pool
= ceph_decode_64(p
);
1714 *p
+= 4; /* skip preferred */
1716 len
= ceph_decode_32(p
);
1718 pr_warn("ceph_object_locator::key is set\n");
1722 if (struct_v
>= 5) {
1723 len
= ceph_decode_32(p
);
1725 pr_warn("ceph_object_locator::nspace is set\n");
1730 if (struct_v
>= 6) {
1731 s64 hash
= ceph_decode_64(p
);
1733 pr_warn("ceph_object_locator::hash is set\n");
1748 static int ceph_redirect_decode(void **p
, void *end
,
1749 struct ceph_request_redirect
*redir
)
1751 u8 struct_v
, struct_cv
;
1756 ceph_decode_need(p
, end
, 1 + 1 + 4, e_inval
);
1757 struct_v
= ceph_decode_8(p
);
1758 struct_cv
= ceph_decode_8(p
);
1759 if (struct_cv
> 1) {
1760 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\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 ret
= ceph_oloc_decode(p
, end
, &redir
->oloc
);
1772 len
= ceph_decode_32(p
);
1774 pr_warn("ceph_request_redirect::object_name is set\n");
1778 len
= ceph_decode_32(p
);
1779 *p
+= len
; /* skip osd_instructions */
1791 static void complete_request(struct ceph_osd_request
*req
)
1793 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1797 * handle osd op reply. either call the callback if it is specified,
1798 * or do the completion to wake up the waiting thread.
1800 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1803 struct ceph_osd_request
*req
;
1804 struct ceph_request_redirect redir
;
1807 unsigned int numops
;
1808 int payload_len
, flags
;
1814 u64 reassert_version
;
1816 int already_completed
;
1821 tid
= le64_to_cpu(msg
->hdr
.tid
);
1822 dout("handle_reply %p tid %llu\n", msg
, tid
);
1824 p
= msg
->front
.iov_base
;
1825 end
= p
+ msg
->front
.iov_len
;
1827 ceph_decode_need(&p
, end
, 4, bad
);
1828 object_len
= ceph_decode_32(&p
);
1829 ceph_decode_need(&p
, end
, object_len
, bad
);
1832 err
= ceph_decode_pgid(&p
, end
, &pg
);
1836 ceph_decode_need(&p
, end
, 8 + 4 + 4 + 8 + 4, bad
);
1837 flags
= ceph_decode_64(&p
);
1838 result
= ceph_decode_32(&p
);
1839 reassert_epoch
= ceph_decode_32(&p
);
1840 reassert_version
= ceph_decode_64(&p
);
1841 osdmap_epoch
= ceph_decode_32(&p
);
1844 down_read(&osdc
->map_sem
);
1845 mutex_lock(&osdc
->request_mutex
);
1846 req
= __lookup_request(osdc
, tid
);
1848 dout("handle_reply tid %llu dne\n", tid
);
1851 ceph_osdc_get_request(req
);
1853 dout("handle_reply %p tid %llu req %p result %d\n", msg
, tid
,
1856 ceph_decode_need(&p
, end
, 4, bad_put
);
1857 numops
= ceph_decode_32(&p
);
1858 if (numops
> CEPH_OSD_MAX_OPS
)
1860 if (numops
!= req
->r_num_ops
)
1863 ceph_decode_need(&p
, end
, numops
* sizeof(struct ceph_osd_op
), bad_put
);
1864 for (i
= 0; i
< numops
; i
++) {
1865 struct ceph_osd_op
*op
= p
;
1868 len
= le32_to_cpu(op
->payload_len
);
1869 req
->r_ops
[i
].outdata_len
= len
;
1870 dout(" op %d has %d bytes\n", i
, len
);
1874 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1875 if (payload_len
!= bytes
) {
1876 pr_warn("sum of op payload lens %d != data_len %d\n",
1877 payload_len
, bytes
);
1881 ceph_decode_need(&p
, end
, 4 + numops
* 4, bad_put
);
1882 retry_attempt
= ceph_decode_32(&p
);
1883 for (i
= 0; i
< numops
; i
++)
1884 req
->r_ops
[i
].rval
= ceph_decode_32(&p
);
1886 if (le16_to_cpu(msg
->hdr
.version
) >= 6) {
1887 p
+= 8 + 4; /* skip replay_version */
1888 p
+= 8; /* skip user_version */
1890 if (le16_to_cpu(msg
->hdr
.version
) >= 7)
1891 ceph_decode_8_safe(&p
, end
, decode_redir
, bad_put
);
1899 err
= ceph_redirect_decode(&p
, end
, &redir
);
1903 redir
.oloc
.pool
= -1;
1906 if (redir
.oloc
.pool
!= -1) {
1907 dout("redirect pool %lld\n", redir
.oloc
.pool
);
1909 __unregister_request(osdc
, req
);
1911 req
->r_target_oloc
= redir
.oloc
; /* struct */
1914 * Start redirect requests with nofail=true. If
1915 * mapping fails, request will end up on the notarget
1916 * list, waiting for the new osdmap (which can take
1917 * a while), even though the original request mapped
1918 * successfully. In the future we might want to follow
1919 * original request's nofail setting here.
1921 err
= __ceph_osdc_start_request(osdc
, req
, true);
1927 already_completed
= req
->r_got_reply
;
1928 if (!req
->r_got_reply
) {
1929 req
->r_result
= result
;
1930 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1932 if (req
->r_result
== 0)
1933 req
->r_result
= bytes
;
1935 /* in case this is a write and we need to replay, */
1936 req
->r_reassert_version
.epoch
= cpu_to_le32(reassert_epoch
);
1937 req
->r_reassert_version
.version
= cpu_to_le64(reassert_version
);
1939 req
->r_got_reply
= 1;
1940 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1941 dout("handle_reply tid %llu dup ack\n", tid
);
1945 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1947 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1948 __register_linger_request(osdc
, req
);
1950 /* either this is a read, or we got the safe response */
1952 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1953 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1954 __unregister_request(osdc
, req
);
1956 mutex_unlock(&osdc
->request_mutex
);
1957 up_read(&osdc
->map_sem
);
1959 if (!already_completed
) {
1960 if (req
->r_unsafe_callback
&&
1961 result
>= 0 && !(flags
& CEPH_OSD_FLAG_ONDISK
))
1962 req
->r_unsafe_callback(req
, true);
1963 if (req
->r_callback
)
1964 req
->r_callback(req
, msg
);
1966 complete_all(&req
->r_completion
);
1969 if (flags
& CEPH_OSD_FLAG_ONDISK
) {
1970 if (req
->r_unsafe_callback
&& already_completed
)
1971 req
->r_unsafe_callback(req
, false);
1972 complete_request(req
);
1976 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1977 ceph_osdc_put_request(req
);
1980 mutex_unlock(&osdc
->request_mutex
);
1981 up_read(&osdc
->map_sem
);
1985 req
->r_result
= -EIO
;
1986 __unregister_request(osdc
, req
);
1987 if (req
->r_callback
)
1988 req
->r_callback(req
, msg
);
1990 complete_all(&req
->r_completion
);
1991 complete_request(req
);
1992 ceph_osdc_put_request(req
);
1994 mutex_unlock(&osdc
->request_mutex
);
1995 up_read(&osdc
->map_sem
);
1997 pr_err("corrupt osd_op_reply got %d %d\n",
1998 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
));
2002 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
2004 struct rb_node
*p
, *n
;
2006 dout("%s %p\n", __func__
, osdc
);
2007 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
2008 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
2011 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
2012 memcmp(&osd
->o_con
.peer_addr
,
2013 ceph_osd_addr(osdc
->osdmap
,
2015 sizeof(struct ceph_entity_addr
)) != 0)
2016 __reset_osd(osdc
, osd
);
2021 * Requeue requests whose mapping to an OSD has changed. If requests map to
2022 * no osd, request a new map.
2024 * Caller should hold map_sem for read.
2026 static void kick_requests(struct ceph_osd_client
*osdc
, bool force_resend
,
2027 bool force_resend_writes
)
2029 struct ceph_osd_request
*req
, *nreq
;
2033 bool force_resend_req
;
2035 dout("kick_requests %s %s\n", force_resend
? " (force resend)" : "",
2036 force_resend_writes
? " (force resend writes)" : "");
2037 mutex_lock(&osdc
->request_mutex
);
2038 for (p
= rb_first(&osdc
->requests
); p
; ) {
2039 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
2043 * For linger requests that have not yet been
2044 * registered, move them to the linger list; they'll
2045 * be sent to the osd in the loop below. Unregister
2046 * the request before re-registering it as a linger
2047 * request to ensure the __map_request() below
2048 * will decide it needs to be sent.
2050 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
2051 dout("%p tid %llu restart on osd%d\n",
2053 req
->r_osd
? req
->r_osd
->o_osd
: -1);
2054 ceph_osdc_get_request(req
);
2055 __unregister_request(osdc
, req
);
2056 __register_linger_request(osdc
, req
);
2057 ceph_osdc_put_request(req
);
2061 force_resend_req
= force_resend
||
2062 (force_resend_writes
&&
2063 req
->r_flags
& CEPH_OSD_FLAG_WRITE
);
2064 err
= __map_request(osdc
, req
, force_resend_req
);
2066 continue; /* error */
2067 if (req
->r_osd
== NULL
) {
2068 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
2069 needmap
++; /* request a newer map */
2070 } else if (err
> 0) {
2071 if (!req
->r_linger
) {
2072 dout("%p tid %llu requeued on osd%d\n", req
,
2074 req
->r_osd
? req
->r_osd
->o_osd
: -1);
2075 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
2080 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
2082 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
2084 err
= __map_request(osdc
, req
,
2085 force_resend
|| force_resend_writes
);
2086 dout("__map_request returned %d\n", err
);
2088 continue; /* hrm! */
2089 if (req
->r_osd
== NULL
|| err
> 0) {
2090 if (req
->r_osd
== NULL
) {
2091 dout("lingering %p tid %llu maps to no osd\n",
2094 * A homeless lingering request makes
2095 * no sense, as it's job is to keep
2096 * a particular OSD connection open.
2097 * Request a newer map and kick the
2098 * request, knowing that it won't be
2099 * resent until we actually get a map
2100 * that can tell us where to send it.
2105 dout("kicking lingering %p tid %llu osd%d\n", req
,
2106 req
->r_tid
, req
->r_osd
? req
->r_osd
->o_osd
: -1);
2107 __register_request(osdc
, req
);
2108 __unregister_linger_request(osdc
, req
);
2111 reset_changed_osds(osdc
);
2112 mutex_unlock(&osdc
->request_mutex
);
2115 dout("%d requests for down osds, need new map\n", needmap
);
2116 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
2122 * Process updated osd map.
2124 * The message contains any number of incremental and full maps, normally
2125 * indicating some sort of topology change in the cluster. Kick requests
2126 * off to different OSDs as needed.
2128 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
2130 void *p
, *end
, *next
;
2131 u32 nr_maps
, maplen
;
2133 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
2135 struct ceph_fsid fsid
;
2138 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
2139 p
= msg
->front
.iov_base
;
2140 end
= p
+ msg
->front
.iov_len
;
2143 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
2144 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
2145 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
2148 down_write(&osdc
->map_sem
);
2150 was_full
= ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
);
2152 /* incremental maps */
2153 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
2154 dout(" %d inc maps\n", nr_maps
);
2155 while (nr_maps
> 0) {
2156 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2157 epoch
= ceph_decode_32(&p
);
2158 maplen
= ceph_decode_32(&p
);
2159 ceph_decode_need(&p
, end
, maplen
, bad
);
2161 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
2162 dout("applying incremental map %u len %d\n",
2164 newmap
= osdmap_apply_incremental(&p
, next
,
2166 &osdc
->client
->msgr
);
2167 if (IS_ERR(newmap
)) {
2168 err
= PTR_ERR(newmap
);
2172 if (newmap
!= osdc
->osdmap
) {
2173 ceph_osdmap_destroy(osdc
->osdmap
);
2174 osdc
->osdmap
= newmap
;
2176 was_full
= was_full
||
2177 ceph_osdmap_flag(osdc
->osdmap
,
2179 kick_requests(osdc
, 0, was_full
);
2181 dout("ignoring incremental map %u len %d\n",
2191 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
2192 dout(" %d full maps\n", nr_maps
);
2194 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
2195 epoch
= ceph_decode_32(&p
);
2196 maplen
= ceph_decode_32(&p
);
2197 ceph_decode_need(&p
, end
, maplen
, bad
);
2199 dout("skipping non-latest full map %u len %d\n",
2201 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
2202 dout("skipping full map %u len %d, "
2203 "older than our %u\n", epoch
, maplen
,
2204 osdc
->osdmap
->epoch
);
2206 int skipped_map
= 0;
2208 dout("taking full map %u len %d\n", epoch
, maplen
);
2209 newmap
= ceph_osdmap_decode(&p
, p
+maplen
);
2210 if (IS_ERR(newmap
)) {
2211 err
= PTR_ERR(newmap
);
2215 oldmap
= osdc
->osdmap
;
2216 osdc
->osdmap
= newmap
;
2218 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
2220 ceph_osdmap_destroy(oldmap
);
2222 was_full
= was_full
||
2223 ceph_osdmap_flag(osdc
->osdmap
,
2225 kick_requests(osdc
, skipped_map
, was_full
);
2234 downgrade_write(&osdc
->map_sem
);
2235 ceph_monc_got_map(&osdc
->client
->monc
, CEPH_SUB_OSDMAP
,
2236 osdc
->osdmap
->epoch
);
2239 * subscribe to subsequent osdmap updates if full to ensure
2240 * we find out when we are no longer full and stop returning
2243 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
) ||
2244 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSERD
) ||
2245 ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_PAUSEWR
))
2246 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
2248 mutex_lock(&osdc
->request_mutex
);
2249 __send_queued(osdc
);
2250 mutex_unlock(&osdc
->request_mutex
);
2251 up_read(&osdc
->map_sem
);
2252 wake_up_all(&osdc
->client
->auth_wq
);
2256 pr_err("osdc handle_map corrupt msg\n");
2258 up_write(&osdc
->map_sem
);
2262 * watch/notify callback event infrastructure
2264 * These callbacks are used both for watch and notify operations.
2266 static void __release_event(struct kref
*kref
)
2268 struct ceph_osd_event
*event
=
2269 container_of(kref
, struct ceph_osd_event
, kref
);
2271 dout("__release_event %p\n", event
);
2275 static void get_event(struct ceph_osd_event
*event
)
2277 kref_get(&event
->kref
);
2280 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
2282 kref_put(&event
->kref
, __release_event
);
2284 EXPORT_SYMBOL(ceph_osdc_put_event
);
2286 static void __insert_event(struct ceph_osd_client
*osdc
,
2287 struct ceph_osd_event
*new)
2289 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
2290 struct rb_node
*parent
= NULL
;
2291 struct ceph_osd_event
*event
= NULL
;
2295 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
2296 if (new->cookie
< event
->cookie
)
2298 else if (new->cookie
> event
->cookie
)
2299 p
= &(*p
)->rb_right
;
2304 rb_link_node(&new->node
, parent
, p
);
2305 rb_insert_color(&new->node
, &osdc
->event_tree
);
2308 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
2311 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
2312 struct rb_node
*parent
= NULL
;
2313 struct ceph_osd_event
*event
= NULL
;
2317 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
2318 if (cookie
< event
->cookie
)
2320 else if (cookie
> event
->cookie
)
2321 p
= &(*p
)->rb_right
;
2328 static void __remove_event(struct ceph_osd_event
*event
)
2330 struct ceph_osd_client
*osdc
= event
->osdc
;
2332 if (!RB_EMPTY_NODE(&event
->node
)) {
2333 dout("__remove_event removed %p\n", event
);
2334 rb_erase(&event
->node
, &osdc
->event_tree
);
2335 ceph_osdc_put_event(event
);
2337 dout("__remove_event didn't remove %p\n", event
);
2341 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
2342 void (*event_cb
)(u64
, u64
, u8
, void *),
2343 void *data
, struct ceph_osd_event
**pevent
)
2345 struct ceph_osd_event
*event
;
2347 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
2351 dout("create_event %p\n", event
);
2352 event
->cb
= event_cb
;
2353 event
->one_shot
= 0;
2356 INIT_LIST_HEAD(&event
->osd_node
);
2357 RB_CLEAR_NODE(&event
->node
);
2358 kref_init(&event
->kref
); /* one ref for us */
2359 kref_get(&event
->kref
); /* one ref for the caller */
2361 spin_lock(&osdc
->event_lock
);
2362 event
->cookie
= ++osdc
->event_count
;
2363 __insert_event(osdc
, event
);
2364 spin_unlock(&osdc
->event_lock
);
2369 EXPORT_SYMBOL(ceph_osdc_create_event
);
2371 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
2373 struct ceph_osd_client
*osdc
= event
->osdc
;
2375 dout("cancel_event %p\n", event
);
2376 spin_lock(&osdc
->event_lock
);
2377 __remove_event(event
);
2378 spin_unlock(&osdc
->event_lock
);
2379 ceph_osdc_put_event(event
); /* caller's */
2381 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
2384 static void do_event_work(struct work_struct
*work
)
2386 struct ceph_osd_event_work
*event_work
=
2387 container_of(work
, struct ceph_osd_event_work
, work
);
2388 struct ceph_osd_event
*event
= event_work
->event
;
2389 u64 ver
= event_work
->ver
;
2390 u64 notify_id
= event_work
->notify_id
;
2391 u8 opcode
= event_work
->opcode
;
2393 dout("do_event_work completing %p\n", event
);
2394 event
->cb(ver
, notify_id
, opcode
, event
->data
);
2395 dout("do_event_work completed %p\n", event
);
2396 ceph_osdc_put_event(event
);
2402 * Process osd watch notifications
2404 static void handle_watch_notify(struct ceph_osd_client
*osdc
,
2405 struct ceph_msg
*msg
)
2409 u64 cookie
, ver
, notify_id
;
2411 struct ceph_osd_event
*event
;
2412 struct ceph_osd_event_work
*event_work
;
2414 p
= msg
->front
.iov_base
;
2415 end
= p
+ msg
->front
.iov_len
;
2417 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
2418 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
2419 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
2420 ceph_decode_64_safe(&p
, end
, ver
, bad
);
2421 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
2423 spin_lock(&osdc
->event_lock
);
2424 event
= __find_event(osdc
, cookie
);
2426 BUG_ON(event
->one_shot
);
2429 spin_unlock(&osdc
->event_lock
);
2430 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2431 cookie
, ver
, event
);
2433 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
2435 pr_err("couldn't allocate event_work\n");
2436 ceph_osdc_put_event(event
);
2439 INIT_WORK(&event_work
->work
, do_event_work
);
2440 event_work
->event
= event
;
2441 event_work
->ver
= ver
;
2442 event_work
->notify_id
= notify_id
;
2443 event_work
->opcode
= opcode
;
2445 queue_work(osdc
->notify_wq
, &event_work
->work
);
2451 pr_err("osdc handle_watch_notify corrupt msg\n");
2455 * build new request AND message
2458 void ceph_osdc_build_request(struct ceph_osd_request
*req
, u64 off
,
2459 struct ceph_snap_context
*snapc
, u64 snap_id
,
2460 struct timespec
*mtime
)
2462 struct ceph_msg
*msg
= req
->r_request
;
2465 int flags
= req
->r_flags
;
2469 req
->r_snapid
= snap_id
;
2470 WARN_ON(snapc
!= req
->r_snapc
);
2472 /* encode request */
2473 msg
->hdr
.version
= cpu_to_le16(4);
2475 p
= msg
->front
.iov_base
;
2476 ceph_encode_32(&p
, 1); /* client_inc is always 1 */
2477 req
->r_request_osdmap_epoch
= p
;
2479 req
->r_request_flags
= p
;
2481 if (req
->r_flags
& CEPH_OSD_FLAG_WRITE
)
2482 ceph_encode_timespec(p
, mtime
);
2483 p
+= sizeof(struct ceph_timespec
);
2484 req
->r_request_reassert_version
= p
;
2485 p
+= sizeof(struct ceph_eversion
); /* will get filled in */
2488 ceph_encode_8(&p
, 4);
2489 ceph_encode_8(&p
, 4);
2490 ceph_encode_32(&p
, 8 + 4 + 4);
2491 req
->r_request_pool
= p
;
2493 ceph_encode_32(&p
, -1); /* preferred */
2494 ceph_encode_32(&p
, 0); /* key len */
2496 ceph_encode_8(&p
, 1);
2497 req
->r_request_pgid
= p
;
2499 ceph_encode_32(&p
, -1); /* preferred */
2502 ceph_encode_32(&p
, req
->r_base_oid
.name_len
);
2503 memcpy(p
, req
->r_base_oid
.name
, req
->r_base_oid
.name_len
);
2504 dout("oid '%.*s' len %d\n", req
->r_base_oid
.name_len
,
2505 req
->r_base_oid
.name
, req
->r_base_oid
.name_len
);
2506 p
+= req
->r_base_oid
.name_len
;
2508 /* ops--can imply data */
2509 ceph_encode_16(&p
, (u16
)req
->r_num_ops
);
2511 for (i
= 0; i
< req
->r_num_ops
; i
++) {
2512 data_len
+= osd_req_encode_op(req
, p
, i
);
2513 p
+= sizeof(struct ceph_osd_op
);
2517 ceph_encode_64(&p
, req
->r_snapid
);
2518 ceph_encode_64(&p
, req
->r_snapc
? req
->r_snapc
->seq
: 0);
2519 ceph_encode_32(&p
, req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
2521 for (i
= 0; i
< req
->r_snapc
->num_snaps
; i
++) {
2522 ceph_encode_64(&p
, req
->r_snapc
->snaps
[i
]);
2526 req
->r_request_attempts
= p
;
2530 if (flags
& CEPH_OSD_FLAG_WRITE
) {
2534 * The header "data_off" is a hint to the receiver
2535 * allowing it to align received data into its
2536 * buffers such that there's no need to re-copy
2537 * it before writing it to disk (direct I/O).
2539 data_off
= (u16
) (off
& 0xffff);
2540 req
->r_request
->hdr
.data_off
= cpu_to_le16(data_off
);
2542 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
2544 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
2545 msg_size
= p
- msg
->front
.iov_base
;
2546 msg
->front
.iov_len
= msg_size
;
2547 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
2549 dout("build_request msg_size was %d\n", (int)msg_size
);
2551 EXPORT_SYMBOL(ceph_osdc_build_request
);
2554 * Register request, send initial attempt.
2556 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
2557 struct ceph_osd_request
*req
,
2562 down_read(&osdc
->map_sem
);
2563 mutex_lock(&osdc
->request_mutex
);
2565 rc
= __ceph_osdc_start_request(osdc
, req
, nofail
);
2567 mutex_unlock(&osdc
->request_mutex
);
2568 up_read(&osdc
->map_sem
);
2572 EXPORT_SYMBOL(ceph_osdc_start_request
);
2575 * Unregister a registered request. The request is not completed (i.e.
2576 * no callbacks or wakeups) - higher layers are supposed to know what
2577 * they are canceling.
2579 void ceph_osdc_cancel_request(struct ceph_osd_request
*req
)
2581 struct ceph_osd_client
*osdc
= req
->r_osdc
;
2583 mutex_lock(&osdc
->request_mutex
);
2585 __unregister_linger_request(osdc
, req
);
2586 __unregister_request(osdc
, req
);
2587 mutex_unlock(&osdc
->request_mutex
);
2589 dout("%s %p tid %llu canceled\n", __func__
, req
, req
->r_tid
);
2591 EXPORT_SYMBOL(ceph_osdc_cancel_request
);
2594 * wait for a request to complete
2596 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
2597 struct ceph_osd_request
*req
)
2601 dout("%s %p tid %llu\n", __func__
, req
, req
->r_tid
);
2603 rc
= wait_for_completion_interruptible(&req
->r_completion
);
2605 dout("%s %p tid %llu interrupted\n", __func__
, req
, req
->r_tid
);
2606 ceph_osdc_cancel_request(req
);
2607 complete_request(req
);
2611 dout("%s %p tid %llu result %d\n", __func__
, req
, req
->r_tid
,
2613 return req
->r_result
;
2615 EXPORT_SYMBOL(ceph_osdc_wait_request
);
2618 * sync - wait for all in-flight requests to flush. avoid starvation.
2620 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
2622 struct ceph_osd_request
*req
;
2623 u64 last_tid
, next_tid
= 0;
2625 mutex_lock(&osdc
->request_mutex
);
2626 last_tid
= osdc
->last_tid
;
2628 req
= __lookup_request_ge(osdc
, next_tid
);
2631 if (req
->r_tid
> last_tid
)
2634 next_tid
= req
->r_tid
+ 1;
2635 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
2638 ceph_osdc_get_request(req
);
2639 mutex_unlock(&osdc
->request_mutex
);
2640 dout("sync waiting on tid %llu (last is %llu)\n",
2641 req
->r_tid
, last_tid
);
2642 wait_for_completion(&req
->r_safe_completion
);
2643 mutex_lock(&osdc
->request_mutex
);
2644 ceph_osdc_put_request(req
);
2646 mutex_unlock(&osdc
->request_mutex
);
2647 dout("sync done (thru tid %llu)\n", last_tid
);
2649 EXPORT_SYMBOL(ceph_osdc_sync
);
2652 * Call all pending notify callbacks - for use after a watch is
2653 * unregistered, to make sure no more callbacks for it will be invoked
2655 void ceph_osdc_flush_notifies(struct ceph_osd_client
*osdc
)
2657 flush_workqueue(osdc
->notify_wq
);
2659 EXPORT_SYMBOL(ceph_osdc_flush_notifies
);
2665 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
2670 osdc
->client
= client
;
2671 osdc
->osdmap
= NULL
;
2672 init_rwsem(&osdc
->map_sem
);
2673 init_completion(&osdc
->map_waiters
);
2674 osdc
->last_requested_map
= 0;
2675 mutex_init(&osdc
->request_mutex
);
2677 osdc
->osds
= RB_ROOT
;
2678 INIT_LIST_HEAD(&osdc
->osd_lru
);
2679 osdc
->requests
= RB_ROOT
;
2680 INIT_LIST_HEAD(&osdc
->req_lru
);
2681 INIT_LIST_HEAD(&osdc
->req_unsent
);
2682 INIT_LIST_HEAD(&osdc
->req_notarget
);
2683 INIT_LIST_HEAD(&osdc
->req_linger
);
2684 osdc
->num_requests
= 0;
2685 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
2686 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
2687 spin_lock_init(&osdc
->event_lock
);
2688 osdc
->event_tree
= RB_ROOT
;
2689 osdc
->event_count
= 0;
2691 schedule_delayed_work(&osdc
->osds_timeout_work
,
2692 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
));
2695 osdc
->req_mempool
= mempool_create_slab_pool(10,
2696 ceph_osd_request_cache
);
2697 if (!osdc
->req_mempool
)
2700 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
2701 PAGE_SIZE
, 10, true, "osd_op");
2704 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
2705 PAGE_SIZE
, 10, true, "osd_op_reply");
2710 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
2711 if (!osdc
->notify_wq
)
2712 goto out_msgpool_reply
;
2717 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2719 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2721 mempool_destroy(osdc
->req_mempool
);
2726 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
2728 flush_workqueue(osdc
->notify_wq
);
2729 destroy_workqueue(osdc
->notify_wq
);
2730 cancel_delayed_work_sync(&osdc
->timeout_work
);
2731 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
2733 ceph_osdmap_destroy(osdc
->osdmap
);
2734 osdc
->osdmap
= NULL
;
2736 remove_all_osds(osdc
);
2737 mempool_destroy(osdc
->req_mempool
);
2738 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2739 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2743 * Read some contiguous pages. If we cross a stripe boundary, shorten
2744 * *plen. Return number of bytes read, or error.
2746 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
2747 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
2749 u32 truncate_seq
, u64 truncate_size
,
2750 struct page
**pages
, int num_pages
, int page_align
)
2752 struct ceph_osd_request
*req
;
2755 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
2756 vino
.snap
, off
, *plen
);
2757 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
, 0, 1,
2758 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
2759 NULL
, truncate_seq
, truncate_size
,
2762 return PTR_ERR(req
);
2764 /* it may be a short read due to an object boundary */
2766 osd_req_op_extent_osd_data_pages(req
, 0,
2767 pages
, *plen
, page_align
, false, false);
2769 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2770 off
, *plen
, *plen
, page_align
);
2772 ceph_osdc_build_request(req
, off
, NULL
, vino
.snap
, NULL
);
2774 rc
= ceph_osdc_start_request(osdc
, req
, false);
2776 rc
= ceph_osdc_wait_request(osdc
, req
);
2778 ceph_osdc_put_request(req
);
2779 dout("readpages result %d\n", rc
);
2782 EXPORT_SYMBOL(ceph_osdc_readpages
);
2785 * do a synchronous write on N pages
2787 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
2788 struct ceph_file_layout
*layout
,
2789 struct ceph_snap_context
*snapc
,
2791 u32 truncate_seq
, u64 truncate_size
,
2792 struct timespec
*mtime
,
2793 struct page
**pages
, int num_pages
)
2795 struct ceph_osd_request
*req
;
2797 int page_align
= off
& ~PAGE_MASK
;
2799 BUG_ON(vino
.snap
!= CEPH_NOSNAP
); /* snapshots aren't writeable */
2800 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
, 0, 1,
2802 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
2803 snapc
, truncate_seq
, truncate_size
,
2806 return PTR_ERR(req
);
2808 /* it may be a short write due to an object boundary */
2809 osd_req_op_extent_osd_data_pages(req
, 0, pages
, len
, page_align
,
2811 dout("writepages %llu~%llu (%llu bytes)\n", off
, len
, len
);
2813 ceph_osdc_build_request(req
, off
, snapc
, CEPH_NOSNAP
, mtime
);
2815 rc
= ceph_osdc_start_request(osdc
, req
, true);
2817 rc
= ceph_osdc_wait_request(osdc
, req
);
2819 ceph_osdc_put_request(req
);
2822 dout("writepages result %d\n", rc
);
2825 EXPORT_SYMBOL(ceph_osdc_writepages
);
2827 int ceph_osdc_setup(void)
2829 size_t size
= sizeof(struct ceph_osd_request
) +
2830 CEPH_OSD_SLAB_OPS
* sizeof(struct ceph_osd_req_op
);
2832 BUG_ON(ceph_osd_request_cache
);
2833 ceph_osd_request_cache
= kmem_cache_create("ceph_osd_request", size
,
2836 return ceph_osd_request_cache
? 0 : -ENOMEM
;
2838 EXPORT_SYMBOL(ceph_osdc_setup
);
2840 void ceph_osdc_cleanup(void)
2842 BUG_ON(!ceph_osd_request_cache
);
2843 kmem_cache_destroy(ceph_osd_request_cache
);
2844 ceph_osd_request_cache
= NULL
;
2846 EXPORT_SYMBOL(ceph_osdc_cleanup
);
2849 * handle incoming message
2851 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2853 struct ceph_osd
*osd
= con
->private;
2854 struct ceph_osd_client
*osdc
;
2855 int type
= le16_to_cpu(msg
->hdr
.type
);
2862 case CEPH_MSG_OSD_MAP
:
2863 ceph_osdc_handle_map(osdc
, msg
);
2865 case CEPH_MSG_OSD_OPREPLY
:
2866 handle_reply(osdc
, msg
);
2868 case CEPH_MSG_WATCH_NOTIFY
:
2869 handle_watch_notify(osdc
, msg
);
2873 pr_err("received unknown message type %d %s\n", type
,
2874 ceph_msg_type_name(type
));
2881 * Lookup and return message for incoming reply. Don't try to do
2882 * anything about a larger than preallocated data portion of the
2883 * message at the moment - for now, just skip the message.
2885 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2886 struct ceph_msg_header
*hdr
,
2889 struct ceph_osd
*osd
= con
->private;
2890 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2892 struct ceph_osd_request
*req
;
2893 int front_len
= le32_to_cpu(hdr
->front_len
);
2894 int data_len
= le32_to_cpu(hdr
->data_len
);
2897 tid
= le64_to_cpu(hdr
->tid
);
2898 mutex_lock(&osdc
->request_mutex
);
2899 req
= __lookup_request(osdc
, tid
);
2901 dout("%s osd%d tid %llu unknown, skipping\n", __func__
,
2908 ceph_msg_revoke_incoming(req
->r_reply
);
2910 if (front_len
> req
->r_reply
->front_alloc_len
) {
2911 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2912 __func__
, osd
->o_osd
, req
->r_tid
, front_len
,
2913 req
->r_reply
->front_alloc_len
);
2914 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front_len
, GFP_NOFS
,
2918 ceph_msg_put(req
->r_reply
);
2922 if (data_len
> req
->r_reply
->data_length
) {
2923 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2924 __func__
, osd
->o_osd
, req
->r_tid
, data_len
,
2925 req
->r_reply
->data_length
);
2931 m
= ceph_msg_get(req
->r_reply
);
2932 dout("get_reply tid %lld %p\n", tid
, m
);
2935 mutex_unlock(&osdc
->request_mutex
);
2939 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2940 struct ceph_msg_header
*hdr
,
2943 struct ceph_osd
*osd
= con
->private;
2944 int type
= le16_to_cpu(hdr
->type
);
2945 int front
= le32_to_cpu(hdr
->front_len
);
2949 case CEPH_MSG_OSD_MAP
:
2950 case CEPH_MSG_WATCH_NOTIFY
:
2951 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2952 case CEPH_MSG_OSD_OPREPLY
:
2953 return get_reply(con
, hdr
, skip
);
2955 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2963 * Wrappers to refcount containing ceph_osd struct
2965 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2967 struct ceph_osd
*osd
= con
->private;
2973 static void put_osd_con(struct ceph_connection
*con
)
2975 struct ceph_osd
*osd
= con
->private;
2983 * Note: returned pointer is the address of a structure that's
2984 * managed separately. Caller must *not* attempt to free it.
2986 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2987 int *proto
, int force_new
)
2989 struct ceph_osd
*o
= con
->private;
2990 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2991 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2992 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2994 if (force_new
&& auth
->authorizer
) {
2995 ceph_auth_destroy_authorizer(auth
->authorizer
);
2996 auth
->authorizer
= NULL
;
2998 if (!auth
->authorizer
) {
2999 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
3002 return ERR_PTR(ret
);
3004 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
3007 return ERR_PTR(ret
);
3009 *proto
= ac
->protocol
;
3015 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
3017 struct ceph_osd
*o
= con
->private;
3018 struct ceph_osd_client
*osdc
= o
->o_osdc
;
3019 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
3021 return ceph_auth_verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
3024 static int invalidate_authorizer(struct ceph_connection
*con
)
3026 struct ceph_osd
*o
= con
->private;
3027 struct ceph_osd_client
*osdc
= o
->o_osdc
;
3028 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
3030 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
3031 return ceph_monc_validate_auth(&osdc
->client
->monc
);
3034 static int osd_sign_message(struct ceph_msg
*msg
)
3036 struct ceph_osd
*o
= msg
->con
->private;
3037 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
3039 return ceph_auth_sign_message(auth
, msg
);
3042 static int osd_check_message_signature(struct ceph_msg
*msg
)
3044 struct ceph_osd
*o
= msg
->con
->private;
3045 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
3047 return ceph_auth_check_message_signature(auth
, msg
);
3050 static const struct ceph_connection_operations osd_con_ops
= {
3053 .dispatch
= dispatch
,
3054 .get_authorizer
= get_authorizer
,
3055 .verify_authorizer_reply
= verify_authorizer_reply
,
3056 .invalidate_authorizer
= invalidate_authorizer
,
3057 .alloc_msg
= alloc_msg
,
3058 .sign_message
= osd_sign_message
,
3059 .check_message_signature
= osd_check_message_signature
,