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