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