1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/highmem.h>
7 #include <linux/pagemap.h>
8 #include <linux/slab.h>
9 #include <linux/uaccess.h>
11 #include <linux/bio.h>
14 #include <linux/ceph/libceph.h>
15 #include <linux/ceph/osd_client.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/auth.h>
19 #include <linux/ceph/pagelist.h>
21 #define OSD_OP_FRONT_LEN 4096
22 #define OSD_OPREPLY_FRONT_LEN 512
24 static const struct ceph_connection_operations osd_con_ops
;
26 static void __send_queued(struct ceph_osd_client
*osdc
);
27 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
);
28 static void __register_request(struct ceph_osd_client
*osdc
,
29 struct ceph_osd_request
*req
);
30 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
31 struct ceph_osd_request
*req
);
32 static void __send_request(struct ceph_osd_client
*osdc
,
33 struct ceph_osd_request
*req
);
35 static int op_has_extent(int op
)
37 return (op
== CEPH_OSD_OP_READ
||
38 op
== CEPH_OSD_OP_WRITE
);
42 * Implement client access to distributed object storage cluster.
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
64 * fill osd op in request message.
66 static int calc_layout(struct ceph_file_layout
*layout
, u64 off
, u64
*plen
,
67 struct ceph_osd_req_op
*op
, u64
*bno
)
75 r
= ceph_calc_file_object_mapping(layout
, off
, orig_len
, bno
,
79 if (objlen
< orig_len
) {
81 dout(" skipping last %llu, final file extent %llu~%llu\n",
82 orig_len
- *plen
, off
, *plen
);
85 if (op_has_extent(op
->op
)) {
86 u32 osize
= le32_to_cpu(layout
->fl_object_size
);
87 op
->extent
.offset
= objoff
;
88 op
->extent
.length
= objlen
;
89 if (op
->extent
.truncate_size
<= off
- objoff
) {
90 op
->extent
.truncate_size
= 0;
92 op
->extent
.truncate_size
-= off
- objoff
;
93 if (op
->extent
.truncate_size
> osize
)
94 op
->extent
.truncate_size
= osize
;
97 if (op
->op
== CEPH_OSD_OP_WRITE
)
98 op
->payload_len
= *plen
;
100 dout("calc_layout bno=%llx %llu~%llu\n", *bno
, objoff
, objlen
);
108 void ceph_osdc_release_request(struct kref
*kref
)
111 struct ceph_osd_request
*req
= container_of(kref
,
112 struct ceph_osd_request
,
116 ceph_msg_put(req
->r_request
);
117 if (req
->r_con_filling_msg
) {
118 dout("%s revoking msg %p from con %p\n", __func__
,
119 req
->r_reply
, req
->r_con_filling_msg
);
120 ceph_msg_revoke_incoming(req
->r_reply
);
121 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
122 req
->r_con_filling_msg
= NULL
;
125 ceph_msg_put(req
->r_reply
);
127 if (req
->r_data_in
.type
== CEPH_OSD_DATA_TYPE_PAGES
&&
128 req
->r_data_in
.own_pages
) {
129 num_pages
= calc_pages_for((u64
)req
->r_data_in
.alignment
,
130 (u64
)req
->r_data_in
.length
);
131 ceph_release_page_vector(req
->r_data_in
.pages
, num_pages
);
133 if (req
->r_data_out
.type
== CEPH_OSD_DATA_TYPE_PAGES
&&
134 req
->r_data_out
.own_pages
) {
135 num_pages
= calc_pages_for((u64
)req
->r_data_out
.alignment
,
136 (u64
)req
->r_data_out
.length
);
137 ceph_release_page_vector(req
->r_data_out
.pages
, num_pages
);
140 ceph_put_snap_context(req
->r_snapc
);
142 mempool_free(req
, req
->r_osdc
->req_mempool
);
146 EXPORT_SYMBOL(ceph_osdc_release_request
);
148 struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
149 struct ceph_snap_context
*snapc
,
150 unsigned int num_ops
,
154 struct ceph_osd_request
*req
;
155 struct ceph_msg
*msg
;
158 msg_size
= 4 + 4 + 8 + 8 + 4+8;
159 msg_size
+= 2 + 4 + 8 + 4 + 4; /* oloc */
160 msg_size
+= 1 + 8 + 4 + 4; /* pg_t */
161 msg_size
+= 4 + MAX_OBJ_NAME_SIZE
;
162 msg_size
+= 2 + num_ops
*sizeof(struct ceph_osd_op
);
163 msg_size
+= 8; /* snapid */
164 msg_size
+= 8; /* snap_seq */
165 msg_size
+= 8 * (snapc
? snapc
->num_snaps
: 0); /* snaps */
169 req
= mempool_alloc(osdc
->req_mempool
, gfp_flags
);
170 memset(req
, 0, sizeof(*req
));
172 req
= kzalloc(sizeof(*req
), gfp_flags
);
178 req
->r_mempool
= use_mempool
;
180 kref_init(&req
->r_kref
);
181 init_completion(&req
->r_completion
);
182 init_completion(&req
->r_safe_completion
);
183 RB_CLEAR_NODE(&req
->r_node
);
184 INIT_LIST_HEAD(&req
->r_unsafe_item
);
185 INIT_LIST_HEAD(&req
->r_linger_item
);
186 INIT_LIST_HEAD(&req
->r_linger_osd
);
187 INIT_LIST_HEAD(&req
->r_req_lru_item
);
188 INIT_LIST_HEAD(&req
->r_osd_item
);
190 /* create reply message */
192 msg
= ceph_msgpool_get(&osdc
->msgpool_op_reply
, 0);
194 msg
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
,
195 OSD_OPREPLY_FRONT_LEN
, gfp_flags
, true);
197 ceph_osdc_put_request(req
);
202 req
->r_data_in
.type
= CEPH_OSD_DATA_TYPE_NONE
;
203 req
->r_data_out
.type
= CEPH_OSD_DATA_TYPE_NONE
;
205 /* create request message; allow space for oid */
207 msg
= ceph_msgpool_get(&osdc
->msgpool_op
, 0);
209 msg
= ceph_msg_new(CEPH_MSG_OSD_OP
, msg_size
, gfp_flags
, true);
211 ceph_osdc_put_request(req
);
215 memset(msg
->front
.iov_base
, 0, msg
->front
.iov_len
);
217 req
->r_request
= msg
;
221 EXPORT_SYMBOL(ceph_osdc_alloc_request
);
223 static bool osd_req_opcode_valid(u16 opcode
)
226 case CEPH_OSD_OP_READ
:
227 case CEPH_OSD_OP_STAT
:
228 case CEPH_OSD_OP_MAPEXT
:
229 case CEPH_OSD_OP_MASKTRUNC
:
230 case CEPH_OSD_OP_SPARSE_READ
:
231 case CEPH_OSD_OP_NOTIFY
:
232 case CEPH_OSD_OP_NOTIFY_ACK
:
233 case CEPH_OSD_OP_ASSERT_VER
:
234 case CEPH_OSD_OP_WRITE
:
235 case CEPH_OSD_OP_WRITEFULL
:
236 case CEPH_OSD_OP_TRUNCATE
:
237 case CEPH_OSD_OP_ZERO
:
238 case CEPH_OSD_OP_DELETE
:
239 case CEPH_OSD_OP_APPEND
:
240 case CEPH_OSD_OP_STARTSYNC
:
241 case CEPH_OSD_OP_SETTRUNC
:
242 case CEPH_OSD_OP_TRIMTRUNC
:
243 case CEPH_OSD_OP_TMAPUP
:
244 case CEPH_OSD_OP_TMAPPUT
:
245 case CEPH_OSD_OP_TMAPGET
:
246 case CEPH_OSD_OP_CREATE
:
247 case CEPH_OSD_OP_ROLLBACK
:
248 case CEPH_OSD_OP_WATCH
:
249 case CEPH_OSD_OP_OMAPGETKEYS
:
250 case CEPH_OSD_OP_OMAPGETVALS
:
251 case CEPH_OSD_OP_OMAPGETHEADER
:
252 case CEPH_OSD_OP_OMAPGETVALSBYKEYS
:
253 case CEPH_OSD_OP_OMAPSETVALS
:
254 case CEPH_OSD_OP_OMAPSETHEADER
:
255 case CEPH_OSD_OP_OMAPCLEAR
:
256 case CEPH_OSD_OP_OMAPRMKEYS
:
257 case CEPH_OSD_OP_OMAP_CMP
:
258 case CEPH_OSD_OP_CLONERANGE
:
259 case CEPH_OSD_OP_ASSERT_SRC_VERSION
:
260 case CEPH_OSD_OP_SRC_CMPXATTR
:
261 case CEPH_OSD_OP_GETXATTR
:
262 case CEPH_OSD_OP_GETXATTRS
:
263 case CEPH_OSD_OP_CMPXATTR
:
264 case CEPH_OSD_OP_SETXATTR
:
265 case CEPH_OSD_OP_SETXATTRS
:
266 case CEPH_OSD_OP_RESETXATTRS
:
267 case CEPH_OSD_OP_RMXATTR
:
268 case CEPH_OSD_OP_PULL
:
269 case CEPH_OSD_OP_PUSH
:
270 case CEPH_OSD_OP_BALANCEREADS
:
271 case CEPH_OSD_OP_UNBALANCEREADS
:
272 case CEPH_OSD_OP_SCRUB
:
273 case CEPH_OSD_OP_SCRUB_RESERVE
:
274 case CEPH_OSD_OP_SCRUB_UNRESERVE
:
275 case CEPH_OSD_OP_SCRUB_STOP
:
276 case CEPH_OSD_OP_SCRUB_MAP
:
277 case CEPH_OSD_OP_WRLOCK
:
278 case CEPH_OSD_OP_WRUNLOCK
:
279 case CEPH_OSD_OP_RDLOCK
:
280 case CEPH_OSD_OP_RDUNLOCK
:
281 case CEPH_OSD_OP_UPLOCK
:
282 case CEPH_OSD_OP_DNLOCK
:
283 case CEPH_OSD_OP_CALL
:
284 case CEPH_OSD_OP_PGLS
:
285 case CEPH_OSD_OP_PGLS_FILTER
:
293 * This is an osd op init function for opcodes that have no data or
294 * other information associated with them. It also serves as a
295 * common init routine for all the other init functions, below.
297 void osd_req_op_init(struct ceph_osd_req_op
*op
, u16 opcode
)
299 BUG_ON(!osd_req_opcode_valid(opcode
));
301 memset(op
, 0, sizeof (*op
));
306 void osd_req_op_extent_init(struct ceph_osd_req_op
*op
, u16 opcode
,
307 u64 offset
, u64 length
,
308 u64 truncate_size
, u32 truncate_seq
)
310 size_t payload_len
= 0;
312 BUG_ON(opcode
!= CEPH_OSD_OP_READ
&& opcode
!= CEPH_OSD_OP_WRITE
);
314 osd_req_op_init(op
, opcode
);
316 op
->extent
.offset
= offset
;
317 op
->extent
.length
= length
;
318 op
->extent
.truncate_size
= truncate_size
;
319 op
->extent
.truncate_seq
= truncate_seq
;
320 if (opcode
== CEPH_OSD_OP_WRITE
)
321 payload_len
+= length
;
323 op
->payload_len
= payload_len
;
325 EXPORT_SYMBOL(osd_req_op_extent_init
);
327 void osd_req_op_cls_init(struct ceph_osd_req_op
*op
, u16 opcode
,
328 const char *class, const char *method
,
329 const void *request_data
, size_t request_data_size
)
331 size_t payload_len
= 0;
334 BUG_ON(opcode
!= CEPH_OSD_OP_CALL
);
336 osd_req_op_init(op
, opcode
);
338 op
->cls
.class_name
= class;
339 size
= strlen(class);
340 BUG_ON(size
> (size_t) U8_MAX
);
341 op
->cls
.class_len
= size
;
344 op
->cls
.method_name
= method
;
345 size
= strlen(method
);
346 BUG_ON(size
> (size_t) U8_MAX
);
347 op
->cls
.method_len
= size
;
350 op
->cls
.indata
= request_data
;
351 BUG_ON(request_data_size
> (size_t) U32_MAX
);
352 op
->cls
.indata_len
= (u32
) request_data_size
;
353 payload_len
+= request_data_size
;
355 op
->cls
.argc
= 0; /* currently unused */
357 op
->payload_len
= payload_len
;
359 EXPORT_SYMBOL(osd_req_op_cls_init
);
361 void osd_req_op_watch_init(struct ceph_osd_req_op
*op
, u16 opcode
,
362 u64 cookie
, u64 version
, int flag
)
364 BUG_ON(opcode
!= CEPH_OSD_OP_NOTIFY_ACK
&& opcode
!= CEPH_OSD_OP_WATCH
);
366 osd_req_op_init(op
, opcode
);
368 op
->watch
.cookie
= cookie
;
369 /* op->watch.ver = version; */ /* XXX 3847 */
370 op
->watch
.ver
= cpu_to_le64(version
);
371 if (opcode
== CEPH_OSD_OP_WATCH
&& flag
)
372 op
->watch
.flag
= (u8
) 1;
374 EXPORT_SYMBOL(osd_req_op_watch_init
);
376 static u64
osd_req_encode_op(struct ceph_osd_request
*req
,
377 struct ceph_osd_op
*dst
,
378 struct ceph_osd_req_op
*src
)
380 u64 out_data_len
= 0;
381 struct ceph_pagelist
*pagelist
;
383 if (WARN_ON(!osd_req_opcode_valid(src
->op
))) {
384 pr_err("unrecognized osd opcode %d\n", src
->op
);
390 case CEPH_OSD_OP_STAT
:
392 case CEPH_OSD_OP_READ
:
393 case CEPH_OSD_OP_WRITE
:
394 if (src
->op
== CEPH_OSD_OP_WRITE
)
395 out_data_len
= src
->extent
.length
;
396 dst
->extent
.offset
= cpu_to_le64(src
->extent
.offset
);
397 dst
->extent
.length
= cpu_to_le64(src
->extent
.length
);
398 dst
->extent
.truncate_size
=
399 cpu_to_le64(src
->extent
.truncate_size
);
400 dst
->extent
.truncate_seq
=
401 cpu_to_le32(src
->extent
.truncate_seq
);
403 case CEPH_OSD_OP_CALL
:
404 pagelist
= kmalloc(sizeof (*pagelist
), GFP_NOFS
);
406 ceph_pagelist_init(pagelist
);
408 dst
->cls
.class_len
= src
->cls
.class_len
;
409 dst
->cls
.method_len
= src
->cls
.method_len
;
410 dst
->cls
.indata_len
= cpu_to_le32(src
->cls
.indata_len
);
411 ceph_pagelist_append(pagelist
, src
->cls
.class_name
,
413 ceph_pagelist_append(pagelist
, src
->cls
.method_name
,
414 src
->cls
.method_len
);
415 ceph_pagelist_append(pagelist
, src
->cls
.indata
,
416 src
->cls
.indata_len
);
418 req
->r_data_out
.type
= CEPH_OSD_DATA_TYPE_PAGELIST
;
419 req
->r_data_out
.pagelist
= pagelist
;
420 out_data_len
= pagelist
->length
;
422 case CEPH_OSD_OP_STARTSYNC
:
424 case CEPH_OSD_OP_NOTIFY_ACK
:
425 case CEPH_OSD_OP_WATCH
:
426 dst
->watch
.cookie
= cpu_to_le64(src
->watch
.cookie
);
427 dst
->watch
.ver
= cpu_to_le64(src
->watch
.ver
);
428 dst
->watch
.flag
= src
->watch
.flag
;
431 pr_err("unsupported osd opcode %s\n",
432 ceph_osd_op_name(src
->op
));
437 dst
->op
= cpu_to_le16(src
->op
);
438 dst
->payload_len
= cpu_to_le32(src
->payload_len
);
444 * build new request AND message
447 void ceph_osdc_build_request(struct ceph_osd_request
*req
,
448 u64 off
, unsigned int num_ops
,
449 struct ceph_osd_req_op
*src_ops
,
450 struct ceph_snap_context
*snapc
, u64 snap_id
,
451 struct timespec
*mtime
)
453 struct ceph_msg
*msg
= req
->r_request
;
454 struct ceph_osd_req_op
*src_op
;
457 int flags
= req
->r_flags
;
461 req
->r_num_ops
= num_ops
;
462 req
->r_snapid
= snap_id
;
463 req
->r_snapc
= ceph_get_snap_context(snapc
);
466 msg
->hdr
.version
= cpu_to_le16(4);
468 p
= msg
->front
.iov_base
;
469 ceph_encode_32(&p
, 1); /* client_inc is always 1 */
470 req
->r_request_osdmap_epoch
= p
;
472 req
->r_request_flags
= p
;
474 if (req
->r_flags
& CEPH_OSD_FLAG_WRITE
)
475 ceph_encode_timespec(p
, mtime
);
476 p
+= sizeof(struct ceph_timespec
);
477 req
->r_request_reassert_version
= p
;
478 p
+= sizeof(struct ceph_eversion
); /* will get filled in */
481 ceph_encode_8(&p
, 4);
482 ceph_encode_8(&p
, 4);
483 ceph_encode_32(&p
, 8 + 4 + 4);
484 req
->r_request_pool
= p
;
486 ceph_encode_32(&p
, -1); /* preferred */
487 ceph_encode_32(&p
, 0); /* key len */
489 ceph_encode_8(&p
, 1);
490 req
->r_request_pgid
= p
;
492 ceph_encode_32(&p
, -1); /* preferred */
495 ceph_encode_32(&p
, req
->r_oid_len
);
496 memcpy(p
, req
->r_oid
, req
->r_oid_len
);
497 dout("oid '%.*s' len %d\n", req
->r_oid_len
, req
->r_oid
, req
->r_oid_len
);
500 /* ops--can imply data */
501 ceph_encode_16(&p
, num_ops
);
503 req
->r_request_ops
= p
;
505 for (i
= 0; i
< num_ops
; i
++, src_op
++) {
506 data_len
+= osd_req_encode_op(req
, p
, src_op
);
507 p
+= sizeof(struct ceph_osd_op
);
511 ceph_encode_64(&p
, req
->r_snapid
);
512 ceph_encode_64(&p
, req
->r_snapc
? req
->r_snapc
->seq
: 0);
513 ceph_encode_32(&p
, req
->r_snapc
? req
->r_snapc
->num_snaps
: 0);
515 for (i
= 0; i
< snapc
->num_snaps
; i
++) {
516 ceph_encode_64(&p
, req
->r_snapc
->snaps
[i
]);
520 req
->r_request_attempts
= p
;
524 if (flags
& CEPH_OSD_FLAG_WRITE
) {
528 * The header "data_off" is a hint to the receiver
529 * allowing it to align received data into its
530 * buffers such that there's no need to re-copy
531 * it before writing it to disk (direct I/O).
533 data_off
= (u16
) (off
& 0xffff);
534 req
->r_request
->hdr
.data_off
= cpu_to_le16(data_off
);
536 req
->r_request
->hdr
.data_len
= cpu_to_le32(data_len
);
538 BUG_ON(p
> msg
->front
.iov_base
+ msg
->front
.iov_len
);
539 msg_size
= p
- msg
->front
.iov_base
;
540 msg
->front
.iov_len
= msg_size
;
541 msg
->hdr
.front_len
= cpu_to_le32(msg_size
);
543 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size
,
547 EXPORT_SYMBOL(ceph_osdc_build_request
);
550 * build new request AND message, calculate layout, and adjust file
553 * if the file was recently truncated, we include information about its
554 * old and new size so that the object can be updated appropriately. (we
555 * avoid synchronously deleting truncated objects because it's slow.)
557 * if @do_sync, include a 'startsync' command so that the osd will flush
560 struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*osdc
,
561 struct ceph_file_layout
*layout
,
562 struct ceph_vino vino
,
564 int opcode
, int flags
,
565 struct ceph_snap_context
*snapc
,
569 struct timespec
*mtime
,
572 struct ceph_osd_req_op ops
[2];
573 struct ceph_osd_request
*req
;
574 unsigned int num_op
= 1;
578 memset(&ops
, 0, sizeof ops
);
581 ops
[0].extent
.truncate_seq
= truncate_seq
;
582 ops
[0].extent
.truncate_size
= truncate_size
;
585 ops
[1].op
= CEPH_OSD_OP_STARTSYNC
;
589 req
= ceph_osdc_alloc_request(osdc
, snapc
, num_op
, use_mempool
,
592 return ERR_PTR(-ENOMEM
);
593 req
->r_flags
= flags
;
595 /* calculate max write size */
596 r
= calc_layout(layout
, off
, plen
, ops
, &bno
);
598 ceph_osdc_put_request(req
);
601 req
->r_file_layout
= *layout
; /* keep a copy */
603 snprintf(req
->r_oid
, sizeof(req
->r_oid
), "%llx.%08llx", vino
.ino
, bno
);
604 req
->r_oid_len
= strlen(req
->r_oid
);
606 ceph_osdc_build_request(req
, off
, num_op
, ops
,
607 snapc
, vino
.snap
, mtime
);
611 EXPORT_SYMBOL(ceph_osdc_new_request
);
614 * We keep osd requests in an rbtree, sorted by ->r_tid.
616 static void __insert_request(struct ceph_osd_client
*osdc
,
617 struct ceph_osd_request
*new)
619 struct rb_node
**p
= &osdc
->requests
.rb_node
;
620 struct rb_node
*parent
= NULL
;
621 struct ceph_osd_request
*req
= NULL
;
625 req
= rb_entry(parent
, struct ceph_osd_request
, r_node
);
626 if (new->r_tid
< req
->r_tid
)
628 else if (new->r_tid
> req
->r_tid
)
634 rb_link_node(&new->r_node
, parent
, p
);
635 rb_insert_color(&new->r_node
, &osdc
->requests
);
638 static struct ceph_osd_request
*__lookup_request(struct ceph_osd_client
*osdc
,
641 struct ceph_osd_request
*req
;
642 struct rb_node
*n
= osdc
->requests
.rb_node
;
645 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
646 if (tid
< req
->r_tid
)
648 else if (tid
> req
->r_tid
)
656 static struct ceph_osd_request
*
657 __lookup_request_ge(struct ceph_osd_client
*osdc
,
660 struct ceph_osd_request
*req
;
661 struct rb_node
*n
= osdc
->requests
.rb_node
;
664 req
= rb_entry(n
, struct ceph_osd_request
, r_node
);
665 if (tid
< req
->r_tid
) {
669 } else if (tid
> req
->r_tid
) {
679 * Resubmit requests pending on the given osd.
681 static void __kick_osd_requests(struct ceph_osd_client
*osdc
,
682 struct ceph_osd
*osd
)
684 struct ceph_osd_request
*req
, *nreq
;
688 dout("__kick_osd_requests osd%d\n", osd
->o_osd
);
689 err
= __reset_osd(osdc
, osd
);
693 * Build up a list of requests to resend by traversing the
694 * osd's list of requests. Requests for a given object are
695 * sent in tid order, and that is also the order they're
696 * kept on this list. Therefore all requests that are in
697 * flight will be found first, followed by all requests that
698 * have not yet been sent. And to resend requests while
699 * preserving this order we will want to put any sent
700 * requests back on the front of the osd client's unsent
703 * So we build a separate ordered list of already-sent
704 * requests for the affected osd and splice it onto the
705 * front of the osd client's unsent list. Once we've seen a
706 * request that has not yet been sent we're done. Those
707 * requests are already sitting right where they belong.
709 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
) {
712 list_move_tail(&req
->r_req_lru_item
, &resend
);
713 dout("requeueing %p tid %llu osd%d\n", req
, req
->r_tid
,
716 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
718 list_splice(&resend
, &osdc
->req_unsent
);
721 * Linger requests are re-registered before sending, which
722 * sets up a new tid for each. We add them to the unsent
723 * list at the end to keep things in tid order.
725 list_for_each_entry_safe(req
, nreq
, &osd
->o_linger_requests
,
728 * reregister request prior to unregistering linger so
729 * that r_osd is preserved.
731 BUG_ON(!list_empty(&req
->r_req_lru_item
));
732 __register_request(osdc
, req
);
733 list_add_tail(&req
->r_req_lru_item
, &osdc
->req_unsent
);
734 list_add_tail(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
735 __unregister_linger_request(osdc
, req
);
736 dout("requeued lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
742 * If the osd connection drops, we need to resubmit all requests.
744 static void osd_reset(struct ceph_connection
*con
)
746 struct ceph_osd
*osd
= con
->private;
747 struct ceph_osd_client
*osdc
;
751 dout("osd_reset osd%d\n", osd
->o_osd
);
753 down_read(&osdc
->map_sem
);
754 mutex_lock(&osdc
->request_mutex
);
755 __kick_osd_requests(osdc
, osd
);
757 mutex_unlock(&osdc
->request_mutex
);
758 up_read(&osdc
->map_sem
);
762 * Track open sessions with osds.
764 static struct ceph_osd
*create_osd(struct ceph_osd_client
*osdc
, int onum
)
766 struct ceph_osd
*osd
;
768 osd
= kzalloc(sizeof(*osd
), GFP_NOFS
);
772 atomic_set(&osd
->o_ref
, 1);
775 RB_CLEAR_NODE(&osd
->o_node
);
776 INIT_LIST_HEAD(&osd
->o_requests
);
777 INIT_LIST_HEAD(&osd
->o_linger_requests
);
778 INIT_LIST_HEAD(&osd
->o_osd_lru
);
779 osd
->o_incarnation
= 1;
781 ceph_con_init(&osd
->o_con
, osd
, &osd_con_ops
, &osdc
->client
->msgr
);
783 INIT_LIST_HEAD(&osd
->o_keepalive_item
);
787 static struct ceph_osd
*get_osd(struct ceph_osd
*osd
)
789 if (atomic_inc_not_zero(&osd
->o_ref
)) {
790 dout("get_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
)-1,
791 atomic_read(&osd
->o_ref
));
794 dout("get_osd %p FAIL\n", osd
);
799 static void put_osd(struct ceph_osd
*osd
)
801 dout("put_osd %p %d -> %d\n", osd
, atomic_read(&osd
->o_ref
),
802 atomic_read(&osd
->o_ref
) - 1);
803 if (atomic_dec_and_test(&osd
->o_ref
) && osd
->o_auth
.authorizer
) {
804 struct ceph_auth_client
*ac
= osd
->o_osdc
->client
->monc
.auth
;
806 ceph_auth_destroy_authorizer(ac
, osd
->o_auth
.authorizer
);
812 * remove an osd from our map
814 static void __remove_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
816 dout("__remove_osd %p\n", osd
);
817 BUG_ON(!list_empty(&osd
->o_requests
));
818 rb_erase(&osd
->o_node
, &osdc
->osds
);
819 list_del_init(&osd
->o_osd_lru
);
820 ceph_con_close(&osd
->o_con
);
824 static void remove_all_osds(struct ceph_osd_client
*osdc
)
826 dout("%s %p\n", __func__
, osdc
);
827 mutex_lock(&osdc
->request_mutex
);
828 while (!RB_EMPTY_ROOT(&osdc
->osds
)) {
829 struct ceph_osd
*osd
= rb_entry(rb_first(&osdc
->osds
),
830 struct ceph_osd
, o_node
);
831 __remove_osd(osdc
, osd
);
833 mutex_unlock(&osdc
->request_mutex
);
836 static void __move_osd_to_lru(struct ceph_osd_client
*osdc
,
837 struct ceph_osd
*osd
)
839 dout("__move_osd_to_lru %p\n", osd
);
840 BUG_ON(!list_empty(&osd
->o_osd_lru
));
841 list_add_tail(&osd
->o_osd_lru
, &osdc
->osd_lru
);
842 osd
->lru_ttl
= jiffies
+ osdc
->client
->options
->osd_idle_ttl
* HZ
;
845 static void __remove_osd_from_lru(struct ceph_osd
*osd
)
847 dout("__remove_osd_from_lru %p\n", osd
);
848 if (!list_empty(&osd
->o_osd_lru
))
849 list_del_init(&osd
->o_osd_lru
);
852 static void remove_old_osds(struct ceph_osd_client
*osdc
)
854 struct ceph_osd
*osd
, *nosd
;
856 dout("__remove_old_osds %p\n", osdc
);
857 mutex_lock(&osdc
->request_mutex
);
858 list_for_each_entry_safe(osd
, nosd
, &osdc
->osd_lru
, o_osd_lru
) {
859 if (time_before(jiffies
, osd
->lru_ttl
))
861 __remove_osd(osdc
, osd
);
863 mutex_unlock(&osdc
->request_mutex
);
869 static int __reset_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*osd
)
871 struct ceph_entity_addr
*peer_addr
;
873 dout("__reset_osd %p osd%d\n", osd
, osd
->o_osd
);
874 if (list_empty(&osd
->o_requests
) &&
875 list_empty(&osd
->o_linger_requests
)) {
876 __remove_osd(osdc
, osd
);
881 peer_addr
= &osdc
->osdmap
->osd_addr
[osd
->o_osd
];
882 if (!memcmp(peer_addr
, &osd
->o_con
.peer_addr
, sizeof (*peer_addr
)) &&
883 !ceph_con_opened(&osd
->o_con
)) {
884 struct ceph_osd_request
*req
;
886 dout(" osd addr hasn't changed and connection never opened,"
887 " letting msgr retry");
888 /* touch each r_stamp for handle_timeout()'s benfit */
889 list_for_each_entry(req
, &osd
->o_requests
, r_osd_item
)
890 req
->r_stamp
= jiffies
;
895 ceph_con_close(&osd
->o_con
);
896 ceph_con_open(&osd
->o_con
, CEPH_ENTITY_TYPE_OSD
, osd
->o_osd
, peer_addr
);
897 osd
->o_incarnation
++;
902 static void __insert_osd(struct ceph_osd_client
*osdc
, struct ceph_osd
*new)
904 struct rb_node
**p
= &osdc
->osds
.rb_node
;
905 struct rb_node
*parent
= NULL
;
906 struct ceph_osd
*osd
= NULL
;
908 dout("__insert_osd %p osd%d\n", new, new->o_osd
);
911 osd
= rb_entry(parent
, struct ceph_osd
, o_node
);
912 if (new->o_osd
< osd
->o_osd
)
914 else if (new->o_osd
> osd
->o_osd
)
920 rb_link_node(&new->o_node
, parent
, p
);
921 rb_insert_color(&new->o_node
, &osdc
->osds
);
924 static struct ceph_osd
*__lookup_osd(struct ceph_osd_client
*osdc
, int o
)
926 struct ceph_osd
*osd
;
927 struct rb_node
*n
= osdc
->osds
.rb_node
;
930 osd
= rb_entry(n
, struct ceph_osd
, o_node
);
933 else if (o
> osd
->o_osd
)
941 static void __schedule_osd_timeout(struct ceph_osd_client
*osdc
)
943 schedule_delayed_work(&osdc
->timeout_work
,
944 osdc
->client
->options
->osd_keepalive_timeout
* HZ
);
947 static void __cancel_osd_timeout(struct ceph_osd_client
*osdc
)
949 cancel_delayed_work(&osdc
->timeout_work
);
953 * Register request, assign tid. If this is the first request, set up
956 static void __register_request(struct ceph_osd_client
*osdc
,
957 struct ceph_osd_request
*req
)
959 req
->r_tid
= ++osdc
->last_tid
;
960 req
->r_request
->hdr
.tid
= cpu_to_le64(req
->r_tid
);
961 dout("__register_request %p tid %lld\n", req
, req
->r_tid
);
962 __insert_request(osdc
, req
);
963 ceph_osdc_get_request(req
);
964 osdc
->num_requests
++;
965 if (osdc
->num_requests
== 1) {
966 dout(" first request, scheduling timeout\n");
967 __schedule_osd_timeout(osdc
);
972 * called under osdc->request_mutex
974 static void __unregister_request(struct ceph_osd_client
*osdc
,
975 struct ceph_osd_request
*req
)
977 if (RB_EMPTY_NODE(&req
->r_node
)) {
978 dout("__unregister_request %p tid %lld not registered\n",
983 dout("__unregister_request %p tid %lld\n", req
, req
->r_tid
);
984 rb_erase(&req
->r_node
, &osdc
->requests
);
985 osdc
->num_requests
--;
988 /* make sure the original request isn't in flight. */
989 ceph_msg_revoke(req
->r_request
);
991 list_del_init(&req
->r_osd_item
);
992 if (list_empty(&req
->r_osd
->o_requests
) &&
993 list_empty(&req
->r_osd
->o_linger_requests
)) {
994 dout("moving osd to %p lru\n", req
->r_osd
);
995 __move_osd_to_lru(osdc
, req
->r_osd
);
997 if (list_empty(&req
->r_linger_item
))
1001 list_del_init(&req
->r_req_lru_item
);
1002 ceph_osdc_put_request(req
);
1004 if (osdc
->num_requests
== 0) {
1005 dout(" no requests, canceling timeout\n");
1006 __cancel_osd_timeout(osdc
);
1011 * Cancel a previously queued request message
1013 static void __cancel_request(struct ceph_osd_request
*req
)
1015 if (req
->r_sent
&& req
->r_osd
) {
1016 ceph_msg_revoke(req
->r_request
);
1021 static void __register_linger_request(struct ceph_osd_client
*osdc
,
1022 struct ceph_osd_request
*req
)
1024 dout("__register_linger_request %p\n", req
);
1025 list_add_tail(&req
->r_linger_item
, &osdc
->req_linger
);
1027 list_add_tail(&req
->r_linger_osd
,
1028 &req
->r_osd
->o_linger_requests
);
1031 static void __unregister_linger_request(struct ceph_osd_client
*osdc
,
1032 struct ceph_osd_request
*req
)
1034 dout("__unregister_linger_request %p\n", req
);
1035 list_del_init(&req
->r_linger_item
);
1037 list_del_init(&req
->r_linger_osd
);
1039 if (list_empty(&req
->r_osd
->o_requests
) &&
1040 list_empty(&req
->r_osd
->o_linger_requests
)) {
1041 dout("moving osd to %p lru\n", req
->r_osd
);
1042 __move_osd_to_lru(osdc
, req
->r_osd
);
1044 if (list_empty(&req
->r_osd_item
))
1049 void ceph_osdc_unregister_linger_request(struct ceph_osd_client
*osdc
,
1050 struct ceph_osd_request
*req
)
1052 mutex_lock(&osdc
->request_mutex
);
1053 if (req
->r_linger
) {
1054 __unregister_linger_request(osdc
, req
);
1055 ceph_osdc_put_request(req
);
1057 mutex_unlock(&osdc
->request_mutex
);
1059 EXPORT_SYMBOL(ceph_osdc_unregister_linger_request
);
1061 void ceph_osdc_set_request_linger(struct ceph_osd_client
*osdc
,
1062 struct ceph_osd_request
*req
)
1064 if (!req
->r_linger
) {
1065 dout("set_request_linger %p\n", req
);
1068 * caller is now responsible for calling
1069 * unregister_linger_request
1071 ceph_osdc_get_request(req
);
1074 EXPORT_SYMBOL(ceph_osdc_set_request_linger
);
1077 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1078 * (as needed), and set the request r_osd appropriately. If there is
1079 * no up osd, set r_osd to NULL. Move the request to the appropriate list
1080 * (unsent, homeless) or leave on in-flight lru.
1082 * Return 0 if unchanged, 1 if changed, or negative on error.
1084 * Caller should hold map_sem for read and request_mutex.
1086 static int __map_request(struct ceph_osd_client
*osdc
,
1087 struct ceph_osd_request
*req
, int force_resend
)
1089 struct ceph_pg pgid
;
1090 int acting
[CEPH_PG_MAX_SIZE
];
1091 int o
= -1, num
= 0;
1094 dout("map_request %p tid %lld\n", req
, req
->r_tid
);
1095 err
= ceph_calc_ceph_pg(&pgid
, req
->r_oid
, osdc
->osdmap
,
1096 ceph_file_layout_pg_pool(req
->r_file_layout
));
1098 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1103 err
= ceph_calc_pg_acting(osdc
->osdmap
, pgid
, acting
);
1109 if ((!force_resend
&&
1110 req
->r_osd
&& req
->r_osd
->o_osd
== o
&&
1111 req
->r_sent
>= req
->r_osd
->o_incarnation
&&
1112 req
->r_num_pg_osds
== num
&&
1113 memcmp(req
->r_pg_osds
, acting
, sizeof(acting
[0])*num
) == 0) ||
1114 (req
->r_osd
== NULL
&& o
== -1))
1115 return 0; /* no change */
1117 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1118 req
->r_tid
, pgid
.pool
, pgid
.seed
, o
,
1119 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1121 /* record full pg acting set */
1122 memcpy(req
->r_pg_osds
, acting
, sizeof(acting
[0]) * num
);
1123 req
->r_num_pg_osds
= num
;
1126 __cancel_request(req
);
1127 list_del_init(&req
->r_osd_item
);
1131 req
->r_osd
= __lookup_osd(osdc
, o
);
1132 if (!req
->r_osd
&& o
>= 0) {
1134 req
->r_osd
= create_osd(osdc
, o
);
1136 list_move(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1140 dout("map_request osd %p is osd%d\n", req
->r_osd
, o
);
1141 __insert_osd(osdc
, req
->r_osd
);
1143 ceph_con_open(&req
->r_osd
->o_con
,
1144 CEPH_ENTITY_TYPE_OSD
, o
,
1145 &osdc
->osdmap
->osd_addr
[o
]);
1149 __remove_osd_from_lru(req
->r_osd
);
1150 list_add_tail(&req
->r_osd_item
, &req
->r_osd
->o_requests
);
1151 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_unsent
);
1153 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_notarget
);
1155 err
= 1; /* osd or pg changed */
1162 * caller should hold map_sem (for read) and request_mutex
1164 static void __send_request(struct ceph_osd_client
*osdc
,
1165 struct ceph_osd_request
*req
)
1169 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1170 req
, req
->r_tid
, req
->r_osd
->o_osd
, req
->r_flags
,
1171 (unsigned long long)req
->r_pgid
.pool
, req
->r_pgid
.seed
);
1173 /* fill in message content that changes each time we send it */
1174 put_unaligned_le32(osdc
->osdmap
->epoch
, req
->r_request_osdmap_epoch
);
1175 put_unaligned_le32(req
->r_flags
, req
->r_request_flags
);
1176 put_unaligned_le64(req
->r_pgid
.pool
, req
->r_request_pool
);
1177 p
= req
->r_request_pgid
;
1178 ceph_encode_64(&p
, req
->r_pgid
.pool
);
1179 ceph_encode_32(&p
, req
->r_pgid
.seed
);
1180 put_unaligned_le64(1, req
->r_request_attempts
); /* FIXME */
1181 memcpy(req
->r_request_reassert_version
, &req
->r_reassert_version
,
1182 sizeof(req
->r_reassert_version
));
1184 req
->r_stamp
= jiffies
;
1185 list_move_tail(&req
->r_req_lru_item
, &osdc
->req_lru
);
1187 ceph_msg_get(req
->r_request
); /* send consumes a ref */
1188 ceph_con_send(&req
->r_osd
->o_con
, req
->r_request
);
1189 req
->r_sent
= req
->r_osd
->o_incarnation
;
1193 * Send any requests in the queue (req_unsent).
1195 static void __send_queued(struct ceph_osd_client
*osdc
)
1197 struct ceph_osd_request
*req
, *tmp
;
1199 dout("__send_queued\n");
1200 list_for_each_entry_safe(req
, tmp
, &osdc
->req_unsent
, r_req_lru_item
)
1201 __send_request(osdc
, req
);
1205 * Timeout callback, called every N seconds when 1 or more osd
1206 * requests has been active for more than N seconds. When this
1207 * happens, we ping all OSDs with requests who have timed out to
1208 * ensure any communications channel reset is detected. Reset the
1209 * request timeouts another N seconds in the future as we go.
1210 * Reschedule the timeout event another N seconds in future (unless
1211 * there are no open requests).
1213 static void handle_timeout(struct work_struct
*work
)
1215 struct ceph_osd_client
*osdc
=
1216 container_of(work
, struct ceph_osd_client
, timeout_work
.work
);
1217 struct ceph_osd_request
*req
;
1218 struct ceph_osd
*osd
;
1219 unsigned long keepalive
=
1220 osdc
->client
->options
->osd_keepalive_timeout
* HZ
;
1221 struct list_head slow_osds
;
1223 down_read(&osdc
->map_sem
);
1225 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1227 mutex_lock(&osdc
->request_mutex
);
1230 * ping osds that are a bit slow. this ensures that if there
1231 * is a break in the TCP connection we will notice, and reopen
1232 * a connection with that osd (from the fault callback).
1234 INIT_LIST_HEAD(&slow_osds
);
1235 list_for_each_entry(req
, &osdc
->req_lru
, r_req_lru_item
) {
1236 if (time_before(jiffies
, req
->r_stamp
+ keepalive
))
1241 dout(" tid %llu is slow, will send keepalive on osd%d\n",
1242 req
->r_tid
, osd
->o_osd
);
1243 list_move_tail(&osd
->o_keepalive_item
, &slow_osds
);
1245 while (!list_empty(&slow_osds
)) {
1246 osd
= list_entry(slow_osds
.next
, struct ceph_osd
,
1248 list_del_init(&osd
->o_keepalive_item
);
1249 ceph_con_keepalive(&osd
->o_con
);
1252 __schedule_osd_timeout(osdc
);
1253 __send_queued(osdc
);
1254 mutex_unlock(&osdc
->request_mutex
);
1255 up_read(&osdc
->map_sem
);
1258 static void handle_osds_timeout(struct work_struct
*work
)
1260 struct ceph_osd_client
*osdc
=
1261 container_of(work
, struct ceph_osd_client
,
1262 osds_timeout_work
.work
);
1263 unsigned long delay
=
1264 osdc
->client
->options
->osd_idle_ttl
* HZ
>> 2;
1266 dout("osds timeout\n");
1267 down_read(&osdc
->map_sem
);
1268 remove_old_osds(osdc
);
1269 up_read(&osdc
->map_sem
);
1271 schedule_delayed_work(&osdc
->osds_timeout_work
,
1272 round_jiffies_relative(delay
));
1275 static void complete_request(struct ceph_osd_request
*req
)
1277 if (req
->r_safe_callback
)
1278 req
->r_safe_callback(req
, NULL
);
1279 complete_all(&req
->r_safe_completion
); /* fsync waiter */
1282 static int __decode_pgid(void **p
, void *end
, struct ceph_pg
*pgid
)
1286 ceph_decode_need(p
, end
, 1 + 8 + 4 + 4, bad
);
1287 v
= ceph_decode_8(p
);
1289 pr_warning("do not understand pg encoding %d > 1", v
);
1292 pgid
->pool
= ceph_decode_64(p
);
1293 pgid
->seed
= ceph_decode_32(p
);
1298 pr_warning("incomplete pg encoding");
1303 * handle osd op reply. either call the callback if it is specified,
1304 * or do the completion to wake up the waiting thread.
1306 static void handle_reply(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
,
1307 struct ceph_connection
*con
)
1310 struct ceph_osd_request
*req
;
1313 int numops
, payload_len
, flags
;
1319 u64 reassert_version
;
1321 int already_completed
;
1324 tid
= le64_to_cpu(msg
->hdr
.tid
);
1325 dout("handle_reply %p tid %llu\n", msg
, tid
);
1327 p
= msg
->front
.iov_base
;
1328 end
= p
+ msg
->front
.iov_len
;
1330 ceph_decode_need(&p
, end
, 4, bad
);
1331 object_len
= ceph_decode_32(&p
);
1332 ceph_decode_need(&p
, end
, object_len
, bad
);
1335 err
= __decode_pgid(&p
, end
, &pg
);
1339 ceph_decode_need(&p
, end
, 8 + 4 + 4 + 8 + 4, bad
);
1340 flags
= ceph_decode_64(&p
);
1341 result
= ceph_decode_32(&p
);
1342 reassert_epoch
= ceph_decode_32(&p
);
1343 reassert_version
= ceph_decode_64(&p
);
1344 osdmap_epoch
= ceph_decode_32(&p
);
1347 mutex_lock(&osdc
->request_mutex
);
1348 req
= __lookup_request(osdc
, tid
);
1350 dout("handle_reply tid %llu dne\n", tid
);
1351 mutex_unlock(&osdc
->request_mutex
);
1354 ceph_osdc_get_request(req
);
1356 dout("handle_reply %p tid %llu req %p result %d\n", msg
, tid
,
1359 ceph_decode_need(&p
, end
, 4, bad
);
1360 numops
= ceph_decode_32(&p
);
1361 if (numops
> CEPH_OSD_MAX_OP
)
1363 if (numops
!= req
->r_num_ops
)
1366 ceph_decode_need(&p
, end
, numops
* sizeof(struct ceph_osd_op
), bad
);
1367 for (i
= 0; i
< numops
; i
++) {
1368 struct ceph_osd_op
*op
= p
;
1371 len
= le32_to_cpu(op
->payload_len
);
1372 req
->r_reply_op_len
[i
] = len
;
1373 dout(" op %d has %d bytes\n", i
, len
);
1377 if (payload_len
!= le32_to_cpu(msg
->hdr
.data_len
)) {
1378 pr_warning("sum of op payload lens %d != data_len %d",
1379 payload_len
, le32_to_cpu(msg
->hdr
.data_len
));
1383 ceph_decode_need(&p
, end
, 4 + numops
* 4, bad
);
1384 retry_attempt
= ceph_decode_32(&p
);
1385 for (i
= 0; i
< numops
; i
++)
1386 req
->r_reply_op_result
[i
] = ceph_decode_32(&p
);
1389 * if this connection filled our message, drop our reference now, to
1390 * avoid a (safe but slower) revoke later.
1392 if (req
->r_con_filling_msg
== con
&& req
->r_reply
== msg
) {
1393 dout(" dropping con_filling_msg ref %p\n", con
);
1394 req
->r_con_filling_msg
= NULL
;
1398 if (!req
->r_got_reply
) {
1401 req
->r_result
= result
;
1402 bytes
= le32_to_cpu(msg
->hdr
.data_len
);
1403 dout("handle_reply result %d bytes %d\n", req
->r_result
,
1405 if (req
->r_result
== 0)
1406 req
->r_result
= bytes
;
1408 /* in case this is a write and we need to replay, */
1409 req
->r_reassert_version
.epoch
= cpu_to_le32(reassert_epoch
);
1410 req
->r_reassert_version
.version
= cpu_to_le64(reassert_version
);
1412 req
->r_got_reply
= 1;
1413 } else if ((flags
& CEPH_OSD_FLAG_ONDISK
) == 0) {
1414 dout("handle_reply tid %llu dup ack\n", tid
);
1415 mutex_unlock(&osdc
->request_mutex
);
1419 dout("handle_reply tid %llu flags %d\n", tid
, flags
);
1421 if (req
->r_linger
&& (flags
& CEPH_OSD_FLAG_ONDISK
))
1422 __register_linger_request(osdc
, req
);
1424 /* either this is a read, or we got the safe response */
1426 (flags
& CEPH_OSD_FLAG_ONDISK
) ||
1427 ((flags
& CEPH_OSD_FLAG_WRITE
) == 0))
1428 __unregister_request(osdc
, req
);
1430 already_completed
= req
->r_completed
;
1431 req
->r_completed
= 1;
1432 mutex_unlock(&osdc
->request_mutex
);
1433 if (already_completed
)
1436 if (req
->r_callback
)
1437 req
->r_callback(req
, msg
);
1439 complete_all(&req
->r_completion
);
1441 if (flags
& CEPH_OSD_FLAG_ONDISK
)
1442 complete_request(req
);
1445 dout("req=%p req->r_linger=%d\n", req
, req
->r_linger
);
1446 ceph_osdc_put_request(req
);
1450 ceph_osdc_put_request(req
);
1452 pr_err("corrupt osd_op_reply got %d %d\n",
1453 (int)msg
->front
.iov_len
, le32_to_cpu(msg
->hdr
.front_len
));
1457 static void reset_changed_osds(struct ceph_osd_client
*osdc
)
1459 struct rb_node
*p
, *n
;
1461 for (p
= rb_first(&osdc
->osds
); p
; p
= n
) {
1462 struct ceph_osd
*osd
= rb_entry(p
, struct ceph_osd
, o_node
);
1465 if (!ceph_osd_is_up(osdc
->osdmap
, osd
->o_osd
) ||
1466 memcmp(&osd
->o_con
.peer_addr
,
1467 ceph_osd_addr(osdc
->osdmap
,
1469 sizeof(struct ceph_entity_addr
)) != 0)
1470 __reset_osd(osdc
, osd
);
1475 * Requeue requests whose mapping to an OSD has changed. If requests map to
1476 * no osd, request a new map.
1478 * Caller should hold map_sem for read.
1480 static void kick_requests(struct ceph_osd_client
*osdc
, int force_resend
)
1482 struct ceph_osd_request
*req
, *nreq
;
1487 dout("kick_requests %s\n", force_resend
? " (force resend)" : "");
1488 mutex_lock(&osdc
->request_mutex
);
1489 for (p
= rb_first(&osdc
->requests
); p
; ) {
1490 req
= rb_entry(p
, struct ceph_osd_request
, r_node
);
1494 * For linger requests that have not yet been
1495 * registered, move them to the linger list; they'll
1496 * be sent to the osd in the loop below. Unregister
1497 * the request before re-registering it as a linger
1498 * request to ensure the __map_request() below
1499 * will decide it needs to be sent.
1501 if (req
->r_linger
&& list_empty(&req
->r_linger_item
)) {
1502 dout("%p tid %llu restart on osd%d\n",
1504 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1505 __unregister_request(osdc
, req
);
1506 __register_linger_request(osdc
, req
);
1510 err
= __map_request(osdc
, req
, force_resend
);
1512 continue; /* error */
1513 if (req
->r_osd
== NULL
) {
1514 dout("%p tid %llu maps to no osd\n", req
, req
->r_tid
);
1515 needmap
++; /* request a newer map */
1516 } else if (err
> 0) {
1517 if (!req
->r_linger
) {
1518 dout("%p tid %llu requeued on osd%d\n", req
,
1520 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1521 req
->r_flags
|= CEPH_OSD_FLAG_RETRY
;
1526 list_for_each_entry_safe(req
, nreq
, &osdc
->req_linger
,
1528 dout("linger req=%p req->r_osd=%p\n", req
, req
->r_osd
);
1530 err
= __map_request(osdc
, req
, force_resend
);
1531 dout("__map_request returned %d\n", err
);
1533 continue; /* no change and no osd was specified */
1535 continue; /* hrm! */
1536 if (req
->r_osd
== NULL
) {
1537 dout("tid %llu maps to no valid osd\n", req
->r_tid
);
1538 needmap
++; /* request a newer map */
1542 dout("kicking lingering %p tid %llu osd%d\n", req
, req
->r_tid
,
1543 req
->r_osd
? req
->r_osd
->o_osd
: -1);
1544 __register_request(osdc
, req
);
1545 __unregister_linger_request(osdc
, req
);
1547 mutex_unlock(&osdc
->request_mutex
);
1550 dout("%d requests for down osds, need new map\n", needmap
);
1551 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1553 reset_changed_osds(osdc
);
1558 * Process updated osd map.
1560 * The message contains any number of incremental and full maps, normally
1561 * indicating some sort of topology change in the cluster. Kick requests
1562 * off to different OSDs as needed.
1564 void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
, struct ceph_msg
*msg
)
1566 void *p
, *end
, *next
;
1567 u32 nr_maps
, maplen
;
1569 struct ceph_osdmap
*newmap
= NULL
, *oldmap
;
1571 struct ceph_fsid fsid
;
1573 dout("handle_map have %u\n", osdc
->osdmap
? osdc
->osdmap
->epoch
: 0);
1574 p
= msg
->front
.iov_base
;
1575 end
= p
+ msg
->front
.iov_len
;
1578 ceph_decode_need(&p
, end
, sizeof(fsid
), bad
);
1579 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
1580 if (ceph_check_fsid(osdc
->client
, &fsid
) < 0)
1583 down_write(&osdc
->map_sem
);
1585 /* incremental maps */
1586 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1587 dout(" %d inc maps\n", nr_maps
);
1588 while (nr_maps
> 0) {
1589 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1590 epoch
= ceph_decode_32(&p
);
1591 maplen
= ceph_decode_32(&p
);
1592 ceph_decode_need(&p
, end
, maplen
, bad
);
1594 if (osdc
->osdmap
&& osdc
->osdmap
->epoch
+1 == epoch
) {
1595 dout("applying incremental map %u len %d\n",
1597 newmap
= osdmap_apply_incremental(&p
, next
,
1599 &osdc
->client
->msgr
);
1600 if (IS_ERR(newmap
)) {
1601 err
= PTR_ERR(newmap
);
1605 if (newmap
!= osdc
->osdmap
) {
1606 ceph_osdmap_destroy(osdc
->osdmap
);
1607 osdc
->osdmap
= newmap
;
1609 kick_requests(osdc
, 0);
1611 dout("ignoring incremental map %u len %d\n",
1621 ceph_decode_32_safe(&p
, end
, nr_maps
, bad
);
1622 dout(" %d full maps\n", nr_maps
);
1624 ceph_decode_need(&p
, end
, 2*sizeof(u32
), bad
);
1625 epoch
= ceph_decode_32(&p
);
1626 maplen
= ceph_decode_32(&p
);
1627 ceph_decode_need(&p
, end
, maplen
, bad
);
1629 dout("skipping non-latest full map %u len %d\n",
1631 } else if (osdc
->osdmap
&& osdc
->osdmap
->epoch
>= epoch
) {
1632 dout("skipping full map %u len %d, "
1633 "older than our %u\n", epoch
, maplen
,
1634 osdc
->osdmap
->epoch
);
1636 int skipped_map
= 0;
1638 dout("taking full map %u len %d\n", epoch
, maplen
);
1639 newmap
= osdmap_decode(&p
, p
+maplen
);
1640 if (IS_ERR(newmap
)) {
1641 err
= PTR_ERR(newmap
);
1645 oldmap
= osdc
->osdmap
;
1646 osdc
->osdmap
= newmap
;
1648 if (oldmap
->epoch
+ 1 < newmap
->epoch
)
1650 ceph_osdmap_destroy(oldmap
);
1652 kick_requests(osdc
, skipped_map
);
1659 downgrade_write(&osdc
->map_sem
);
1660 ceph_monc_got_osdmap(&osdc
->client
->monc
, osdc
->osdmap
->epoch
);
1663 * subscribe to subsequent osdmap updates if full to ensure
1664 * we find out when we are no longer full and stop returning
1667 if (ceph_osdmap_flag(osdc
->osdmap
, CEPH_OSDMAP_FULL
))
1668 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1670 mutex_lock(&osdc
->request_mutex
);
1671 __send_queued(osdc
);
1672 mutex_unlock(&osdc
->request_mutex
);
1673 up_read(&osdc
->map_sem
);
1674 wake_up_all(&osdc
->client
->auth_wq
);
1678 pr_err("osdc handle_map corrupt msg\n");
1680 up_write(&osdc
->map_sem
);
1685 * watch/notify callback event infrastructure
1687 * These callbacks are used both for watch and notify operations.
1689 static void __release_event(struct kref
*kref
)
1691 struct ceph_osd_event
*event
=
1692 container_of(kref
, struct ceph_osd_event
, kref
);
1694 dout("__release_event %p\n", event
);
1698 static void get_event(struct ceph_osd_event
*event
)
1700 kref_get(&event
->kref
);
1703 void ceph_osdc_put_event(struct ceph_osd_event
*event
)
1705 kref_put(&event
->kref
, __release_event
);
1707 EXPORT_SYMBOL(ceph_osdc_put_event
);
1709 static void __insert_event(struct ceph_osd_client
*osdc
,
1710 struct ceph_osd_event
*new)
1712 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1713 struct rb_node
*parent
= NULL
;
1714 struct ceph_osd_event
*event
= NULL
;
1718 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1719 if (new->cookie
< event
->cookie
)
1721 else if (new->cookie
> event
->cookie
)
1722 p
= &(*p
)->rb_right
;
1727 rb_link_node(&new->node
, parent
, p
);
1728 rb_insert_color(&new->node
, &osdc
->event_tree
);
1731 static struct ceph_osd_event
*__find_event(struct ceph_osd_client
*osdc
,
1734 struct rb_node
**p
= &osdc
->event_tree
.rb_node
;
1735 struct rb_node
*parent
= NULL
;
1736 struct ceph_osd_event
*event
= NULL
;
1740 event
= rb_entry(parent
, struct ceph_osd_event
, node
);
1741 if (cookie
< event
->cookie
)
1743 else if (cookie
> event
->cookie
)
1744 p
= &(*p
)->rb_right
;
1751 static void __remove_event(struct ceph_osd_event
*event
)
1753 struct ceph_osd_client
*osdc
= event
->osdc
;
1755 if (!RB_EMPTY_NODE(&event
->node
)) {
1756 dout("__remove_event removed %p\n", event
);
1757 rb_erase(&event
->node
, &osdc
->event_tree
);
1758 ceph_osdc_put_event(event
);
1760 dout("__remove_event didn't remove %p\n", event
);
1764 int ceph_osdc_create_event(struct ceph_osd_client
*osdc
,
1765 void (*event_cb
)(u64
, u64
, u8
, void *),
1766 void *data
, struct ceph_osd_event
**pevent
)
1768 struct ceph_osd_event
*event
;
1770 event
= kmalloc(sizeof(*event
), GFP_NOIO
);
1774 dout("create_event %p\n", event
);
1775 event
->cb
= event_cb
;
1776 event
->one_shot
= 0;
1779 INIT_LIST_HEAD(&event
->osd_node
);
1780 RB_CLEAR_NODE(&event
->node
);
1781 kref_init(&event
->kref
); /* one ref for us */
1782 kref_get(&event
->kref
); /* one ref for the caller */
1784 spin_lock(&osdc
->event_lock
);
1785 event
->cookie
= ++osdc
->event_count
;
1786 __insert_event(osdc
, event
);
1787 spin_unlock(&osdc
->event_lock
);
1792 EXPORT_SYMBOL(ceph_osdc_create_event
);
1794 void ceph_osdc_cancel_event(struct ceph_osd_event
*event
)
1796 struct ceph_osd_client
*osdc
= event
->osdc
;
1798 dout("cancel_event %p\n", event
);
1799 spin_lock(&osdc
->event_lock
);
1800 __remove_event(event
);
1801 spin_unlock(&osdc
->event_lock
);
1802 ceph_osdc_put_event(event
); /* caller's */
1804 EXPORT_SYMBOL(ceph_osdc_cancel_event
);
1807 static void do_event_work(struct work_struct
*work
)
1809 struct ceph_osd_event_work
*event_work
=
1810 container_of(work
, struct ceph_osd_event_work
, work
);
1811 struct ceph_osd_event
*event
= event_work
->event
;
1812 u64 ver
= event_work
->ver
;
1813 u64 notify_id
= event_work
->notify_id
;
1814 u8 opcode
= event_work
->opcode
;
1816 dout("do_event_work completing %p\n", event
);
1817 event
->cb(ver
, notify_id
, opcode
, event
->data
);
1818 dout("do_event_work completed %p\n", event
);
1819 ceph_osdc_put_event(event
);
1825 * Process osd watch notifications
1827 static void handle_watch_notify(struct ceph_osd_client
*osdc
,
1828 struct ceph_msg
*msg
)
1832 u64 cookie
, ver
, notify_id
;
1834 struct ceph_osd_event
*event
;
1835 struct ceph_osd_event_work
*event_work
;
1837 p
= msg
->front
.iov_base
;
1838 end
= p
+ msg
->front
.iov_len
;
1840 ceph_decode_8_safe(&p
, end
, proto_ver
, bad
);
1841 ceph_decode_8_safe(&p
, end
, opcode
, bad
);
1842 ceph_decode_64_safe(&p
, end
, cookie
, bad
);
1843 ceph_decode_64_safe(&p
, end
, ver
, bad
);
1844 ceph_decode_64_safe(&p
, end
, notify_id
, bad
);
1846 spin_lock(&osdc
->event_lock
);
1847 event
= __find_event(osdc
, cookie
);
1849 BUG_ON(event
->one_shot
);
1852 spin_unlock(&osdc
->event_lock
);
1853 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1854 cookie
, ver
, event
);
1856 event_work
= kmalloc(sizeof(*event_work
), GFP_NOIO
);
1858 dout("ERROR: could not allocate event_work\n");
1861 INIT_WORK(&event_work
->work
, do_event_work
);
1862 event_work
->event
= event
;
1863 event_work
->ver
= ver
;
1864 event_work
->notify_id
= notify_id
;
1865 event_work
->opcode
= opcode
;
1866 if (!queue_work(osdc
->notify_wq
, &event_work
->work
)) {
1867 dout("WARNING: failed to queue notify event work\n");
1875 ceph_osdc_put_event(event
);
1879 pr_err("osdc handle_watch_notify corrupt msg\n");
1883 static void ceph_osdc_msg_data_set(struct ceph_msg
*msg
,
1884 struct ceph_osd_data
*osd_data
)
1886 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
) {
1887 BUG_ON(osd_data
->length
> (u64
) SIZE_MAX
);
1888 if (osd_data
->length
)
1889 ceph_msg_data_set_pages(msg
, osd_data
->pages
,
1890 osd_data
->length
, osd_data
->alignment
);
1891 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGELIST
) {
1892 BUG_ON(!osd_data
->pagelist
->length
);
1893 ceph_msg_data_set_pagelist(msg
, osd_data
->pagelist
);
1895 } else if (osd_data
->type
== CEPH_OSD_DATA_TYPE_BIO
) {
1896 ceph_msg_data_set_bio(msg
, osd_data
->bio
);
1899 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_NONE
);
1904 * Register request, send initial attempt.
1906 int ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
1907 struct ceph_osd_request
*req
,
1912 /* Set up response incoming data and request outgoing data fields */
1914 ceph_osdc_msg_data_set(req
->r_reply
, &req
->r_data_in
);
1915 ceph_osdc_msg_data_set(req
->r_request
, &req
->r_data_out
);
1917 down_read(&osdc
->map_sem
);
1918 mutex_lock(&osdc
->request_mutex
);
1919 __register_request(osdc
, req
);
1920 WARN_ON(req
->r_sent
);
1921 rc
= __map_request(osdc
, req
, 0);
1924 dout("osdc_start_request failed map, "
1925 " will retry %lld\n", req
->r_tid
);
1930 if (req
->r_osd
== NULL
) {
1931 dout("send_request %p no up osds in pg\n", req
);
1932 ceph_monc_request_next_osdmap(&osdc
->client
->monc
);
1934 __send_queued(osdc
);
1938 mutex_unlock(&osdc
->request_mutex
);
1939 up_read(&osdc
->map_sem
);
1942 EXPORT_SYMBOL(ceph_osdc_start_request
);
1945 * wait for a request to complete
1947 int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
1948 struct ceph_osd_request
*req
)
1952 rc
= wait_for_completion_interruptible(&req
->r_completion
);
1954 mutex_lock(&osdc
->request_mutex
);
1955 __cancel_request(req
);
1956 __unregister_request(osdc
, req
);
1957 mutex_unlock(&osdc
->request_mutex
);
1958 complete_request(req
);
1959 dout("wait_request tid %llu canceled/timed out\n", req
->r_tid
);
1963 dout("wait_request tid %llu result %d\n", req
->r_tid
, req
->r_result
);
1964 return req
->r_result
;
1966 EXPORT_SYMBOL(ceph_osdc_wait_request
);
1969 * sync - wait for all in-flight requests to flush. avoid starvation.
1971 void ceph_osdc_sync(struct ceph_osd_client
*osdc
)
1973 struct ceph_osd_request
*req
;
1974 u64 last_tid
, next_tid
= 0;
1976 mutex_lock(&osdc
->request_mutex
);
1977 last_tid
= osdc
->last_tid
;
1979 req
= __lookup_request_ge(osdc
, next_tid
);
1982 if (req
->r_tid
> last_tid
)
1985 next_tid
= req
->r_tid
+ 1;
1986 if ((req
->r_flags
& CEPH_OSD_FLAG_WRITE
) == 0)
1989 ceph_osdc_get_request(req
);
1990 mutex_unlock(&osdc
->request_mutex
);
1991 dout("sync waiting on tid %llu (last is %llu)\n",
1992 req
->r_tid
, last_tid
);
1993 wait_for_completion(&req
->r_safe_completion
);
1994 mutex_lock(&osdc
->request_mutex
);
1995 ceph_osdc_put_request(req
);
1997 mutex_unlock(&osdc
->request_mutex
);
1998 dout("sync done (thru tid %llu)\n", last_tid
);
2000 EXPORT_SYMBOL(ceph_osdc_sync
);
2005 int ceph_osdc_init(struct ceph_osd_client
*osdc
, struct ceph_client
*client
)
2010 osdc
->client
= client
;
2011 osdc
->osdmap
= NULL
;
2012 init_rwsem(&osdc
->map_sem
);
2013 init_completion(&osdc
->map_waiters
);
2014 osdc
->last_requested_map
= 0;
2015 mutex_init(&osdc
->request_mutex
);
2017 osdc
->osds
= RB_ROOT
;
2018 INIT_LIST_HEAD(&osdc
->osd_lru
);
2019 osdc
->requests
= RB_ROOT
;
2020 INIT_LIST_HEAD(&osdc
->req_lru
);
2021 INIT_LIST_HEAD(&osdc
->req_unsent
);
2022 INIT_LIST_HEAD(&osdc
->req_notarget
);
2023 INIT_LIST_HEAD(&osdc
->req_linger
);
2024 osdc
->num_requests
= 0;
2025 INIT_DELAYED_WORK(&osdc
->timeout_work
, handle_timeout
);
2026 INIT_DELAYED_WORK(&osdc
->osds_timeout_work
, handle_osds_timeout
);
2027 spin_lock_init(&osdc
->event_lock
);
2028 osdc
->event_tree
= RB_ROOT
;
2029 osdc
->event_count
= 0;
2031 schedule_delayed_work(&osdc
->osds_timeout_work
,
2032 round_jiffies_relative(osdc
->client
->options
->osd_idle_ttl
* HZ
));
2035 osdc
->req_mempool
= mempool_create_kmalloc_pool(10,
2036 sizeof(struct ceph_osd_request
));
2037 if (!osdc
->req_mempool
)
2040 err
= ceph_msgpool_init(&osdc
->msgpool_op
, CEPH_MSG_OSD_OP
,
2041 OSD_OP_FRONT_LEN
, 10, true,
2045 err
= ceph_msgpool_init(&osdc
->msgpool_op_reply
, CEPH_MSG_OSD_OPREPLY
,
2046 OSD_OPREPLY_FRONT_LEN
, 10, true,
2051 osdc
->notify_wq
= create_singlethread_workqueue("ceph-watch-notify");
2052 if (IS_ERR(osdc
->notify_wq
)) {
2053 err
= PTR_ERR(osdc
->notify_wq
);
2054 osdc
->notify_wq
= NULL
;
2060 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2062 mempool_destroy(osdc
->req_mempool
);
2067 void ceph_osdc_stop(struct ceph_osd_client
*osdc
)
2069 flush_workqueue(osdc
->notify_wq
);
2070 destroy_workqueue(osdc
->notify_wq
);
2071 cancel_delayed_work_sync(&osdc
->timeout_work
);
2072 cancel_delayed_work_sync(&osdc
->osds_timeout_work
);
2074 ceph_osdmap_destroy(osdc
->osdmap
);
2075 osdc
->osdmap
= NULL
;
2077 remove_all_osds(osdc
);
2078 mempool_destroy(osdc
->req_mempool
);
2079 ceph_msgpool_destroy(&osdc
->msgpool_op
);
2080 ceph_msgpool_destroy(&osdc
->msgpool_op_reply
);
2084 * Read some contiguous pages. If we cross a stripe boundary, shorten
2085 * *plen. Return number of bytes read, or error.
2087 int ceph_osdc_readpages(struct ceph_osd_client
*osdc
,
2088 struct ceph_vino vino
, struct ceph_file_layout
*layout
,
2090 u32 truncate_seq
, u64 truncate_size
,
2091 struct page
**pages
, int num_pages
, int page_align
)
2093 struct ceph_osd_request
*req
;
2094 struct ceph_osd_data
*osd_data
;
2097 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino
.ino
,
2098 vino
.snap
, off
, *plen
);
2099 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, plen
,
2100 CEPH_OSD_OP_READ
, CEPH_OSD_FLAG_READ
,
2101 NULL
, 0, truncate_seq
, truncate_size
, NULL
,
2104 return PTR_ERR(req
);
2106 /* it may be a short read due to an object boundary */
2108 osd_data
= &req
->r_data_in
;
2109 osd_data
->type
= CEPH_OSD_DATA_TYPE_PAGES
;
2110 osd_data
->pages
= pages
;
2111 osd_data
->length
= *plen
;
2112 osd_data
->alignment
= page_align
;
2114 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2115 off
, *plen
, osd_data
->length
, page_align
);
2117 rc
= ceph_osdc_start_request(osdc
, req
, false);
2119 rc
= ceph_osdc_wait_request(osdc
, req
);
2121 ceph_osdc_put_request(req
);
2122 dout("readpages result %d\n", rc
);
2125 EXPORT_SYMBOL(ceph_osdc_readpages
);
2128 * do a synchronous write on N pages
2130 int ceph_osdc_writepages(struct ceph_osd_client
*osdc
, struct ceph_vino vino
,
2131 struct ceph_file_layout
*layout
,
2132 struct ceph_snap_context
*snapc
,
2134 u32 truncate_seq
, u64 truncate_size
,
2135 struct timespec
*mtime
,
2136 struct page
**pages
, int num_pages
)
2138 struct ceph_osd_request
*req
;
2139 struct ceph_osd_data
*osd_data
;
2141 int page_align
= off
& ~PAGE_MASK
;
2143 BUG_ON(vino
.snap
!= CEPH_NOSNAP
);
2144 req
= ceph_osdc_new_request(osdc
, layout
, vino
, off
, &len
,
2146 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
2148 truncate_seq
, truncate_size
, mtime
,
2151 return PTR_ERR(req
);
2153 /* it may be a short write due to an object boundary */
2154 osd_data
= &req
->r_data_out
;
2155 osd_data
->type
= CEPH_OSD_DATA_TYPE_PAGES
;
2156 osd_data
->pages
= pages
;
2157 osd_data
->length
= len
;
2158 osd_data
->alignment
= page_align
;
2159 dout("writepages %llu~%llu (%llu bytes)\n", off
, len
, osd_data
->length
);
2161 rc
= ceph_osdc_start_request(osdc
, req
, true);
2163 rc
= ceph_osdc_wait_request(osdc
, req
);
2165 ceph_osdc_put_request(req
);
2168 dout("writepages result %d\n", rc
);
2171 EXPORT_SYMBOL(ceph_osdc_writepages
);
2174 * handle incoming message
2176 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2178 struct ceph_osd
*osd
= con
->private;
2179 struct ceph_osd_client
*osdc
;
2180 int type
= le16_to_cpu(msg
->hdr
.type
);
2187 case CEPH_MSG_OSD_MAP
:
2188 ceph_osdc_handle_map(osdc
, msg
);
2190 case CEPH_MSG_OSD_OPREPLY
:
2191 handle_reply(osdc
, msg
, con
);
2193 case CEPH_MSG_WATCH_NOTIFY
:
2194 handle_watch_notify(osdc
, msg
);
2198 pr_err("received unknown message type %d %s\n", type
,
2199 ceph_msg_type_name(type
));
2206 * lookup and return message for incoming reply. set up reply message
2209 static struct ceph_msg
*get_reply(struct ceph_connection
*con
,
2210 struct ceph_msg_header
*hdr
,
2213 struct ceph_osd
*osd
= con
->private;
2214 struct ceph_osd_client
*osdc
= osd
->o_osdc
;
2216 struct ceph_osd_request
*req
;
2217 int front
= le32_to_cpu(hdr
->front_len
);
2218 int data_len
= le32_to_cpu(hdr
->data_len
);
2221 tid
= le64_to_cpu(hdr
->tid
);
2222 mutex_lock(&osdc
->request_mutex
);
2223 req
= __lookup_request(osdc
, tid
);
2227 dout("get_reply unknown tid %llu from osd%d\n", tid
,
2232 if (req
->r_con_filling_msg
) {
2233 dout("%s revoking msg %p from old con %p\n", __func__
,
2234 req
->r_reply
, req
->r_con_filling_msg
);
2235 ceph_msg_revoke_incoming(req
->r_reply
);
2236 req
->r_con_filling_msg
->ops
->put(req
->r_con_filling_msg
);
2237 req
->r_con_filling_msg
= NULL
;
2240 if (front
> req
->r_reply
->front
.iov_len
) {
2241 pr_warning("get_reply front %d > preallocated %d\n",
2242 front
, (int)req
->r_reply
->front
.iov_len
);
2243 m
= ceph_msg_new(CEPH_MSG_OSD_OPREPLY
, front
, GFP_NOFS
, false);
2246 ceph_msg_put(req
->r_reply
);
2249 m
= ceph_msg_get(req
->r_reply
);
2252 struct ceph_osd_data
*osd_data
= &req
->r_data_in
;
2254 if (osd_data
->type
== CEPH_OSD_DATA_TYPE_PAGES
) {
2255 if (osd_data
->pages
&&
2256 unlikely(osd_data
->length
< data_len
)) {
2258 pr_warning("tid %lld reply has %d bytes "
2259 "we had only %llu bytes ready\n",
2260 tid
, data_len
, osd_data
->length
);
2269 req
->r_con_filling_msg
= con
->ops
->get(con
);
2270 dout("get_reply tid %lld %p\n", tid
, m
);
2273 mutex_unlock(&osdc
->request_mutex
);
2278 static struct ceph_msg
*alloc_msg(struct ceph_connection
*con
,
2279 struct ceph_msg_header
*hdr
,
2282 struct ceph_osd
*osd
= con
->private;
2283 int type
= le16_to_cpu(hdr
->type
);
2284 int front
= le32_to_cpu(hdr
->front_len
);
2288 case CEPH_MSG_OSD_MAP
:
2289 case CEPH_MSG_WATCH_NOTIFY
:
2290 return ceph_msg_new(type
, front
, GFP_NOFS
, false);
2291 case CEPH_MSG_OSD_OPREPLY
:
2292 return get_reply(con
, hdr
, skip
);
2294 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type
,
2302 * Wrappers to refcount containing ceph_osd struct
2304 static struct ceph_connection
*get_osd_con(struct ceph_connection
*con
)
2306 struct ceph_osd
*osd
= con
->private;
2312 static void put_osd_con(struct ceph_connection
*con
)
2314 struct ceph_osd
*osd
= con
->private;
2322 * Note: returned pointer is the address of a structure that's
2323 * managed separately. Caller must *not* attempt to free it.
2325 static struct ceph_auth_handshake
*get_authorizer(struct ceph_connection
*con
,
2326 int *proto
, int force_new
)
2328 struct ceph_osd
*o
= con
->private;
2329 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2330 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2331 struct ceph_auth_handshake
*auth
= &o
->o_auth
;
2333 if (force_new
&& auth
->authorizer
) {
2334 ceph_auth_destroy_authorizer(ac
, auth
->authorizer
);
2335 auth
->authorizer
= NULL
;
2337 if (!auth
->authorizer
) {
2338 int ret
= ceph_auth_create_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2341 return ERR_PTR(ret
);
2343 int ret
= ceph_auth_update_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
,
2346 return ERR_PTR(ret
);
2348 *proto
= ac
->protocol
;
2354 static int verify_authorizer_reply(struct ceph_connection
*con
, int len
)
2356 struct ceph_osd
*o
= con
->private;
2357 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2358 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2360 return ceph_auth_verify_authorizer_reply(ac
, o
->o_auth
.authorizer
, len
);
2363 static int invalidate_authorizer(struct ceph_connection
*con
)
2365 struct ceph_osd
*o
= con
->private;
2366 struct ceph_osd_client
*osdc
= o
->o_osdc
;
2367 struct ceph_auth_client
*ac
= osdc
->client
->monc
.auth
;
2369 ceph_auth_invalidate_authorizer(ac
, CEPH_ENTITY_TYPE_OSD
);
2370 return ceph_monc_validate_auth(&osdc
->client
->monc
);
2373 static const struct ceph_connection_operations osd_con_ops
= {
2376 .dispatch
= dispatch
,
2377 .get_authorizer
= get_authorizer
,
2378 .verify_authorizer_reply
= verify_authorizer_reply
,
2379 .invalidate_authorizer
= invalidate_authorizer
,
2380 .alloc_msg
= alloc_msg
,