xref: /linux-6.15/include/linux/ceph/osd_client.h (revision dfd32cad)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _FS_CEPH_OSD_CLIENT_H
3 #define _FS_CEPH_OSD_CLIENT_H
4 
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 
12 #include <linux/ceph/types.h>
13 #include <linux/ceph/osdmap.h>
14 #include <linux/ceph/messenger.h>
15 #include <linux/ceph/msgpool.h>
16 #include <linux/ceph/auth.h>
17 #include <linux/ceph/pagelist.h>
18 
19 struct ceph_msg;
20 struct ceph_snap_context;
21 struct ceph_osd_request;
22 struct ceph_osd_client;
23 
24 /*
25  * completion callback for async writepages
26  */
27 typedef void (*ceph_osdc_callback_t)(struct ceph_osd_request *);
28 
29 #define CEPH_HOMELESS_OSD	-1
30 
31 /* a given osd we're communicating with */
32 struct ceph_osd {
33 	refcount_t o_ref;
34 	struct ceph_osd_client *o_osdc;
35 	int o_osd;
36 	int o_incarnation;
37 	struct rb_node o_node;
38 	struct ceph_connection o_con;
39 	struct rb_root o_requests;
40 	struct rb_root o_linger_requests;
41 	struct rb_root o_backoff_mappings;
42 	struct rb_root o_backoffs_by_id;
43 	struct list_head o_osd_lru;
44 	struct ceph_auth_handshake o_auth;
45 	unsigned long lru_ttl;
46 	struct list_head o_keepalive_item;
47 	struct mutex lock;
48 };
49 
50 #define CEPH_OSD_SLAB_OPS	2
51 #define CEPH_OSD_MAX_OPS	16
52 
53 enum ceph_osd_data_type {
54 	CEPH_OSD_DATA_TYPE_NONE = 0,
55 	CEPH_OSD_DATA_TYPE_PAGES,
56 	CEPH_OSD_DATA_TYPE_PAGELIST,
57 #ifdef CONFIG_BLOCK
58 	CEPH_OSD_DATA_TYPE_BIO,
59 #endif /* CONFIG_BLOCK */
60 	CEPH_OSD_DATA_TYPE_BVECS,
61 };
62 
63 struct ceph_osd_data {
64 	enum ceph_osd_data_type	type;
65 	union {
66 		struct {
67 			struct page	**pages;
68 			u64		length;
69 			u32		alignment;
70 			bool		pages_from_pool;
71 			bool		own_pages;
72 		};
73 		struct ceph_pagelist	*pagelist;
74 #ifdef CONFIG_BLOCK
75 		struct {
76 			struct ceph_bio_iter	bio_pos;
77 			u32			bio_length;
78 		};
79 #endif /* CONFIG_BLOCK */
80 		struct {
81 			struct ceph_bvec_iter	bvec_pos;
82 			u32			num_bvecs;
83 		};
84 	};
85 };
86 
87 struct ceph_osd_req_op {
88 	u16 op;           /* CEPH_OSD_OP_* */
89 	u32 flags;        /* CEPH_OSD_OP_FLAG_* */
90 	u32 indata_len;   /* request */
91 	u32 outdata_len;  /* reply */
92 	s32 rval;
93 
94 	union {
95 		struct ceph_osd_data raw_data_in;
96 		struct {
97 			u64 offset, length;
98 			u64 truncate_size;
99 			u32 truncate_seq;
100 			struct ceph_osd_data osd_data;
101 		} extent;
102 		struct {
103 			u32 name_len;
104 			u32 value_len;
105 			__u8 cmp_op;       /* CEPH_OSD_CMPXATTR_OP_* */
106 			__u8 cmp_mode;     /* CEPH_OSD_CMPXATTR_MODE_* */
107 			struct ceph_osd_data osd_data;
108 		} xattr;
109 		struct {
110 			const char *class_name;
111 			const char *method_name;
112 			struct ceph_osd_data request_info;
113 			struct ceph_osd_data request_data;
114 			struct ceph_osd_data response_data;
115 			__u8 class_len;
116 			__u8 method_len;
117 			u32 indata_len;
118 		} cls;
119 		struct {
120 			u64 cookie;
121 			__u8 op;           /* CEPH_OSD_WATCH_OP_ */
122 			u32 gen;
123 		} watch;
124 		struct {
125 			struct ceph_osd_data request_data;
126 		} notify_ack;
127 		struct {
128 			u64 cookie;
129 			struct ceph_osd_data request_data;
130 			struct ceph_osd_data response_data;
131 		} notify;
132 		struct {
133 			struct ceph_osd_data response_data;
134 		} list_watchers;
135 		struct {
136 			u64 expected_object_size;
137 			u64 expected_write_size;
138 		} alloc_hint;
139 		struct {
140 			u64 snapid;
141 			u64 src_version;
142 			u8 flags;
143 			u32 src_fadvise_flags;
144 			struct ceph_osd_data osd_data;
145 		} copy_from;
146 	};
147 };
148 
149 struct ceph_osd_request_target {
150 	struct ceph_object_id base_oid;
151 	struct ceph_object_locator base_oloc;
152 	struct ceph_object_id target_oid;
153 	struct ceph_object_locator target_oloc;
154 
155 	struct ceph_pg pgid;               /* last raw pg we mapped to */
156 	struct ceph_spg spgid;             /* last actual spg we mapped to */
157 	u32 pg_num;
158 	u32 pg_num_mask;
159 	struct ceph_osds acting;
160 	struct ceph_osds up;
161 	int size;
162 	int min_size;
163 	bool sort_bitwise;
164 	bool recovery_deletes;
165 
166 	unsigned int flags;                /* CEPH_OSD_FLAG_* */
167 	bool paused;
168 
169 	u32 epoch;
170 	u32 last_force_resend;
171 
172 	int osd;
173 };
174 
175 /* an in-flight request */
176 struct ceph_osd_request {
177 	u64             r_tid;              /* unique for this client */
178 	struct rb_node  r_node;
179 	struct rb_node  r_mc_node;          /* map check */
180 	struct work_struct r_complete_work;
181 	struct ceph_osd *r_osd;
182 
183 	struct ceph_osd_request_target r_t;
184 #define r_base_oid	r_t.base_oid
185 #define r_base_oloc	r_t.base_oloc
186 #define r_flags		r_t.flags
187 
188 	struct ceph_msg  *r_request, *r_reply;
189 	u32               r_sent;      /* >0 if r_request is sending/sent */
190 
191 	/* request osd ops array  */
192 	unsigned int		r_num_ops;
193 
194 	int               r_result;
195 
196 	struct ceph_osd_client *r_osdc;
197 	struct kref       r_kref;
198 	bool              r_mempool;
199 	struct completion r_completion;       /* private to osd_client.c */
200 	ceph_osdc_callback_t r_callback;
201 	struct list_head  r_unsafe_item;
202 
203 	struct inode *r_inode;         	      /* for use by callbacks */
204 	void *r_priv;			      /* ditto */
205 
206 	/* set by submitter */
207 	u64 r_snapid;                         /* for reads, CEPH_NOSNAP o/w */
208 	struct ceph_snap_context *r_snapc;    /* for writes */
209 	struct timespec64 r_mtime;            /* ditto */
210 	u64 r_data_offset;                    /* ditto */
211 	bool r_linger;                        /* don't resend on failure */
212 
213 	/* internal */
214 	unsigned long r_stamp;                /* jiffies, send or check time */
215 	unsigned long r_start_stamp;          /* jiffies */
216 	int r_attempts;
217 	u32 r_map_dne_bound;
218 
219 	struct ceph_osd_req_op r_ops[];
220 };
221 
222 struct ceph_request_redirect {
223 	struct ceph_object_locator oloc;
224 };
225 
226 /*
227  * osd request identifier
228  *
229  * caller name + incarnation# + tid to unique identify this request
230  */
231 struct ceph_osd_reqid {
232 	struct ceph_entity_name name;
233 	__le64 tid;
234 	__le32 inc;
235 } __packed;
236 
237 struct ceph_blkin_trace_info {
238 	__le64 trace_id;
239 	__le64 span_id;
240 	__le64 parent_span_id;
241 } __packed;
242 
243 typedef void (*rados_watchcb2_t)(void *arg, u64 notify_id, u64 cookie,
244 				 u64 notifier_id, void *data, size_t data_len);
245 typedef void (*rados_watcherrcb_t)(void *arg, u64 cookie, int err);
246 
247 struct ceph_osd_linger_request {
248 	struct ceph_osd_client *osdc;
249 	u64 linger_id;
250 	bool committed;
251 	bool is_watch;                  /* watch or notify */
252 
253 	struct ceph_osd *osd;
254 	struct ceph_osd_request *reg_req;
255 	struct ceph_osd_request *ping_req;
256 	unsigned long ping_sent;
257 	unsigned long watch_valid_thru;
258 	struct list_head pending_lworks;
259 
260 	struct ceph_osd_request_target t;
261 	u32 map_dne_bound;
262 
263 	struct timespec64 mtime;
264 
265 	struct kref kref;
266 	struct mutex lock;
267 	struct rb_node node;            /* osd */
268 	struct rb_node osdc_node;       /* osdc */
269 	struct rb_node mc_node;         /* map check */
270 	struct list_head scan_item;
271 
272 	struct completion reg_commit_wait;
273 	struct completion notify_finish_wait;
274 	int reg_commit_error;
275 	int notify_finish_error;
276 	int last_error;
277 
278 	u32 register_gen;
279 	u64 notify_id;
280 
281 	rados_watchcb2_t wcb;
282 	rados_watcherrcb_t errcb;
283 	void *data;
284 
285 	struct page ***preply_pages;
286 	size_t *preply_len;
287 };
288 
289 struct ceph_watch_item {
290 	struct ceph_entity_name name;
291 	u64 cookie;
292 	struct ceph_entity_addr addr;
293 };
294 
295 struct ceph_spg_mapping {
296 	struct rb_node node;
297 	struct ceph_spg spgid;
298 
299 	struct rb_root backoffs;
300 };
301 
302 struct ceph_hobject_id {
303 	void *key;
304 	size_t key_len;
305 	void *oid;
306 	size_t oid_len;
307 	u64 snapid;
308 	u32 hash;
309 	u8 is_max;
310 	void *nspace;
311 	size_t nspace_len;
312 	s64 pool;
313 
314 	/* cache */
315 	u32 hash_reverse_bits;
316 };
317 
318 static inline void ceph_hoid_build_hash_cache(struct ceph_hobject_id *hoid)
319 {
320 	hoid->hash_reverse_bits = bitrev32(hoid->hash);
321 }
322 
323 /*
324  * PG-wide backoff: [begin, end)
325  * per-object backoff: begin == end
326  */
327 struct ceph_osd_backoff {
328 	struct rb_node spg_node;
329 	struct rb_node id_node;
330 
331 	struct ceph_spg spgid;
332 	u64 id;
333 	struct ceph_hobject_id *begin;
334 	struct ceph_hobject_id *end;
335 };
336 
337 #define CEPH_LINGER_ID_START	0xffff000000000000ULL
338 
339 struct ceph_osd_client {
340 	struct ceph_client     *client;
341 
342 	struct ceph_osdmap     *osdmap;       /* current map */
343 	struct rw_semaphore    lock;
344 
345 	struct rb_root         osds;          /* osds */
346 	struct list_head       osd_lru;       /* idle osds */
347 	spinlock_t             osd_lru_lock;
348 	u32		       epoch_barrier;
349 	struct ceph_osd        homeless_osd;
350 	atomic64_t             last_tid;      /* tid of last request */
351 	u64                    last_linger_id;
352 	struct rb_root         linger_requests; /* lingering requests */
353 	struct rb_root         map_checks;
354 	struct rb_root         linger_map_checks;
355 	atomic_t               num_requests;
356 	atomic_t               num_homeless;
357 	bool                   abort_on_full; /* abort w/ ENOSPC when full */
358 	int                    abort_err;
359 	struct delayed_work    timeout_work;
360 	struct delayed_work    osds_timeout_work;
361 #ifdef CONFIG_DEBUG_FS
362 	struct dentry 	       *debugfs_file;
363 #endif
364 
365 	mempool_t              *req_mempool;
366 
367 	struct ceph_msgpool	msgpool_op;
368 	struct ceph_msgpool	msgpool_op_reply;
369 
370 	struct workqueue_struct	*notify_wq;
371 	struct workqueue_struct	*completion_wq;
372 };
373 
374 static inline bool ceph_osdmap_flag(struct ceph_osd_client *osdc, int flag)
375 {
376 	return osdc->osdmap->flags & flag;
377 }
378 
379 extern int ceph_osdc_setup(void);
380 extern void ceph_osdc_cleanup(void);
381 
382 extern int ceph_osdc_init(struct ceph_osd_client *osdc,
383 			  struct ceph_client *client);
384 extern void ceph_osdc_stop(struct ceph_osd_client *osdc);
385 
386 extern void ceph_osdc_handle_reply(struct ceph_osd_client *osdc,
387 				   struct ceph_msg *msg);
388 extern void ceph_osdc_handle_map(struct ceph_osd_client *osdc,
389 				 struct ceph_msg *msg);
390 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb);
391 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err);
392 
393 extern void osd_req_op_init(struct ceph_osd_request *osd_req,
394 			    unsigned int which, u16 opcode, u32 flags);
395 
396 extern void osd_req_op_raw_data_in_pages(struct ceph_osd_request *,
397 					unsigned int which,
398 					struct page **pages, u64 length,
399 					u32 alignment, bool pages_from_pool,
400 					bool own_pages);
401 
402 extern void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
403 					unsigned int which, u16 opcode,
404 					u64 offset, u64 length,
405 					u64 truncate_size, u32 truncate_seq);
406 extern void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
407 					unsigned int which, u64 length);
408 extern void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
409 				       unsigned int which, u64 offset_inc);
410 
411 extern struct ceph_osd_data *osd_req_op_extent_osd_data(
412 					struct ceph_osd_request *osd_req,
413 					unsigned int which);
414 
415 extern void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *,
416 					unsigned int which,
417 					struct page **pages, u64 length,
418 					u32 alignment, bool pages_from_pool,
419 					bool own_pages);
420 extern void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *,
421 					unsigned int which,
422 					struct ceph_pagelist *pagelist);
423 #ifdef CONFIG_BLOCK
424 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
425 				    unsigned int which,
426 				    struct ceph_bio_iter *bio_pos,
427 				    u32 bio_length);
428 #endif /* CONFIG_BLOCK */
429 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
430 				      unsigned int which,
431 				      struct bio_vec *bvecs, u32 num_bvecs,
432 				      u32 bytes);
433 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
434 					 unsigned int which,
435 					 struct ceph_bvec_iter *bvec_pos);
436 
437 extern void osd_req_op_cls_request_data_pagelist(struct ceph_osd_request *,
438 					unsigned int which,
439 					struct ceph_pagelist *pagelist);
440 extern void osd_req_op_cls_request_data_pages(struct ceph_osd_request *,
441 					unsigned int which,
442 					struct page **pages, u64 length,
443 					u32 alignment, bool pages_from_pool,
444 					bool own_pages);
445 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
446 				       unsigned int which,
447 				       struct bio_vec *bvecs, u32 num_bvecs,
448 				       u32 bytes);
449 extern void osd_req_op_cls_response_data_pages(struct ceph_osd_request *,
450 					unsigned int which,
451 					struct page **pages, u64 length,
452 					u32 alignment, bool pages_from_pool,
453 					bool own_pages);
454 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
455 			const char *class, const char *method);
456 extern int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
457 				 u16 opcode, const char *name, const void *value,
458 				 size_t size, u8 cmp_op, u8 cmp_mode);
459 extern void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
460 				       unsigned int which,
461 				       u64 expected_object_size,
462 				       u64 expected_write_size);
463 
464 extern struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
465 					       struct ceph_snap_context *snapc,
466 					       unsigned int num_ops,
467 					       bool use_mempool,
468 					       gfp_t gfp_flags);
469 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp);
470 
471 extern struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *,
472 				      struct ceph_file_layout *layout,
473 				      struct ceph_vino vino,
474 				      u64 offset, u64 *len,
475 				      unsigned int which, int num_ops,
476 				      int opcode, int flags,
477 				      struct ceph_snap_context *snapc,
478 				      u32 truncate_seq, u64 truncate_size,
479 				      bool use_mempool);
480 
481 extern void ceph_osdc_get_request(struct ceph_osd_request *req);
482 extern void ceph_osdc_put_request(struct ceph_osd_request *req);
483 
484 extern int ceph_osdc_start_request(struct ceph_osd_client *osdc,
485 				   struct ceph_osd_request *req,
486 				   bool nofail);
487 extern void ceph_osdc_cancel_request(struct ceph_osd_request *req);
488 extern int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
489 				  struct ceph_osd_request *req);
490 extern void ceph_osdc_sync(struct ceph_osd_client *osdc);
491 
492 extern void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc);
493 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc);
494 
495 int ceph_osdc_call(struct ceph_osd_client *osdc,
496 		   struct ceph_object_id *oid,
497 		   struct ceph_object_locator *oloc,
498 		   const char *class, const char *method,
499 		   unsigned int flags,
500 		   struct page *req_page, size_t req_len,
501 		   struct page *resp_page, size_t *resp_len);
502 
503 extern int ceph_osdc_readpages(struct ceph_osd_client *osdc,
504 			       struct ceph_vino vino,
505 			       struct ceph_file_layout *layout,
506 			       u64 off, u64 *plen,
507 			       u32 truncate_seq, u64 truncate_size,
508 			       struct page **pages, int nr_pages,
509 			       int page_align);
510 
511 extern int ceph_osdc_writepages(struct ceph_osd_client *osdc,
512 				struct ceph_vino vino,
513 				struct ceph_file_layout *layout,
514 				struct ceph_snap_context *sc,
515 				u64 off, u64 len,
516 				u32 truncate_seq, u64 truncate_size,
517 				struct timespec64 *mtime,
518 				struct page **pages, int nr_pages);
519 
520 int ceph_osdc_copy_from(struct ceph_osd_client *osdc,
521 			u64 src_snapid, u64 src_version,
522 			struct ceph_object_id *src_oid,
523 			struct ceph_object_locator *src_oloc,
524 			u32 src_fadvise_flags,
525 			struct ceph_object_id *dst_oid,
526 			struct ceph_object_locator *dst_oloc,
527 			u32 dst_fadvise_flags,
528 			u8 copy_from_flags);
529 
530 /* watch/notify */
531 struct ceph_osd_linger_request *
532 ceph_osdc_watch(struct ceph_osd_client *osdc,
533 		struct ceph_object_id *oid,
534 		struct ceph_object_locator *oloc,
535 		rados_watchcb2_t wcb,
536 		rados_watcherrcb_t errcb,
537 		void *data);
538 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
539 		      struct ceph_osd_linger_request *lreq);
540 
541 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
542 			 struct ceph_object_id *oid,
543 			 struct ceph_object_locator *oloc,
544 			 u64 notify_id,
545 			 u64 cookie,
546 			 void *payload,
547 			 u32 payload_len);
548 int ceph_osdc_notify(struct ceph_osd_client *osdc,
549 		     struct ceph_object_id *oid,
550 		     struct ceph_object_locator *oloc,
551 		     void *payload,
552 		     u32 payload_len,
553 		     u32 timeout,
554 		     struct page ***preply_pages,
555 		     size_t *preply_len);
556 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
557 			  struct ceph_osd_linger_request *lreq);
558 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
559 			    struct ceph_object_id *oid,
560 			    struct ceph_object_locator *oloc,
561 			    struct ceph_watch_item **watchers,
562 			    u32 *num_watchers);
563 #endif
564 
565