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_reply(struct ceph_osd_client *osdc,
453 struct ceph_msg *msg);
454 extern void ceph_osdc_handle_map(struct ceph_osd_client *osdc,
455 struct ceph_msg *msg);
456 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb);
457 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err);
458 void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc);
460 #define osd_req_op_data(oreq, whch, typ, fld) \
462 struct ceph_osd_request *__oreq = (oreq); \
463 unsigned int __whch = (whch); \
464 BUG_ON(__whch >= __oreq->r_num_ops); \
465 &__oreq->r_ops[__whch].typ.fld; \
468 struct ceph_osd_req_op *osd_req_op_init(struct ceph_osd_request *osd_req,
469 unsigned int which, u16 opcode, u32 flags);
471 extern void osd_req_op_raw_data_in_pages(struct ceph_osd_request *,
473 struct page **pages, u64 length,
474 u32 alignment, bool pages_from_pool,
477 extern void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
478 unsigned int which, u16 opcode,
479 u64 offset, u64 length,
480 u64 truncate_size, u32 truncate_seq);
481 extern void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
482 unsigned int which, u64 length);
483 extern void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
484 unsigned int which, u64 offset_inc);
486 extern struct ceph_osd_data *osd_req_op_extent_osd_data(
487 struct ceph_osd_request *osd_req,
490 extern void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *,
492 struct page **pages, u64 length,
493 u32 alignment, bool pages_from_pool,
495 extern void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *,
497 struct ceph_pagelist *pagelist);
499 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
501 struct ceph_bio_iter *bio_pos,
503 #endif /* CONFIG_BLOCK */
504 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
506 struct bio_vec *bvecs, u32 num_bvecs,
508 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
510 struct ceph_bvec_iter *bvec_pos);
511 void osd_req_op_extent_osd_iter(struct ceph_osd_request *osd_req,
512 unsigned int which, struct iov_iter *iter);
514 extern void osd_req_op_cls_request_data_pagelist(struct ceph_osd_request *,
516 struct ceph_pagelist *pagelist);
517 extern void osd_req_op_cls_request_data_pages(struct ceph_osd_request *,
519 struct page **pages, u64 length,
520 u32 alignment, bool pages_from_pool,
522 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
524 struct bio_vec *bvecs, u32 num_bvecs,
526 extern void osd_req_op_cls_response_data_pages(struct ceph_osd_request *,
528 struct page **pages, u64 length,
529 u32 alignment, bool pages_from_pool,
531 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
532 const char *class, const char *method);
533 extern int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
534 u16 opcode, const char *name, const void *value,
535 size_t size, u8 cmp_op, u8 cmp_mode);
536 extern void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
538 u64 expected_object_size,
539 u64 expected_write_size,
541 extern int osd_req_op_copy_from_init(struct ceph_osd_request *req,
542 u64 src_snapid, u64 src_version,
543 struct ceph_object_id *src_oid,
544 struct ceph_object_locator *src_oloc,
545 u32 src_fadvise_flags,
546 u32 dst_fadvise_flags,
547 u32 truncate_seq, u64 truncate_size,
550 extern struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
551 struct ceph_snap_context *snapc,
552 unsigned int num_ops,
555 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp);
557 extern struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *,
558 struct ceph_file_layout *layout,
559 struct ceph_vino vino,
560 u64 offset, u64 *len,
561 unsigned int which, int num_ops,
562 int opcode, int flags,
563 struct ceph_snap_context *snapc,
564 u32 truncate_seq, u64 truncate_size,
567 int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt);
570 * How big an extent array should we preallocate for a sparse read? This is
571 * just a starting value. If we get more than this back from the OSD, the
572 * receiver will reallocate.
574 #define CEPH_SPARSE_EXT_ARRAY_INITIAL 16
576 static inline int ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt)
579 cnt = CEPH_SPARSE_EXT_ARRAY_INITIAL;
581 return __ceph_alloc_sparse_ext_map(op, cnt);
584 extern void ceph_osdc_get_request(struct ceph_osd_request *req);
585 extern void ceph_osdc_put_request(struct ceph_osd_request *req);
587 void ceph_osdc_start_request(struct ceph_osd_client *osdc,
588 struct ceph_osd_request *req);
589 extern void ceph_osdc_cancel_request(struct ceph_osd_request *req);
590 extern int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
591 struct ceph_osd_request *req);
592 extern void ceph_osdc_sync(struct ceph_osd_client *osdc);
594 extern void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc);
595 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc);
597 int ceph_osdc_call(struct ceph_osd_client *osdc,
598 struct ceph_object_id *oid,
599 struct ceph_object_locator *oloc,
600 const char *class, const char *method,
602 struct page *req_page, size_t req_len,
603 struct page **resp_pages, size_t *resp_len);
606 struct ceph_osd_linger_request *
607 ceph_osdc_watch(struct ceph_osd_client *osdc,
608 struct ceph_object_id *oid,
609 struct ceph_object_locator *oloc,
610 rados_watchcb2_t wcb,
611 rados_watcherrcb_t errcb,
613 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
614 struct ceph_osd_linger_request *lreq);
616 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
617 struct ceph_object_id *oid,
618 struct ceph_object_locator *oloc,
623 int ceph_osdc_notify(struct ceph_osd_client *osdc,
624 struct ceph_object_id *oid,
625 struct ceph_object_locator *oloc,
629 struct page ***preply_pages,
631 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
632 struct ceph_osd_linger_request *lreq);
633 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
634 struct ceph_object_id *oid,
635 struct ceph_object_locator *oloc,
636 struct ceph_watch_item **watchers,
639 /* Find offset into the buffer of the end of the extent map */
640 static inline u64 ceph_sparse_ext_map_end(struct ceph_osd_req_op *op)
642 struct ceph_sparse_extent *ext;
644 /* No extents? No data */
645 if (op->extent.sparse_ext_cnt == 0)
648 ext = &op->extent.sparse_ext[op->extent.sparse_ext_cnt - 1];
650 return ext->off + ext->len - op->extent.offset;