1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _FS_CEPH_OSD_CLIENT_H
3 #define _FS_CEPH_OSD_CLIENT_H
5 #include <linux/bitrev.h>
6 #include <linux/completion.h>
7 #include <linux/kref.h>
8 #include <linux/mempool.h>
9 #include <linux/rbtree.h>
10 #include <linux/refcount.h>
11 #include <linux/ktime.h>
13 #include <linux/ceph/types.h>
14 #include <linux/ceph/osdmap.h>
15 #include <linux/ceph/messenger.h>
16 #include <linux/ceph/msgpool.h>
17 #include <linux/ceph/auth.h>
18 #include <linux/ceph/pagelist.h>
21 struct ceph_snap_context
;
22 struct ceph_osd_request
;
23 struct ceph_osd_client
;
26 * completion callback for async writepages
28 typedef void (*ceph_osdc_callback_t
)(struct ceph_osd_request
*);
30 #define CEPH_HOMELESS_OSD -1
33 * A single extent in a SPARSE_READ reply.
35 * Note that these come from the OSD as little-endian values. On BE arches,
36 * we convert them in-place after receipt.
38 struct ceph_sparse_extent
{
43 /* Sparse read state machine state values */
44 enum ceph_sparse_read_state
{
45 CEPH_SPARSE_READ_HDR
= 0,
46 CEPH_SPARSE_READ_EXTENTS
,
47 CEPH_SPARSE_READ_DATA_LEN
,
48 CEPH_SPARSE_READ_DATA_PRE
,
49 CEPH_SPARSE_READ_DATA
,
53 * A SPARSE_READ reply is a 32-bit count of extents, followed by an array of
54 * 64-bit offset/length pairs, and then all of the actual file data
55 * concatenated after it (sans holes).
57 * Unfortunately, we don't know how long the extent array is until we've
58 * started reading the data section of the reply. The caller should send down
59 * a destination buffer for the array, but we'll alloc one if it's too small
60 * or if the caller doesn't.
62 struct ceph_sparse_read
{
63 enum ceph_sparse_read_state sr_state
; /* state machine state */
64 u64 sr_req_off
; /* orig request offset */
65 u64 sr_req_len
; /* orig request length */
66 u64 sr_pos
; /* current pos in buffer */
67 int sr_index
; /* current extent index */
68 u32 sr_datalen
; /* length of actual data */
69 u32 sr_count
; /* extent count in reply */
70 int sr_ext_len
; /* length of extent array */
71 struct ceph_sparse_extent
*sr_extent
; /* extent array */
75 * A given osd we're communicating with.
77 * Note that the o_requests tree can be searched while holding the "lock" mutex
78 * or the "o_requests_lock" spinlock. Insertion or removal requires both!
83 struct ceph_osd_client
*o_osdc
;
86 struct rb_node o_node
;
87 struct ceph_connection o_con
;
88 spinlock_t o_requests_lock
;
89 struct rb_root o_requests
;
90 struct rb_root o_linger_requests
;
91 struct rb_root o_backoff_mappings
;
92 struct rb_root o_backoffs_by_id
;
93 struct list_head o_osd_lru
;
94 struct ceph_auth_handshake o_auth
;
95 unsigned long lru_ttl
;
96 struct list_head o_keepalive_item
;
98 struct ceph_sparse_read o_sparse_read
;
101 #define CEPH_OSD_SLAB_OPS 2
102 #define CEPH_OSD_MAX_OPS 16
104 enum ceph_osd_data_type
{
105 CEPH_OSD_DATA_TYPE_NONE
= 0,
106 CEPH_OSD_DATA_TYPE_PAGES
,
107 CEPH_OSD_DATA_TYPE_PAGELIST
,
109 CEPH_OSD_DATA_TYPE_BIO
,
110 #endif /* CONFIG_BLOCK */
111 CEPH_OSD_DATA_TYPE_BVECS
,
112 CEPH_OSD_DATA_TYPE_ITER
,
115 struct ceph_osd_data
{
116 enum ceph_osd_data_type type
;
122 bool pages_from_pool
;
125 struct ceph_pagelist
*pagelist
;
128 struct ceph_bio_iter bio_pos
;
131 #endif /* CONFIG_BLOCK */
133 struct ceph_bvec_iter bvec_pos
;
136 struct iov_iter iter
;
140 struct ceph_osd_req_op
{
141 u16 op
; /* CEPH_OSD_OP_* */
142 u32 flags
; /* CEPH_OSD_OP_FLAG_* */
143 u32 indata_len
; /* request */
144 u32 outdata_len
; /* reply */
148 struct ceph_osd_data raw_data_in
;
154 struct ceph_sparse_extent
*sparse_ext
;
155 struct ceph_osd_data osd_data
;
160 __u8 cmp_op
; /* CEPH_OSD_CMPXATTR_OP_* */
161 __u8 cmp_mode
; /* CEPH_OSD_CMPXATTR_MODE_* */
162 struct ceph_osd_data osd_data
;
165 const char *class_name
;
166 const char *method_name
;
167 struct ceph_osd_data request_info
;
168 struct ceph_osd_data request_data
;
169 struct ceph_osd_data response_data
;
176 __u8 op
; /* CEPH_OSD_WATCH_OP_ */
180 struct ceph_osd_data request_data
;
184 struct ceph_osd_data request_data
;
185 struct ceph_osd_data response_data
;
188 struct ceph_osd_data response_data
;
191 u64 expected_object_size
;
192 u64 expected_write_size
;
193 u32 flags
; /* CEPH_OSD_OP_ALLOC_HINT_FLAG_* */
199 u32 src_fadvise_flags
;
200 struct ceph_osd_data osd_data
;
208 struct ceph_osd_request_target
{
209 struct ceph_object_id base_oid
;
210 struct ceph_object_locator base_oloc
;
211 struct ceph_object_id target_oid
;
212 struct ceph_object_locator target_oloc
;
214 struct ceph_pg pgid
; /* last raw pg we mapped to */
215 struct ceph_spg spgid
; /* last actual spg we mapped to */
218 struct ceph_osds acting
;
223 bool recovery_deletes
;
225 unsigned int flags
; /* CEPH_OSD_FLAG_* */
230 u32 last_force_resend
;
235 /* an in-flight request */
236 struct ceph_osd_request
{
237 u64 r_tid
; /* unique for this client */
238 struct rb_node r_node
;
239 struct rb_node r_mc_node
; /* map check */
240 struct work_struct r_complete_work
;
241 struct ceph_osd
*r_osd
;
243 struct ceph_osd_request_target r_t
;
244 #define r_base_oid r_t.base_oid
245 #define r_base_oloc r_t.base_oloc
246 #define r_flags r_t.flags
248 struct ceph_msg
*r_request
, *r_reply
;
249 u32 r_sent
; /* >0 if r_request is sending/sent */
251 /* request osd ops array */
252 unsigned int r_num_ops
;
256 struct ceph_osd_client
*r_osdc
;
259 bool r_linger
; /* don't resend on failure */
260 struct completion r_completion
; /* private to osd_client.c */
261 ceph_osdc_callback_t r_callback
;
263 struct inode
*r_inode
; /* for use by callbacks */
264 struct list_head r_private_item
; /* ditto */
265 void *r_priv
; /* ditto */
267 /* set by submitter */
268 u64 r_snapid
; /* for reads, CEPH_NOSNAP o/w */
269 struct ceph_snap_context
*r_snapc
; /* for writes */
270 struct timespec64 r_mtime
; /* ditto */
271 u64 r_data_offset
; /* ditto */
274 u64 r_version
; /* data version sent in reply */
275 unsigned long r_stamp
; /* jiffies, send or check time */
276 unsigned long r_start_stamp
; /* jiffies */
277 ktime_t r_start_latency
; /* ktime_t */
278 ktime_t r_end_latency
; /* ktime_t */
282 struct ceph_osd_req_op r_ops
[] __counted_by(r_num_ops
);
285 struct ceph_request_redirect
{
286 struct ceph_object_locator oloc
;
290 * osd request identifier
292 * caller name + incarnation# + tid to unique identify this request
294 struct ceph_osd_reqid
{
295 struct ceph_entity_name name
;
300 struct ceph_blkin_trace_info
{
303 __le64 parent_span_id
;
306 typedef void (*rados_watchcb2_t
)(void *arg
, u64 notify_id
, u64 cookie
,
307 u64 notifier_id
, void *data
, size_t data_len
);
308 typedef void (*rados_watcherrcb_t
)(void *arg
, u64 cookie
, int err
);
310 struct ceph_osd_linger_request
{
311 struct ceph_osd_client
*osdc
;
314 bool is_watch
; /* watch or notify */
316 struct ceph_osd
*osd
;
317 struct ceph_osd_request
*reg_req
;
318 struct ceph_osd_request
*ping_req
;
319 unsigned long ping_sent
;
320 unsigned long watch_valid_thru
;
321 struct list_head pending_lworks
;
323 struct ceph_osd_request_target t
;
326 struct timespec64 mtime
;
330 struct rb_node node
; /* osd */
331 struct rb_node osdc_node
; /* osdc */
332 struct rb_node mc_node
; /* map check */
333 struct list_head scan_item
;
335 struct completion reg_commit_wait
;
336 struct completion notify_finish_wait
;
337 int reg_commit_error
;
338 int notify_finish_error
;
344 rados_watchcb2_t wcb
;
345 rados_watcherrcb_t errcb
;
348 struct ceph_pagelist
*request_pl
;
349 struct page
**notify_id_pages
;
351 struct page
***preply_pages
;
355 struct ceph_watch_item
{
356 struct ceph_entity_name name
;
358 struct ceph_entity_addr addr
;
361 struct ceph_spg_mapping
{
363 struct ceph_spg spgid
;
365 struct rb_root backoffs
;
368 struct ceph_hobject_id
{
381 u32 hash_reverse_bits
;
384 static inline void ceph_hoid_build_hash_cache(struct ceph_hobject_id
*hoid
)
386 hoid
->hash_reverse_bits
= bitrev32(hoid
->hash
);
390 * PG-wide backoff: [begin, end)
391 * per-object backoff: begin == end
393 struct ceph_osd_backoff
{
394 struct rb_node spg_node
;
395 struct rb_node id_node
;
397 struct ceph_spg spgid
;
399 struct ceph_hobject_id
*begin
;
400 struct ceph_hobject_id
*end
;
403 #define CEPH_LINGER_ID_START 0xffff000000000000ULL
405 struct ceph_osd_client
{
406 struct ceph_client
*client
;
408 struct ceph_osdmap
*osdmap
; /* current map */
409 struct rw_semaphore lock
;
411 struct rb_root osds
; /* osds */
412 struct list_head osd_lru
; /* idle osds */
413 spinlock_t osd_lru_lock
;
415 struct ceph_osd homeless_osd
;
416 atomic64_t last_tid
; /* tid of last request */
418 struct rb_root linger_requests
; /* lingering requests */
419 struct rb_root map_checks
;
420 struct rb_root linger_map_checks
;
421 atomic_t num_requests
;
422 atomic_t num_homeless
;
424 struct delayed_work timeout_work
;
425 struct delayed_work osds_timeout_work
;
426 #ifdef CONFIG_DEBUG_FS
427 struct dentry
*debugfs_file
;
430 mempool_t
*req_mempool
;
432 struct ceph_msgpool msgpool_op
;
433 struct ceph_msgpool msgpool_op_reply
;
435 struct workqueue_struct
*notify_wq
;
436 struct workqueue_struct
*completion_wq
;
439 static inline bool ceph_osdmap_flag(struct ceph_osd_client
*osdc
, int flag
)
441 return osdc
->osdmap
->flags
& flag
;
444 extern int ceph_osdc_setup(void);
445 extern void ceph_osdc_cleanup(void);
447 extern int ceph_osdc_init(struct ceph_osd_client
*osdc
,
448 struct ceph_client
*client
);
449 extern void ceph_osdc_stop(struct ceph_osd_client
*osdc
);
450 extern void ceph_osdc_reopen_osds(struct ceph_osd_client
*osdc
);
452 extern void ceph_osdc_handle_map(struct ceph_osd_client
*osdc
,
453 struct ceph_msg
*msg
);
454 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client
*osdc
, u32 eb
);
455 void ceph_osdc_abort_requests(struct ceph_osd_client
*osdc
, int err
);
456 void ceph_osdc_clear_abort_err(struct ceph_osd_client
*osdc
);
458 #define osd_req_op_data(oreq, whch, typ, fld) \
460 struct ceph_osd_request *__oreq = (oreq); \
461 unsigned int __whch = (whch); \
462 BUG_ON(__whch >= __oreq->r_num_ops); \
463 &__oreq->r_ops[__whch].typ.fld; \
466 struct ceph_osd_req_op
*osd_req_op_init(struct ceph_osd_request
*osd_req
,
467 unsigned int which
, u16 opcode
, u32 flags
);
469 extern void osd_req_op_raw_data_in_pages(struct ceph_osd_request
*,
471 struct page
**pages
, u64 length
,
472 u32 alignment
, bool pages_from_pool
,
475 extern void osd_req_op_extent_init(struct ceph_osd_request
*osd_req
,
476 unsigned int which
, u16 opcode
,
477 u64 offset
, u64 length
,
478 u64 truncate_size
, u32 truncate_seq
);
479 extern void osd_req_op_extent_update(struct ceph_osd_request
*osd_req
,
480 unsigned int which
, u64 length
);
481 extern void osd_req_op_extent_dup_last(struct ceph_osd_request
*osd_req
,
482 unsigned int which
, u64 offset_inc
);
484 extern struct ceph_osd_data
*osd_req_op_extent_osd_data(
485 struct ceph_osd_request
*osd_req
,
488 extern void osd_req_op_extent_osd_data_pages(struct ceph_osd_request
*,
490 struct page
**pages
, u64 length
,
491 u32 alignment
, bool pages_from_pool
,
493 extern void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request
*,
495 struct ceph_pagelist
*pagelist
);
497 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request
*osd_req
,
499 struct ceph_bio_iter
*bio_pos
,
501 #endif /* CONFIG_BLOCK */
502 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request
*osd_req
,
504 struct bio_vec
*bvecs
, u32 num_bvecs
,
506 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request
*osd_req
,
508 struct ceph_bvec_iter
*bvec_pos
);
509 void osd_req_op_extent_osd_iter(struct ceph_osd_request
*osd_req
,
510 unsigned int which
, struct iov_iter
*iter
);
512 extern void osd_req_op_cls_request_data_pagelist(struct ceph_osd_request
*,
514 struct ceph_pagelist
*pagelist
);
515 extern void osd_req_op_cls_request_data_pages(struct ceph_osd_request
*,
517 struct page
**pages
, u64 length
,
518 u32 alignment
, bool pages_from_pool
,
520 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request
*osd_req
,
522 struct bio_vec
*bvecs
, u32 num_bvecs
,
524 extern void osd_req_op_cls_response_data_pages(struct ceph_osd_request
*,
526 struct page
**pages
, u64 length
,
527 u32 alignment
, bool pages_from_pool
,
529 int osd_req_op_cls_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
530 const char *class, const char *method
);
531 extern int osd_req_op_xattr_init(struct ceph_osd_request
*osd_req
, unsigned int which
,
532 u16 opcode
, const char *name
, const void *value
,
533 size_t size
, u8 cmp_op
, u8 cmp_mode
);
534 extern void osd_req_op_alloc_hint_init(struct ceph_osd_request
*osd_req
,
536 u64 expected_object_size
,
537 u64 expected_write_size
,
539 extern int osd_req_op_copy_from_init(struct ceph_osd_request
*req
,
540 u64 src_snapid
, u64 src_version
,
541 struct ceph_object_id
*src_oid
,
542 struct ceph_object_locator
*src_oloc
,
543 u32 src_fadvise_flags
,
544 u32 dst_fadvise_flags
,
545 u32 truncate_seq
, u64 truncate_size
,
548 extern struct ceph_osd_request
*ceph_osdc_alloc_request(struct ceph_osd_client
*osdc
,
549 struct ceph_snap_context
*snapc
,
550 unsigned int num_ops
,
553 int ceph_osdc_alloc_messages(struct ceph_osd_request
*req
, gfp_t gfp
);
555 extern struct ceph_osd_request
*ceph_osdc_new_request(struct ceph_osd_client
*,
556 struct ceph_file_layout
*layout
,
557 struct ceph_vino vino
,
558 u64 offset
, u64
*len
,
559 unsigned int which
, int num_ops
,
560 int opcode
, int flags
,
561 struct ceph_snap_context
*snapc
,
562 u32 truncate_seq
, u64 truncate_size
,
565 int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op
*op
, int cnt
);
568 * How big an extent array should we preallocate for a sparse read? This is
569 * just a starting value. If we get more than this back from the OSD, the
570 * receiver will reallocate.
572 #define CEPH_SPARSE_EXT_ARRAY_INITIAL 16
574 static inline int ceph_alloc_sparse_ext_map(struct ceph_osd_req_op
*op
, int cnt
)
577 cnt
= CEPH_SPARSE_EXT_ARRAY_INITIAL
;
579 return __ceph_alloc_sparse_ext_map(op
, cnt
);
582 extern void ceph_osdc_get_request(struct ceph_osd_request
*req
);
583 extern void ceph_osdc_put_request(struct ceph_osd_request
*req
);
585 void ceph_osdc_start_request(struct ceph_osd_client
*osdc
,
586 struct ceph_osd_request
*req
);
587 extern void ceph_osdc_cancel_request(struct ceph_osd_request
*req
);
588 extern int ceph_osdc_wait_request(struct ceph_osd_client
*osdc
,
589 struct ceph_osd_request
*req
);
590 extern void ceph_osdc_sync(struct ceph_osd_client
*osdc
);
592 extern void ceph_osdc_flush_notifies(struct ceph_osd_client
*osdc
);
593 void ceph_osdc_maybe_request_map(struct ceph_osd_client
*osdc
);
595 int ceph_osdc_call(struct ceph_osd_client
*osdc
,
596 struct ceph_object_id
*oid
,
597 struct ceph_object_locator
*oloc
,
598 const char *class, const char *method
,
600 struct page
*req_page
, size_t req_len
,
601 struct page
**resp_pages
, size_t *resp_len
);
604 struct ceph_osd_linger_request
*
605 ceph_osdc_watch(struct ceph_osd_client
*osdc
,
606 struct ceph_object_id
*oid
,
607 struct ceph_object_locator
*oloc
,
608 rados_watchcb2_t wcb
,
609 rados_watcherrcb_t errcb
,
611 int ceph_osdc_unwatch(struct ceph_osd_client
*osdc
,
612 struct ceph_osd_linger_request
*lreq
);
614 int ceph_osdc_notify_ack(struct ceph_osd_client
*osdc
,
615 struct ceph_object_id
*oid
,
616 struct ceph_object_locator
*oloc
,
621 int ceph_osdc_notify(struct ceph_osd_client
*osdc
,
622 struct ceph_object_id
*oid
,
623 struct ceph_object_locator
*oloc
,
627 struct page
***preply_pages
,
629 int ceph_osdc_list_watchers(struct ceph_osd_client
*osdc
,
630 struct ceph_object_id
*oid
,
631 struct ceph_object_locator
*oloc
,
632 struct ceph_watch_item
**watchers
,
635 /* Find offset into the buffer of the end of the extent map */
636 static inline u64
ceph_sparse_ext_map_end(struct ceph_osd_req_op
*op
)
638 struct ceph_sparse_extent
*ext
;
640 /* No extents? No data */
641 if (op
->extent
.sparse_ext_cnt
== 0)
644 ext
= &op
->extent
.sparse_ext
[op
->extent
.sparse_ext_cnt
- 1];
646 return ext
->off
+ ext
->len
- op
->extent
.offset
;